blob: a208b2a727e88e5a79bde0359fb21b536d5a59ae [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);
738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100799static void f_tolower(typval_T *argvars, typval_T *rettv);
800static void f_toupper(typval_T *argvars, typval_T *rettv);
801static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000802#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_type(typval_T *argvars, typval_T *rettv);
806static void f_undofile(typval_T *argvars, typval_T *rettv);
807static void f_undotree(typval_T *argvars, typval_T *rettv);
808static void f_uniq(typval_T *argvars, typval_T *rettv);
809static void f_values(typval_T *argvars, typval_T *rettv);
810static void f_virtcol(typval_T *argvars, typval_T *rettv);
811static void f_visualmode(typval_T *argvars, typval_T *rettv);
812static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
813static void f_winbufnr(typval_T *argvars, typval_T *rettv);
814static void f_wincol(typval_T *argvars, typval_T *rettv);
815static void f_winheight(typval_T *argvars, typval_T *rettv);
816static void f_winline(typval_T *argvars, typval_T *rettv);
817static void f_winnr(typval_T *argvars, typval_T *rettv);
818static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
819static void f_winrestview(typval_T *argvars, typval_T *rettv);
820static void f_winsaveview(typval_T *argvars, typval_T *rettv);
821static void f_winwidth(typval_T *argvars, typval_T *rettv);
822static void f_writefile(typval_T *argvars, typval_T *rettv);
823static void f_wordcount(typval_T *argvars, typval_T *rettv);
824static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000825
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100826static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
827static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
828static int get_env_len(char_u **arg);
829static int get_id_len(char_u **arg);
830static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
831static 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 +0000832#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
833#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
834 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
836static int eval_isnamec(int c);
837static int eval_isnamec1(int c);
838static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
839static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100840static typval_T *alloc_string_tv(char_u *string);
841static void init_tv(typval_T *varp);
842static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100843#ifdef FEAT_FLOAT
844static float_T get_tv_float(typval_T *varp);
845#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static linenr_T get_tv_lnum(typval_T *argvars);
847static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
848static char_u *get_tv_string(typval_T *varp);
849static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
850static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
851static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
852static hashtab_T *find_var_ht(char_u *name, char_u **varname);
853static funccall_T *get_funccal(void);
854static void vars_clear_ext(hashtab_T *ht, int free_val);
855static void delete_var(hashtab_T *ht, hashitem_T *hi);
856static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
857static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
858static void set_var(char_u *name, typval_T *varp, int copy);
859static int var_check_ro(int flags, char_u *name, int use_gettext);
860static int var_check_fixed(int flags, char_u *name, int use_gettext);
861static int var_check_func_name(char_u *name, int new_var);
862static int valid_varname(char_u *varname);
863static int tv_check_lock(int lock, char_u *name, int use_gettext);
864static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
865static char_u *find_option_end(char_u **arg, int *opt_flags);
866static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
867static int eval_fname_script(char_u *p);
868static int eval_fname_sid(char_u *p);
869static void list_func_head(ufunc_T *fp, int indent);
870static ufunc_T *find_func(char_u *name);
871static int function_exists(char_u *name);
872static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000873#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100874static void func_do_profile(ufunc_T *fp);
875static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
876static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000877static int
878# ifdef __BORLANDC__
879 _RTLENTRYF
880# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000882static int
883# ifdef __BORLANDC__
884 _RTLENTRYF
885# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100886 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000887#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888static int script_autoload(char_u *name, int reload);
889static char_u *autoload_name(char_u *name);
890static void cat_func_name(char_u *buf, ufunc_T *fp);
891static void func_free(ufunc_T *fp);
892static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
893static int can_free_funccal(funccall_T *fc, int copyID) ;
894static void free_funccal(funccall_T *fc, int free_val);
895static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
896static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
897static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
898static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
899static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
900static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
901static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int write_list(FILE *fd, list_T *list, int binary);
903static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000904
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200905
906#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100907static int compare_func_name(const void *s1, const void *s2);
908static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200909#endif
910
Bram Moolenaar33570922005-01-25 22:26:29 +0000911/*
912 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000913 */
914 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100915eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000916{
Bram Moolenaar33570922005-01-25 22:26:29 +0000917 int i;
918 struct vimvar *p;
919
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200920 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
921 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200922 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000923 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000924 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000925
926 for (i = 0; i < VV_LEN; ++i)
927 {
928 p = &vimvars[i];
929 STRCPY(p->vv_di.di_key, p->vv_name);
930 if (p->vv_flags & VV_RO)
931 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
932 else if (p->vv_flags & VV_RO_SBX)
933 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
934 else
935 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000936
937 /* add to v: scope dict, unless the value is not always available */
938 if (p->vv_type != VAR_UNKNOWN)
939 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000940 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000941 /* add to compat scope dict */
942 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100944 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
945
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000946 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100947 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200948 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100949 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100950
951 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
952 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
953 set_vim_var_nr(VV_NONE, VVAL_NONE);
954 set_vim_var_nr(VV_NULL, VVAL_NULL);
955
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200956 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200957
958#ifdef EBCDIC
959 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100960 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200961 */
962 sortFunctions();
963#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000964}
965
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000966#if defined(EXITFREE) || defined(PROTO)
967 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100968eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969{
970 int i;
971 struct vimvar *p;
972
973 for (i = 0; i < VV_LEN; ++i)
974 {
975 p = &vimvars[i];
976 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000977 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000978 vim_free(p->vv_str);
979 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000980 }
981 else if (p->vv_di.di_tv.v_type == VAR_LIST)
982 {
983 list_unref(p->vv_list);
984 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000986 }
987 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000988 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 hash_clear(&compat_hashtab);
990
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100992# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200993 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100994# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995
996 /* global variables */
997 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000999 /* autoloaded script names */
1000 ga_clear_strings(&ga_loaded);
1001
Bram Moolenaarcca74132013-09-25 21:00:28 +02001002 /* Script-local variables. First clear all the variables and in a second
1003 * loop free the scriptvar_T, because a variable in one script might hold
1004 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001005 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001006 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001007 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 ga_clear(&ga_scripts);
1010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001011 /* unreferenced lists and dicts */
1012 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001013
1014 /* functions */
1015 free_all_functions();
1016 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001017}
1018#endif
1019
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001020/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001021 * Return the name of the executed function.
1022 */
1023 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001024func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001025{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001026 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027}
1028
1029/*
1030 * Return the address holding the next breakpoint line for a funccall cookie.
1031 */
1032 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001033func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034{
Bram Moolenaar33570922005-01-25 22:26:29 +00001035 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001036}
1037
1038/*
1039 * Return the address holding the debug tick for a funccall cookie.
1040 */
1041 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001042func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043{
Bram Moolenaar33570922005-01-25 22:26:29 +00001044 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001045}
1046
1047/*
1048 * Return the nesting level for a funccall cookie.
1049 */
1050 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001051func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052{
Bram Moolenaar33570922005-01-25 22:26:29 +00001053 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001054}
1055
1056/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001057funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001059/* pointer to list of previously used funccal, still around because some
1060 * item in it is still being used. */
1061funccall_T *previous_funccal = NULL;
1062
Bram Moolenaar071d4272004-06-13 20:20:40 +00001063/*
1064 * Return TRUE when a function was ended by a ":return" command.
1065 */
1066 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001067current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001068{
1069 return current_funccal->returned;
1070}
1071
1072
1073/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074 * Set an internal variable to a string value. Creates the variable if it does
1075 * not already exist.
1076 */
1077 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001078set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001079{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001080 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001081 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082
1083 val = vim_strsave(value);
1084 if (val != NULL)
1085 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001086 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001089 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001090 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 }
1092 }
1093}
1094
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001095static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001096static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001097static char_u *redir_endp = NULL;
1098static char_u *redir_varname = NULL;
1099
1100/*
1101 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001102 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103 * Returns OK if successfully completed the setup. FAIL otherwise.
1104 */
1105 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001106var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001107{
1108 int save_emsg;
1109 int err;
1110 typval_T tv;
1111
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001112 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001113 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114 {
1115 EMSG(_(e_invarg));
1116 return FAIL;
1117 }
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 redir_varname = vim_strsave(name);
1121 if (redir_varname == NULL)
1122 return FAIL;
1123
1124 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1125 if (redir_lval == NULL)
1126 {
1127 var_redir_stop();
1128 return FAIL;
1129 }
1130
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001131 /* The output is stored in growarray "redir_ga" until redirection ends. */
1132 ga_init2(&redir_ga, (int)sizeof(char), 500);
1133
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001134 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001135 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001136 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1138 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001139 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp != NULL && *redir_endp != NUL)
1141 /* Trailing characters are present after the variable name */
1142 EMSG(_(e_trailing));
1143 else
1144 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001145 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 var_redir_stop();
1147 return FAIL;
1148 }
1149
1150 /* check if we can write to the variable: set it to or append an empty
1151 * string */
1152 save_emsg = did_emsg;
1153 did_emsg = FALSE;
1154 tv.v_type = VAR_STRING;
1155 tv.vval.v_string = (char_u *)"";
1156 if (append)
1157 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1158 else
1159 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001160 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001161 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001162 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001163 if (err)
1164 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001165 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 var_redir_stop();
1167 return FAIL;
1168 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169
1170 return OK;
1171}
1172
1173/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001174 * Append "value[value_len]" to the variable set by var_redir_start().
1175 * The actual appending is postponed until redirection ends, because the value
1176 * appended may in fact be the string we write to, changing it may cause freed
1177 * memory to be used:
1178 * :redir => foo
1179 * :let foo
1180 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001181 */
1182 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001183var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001185 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001186
1187 if (redir_lval == NULL)
1188 return;
1189
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001190 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001195 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 {
1197 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 }
1200 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202}
1203
1204/*
1205 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001206 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 */
1208 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001209var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001211 typval_T tv;
1212
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 if (redir_lval != NULL)
1214 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001215 /* If there was no error: assign the text to the variable. */
1216 if (redir_endp != NULL)
1217 {
1218 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1219 tv.v_type = VAR_STRING;
1220 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001221 /* Call get_lval() again, if it's inside a Dict or List it may
1222 * have changed. */
1223 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001224 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001225 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1226 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1227 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001229
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001230 /* free the collected output */
1231 vim_free(redir_ga.ga_data);
1232 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001233
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001234 vim_free(redir_lval);
1235 redir_lval = NULL;
1236 }
1237 vim_free(redir_varname);
1238 redir_varname = NULL;
1239}
1240
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241# if defined(FEAT_MBYTE) || defined(PROTO)
1242 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001243eval_charconvert(
1244 char_u *enc_from,
1245 char_u *enc_to,
1246 char_u *fname_from,
1247 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248{
1249 int err = FALSE;
1250
1251 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1252 set_vim_var_string(VV_CC_TO, enc_to, -1);
1253 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1254 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1255 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1256 err = TRUE;
1257 set_vim_var_string(VV_CC_FROM, NULL, -1);
1258 set_vim_var_string(VV_CC_TO, NULL, -1);
1259 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1260 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1261
1262 if (err)
1263 return FAIL;
1264 return OK;
1265}
1266# endif
1267
1268# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1269 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001270eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001271{
1272 int err = FALSE;
1273
1274 set_vim_var_string(VV_FNAME_IN, fname, -1);
1275 set_vim_var_string(VV_CMDARG, args, -1);
1276 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1277 err = TRUE;
1278 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1279 set_vim_var_string(VV_CMDARG, NULL, -1);
1280
1281 if (err)
1282 {
1283 mch_remove(fname);
1284 return FAIL;
1285 }
1286 return OK;
1287}
1288# endif
1289
1290# if defined(FEAT_DIFF) || defined(PROTO)
1291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001292eval_diff(
1293 char_u *origfile,
1294 char_u *newfile,
1295 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296{
1297 int err = FALSE;
1298
1299 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1300 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1301 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1302 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1303 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1304 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1305 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1306}
1307
1308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001309eval_patch(
1310 char_u *origfile,
1311 char_u *difffile,
1312 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001313{
1314 int err;
1315
1316 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1317 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1318 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1319 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1320 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1321 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1322 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1323}
1324# endif
1325
1326/*
1327 * Top level evaluation function, returning a boolean.
1328 * Sets "error" to TRUE if there was an error.
1329 * Return TRUE or FALSE.
1330 */
1331 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001332eval_to_bool(
1333 char_u *arg,
1334 int *error,
1335 char_u **nextcmd,
1336 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001337{
Bram Moolenaar33570922005-01-25 22:26:29 +00001338 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001339 int retval = FALSE;
1340
1341 if (skip)
1342 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001343 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 else
1346 {
1347 *error = FALSE;
1348 if (!skip)
1349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001350 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 }
1353 }
1354 if (skip)
1355 --emsg_skip;
1356
1357 return retval;
1358}
1359
1360/*
1361 * Top level evaluation function, returning a string. If "skip" is TRUE,
1362 * only parsing to "nextcmd" is done, without reporting errors. Return
1363 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1364 */
1365 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001366eval_to_string_skip(
1367 char_u *arg,
1368 char_u **nextcmd,
1369 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001370{
Bram Moolenaar33570922005-01-25 22:26:29 +00001371 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001372 char_u *retval;
1373
1374 if (skip)
1375 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001376 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377 retval = NULL;
1378 else
1379 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001380 retval = vim_strsave(get_tv_string(&tv));
1381 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001382 }
1383 if (skip)
1384 --emsg_skip;
1385
1386 return retval;
1387}
1388
1389/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001390 * Skip over an expression at "*pp".
1391 * Return FAIL for an error, OK otherwise.
1392 */
1393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001394skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001395{
Bram Moolenaar33570922005-01-25 22:26:29 +00001396 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397
1398 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001399 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400}
1401
1402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001403 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001404 * When "convert" is TRUE convert a List into a sequence of lines and convert
1405 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Return pointer to allocated memory, or NULL for failure.
1407 */
1408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001409eval_to_string(
1410 char_u *arg,
1411 char_u **nextcmd,
1412 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413{
Bram Moolenaar33570922005-01-25 22:26:29 +00001414 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001415 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001416 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001417#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001418 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001419#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001421 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 retval = NULL;
1423 else
1424 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001426 {
1427 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001428 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001429 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001430 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001431 if (tv.vval.v_list->lv_len > 0)
1432 ga_append(&ga, NL);
1433 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 ga_append(&ga, NUL);
1435 retval = (char_u *)ga.ga_data;
1436 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001437#ifdef FEAT_FLOAT
1438 else if (convert && tv.v_type == VAR_FLOAT)
1439 {
1440 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1441 retval = vim_strsave(numbuf);
1442 }
1443#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001444 else
1445 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001446 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001447 }
1448
1449 return retval;
1450}
1451
1452/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001453 * Call eval_to_string() without using current local variables and using
1454 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 */
1456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001457eval_to_string_safe(
1458 char_u *arg,
1459 char_u **nextcmd,
1460 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461{
1462 char_u *retval;
1463 void *save_funccalp;
1464
1465 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 if (use_sandbox)
1467 ++sandbox;
1468 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001469 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001470 if (use_sandbox)
1471 --sandbox;
1472 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001473 restore_funccal(save_funccalp);
1474 return retval;
1475}
1476
Bram Moolenaar071d4272004-06-13 20:20:40 +00001477/*
1478 * Top level evaluation function, returning a number.
1479 * Evaluates "expr" silently.
1480 * Returns -1 for an error.
1481 */
1482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001483eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484{
Bram Moolenaar33570922005-01-25 22:26:29 +00001485 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001487 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001488
1489 ++emsg_off;
1490
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001491 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 retval = -1;
1493 else
1494 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001495 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001496 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497 }
1498 --emsg_off;
1499
1500 return retval;
1501}
1502
Bram Moolenaara40058a2005-07-11 22:42:07 +00001503/*
1504 * Prepare v: variable "idx" to be used.
1505 * Save the current typeval in "save_tv".
1506 * When not used yet add the variable to the v: hashtable.
1507 */
1508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001509prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510{
1511 *save_tv = vimvars[idx].vv_tv;
1512 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1513 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1514}
1515
1516/*
1517 * Restore v: variable "idx" to typeval "save_tv".
1518 * When no longer defined, remove the variable from the v: hashtable.
1519 */
1520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001521restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001522{
1523 hashitem_T *hi;
1524
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525 vimvars[idx].vv_tv = *save_tv;
1526 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1527 {
1528 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1529 if (HASHITEM_EMPTY(hi))
1530 EMSG2(_(e_intern2), "restore_vimvar()");
1531 else
1532 hash_remove(&vimvarht, hi);
1533 }
1534}
1535
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001536#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001537/*
1538 * Evaluate an expression to a list with suggestions.
1539 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001540 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001541 */
1542 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001543eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544{
1545 typval_T save_val;
1546 typval_T rettv;
1547 list_T *list = NULL;
1548 char_u *p = skipwhite(expr);
1549
1550 /* Set "v:val" to the bad word. */
1551 prepare_vimvar(VV_VAL, &save_val);
1552 vimvars[VV_VAL].vv_type = VAR_STRING;
1553 vimvars[VV_VAL].vv_str = badword;
1554 if (p_verbose == 0)
1555 ++emsg_off;
1556
1557 if (eval1(&p, &rettv, TRUE) == OK)
1558 {
1559 if (rettv.v_type != VAR_LIST)
1560 clear_tv(&rettv);
1561 else
1562 list = rettv.vval.v_list;
1563 }
1564
1565 if (p_verbose == 0)
1566 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001567 restore_vimvar(VV_VAL, &save_val);
1568
1569 return list;
1570}
1571
1572/*
1573 * "list" is supposed to contain two items: a word and a number. Return the
1574 * word in "pp" and the number as the return value.
1575 * Return -1 if anything isn't right.
1576 * Used to get the good word and score from the eval_spell_expr() result.
1577 */
1578 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001579get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580{
1581 listitem_T *li;
1582
1583 li = list->lv_first;
1584 if (li == NULL)
1585 return -1;
1586 *pp = get_tv_string(&li->li_tv);
1587
1588 li = li->li_next;
1589 if (li == NULL)
1590 return -1;
1591 return get_tv_number(&li->li_tv);
1592}
1593#endif
1594
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001595/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001596 * Top level evaluation function.
1597 * Returns an allocated typval_T with the result.
1598 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001599 */
1600 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001601eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602{
1603 typval_T *tv;
1604
1605 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001606 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 {
1608 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 }
1611
1612 return tv;
1613}
1614
1615
Bram Moolenaar071d4272004-06-13 20:20:40 +00001616/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001617 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001618 * Uses argv[argc] for the function arguments. Only Number and String
1619 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001621 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001622 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001623call_vim_function(
1624 char_u *func,
1625 int argc,
1626 char_u **argv,
1627 int safe, /* use the sandbox */
1628 int str_arg_only, /* all arguments are strings */
1629 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001630{
Bram Moolenaar33570922005-01-25 22:26:29 +00001631 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001632 long n;
1633 int len;
1634 int i;
1635 int doesrange;
1636 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001637 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001639 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001641 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001642
1643 for (i = 0; i < argc; i++)
1644 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001645 /* Pass a NULL or empty argument as an empty string */
1646 if (argv[i] == NULL || *argv[i] == NUL)
1647 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001648 argvars[i].v_type = VAR_STRING;
1649 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001650 continue;
1651 }
1652
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001653 if (str_arg_only)
1654 len = 0;
1655 else
1656 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001657 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001658 if (len != 0 && len == (int)STRLEN(argv[i]))
1659 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001660 argvars[i].v_type = VAR_NUMBER;
1661 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001662 }
1663 else
1664 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001665 argvars[i].v_type = VAR_STRING;
1666 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001667 }
1668 }
1669
1670 if (safe)
1671 {
1672 save_funccalp = save_funccal();
1673 ++sandbox;
1674 }
1675
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001676 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1677 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001678 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 if (safe)
1681 {
1682 --sandbox;
1683 restore_funccal(save_funccalp);
1684 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 vim_free(argvars);
1686
1687 if (ret == FAIL)
1688 clear_tv(rettv);
1689
1690 return ret;
1691}
1692
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001693/*
1694 * Call vimL function "func" and return the result as a number.
1695 * Returns -1 when calling the function fails.
1696 * Uses argv[argc] for the function arguments.
1697 */
1698 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001699call_func_retnr(
1700 char_u *func,
1701 int argc,
1702 char_u **argv,
1703 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001704{
1705 typval_T rettv;
1706 long retval;
1707
1708 /* All arguments are passed as strings, no conversion to number. */
1709 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1710 return -1;
1711
1712 retval = get_tv_number_chk(&rettv, NULL);
1713 clear_tv(&rettv);
1714 return retval;
1715}
1716
1717#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1718 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1719
Bram Moolenaar4f688582007-07-24 12:34:30 +00001720# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001721/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001722 * Call vimL function "func" and return the result as a string.
1723 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724 * Uses argv[argc] for the function arguments.
1725 */
1726 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001727call_func_retstr(
1728 char_u *func,
1729 int argc,
1730 char_u **argv,
1731 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732{
1733 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001734 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001736 /* All arguments are passed as strings, no conversion to number. */
1737 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738 return NULL;
1739
1740 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001742 return retval;
1743}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001744# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001746/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001747 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001749 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001750 */
1751 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001752call_func_retlist(
1753 char_u *func,
1754 int argc,
1755 char_u **argv,
1756 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757{
1758 typval_T rettv;
1759
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001760 /* All arguments are passed as strings, no conversion to number. */
1761 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001762 return NULL;
1763
1764 if (rettv.v_type != VAR_LIST)
1765 {
1766 clear_tv(&rettv);
1767 return NULL;
1768 }
1769
1770 return rettv.vval.v_list;
1771}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001772#endif
1773
1774/*
1775 * Save the current function call pointer, and set it to NULL.
1776 * Used when executing autocommands and for ":source".
1777 */
1778 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001779save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001781 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001782
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783 current_funccal = NULL;
1784 return (void *)fc;
1785}
1786
1787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001788restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001790 funccall_T *fc = (funccall_T *)vfc;
1791
1792 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793}
1794
Bram Moolenaar05159a02005-02-26 23:04:13 +00001795#if defined(FEAT_PROFILE) || defined(PROTO)
1796/*
1797 * Prepare profiling for entering a child or something else that is not
1798 * counted for the script/function itself.
1799 * Should always be called in pair with prof_child_exit().
1800 */
1801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001802prof_child_enter(
1803 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001804{
1805 funccall_T *fc = current_funccal;
1806
1807 if (fc != NULL && fc->func->uf_profiling)
1808 profile_start(&fc->prof_child);
1809 script_prof_save(tm);
1810}
1811
1812/*
1813 * Take care of time spent in a child.
1814 * Should always be called after prof_child_enter().
1815 */
1816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001817prof_child_exit(
1818 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001819{
1820 funccall_T *fc = current_funccal;
1821
1822 if (fc != NULL && fc->func->uf_profiling)
1823 {
1824 profile_end(&fc->prof_child);
1825 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1826 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1827 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1828 }
1829 script_prof_restore(tm);
1830}
1831#endif
1832
1833
Bram Moolenaar071d4272004-06-13 20:20:40 +00001834#ifdef FEAT_FOLDING
1835/*
1836 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1837 * it in "*cp". Doesn't give error messages.
1838 */
1839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001840eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841{
Bram Moolenaar33570922005-01-25 22:26:29 +00001842 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001843 int retval;
1844 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001845 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1846 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847
1848 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001849 if (use_sandbox)
1850 ++sandbox;
1851 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001852 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001853 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854 retval = 0;
1855 else
1856 {
1857 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001858 if (tv.v_type == VAR_NUMBER)
1859 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001860 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a string, check if there is a non-digit before
1865 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 if (!VIM_ISDIGIT(*s) && *s != '-')
1868 *cp = *s++;
1869 retval = atol((char *)s);
1870 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001872 }
1873 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001874 if (use_sandbox)
1875 --sandbox;
1876 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001877
1878 return retval;
1879}
1880#endif
1881
Bram Moolenaar071d4272004-06-13 20:20:40 +00001882/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001883 * ":let" list all variable values
1884 * ":let var1 var2" list variable values
1885 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001886 * ":let var += expr" assignment command.
1887 * ":let var -= expr" assignment command.
1888 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890 */
1891 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001892ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893{
1894 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001896 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 int var_count = 0;
1899 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001900 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001901 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001902 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 argend = skip_var_list(arg, &var_count, &semicolon);
1905 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001906 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001907 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1908 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001909 expr = skipwhite(argend);
1910 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1911 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001912 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001913 /*
1914 * ":let" without "=": list variables
1915 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001916 if (*arg == '[')
1917 EMSG(_(e_invarg));
1918 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001919 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001920 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001921 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001922 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001923 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001924 list_glob_vars(&first);
1925 list_buf_vars(&first);
1926 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001927#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001929#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_script_vars(&first);
1931 list_func_vars(&first);
1932 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001933 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001934 eap->nextcmd = check_nextcmd(arg);
1935 }
1936 else
1937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001938 op[0] = '=';
1939 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001940 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001942 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1943 op[0] = *expr; /* +=, -= or .= */
1944 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 else
1947 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001948
Bram Moolenaar071d4272004-06-13 20:20:40 +00001949 if (eap->skip)
1950 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001951 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 {
1954 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 --emsg_skip;
1957 }
1958 else if (i != FAIL)
1959 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001960 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001961 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 }
1964 }
1965}
1966
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967/*
1968 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1969 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001970 * When "nextchars" is not NULL it points to a string with characters that
1971 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1972 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 * Returns OK or FAIL;
1974 */
1975 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001976ex_let_vars(
1977 char_u *arg_start,
1978 typval_T *tv,
1979 int copy, /* copy values from "tv", don't move */
1980 int semicolon, /* from skip_var_list() */
1981 int var_count, /* from skip_var_list() */
1982 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001983{
1984 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001985 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001987 listitem_T *item;
1988 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989
1990 if (*arg != '[')
1991 {
1992 /*
1993 * ":let var = expr" or ":for var in list"
1994 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001995 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996 return FAIL;
1997 return OK;
1998 }
1999
2000 /*
2001 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2002 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002003 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 {
2005 EMSG(_(e_listreq));
2006 return FAIL;
2007 }
2008
2009 i = list_len(l);
2010 if (semicolon == 0 && var_count < i)
2011 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002012 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002013 return FAIL;
2014 }
2015 if (var_count - semicolon > i)
2016 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002017 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002018 return FAIL;
2019 }
2020
2021 item = l->lv_first;
2022 while (*arg != ']')
2023 {
2024 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002025 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 item = item->li_next;
2027 if (arg == NULL)
2028 return FAIL;
2029
2030 arg = skipwhite(arg);
2031 if (*arg == ';')
2032 {
2033 /* Put the rest of the list (may be empty) in the var after ';'.
2034 * Create a new list for this. */
2035 l = list_alloc();
2036 if (l == NULL)
2037 return FAIL;
2038 while (item != NULL)
2039 {
2040 list_append_tv(l, &item->li_tv);
2041 item = item->li_next;
2042 }
2043
2044 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002045 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002046 ltv.vval.v_list = l;
2047 l->lv_refcount = 1;
2048
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002049 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2050 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002051 clear_tv(&ltv);
2052 if (arg == NULL)
2053 return FAIL;
2054 break;
2055 }
2056 else if (*arg != ',' && *arg != ']')
2057 {
2058 EMSG2(_(e_intern2), "ex_let_vars()");
2059 return FAIL;
2060 }
2061 }
2062
2063 return OK;
2064}
2065
2066/*
2067 * Skip over assignable variable "var" or list of variables "[var, var]".
2068 * Used for ":let varvar = expr" and ":for varvar in expr".
2069 * For "[var, var]" increment "*var_count" for each variable.
2070 * for "[var, var; var]" set "semicolon".
2071 * Return NULL for an error.
2072 */
2073 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002074skip_var_list(
2075 char_u *arg,
2076 int *var_count,
2077 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002078{
2079 char_u *p, *s;
2080
2081 if (*arg == '[')
2082 {
2083 /* "[var, var]": find the matching ']'. */
2084 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002085 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086 {
2087 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2088 s = skip_var_one(p);
2089 if (s == p)
2090 {
2091 EMSG2(_(e_invarg2), p);
2092 return NULL;
2093 }
2094 ++*var_count;
2095
2096 p = skipwhite(s);
2097 if (*p == ']')
2098 break;
2099 else if (*p == ';')
2100 {
2101 if (*semicolon == 1)
2102 {
2103 EMSG(_("Double ; in list of variables"));
2104 return NULL;
2105 }
2106 *semicolon = 1;
2107 }
2108 else if (*p != ',')
2109 {
2110 EMSG2(_(e_invarg2), p);
2111 return NULL;
2112 }
2113 }
2114 return p + 1;
2115 }
2116 else
2117 return skip_var_one(arg);
2118}
2119
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002120/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002121 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002122 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002124 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002125skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002126{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002127 if (*arg == '@' && arg[1] != NUL)
2128 return arg + 2;
2129 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2130 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131}
2132
Bram Moolenaara7043832005-01-21 11:56:39 +00002133/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002134 * List variables for hashtab "ht" with prefix "prefix".
2135 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002136 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138list_hashtable_vars(
2139 hashtab_T *ht,
2140 char_u *prefix,
2141 int empty,
2142 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002143{
Bram Moolenaar33570922005-01-25 22:26:29 +00002144 hashitem_T *hi;
2145 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002146 int todo;
2147
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002148 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2150 {
2151 if (!HASHITEM_EMPTY(hi))
2152 {
2153 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002154 di = HI2DI(hi);
2155 if (empty || di->di_tv.v_type != VAR_STRING
2156 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002157 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002158 }
2159 }
2160}
2161
2162/*
2163 * List global variables.
2164 */
2165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002166list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002167{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002168 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002169}
2170
2171/*
2172 * List buffer variables.
2173 */
2174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002175list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002176{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002177 char_u numbuf[NUMBUFLEN];
2178
Bram Moolenaar429fa852013-04-15 12:27:36 +02002179 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002180 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002181
2182 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2184 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002185}
2186
2187/*
2188 * List window variables.
2189 */
2190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002191list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002192{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002193 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002194 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002195}
2196
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002197#ifdef FEAT_WINDOWS
2198/*
2199 * List tab page variables.
2200 */
2201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002202list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002204 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002205 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206}
2207#endif
2208
Bram Moolenaara7043832005-01-21 11:56:39 +00002209/*
2210 * List Vim variables.
2211 */
2212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002213list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002214{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002215 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002216}
2217
2218/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002219 * List script-local variables, if there is a script.
2220 */
2221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002222list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002223{
2224 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002225 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2226 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227}
2228
2229/*
2230 * List function variables, if there is a function.
2231 */
2232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002233list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234{
2235 if (current_funccal != NULL)
2236 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002237 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002238}
2239
2240/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002241 * List variables in "arg".
2242 */
2243 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002244list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002245{
2246 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002247 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002249 char_u *name_start;
2250 char_u *arg_subsc;
2251 char_u *tofree;
2252 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253
2254 while (!ends_excmd(*arg) && !got_int)
2255 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002257 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002258 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2260 {
2261 emsg_severe = TRUE;
2262 EMSG(_(e_trailing));
2263 break;
2264 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002267 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002268 /* get_name_len() takes care of expanding curly braces */
2269 name_start = name = arg;
2270 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2271 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 /* This is mainly to keep test 49 working: when expanding
2274 * curly braces fails overrule the exception error message. */
2275 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002276 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002277 emsg_severe = TRUE;
2278 EMSG2(_(e_invarg2), arg);
2279 break;
2280 }
2281 error = TRUE;
2282 }
2283 else
2284 {
2285 if (tofree != NULL)
2286 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002287 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002288 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 else
2290 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002291 /* handle d.key, l[idx], f(expr) */
2292 arg_subsc = arg;
2293 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002301 case 'g': list_glob_vars(first); break;
2302 case 'b': list_buf_vars(first); break;
2303 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002304#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002305 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002306#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'v': list_vim_vars(first); break;
2308 case 's': list_script_vars(first); break;
2309 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 default:
2311 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002312 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 }
2314 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002315 {
2316 char_u numbuf[NUMBUFLEN];
2317 char_u *tf;
2318 int c;
2319 char_u *s;
2320
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002321 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 c = *arg;
2323 *arg = NUL;
2324 list_one_var_a((char_u *)"",
2325 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002326 tv.v_type,
2327 s == NULL ? (char_u *)"" : s,
2328 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 *arg = c;
2330 vim_free(tf);
2331 }
2332 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002333 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002334 }
2335 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336
2337 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342
2343 return arg;
2344}
2345
2346/*
2347 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2348 * Returns a pointer to the char just after the var name.
2349 * Returns NULL if there is an error.
2350 */
2351 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002352ex_let_one(
2353 char_u *arg, /* points to variable name */
2354 typval_T *tv, /* value to assign to variable */
2355 int copy, /* copy value from "tv" */
2356 char_u *endchars, /* valid chars after variable name or NULL */
2357 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002358{
2359 int c1;
2360 char_u *name;
2361 char_u *p;
2362 char_u *arg_end = NULL;
2363 int len;
2364 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002365 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366
2367 /*
2368 * ":let $VAR = expr": Set environment variable.
2369 */
2370 if (*arg == '$')
2371 {
2372 /* Find the end of the name. */
2373 ++arg;
2374 name = arg;
2375 len = get_env_len(&arg);
2376 if (len == 0)
2377 EMSG2(_(e_invarg2), name - 1);
2378 else
2379 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002380 if (op != NULL && (*op == '+' || *op == '-'))
2381 EMSG2(_(e_letwrong), op);
2382 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002383 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002384 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002385 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002386 {
2387 c1 = name[len];
2388 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002389 p = get_tv_string_chk(tv);
2390 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002391 {
2392 int mustfree = FALSE;
2393 char_u *s = vim_getenv(name, &mustfree);
2394
2395 if (s != NULL)
2396 {
2397 p = tofree = concat_str(s, p);
2398 if (mustfree)
2399 vim_free(s);
2400 }
2401 }
2402 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002403 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002405 if (STRICMP(name, "HOME") == 0)
2406 init_homedir();
2407 else if (didset_vim && STRICMP(name, "VIM") == 0)
2408 didset_vim = FALSE;
2409 else if (didset_vimruntime
2410 && STRICMP(name, "VIMRUNTIME") == 0)
2411 didset_vimruntime = FALSE;
2412 arg_end = arg;
2413 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002414 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002415 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002416 }
2417 }
2418 }
2419
2420 /*
2421 * ":let &option = expr": Set option value.
2422 * ":let &l:option = expr": Set local option value.
2423 * ":let &g:option = expr": Set global option value.
2424 */
2425 else if (*arg == '&')
2426 {
2427 /* Find the end of the name. */
2428 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002429 if (p == NULL || (endchars != NULL
2430 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002431 EMSG(_(e_letunexp));
2432 else
2433 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002434 long n;
2435 int opt_type;
2436 long numval;
2437 char_u *stringval = NULL;
2438 char_u *s;
2439
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002440 c1 = *p;
2441 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442
2443 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002444 s = get_tv_string_chk(tv); /* != NULL if number or string */
2445 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002446 {
2447 opt_type = get_option_value(arg, &numval,
2448 &stringval, opt_flags);
2449 if ((opt_type == 1 && *op == '.')
2450 || (opt_type == 0 && *op != '.'))
2451 EMSG2(_(e_letwrong), op);
2452 else
2453 {
2454 if (opt_type == 1) /* number */
2455 {
2456 if (*op == '+')
2457 n = numval + n;
2458 else
2459 n = numval - n;
2460 }
2461 else if (opt_type == 0 && stringval != NULL) /* string */
2462 {
2463 s = concat_str(stringval, s);
2464 vim_free(stringval);
2465 stringval = s;
2466 }
2467 }
2468 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002469 if (s != NULL)
2470 {
2471 set_option_value(arg, n, s, opt_flags);
2472 arg_end = p;
2473 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002474 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002475 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002476 }
2477 }
2478
2479 /*
2480 * ":let @r = expr": Set register contents.
2481 */
2482 else if (*arg == '@')
2483 {
2484 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002485 if (op != NULL && (*op == '+' || *op == '-'))
2486 EMSG2(_(e_letwrong), op);
2487 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002488 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 EMSG(_(e_letunexp));
2490 else
2491 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002492 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 char_u *s;
2494
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002495 p = get_tv_string_chk(tv);
2496 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002497 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002498 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 if (s != NULL)
2500 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002501 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 vim_free(s);
2503 }
2504 }
2505 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002506 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 arg_end = arg + 1;
2509 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002510 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002511 }
2512 }
2513
2514 /*
2515 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002516 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002518 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002520 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002521
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002522 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002525 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2526 EMSG(_(e_letunexp));
2527 else
2528 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002529 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 arg_end = p;
2531 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534 }
2535
2536 else
2537 EMSG2(_(e_invarg2), arg);
2538
2539 return arg_end;
2540}
2541
2542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002543 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2544 */
2545 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002546check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002547{
2548 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2549 {
2550 EMSG2(_(e_readonlyvar), arg);
2551 return TRUE;
2552 }
2553 return FALSE;
2554}
2555
2556/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002557 * Get an lval: variable, Dict item or List item that can be assigned a value
2558 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2559 * "name.key", "name.key[expr]" etc.
2560 * Indexing only works if "name" is an existing List or Dictionary.
2561 * "name" points to the start of the name.
2562 * If "rettv" is not NULL it points to the value to be assigned.
2563 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2564 * wrong; must end in space or cmd separator.
2565 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002566 * flags:
2567 * GLV_QUIET: do not give error messages
2568 * GLV_NO_AUTOLOAD: do not use script autoloading
2569 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002571 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002572 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002573 */
2574 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002575get_lval(
2576 char_u *name,
2577 typval_T *rettv,
2578 lval_T *lp,
2579 int unlet,
2580 int skip,
2581 int flags, /* GLV_ values */
2582 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002583{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002584 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 char_u *expr_start, *expr_end;
2586 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002587 dictitem_T *v;
2588 typval_T var1;
2589 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002590 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002591 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002592 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002593 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002595 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002599
2600 if (skip)
2601 {
2602 /* When skipping just find the end of the name. */
2603 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002604 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 }
2606
2607 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002608 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002609 if (expr_start != NULL)
2610 {
2611 /* Don't expand the name when we already know there is an error. */
2612 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2613 && *p != '[' && *p != '.')
2614 {
2615 EMSG(_(e_trailing));
2616 return NULL;
2617 }
2618
2619 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2620 if (lp->ll_exp_name == NULL)
2621 {
2622 /* Report an invalid expression in braces, unless the
2623 * expression evaluation has been cancelled due to an
2624 * aborting error, an interrupt, or an exception. */
2625 if (!aborting() && !quiet)
2626 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002627 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002628 EMSG2(_(e_invarg2), name);
2629 return NULL;
2630 }
2631 }
2632 lp->ll_name = lp->ll_exp_name;
2633 }
2634 else
2635 lp->ll_name = name;
2636
2637 /* Without [idx] or .key we are done. */
2638 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2639 return p;
2640
2641 cc = *p;
2642 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002643 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002644 if (v == NULL && !quiet)
2645 EMSG2(_(e_undefvar), lp->ll_name);
2646 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002647 if (v == NULL)
2648 return NULL;
2649
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 /*
2651 * Loop until no more [idx] or .key is following.
2652 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002653 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002654 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2657 && !(lp->ll_tv->v_type == VAR_DICT
2658 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002659 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 if (!quiet)
2661 EMSG(_("E689: Can only index a List or Dictionary"));
2662 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E708: [:] must come last"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002670
Bram Moolenaar8c711452005-01-14 21:53:12 +00002671 len = -1;
2672 if (*p == '.')
2673 {
2674 key = p + 1;
2675 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2676 ;
2677 if (len == 0)
2678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_(e_emptykey));
2681 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002682 }
2683 p = key + len;
2684 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002685 else
2686 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002687 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 if (*p == ':')
2690 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 empty1 = FALSE;
2694 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002695 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002696 if (get_tv_string_chk(&var1) == NULL)
2697 {
2698 /* not a number or string */
2699 clear_tv(&var1);
2700 return NULL;
2701 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 }
2703
2704 /* Optionally get the second index [ :expr]. */
2705 if (*p == ':')
2706 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002707 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002709 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002710 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002711 if (!empty1)
2712 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (rettv != NULL && (rettv->v_type != VAR_LIST
2716 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (!quiet)
2719 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 if (!empty1)
2721 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 }
2724 p = skipwhite(p + 1);
2725 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 else
2728 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2731 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002732 if (!empty1)
2733 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002736 if (get_tv_string_chk(&var2) == NULL)
2737 {
2738 /* not a number or string */
2739 if (!empty1)
2740 clear_tv(&var1);
2741 clear_tv(&var2);
2742 return NULL;
2743 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002744 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002745 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002751 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 if (!quiet)
2753 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 if (!empty1)
2755 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002759 }
2760
2761 /* Skip to past ']'. */
2762 ++p;
2763 }
2764
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 {
2767 if (len == -1)
2768 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002770 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002771 if (*key == NUL)
2772 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (!quiet)
2774 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002775 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 }
2778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002779 lp->ll_list = NULL;
2780 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002781 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002782
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002783 /* When assigning to a scope dictionary check that a function and
2784 * variable name is valid (only variable name unless it is l: or
2785 * g: dictionary). Disallow overwriting a builtin function. */
2786 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002787 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002788 int prevval;
2789 int wrong;
2790
2791 if (len != -1)
2792 {
2793 prevval = key[len];
2794 key[len] = NUL;
2795 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002796 else
2797 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002798 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2799 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 || !valid_varname(key);
2802 if (len != -1)
2803 key[len] = prevval;
2804 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002805 return NULL;
2806 }
2807
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002808 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002809 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002810 /* Can't add "v:" variable. */
2811 if (lp->ll_dict == &vimvardict)
2812 {
2813 EMSG2(_(e_illvar), name);
2814 return NULL;
2815 }
2816
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002817 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002818 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002819 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002820 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002821 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 if (len == -1)
2823 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 }
2826 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002829 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 p = NULL;
2834 break;
2835 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002836 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002837 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002838 return NULL;
2839
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 if (len == -1)
2841 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 }
2844 else
2845 {
2846 /*
2847 * Get the number and item for the only or first index of the List.
2848 */
2849 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 else
2852 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002853 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 clear_tv(&var1);
2855 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002856 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_list = lp->ll_tv->vval.v_list;
2858 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2859 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002861 if (lp->ll_n1 < 0)
2862 {
2863 lp->ll_n1 = 0;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 }
2866 }
2867 if (lp->ll_li == NULL)
2868 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002869 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002870 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002871 if (!quiet)
2872 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002873 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002874 }
2875
2876 /*
2877 * May need to find the item or absolute index for the second
2878 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 * When no index given: "lp->ll_empty2" is TRUE.
2880 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002884 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002885 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002890 {
2891 if (!quiet)
2892 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002894 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 }
2897
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2899 if (lp->ll_n1 < 0)
2900 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2901 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 {
2903 if (!quiet)
2904 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002906 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002907 }
2908
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002909 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002911 }
2912
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return p;
2914}
2915
2916/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002917 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 */
2919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002920clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921{
2922 vim_free(lp->ll_exp_name);
2923 vim_free(lp->ll_newkey);
2924}
2925
2926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002927 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002929 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002930 */
2931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002932set_var_lval(
2933 lval_T *lp,
2934 char_u *endp,
2935 typval_T *rettv,
2936 int copy,
2937 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938{
2939 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002940 listitem_T *ri;
2941 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002942
2943 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002944 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002946 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002947 cc = *endp;
2948 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002949 if (op != NULL && *op != '=')
2950 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002951 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002953 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002954 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002955 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002956 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002958 if ((di == NULL
2959 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2960 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2961 FALSE)))
2962 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 set_var(lp->ll_name, &tv, FALSE);
2964 clear_tv(&tv);
2965 }
2966 }
2967 else
2968 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002969 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002970 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002971 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002972 else if (tv_check_lock(lp->ll_newkey == NULL
2973 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002974 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 else if (lp->ll_range)
2977 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002978 listitem_T *ll_li = lp->ll_li;
2979 int ll_n1 = lp->ll_n1;
2980
2981 /*
2982 * Check whether any of the list items is locked
2983 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002984 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002986 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002987 return;
2988 ri = ri->li_next;
2989 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2990 break;
2991 ll_li = ll_li->li_next;
2992 ++ll_n1;
2993 }
2994
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002995 /*
2996 * Assign the List values to the list items.
2997 */
2998 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002999 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003000 if (op != NULL && *op != '=')
3001 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3002 else
3003 {
3004 clear_tv(&lp->ll_li->li_tv);
3005 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3006 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003007 ri = ri->li_next;
3008 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3009 break;
3010 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003011 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003012 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003013 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = NULL;
3016 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 lp->ll_li = lp->ll_li->li_next;
3020 ++lp->ll_n1;
3021 }
3022 if (ri != NULL)
3023 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003024 else if (lp->ll_empty2
3025 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 : lp->ll_n1 != lp->ll_n2)
3027 EMSG(_("E711: List value has not enough items"));
3028 }
3029 else
3030 {
3031 /*
3032 * Assign to a List or Dictionary item.
3033 */
3034 if (lp->ll_newkey != NULL)
3035 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003036 if (op != NULL && *op != '=')
3037 {
3038 EMSG2(_(e_letwrong), op);
3039 return;
3040 }
3041
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003042 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003043 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003044 if (di == NULL)
3045 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3047 {
3048 vim_free(di);
3049 return;
3050 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003052 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003053 else if (op != NULL && *op != '=')
3054 {
3055 tv_op(lp->ll_tv, rettv, op);
3056 return;
3057 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003058 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003060
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003061 /*
3062 * Assign the value to the variable or list item.
3063 */
3064 if (copy)
3065 copy_tv(rettv, lp->ll_tv);
3066 else
3067 {
3068 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003069 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003070 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 }
3072 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003073}
3074
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003075/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003076 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3077 * Returns OK or FAIL.
3078 */
3079 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003080tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003081{
3082 long n;
3083 char_u numbuf[NUMBUFLEN];
3084 char_u *s;
3085
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003086 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3087 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3088 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 {
3090 switch (tv1->v_type)
3091 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003092 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003093 case VAR_DICT:
3094 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003095 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003096 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003097 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003098 break;
3099
3100 case VAR_LIST:
3101 if (*op != '+' || tv2->v_type != VAR_LIST)
3102 break;
3103 /* List += List */
3104 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3105 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3106 return OK;
3107
3108 case VAR_NUMBER:
3109 case VAR_STRING:
3110 if (tv2->v_type == VAR_LIST)
3111 break;
3112 if (*op == '+' || *op == '-')
3113 {
3114 /* nr += nr or nr -= nr*/
3115 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003116#ifdef FEAT_FLOAT
3117 if (tv2->v_type == VAR_FLOAT)
3118 {
3119 float_T f = n;
3120
3121 if (*op == '+')
3122 f += tv2->vval.v_float;
3123 else
3124 f -= tv2->vval.v_float;
3125 clear_tv(tv1);
3126 tv1->v_type = VAR_FLOAT;
3127 tv1->vval.v_float = f;
3128 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003129 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#endif
3131 {
3132 if (*op == '+')
3133 n += get_tv_number(tv2);
3134 else
3135 n -= get_tv_number(tv2);
3136 clear_tv(tv1);
3137 tv1->v_type = VAR_NUMBER;
3138 tv1->vval.v_number = n;
3139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003140 }
3141 else
3142 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003143 if (tv2->v_type == VAR_FLOAT)
3144 break;
3145
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 /* str .= str */
3147 s = get_tv_string(tv1);
3148 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3149 clear_tv(tv1);
3150 tv1->v_type = VAR_STRING;
3151 tv1->vval.v_string = s;
3152 }
3153 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003154
3155#ifdef FEAT_FLOAT
3156 case VAR_FLOAT:
3157 {
3158 float_T f;
3159
3160 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3161 && tv2->v_type != VAR_NUMBER
3162 && tv2->v_type != VAR_STRING))
3163 break;
3164 if (tv2->v_type == VAR_FLOAT)
3165 f = tv2->vval.v_float;
3166 else
3167 f = get_tv_number(tv2);
3168 if (*op == '+')
3169 tv1->vval.v_float += f;
3170 else
3171 tv1->vval.v_float -= f;
3172 }
3173 return OK;
3174#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003175 }
3176 }
3177
3178 EMSG2(_(e_letwrong), op);
3179 return FAIL;
3180}
3181
3182/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003183 * Add a watcher to a list.
3184 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003185 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003186list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187{
3188 lw->lw_next = l->lv_watch;
3189 l->lv_watch = lw;
3190}
3191
3192/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003193 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194 * No warning when it isn't found...
3195 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003196 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003197list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198{
Bram Moolenaar33570922005-01-25 22:26:29 +00003199 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200
3201 lwp = &l->lv_watch;
3202 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3203 {
3204 if (lw == lwrem)
3205 {
3206 *lwp = lw->lw_next;
3207 break;
3208 }
3209 lwp = &lw->lw_next;
3210 }
3211}
3212
3213/*
3214 * Just before removing an item from a list: advance watchers to the next
3215 * item.
3216 */
3217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003218list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003219{
Bram Moolenaar33570922005-01-25 22:26:29 +00003220 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003221
3222 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3223 if (lw->lw_item == item)
3224 lw->lw_item = item->li_next;
3225}
3226
3227/*
3228 * Evaluate the expression used in a ":for var in expr" command.
3229 * "arg" points to "var".
3230 * Set "*errp" to TRUE for an error, FALSE otherwise;
3231 * Return a pointer that holds the info. Null when there is an error.
3232 */
3233 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003234eval_for_line(
3235 char_u *arg,
3236 int *errp,
3237 char_u **nextcmdp,
3238 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003239{
Bram Moolenaar33570922005-01-25 22:26:29 +00003240 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003241 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003242 typval_T tv;
3243 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003244
3245 *errp = TRUE; /* default: there is an error */
3246
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 if (fi == NULL)
3249 return NULL;
3250
3251 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3252 if (expr == NULL)
3253 return fi;
3254
3255 expr = skipwhite(expr);
3256 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3257 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003258 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003259 return fi;
3260 }
3261
3262 if (skip)
3263 ++emsg_skip;
3264 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3265 {
3266 *errp = FALSE;
3267 if (!skip)
3268 {
3269 l = tv.vval.v_list;
3270 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003271 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003272 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003273 clear_tv(&tv);
3274 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003275 else
3276 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003277 /* No need to increment the refcount, it's already set for the
3278 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 fi->fi_list = l;
3280 list_add_watch(l, &fi->fi_lw);
3281 fi->fi_lw.lw_item = l->lv_first;
3282 }
3283 }
3284 }
3285 if (skip)
3286 --emsg_skip;
3287
3288 return fi;
3289}
3290
3291/*
3292 * Use the first item in a ":for" list. Advance to the next.
3293 * Assign the values to the variable (list). "arg" points to the first one.
3294 * Return TRUE when a valid item was found, FALSE when at end of list or
3295 * something wrong.
3296 */
3297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003298next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003299{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003300 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003301 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003302 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303
3304 item = fi->fi_lw.lw_item;
3305 if (item == NULL)
3306 result = FALSE;
3307 else
3308 {
3309 fi->fi_lw.lw_item = item->li_next;
3310 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3311 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3312 }
3313 return result;
3314}
3315
3316/*
3317 * Free the structure used to store info used by ":for".
3318 */
3319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003320free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003321{
Bram Moolenaar33570922005-01-25 22:26:29 +00003322 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003323
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003324 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003325 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003326 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003327 list_unref(fi->fi_list);
3328 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329 vim_free(fi);
3330}
3331
Bram Moolenaar071d4272004-06-13 20:20:40 +00003332#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3333
3334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003335set_context_for_expression(
3336 expand_T *xp,
3337 char_u *arg,
3338 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339{
3340 int got_eq = FALSE;
3341 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003342 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003344 if (cmdidx == CMD_let)
3345 {
3346 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003347 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 {
3349 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003350 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 {
3352 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003353 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 if (vim_iswhite(*p))
3355 break;
3356 }
3357 return;
3358 }
3359 }
3360 else
3361 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3362 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003363 while ((xp->xp_pattern = vim_strpbrk(arg,
3364 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3365 {
3366 c = *xp->xp_pattern;
3367 if (c == '&')
3368 {
3369 c = xp->xp_pattern[1];
3370 if (c == '&')
3371 {
3372 ++xp->xp_pattern;
3373 xp->xp_context = cmdidx != CMD_let || got_eq
3374 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3375 }
3376 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003377 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003378 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003379 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3380 xp->xp_pattern += 2;
3381
3382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003383 }
3384 else if (c == '$')
3385 {
3386 /* environment variable */
3387 xp->xp_context = EXPAND_ENV_VARS;
3388 }
3389 else if (c == '=')
3390 {
3391 got_eq = TRUE;
3392 xp->xp_context = EXPAND_EXPRESSION;
3393 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003394 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003395 && xp->xp_context == EXPAND_FUNCTIONS
3396 && vim_strchr(xp->xp_pattern, '(') == NULL)
3397 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 break;
3400 }
3401 else if (cmdidx != CMD_let || got_eq)
3402 {
3403 if (c == '"') /* string */
3404 {
3405 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3406 if (c == '\\' && xp->xp_pattern[1] != NUL)
3407 ++xp->xp_pattern;
3408 xp->xp_context = EXPAND_NOTHING;
3409 }
3410 else if (c == '\'') /* literal string */
3411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003412 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3414 /* skip */ ;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '|')
3418 {
3419 if (xp->xp_pattern[1] == '|')
3420 {
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_EXPRESSION;
3423 }
3424 else
3425 xp->xp_context = EXPAND_COMMANDS;
3426 }
3427 else
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003431 /* Doesn't look like something valid, expand as an expression
3432 * anyway. */
3433 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003434 arg = xp->xp_pattern;
3435 if (*arg != NUL)
3436 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3437 /* skip */ ;
3438 }
3439 xp->xp_pattern = arg;
3440}
3441
3442#endif /* FEAT_CMDL_COMPL */
3443
3444/*
3445 * ":1,25call func(arg1, arg2)" function call.
3446 */
3447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003448ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449{
3450 char_u *arg = eap->arg;
3451 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003452 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003453 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003455 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 linenr_T lnum;
3457 int doesrange;
3458 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003461 if (eap->skip)
3462 {
3463 /* trans_function_name() doesn't work well when skipping, use eval0()
3464 * instead to skip to any following command, e.g. for:
3465 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003466 ++emsg_skip;
3467 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3468 clear_tv(&rettv);
3469 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003470 return;
3471 }
3472
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003473 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003474 if (fudi.fd_newkey != NULL)
3475 {
3476 /* Still need to give an error message for missing key. */
3477 EMSG2(_(e_dictkey), fudi.fd_newkey);
3478 vim_free(fudi.fd_newkey);
3479 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003480 if (tofree == NULL)
3481 return;
3482
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003483 /* Increase refcount on dictionary, it could get deleted when evaluating
3484 * the arguments. */
3485 if (fudi.fd_dict != NULL)
3486 ++fudi.fd_dict->dv_refcount;
3487
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003489 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003490 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003491
Bram Moolenaar532c7802005-01-27 14:44:31 +00003492 /* Skip white space to allow ":call func ()". Not good, but required for
3493 * backward compatibility. */
3494 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003495 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003496
3497 if (*startarg != '(')
3498 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003499 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 goto end;
3501 }
3502
3503 /*
3504 * When skipping, evaluate the function once, to find the end of the
3505 * arguments.
3506 * When the function takes a range, this is discovered after the first
3507 * call, and the loop is broken.
3508 */
3509 if (eap->skip)
3510 {
3511 ++emsg_skip;
3512 lnum = eap->line2; /* do it once, also with an invalid range */
3513 }
3514 else
3515 lnum = eap->line1;
3516 for ( ; lnum <= eap->line2; ++lnum)
3517 {
3518 if (!eap->skip && eap->addr_count > 0)
3519 {
3520 curwin->w_cursor.lnum = lnum;
3521 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003522#ifdef FEAT_VIRTUALEDIT
3523 curwin->w_cursor.coladd = 0;
3524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003525 }
3526 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003527 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003528 eap->line1, eap->line2, &doesrange,
3529 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 {
3531 failed = TRUE;
3532 break;
3533 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003534
3535 /* Handle a function returning a Funcref, Dictionary or List. */
3536 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3537 {
3538 failed = TRUE;
3539 break;
3540 }
3541
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003542 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 if (doesrange || eap->skip)
3544 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
Bram Moolenaar071d4272004-06-13 20:20:40 +00003546 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003547 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003549 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003550 if (aborting())
3551 break;
3552 }
3553 if (eap->skip)
3554 --emsg_skip;
3555
3556 if (!failed)
3557 {
3558 /* Check for trailing illegal characters and a following command. */
3559 if (!ends_excmd(*arg))
3560 {
3561 emsg_severe = TRUE;
3562 EMSG(_(e_trailing));
3563 }
3564 else
3565 eap->nextcmd = check_nextcmd(arg);
3566 }
3567
3568end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003569 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003570 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003571}
3572
3573/*
3574 * ":unlet[!] var1 ... " command.
3575 */
3576 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003577ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003579 ex_unletlock(eap, eap->arg, 0);
3580}
3581
3582/*
3583 * ":lockvar" and ":unlockvar" commands
3584 */
3585 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003586ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003588 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003589 int deep = 2;
3590
3591 if (eap->forceit)
3592 deep = -1;
3593 else if (vim_isdigit(*arg))
3594 {
3595 deep = getdigits(&arg);
3596 arg = skipwhite(arg);
3597 }
3598
3599 ex_unletlock(eap, arg, deep);
3600}
3601
3602/*
3603 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3604 */
3605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003606ex_unletlock(
3607 exarg_T *eap,
3608 char_u *argstart,
3609 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003610{
3611 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003613 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003614 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003615
3616 do
3617 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003618 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003619 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003620 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003621 if (lv.ll_name == NULL)
3622 error = TRUE; /* error but continue parsing */
3623 if (name_end == NULL || (!vim_iswhite(*name_end)
3624 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 if (name_end != NULL)
3627 {
3628 emsg_severe = TRUE;
3629 EMSG(_(e_trailing));
3630 }
3631 if (!(eap->skip || error))
3632 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 break;
3634 }
3635
3636 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003637 {
3638 if (eap->cmdidx == CMD_unlet)
3639 {
3640 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3641 error = TRUE;
3642 }
3643 else
3644 {
3645 if (do_lock_var(&lv, name_end, deep,
3646 eap->cmdidx == CMD_lockvar) == FAIL)
3647 error = TRUE;
3648 }
3649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003650
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003651 if (!eap->skip)
3652 clear_lval(&lv);
3653
Bram Moolenaar071d4272004-06-13 20:20:40 +00003654 arg = skipwhite(name_end);
3655 } while (!ends_excmd(*arg));
3656
3657 eap->nextcmd = check_nextcmd(arg);
3658}
3659
Bram Moolenaar8c711452005-01-14 21:53:12 +00003660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003661do_unlet_var(
3662 lval_T *lp,
3663 char_u *name_end,
3664 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003665{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003666 int ret = OK;
3667 int cc;
3668
3669 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003670 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003671 cc = *name_end;
3672 *name_end = NUL;
3673
3674 /* Normal name or expanded name. */
3675 if (check_changedtick(lp->ll_name))
3676 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003677 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003678 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003680 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003681 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003682 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003683 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003684 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 else if (lp->ll_range)
3687 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003688 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003689 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003690 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003691
3692 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3693 {
3694 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003696 return FAIL;
3697 ll_li = li;
3698 ++ll_n1;
3699 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700
3701 /* Delete a range of List items. */
3702 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3703 {
3704 li = lp->ll_li->li_next;
3705 listitem_remove(lp->ll_list, lp->ll_li);
3706 lp->ll_li = li;
3707 ++lp->ll_n1;
3708 }
3709 }
3710 else
3711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003712 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003713 /* unlet a List item. */
3714 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003715 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003716 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003717 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 }
3719
3720 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003721}
3722
Bram Moolenaar071d4272004-06-13 20:20:40 +00003723/*
3724 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003725 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003726 */
3727 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003728do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729{
Bram Moolenaar33570922005-01-25 22:26:29 +00003730 hashtab_T *ht;
3731 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003732 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003733 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003734 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 ht = find_var_ht(name, &varname);
3737 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003738 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003739 if (ht == &globvarht)
3740 d = &globvardict;
3741 else if (current_funccal != NULL
3742 && ht == &current_funccal->l_vars.dv_hashtab)
3743 d = &current_funccal->l_vars;
3744 else if (ht == &compat_hashtab)
3745 d = &vimvardict;
3746 else
3747 {
3748 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3749 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3750 }
3751 if (d == NULL)
3752 {
3753 EMSG2(_(e_intern2), "do_unlet()");
3754 return FAIL;
3755 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003756 hi = hash_find(ht, varname);
3757 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003758 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003759 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003760 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003761 || var_check_ro(di->di_flags, name, FALSE)
3762 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003763 return FAIL;
3764
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003765 delete_var(ht, hi);
3766 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003767 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003768 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003769 if (forceit)
3770 return OK;
3771 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003772 return FAIL;
3773}
3774
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775/*
3776 * Lock or unlock variable indicated by "lp".
3777 * "deep" is the levels to go (-1 for unlimited);
3778 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3779 */
3780 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003781do_lock_var(
3782 lval_T *lp,
3783 char_u *name_end,
3784 int deep,
3785 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786{
3787 int ret = OK;
3788 int cc;
3789 dictitem_T *di;
3790
3791 if (deep == 0) /* nothing to do */
3792 return OK;
3793
3794 if (lp->ll_tv == NULL)
3795 {
3796 cc = *name_end;
3797 *name_end = NUL;
3798
3799 /* Normal name or expanded name. */
3800 if (check_changedtick(lp->ll_name))
3801 ret = FAIL;
3802 else
3803 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003804 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003805 if (di == NULL)
3806 ret = FAIL;
3807 else
3808 {
3809 if (lock)
3810 di->di_flags |= DI_FLAGS_LOCK;
3811 else
3812 di->di_flags &= ~DI_FLAGS_LOCK;
3813 item_lock(&di->di_tv, deep, lock);
3814 }
3815 }
3816 *name_end = cc;
3817 }
3818 else if (lp->ll_range)
3819 {
3820 listitem_T *li = lp->ll_li;
3821
3822 /* (un)lock a range of List items. */
3823 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3824 {
3825 item_lock(&li->li_tv, deep, lock);
3826 li = li->li_next;
3827 ++lp->ll_n1;
3828 }
3829 }
3830 else if (lp->ll_list != NULL)
3831 /* (un)lock a List item. */
3832 item_lock(&lp->ll_li->li_tv, deep, lock);
3833 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003834 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003835 item_lock(&lp->ll_di->di_tv, deep, lock);
3836
3837 return ret;
3838}
3839
3840/*
3841 * Lock or unlock an item. "deep" is nr of levels to go.
3842 */
3843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003844item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003845{
3846 static int recurse = 0;
3847 list_T *l;
3848 listitem_T *li;
3849 dict_T *d;
3850 hashitem_T *hi;
3851 int todo;
3852
3853 if (recurse >= DICT_MAXNEST)
3854 {
3855 EMSG(_("E743: variable nested too deep for (un)lock"));
3856 return;
3857 }
3858 if (deep == 0)
3859 return;
3860 ++recurse;
3861
3862 /* lock/unlock the item itself */
3863 if (lock)
3864 tv->v_lock |= VAR_LOCKED;
3865 else
3866 tv->v_lock &= ~VAR_LOCKED;
3867
3868 switch (tv->v_type)
3869 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003870 case VAR_UNKNOWN:
3871 case VAR_NUMBER:
3872 case VAR_STRING:
3873 case VAR_FUNC:
3874 case VAR_FLOAT:
3875 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003876 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003877 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 break;
3879
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003880 case VAR_LIST:
3881 if ((l = tv->vval.v_list) != NULL)
3882 {
3883 if (lock)
3884 l->lv_lock |= VAR_LOCKED;
3885 else
3886 l->lv_lock &= ~VAR_LOCKED;
3887 if (deep < 0 || deep > 1)
3888 /* recursive: lock/unlock the items the List contains */
3889 for (li = l->lv_first; li != NULL; li = li->li_next)
3890 item_lock(&li->li_tv, deep - 1, lock);
3891 }
3892 break;
3893 case VAR_DICT:
3894 if ((d = tv->vval.v_dict) != NULL)
3895 {
3896 if (lock)
3897 d->dv_lock |= VAR_LOCKED;
3898 else
3899 d->dv_lock &= ~VAR_LOCKED;
3900 if (deep < 0 || deep > 1)
3901 {
3902 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003903 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003904 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3905 {
3906 if (!HASHITEM_EMPTY(hi))
3907 {
3908 --todo;
3909 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3910 }
3911 }
3912 }
3913 }
3914 }
3915 --recurse;
3916}
3917
Bram Moolenaara40058a2005-07-11 22:42:07 +00003918/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003919 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3920 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003921 */
3922 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003923tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924{
3925 return (tv->v_lock & VAR_LOCKED)
3926 || (tv->v_type == VAR_LIST
3927 && tv->vval.v_list != NULL
3928 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3929 || (tv->v_type == VAR_DICT
3930 && tv->vval.v_dict != NULL
3931 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3932}
3933
Bram Moolenaar071d4272004-06-13 20:20:40 +00003934#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3935/*
3936 * Delete all "menutrans_" variables.
3937 */
3938 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003939del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940{
Bram Moolenaar33570922005-01-25 22:26:29 +00003941 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003942 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003943
Bram Moolenaar33570922005-01-25 22:26:29 +00003944 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003945 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003946 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003947 {
3948 if (!HASHITEM_EMPTY(hi))
3949 {
3950 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003951 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3952 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 }
3954 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003955 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003956}
3957#endif
3958
3959#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3960
3961/*
3962 * Local string buffer for the next two functions to store a variable name
3963 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3964 * get_user_var_name().
3965 */
3966
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003967static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
3969static char_u *varnamebuf = NULL;
3970static int varnamebuflen = 0;
3971
3972/*
3973 * Function to concatenate a prefix and a variable name.
3974 */
3975 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003976cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003977{
3978 int len;
3979
3980 len = (int)STRLEN(name) + 3;
3981 if (len > varnamebuflen)
3982 {
3983 vim_free(varnamebuf);
3984 len += 10; /* some additional space */
3985 varnamebuf = alloc(len);
3986 if (varnamebuf == NULL)
3987 {
3988 varnamebuflen = 0;
3989 return NULL;
3990 }
3991 varnamebuflen = len;
3992 }
3993 *varnamebuf = prefix;
3994 varnamebuf[1] = ':';
3995 STRCPY(varnamebuf + 2, name);
3996 return varnamebuf;
3997}
3998
3999/*
4000 * Function given to ExpandGeneric() to obtain the list of user defined
4001 * (global/buffer/window/built-in) variable names.
4002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004003 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004004get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004005{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004006 static long_u gdone;
4007 static long_u bdone;
4008 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004009#ifdef FEAT_WINDOWS
4010 static long_u tdone;
4011#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static int vidx;
4013 static hashitem_T *hi;
4014 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015
4016 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004018 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004019#ifdef FEAT_WINDOWS
4020 tdone = 0;
4021#endif
4022 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004023
4024 /* Global variables */
4025 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004026 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004027 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004028 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004029 else
4030 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004031 while (HASHITEM_EMPTY(hi))
4032 ++hi;
4033 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4034 return cat_prefix_varname('g', hi->hi_key);
4035 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004036 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004037
4038 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004039 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004050 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 return (char_u *)"b:changedtick";
4054 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004055
4056 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004057 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004060 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004061 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004062 else
4063 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 while (HASHITEM_EMPTY(hi))
4065 ++hi;
4066 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004068
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004069#ifdef FEAT_WINDOWS
4070 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004071 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004072 if (tdone < ht->ht_used)
4073 {
4074 if (tdone++ == 0)
4075 hi = ht->ht_array;
4076 else
4077 ++hi;
4078 while (HASHITEM_EMPTY(hi))
4079 ++hi;
4080 return cat_prefix_varname('t', hi->hi_key);
4081 }
4082#endif
4083
Bram Moolenaar33570922005-01-25 22:26:29 +00004084 /* v: variables */
4085 if (vidx < VV_LEN)
4086 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004087
4088 vim_free(varnamebuf);
4089 varnamebuf = NULL;
4090 varnamebuflen = 0;
4091 return NULL;
4092}
4093
4094#endif /* FEAT_CMDL_COMPL */
4095
4096/*
4097 * types for expressions.
4098 */
4099typedef enum
4100{
4101 TYPE_UNKNOWN = 0
4102 , TYPE_EQUAL /* == */
4103 , TYPE_NEQUAL /* != */
4104 , TYPE_GREATER /* > */
4105 , TYPE_GEQUAL /* >= */
4106 , TYPE_SMALLER /* < */
4107 , TYPE_SEQUAL /* <= */
4108 , TYPE_MATCH /* =~ */
4109 , TYPE_NOMATCH /* !~ */
4110} exptype_T;
4111
4112/*
4113 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004114 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004115 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4116 */
4117
4118/*
4119 * Handle zero level expression.
4120 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004121 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004122 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * Return OK or FAIL.
4124 */
4125 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004126eval0(
4127 char_u *arg,
4128 typval_T *rettv,
4129 char_u **nextcmd,
4130 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131{
4132 int ret;
4133 char_u *p;
4134
4135 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137 if (ret == FAIL || !ends_excmd(*p))
4138 {
4139 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004140 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 /*
4142 * Report the invalid expression unless the expression evaluation has
4143 * been cancelled due to an aborting error, an interrupt, or an
4144 * exception.
4145 */
4146 if (!aborting())
4147 EMSG2(_(e_invexpr2), arg);
4148 ret = FAIL;
4149 }
4150 if (nextcmd != NULL)
4151 *nextcmd = check_nextcmd(p);
4152
4153 return ret;
4154}
4155
4156/*
4157 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004158 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 *
4160 * "arg" must point to the first non-white of the expression.
4161 * "arg" is advanced to the next non-white after the recognized expression.
4162 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004163 * Note: "rettv.v_lock" is not set.
4164 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * Return OK or FAIL.
4166 */
4167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004168eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004169{
4170 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004171 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004172
4173 /*
4174 * Get the first variable.
4175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004176 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 return FAIL;
4178
4179 if ((*arg)[0] == '?')
4180 {
4181 result = FALSE;
4182 if (evaluate)
4183 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004184 int error = FALSE;
4185
4186 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004189 if (error)
4190 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 }
4192
4193 /*
4194 * Get the second variable.
4195 */
4196 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004197 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004198 return FAIL;
4199
4200 /*
4201 * Check for the ":".
4202 */
4203 if ((*arg)[0] != ':')
4204 {
4205 EMSG(_("E109: Missing ':' after '?'"));
4206 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004207 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004208 return FAIL;
4209 }
4210
4211 /*
4212 * Get the third variable.
4213 */
4214 *arg = skipwhite(*arg + 1);
4215 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4216 {
4217 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004218 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219 return FAIL;
4220 }
4221 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 }
4224
4225 return OK;
4226}
4227
4228/*
4229 * Handle first level expression:
4230 * expr2 || expr2 || expr2 logical OR
4231 *
4232 * "arg" must point to the first non-white of the expression.
4233 * "arg" is advanced to the next non-white after the recognized expression.
4234 *
4235 * Return OK or FAIL.
4236 */
4237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004238eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004239{
Bram Moolenaar33570922005-01-25 22:26:29 +00004240 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 long result;
4242 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004243 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004244
4245 /*
4246 * Get the first variable.
4247 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004248 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 return FAIL;
4250
4251 /*
4252 * Repeat until there is no following "||".
4253 */
4254 first = TRUE;
4255 result = FALSE;
4256 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4257 {
4258 if (evaluate && first)
4259 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004260 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004263 if (error)
4264 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004265 first = FALSE;
4266 }
4267
4268 /*
4269 * Get the second variable.
4270 */
4271 *arg = skipwhite(*arg + 2);
4272 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4273 return FAIL;
4274
4275 /*
4276 * Compute the result.
4277 */
4278 if (evaluate && !result)
4279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004280 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004281 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004282 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004283 if (error)
4284 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 }
4286 if (evaluate)
4287 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 rettv->v_type = VAR_NUMBER;
4289 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004290 }
4291 }
4292
4293 return OK;
4294}
4295
4296/*
4297 * Handle second level expression:
4298 * expr3 && expr3 && expr3 logical AND
4299 *
4300 * "arg" must point to the first non-white of the expression.
4301 * "arg" is advanced to the next non-white after the recognized expression.
4302 *
4303 * Return OK or FAIL.
4304 */
4305 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004306eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004307{
Bram Moolenaar33570922005-01-25 22:26:29 +00004308 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 long result;
4310 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004311 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004312
4313 /*
4314 * Get the first variable.
4315 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004316 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 return FAIL;
4318
4319 /*
4320 * Repeat until there is no following "&&".
4321 */
4322 first = TRUE;
4323 result = TRUE;
4324 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4325 {
4326 if (evaluate && first)
4327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004328 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004330 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004331 if (error)
4332 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004333 first = FALSE;
4334 }
4335
4336 /*
4337 * Get the second variable.
4338 */
4339 *arg = skipwhite(*arg + 2);
4340 if (eval4(arg, &var2, evaluate && result) == FAIL)
4341 return FAIL;
4342
4343 /*
4344 * Compute the result.
4345 */
4346 if (evaluate && result)
4347 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004348 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004349 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004350 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004351 if (error)
4352 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 }
4354 if (evaluate)
4355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 rettv->v_type = VAR_NUMBER;
4357 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004358 }
4359 }
4360
4361 return OK;
4362}
4363
4364/*
4365 * Handle third level expression:
4366 * var1 == var2
4367 * var1 =~ var2
4368 * var1 != var2
4369 * var1 !~ var2
4370 * var1 > var2
4371 * var1 >= var2
4372 * var1 < var2
4373 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004374 * var1 is var2
4375 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004376 *
4377 * "arg" must point to the first non-white of the expression.
4378 * "arg" is advanced to the next non-white after the recognized expression.
4379 *
4380 * Return OK or FAIL.
4381 */
4382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004383eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384{
Bram Moolenaar33570922005-01-25 22:26:29 +00004385 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004386 char_u *p;
4387 int i;
4388 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390 int len = 2;
4391 long n1, n2;
4392 char_u *s1, *s2;
4393 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4394 regmatch_T regmatch;
4395 int ic;
4396 char_u *save_cpo;
4397
4398 /*
4399 * Get the first variable.
4400 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004401 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 return FAIL;
4403
4404 p = *arg;
4405 switch (p[0])
4406 {
4407 case '=': if (p[1] == '=')
4408 type = TYPE_EQUAL;
4409 else if (p[1] == '~')
4410 type = TYPE_MATCH;
4411 break;
4412 case '!': if (p[1] == '=')
4413 type = TYPE_NEQUAL;
4414 else if (p[1] == '~')
4415 type = TYPE_NOMATCH;
4416 break;
4417 case '>': if (p[1] != '=')
4418 {
4419 type = TYPE_GREATER;
4420 len = 1;
4421 }
4422 else
4423 type = TYPE_GEQUAL;
4424 break;
4425 case '<': if (p[1] != '=')
4426 {
4427 type = TYPE_SMALLER;
4428 len = 1;
4429 }
4430 else
4431 type = TYPE_SEQUAL;
4432 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 case 'i': if (p[1] == 's')
4434 {
4435 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4436 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004437 i = p[len];
4438 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 {
4440 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4441 type_is = TRUE;
4442 }
4443 }
4444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445 }
4446
4447 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004448 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004449 */
4450 if (type != TYPE_UNKNOWN)
4451 {
4452 /* extra question mark appended: ignore case */
4453 if (p[len] == '?')
4454 {
4455 ic = TRUE;
4456 ++len;
4457 }
4458 /* extra '#' appended: match case */
4459 else if (p[len] == '#')
4460 {
4461 ic = FALSE;
4462 ++len;
4463 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004464 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465 else
4466 ic = p_ic;
4467
4468 /*
4469 * Get the second variable.
4470 */
4471 *arg = skipwhite(p + len);
4472 if (eval5(arg, &var2, evaluate) == FAIL)
4473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004474 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004475 return FAIL;
4476 }
4477
4478 if (evaluate)
4479 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004480 if (type_is && rettv->v_type != var2.v_type)
4481 {
4482 /* For "is" a different type always means FALSE, for "notis"
4483 * it means TRUE. */
4484 n1 = (type == TYPE_NEQUAL);
4485 }
4486 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4487 {
4488 if (type_is)
4489 {
4490 n1 = (rettv->v_type == var2.v_type
4491 && rettv->vval.v_list == var2.vval.v_list);
4492 if (type == TYPE_NEQUAL)
4493 n1 = !n1;
4494 }
4495 else if (rettv->v_type != var2.v_type
4496 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4497 {
4498 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004499 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004500 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004501 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004502 clear_tv(rettv);
4503 clear_tv(&var2);
4504 return FAIL;
4505 }
4506 else
4507 {
4508 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004509 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4510 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004511 if (type == TYPE_NEQUAL)
4512 n1 = !n1;
4513 }
4514 }
4515
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004516 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4517 {
4518 if (type_is)
4519 {
4520 n1 = (rettv->v_type == var2.v_type
4521 && rettv->vval.v_dict == var2.vval.v_dict);
4522 if (type == TYPE_NEQUAL)
4523 n1 = !n1;
4524 }
4525 else if (rettv->v_type != var2.v_type
4526 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4527 {
4528 if (rettv->v_type != var2.v_type)
4529 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4530 else
4531 EMSG(_("E736: Invalid operation for Dictionary"));
4532 clear_tv(rettv);
4533 clear_tv(&var2);
4534 return FAIL;
4535 }
4536 else
4537 {
4538 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004539 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4540 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004541 if (type == TYPE_NEQUAL)
4542 n1 = !n1;
4543 }
4544 }
4545
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004546 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4547 {
4548 if (rettv->v_type != var2.v_type
4549 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4550 {
4551 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004552 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004554 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004555 clear_tv(rettv);
4556 clear_tv(&var2);
4557 return FAIL;
4558 }
4559 else
4560 {
4561 /* Compare two Funcrefs for being equal or unequal. */
4562 if (rettv->vval.v_string == NULL
4563 || var2.vval.v_string == NULL)
4564 n1 = FALSE;
4565 else
4566 n1 = STRCMP(rettv->vval.v_string,
4567 var2.vval.v_string) == 0;
4568 if (type == TYPE_NEQUAL)
4569 n1 = !n1;
4570 }
4571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
5234 /* If "s" is the name of a variable of type VAR_FUNC
5235 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005236 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237
5238 /* Invoke the function. */
5239 ret = get_func_tv(s, len, rettv, arg,
5240 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005241 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005242
5243 /* If evaluate is FALSE rettv->v_type was not set in
5244 * get_func_tv, but it's needed in handle_subscript() to parse
5245 * what follows. So set it here. */
5246 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5247 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005248 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005249 rettv->v_type = VAR_FUNC;
5250 }
5251
Bram Moolenaar8c711452005-01-14 21:53:12 +00005252 /* Stop the expression evaluation when immediately
5253 * aborting on error, or when an interrupt occurred or
5254 * an exception was thrown but not caught. */
5255 if (aborting())
5256 {
5257 if (ret == OK)
5258 clear_tv(rettv);
5259 ret = FAIL;
5260 }
5261 }
5262 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005263 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005264 else
5265 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005266 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005267 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
5269
Bram Moolenaar071d4272004-06-13 20:20:40 +00005270 *arg = skipwhite(*arg);
5271
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005272 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5273 * expr(expr). */
5274 if (ret == OK)
5275 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276
5277 /*
5278 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5279 */
5280 if (ret == OK && evaluate && end_leader > start_leader)
5281 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005282 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005283 int val = 0;
5284#ifdef FEAT_FLOAT
5285 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005286
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005287 if (rettv->v_type == VAR_FLOAT)
5288 f = rettv->vval.v_float;
5289 else
5290#endif
5291 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005293 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 clear_tv(rettv);
5295 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005296 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005297 else
5298 {
5299 while (end_leader > start_leader)
5300 {
5301 --end_leader;
5302 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005303 {
5304#ifdef FEAT_FLOAT
5305 if (rettv->v_type == VAR_FLOAT)
5306 f = !f;
5307 else
5308#endif
5309 val = !val;
5310 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005311 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005312 {
5313#ifdef FEAT_FLOAT
5314 if (rettv->v_type == VAR_FLOAT)
5315 f = -f;
5316 else
5317#endif
5318 val = -val;
5319 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005320 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 {
5324 clear_tv(rettv);
5325 rettv->vval.v_float = f;
5326 }
5327 else
5328#endif
5329 {
5330 clear_tv(rettv);
5331 rettv->v_type = VAR_NUMBER;
5332 rettv->vval.v_number = val;
5333 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005334 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005335 }
5336
5337 return ret;
5338}
5339
5340/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005341 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5342 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005343 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5344 */
5345 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005346eval_index(
5347 char_u **arg,
5348 typval_T *rettv,
5349 int evaluate,
5350 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351{
5352 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005353 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005354 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005355 long len = -1;
5356 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005358 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359
Bram Moolenaara03f2332016-02-06 18:09:59 +01005360 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 case VAR_FUNC:
5363 if (verbose)
5364 EMSG(_("E695: Cannot index a Funcref"));
5365 return FAIL;
5366 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005367#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 if (verbose)
5369 EMSG(_(e_float_as_string));
5370 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005371#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005372 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005374 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 if (verbose)
5376 EMSG(_("E909: Cannot index a special variable"));
5377 return FAIL;
5378 case VAR_UNKNOWN:
5379 if (evaluate)
5380 return FAIL;
5381 /* FALLTHROUGH */
5382
5383 case VAR_STRING:
5384 case VAR_NUMBER:
5385 case VAR_LIST:
5386 case VAR_DICT:
5387 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005388 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005389
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005390 init_tv(&var1);
5391 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005392 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005393 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005394 /*
5395 * dict.name
5396 */
5397 key = *arg + 1;
5398 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5399 ;
5400 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005403 }
5404 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005405 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005406 /*
5407 * something[idx]
5408 *
5409 * Get the (first) variable from inside the [].
5410 */
5411 *arg = skipwhite(*arg + 1);
5412 if (**arg == ':')
5413 empty1 = TRUE;
5414 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5415 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005416 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5417 {
5418 /* not a number or string */
5419 clear_tv(&var1);
5420 return FAIL;
5421 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422
5423 /*
5424 * Get the second variable from inside the [:].
5425 */
5426 if (**arg == ':')
5427 {
5428 range = TRUE;
5429 *arg = skipwhite(*arg + 1);
5430 if (**arg == ']')
5431 empty2 = TRUE;
5432 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005434 if (!empty1)
5435 clear_tv(&var1);
5436 return FAIL;
5437 }
5438 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5439 {
5440 /* not a number or string */
5441 if (!empty1)
5442 clear_tv(&var1);
5443 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444 return FAIL;
5445 }
5446 }
5447
5448 /* Check for the ']'. */
5449 if (**arg != ']')
5450 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005451 if (verbose)
5452 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005453 clear_tv(&var1);
5454 if (range)
5455 clear_tv(&var2);
5456 return FAIL;
5457 }
5458 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005459 }
5460
5461 if (evaluate)
5462 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005463 n1 = 0;
5464 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005466 n1 = get_tv_number(&var1);
5467 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 }
5469 if (range)
5470 {
5471 if (empty2)
5472 n2 = -1;
5473 else
5474 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005475 n2 = get_tv_number(&var2);
5476 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005477 }
5478 }
5479
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005480 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005482 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005483 case VAR_FUNC:
5484 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_SPECIAL:
5486 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005487 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 break; /* not evaluating, skipping over subscript */
5489
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 case VAR_NUMBER:
5491 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005492 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 len = (long)STRLEN(s);
5494 if (range)
5495 {
5496 /* The resulting variable is a substring. If the indexes
5497 * are out of range the result is empty. */
5498 if (n1 < 0)
5499 {
5500 n1 = len + n1;
5501 if (n1 < 0)
5502 n1 = 0;
5503 }
5504 if (n2 < 0)
5505 n2 = len + n2;
5506 else if (n2 >= len)
5507 n2 = len;
5508 if (n1 >= len || n2 < 0 || n1 > n2)
5509 s = NULL;
5510 else
5511 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5512 }
5513 else
5514 {
5515 /* The resulting variable is a string of a single
5516 * character. If the index is too big or negative the
5517 * result is empty. */
5518 if (n1 >= len || n1 < 0)
5519 s = NULL;
5520 else
5521 s = vim_strnsave(s + n1, 1);
5522 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005523 clear_tv(rettv);
5524 rettv->v_type = VAR_STRING;
5525 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005526 break;
5527
5528 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 if (n1 < 0)
5531 n1 = len + n1;
5532 if (!empty1 && (n1 < 0 || n1 >= len))
5533 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005534 /* For a range we allow invalid values and return an empty
5535 * list. A list index out of range is an error. */
5536 if (!range)
5537 {
5538 if (verbose)
5539 EMSGN(_(e_listidx), n1);
5540 return FAIL;
5541 }
5542 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005543 }
5544 if (range)
5545 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005546 list_T *l;
5547 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005548
5549 if (n2 < 0)
5550 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005551 else if (n2 >= len)
5552 n2 = len - 1;
5553 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005554 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 l = list_alloc();
5556 if (l == NULL)
5557 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 n1 <= n2; ++n1)
5560 {
5561 if (list_append_tv(l, &item->li_tv) == FAIL)
5562 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005563 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005564 return FAIL;
5565 }
5566 item = item->li_next;
5567 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005568 clear_tv(rettv);
5569 rettv->v_type = VAR_LIST;
5570 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005571 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 else
5574 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005575 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 clear_tv(rettv);
5577 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005580
5581 case VAR_DICT:
5582 if (range)
5583 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005584 if (verbose)
5585 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586 if (len == -1)
5587 clear_tv(&var1);
5588 return FAIL;
5589 }
5590 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005591 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592
5593 if (len == -1)
5594 {
5595 key = get_tv_string(&var1);
5596 if (*key == NUL)
5597 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005598 if (verbose)
5599 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600 clear_tv(&var1);
5601 return FAIL;
5602 }
5603 }
5604
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005605 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005608 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (len == -1)
5610 clear_tv(&var1);
5611 if (item == NULL)
5612 return FAIL;
5613
5614 copy_tv(&item->di_tv, &var1);
5615 clear_tv(rettv);
5616 *rettv = var1;
5617 }
5618 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005619 }
5620 }
5621
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005622 return OK;
5623}
5624
5625/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005626 * Get an option value.
5627 * "arg" points to the '&' or '+' before the option name.
5628 * "arg" is advanced to character after the option name.
5629 * Return OK or FAIL.
5630 */
5631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005632get_option_tv(
5633 char_u **arg,
5634 typval_T *rettv, /* when NULL, only check if option exists */
5635 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005636{
5637 char_u *option_end;
5638 long numval;
5639 char_u *stringval;
5640 int opt_type;
5641 int c;
5642 int working = (**arg == '+'); /* has("+option") */
5643 int ret = OK;
5644 int opt_flags;
5645
5646 /*
5647 * Isolate the option name and find its value.
5648 */
5649 option_end = find_option_end(arg, &opt_flags);
5650 if (option_end == NULL)
5651 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005652 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005653 EMSG2(_("E112: Option name missing: %s"), *arg);
5654 return FAIL;
5655 }
5656
5657 if (!evaluate)
5658 {
5659 *arg = option_end;
5660 return OK;
5661 }
5662
5663 c = *option_end;
5664 *option_end = NUL;
5665 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005666 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005667
5668 if (opt_type == -3) /* invalid name */
5669 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671 EMSG2(_("E113: Unknown option: %s"), *arg);
5672 ret = FAIL;
5673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 {
5676 if (opt_type == -2) /* hidden string option */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 rettv->v_type = VAR_STRING;
5679 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005680 }
5681 else if (opt_type == -1) /* hidden number option */
5682 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005683 rettv->v_type = VAR_NUMBER;
5684 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005685 }
5686 else if (opt_type == 1) /* number option */
5687 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005688 rettv->v_type = VAR_NUMBER;
5689 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690 }
5691 else /* string option */
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 rettv->v_type = VAR_STRING;
5694 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005695 }
5696 }
5697 else if (working && (opt_type == -2 || opt_type == -1))
5698 ret = FAIL;
5699
5700 *option_end = c; /* put back for error messages */
5701 *arg = option_end;
5702
5703 return ret;
5704}
5705
5706/*
5707 * Allocate a variable for a string constant.
5708 * Return OK or FAIL.
5709 */
5710 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005711get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005712{
5713 char_u *p;
5714 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005715 int extra = 0;
5716
5717 /*
5718 * Find the end of the string, skipping backslashed characters.
5719 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005720 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 {
5722 if (*p == '\\' && p[1] != NUL)
5723 {
5724 ++p;
5725 /* A "\<x>" form occupies at least 4 characters, and produces up
5726 * to 6 characters: reserve space for 2 extra */
5727 if (*p == '<')
5728 extra += 2;
5729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005730 }
5731
5732 if (*p != '"')
5733 {
5734 EMSG2(_("E114: Missing quote: %s"), *arg);
5735 return FAIL;
5736 }
5737
5738 /* If only parsing, set *arg and return here */
5739 if (!evaluate)
5740 {
5741 *arg = p + 1;
5742 return OK;
5743 }
5744
5745 /*
5746 * Copy the string into allocated memory, handling backslashed
5747 * characters.
5748 */
5749 name = alloc((unsigned)(p - *arg + extra));
5750 if (name == NULL)
5751 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005752 rettv->v_type = VAR_STRING;
5753 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005754
Bram Moolenaar8c711452005-01-14 21:53:12 +00005755 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 {
5757 if (*p == '\\')
5758 {
5759 switch (*++p)
5760 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 case 'b': *name++ = BS; ++p; break;
5762 case 'e': *name++ = ESC; ++p; break;
5763 case 'f': *name++ = FF; ++p; break;
5764 case 'n': *name++ = NL; ++p; break;
5765 case 'r': *name++ = CAR; ++p; break;
5766 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005767
5768 case 'X': /* hex: "\x1", "\x12" */
5769 case 'x':
5770 case 'u': /* Unicode: "\u0023" */
5771 case 'U':
5772 if (vim_isxdigit(p[1]))
5773 {
5774 int n, nr;
5775 int c = toupper(*p);
5776
5777 if (c == 'X')
5778 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005779 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005781 else
5782 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783 nr = 0;
5784 while (--n >= 0 && vim_isxdigit(p[1]))
5785 {
5786 ++p;
5787 nr = (nr << 4) + hex2nr(*p);
5788 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005789 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790#ifdef FEAT_MBYTE
5791 /* For "\u" store the number according to
5792 * 'encoding'. */
5793 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005794 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005795 else
5796#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 break;
5800
5801 /* octal: "\1", "\12", "\123" */
5802 case '0':
5803 case '1':
5804 case '2':
5805 case '3':
5806 case '4':
5807 case '5':
5808 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005809 case '7': *name = *p++ - '0';
5810 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005811 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 *name = (*name << 3) + *p++ - '0';
5813 if (*p >= '0' && *p <= '7')
5814 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 break;
5818
5819 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 if (extra != 0)
5822 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 break;
5825 }
5826 /* FALLTHROUGH */
5827
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 break;
5830 }
5831 }
5832 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005833 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 *arg = p + 1;
5838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 return OK;
5840}
5841
5842/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 * Return OK or FAIL.
5845 */
5846 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005847get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848{
5849 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005850 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005851 int reduce = 0;
5852
5853 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 */
5856 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5857 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 break;
5862 ++reduce;
5863 ++p;
5864 }
5865 }
5866
Bram Moolenaar8c711452005-01-14 21:53:12 +00005867 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005868 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005870 return FAIL;
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 if (!evaluate)
5875 {
5876 *arg = p + 1;
5877 return OK;
5878 }
5879
5880 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 */
5883 str = alloc((unsigned)((p - *arg) - reduce));
5884 if (str == NULL)
5885 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 rettv->v_type = VAR_STRING;
5887 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005891 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 break;
5895 ++p;
5896 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 *arg = p + 1;
5901
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 return OK;
5903}
5904
5905/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005906 * Allocate a variable for a List and fill it from "*arg".
5907 * Return OK or FAIL.
5908 */
5909 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005910get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005911{
Bram Moolenaar33570922005-01-25 22:26:29 +00005912 list_T *l = NULL;
5913 typval_T tv;
5914 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915
5916 if (evaluate)
5917 {
5918 l = list_alloc();
5919 if (l == NULL)
5920 return FAIL;
5921 }
5922
5923 *arg = skipwhite(*arg + 1);
5924 while (**arg != ']' && **arg != NUL)
5925 {
5926 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5927 goto failret;
5928 if (evaluate)
5929 {
5930 item = listitem_alloc();
5931 if (item != NULL)
5932 {
5933 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005934 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 list_append(l, item);
5936 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005937 else
5938 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 }
5940
5941 if (**arg == ']')
5942 break;
5943 if (**arg != ',')
5944 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005945 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005946 goto failret;
5947 }
5948 *arg = skipwhite(*arg + 1);
5949 }
5950
5951 if (**arg != ']')
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954failret:
5955 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005956 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005957 return FAIL;
5958 }
5959
5960 *arg = skipwhite(*arg + 1);
5961 if (evaluate)
5962 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005963 rettv->v_type = VAR_LIST;
5964 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 ++l->lv_refcount;
5966 }
5967
5968 return OK;
5969}
5970
5971/*
5972 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005973 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005974 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005975 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005976list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005978 list_T *l;
5979
5980 l = (list_T *)alloc_clear(sizeof(list_T));
5981 if (l != NULL)
5982 {
5983 /* Prepend the list to the list of lists for garbage collection. */
5984 if (first_list != NULL)
5985 first_list->lv_used_prev = l;
5986 l->lv_used_prev = NULL;
5987 l->lv_used_next = first_list;
5988 first_list = l;
5989 }
5990 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991}
5992
5993/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005994 * Allocate an empty list for a return value.
5995 * Returns OK or FAIL.
5996 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005997 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005998rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005999{
6000 list_T *l = list_alloc();
6001
6002 if (l == NULL)
6003 return FAIL;
6004
6005 rettv->vval.v_list = l;
6006 rettv->v_type = VAR_LIST;
6007 ++l->lv_refcount;
6008 return OK;
6009}
6010
6011/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006012 * Unreference a list: decrement the reference count and free it when it
6013 * becomes zero.
6014 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006015 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006016list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006018 if (l != NULL && --l->lv_refcount <= 0)
6019 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006023 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024 * Ignores the reference count.
6025 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027list_free(
6028 list_T *l,
6029 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030{
Bram Moolenaar33570922005-01-25 22:26:29 +00006031 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006033 /* Remove the list from the list of lists for garbage collection. */
6034 if (l->lv_used_prev == NULL)
6035 first_list = l->lv_used_next;
6036 else
6037 l->lv_used_prev->lv_used_next = l->lv_used_next;
6038 if (l->lv_used_next != NULL)
6039 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6040
Bram Moolenaard9fba312005-06-26 22:34:35 +00006041 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006043 /* Remove the item before deleting it. */
6044 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006045 if (recurse || (item->li_tv.v_type != VAR_LIST
6046 && item->li_tv.v_type != VAR_DICT))
6047 clear_tv(&item->li_tv);
6048 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006049 }
6050 vim_free(l);
6051}
6052
6053/*
6054 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006055 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006057 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006058listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006059{
Bram Moolenaar33570922005-01-25 22:26:29 +00006060 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006061}
6062
6063/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006064 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006066 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006067listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006069 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006070 vim_free(item);
6071}
6072
6073/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006074 * Remove a list item from a List and free it. Also clears the value.
6075 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006076 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006077listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006079 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 listitem_free(item);
6081}
6082
6083/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084 * Get the number of items in a list.
6085 */
6086 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006087list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 if (l == NULL)
6090 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006091 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092}
6093
6094/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006095 * Return TRUE when two lists have exactly the same values.
6096 */
6097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006098list_equal(
6099 list_T *l1,
6100 list_T *l2,
6101 int ic, /* ignore case for strings */
6102 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103{
Bram Moolenaar33570922005-01-25 22:26:29 +00006104 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006105
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006106 if (l1 == NULL || l2 == NULL)
6107 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006108 if (l1 == l2)
6109 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006110 if (list_len(l1) != list_len(l2))
6111 return FALSE;
6112
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113 for (item1 = l1->lv_first, item2 = l2->lv_first;
6114 item1 != NULL && item2 != NULL;
6115 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006116 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 return FALSE;
6118 return item1 == NULL && item2 == NULL;
6119}
6120
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006121/*
6122 * Return the dictitem that an entry in a hashtable points to.
6123 */
6124 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006125dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006126{
6127 return HI2DI(hi);
6128}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006130/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006131 * Return TRUE when two dictionaries have exactly the same key/values.
6132 */
6133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006134dict_equal(
6135 dict_T *d1,
6136 dict_T *d2,
6137 int ic, /* ignore case for strings */
6138 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139{
Bram Moolenaar33570922005-01-25 22:26:29 +00006140 hashitem_T *hi;
6141 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006142 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006144 if (d1 == NULL || d2 == NULL)
6145 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006146 if (d1 == d2)
6147 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006148 if (dict_len(d1) != dict_len(d2))
6149 return FALSE;
6150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006151 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006152 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006153 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006154 if (!HASHITEM_EMPTY(hi))
6155 {
6156 item2 = dict_find(d2, hi->hi_key, -1);
6157 if (item2 == NULL)
6158 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006159 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 return FALSE;
6161 --todo;
6162 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163 }
6164 return TRUE;
6165}
6166
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167static int tv_equal_recurse_limit;
6168
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006170 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006171 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006172 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006173 */
6174 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006175tv_equal(
6176 typval_T *tv1,
6177 typval_T *tv2,
6178 int ic, /* ignore case */
6179 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006180{
6181 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006182 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006184 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006185
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006186 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006187 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006188
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006189 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190 * recursiveness to a limit. We guess they are equal then.
6191 * A fixed limit has the problem of still taking an awful long time.
6192 * Reduce the limit every time running into it. That should work fine for
6193 * deeply linked structures that are not recursively linked and catch
6194 * recursiveness quickly. */
6195 if (!recursive)
6196 tv_equal_recurse_limit = 1000;
6197 if (recursive_cnt >= tv_equal_recurse_limit)
6198 {
6199 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006200 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006201 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006202
6203 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006205 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 ++recursive_cnt;
6207 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6208 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006209 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_FUNC:
6218 return (tv1->vval.v_string != NULL
6219 && tv2->vval.v_string != NULL
6220 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6221
6222 case VAR_NUMBER:
6223 return tv1->vval.v_number == tv2->vval.v_number;
6224
6225 case VAR_STRING:
6226 s1 = get_tv_string_buf(tv1, buf1);
6227 s2 = get_tv_string_buf(tv2, buf2);
6228 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006229
6230 case VAR_SPECIAL:
6231 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006232
6233 case VAR_FLOAT:
6234#ifdef FEAT_FLOAT
6235 return tv1->vval.v_float == tv2->vval.v_float;
6236#endif
6237 case VAR_JOB:
6238#ifdef FEAT_JOB
6239 return tv1->vval.v_job == tv2->vval.v_job;
6240#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006241 case VAR_CHANNEL:
6242#ifdef FEAT_CHANNEL
6243 return tv1->vval.v_channel == tv2->vval.v_channel;
6244#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006245 case VAR_UNKNOWN:
6246 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248
Bram Moolenaara03f2332016-02-06 18:09:59 +01006249 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6250 * does not equal anything, not even itself. */
6251 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006252}
6253
6254/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006255 * Locate item with index "n" in list "l" and return it.
6256 * A negative index is counted from the end; -1 is the last item.
6257 * Returns NULL when "n" is out of range.
6258 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006259 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006260list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261{
Bram Moolenaar33570922005-01-25 22:26:29 +00006262 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 long idx;
6264
6265 if (l == NULL)
6266 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006267
6268 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006270 n = l->lv_len + n;
6271
6272 /* Check for index out of range. */
6273 if (n < 0 || n >= l->lv_len)
6274 return NULL;
6275
6276 /* When there is a cached index may start search from there. */
6277 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006278 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006279 if (n < l->lv_idx / 2)
6280 {
6281 /* closest to the start of the list */
6282 item = l->lv_first;
6283 idx = 0;
6284 }
6285 else if (n > (l->lv_idx + l->lv_len) / 2)
6286 {
6287 /* closest to the end of the list */
6288 item = l->lv_last;
6289 idx = l->lv_len - 1;
6290 }
6291 else
6292 {
6293 /* closest to the cached index */
6294 item = l->lv_idx_item;
6295 idx = l->lv_idx;
6296 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 }
6298 else
6299 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 if (n < l->lv_len / 2)
6301 {
6302 /* closest to the start of the list */
6303 item = l->lv_first;
6304 idx = 0;
6305 }
6306 else
6307 {
6308 /* closest to the end of the list */
6309 item = l->lv_last;
6310 idx = l->lv_len - 1;
6311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313
6314 while (n > idx)
6315 {
6316 /* search forward */
6317 item = item->li_next;
6318 ++idx;
6319 }
6320 while (n < idx)
6321 {
6322 /* search backward */
6323 item = item->li_prev;
6324 --idx;
6325 }
6326
6327 /* cache the used index */
6328 l->lv_idx = idx;
6329 l->lv_idx_item = item;
6330
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 return item;
6332}
6333
6334/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006335 * Get list item "l[idx]" as a number.
6336 */
6337 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006338list_find_nr(
6339 list_T *l,
6340 long idx,
6341 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006342{
6343 listitem_T *li;
6344
6345 li = list_find(l, idx);
6346 if (li == NULL)
6347 {
6348 if (errorp != NULL)
6349 *errorp = TRUE;
6350 return -1L;
6351 }
6352 return get_tv_number_chk(&li->li_tv, errorp);
6353}
6354
6355/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006356 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6357 */
6358 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006359list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006360{
6361 listitem_T *li;
6362
6363 li = list_find(l, idx - 1);
6364 if (li == NULL)
6365 {
6366 EMSGN(_(e_listidx), idx);
6367 return NULL;
6368 }
6369 return get_tv_string(&li->li_tv);
6370}
6371
6372/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006373 * Locate "item" list "l" and return its index.
6374 * Returns -1 when "item" is not in the list.
6375 */
6376 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006377list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006378{
6379 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006380 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381
6382 if (l == NULL)
6383 return -1;
6384 idx = 0;
6385 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6386 ++idx;
6387 if (li == NULL)
6388 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006389 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006390}
6391
6392/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006393 * Append item "item" to the end of list "l".
6394 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006396list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006397{
6398 if (l->lv_last == NULL)
6399 {
6400 /* empty list */
6401 l->lv_first = item;
6402 l->lv_last = item;
6403 item->li_prev = NULL;
6404 }
6405 else
6406 {
6407 l->lv_last->li_next = item;
6408 item->li_prev = l->lv_last;
6409 l->lv_last = item;
6410 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006411 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006412 item->li_next = NULL;
6413}
6414
6415/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006416 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006417 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006419 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006420list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006421{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006422 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423
Bram Moolenaar05159a02005-02-26 23:04:13 +00006424 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006425 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006426 copy_tv(tv, &li->li_tv);
6427 list_append(l, li);
6428 return OK;
6429}
6430
6431/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006432 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006433 * Return FAIL when out of memory.
6434 */
6435 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006437{
6438 listitem_T *li = listitem_alloc();
6439
6440 if (li == NULL)
6441 return FAIL;
6442 li->li_tv.v_type = VAR_DICT;
6443 li->li_tv.v_lock = 0;
6444 li->li_tv.vval.v_dict = dict;
6445 list_append(list, li);
6446 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006447 return OK;
6448}
6449
6450/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006451 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006452 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006453 * Returns FAIL when out of memory.
6454 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006455 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006456list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457{
6458 listitem_T *li = listitem_alloc();
6459
6460 if (li == NULL)
6461 return FAIL;
6462 list_append(l, li);
6463 li->li_tv.v_type = VAR_STRING;
6464 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006465 if (str == NULL)
6466 li->li_tv.vval.v_string = NULL;
6467 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006468 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006469 return FAIL;
6470 return OK;
6471}
6472
6473/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006474 * Append "n" to list "l".
6475 * Returns FAIL when out of memory.
6476 */
6477 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006478list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006479{
6480 listitem_T *li;
6481
6482 li = listitem_alloc();
6483 if (li == NULL)
6484 return FAIL;
6485 li->li_tv.v_type = VAR_NUMBER;
6486 li->li_tv.v_lock = 0;
6487 li->li_tv.vval.v_number = n;
6488 list_append(l, li);
6489 return OK;
6490}
6491
6492/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006493 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006494 * If "item" is NULL append at the end.
6495 * Return FAIL when out of memory.
6496 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006497 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006498list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006499{
Bram Moolenaar33570922005-01-25 22:26:29 +00006500 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006501
6502 if (ni == NULL)
6503 return FAIL;
6504 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006505 list_insert(l, ni, item);
6506 return OK;
6507}
6508
6509 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006510list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006512 if (item == NULL)
6513 /* Append new item at end of list. */
6514 list_append(l, ni);
6515 else
6516 {
6517 /* Insert new item before existing item. */
6518 ni->li_prev = item->li_prev;
6519 ni->li_next = item;
6520 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006522 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006523 ++l->lv_idx;
6524 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006525 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006526 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006528 l->lv_idx_item = NULL;
6529 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533}
6534
6535/*
6536 * Extend "l1" with "l2".
6537 * If "bef" is NULL append at the end, otherwise insert before this item.
6538 * Returns FAIL when out of memory.
6539 */
6540 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006541list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542{
Bram Moolenaar33570922005-01-25 22:26:29 +00006543 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006544 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006546 /* We also quit the loop when we have inserted the original item count of
6547 * the list, avoid a hang when we extend a list with itself. */
6548 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006549 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6550 return FAIL;
6551 return OK;
6552}
6553
6554/*
6555 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6556 * Return FAIL when out of memory.
6557 */
6558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006559list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560{
Bram Moolenaar33570922005-01-25 22:26:29 +00006561 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006563 if (l1 == NULL || l2 == NULL)
6564 return FAIL;
6565
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006567 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568 if (l == NULL)
6569 return FAIL;
6570 tv->v_type = VAR_LIST;
6571 tv->vval.v_list = l;
6572
6573 /* append all items from the second list */
6574 return list_extend(l, l2, NULL);
6575}
6576
6577/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006578 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006579 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006580 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006581 * Returns NULL when out of memory.
6582 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006584list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585{
Bram Moolenaar33570922005-01-25 22:26:29 +00006586 list_T *copy;
6587 listitem_T *item;
6588 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589
6590 if (orig == NULL)
6591 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006592
6593 copy = list_alloc();
6594 if (copy != NULL)
6595 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006596 if (copyID != 0)
6597 {
6598 /* Do this before adding the items, because one of the items may
6599 * refer back to this list. */
6600 orig->lv_copyID = copyID;
6601 orig->lv_copylist = copy;
6602 }
6603 for (item = orig->lv_first; item != NULL && !got_int;
6604 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006605 {
6606 ni = listitem_alloc();
6607 if (ni == NULL)
6608 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006609 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 {
6611 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6612 {
6613 vim_free(ni);
6614 break;
6615 }
6616 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006617 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006618 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 list_append(copy, ni);
6620 }
6621 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006622 if (item != NULL)
6623 {
6624 list_unref(copy);
6625 copy = NULL;
6626 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 }
6628
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629 return copy;
6630}
6631
6632/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006633 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006634 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006635 * This used to be called list_remove, but that conflicts with a Sun header
6636 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006638 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006639vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006640{
Bram Moolenaar33570922005-01-25 22:26:29 +00006641 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006642
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006643 /* notify watchers */
6644 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006646 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 list_fix_watch(l, ip);
6648 if (ip == item2)
6649 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651
6652 if (item2->li_next == NULL)
6653 l->lv_last = item->li_prev;
6654 else
6655 item2->li_next->li_prev = item->li_prev;
6656 if (item->li_prev == NULL)
6657 l->lv_first = item2->li_next;
6658 else
6659 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661}
6662
6663/*
6664 * Return an allocated string with the string representation of a list.
6665 * May return NULL.
6666 */
6667 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006668list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669{
6670 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671
6672 if (tv->vval.v_list == NULL)
6673 return NULL;
6674 ga_init2(&ga, (int)sizeof(char), 80);
6675 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006676 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006677 {
6678 vim_free(ga.ga_data);
6679 return NULL;
6680 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006681 ga_append(&ga, ']');
6682 ga_append(&ga, NUL);
6683 return (char_u *)ga.ga_data;
6684}
6685
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006686typedef struct join_S {
6687 char_u *s;
6688 char_u *tofree;
6689} join_T;
6690
6691 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006692list_join_inner(
6693 garray_T *gap, /* to store the result in */
6694 list_T *l,
6695 char_u *sep,
6696 int echo_style,
6697 int copyID,
6698 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006699{
6700 int i;
6701 join_T *p;
6702 int len;
6703 int sumlen = 0;
6704 int first = TRUE;
6705 char_u *tofree;
6706 char_u numbuf[NUMBUFLEN];
6707 listitem_T *item;
6708 char_u *s;
6709
6710 /* Stringify each item in the list. */
6711 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6712 {
6713 if (echo_style)
6714 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6715 else
6716 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6717 if (s == NULL)
6718 return FAIL;
6719
6720 len = (int)STRLEN(s);
6721 sumlen += len;
6722
Bram Moolenaarcde88542015-08-11 19:14:00 +02006723 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006724 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6725 if (tofree != NULL || s != numbuf)
6726 {
6727 p->s = s;
6728 p->tofree = tofree;
6729 }
6730 else
6731 {
6732 p->s = vim_strnsave(s, len);
6733 p->tofree = p->s;
6734 }
6735
6736 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006737 if (did_echo_string_emsg) /* recursion error, bail out */
6738 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739 }
6740
6741 /* Allocate result buffer with its total size, avoid re-allocation and
6742 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6743 if (join_gap->ga_len >= 2)
6744 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6745 if (ga_grow(gap, sumlen + 2) == FAIL)
6746 return FAIL;
6747
6748 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6749 {
6750 if (first)
6751 first = FALSE;
6752 else
6753 ga_concat(gap, sep);
6754 p = ((join_T *)join_gap->ga_data) + i;
6755
6756 if (p->s != NULL)
6757 ga_concat(gap, p->s);
6758 line_breakcheck();
6759 }
6760
6761 return OK;
6762}
6763
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006764/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006765 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006766 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006767 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006768 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006770list_join(
6771 garray_T *gap,
6772 list_T *l,
6773 char_u *sep,
6774 int echo_style,
6775 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006777 garray_T join_ga;
6778 int retval;
6779 join_T *p;
6780 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006781
Bram Moolenaard39a7512015-04-16 22:51:22 +02006782 if (l->lv_len < 1)
6783 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006784 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6785 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6786
6787 /* Dispose each item in join_ga. */
6788 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 p = (join_T *)join_ga.ga_data;
6791 for (i = 0; i < join_ga.ga_len; ++i)
6792 {
6793 vim_free(p->tofree);
6794 ++p;
6795 }
6796 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798
6799 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006800}
6801
6802/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006803 * Return the next (unique) copy ID.
6804 * Used for serializing nested structures.
6805 */
6806 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006807get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006808{
6809 current_copyID += COPYID_INC;
6810 return current_copyID;
6811}
6812
6813/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006814 * Garbage collection for lists and dictionaries.
6815 *
6816 * We use reference counts to be able to free most items right away when they
6817 * are no longer used. But for composite items it's possible that it becomes
6818 * unused while the reference count is > 0: When there is a recursive
6819 * reference. Example:
6820 * :let l = [1, 2, 3]
6821 * :let d = {9: l}
6822 * :let l[1] = d
6823 *
6824 * Since this is quite unusual we handle this with garbage collection: every
6825 * once in a while find out which lists and dicts are not referenced from any
6826 * variable.
6827 *
6828 * Here is a good reference text about garbage collection (refers to Python
6829 * but it applies to all reference-counting mechanisms):
6830 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006831 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006832
6833/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006834 * Do garbage collection for lists and dicts.
6835 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006836 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006837 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006838garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006840 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006841 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 buf_T *buf;
6843 win_T *wp;
6844 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006845 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006846 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006847 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006848#ifdef FEAT_WINDOWS
6849 tabpage_T *tp;
6850#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006852 /* Only do this once. */
6853 want_garbage_collect = FALSE;
6854 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006855 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006856
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006857 /* We advance by two because we add one for items referenced through
6858 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006859 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006860
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006861 /*
6862 * 1. Go through all accessible variables and mark all lists and dicts
6863 * with copyID.
6864 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865
6866 /* Don't free variables in the previous_funccal list unless they are only
6867 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006868 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006869 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6870 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6872 NULL);
6873 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6874 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 }
6876
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 /* script-local variables */
6878 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006880
6881 /* buffer-local variables */
6882 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006883 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6884 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885
6886 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006887 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006888 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6889 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006890#ifdef FEAT_AUTOCMD
6891 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006892 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6893 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006894#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006896#ifdef FEAT_WINDOWS
6897 /* tabpage-local variables */
6898 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006899 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6900 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901#endif
6902
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006905
6906 /* function-local variables */
6907 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6908 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6910 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 }
6912
Bram Moolenaard812df62008-11-09 12:46:09 +00006913 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006914 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006915
Bram Moolenaar1dced572012-04-05 16:54:08 +02006916#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006918#endif
6919
Bram Moolenaardb913952012-06-29 12:54:53 +02006920#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006922#endif
6923
6924#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#endif
6927
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006928#ifdef FEAT_CHANNEL
6929 abort = abort || set_ref_in_channel(copyID);
6930#endif
6931
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006933 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 /*
6935 * 2. Free lists and dictionaries that are not referenced.
6936 */
6937 did_free = free_unref_items(copyID);
6938
6939 /*
6940 * 3. Check if any funccal can be freed now.
6941 */
6942 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006943 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 if (can_free_funccal(*pfc, copyID))
6945 {
6946 fc = *pfc;
6947 *pfc = fc->caller;
6948 free_funccal(fc, TRUE);
6949 did_free = TRUE;
6950 did_free_funccal = TRUE;
6951 }
6952 else
6953 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006954 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 if (did_free_funccal)
6956 /* When a funccal was freed some more items might be garbage
6957 * collected, so run again. */
6958 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006959 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006960 else if (p_verbose > 0)
6961 {
6962 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6963 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006964
6965 return did_free;
6966}
6967
6968/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006969 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970 */
6971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006972free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006974 dict_T *dd, *dd_next;
6975 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 int did_free = FALSE;
6977
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006978 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006980 */
6981 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 {
6983 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006985 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006986 /* Free the Dictionary and ordinary items it contains, but don't
6987 * recurse into Lists and Dictionaries, they will be in the list
6988 * of dicts or list of lists. */
6989 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006990 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006992 dd = dd_next;
6993 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994
6995 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006996 * Go through the list of lists and free items without the copyID.
6997 * But don't free a list that has a watcher (used in a for loop), these
6998 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 */
7000 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007001 {
7002 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7004 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007006 /* Free the List and ordinary items it contains, but don't recurse
7007 * into Lists and Dictionaries, they will be in the list of dicts
7008 * or list of lists. */
7009 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 ll = ll_next;
7013 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007014
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 return did_free;
7016}
7017
7018/*
7019 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007020 * "list_stack" is used to add lists to be marked. Can be NULL.
7021 *
7022 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007024 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007025set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007026{
7027 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 hashtab_T *cur_ht;
7031 ht_stack_T *ht_stack = NULL;
7032 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 cur_ht = ht;
7035 for (;;)
7036 {
7037 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007039 /* Mark each item in the hashtab. If the item contains a hashtab
7040 * it is added to ht_stack, if it contains a list it is added to
7041 * list_stack. */
7042 todo = (int)cur_ht->ht_used;
7043 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7044 if (!HASHITEM_EMPTY(hi))
7045 {
7046 --todo;
7047 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7048 &ht_stack, list_stack);
7049 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007051
7052 if (ht_stack == NULL)
7053 break;
7054
7055 /* take an item from the stack */
7056 cur_ht = ht_stack->ht;
7057 tempitem = ht_stack;
7058 ht_stack = ht_stack->prev;
7059 free(tempitem);
7060 }
7061
7062 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063}
7064
7065/*
7066 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7068 *
7069 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007070 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007071 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007072set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 listitem_T *li;
7075 int abort = FALSE;
7076 list_T *cur_l;
7077 list_stack_T *list_stack = NULL;
7078 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 cur_l = l;
7081 for (;;)
7082 {
7083 if (!abort)
7084 /* Mark each item in the list. If the item contains a hashtab
7085 * it is added to ht_stack, if it contains a list it is added to
7086 * list_stack. */
7087 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7088 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7089 ht_stack, &list_stack);
7090 if (list_stack == NULL)
7091 break;
7092
7093 /* take an item from the stack */
7094 cur_l = list_stack->list;
7095 tempitem = list_stack;
7096 list_stack = list_stack->prev;
7097 free(tempitem);
7098 }
7099
7100 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007101}
7102
7103/*
7104 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 * "list_stack" is used to add lists to be marked. Can be NULL.
7106 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7107 *
7108 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007111set_ref_in_item(
7112 typval_T *tv,
7113 int copyID,
7114 ht_stack_T **ht_stack,
7115 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007116{
7117 dict_T *dd;
7118 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007120
Bram Moolenaara03f2332016-02-06 18:09:59 +01007121 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007122 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007123 dd = tv->vval.v_dict;
7124 if (dd != NULL && dd->dv_copyID != copyID)
7125 {
7126 /* Didn't see this dict yet. */
7127 dd->dv_copyID = copyID;
7128 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007129 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007130 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7131 }
7132 else
7133 {
7134 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7135 if (newitem == NULL)
7136 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007137 else
7138 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007139 newitem->ht = &dd->dv_hashtab;
7140 newitem->prev = *ht_stack;
7141 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007142 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007143 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007144 }
7145 }
7146 else if (tv->v_type == VAR_LIST)
7147 {
7148 ll = tv->vval.v_list;
7149 if (ll != NULL && ll->lv_copyID != copyID)
7150 {
7151 /* Didn't see this list yet. */
7152 ll->lv_copyID = copyID;
7153 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007155 abort = set_ref_in_list(ll, copyID, ht_stack);
7156 }
7157 else
7158 {
7159 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007160 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 if (newitem == NULL)
7162 abort = TRUE;
7163 else
7164 {
7165 newitem->list = ll;
7166 newitem->prev = *list_stack;
7167 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007169 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007170 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007171 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173}
7174
7175/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007176 * Allocate an empty header for a dictionary.
7177 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007178 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007179dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007180{
Bram Moolenaar33570922005-01-25 22:26:29 +00007181 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007182
Bram Moolenaar33570922005-01-25 22:26:29 +00007183 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007184 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007185 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007186 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007187 if (first_dict != NULL)
7188 first_dict->dv_used_prev = d;
7189 d->dv_used_next = first_dict;
7190 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007191 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007192
Bram Moolenaar33570922005-01-25 22:26:29 +00007193 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007194 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007195 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007196 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007197 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007198 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007199 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007200}
7201
7202/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007203 * Allocate an empty dict for a return value.
7204 * Returns OK or FAIL.
7205 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007206 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007207rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007208{
7209 dict_T *d = dict_alloc();
7210
7211 if (d == NULL)
7212 return FAIL;
7213
7214 rettv->vval.v_dict = d;
7215 rettv->v_type = VAR_DICT;
7216 ++d->dv_refcount;
7217 return OK;
7218}
7219
7220
7221/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007222 * Unreference a Dictionary: decrement the reference count and free it when it
7223 * becomes zero.
7224 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007225 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007226dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007227{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007228 if (d != NULL && --d->dv_refcount <= 0)
7229 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007233 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007234 * Ignores the reference count.
7235 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007236 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237dict_free(
7238 dict_T *d,
7239 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007241 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007242 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007243 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007245 /* Remove the dict from the list of dicts for garbage collection. */
7246 if (d->dv_used_prev == NULL)
7247 first_dict = d->dv_used_next;
7248 else
7249 d->dv_used_prev->dv_used_next = d->dv_used_next;
7250 if (d->dv_used_next != NULL)
7251 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7252
7253 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007254 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007255 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007256 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007258 if (!HASHITEM_EMPTY(hi))
7259 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 /* Remove the item before deleting it, just in case there is
7261 * something recursive causing trouble. */
7262 di = HI2DI(hi);
7263 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007264 if (recurse || (di->di_tv.v_type != VAR_LIST
7265 && di->di_tv.v_type != VAR_DICT))
7266 clear_tv(&di->di_tv);
7267 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007268 --todo;
7269 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007271 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272 vim_free(d);
7273}
7274
7275/*
7276 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007277 * The "key" is copied to the new item.
7278 * Note that the value of the item "di_tv" still needs to be initialized!
7279 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007281 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007283{
Bram Moolenaar33570922005-01-25 22:26:29 +00007284 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007286 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007287 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007288 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007289 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007290 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007291 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007292 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293}
7294
7295/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007296 * Make a copy of a Dictionary item.
7297 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007298 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007299dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007300{
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007303 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7304 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007305 if (di != NULL)
7306 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007308 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007309 copy_tv(&org->di_tv, &di->di_tv);
7310 }
7311 return di;
7312}
7313
7314/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 * Remove item "item" from Dictionary "dict" and free it.
7316 */
7317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007318dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319{
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 if (HASHITEM_EMPTY(hi))
7324 EMSG2(_(e_intern2), "dictitem_remove()");
7325 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327 dictitem_free(item);
7328}
7329
7330/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007331 * Free a dict item. Also clears the value.
7332 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007334dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007335{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007337 if (item->di_flags & DI_FLAGS_ALLOC)
7338 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339}
7340
7341/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007342 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7343 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007344 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007345 * Returns NULL when out of memory.
7346 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007347 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 dict_T *copy;
7351 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007354
7355 if (orig == NULL)
7356 return NULL;
7357
7358 copy = dict_alloc();
7359 if (copy != NULL)
7360 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007361 if (copyID != 0)
7362 {
7363 orig->dv_copyID = copyID;
7364 orig->dv_copydict = copy;
7365 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007366 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007368 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007370 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 --todo;
7372
7373 di = dictitem_alloc(hi->hi_key);
7374 if (di == NULL)
7375 break;
7376 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007377 {
7378 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7379 copyID) == FAIL)
7380 {
7381 vim_free(di);
7382 break;
7383 }
7384 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007385 else
7386 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7387 if (dict_add(copy, di) == FAIL)
7388 {
7389 dictitem_free(di);
7390 break;
7391 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007393 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394
Bram Moolenaare9a41262005-01-15 22:18:47 +00007395 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007396 if (todo > 0)
7397 {
7398 dict_unref(copy);
7399 copy = NULL;
7400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 }
7402
7403 return copy;
7404}
7405
7406/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007407 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007408 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007410 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar33570922005-01-25 22:26:29 +00007413 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414}
7415
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007417 * Add a number or string entry to dictionary "d".
7418 * When "str" is NULL use number "nr", otherwise use "str".
7419 * Returns FAIL when out of memory and when key already exists.
7420 */
7421 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007422dict_add_nr_str(
7423 dict_T *d,
7424 char *key,
7425 long nr,
7426 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007427{
7428 dictitem_T *item;
7429
7430 item = dictitem_alloc((char_u *)key);
7431 if (item == NULL)
7432 return FAIL;
7433 item->di_tv.v_lock = 0;
7434 if (str == NULL)
7435 {
7436 item->di_tv.v_type = VAR_NUMBER;
7437 item->di_tv.vval.v_number = nr;
7438 }
7439 else
7440 {
7441 item->di_tv.v_type = VAR_STRING;
7442 item->di_tv.vval.v_string = vim_strsave(str);
7443 }
7444 if (dict_add(d, item) == FAIL)
7445 {
7446 dictitem_free(item);
7447 return FAIL;
7448 }
7449 return OK;
7450}
7451
7452/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007453 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007454 * Returns FAIL when out of memory and when key already exists.
7455 */
7456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007457dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007458{
7459 dictitem_T *item;
7460
7461 item = dictitem_alloc((char_u *)key);
7462 if (item == NULL)
7463 return FAIL;
7464 item->di_tv.v_lock = 0;
7465 item->di_tv.v_type = VAR_LIST;
7466 item->di_tv.vval.v_list = list;
7467 if (dict_add(d, item) == FAIL)
7468 {
7469 dictitem_free(item);
7470 return FAIL;
7471 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007472 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007473 return OK;
7474}
7475
7476/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007477 * Get the number of items in a Dictionary.
7478 */
7479 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007480dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007481{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007482 if (d == NULL)
7483 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007484 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485}
7486
7487/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007488 * Find item "key[len]" in Dictionary "d".
7489 * If "len" is negative use strlen(key).
7490 * Returns NULL when not found.
7491 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007492 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007493dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007495#define AKEYLEN 200
7496 char_u buf[AKEYLEN];
7497 char_u *akey;
7498 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007499 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501 if (len < 0)
7502 akey = key;
7503 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007504 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007505 tofree = akey = vim_strnsave(key, len);
7506 if (akey == NULL)
7507 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007508 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 else
7510 {
7511 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007512 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 akey = buf;
7514 }
7515
Bram Moolenaar33570922005-01-25 22:26:29 +00007516 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 vim_free(tofree);
7518 if (HASHITEM_EMPTY(hi))
7519 return NULL;
7520 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007521}
7522
7523/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007524 * Get a string item from a dictionary.
7525 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007526 * Returns NULL if the entry doesn't exist or out of memory.
7527 */
7528 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007529get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007530{
7531 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007533
7534 di = dict_find(d, key, -1);
7535 if (di == NULL)
7536 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007537 s = get_tv_string(&di->di_tv);
7538 if (save && s != NULL)
7539 s = vim_strsave(s);
7540 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541}
7542
7543/*
7544 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007545 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007546 */
7547 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007548get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549{
7550 dictitem_T *di;
7551
7552 di = dict_find(d, key, -1);
7553 if (di == NULL)
7554 return 0;
7555 return get_tv_number(&di->di_tv);
7556}
7557
7558/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007559 * Return an allocated string with the string representation of a Dictionary.
7560 * May return NULL.
7561 */
7562 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007564{
7565 garray_T ga;
7566 int first = TRUE;
7567 char_u *tofree;
7568 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007569 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007571 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007573
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007574 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575 return NULL;
7576 ga_init2(&ga, (int)sizeof(char), 80);
7577 ga_append(&ga, '{');
7578
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007579 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007580 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 --todo;
7585
7586 if (first)
7587 first = FALSE;
7588 else
7589 ga_concat(&ga, (char_u *)", ");
7590
7591 tofree = string_quote(hi->hi_key, FALSE);
7592 if (tofree != NULL)
7593 {
7594 ga_concat(&ga, tofree);
7595 vim_free(tofree);
7596 }
7597 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007598 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007599 if (s != NULL)
7600 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007601 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007602 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007603 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007604 line_breakcheck();
7605
Bram Moolenaar8c711452005-01-14 21:53:12 +00007606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007608 if (todo > 0)
7609 {
7610 vim_free(ga.ga_data);
7611 return NULL;
7612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613
7614 ga_append(&ga, '}');
7615 ga_append(&ga, NUL);
7616 return (char_u *)ga.ga_data;
7617}
7618
7619/*
7620 * Allocate a variable for a Dictionary and fill it from "*arg".
7621 * Return OK or FAIL. Returns NOTDONE for {expr}.
7622 */
7623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007624get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007625{
Bram Moolenaar33570922005-01-25 22:26:29 +00007626 dict_T *d = NULL;
7627 typval_T tvkey;
7628 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007629 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007630 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007632 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633
7634 /*
7635 * First check if it's not a curly-braces thing: {expr}.
7636 * Must do this without evaluating, otherwise a function may be called
7637 * twice. Unfortunately this means we need to call eval1() twice for the
7638 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007639 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007640 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007641 if (*start != '}')
7642 {
7643 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7644 return FAIL;
7645 if (*start == '}')
7646 return NOTDONE;
7647 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
7649 if (evaluate)
7650 {
7651 d = dict_alloc();
7652 if (d == NULL)
7653 return FAIL;
7654 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 tvkey.v_type = VAR_UNKNOWN;
7656 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657
7658 *arg = skipwhite(*arg + 1);
7659 while (**arg != '}' && **arg != NUL)
7660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 goto failret;
7663 if (**arg != ':')
7664 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007665 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007666 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007667 goto failret;
7668 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007669 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007671 key = get_tv_string_buf_chk(&tvkey, buf);
7672 if (key == NULL || *key == NUL)
7673 {
7674 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7675 if (key != NULL)
7676 EMSG(_(e_emptykey));
7677 clear_tv(&tvkey);
7678 goto failret;
7679 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007680 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681
7682 *arg = skipwhite(*arg + 1);
7683 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7684 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007685 if (evaluate)
7686 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 goto failret;
7688 }
7689 if (evaluate)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 if (item != NULL)
7693 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007694 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007695 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 clear_tv(&tv);
7697 goto failret;
7698 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 item = dictitem_alloc(key);
7700 clear_tv(&tvkey);
7701 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007703 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007704 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 if (dict_add(d, item) == FAIL)
7706 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007707 }
7708 }
7709
7710 if (**arg == '}')
7711 break;
7712 if (**arg != ',')
7713 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007714 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 goto failret;
7716 }
7717 *arg = skipwhite(*arg + 1);
7718 }
7719
7720 if (**arg != '}')
7721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007722 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723failret:
7724 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007725 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 return FAIL;
7727 }
7728
7729 *arg = skipwhite(*arg + 1);
7730 if (evaluate)
7731 {
7732 rettv->v_type = VAR_DICT;
7733 rettv->vval.v_dict = d;
7734 ++d->dv_refcount;
7735 }
7736
7737 return OK;
7738}
7739
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007740#if defined(FEAT_CHANNEL) || defined(PROTO)
7741/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007742 * Decrement the reference count on "channel" and maybe free it when it goes
7743 * down to zero. Don't free it if there is a pending action.
Bram Moolenaard6051b52016-02-28 15:49:03 +01007744 * Returns TRUE when the channel is no longer referenced.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007745 */
7746 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007747channel_unref(channel_T *channel)
7748{
7749 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar70765942016-02-28 19:28:59 +01007750 return channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007751 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007752}
7753#endif
7754
Bram Moolenaar65edff82016-02-21 16:40:11 +01007755#if defined(FEAT_JOB) || defined(PROTO)
7756static job_T *first_job = NULL;
7757
Bram Moolenaar835dc632016-02-07 14:27:38 +01007758 static void
7759job_free(job_T *job)
7760{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007761# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007762 ch_log(job->jv_channel, "Freeing job");
Bram Moolenaar77073442016-02-13 23:23:53 +01007763 if (job->jv_channel != NULL)
7764 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007765 /* The link from the channel to the job doesn't count as a reference,
7766 * thus don't decrement the refcount of the job. The reference from
7767 * the job to the channel does count the refrence, decrement it and
7768 * NULL the reference. We don't set ch_job_killed, unreferencing the
7769 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007770 job->jv_channel->ch_job = NULL;
7771 channel_unref(job->jv_channel);
7772 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007773# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007774 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007775
7776 if (job->jv_next != NULL)
7777 job->jv_next->jv_prev = job->jv_prev;
7778 if (job->jv_prev == NULL)
7779 first_job = job->jv_next;
7780 else
7781 job->jv_prev->jv_next = job->jv_next;
7782
7783 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007784 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007785 vim_free(job);
7786}
7787
7788 static void
7789job_unref(job_T *job)
7790{
7791 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007792 {
7793 /* Do not free the job when it has not ended yet and there is a
7794 * "stoponexit" flag or an exit callback. */
7795 if (job->jv_status != JOB_STARTED
7796 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
Bram Moolenaard6051b52016-02-28 15:49:03 +01007797 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007798 job_free(job);
Bram Moolenaard6051b52016-02-28 15:49:03 +01007799 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007800# ifdef FEAT_CHANNEL
Bram Moolenaard6051b52016-02-28 15:49:03 +01007801 else if (job->jv_channel != NULL)
7802 {
7803 /* Do remove the link to the channel, otherwise it hangs
7804 * around until Vim exits. See job_free() for refcount. */
7805 job->jv_channel->ch_job = NULL;
7806 channel_unref(job->jv_channel);
7807 job->jv_channel = NULL;
7808 }
Bram Moolenaar8cc69772016-02-28 16:42:03 +01007809# endif
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007810 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007811}
7812
7813/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007814 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007815 */
7816 static job_T *
7817job_alloc(void)
7818{
7819 job_T *job;
7820
7821 job = (job_T *)alloc_clear(sizeof(job_T));
7822 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007823 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007824 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007825 job->jv_stoponexit = vim_strsave((char_u *)"term");
7826
7827 if (first_job != NULL)
7828 {
7829 first_job->jv_prev = job;
7830 job->jv_next = first_job;
7831 }
7832 first_job = job;
7833 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007834 return job;
7835}
7836
Bram Moolenaar65edff82016-02-21 16:40:11 +01007837 static void
7838job_set_options(job_T *job, jobopt_T *opt)
7839{
7840 if (opt->jo_set & JO_STOPONEXIT)
7841 {
7842 vim_free(job->jv_stoponexit);
7843 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7844 job->jv_stoponexit = NULL;
7845 else
7846 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7847 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007848 if (opt->jo_set & JO_EXIT_CB)
7849 {
7850 vim_free(job->jv_exit_cb);
7851 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7852 job->jv_exit_cb = NULL;
7853 else
7854 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7855 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007856}
7857
7858/*
7859 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7860 */
7861 void
7862job_stop_on_exit()
7863{
7864 job_T *job;
7865
7866 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007867 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007868 mch_stop_job(job, job->jv_stoponexit);
7869}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007870#endif
7871
Bram Moolenaar17a13432016-01-24 14:22:10 +01007872 static char *
7873get_var_special_name(int nr)
7874{
7875 switch (nr)
7876 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007877 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007878 case VVAL_TRUE: return "v:true";
7879 case VVAL_NONE: return "v:none";
7880 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007881 }
7882 EMSG2(_(e_intern2), "get_var_special_name()");
7883 return "42";
7884}
7885
Bram Moolenaar8c711452005-01-14 21:53:12 +00007886/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007887 * Return a string with the string representation of a variable.
7888 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007889 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007890 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007891 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007892 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007893 */
7894 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007895echo_string(
7896 typval_T *tv,
7897 char_u **tofree,
7898 char_u *numbuf,
7899 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007900{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 static int recurse = 0;
7902 char_u *r = NULL;
7903
Bram Moolenaar33570922005-01-25 22:26:29 +00007904 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007905 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007906 if (!did_echo_string_emsg)
7907 {
7908 /* Only give this message once for a recursive call to avoid
7909 * flooding the user with errors. And stop iterating over lists
7910 * and dicts. */
7911 did_echo_string_emsg = TRUE;
7912 EMSG(_("E724: variable nested too deep for displaying"));
7913 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007915 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007916 }
7917 ++recurse;
7918
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007919 switch (tv->v_type)
7920 {
7921 case VAR_FUNC:
7922 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007923 r = tv->vval.v_string;
7924 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007927 if (tv->vval.v_list == NULL)
7928 {
7929 *tofree = NULL;
7930 r = NULL;
7931 }
7932 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7933 {
7934 *tofree = NULL;
7935 r = (char_u *)"[...]";
7936 }
7937 else
7938 {
7939 tv->vval.v_list->lv_copyID = copyID;
7940 *tofree = list2string(tv, copyID);
7941 r = *tofree;
7942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007944
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007946 if (tv->vval.v_dict == NULL)
7947 {
7948 *tofree = NULL;
7949 r = NULL;
7950 }
7951 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7952 {
7953 *tofree = NULL;
7954 r = (char_u *)"{...}";
7955 }
7956 else
7957 {
7958 tv->vval.v_dict->dv_copyID = copyID;
7959 *tofree = dict2string(tv, copyID);
7960 r = *tofree;
7961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 case VAR_STRING:
7965 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007966 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 *tofree = NULL;
7970 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 *tofree = NULL;
7976 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7977 r = numbuf;
7978 break;
7979#endif
7980
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007981 case VAR_SPECIAL:
7982 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007983 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007984 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986
Bram Moolenaar8502c702014-06-17 12:51:16 +02007987 if (--recurse == 0)
7988 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990}
7991
7992/*
7993 * Return a string with the string representation of a variable.
7994 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7995 * "numbuf" is used for a number.
7996 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007997 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 */
7999 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000tv2string(
8001 typval_T *tv,
8002 char_u **tofree,
8003 char_u *numbuf,
8004 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005{
8006 switch (tv->v_type)
8007 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008 case VAR_FUNC:
8009 *tofree = string_quote(tv->vval.v_string, TRUE);
8010 return *tofree;
8011 case VAR_STRING:
8012 *tofree = string_quote(tv->vval.v_string, FALSE);
8013 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014#ifdef FEAT_FLOAT
8015 case VAR_FLOAT:
8016 *tofree = NULL;
8017 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8018 return numbuf;
8019#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008023 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008024 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008025 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008026 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008027 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008028 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008029 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008030}
8031
8032/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 * Return string "str" in ' quotes, doubling ' characters.
8034 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008035 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008036 */
8037 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008038string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039{
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008041 char_u *p, *r, *s;
8042
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 len = (function ? 13 : 3);
8044 if (str != NULL)
8045 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008046 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008047 for (p = str; *p != NUL; mb_ptr_adv(p))
8048 if (*p == '\'')
8049 ++len;
8050 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008051 s = r = alloc(len);
8052 if (r != NULL)
8053 {
8054 if (function)
8055 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008057 r += 10;
8058 }
8059 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008060 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008061 if (str != NULL)
8062 for (p = str; *p != NUL; )
8063 {
8064 if (*p == '\'')
8065 *r++ = '\'';
8066 MB_COPY_CHAR(p, r);
8067 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008068 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008069 if (function)
8070 *r++ = ')';
8071 *r++ = NUL;
8072 }
8073 return s;
8074}
8075
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008076#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008077/*
8078 * Convert the string "text" to a floating point number.
8079 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8080 * this always uses a decimal point.
8081 * Returns the length of the text that was consumed.
8082 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008083 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008084string2float(
8085 char_u *text,
8086 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008087{
8088 char *s = (char *)text;
8089 float_T f;
8090
8091 f = strtod(s, &s);
8092 *value = f;
8093 return (int)((char_u *)s - text);
8094}
8095#endif
8096
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008097/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 * Get the value of an environment variable.
8099 * "arg" is pointing to the '$'. It is advanced to after the name.
8100 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008101 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008102 */
8103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008104get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105{
8106 char_u *string = NULL;
8107 int len;
8108 int cc;
8109 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008110 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008111
8112 ++*arg;
8113 name = *arg;
8114 len = get_env_len(arg);
8115 if (evaluate)
8116 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008117 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008118 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008119
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008120 cc = name[len];
8121 name[len] = NUL;
8122 /* first try vim_getenv(), fast for normal environment vars */
8123 string = vim_getenv(name, &mustfree);
8124 if (string != NULL && *string != NUL)
8125 {
8126 if (!mustfree)
8127 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008128 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008129 else
8130 {
8131 if (mustfree)
8132 vim_free(string);
8133
8134 /* next try expanding things like $VIM and ${HOME} */
8135 string = expand_env_save(name - 1);
8136 if (string != NULL && *string == '$')
8137 {
8138 vim_free(string);
8139 string = NULL;
8140 }
8141 }
8142 name[len] = cc;
8143
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008144 rettv->v_type = VAR_STRING;
8145 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008146 }
8147
8148 return OK;
8149}
8150
8151/*
8152 * Array with names and number of arguments of all internal functions
8153 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8154 */
8155static struct fst
8156{
8157 char *f_name; /* function name */
8158 char f_min_argc; /* minimal number of arguments */
8159 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008160 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008161 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008162} functions[] =
8163{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#ifdef FEAT_FLOAT
8165 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008166 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008167#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008168 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008169 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008170 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"append", 2, 2, f_append},
8172 {"argc", 0, 0, f_argc},
8173 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008174 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008175 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008176#ifdef FEAT_FLOAT
8177 {"asin", 1, 1, f_asin}, /* WJMc */
8178#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008179 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008180 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008181 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008182 {"assert_false", 1, 2, f_assert_false},
8183 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008184#ifdef FEAT_FLOAT
8185 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008186 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008187#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008189 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"bufexists", 1, 1, f_bufexists},
8191 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8192 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8193 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8194 {"buflisted", 1, 1, f_buflisted},
8195 {"bufloaded", 1, 1, f_bufloaded},
8196 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008197 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"bufwinnr", 1, 1, f_bufwinnr},
8199 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008200 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008201 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008202 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008203#ifdef FEAT_FLOAT
8204 {"ceil", 1, 1, f_ceil},
8205#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008206#ifdef FEAT_CHANNEL
8207 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008208 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8209 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008210 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008211# ifdef FEAT_JOB
8212 {"ch_getjob", 1, 1, f_ch_getjob},
8213# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008214 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008215 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008216 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008217 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008218 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008219 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8220 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008221 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008222 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008223#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008224 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008225 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008227 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008229#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008230 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008231 {"complete_add", 1, 1, f_complete_add},
8232 {"complete_check", 0, 0, f_complete_check},
8233#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008235 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236#ifdef FEAT_FLOAT
8237 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008238 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008239#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008241 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008242 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008243 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008244 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008246 {"diff_filler", 1, 1, f_diff_filler},
8247 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008248 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008249 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008251 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"eventhandler", 0, 0, f_eventhandler},
8253 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008254 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008256#ifdef FEAT_FLOAT
8257 {"exp", 1, 1, f_exp},
8258#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008259 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008260 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008261 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8263 {"filereadable", 1, 1, f_filereadable},
8264 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008265 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008266 {"finddir", 1, 3, f_finddir},
8267 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008268#ifdef FEAT_FLOAT
8269 {"float2nr", 1, 1, f_float2nr},
8270 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008271 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008272#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008273 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008274 {"fnamemodify", 2, 2, f_fnamemodify},
8275 {"foldclosed", 1, 1, f_foldclosed},
8276 {"foldclosedend", 1, 1, f_foldclosedend},
8277 {"foldlevel", 1, 1, f_foldlevel},
8278 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008279 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008280 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008281 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008282 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008283 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008284 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008285 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getchar", 0, 1, f_getchar},
8287 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008288 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008289 {"getcmdline", 0, 0, f_getcmdline},
8290 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008291 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008292 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008293 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008294 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008295 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008296 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008297 {"getfsize", 1, 1, f_getfsize},
8298 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008299 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008300 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008301 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008302 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008303 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008304 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008305 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008306 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008308 {"gettabvar", 2, 3, f_gettabvar},
8309 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008310 {"getwinposx", 0, 0, f_getwinposx},
8311 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008312 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008313 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008314 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008315 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008317 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008318 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008319 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008320 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8321 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8322 {"histadd", 2, 2, f_histadd},
8323 {"histdel", 1, 2, f_histdel},
8324 {"histget", 1, 2, f_histget},
8325 {"histnr", 1, 1, f_histnr},
8326 {"hlID", 1, 1, f_hlID},
8327 {"hlexists", 1, 1, f_hlexists},
8328 {"hostname", 0, 0, f_hostname},
8329 {"iconv", 3, 3, f_iconv},
8330 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008331 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008332 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008334 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"inputrestore", 0, 0, f_inputrestore},
8336 {"inputsave", 0, 0, f_inputsave},
8337 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008338 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008339 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008340 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008341 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008342#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8343 {"isnan", 1, 1, f_isnan},
8344#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008345 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008346#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008347# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008348 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008349# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008350 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008351 {"job_start", 1, 2, f_job_start},
8352 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008353 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008354#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008356 {"js_decode", 1, 1, f_js_decode},
8357 {"js_encode", 1, 1, f_js_encode},
8358 {"json_decode", 1, 1, f_json_decode},
8359 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"libcall", 3, 3, f_libcall},
8364 {"libcallnr", 3, 3, f_libcallnr},
8365 {"line", 1, 1, f_line},
8366 {"line2byte", 1, 1, f_line2byte},
8367 {"lispindent", 1, 1, f_lispindent},
8368 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 {"log10", 1, 1, f_log10},
8372#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008373#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008374 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008375#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008376 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008377 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008378 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008379 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008380 {"matchadd", 2, 5, f_matchadd},
8381 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008382 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008383 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008384 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008385 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008386 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008387 {"max", 1, 1, f_max},
8388 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008389#ifdef vim_mkdir
8390 {"mkdir", 1, 3, f_mkdir},
8391#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008392 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008393#ifdef FEAT_MZSCHEME
8394 {"mzeval", 1, 1, f_mzeval},
8395#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008396 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008397 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008398 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008399 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008400#ifdef FEAT_PERL
8401 {"perleval", 1, 1, f_perleval},
8402#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008403#ifdef FEAT_FLOAT
8404 {"pow", 2, 2, f_pow},
8405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008406 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008407 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008408 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008409#ifdef FEAT_PYTHON3
8410 {"py3eval", 1, 1, f_py3eval},
8411#endif
8412#ifdef FEAT_PYTHON
8413 {"pyeval", 1, 1, f_pyeval},
8414#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008415 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008416 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008417 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008418 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008419 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"remote_expr", 2, 3, f_remote_expr},
8421 {"remote_foreground", 1, 1, f_remote_foreground},
8422 {"remote_peek", 1, 2, f_remote_peek},
8423 {"remote_read", 1, 1, f_remote_read},
8424 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008425 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008426 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008427 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008428 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008429 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008430#ifdef FEAT_FLOAT
8431 {"round", 1, 1, f_round},
8432#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008433 {"screenattr", 2, 2, f_screenattr},
8434 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008435 {"screencol", 0, 0, f_screencol},
8436 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008437 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008438 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008439 {"searchpair", 3, 7, f_searchpair},
8440 {"searchpairpos", 3, 7, f_searchpairpos},
8441 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"server2client", 2, 2, f_server2client},
8443 {"serverlist", 0, 0, f_serverlist},
8444 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008445 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008446 {"setcmdpos", 1, 1, f_setcmdpos},
8447 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008448 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008449 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008450 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008451 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008453 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008454 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008455 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008456#ifdef FEAT_CRYPT
8457 {"sha256", 1, 1, f_sha256},
8458#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008459 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008460 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462#ifdef FEAT_FLOAT
8463 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008464 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008465#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008466 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008467 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008468 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008469 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008470 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008471#ifdef FEAT_FLOAT
8472 {"sqrt", 1, 1, f_sqrt},
8473 {"str2float", 1, 1, f_str2float},
8474#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008475 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008476 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008477 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478#ifdef HAVE_STRFTIME
8479 {"strftime", 1, 2, f_strftime},
8480#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008481 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008482 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"strlen", 1, 1, f_strlen},
8484 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008485 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008487 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008488 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008489 {"substitute", 4, 4, f_substitute},
8490 {"synID", 3, 3, f_synID},
8491 {"synIDattr", 2, 3, f_synIDattr},
8492 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008493 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008494 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008495 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008496 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008497 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008498 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008499 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008500 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008501 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008502#ifdef FEAT_FLOAT
8503 {"tan", 1, 1, f_tan},
8504 {"tanh", 1, 1, f_tanh},
8505#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008506 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008507 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008508 {"tolower", 1, 1, f_tolower},
8509 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008510 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008511#ifdef FEAT_FLOAT
8512 {"trunc", 1, 1, f_trunc},
8513#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008515 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008516 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008517 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008518 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"virtcol", 1, 1, f_virtcol},
8520 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008521 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"winbufnr", 1, 1, f_winbufnr},
8523 {"wincol", 0, 0, f_wincol},
8524 {"winheight", 1, 1, f_winheight},
8525 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008526 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008528 {"winrestview", 1, 1, f_winrestview},
8529 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008530 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008531 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008532 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008533 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534};
8535
8536#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8537
8538/*
8539 * Function given to ExpandGeneric() to obtain the list of internal
8540 * or user defined function names.
8541 */
8542 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008543get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008544{
8545 static int intidx = -1;
8546 char_u *name;
8547
8548 if (idx == 0)
8549 intidx = -1;
8550 if (intidx < 0)
8551 {
8552 name = get_user_func_name(xp, idx);
8553 if (name != NULL)
8554 return name;
8555 }
8556 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8557 {
8558 STRCPY(IObuff, functions[intidx].f_name);
8559 STRCAT(IObuff, "(");
8560 if (functions[intidx].f_max_argc == 0)
8561 STRCAT(IObuff, ")");
8562 return IObuff;
8563 }
8564
8565 return NULL;
8566}
8567
8568/*
8569 * Function given to ExpandGeneric() to obtain the list of internal or
8570 * user defined variable or function names.
8571 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008572 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008573get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008574{
8575 static int intidx = -1;
8576 char_u *name;
8577
8578 if (idx == 0)
8579 intidx = -1;
8580 if (intidx < 0)
8581 {
8582 name = get_function_name(xp, idx);
8583 if (name != NULL)
8584 return name;
8585 }
8586 return get_user_var_name(xp, ++intidx);
8587}
8588
8589#endif /* FEAT_CMDL_COMPL */
8590
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008591#if defined(EBCDIC) || defined(PROTO)
8592/*
8593 * Compare struct fst by function name.
8594 */
8595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008596compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008597{
8598 struct fst *p1 = (struct fst *)s1;
8599 struct fst *p2 = (struct fst *)s2;
8600
8601 return STRCMP(p1->f_name, p2->f_name);
8602}
8603
8604/*
8605 * Sort the function table by function name.
8606 * The sorting of the table above is ASCII dependant.
8607 * On machines using EBCDIC we have to sort it.
8608 */
8609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008610sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008611{
8612 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8613
8614 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8615}
8616#endif
8617
8618
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619/*
8620 * Find internal function in table above.
8621 * Return index, or -1 if not found
8622 */
8623 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008624find_internal_func(
8625 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008626{
8627 int first = 0;
8628 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8629 int cmp;
8630 int x;
8631
8632 /*
8633 * Find the function name in the table. Binary search.
8634 */
8635 while (first <= last)
8636 {
8637 x = first + ((unsigned)(last - first) >> 1);
8638 cmp = STRCMP(name, functions[x].f_name);
8639 if (cmp < 0)
8640 last = x - 1;
8641 else if (cmp > 0)
8642 first = x + 1;
8643 else
8644 return x;
8645 }
8646 return -1;
8647}
8648
8649/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8651 * name it contains, otherwise return "name".
8652 */
8653 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008654deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655{
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008657 int cc;
8658
8659 cc = name[*lenp];
8660 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008661 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008662 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008665 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008666 {
8667 *lenp = 0;
8668 return (char_u *)""; /* just in case */
8669 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008670 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008672 }
8673
8674 return name;
8675}
8676
8677/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008678 * Allocate a variable for the result of a function.
8679 * Return OK or FAIL.
8680 */
8681 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008682get_func_tv(
8683 char_u *name, /* name of the function */
8684 int len, /* length of "name" */
8685 typval_T *rettv,
8686 char_u **arg, /* argument, pointing to the '(' */
8687 linenr_T firstline, /* first line of range */
8688 linenr_T lastline, /* last line of range */
8689 int *doesrange, /* return: function handled range */
8690 int evaluate,
8691 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008692{
8693 char_u *argp;
8694 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008695 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008696 int argcount = 0; /* number of arguments found */
8697
8698 /*
8699 * Get the arguments.
8700 */
8701 argp = *arg;
8702 while (argcount < MAX_FUNC_ARGS)
8703 {
8704 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8705 if (*argp == ')' || *argp == ',' || *argp == NUL)
8706 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008707 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8708 {
8709 ret = FAIL;
8710 break;
8711 }
8712 ++argcount;
8713 if (*argp != ',')
8714 break;
8715 }
8716 if (*argp == ')')
8717 ++argp;
8718 else
8719 ret = FAIL;
8720
8721 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008722 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008723 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 {
8726 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008727 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008728 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008729 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731
8732 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008733 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734
8735 *arg = skipwhite(argp);
8736 return ret;
8737}
8738
8739
8740/*
8741 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008742 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008743 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008744 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008746call_func(
8747 char_u *funcname, /* name of the function */
8748 int len, /* length of "name" */
8749 typval_T *rettv, /* return value goes here */
8750 int argcount, /* number of "argvars" */
8751 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008752 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008753 linenr_T firstline, /* first line of range */
8754 linenr_T lastline, /* last line of range */
8755 int *doesrange, /* return: function handled range */
8756 int evaluate,
8757 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758{
8759 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760#define ERROR_UNKNOWN 0
8761#define ERROR_TOOMANY 1
8762#define ERROR_TOOFEW 2
8763#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008764#define ERROR_DICT 4
8765#define ERROR_NONE 5
8766#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008767 int error = ERROR_NONE;
8768 int i;
8769 int llen;
8770 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008771#define FLEN_FIXED 40
8772 char_u fname_buf[FLEN_FIXED + 1];
8773 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008774 char_u *name;
8775
8776 /* Make a copy of the name, if it comes from a funcref variable it could
8777 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008778 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008779 if (name == NULL)
8780 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
8782 /*
8783 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8784 * Change <SNR>123_name() to K_SNR 123_name().
8785 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008787 llen = eval_fname_script(name);
8788 if (llen > 0)
8789 {
8790 fname_buf[0] = K_SPECIAL;
8791 fname_buf[1] = KS_EXTRA;
8792 fname_buf[2] = (int)KE_SNR;
8793 i = 3;
8794 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8795 {
8796 if (current_SID <= 0)
8797 error = ERROR_SCRIPT;
8798 else
8799 {
8800 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8801 i = (int)STRLEN(fname_buf);
8802 }
8803 }
8804 if (i + STRLEN(name + llen) < FLEN_FIXED)
8805 {
8806 STRCPY(fname_buf + i, name + llen);
8807 fname = fname_buf;
8808 }
8809 else
8810 {
8811 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8812 if (fname == NULL)
8813 error = ERROR_OTHER;
8814 else
8815 {
8816 mch_memmove(fname, fname_buf, (size_t)i);
8817 STRCPY(fname + i, name + llen);
8818 }
8819 }
8820 }
8821 else
8822 fname = name;
8823
8824 *doesrange = FALSE;
8825
8826
8827 /* execute the function if no errors detected and executing */
8828 if (evaluate && error == ERROR_NONE)
8829 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008830 char_u *rfname = fname;
8831
8832 /* Ignore "g:" before a function name. */
8833 if (fname[0] == 'g' && fname[1] == ':')
8834 rfname = fname + 2;
8835
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008836 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8837 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 error = ERROR_UNKNOWN;
8839
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008841 {
8842 /*
8843 * User defined function.
8844 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008845 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008846
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008848 /* Trigger FuncUndefined event, may load the function. */
8849 if (fp == NULL
8850 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008851 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008852 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008854 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008855 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 }
8857#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008858 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008859 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008860 {
8861 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008862 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008863 }
8864
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 if (fp != NULL)
8866 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008867 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008869 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008870 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008871 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008874 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008875 else
8876 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008877 int did_save_redo = FALSE;
8878
Bram Moolenaar071d4272004-06-13 20:20:40 +00008879 /*
8880 * Call the user function.
8881 * Save and restore search patterns, script variables and
8882 * redo buffer.
8883 */
8884 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008885#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008886 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008887#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008888 {
8889 saveRedobuff();
8890 did_save_redo = TRUE;
8891 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008892 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008893 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008894 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008895 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8896 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8897 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008898 /* Function was unreferenced while being used, free it
8899 * now. */
8900 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008901 if (did_save_redo)
8902 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 restore_search_patterns();
8904 error = ERROR_NONE;
8905 }
8906 }
8907 }
8908 else
8909 {
8910 /*
8911 * Find the function name in the table, call its implementation.
8912 */
8913 i = find_internal_func(fname);
8914 if (i >= 0)
8915 {
8916 if (argcount < functions[i].f_min_argc)
8917 error = ERROR_TOOFEW;
8918 else if (argcount > functions[i].f_max_argc)
8919 error = ERROR_TOOMANY;
8920 else
8921 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008922 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008923 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008924 error = ERROR_NONE;
8925 }
8926 }
8927 }
8928 /*
8929 * The function call (or "FuncUndefined" autocommand sequence) might
8930 * have been aborted by an error, an interrupt, or an explicitly thrown
8931 * exception that has not been caught so far. This situation can be
8932 * tested for by calling aborting(). For an error in an internal
8933 * function or for the "E132" error in call_user_func(), however, the
8934 * throw point at which the "force_abort" flag (temporarily reset by
8935 * emsg()) is normally updated has not been reached yet. We need to
8936 * update that flag first to make aborting() reliable.
8937 */
8938 update_force_abort();
8939 }
8940 if (error == ERROR_NONE)
8941 ret = OK;
8942
8943 /*
8944 * Report an error unless the argument evaluation or function call has been
8945 * cancelled due to an aborting error, an interrupt, or an exception.
8946 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008947 if (!aborting())
8948 {
8949 switch (error)
8950 {
8951 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008952 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 break;
8954 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008955 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008956 break;
8957 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008958 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008959 name);
8960 break;
8961 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008962 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008963 name);
8964 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008965 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008966 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008967 name);
8968 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008969 }
8970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008971
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 if (fname != name && fname != fname_buf)
8973 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008974 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008975
8976 return ret;
8977}
8978
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008979/*
8980 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008981 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008982 */
8983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008984emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008985{
8986 char_u *p;
8987
8988 if (*name == K_SPECIAL)
8989 p = concat_str((char_u *)"<SNR>", name + 3);
8990 else
8991 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008992 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008993 if (p != name)
8994 vim_free(p);
8995}
8996
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008997/*
8998 * Return TRUE for a non-zero Number and a non-empty String.
8999 */
9000 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009001non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009002{
9003 return ((argvars[0].v_type == VAR_NUMBER
9004 && argvars[0].vval.v_number != 0)
9005 || (argvars[0].v_type == VAR_STRING
9006 && argvars[0].vval.v_string != NULL
9007 && *argvars[0].vval.v_string != NUL));
9008}
9009
Bram Moolenaar071d4272004-06-13 20:20:40 +00009010/*********************************************
9011 * Implementation of the built-in functions
9012 */
9013
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009014#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009015static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009016
9017/*
9018 * Get the float value of "argvars[0]" into "f".
9019 * Returns FAIL when the argument is not a Number or Float.
9020 */
9021 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009022get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009023{
9024 if (argvars[0].v_type == VAR_FLOAT)
9025 {
9026 *f = argvars[0].vval.v_float;
9027 return OK;
9028 }
9029 if (argvars[0].v_type == VAR_NUMBER)
9030 {
9031 *f = (float_T)argvars[0].vval.v_number;
9032 return OK;
9033 }
9034 EMSG(_("E808: Number or Float required"));
9035 return FAIL;
9036}
9037
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009038/*
9039 * "abs(expr)" function
9040 */
9041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009042f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009043{
9044 if (argvars[0].v_type == VAR_FLOAT)
9045 {
9046 rettv->v_type = VAR_FLOAT;
9047 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9048 }
9049 else
9050 {
9051 varnumber_T n;
9052 int error = FALSE;
9053
9054 n = get_tv_number_chk(&argvars[0], &error);
9055 if (error)
9056 rettv->vval.v_number = -1;
9057 else if (n > 0)
9058 rettv->vval.v_number = n;
9059 else
9060 rettv->vval.v_number = -n;
9061 }
9062}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009063
9064/*
9065 * "acos()" function
9066 */
9067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009068f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009069{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009070 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009071
9072 rettv->v_type = VAR_FLOAT;
9073 if (get_float_arg(argvars, &f) == OK)
9074 rettv->vval.v_float = acos(f);
9075 else
9076 rettv->vval.v_float = 0.0;
9077}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009078#endif
9079
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009081 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082 */
9083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009084f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085{
Bram Moolenaar33570922005-01-25 22:26:29 +00009086 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009089 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009090 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009091 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009092 && !tv_check_lock(l->lv_lock,
9093 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009094 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009095 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009096 }
9097 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009098 EMSG(_(e_listreq));
9099}
9100
9101/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009102 * "alloc_fail(id, countdown, repeat)" function
9103 */
9104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009105f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009106{
9107 if (argvars[0].v_type != VAR_NUMBER
9108 || argvars[0].vval.v_number <= 0
9109 || argvars[1].v_type != VAR_NUMBER
9110 || argvars[1].vval.v_number < 0
9111 || argvars[2].v_type != VAR_NUMBER)
9112 EMSG(_(e_invarg));
9113 else
9114 {
9115 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009116 if (alloc_fail_id >= aid_last)
9117 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009118 alloc_fail_countdown = argvars[1].vval.v_number;
9119 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009120 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009121 }
9122}
9123
9124/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009125 * "and(expr, expr)" function
9126 */
9127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009128f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009129{
9130 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9131 & get_tv_number_chk(&argvars[1], NULL);
9132}
9133
9134/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009135 * "append(lnum, string/list)" function
9136 */
9137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009138f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009139{
9140 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009141 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009142 list_T *l = NULL;
9143 listitem_T *li = NULL;
9144 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009145 long added = 0;
9146
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009147 /* When coming here from Insert mode, sync undo, so that this can be
9148 * undone separately from what was previously inserted. */
9149 if (u_sync_once == 2)
9150 {
9151 u_sync_once = 1; /* notify that u_sync() was called */
9152 u_sync(TRUE);
9153 }
9154
Bram Moolenaar0d660222005-01-07 21:51:51 +00009155 lnum = get_tv_lnum(argvars);
9156 if (lnum >= 0
9157 && lnum <= curbuf->b_ml.ml_line_count
9158 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009159 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009161 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009162 l = argvars[1].vval.v_list;
9163 if (l == NULL)
9164 return;
9165 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009166 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009167 for (;;)
9168 {
9169 if (l == NULL)
9170 tv = &argvars[1]; /* append a string */
9171 else if (li == NULL)
9172 break; /* end of list */
9173 else
9174 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009175 line = get_tv_string_chk(tv);
9176 if (line == NULL) /* type error */
9177 {
9178 rettv->vval.v_number = 1; /* Failed */
9179 break;
9180 }
9181 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009182 ++added;
9183 if (l == NULL)
9184 break;
9185 li = li->li_next;
9186 }
9187
9188 appended_lines_mark(lnum, added);
9189 if (curwin->w_cursor.lnum > lnum)
9190 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009192 else
9193 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
9197 * "argc()" function
9198 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009200f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009202 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203}
9204
9205/*
9206 * "argidx()" function
9207 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009209f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009210{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009211 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009212}
9213
9214/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009215 * "arglistid()" function
9216 */
9217 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009218f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009219{
9220 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009221
9222 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009223 wp = find_tabwin(&argvars[0], &argvars[1]);
9224 if (wp != NULL)
9225 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009226}
9227
9228/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009229 * "argv(nr)" function
9230 */
9231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009232f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009233{
9234 int idx;
9235
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009236 if (argvars[0].v_type != VAR_UNKNOWN)
9237 {
9238 idx = get_tv_number_chk(&argvars[0], NULL);
9239 if (idx >= 0 && idx < ARGCOUNT)
9240 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9241 else
9242 rettv->vval.v_string = NULL;
9243 rettv->v_type = VAR_STRING;
9244 }
9245 else if (rettv_list_alloc(rettv) == OK)
9246 for (idx = 0; idx < ARGCOUNT; ++idx)
9247 list_append_string(rettv->vval.v_list,
9248 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009249}
9250
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009251static void prepare_assert_error(garray_T*gap);
9252static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9253static void assert_error(garray_T *gap);
9254static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009255
9256/*
9257 * Prepare "gap" for an assert error and add the sourcing position.
9258 */
9259 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009260prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009261{
9262 char buf[NUMBUFLEN];
9263
9264 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009265 if (sourcing_name != NULL)
9266 {
9267 ga_concat(gap, sourcing_name);
9268 if (sourcing_lnum > 0)
9269 ga_concat(gap, (char_u *)" ");
9270 }
9271 if (sourcing_lnum > 0)
9272 {
9273 sprintf(buf, "line %ld", (long)sourcing_lnum);
9274 ga_concat(gap, (char_u *)buf);
9275 }
9276 if (sourcing_name != NULL || sourcing_lnum > 0)
9277 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009278}
9279
9280/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009281 * Append "str" to "gap", escaping unprintable characters.
9282 * Changes NL to \n, CR to \r, etc.
9283 */
9284 static void
9285ga_concat_esc(garray_T *gap, char_u *str)
9286{
9287 char_u *p;
9288 char_u buf[NUMBUFLEN];
9289
9290 for (p = str; *p != NUL; ++p)
9291 switch (*p)
9292 {
9293 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9294 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9295 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9296 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9297 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9298 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9299 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9300 default:
9301 if (*p < ' ')
9302 {
9303 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9304 ga_concat(gap, buf);
9305 }
9306 else
9307 ga_append(gap, *p);
9308 break;
9309 }
9310}
9311
9312/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009313 * Fill "gap" with information about an assert error.
9314 */
9315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009316fill_assert_error(
9317 garray_T *gap,
9318 typval_T *opt_msg_tv,
9319 char_u *exp_str,
9320 typval_T *exp_tv,
9321 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009322{
9323 char_u numbuf[NUMBUFLEN];
9324 char_u *tofree;
9325
9326 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9327 {
9328 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9329 vim_free(tofree);
9330 }
9331 else
9332 {
9333 ga_concat(gap, (char_u *)"Expected ");
9334 if (exp_str == NULL)
9335 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009336 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009337 vim_free(tofree);
9338 }
9339 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009340 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009341 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009342 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009343 vim_free(tofree);
9344 }
9345}
Bram Moolenaar43345542015-11-29 17:35:35 +01009346
9347/*
9348 * Add an assert error to v:errors.
9349 */
9350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009351assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009352{
9353 struct vimvar *vp = &vimvars[VV_ERRORS];
9354
9355 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9356 /* Make sure v:errors is a list. */
9357 set_vim_var_list(VV_ERRORS, list_alloc());
9358 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9359}
9360
Bram Moolenaar43345542015-11-29 17:35:35 +01009361/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009362 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009363 */
9364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009365f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009366{
9367 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009368
9369 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9370 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371 prepare_assert_error(&ga);
9372 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9373 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009374 ga_clear(&ga);
9375 }
9376}
9377
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009378/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009379 * "assert_exception(string[, msg])" function
9380 */
9381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009382f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009383{
9384 garray_T ga;
9385 char *error;
9386
9387 error = (char *)get_tv_string_chk(&argvars[0]);
9388 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9389 {
9390 prepare_assert_error(&ga);
9391 ga_concat(&ga, (char_u *)"v:exception is not set");
9392 assert_error(&ga);
9393 ga_clear(&ga);
9394 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009395 else if (error != NULL
9396 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009397 {
9398 prepare_assert_error(&ga);
9399 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9400 &vimvars[VV_EXCEPTION].vv_tv);
9401 assert_error(&ga);
9402 ga_clear(&ga);
9403 }
9404}
9405
9406/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009407 * "assert_fails(cmd [, error])" function
9408 */
9409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009410f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009411{
9412 char_u *cmd = get_tv_string_chk(&argvars[0]);
9413 garray_T ga;
9414
9415 called_emsg = FALSE;
9416 suppress_errthrow = TRUE;
9417 emsg_silent = TRUE;
9418 do_cmdline_cmd(cmd);
9419 if (!called_emsg)
9420 {
9421 prepare_assert_error(&ga);
9422 ga_concat(&ga, (char_u *)"command did not fail: ");
9423 ga_concat(&ga, cmd);
9424 assert_error(&ga);
9425 ga_clear(&ga);
9426 }
9427 else if (argvars[1].v_type != VAR_UNKNOWN)
9428 {
9429 char_u buf[NUMBUFLEN];
9430 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9431
9432 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9433 {
9434 prepare_assert_error(&ga);
9435 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9436 &vimvars[VV_ERRMSG].vv_tv);
9437 assert_error(&ga);
9438 ga_clear(&ga);
9439 }
9440 }
9441
9442 called_emsg = FALSE;
9443 suppress_errthrow = FALSE;
9444 emsg_silent = FALSE;
9445 emsg_on_display = FALSE;
9446 set_vim_var_string(VV_ERRMSG, NULL, 0);
9447}
9448
9449/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009450 * Common for assert_true() and assert_false().
9451 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009454{
9455 int error = FALSE;
9456 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009457
Bram Moolenaar37127922016-02-06 20:29:28 +01009458 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009459 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009460 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009461 if (argvars[0].v_type != VAR_NUMBER
9462 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9463 || error)
9464 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009465 prepare_assert_error(&ga);
9466 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009467 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009468 NULL, &argvars[0]);
9469 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009470 ga_clear(&ga);
9471 }
9472}
9473
9474/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009475 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009476 */
9477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009478f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009479{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009480 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009481}
9482
9483/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009484 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009485 */
9486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009487f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009488{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009489 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009490}
9491
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009492#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009493/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009494 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009497f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009498{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009499 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009500
9501 rettv->v_type = VAR_FLOAT;
9502 if (get_float_arg(argvars, &f) == OK)
9503 rettv->vval.v_float = asin(f);
9504 else
9505 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009506}
9507
9508/*
9509 * "atan()" function
9510 */
9511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009512f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009513{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009514 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009515
9516 rettv->v_type = VAR_FLOAT;
9517 if (get_float_arg(argvars, &f) == OK)
9518 rettv->vval.v_float = atan(f);
9519 else
9520 rettv->vval.v_float = 0.0;
9521}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009522
9523/*
9524 * "atan2()" function
9525 */
9526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009527f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009528{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009529 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009530
9531 rettv->v_type = VAR_FLOAT;
9532 if (get_float_arg(argvars, &fx) == OK
9533 && get_float_arg(&argvars[1], &fy) == OK)
9534 rettv->vval.v_float = atan2(fx, fy);
9535 else
9536 rettv->vval.v_float = 0.0;
9537}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009538#endif
9539
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540/*
9541 * "browse(save, title, initdir, default)" function
9542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009544f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009545{
9546#ifdef FEAT_BROWSE
9547 int save;
9548 char_u *title;
9549 char_u *initdir;
9550 char_u *defname;
9551 char_u buf[NUMBUFLEN];
9552 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009555 save = get_tv_number_chk(&argvars[0], &error);
9556 title = get_tv_string_chk(&argvars[1]);
9557 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9558 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009559
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009560 if (error || title == NULL || initdir == NULL || defname == NULL)
9561 rettv->vval.v_string = NULL;
9562 else
9563 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564 do_browse(save ? BROWSE_SAVE : 0,
9565 title, defname, NULL, initdir, NULL, curbuf);
9566#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009567 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009569 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009570}
9571
9572/*
9573 * "browsedir(title, initdir)" function
9574 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009576f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009577{
9578#ifdef FEAT_BROWSE
9579 char_u *title;
9580 char_u *initdir;
9581 char_u buf[NUMBUFLEN];
9582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 title = get_tv_string_chk(&argvars[0]);
9584 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009586 if (title == NULL || initdir == NULL)
9587 rettv->vval.v_string = NULL;
9588 else
9589 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009590 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009591#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009592 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009594 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595}
9596
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009597static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009598
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599/*
9600 * Find a buffer by number or exact name.
9601 */
9602 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009603find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009604{
9605 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009606
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009607 if (avar->v_type == VAR_NUMBER)
9608 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009609 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009611 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009612 if (buf == NULL)
9613 {
9614 /* No full path name match, try a match with a URL or a "nofile"
9615 * buffer, these don't use the full path. */
9616 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9617 if (buf->b_fname != NULL
9618 && (path_with_url(buf->b_fname)
9619#ifdef FEAT_QUICKFIX
9620 || bt_nofile(buf)
9621#endif
9622 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009623 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009624 break;
9625 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626 }
9627 return buf;
9628}
9629
9630/*
9631 * "bufexists(expr)" function
9632 */
9633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009634f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009635{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009636 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637}
9638
9639/*
9640 * "buflisted(expr)" function
9641 */
9642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009643f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009644{
9645 buf_T *buf;
9646
9647 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649}
9650
9651/*
9652 * "bufloaded(expr)" function
9653 */
9654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009655f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009656{
9657 buf_T *buf;
9658
9659 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009660 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009661}
9662
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009663static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009664
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665/*
9666 * Get buffer by number or pattern.
9667 */
9668 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009669get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009670{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009671 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009672 int save_magic;
9673 char_u *save_cpo;
9674 buf_T *buf;
9675
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009676 if (tv->v_type == VAR_NUMBER)
9677 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009678 if (tv->v_type != VAR_STRING)
9679 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009680 if (name == NULL || *name == NUL)
9681 return curbuf;
9682 if (name[0] == '$' && name[1] == NUL)
9683 return lastbuf;
9684
9685 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9686 save_magic = p_magic;
9687 p_magic = TRUE;
9688 save_cpo = p_cpo;
9689 p_cpo = (char_u *)"";
9690
9691 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009692 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 p_magic = save_magic;
9695 p_cpo = save_cpo;
9696
9697 /* If not found, try expanding the name, like done for bufexists(). */
9698 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700
9701 return buf;
9702}
9703
9704/*
9705 * "bufname(expr)" function
9706 */
9707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 buf_T *buf;
9711
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009712 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009714 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009717 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009719 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720 --emsg_off;
9721}
9722
9723/*
9724 * "bufnr(expr)" function
9725 */
9726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009727f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728{
9729 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009730 int error = FALSE;
9731 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009732
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009733 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009735 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009736 --emsg_off;
9737
9738 /* If the buffer isn't found and the second argument is not zero create a
9739 * new buffer. */
9740 if (buf == NULL
9741 && argvars[1].v_type != VAR_UNKNOWN
9742 && get_tv_number_chk(&argvars[1], &error) != 0
9743 && !error
9744 && (name = get_tv_string_chk(&argvars[0])) != NULL
9745 && !error)
9746 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9747
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009749 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009750 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752}
9753
9754/*
9755 * "bufwinnr(nr)" function
9756 */
9757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760#ifdef FEAT_WINDOWS
9761 win_T *wp;
9762 int winnr = 0;
9763#endif
9764 buf_T *buf;
9765
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009766 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009767 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009768 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769#ifdef FEAT_WINDOWS
9770 for (wp = firstwin; wp; wp = wp->w_next)
9771 {
9772 ++winnr;
9773 if (wp->w_buffer == buf)
9774 break;
9775 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009776 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009777#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779#endif
9780 --emsg_off;
9781}
9782
9783/*
9784 * "byte2line(byte)" function
9785 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009787f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788{
9789#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009790 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791#else
9792 long boff = 0;
9793
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009794 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009796 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009798 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009799 (linenr_T)0, &boff);
9800#endif
9801}
9802
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009804byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009805{
9806#ifdef FEAT_MBYTE
9807 char_u *t;
9808#endif
9809 char_u *str;
9810 long idx;
9811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 str = get_tv_string_chk(&argvars[0]);
9813 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009815 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009816 return;
9817
9818#ifdef FEAT_MBYTE
9819 t = str;
9820 for ( ; idx > 0; idx--)
9821 {
9822 if (*t == NUL) /* EOL reached */
9823 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009824 if (enc_utf8 && comp)
9825 t += utf_ptr2len(t);
9826 else
9827 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009828 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009829 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009830#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009831 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009832 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009833#endif
9834}
9835
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009836/*
9837 * "byteidx()" function
9838 */
9839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009840f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009841{
9842 byteidx(argvars, rettv, FALSE);
9843}
9844
9845/*
9846 * "byteidxcomp()" function
9847 */
9848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009849f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009850{
9851 byteidx(argvars, rettv, TRUE);
9852}
9853
Bram Moolenaardb913952012-06-29 12:54:53 +02009854 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855func_call(
9856 char_u *name,
9857 typval_T *args,
9858 dict_T *selfdict,
9859 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009860{
9861 listitem_T *item;
9862 typval_T argv[MAX_FUNC_ARGS + 1];
9863 int argc = 0;
9864 int dummy;
9865 int r = 0;
9866
9867 for (item = args->vval.v_list->lv_first; item != NULL;
9868 item = item->li_next)
9869 {
9870 if (argc == MAX_FUNC_ARGS)
9871 {
9872 EMSG(_("E699: Too many arguments"));
9873 break;
9874 }
9875 /* Make a copy of each argument. This is needed to be able to set
9876 * v_lock to VAR_FIXED in the copy without changing the original list.
9877 */
9878 copy_tv(&item->li_tv, &argv[argc++]);
9879 }
9880
9881 if (item == NULL)
9882 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9883 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9884 &dummy, TRUE, selfdict);
9885
9886 /* Free the arguments. */
9887 while (argc > 0)
9888 clear_tv(&argv[--argc]);
9889
9890 return r;
9891}
9892
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009893/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894 * "call(func, arglist)" function
9895 */
9896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009898{
9899 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009900 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009901
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009902 if (argvars[1].v_type != VAR_LIST)
9903 {
9904 EMSG(_(e_listreq));
9905 return;
9906 }
9907 if (argvars[1].vval.v_list == NULL)
9908 return;
9909
9910 if (argvars[0].v_type == VAR_FUNC)
9911 func = argvars[0].vval.v_string;
9912 else
9913 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009914 if (*func == NUL)
9915 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009916
Bram Moolenaare9a41262005-01-15 22:18:47 +00009917 if (argvars[2].v_type != VAR_UNKNOWN)
9918 {
9919 if (argvars[2].v_type != VAR_DICT)
9920 {
9921 EMSG(_(e_dictreq));
9922 return;
9923 }
9924 selfdict = argvars[2].vval.v_dict;
9925 }
9926
Bram Moolenaardb913952012-06-29 12:54:53 +02009927 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009928}
9929
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009930#ifdef FEAT_FLOAT
9931/*
9932 * "ceil({float})" function
9933 */
9934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009935f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009936{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009937 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009938
9939 rettv->v_type = VAR_FLOAT;
9940 if (get_float_arg(argvars, &f) == OK)
9941 rettv->vval.v_float = ceil(f);
9942 else
9943 rettv->vval.v_float = 0.0;
9944}
9945#endif
9946
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009947#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009948/*
9949 * Get a callback from "arg". It can be a Funcref or a function name.
9950 * When "arg" is zero return an empty string.
9951 * Return NULL for an invalid argument.
9952 */
9953 static char_u *
9954get_callback(typval_T *arg)
9955{
9956 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9957 return arg->vval.v_string;
9958 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9959 return (char_u *)"";
9960 EMSG(_("E999: Invalid callback argument"));
9961 return NULL;
9962}
9963
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009964 static int
9965handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9966{
9967 char_u *val = get_tv_string(item);
9968
9969 opt->jo_set |= jo;
9970 if (STRCMP(val, "nl") == 0)
9971 *modep = MODE_NL;
9972 else if (STRCMP(val, "raw") == 0)
9973 *modep = MODE_RAW;
9974 else if (STRCMP(val, "js") == 0)
9975 *modep = MODE_JS;
9976 else if (STRCMP(val, "json") == 0)
9977 *modep = MODE_JSON;
9978 else
9979 {
9980 EMSG2(_(e_invarg2), val);
9981 return FAIL;
9982 }
9983 return OK;
9984}
9985
Bram Moolenaar187db502016-02-27 14:44:26 +01009986 static int
9987handle_io(typval_T *item, int part, jobopt_T *opt)
9988{
9989 char_u *val = get_tv_string(item);
9990
9991 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9992 if (STRCMP(val, "null") == 0)
9993 opt->jo_io[part] = JIO_NULL;
9994 else if (STRCMP(val, "pipe") == 0)
9995 opt->jo_io[part] = JIO_PIPE;
9996 else if (STRCMP(val, "file") == 0)
9997 opt->jo_io[part] = JIO_FILE;
9998 else if (STRCMP(val, "buffer") == 0)
9999 opt->jo_io[part] = JIO_BUFFER;
10000 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
10001 opt->jo_io[part] = JIO_OUT;
10002 else
10003 {
10004 EMSG2(_(e_invarg2), val);
10005 return FAIL;
10006 }
10007 return OK;
10008}
10009
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010010 static void
10011clear_job_options(jobopt_T *opt)
10012{
10013 vim_memset(opt, 0, sizeof(jobopt_T));
10014}
10015
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010016/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010017 * Get the PART_ number from the first character of an option name.
10018 */
10019 static int
10020part_from_char(int c)
10021{
10022 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10023}
10024
10025/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010026 * Get the option entries from the dict in "tv", parse them and put the result
10027 * in "opt".
10028 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010029 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010030 */
10031 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010032get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010033{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010034 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010035 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010036 dict_T *dict;
10037 int todo;
10038 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010039 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010040
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010041 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010042 if (tv->v_type == VAR_UNKNOWN)
10043 return OK;
10044 if (tv->v_type != VAR_DICT)
10045 {
10046 EMSG(_(e_invarg));
10047 return FAIL;
10048 }
10049 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010050 if (dict == NULL)
10051 return OK;
10052
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010053 todo = (int)dict->dv_hashtab.ht_used;
10054 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10055 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010056 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010057 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010058
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010059 if (STRCMP(hi->hi_key, "mode") == 0)
10060 {
10061 if (!(supported & JO_MODE))
10062 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010063 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010064 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010065 }
10066 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10067 {
10068 if (!(supported & JO_IN_MODE))
10069 break;
10070 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10071 == FAIL)
10072 return FAIL;
10073 }
10074 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10075 {
10076 if (!(supported & JO_OUT_MODE))
10077 break;
10078 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10079 == FAIL)
10080 return FAIL;
10081 }
10082 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10083 {
10084 if (!(supported & JO_ERR_MODE))
10085 break;
10086 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10087 == FAIL)
10088 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010089 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010090 else if (STRCMP(hi->hi_key, "in-io") == 0
10091 || STRCMP(hi->hi_key, "out-io") == 0
10092 || STRCMP(hi->hi_key, "err-io") == 0)
10093 {
10094 if (!(supported & JO_OUT_IO))
10095 break;
10096 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10097 return FAIL;
10098 }
10099 else if (STRCMP(hi->hi_key, "in-name") == 0
10100 || STRCMP(hi->hi_key, "out-name") == 0
10101 || STRCMP(hi->hi_key, "err-name") == 0)
10102 {
10103 part = part_from_char(*hi->hi_key);
10104
10105 if (!(supported & JO_OUT_IO))
10106 break;
10107 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10108 opt->jo_io_name[part] =
10109 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10110 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010111 else if (STRCMP(hi->hi_key, "callback") == 0)
10112 {
10113 if (!(supported & JO_CALLBACK))
10114 break;
10115 opt->jo_set |= JO_CALLBACK;
10116 opt->jo_callback = get_callback(item);
10117 if (opt->jo_callback == NULL)
10118 {
10119 EMSG2(_(e_invarg2), "callback");
10120 return FAIL;
10121 }
10122 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010123 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10124 {
10125 if (!(supported & JO_OUT_CALLBACK))
10126 break;
10127 opt->jo_set |= JO_OUT_CALLBACK;
10128 opt->jo_out_cb = get_callback(item);
10129 if (opt->jo_out_cb == NULL)
10130 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010131 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010132 return FAIL;
10133 }
10134 }
10135 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10136 {
10137 if (!(supported & JO_ERR_CALLBACK))
10138 break;
10139 opt->jo_set |= JO_ERR_CALLBACK;
10140 opt->jo_err_cb = get_callback(item);
10141 if (opt->jo_err_cb == NULL)
10142 {
10143 EMSG2(_(e_invarg2), "err-cb");
10144 return FAIL;
10145 }
10146 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010147 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10148 {
10149 if (!(supported & JO_CLOSE_CALLBACK))
10150 break;
10151 opt->jo_set |= JO_CLOSE_CALLBACK;
10152 opt->jo_close_cb = get_callback(item);
10153 if (opt->jo_close_cb == NULL)
10154 {
10155 EMSG2(_(e_invarg2), "close-cb");
10156 return FAIL;
10157 }
10158 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010159 else if (STRCMP(hi->hi_key, "waittime") == 0)
10160 {
10161 if (!(supported & JO_WAITTIME))
10162 break;
10163 opt->jo_set |= JO_WAITTIME;
10164 opt->jo_waittime = get_tv_number(item);
10165 }
10166 else if (STRCMP(hi->hi_key, "timeout") == 0)
10167 {
10168 if (!(supported & JO_TIMEOUT))
10169 break;
10170 opt->jo_set |= JO_TIMEOUT;
10171 opt->jo_timeout = get_tv_number(item);
10172 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010173 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10174 {
10175 if (!(supported & JO_OUT_TIMEOUT))
10176 break;
10177 opt->jo_set |= JO_OUT_TIMEOUT;
10178 opt->jo_out_timeout = get_tv_number(item);
10179 }
10180 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10181 {
10182 if (!(supported & JO_ERR_TIMEOUT))
10183 break;
10184 opt->jo_set |= JO_ERR_TIMEOUT;
10185 opt->jo_err_timeout = get_tv_number(item);
10186 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010187 else if (STRCMP(hi->hi_key, "part") == 0)
10188 {
10189 if (!(supported & JO_PART))
10190 break;
10191 opt->jo_set |= JO_PART;
10192 val = get_tv_string(item);
10193 if (STRCMP(val, "err") == 0)
10194 opt->jo_part = PART_ERR;
10195 else
10196 {
10197 EMSG2(_(e_invarg2), val);
10198 return FAIL;
10199 }
10200 }
10201 else if (STRCMP(hi->hi_key, "id") == 0)
10202 {
10203 if (!(supported & JO_ID))
10204 break;
10205 opt->jo_set |= JO_ID;
10206 opt->jo_id = get_tv_number(item);
10207 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010208 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10209 {
10210 if (!(supported & JO_STOPONEXIT))
10211 break;
10212 opt->jo_set |= JO_STOPONEXIT;
10213 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10214 opt->jo_soe_buf);
10215 if (opt->jo_stoponexit == NULL)
10216 {
10217 EMSG2(_(e_invarg2), "stoponexit");
10218 return FAIL;
10219 }
10220 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010221 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10222 {
10223 if (!(supported & JO_EXIT_CB))
10224 break;
10225 opt->jo_set |= JO_EXIT_CB;
10226 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010227 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010228 {
10229 EMSG2(_(e_invarg2), "exit-cb");
10230 return FAIL;
10231 }
10232 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010233 else
10234 break;
10235 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010236 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010237 if (todo > 0)
10238 {
10239 EMSG2(_(e_invarg2), hi->hi_key);
10240 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010241 }
10242
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010243 return OK;
10244}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010245#endif
10246
10247#ifdef FEAT_CHANNEL
10248/*
10249 * Get the channel from the argument.
10250 * Returns NULL if the handle is invalid.
10251 */
10252 static channel_T *
10253get_channel_arg(typval_T *tv)
10254{
10255 channel_T *channel;
10256
10257 if (tv->v_type != VAR_CHANNEL)
10258 {
10259 EMSG2(_(e_invarg2), get_tv_string(tv));
10260 return NULL;
10261 }
10262 channel = tv->vval.v_channel;
10263
10264 if (channel == NULL || !channel_is_open(channel))
10265 {
10266 EMSG(_("E906: not an open channel"));
10267 return NULL;
10268 }
10269 return channel;
10270}
10271
10272/*
10273 * "ch_close()" function
10274 */
10275 static void
10276f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10277{
10278 channel_T *channel = get_channel_arg(&argvars[0]);
10279
10280 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010281 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010282 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010283 channel_clear(channel);
10284 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010285}
10286
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010287/*
10288 * "ch_getbufnr()" function
10289 */
10290 static void
10291f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10292{
10293 channel_T *channel = get_channel_arg(&argvars[0]);
10294
10295 rettv->vval.v_number = -1;
10296 if (channel != NULL)
10297 {
10298 char_u *what = get_tv_string(&argvars[1]);
10299 int part;
10300
10301 if (STRCMP(what, "err") == 0)
10302 part = PART_ERR;
10303 else if (STRCMP(what, "out") == 0)
10304 part = PART_OUT;
10305 else if (STRCMP(what, "in") == 0)
10306 part = PART_IN;
10307 else
10308 part = PART_SOCK;
10309 if (channel->ch_part[part].ch_buffer != NULL)
10310 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10311 }
10312}
10313
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010314# ifdef FEAT_JOB
10315/*
10316 * "ch_getjob()" function
10317 */
10318 static void
10319f_ch_getjob(typval_T *argvars, typval_T *rettv)
10320{
10321 channel_T *channel = get_channel_arg(&argvars[0]);
10322
10323 if (channel != NULL)
10324 {
10325 rettv->v_type = VAR_JOB;
10326 rettv->vval.v_job = channel->ch_job;
10327 if (channel->ch_job != NULL)
10328 ++channel->ch_job->jv_refcount;
10329 }
10330}
10331# endif
10332
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010333/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010334 * "ch_log()" function
10335 */
10336 static void
10337f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10338{
10339 char_u *msg = get_tv_string(&argvars[0]);
10340 channel_T *channel = NULL;
10341
10342 if (argvars[1].v_type != VAR_UNKNOWN)
10343 channel = get_channel_arg(&argvars[1]);
10344
10345 ch_log(channel, (char *)msg);
10346}
10347
10348/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010349 * "ch_logfile()" function
10350 */
10351 static void
10352f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10353{
10354 char_u *fname;
10355 char_u *opt = (char_u *)"";
10356 char_u buf[NUMBUFLEN];
10357 FILE *file = NULL;
10358
10359 fname = get_tv_string(&argvars[0]);
10360 if (argvars[1].v_type == VAR_STRING)
10361 opt = get_tv_string_buf(&argvars[1], buf);
10362 if (*fname != NUL)
10363 {
10364 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10365 if (file == NULL)
10366 {
10367 EMSG2(_(e_notopen), fname);
10368 return;
10369 }
10370 }
10371 ch_logfile(file);
10372}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010373
10374/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010375 * "ch_open()" function
10376 */
10377 static void
10378f_ch_open(typval_T *argvars, typval_T *rettv)
10379{
10380 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010381 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010382 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010383 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010384 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010385 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010386
10387 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010388 rettv->v_type = VAR_CHANNEL;
10389 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010390
10391 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010392 if (argvars[1].v_type != VAR_UNKNOWN
10393 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010394 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010395 EMSG(_(e_invarg));
10396 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010397 }
10398
10399 /* parse address */
10400 p = vim_strchr(address, ':');
10401 if (p == NULL)
10402 {
10403 EMSG2(_(e_invarg2), address);
10404 return;
10405 }
10406 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010407 port = strtol((char *)p, &rest, 10);
10408 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010409 {
10410 p[-1] = ':';
10411 EMSG2(_(e_invarg2), address);
10412 return;
10413 }
10414
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010415 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010416 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010417 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010418 opt.jo_timeout = 2000;
10419 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010420 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010421 return;
10422 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010423 {
10424 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010425 return;
10426 }
10427
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010428 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010429 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010430 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010431 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010432 opt.jo_set = JO_ALL;
10433 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010434 }
10435}
10436
10437/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010438 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010439 */
10440 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010441common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010442{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010443 channel_T *channel;
10444 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010445 jobopt_T opt;
10446 int mode;
10447 int timeout;
10448 int id = -1;
10449 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010450
10451 /* return an empty string by default */
10452 rettv->v_type = VAR_STRING;
10453 rettv->vval.v_string = NULL;
10454
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010455 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010456 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10457 == FAIL)
10458 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010459
Bram Moolenaar77073442016-02-13 23:23:53 +010010460 channel = get_channel_arg(&argvars[0]);
10461 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010462 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010463 if (opt.jo_set & JO_PART)
10464 part = opt.jo_part;
10465 else
10466 part = channel_part_read(channel);
10467 mode = channel_get_mode(channel, part);
10468 timeout = channel_get_timeout(channel, part);
10469 if (opt.jo_set & JO_TIMEOUT)
10470 timeout = opt.jo_timeout;
10471
10472 if (raw || mode == MODE_RAW || mode == MODE_NL)
10473 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10474 else
10475 {
10476 if (opt.jo_set & JO_ID)
10477 id = opt.jo_id;
10478 channel_read_json_block(channel, part, timeout, id, &listtv);
10479 if (listtv != NULL)
Bram Moolenaard6051b52016-02-28 15:49:03 +010010480 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010481 *rettv = *listtv;
Bram Moolenaard6051b52016-02-28 15:49:03 +010010482 vim_free(listtv);
10483 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010484 else
10485 {
10486 rettv->v_type = VAR_SPECIAL;
10487 rettv->vval.v_number = VVAL_NONE;
10488 }
10489 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010490 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010491}
10492
10493/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010494 * "ch_read()" function
10495 */
10496 static void
10497f_ch_read(typval_T *argvars, typval_T *rettv)
10498{
10499 common_channel_read(argvars, rettv, FALSE);
10500}
10501
10502/*
10503 * "ch_readraw()" function
10504 */
10505 static void
10506f_ch_readraw(typval_T *argvars, typval_T *rettv)
10507{
10508 common_channel_read(argvars, rettv, TRUE);
10509}
10510
10511/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010512 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010513 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010514 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010515 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010516 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010517 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010518send_common(
10519 typval_T *argvars,
10520 char_u *text,
10521 int id,
10522 int eval,
10523 char *fun,
10524 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010525{
Bram Moolenaar77073442016-02-13 23:23:53 +010010526 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010527 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010528 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010529
Bram Moolenaar77073442016-02-13 23:23:53 +010010530 channel = get_channel_arg(&argvars[0]);
10531 if (channel == NULL)
10532 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010533 part_send = channel_part_send(channel);
10534 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010535
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010536 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010537 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10538 return NULL;
10539
Bram Moolenaara07fec92016-02-05 21:04:08 +010010540 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010541 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10542 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010543 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010544 {
10545 if (eval)
10546 {
10547 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10548 return NULL;
10549 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010550 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010551 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010552
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010553 if (channel_send(channel, part_send, text, fun) == OK
10554 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010555 return channel;
10556 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010557}
10558
10559/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010560 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010561 */
10562 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010563ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564{
10565 char_u *text;
10566 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010567 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010568 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010569 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010570 int part_send;
10571 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010572 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010573
10574 /* return an empty string by default */
10575 rettv->v_type = VAR_STRING;
10576 rettv->vval.v_string = NULL;
10577
Bram Moolenaar77073442016-02-13 23:23:53 +010010578 channel = get_channel_arg(&argvars[0]);
10579 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010580 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010581 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010582
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010583 ch_mode = channel_get_mode(channel, part_send);
10584 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010585 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010586 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010587 return;
10588 }
10589
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010590 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010591 text = json_encode_nr_expr(id, &argvars[1],
10592 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010593 if (text == NULL)
10594 return;
10595
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010596 channel = send_common(argvars, text, id, eval,
10597 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010598 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010599 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010600 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010601 /* TODO: timeout from options */
10602 timeout = channel_get_timeout(channel, part_read);
10603 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10604 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010605 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010606 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010607
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010608 /* Move the item from the list and then change the type to
10609 * avoid the value being freed. */
10610 *rettv = list->lv_last->li_tv;
10611 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010612 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010613 }
10614 }
10615}
10616
10617/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010618 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010619 */
10620 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010621f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10622{
10623 ch_expr_common(argvars, rettv, TRUE);
10624}
10625
10626/*
10627 * "ch_sendexpr()" function
10628 */
10629 static void
10630f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10631{
10632 ch_expr_common(argvars, rettv, FALSE);
10633}
10634
10635/*
10636 * common for "ch_evalraw()" and "ch_sendraw()"
10637 */
10638 static void
10639ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010640{
10641 char_u buf[NUMBUFLEN];
10642 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010643 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010644 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010645 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010646
10647 /* return an empty string by default */
10648 rettv->v_type = VAR_STRING;
10649 rettv->vval.v_string = NULL;
10650
10651 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010652 channel = send_common(argvars, text, 0, eval,
10653 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10654 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010655 {
10656 /* TODO: timeout from options */
10657 timeout = channel_get_timeout(channel, part_read);
10658 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10659 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010660}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010661
10662/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010663 * "ch_evalraw()" function
10664 */
10665 static void
10666f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10667{
10668 ch_raw_common(argvars, rettv, TRUE);
10669}
10670
10671/*
10672 * "ch_sendraw()" function
10673 */
10674 static void
10675f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10676{
10677 ch_raw_common(argvars, rettv, FALSE);
10678}
10679
10680/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010681 * "ch_setoptions()" function
10682 */
10683 static void
10684f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10685{
10686 channel_T *channel;
10687 jobopt_T opt;
10688
10689 channel = get_channel_arg(&argvars[0]);
10690 if (channel == NULL)
10691 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010692 clear_job_options(&opt);
10693 if (get_job_options(&argvars[1], &opt,
10694 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010695 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010696 channel_set_options(channel, &opt);
10697}
10698
10699/*
10700 * "ch_status()" function
10701 */
10702 static void
10703f_ch_status(typval_T *argvars, typval_T *rettv)
10704{
10705 /* return an empty string by default */
10706 rettv->v_type = VAR_STRING;
10707
10708 if (argvars[0].v_type != VAR_CHANNEL)
10709 {
10710 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10711 rettv->vval.v_string = NULL;
10712 }
10713 else
10714 rettv->vval.v_string = vim_strsave(
10715 (char_u *)channel_status(argvars[0].vval.v_channel));
10716}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010717#endif
10718
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010719/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010720 * "changenr()" function
10721 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010723f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010724{
10725 rettv->vval.v_number = curbuf->b_u_seq_cur;
10726}
10727
10728/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 * "char2nr(string)" function
10730 */
10731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010732f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010733{
10734#ifdef FEAT_MBYTE
10735 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010736 {
10737 int utf8 = 0;
10738
10739 if (argvars[1].v_type != VAR_UNKNOWN)
10740 utf8 = get_tv_number_chk(&argvars[1], NULL);
10741
10742 if (utf8)
10743 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10744 else
10745 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 else
10748#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010749 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750}
10751
10752/*
10753 * "cindent(lnum)" function
10754 */
10755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010756f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757{
10758#ifdef FEAT_CINDENT
10759 pos_T pos;
10760 linenr_T lnum;
10761
10762 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010763 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010764 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10765 {
10766 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010767 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 curwin->w_cursor = pos;
10769 }
10770 else
10771#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010772 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773}
10774
10775/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010776 * "clearmatches()" function
10777 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010779f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010780{
10781#ifdef FEAT_SEARCH_EXTRA
10782 clear_matches(curwin);
10783#endif
10784}
10785
10786/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 * "col(string)" function
10788 */
10789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010790f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010791{
10792 colnr_T col = 0;
10793 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010794 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010795
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010796 fp = var2fpos(&argvars[0], FALSE, &fnum);
10797 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 {
10799 if (fp->col == MAXCOL)
10800 {
10801 /* '> can be MAXCOL, get the length of the line then */
10802 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010803 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010804 else
10805 col = MAXCOL;
10806 }
10807 else
10808 {
10809 col = fp->col + 1;
10810#ifdef FEAT_VIRTUALEDIT
10811 /* col(".") when the cursor is on the NUL at the end of the line
10812 * because of "coladd" can be seen as an extra column. */
10813 if (virtual_active() && fp == &curwin->w_cursor)
10814 {
10815 char_u *p = ml_get_cursor();
10816
10817 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10818 curwin->w_virtcol - curwin->w_cursor.coladd))
10819 {
10820# ifdef FEAT_MBYTE
10821 int l;
10822
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010823 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010824 col += l;
10825# else
10826 if (*p != NUL && p[1] == NUL)
10827 ++col;
10828# endif
10829 }
10830 }
10831#endif
10832 }
10833 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010834 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010835}
10836
Bram Moolenaar572cb562005-08-05 21:35:02 +000010837#if defined(FEAT_INS_EXPAND)
10838/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010839 * "complete()" function
10840 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010842f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010843{
10844 int startcol;
10845
10846 if ((State & INSERT) == 0)
10847 {
10848 EMSG(_("E785: complete() can only be used in Insert mode"));
10849 return;
10850 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010851
10852 /* Check for undo allowed here, because if something was already inserted
10853 * the line was already saved for undo and this check isn't done. */
10854 if (!undo_allowed())
10855 return;
10856
Bram Moolenaarade00832006-03-10 21:46:58 +000010857 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10858 {
10859 EMSG(_(e_invarg));
10860 return;
10861 }
10862
10863 startcol = get_tv_number_chk(&argvars[0], NULL);
10864 if (startcol <= 0)
10865 return;
10866
10867 set_completion(startcol - 1, argvars[1].vval.v_list);
10868}
10869
10870/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010871 * "complete_add()" function
10872 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010874f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010875{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010876 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010877}
10878
10879/*
10880 * "complete_check()" function
10881 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010883f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010884{
10885 int saved = RedrawingDisabled;
10886
10887 RedrawingDisabled = 0;
10888 ins_compl_check_keys(0);
10889 rettv->vval.v_number = compl_interrupted;
10890 RedrawingDisabled = saved;
10891}
10892#endif
10893
Bram Moolenaar071d4272004-06-13 20:20:40 +000010894/*
10895 * "confirm(message, buttons[, default [, type]])" function
10896 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010898f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010899{
10900#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10901 char_u *message;
10902 char_u *buttons = NULL;
10903 char_u buf[NUMBUFLEN];
10904 char_u buf2[NUMBUFLEN];
10905 int def = 1;
10906 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 char_u *typestr;
10908 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010910 message = get_tv_string_chk(&argvars[0]);
10911 if (message == NULL)
10912 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010913 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010914 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010915 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10916 if (buttons == NULL)
10917 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010918 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010920 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010921 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010923 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10924 if (typestr == NULL)
10925 error = TRUE;
10926 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010927 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010928 switch (TOUPPER_ASC(*typestr))
10929 {
10930 case 'E': type = VIM_ERROR; break;
10931 case 'Q': type = VIM_QUESTION; break;
10932 case 'I': type = VIM_INFO; break;
10933 case 'W': type = VIM_WARNING; break;
10934 case 'G': type = VIM_GENERIC; break;
10935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 }
10938 }
10939 }
10940
10941 if (buttons == NULL || *buttons == NUL)
10942 buttons = (char_u *)_("&Ok");
10943
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010944 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010945 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010946 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010947#endif
10948}
10949
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010950/*
10951 * "copy()" function
10952 */
10953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010954f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010955{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010956 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010957}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010959#ifdef FEAT_FLOAT
10960/*
10961 * "cos()" function
10962 */
10963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010964f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010965{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010966 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010967
10968 rettv->v_type = VAR_FLOAT;
10969 if (get_float_arg(argvars, &f) == OK)
10970 rettv->vval.v_float = cos(f);
10971 else
10972 rettv->vval.v_float = 0.0;
10973}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010974
10975/*
10976 * "cosh()" function
10977 */
10978 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010979f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010980{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010981 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010982
10983 rettv->v_type = VAR_FLOAT;
10984 if (get_float_arg(argvars, &f) == OK)
10985 rettv->vval.v_float = cosh(f);
10986 else
10987 rettv->vval.v_float = 0.0;
10988}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010989#endif
10990
Bram Moolenaar071d4272004-06-13 20:20:40 +000010991/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010992 * "count()" function
10993 */
10994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010995f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010996{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010997 long n = 0;
10998 int ic = FALSE;
10999
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011001 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011002 listitem_T *li;
11003 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011005
Bram Moolenaare9a41262005-01-15 22:18:47 +000011006 if ((l = argvars[0].vval.v_list) != NULL)
11007 {
11008 li = l->lv_first;
11009 if (argvars[2].v_type != VAR_UNKNOWN)
11010 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 int error = FALSE;
11012
11013 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011014 if (argvars[3].v_type != VAR_UNKNOWN)
11015 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011016 idx = get_tv_number_chk(&argvars[3], &error);
11017 if (!error)
11018 {
11019 li = list_find(l, idx);
11020 if (li == NULL)
11021 EMSGN(_(e_listidx), idx);
11022 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011023 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011024 if (error)
11025 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011026 }
11027
11028 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011029 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011030 ++n;
11031 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011032 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011033 else if (argvars[0].v_type == VAR_DICT)
11034 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011035 int todo;
11036 dict_T *d;
11037 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011038
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011039 if ((d = argvars[0].vval.v_dict) != NULL)
11040 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011041 int error = FALSE;
11042
Bram Moolenaare9a41262005-01-15 22:18:47 +000011043 if (argvars[2].v_type != VAR_UNKNOWN)
11044 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011045 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011046 if (argvars[3].v_type != VAR_UNKNOWN)
11047 EMSG(_(e_invarg));
11048 }
11049
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011050 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011051 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011052 {
11053 if (!HASHITEM_EMPTY(hi))
11054 {
11055 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011056 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011057 ++n;
11058 }
11059 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011060 }
11061 }
11062 else
11063 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011064 rettv->vval.v_number = n;
11065}
11066
11067/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11069 *
11070 * Checks the existence of a cscope connection.
11071 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011073f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074{
11075#ifdef FEAT_CSCOPE
11076 int num = 0;
11077 char_u *dbpath = NULL;
11078 char_u *prepend = NULL;
11079 char_u buf[NUMBUFLEN];
11080
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011081 if (argvars[0].v_type != VAR_UNKNOWN
11082 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011084 num = (int)get_tv_number(&argvars[0]);
11085 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011087 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011088 }
11089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011090 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011091#endif
11092}
11093
11094/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011095 * "cursor(lnum, col)" function, or
11096 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011098 * Moves the cursor to the specified line and column.
11099 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011102f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103{
11104 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011105#ifdef FEAT_VIRTUALEDIT
11106 long coladd = 0;
11107#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011108 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011110 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011111 if (argvars[1].v_type == VAR_UNKNOWN)
11112 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011113 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011114 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011115
Bram Moolenaar493c1782014-05-28 14:34:46 +020011116 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011117 {
11118 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011119 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011120 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011121 line = pos.lnum;
11122 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011123#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011124 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011125#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011126 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011127 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011128 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011129 set_curswant = FALSE;
11130 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011131 }
11132 else
11133 {
11134 line = get_tv_lnum(argvars);
11135 col = get_tv_number_chk(&argvars[1], NULL);
11136#ifdef FEAT_VIRTUALEDIT
11137 if (argvars[2].v_type != VAR_UNKNOWN)
11138 coladd = get_tv_number_chk(&argvars[2], NULL);
11139#endif
11140 }
11141 if (line < 0 || col < 0
11142#ifdef FEAT_VIRTUALEDIT
11143 || coladd < 0
11144#endif
11145 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011146 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011147 if (line > 0)
11148 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011149 if (col > 0)
11150 curwin->w_cursor.col = col - 1;
11151#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011152 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011153#endif
11154
11155 /* Make sure the cursor is in a valid position. */
11156 check_cursor();
11157#ifdef FEAT_MBYTE
11158 /* Correct cursor for multi-byte character. */
11159 if (has_mbyte)
11160 mb_adjust_cursor();
11161#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011162
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011163 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011164 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165}
11166
11167/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011168 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 */
11170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011171f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011172{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011173 int noref = 0;
11174
11175 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011176 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011177 if (noref < 0 || noref > 1)
11178 EMSG(_(e_invarg));
11179 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011180 {
11181 current_copyID += COPYID_INC;
11182 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11183 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011184}
11185
11186/*
11187 * "delete()" function
11188 */
11189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011190f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011191{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011192 char_u nbuf[NUMBUFLEN];
11193 char_u *name;
11194 char_u *flags;
11195
11196 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011198 return;
11199
11200 name = get_tv_string(&argvars[0]);
11201 if (name == NULL || *name == NUL)
11202 {
11203 EMSG(_(e_invarg));
11204 return;
11205 }
11206
11207 if (argvars[1].v_type != VAR_UNKNOWN)
11208 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011209 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011210 flags = (char_u *)"";
11211
11212 if (*flags == NUL)
11213 /* delete a file */
11214 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11215 else if (STRCMP(flags, "d") == 0)
11216 /* delete an empty directory */
11217 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11218 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011219 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011220 rettv->vval.v_number = delete_recursive(name);
11221 else
11222 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011223}
11224
11225/*
11226 * "did_filetype()" function
11227 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011229f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011230{
11231#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011232 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011233#endif
11234}
11235
11236/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011237 * "diff_filler()" function
11238 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011240f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011241{
11242#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011243 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011244#endif
11245}
11246
11247/*
11248 * "diff_hlID()" function
11249 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011251f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011252{
11253#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011255 static linenr_T prev_lnum = 0;
11256 static int changedtick = 0;
11257 static int fnum = 0;
11258 static int change_start = 0;
11259 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011260 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011261 int filler_lines;
11262 int col;
11263
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011264 if (lnum < 0) /* ignore type error in {lnum} arg */
11265 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011266 if (lnum != prev_lnum
11267 || changedtick != curbuf->b_changedtick
11268 || fnum != curbuf->b_fnum)
11269 {
11270 /* New line, buffer, change: need to get the values. */
11271 filler_lines = diff_check(curwin, lnum);
11272 if (filler_lines < 0)
11273 {
11274 if (filler_lines == -1)
11275 {
11276 change_start = MAXCOL;
11277 change_end = -1;
11278 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11279 hlID = HLF_ADD; /* added line */
11280 else
11281 hlID = HLF_CHD; /* changed line */
11282 }
11283 else
11284 hlID = HLF_ADD; /* added line */
11285 }
11286 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011287 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011288 prev_lnum = lnum;
11289 changedtick = curbuf->b_changedtick;
11290 fnum = curbuf->b_fnum;
11291 }
11292
11293 if (hlID == HLF_CHD || hlID == HLF_TXD)
11294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011295 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011296 if (col >= change_start && col <= change_end)
11297 hlID = HLF_TXD; /* changed text */
11298 else
11299 hlID = HLF_CHD; /* changed line */
11300 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011301 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011302#endif
11303}
11304
11305/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011306 * "disable_char_avail_for_testing({expr})" function
11307 */
11308 static void
11309f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11310{
11311 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11312}
11313
11314/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011315 * "empty({expr})" function
11316 */
11317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011318f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011319{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011320 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011321
11322 switch (argvars[0].v_type)
11323 {
11324 case VAR_STRING:
11325 case VAR_FUNC:
11326 n = argvars[0].vval.v_string == NULL
11327 || *argvars[0].vval.v_string == NUL;
11328 break;
11329 case VAR_NUMBER:
11330 n = argvars[0].vval.v_number == 0;
11331 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011332 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011333#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011334 n = argvars[0].vval.v_float == 0.0;
11335 break;
11336#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011337 case VAR_LIST:
11338 n = argvars[0].vval.v_list == NULL
11339 || argvars[0].vval.v_list->lv_first == NULL;
11340 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011341 case VAR_DICT:
11342 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011343 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011344 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011345 case VAR_SPECIAL:
11346 n = argvars[0].vval.v_number != VVAL_TRUE;
11347 break;
11348
Bram Moolenaar835dc632016-02-07 14:27:38 +010011349 case VAR_JOB:
11350#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011351 n = argvars[0].vval.v_job == NULL
11352 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11353 break;
11354#endif
11355 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011356#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011357 n = argvars[0].vval.v_channel == NULL
11358 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011359 break;
11360#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011361 case VAR_UNKNOWN:
11362 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11363 n = TRUE;
11364 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011365 }
11366
11367 rettv->vval.v_number = n;
11368}
11369
11370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011371 * "escape({string}, {chars})" function
11372 */
11373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011374f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011375{
11376 char_u buf[NUMBUFLEN];
11377
Bram Moolenaar758711c2005-02-02 23:11:38 +000011378 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11379 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011381}
11382
11383/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011384 * "eval()" function
11385 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011387f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011388{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011389 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011390
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 s = get_tv_string_chk(&argvars[0]);
11392 if (s != NULL)
11393 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011394
Bram Moolenaar615b9972015-01-14 17:15:05 +010011395 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011396 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11397 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011398 if (p != NULL && !aborting())
11399 EMSG2(_(e_invexpr2), p);
11400 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011401 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011402 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011404 else if (*s != NUL)
11405 EMSG(_(e_trailing));
11406}
11407
11408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011409 * "eventhandler()" function
11410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011412f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011413{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011414 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
11418 * "executable()" function
11419 */
11420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011421f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011423 char_u *name = get_tv_string(&argvars[0]);
11424
11425 /* Check in $PATH and also check directly if there is a directory name. */
11426 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11427 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011428}
11429
11430/*
11431 * "exepath()" function
11432 */
11433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011434f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011435{
11436 char_u *p = NULL;
11437
Bram Moolenaarb5971142015-03-21 17:32:19 +010011438 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011439 rettv->v_type = VAR_STRING;
11440 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011441}
11442
11443/*
11444 * "exists()" function
11445 */
11446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011447f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011448{
11449 char_u *p;
11450 char_u *name;
11451 int n = FALSE;
11452 int len = 0;
11453
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011454 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011455 if (*p == '$') /* environment variable */
11456 {
11457 /* first try "normal" environment variables (fast) */
11458 if (mch_getenv(p + 1) != NULL)
11459 n = TRUE;
11460 else
11461 {
11462 /* try expanding things like $VIM and ${HOME} */
11463 p = expand_env_save(p);
11464 if (p != NULL && *p != '$')
11465 n = TRUE;
11466 vim_free(p);
11467 }
11468 }
11469 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011470 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011472 if (*skipwhite(p) != NUL)
11473 n = FALSE; /* trailing garbage */
11474 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 else if (*p == '*') /* internal or user defined function */
11476 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011477 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011478 }
11479 else if (*p == ':')
11480 {
11481 n = cmd_exists(p + 1);
11482 }
11483 else if (*p == '#')
11484 {
11485#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011486 if (p[1] == '#')
11487 n = autocmd_supported(p + 2);
11488 else
11489 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011490#endif
11491 }
11492 else /* internal variable */
11493 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011494 char_u *tofree;
11495 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011497 /* get_name_len() takes care of expanding curly braces */
11498 name = p;
11499 len = get_name_len(&p, &tofree, TRUE, FALSE);
11500 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011501 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011502 if (tofree != NULL)
11503 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011504 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011505 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011506 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011507 /* handle d.key, l[idx], f(expr) */
11508 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11509 if (n)
11510 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011511 }
11512 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011513 if (*p != NUL)
11514 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011515
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011516 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011517 }
11518
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011519 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011520}
11521
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011522#ifdef FEAT_FLOAT
11523/*
11524 * "exp()" function
11525 */
11526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011527f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011528{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011529 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011530
11531 rettv->v_type = VAR_FLOAT;
11532 if (get_float_arg(argvars, &f) == OK)
11533 rettv->vval.v_float = exp(f);
11534 else
11535 rettv->vval.v_float = 0.0;
11536}
11537#endif
11538
Bram Moolenaar071d4272004-06-13 20:20:40 +000011539/*
11540 * "expand()" function
11541 */
11542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011543f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011544{
11545 char_u *s;
11546 int len;
11547 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011548 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011549 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011551 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011553 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011554 if (argvars[1].v_type != VAR_UNKNOWN
11555 && argvars[2].v_type != VAR_UNKNOWN
11556 && get_tv_number_chk(&argvars[2], &error)
11557 && !error)
11558 {
11559 rettv->v_type = VAR_LIST;
11560 rettv->vval.v_list = NULL;
11561 }
11562
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011563 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011564 if (*s == '%' || *s == '#' || *s == '<')
11565 {
11566 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011567 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011568 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011569 if (rettv->v_type == VAR_LIST)
11570 {
11571 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11572 list_append_string(rettv->vval.v_list, result, -1);
11573 else
11574 vim_free(result);
11575 }
11576 else
11577 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011578 }
11579 else
11580 {
11581 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011582 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011583 if (argvars[1].v_type != VAR_UNKNOWN
11584 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011585 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011586 if (!error)
11587 {
11588 ExpandInit(&xpc);
11589 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011590 if (p_wic)
11591 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011592 if (rettv->v_type == VAR_STRING)
11593 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11594 options, WILD_ALL);
11595 else if (rettv_list_alloc(rettv) != FAIL)
11596 {
11597 int i;
11598
11599 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11600 for (i = 0; i < xpc.xp_numfiles; i++)
11601 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11602 ExpandCleanup(&xpc);
11603 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011604 }
11605 else
11606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011607 }
11608}
11609
11610/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011611 * Go over all entries in "d2" and add them to "d1".
11612 * When "action" is "error" then a duplicate key is an error.
11613 * When "action" is "force" then a duplicate key is overwritten.
11614 * Otherwise duplicate keys are ignored ("action" is "keep").
11615 */
11616 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011617dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011618{
11619 dictitem_T *di1;
11620 hashitem_T *hi2;
11621 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011622 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011623
11624 todo = (int)d2->dv_hashtab.ht_used;
11625 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11626 {
11627 if (!HASHITEM_EMPTY(hi2))
11628 {
11629 --todo;
11630 di1 = dict_find(d1, hi2->hi_key, -1);
11631 if (d1->dv_scope != 0)
11632 {
11633 /* Disallow replacing a builtin function in l: and g:.
11634 * Check the key to be valid when adding to any
11635 * scope. */
11636 if (d1->dv_scope == VAR_DEF_SCOPE
11637 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11638 && var_check_func_name(hi2->hi_key,
11639 di1 == NULL))
11640 break;
11641 if (!valid_varname(hi2->hi_key))
11642 break;
11643 }
11644 if (di1 == NULL)
11645 {
11646 di1 = dictitem_copy(HI2DI(hi2));
11647 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11648 dictitem_free(di1);
11649 }
11650 else if (*action == 'e')
11651 {
11652 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11653 break;
11654 }
11655 else if (*action == 'f' && HI2DI(hi2) != di1)
11656 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011657 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11658 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011659 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011660 clear_tv(&di1->di_tv);
11661 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11662 }
11663 }
11664 }
11665}
11666
11667/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011668 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011669 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011670 */
11671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011672f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011673{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011674 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011675
Bram Moolenaare9a41262005-01-15 22:18:47 +000011676 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011677 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011678 list_T *l1, *l2;
11679 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011680 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011681 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011682
Bram Moolenaare9a41262005-01-15 22:18:47 +000011683 l1 = argvars[0].vval.v_list;
11684 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011685 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011686 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 {
11688 if (argvars[2].v_type != VAR_UNKNOWN)
11689 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011690 before = get_tv_number_chk(&argvars[2], &error);
11691 if (error)
11692 return; /* type error; errmsg already given */
11693
Bram Moolenaar758711c2005-02-02 23:11:38 +000011694 if (before == l1->lv_len)
11695 item = NULL;
11696 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011698 item = list_find(l1, before);
11699 if (item == NULL)
11700 {
11701 EMSGN(_(e_listidx), before);
11702 return;
11703 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011704 }
11705 }
11706 else
11707 item = NULL;
11708 list_extend(l1, l2, item);
11709
Bram Moolenaare9a41262005-01-15 22:18:47 +000011710 copy_tv(&argvars[0], rettv);
11711 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011712 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011713 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11714 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011715 dict_T *d1, *d2;
11716 char_u *action;
11717 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011718
11719 d1 = argvars[0].vval.v_dict;
11720 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011721 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011722 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011723 {
11724 /* Check the third argument. */
11725 if (argvars[2].v_type != VAR_UNKNOWN)
11726 {
11727 static char *(av[]) = {"keep", "force", "error"};
11728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011729 action = get_tv_string_chk(&argvars[2]);
11730 if (action == NULL)
11731 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 for (i = 0; i < 3; ++i)
11733 if (STRCMP(action, av[i]) == 0)
11734 break;
11735 if (i == 3)
11736 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011737 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011738 return;
11739 }
11740 }
11741 else
11742 action = (char_u *)"force";
11743
Bram Moolenaara9922d62013-05-30 13:01:18 +020011744 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011745
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 copy_tv(&argvars[0], rettv);
11747 }
11748 }
11749 else
11750 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011751}
11752
11753/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011754 * "feedkeys()" function
11755 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011757f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011758{
11759 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011760 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011761 char_u *keys, *flags;
11762 char_u nbuf[NUMBUFLEN];
11763 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011764 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011765 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011766
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011767 /* This is not allowed in the sandbox. If the commands would still be
11768 * executed in the sandbox it would be OK, but it probably happens later,
11769 * when "sandbox" is no longer set. */
11770 if (check_secure())
11771 return;
11772
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011773 keys = get_tv_string(&argvars[0]);
11774 if (*keys != NUL)
11775 {
11776 if (argvars[1].v_type != VAR_UNKNOWN)
11777 {
11778 flags = get_tv_string_buf(&argvars[1], nbuf);
11779 for ( ; *flags != NUL; ++flags)
11780 {
11781 switch (*flags)
11782 {
11783 case 'n': remap = FALSE; break;
11784 case 'm': remap = TRUE; break;
11785 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011786 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011787 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011788 }
11789 }
11790 }
11791
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011792 /* Need to escape K_SPECIAL and CSI before putting the string in the
11793 * typeahead buffer. */
11794 keys_esc = vim_strsave_escape_csi(keys);
11795 if (keys_esc != NULL)
11796 {
11797 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011798 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011799 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011800 if (vgetc_busy)
11801 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011802 if (execute)
11803 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011804 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011805 }
11806}
11807
11808/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 * "filereadable()" function
11810 */
11811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011812f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011813{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011814 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 char_u *p;
11816 int n;
11817
Bram Moolenaarc236c162008-07-13 17:41:49 +000011818#ifndef O_NONBLOCK
11819# define O_NONBLOCK 0
11820#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011821 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011822 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11823 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011824 {
11825 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011826 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827 }
11828 else
11829 n = FALSE;
11830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011831 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011832}
11833
11834/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011835 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836 * rights to write into.
11837 */
11838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011839f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011840{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011841 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011842}
11843
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011844 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011845findfilendir(
11846 typval_T *argvars UNUSED,
11847 typval_T *rettv,
11848 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011849{
11850#ifdef FEAT_SEARCHPATH
11851 char_u *fname;
11852 char_u *fresult = NULL;
11853 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11854 char_u *p;
11855 char_u pathbuf[NUMBUFLEN];
11856 int count = 1;
11857 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011858 int error = FALSE;
11859#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011860
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011861 rettv->vval.v_string = NULL;
11862 rettv->v_type = VAR_STRING;
11863
11864#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011865 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011866
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011867 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011868 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011869 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11870 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011871 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 else
11873 {
11874 if (*p != NUL)
11875 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011876
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011877 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011878 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011879 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011880 }
11881
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011882 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11883 error = TRUE;
11884
11885 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011887 do
11888 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011889 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011890 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 fresult = find_file_in_path_option(first ? fname : NULL,
11892 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011893 0, first, path,
11894 find_what,
11895 curbuf->b_ffname,
11896 find_what == FINDFILE_DIR
11897 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011898 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011899
11900 if (fresult != NULL && rettv->v_type == VAR_LIST)
11901 list_append_string(rettv->vval.v_list, fresult, -1);
11902
11903 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011904 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011905
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011906 if (rettv->v_type == VAR_STRING)
11907 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011908#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011909}
11910
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011911static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11912static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011913
11914/*
11915 * Implementation of map() and filter().
11916 */
11917 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011918filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011919{
11920 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011921 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011922 listitem_T *li, *nli;
11923 list_T *l = NULL;
11924 dictitem_T *di;
11925 hashtab_T *ht;
11926 hashitem_T *hi;
11927 dict_T *d = NULL;
11928 typval_T save_val;
11929 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011930 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011931 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011932 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011933 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011934 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011935 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011936 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011937
Bram Moolenaare9a41262005-01-15 22:18:47 +000011938 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011939 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011940 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011941 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011942 return;
11943 }
11944 else if (argvars[0].v_type == VAR_DICT)
11945 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011946 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011947 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948 return;
11949 }
11950 else
11951 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011952 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011953 return;
11954 }
11955
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011956 expr = get_tv_string_buf_chk(&argvars[1], buf);
11957 /* On type errors, the preceding call has already displayed an error
11958 * message. Avoid a misleading error message for an empty string that
11959 * was not passed as argument. */
11960 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011961 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011962 prepare_vimvar(VV_VAL, &save_val);
11963 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011964
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011965 /* We reset "did_emsg" to be able to detect whether an error
11966 * occurred during evaluation of the expression. */
11967 save_did_emsg = did_emsg;
11968 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011969
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011970 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011971 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011972 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011973 vimvars[VV_KEY].vv_type = VAR_STRING;
11974
11975 ht = &d->dv_hashtab;
11976 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011977 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011978 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011979 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011980 if (!HASHITEM_EMPTY(hi))
11981 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011982 int r;
11983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 --todo;
11985 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011986 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011987 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11988 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011989 break;
11990 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011991 r = filter_map_one(&di->di_tv, expr, map, &rem);
11992 clear_tv(&vimvars[VV_KEY].vv_tv);
11993 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 break;
11995 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011996 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011997 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11998 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011999 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020012001 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012002 }
12003 }
12004 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012005 }
12006 else
12007 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012008 vimvars[VV_KEY].vv_type = VAR_NUMBER;
12009
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012010 for (li = l->lv_first; li != NULL; li = nli)
12011 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020012012 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000012013 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012014 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012015 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000012016 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012017 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012018 break;
12019 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012020 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020012021 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012022 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012023 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012024
Bram Moolenaar627b1d32009-11-17 11:20:35 +000012025 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012026 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012027
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012028 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012029 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012030
12031 copy_tv(&argvars[0], rettv);
12032}
12033
12034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012035filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036{
Bram Moolenaar33570922005-01-25 22:26:29 +000012037 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012039 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012040
Bram Moolenaar33570922005-01-25 22:26:29 +000012041 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012042 s = expr;
12043 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012044 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012045 if (*s != NUL) /* check for trailing chars after expr */
12046 {
12047 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012048 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012049 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012050 }
12051 if (map)
12052 {
12053 /* map(): replace the list item value */
12054 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012055 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012056 *tv = rettv;
12057 }
12058 else
12059 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012060 int error = FALSE;
12061
Bram Moolenaare9a41262005-01-15 22:18:47 +000012062 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012063 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012064 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012065 /* On type error, nothing has been removed; return FAIL to stop the
12066 * loop. The error message was given by get_tv_number_chk(). */
12067 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012068 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012069 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012070 retval = OK;
12071theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012072 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012073 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012074}
12075
12076/*
12077 * "filter()" function
12078 */
12079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012080f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012081{
12082 filter_map(argvars, rettv, FALSE);
12083}
12084
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012085/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012086 * "finddir({fname}[, {path}[, {count}]])" function
12087 */
12088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012089f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012090{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012091 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012092}
12093
12094/*
12095 * "findfile({fname}[, {path}[, {count}]])" function
12096 */
12097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012098f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012099{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012100 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012101}
12102
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012103#ifdef FEAT_FLOAT
12104/*
12105 * "float2nr({float})" function
12106 */
12107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012108f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012109{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012110 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012111
12112 if (get_float_arg(argvars, &f) == OK)
12113 {
12114 if (f < -0x7fffffff)
12115 rettv->vval.v_number = -0x7fffffff;
12116 else if (f > 0x7fffffff)
12117 rettv->vval.v_number = 0x7fffffff;
12118 else
12119 rettv->vval.v_number = (varnumber_T)f;
12120 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012121}
12122
12123/*
12124 * "floor({float})" function
12125 */
12126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012127f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012128{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012129 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012130
12131 rettv->v_type = VAR_FLOAT;
12132 if (get_float_arg(argvars, &f) == OK)
12133 rettv->vval.v_float = floor(f);
12134 else
12135 rettv->vval.v_float = 0.0;
12136}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012137
12138/*
12139 * "fmod()" function
12140 */
12141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012142f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012143{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012144 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012145
12146 rettv->v_type = VAR_FLOAT;
12147 if (get_float_arg(argvars, &fx) == OK
12148 && get_float_arg(&argvars[1], &fy) == OK)
12149 rettv->vval.v_float = fmod(fx, fy);
12150 else
12151 rettv->vval.v_float = 0.0;
12152}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012153#endif
12154
Bram Moolenaar0d660222005-01-07 21:51:51 +000012155/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012156 * "fnameescape({string})" function
12157 */
12158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012159f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012160{
12161 rettv->vval.v_string = vim_strsave_fnameescape(
12162 get_tv_string(&argvars[0]), FALSE);
12163 rettv->v_type = VAR_STRING;
12164}
12165
12166/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012167 * "fnamemodify({fname}, {mods})" function
12168 */
12169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012171{
12172 char_u *fname;
12173 char_u *mods;
12174 int usedlen = 0;
12175 int len;
12176 char_u *fbuf = NULL;
12177 char_u buf[NUMBUFLEN];
12178
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012179 fname = get_tv_string_chk(&argvars[0]);
12180 mods = get_tv_string_buf_chk(&argvars[1], buf);
12181 if (fname == NULL || mods == NULL)
12182 fname = NULL;
12183 else
12184 {
12185 len = (int)STRLEN(fname);
12186 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12187 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012188
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012189 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012190 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012191 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012192 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012194 vim_free(fbuf);
12195}
12196
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012197static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012198
12199/*
12200 * "foldclosed()" function
12201 */
12202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012203foldclosed_both(
12204 typval_T *argvars UNUSED,
12205 typval_T *rettv,
12206 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012207{
12208#ifdef FEAT_FOLDING
12209 linenr_T lnum;
12210 linenr_T first, last;
12211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012212 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012213 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12214 {
12215 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12216 {
12217 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012218 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012219 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012220 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 return;
12222 }
12223 }
12224#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012225 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012226}
12227
12228/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012229 * "foldclosed()" function
12230 */
12231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012232f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012233{
12234 foldclosed_both(argvars, rettv, FALSE);
12235}
12236
12237/*
12238 * "foldclosedend()" function
12239 */
12240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012241f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242{
12243 foldclosed_both(argvars, rettv, TRUE);
12244}
12245
12246/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012247 * "foldlevel()" function
12248 */
12249 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012250f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251{
12252#ifdef FEAT_FOLDING
12253 linenr_T lnum;
12254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012255 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012257 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012258#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259}
12260
12261/*
12262 * "foldtext()" function
12263 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012264 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012265f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012266{
12267#ifdef FEAT_FOLDING
12268 linenr_T lnum;
12269 char_u *s;
12270 char_u *r;
12271 int len;
12272 char *txt;
12273#endif
12274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012275 rettv->v_type = VAR_STRING;
12276 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012277#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012278 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12279 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12280 <= curbuf->b_ml.ml_line_count
12281 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282 {
12283 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012284 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12285 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012286 {
12287 if (!linewhite(lnum))
12288 break;
12289 ++lnum;
12290 }
12291
12292 /* Find interesting text in this line. */
12293 s = skipwhite(ml_get(lnum));
12294 /* skip C comment-start */
12295 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012296 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012298 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012299 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012300 {
12301 s = skipwhite(ml_get(lnum + 1));
12302 if (*s == '*')
12303 s = skipwhite(s + 1);
12304 }
12305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306 txt = _("+-%s%3ld lines: ");
12307 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012308 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012309 + 20 /* for %3ld */
12310 + STRLEN(s))); /* concatenated */
12311 if (r != NULL)
12312 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012313 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12314 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12315 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012316 len = (int)STRLEN(r);
12317 STRCAT(r, s);
12318 /* remove 'foldmarker' and 'commentstring' */
12319 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012320 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 }
12322 }
12323#endif
12324}
12325
12326/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012327 * "foldtextresult(lnum)" function
12328 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012330f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012331{
12332#ifdef FEAT_FOLDING
12333 linenr_T lnum;
12334 char_u *text;
12335 char_u buf[51];
12336 foldinfo_T foldinfo;
12337 int fold_count;
12338#endif
12339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->v_type = VAR_STRING;
12341 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012342#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012343 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012344 /* treat illegal types and illegal string values for {lnum} the same */
12345 if (lnum < 0)
12346 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012347 fold_count = foldedCount(curwin, lnum, &foldinfo);
12348 if (fold_count > 0)
12349 {
12350 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12351 &foldinfo, buf);
12352 if (text == buf)
12353 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012354 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012355 }
12356#endif
12357}
12358
12359/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012360 * "foreground()" function
12361 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012363f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012364{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012365#ifdef FEAT_GUI
12366 if (gui.in_use)
12367 gui_mch_set_foreground();
12368#else
12369# ifdef WIN32
12370 win32_set_foreground();
12371# endif
12372#endif
12373}
12374
12375/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012376 * "function()" function
12377 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012379f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012380{
12381 char_u *s;
12382
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012383 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012384 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012385 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012386 /* Don't check an autoload name for existence here. */
12387 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012388 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012389 else
12390 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012391 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012392 {
12393 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012394 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012395
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012396 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12397 * also be called from another script. Using trans_function_name()
12398 * would also work, but some plugins depend on the name being
12399 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012400 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012401 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012402 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012403 if (rettv->vval.v_string != NULL)
12404 {
12405 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012406 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012407 }
12408 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012409 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012410 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012411 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012412 }
12413}
12414
12415/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012416 * "garbagecollect()" function
12417 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012419f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012420{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012421 /* This is postponed until we are back at the toplevel, because we may be
12422 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12423 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012424
12425 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12426 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012427}
12428
12429/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012430 * "get()" function
12431 */
12432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012433f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012434{
Bram Moolenaar33570922005-01-25 22:26:29 +000012435 listitem_T *li;
12436 list_T *l;
12437 dictitem_T *di;
12438 dict_T *d;
12439 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012440
Bram Moolenaare9a41262005-01-15 22:18:47 +000012441 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012442 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012443 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012444 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012445 int error = FALSE;
12446
12447 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12448 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012449 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012450 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012451 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012452 else if (argvars[0].v_type == VAR_DICT)
12453 {
12454 if ((d = argvars[0].vval.v_dict) != NULL)
12455 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012456 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012457 if (di != NULL)
12458 tv = &di->di_tv;
12459 }
12460 }
12461 else
12462 EMSG2(_(e_listdictarg), "get()");
12463
12464 if (tv == NULL)
12465 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012467 copy_tv(&argvars[2], rettv);
12468 }
12469 else
12470 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012471}
12472
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012473static 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 +000012474
12475/*
12476 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012477 * Return a range (from start to end) of lines in rettv from the specified
12478 * buffer.
12479 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012480 */
12481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012482get_buffer_lines(
12483 buf_T *buf,
12484 linenr_T start,
12485 linenr_T end,
12486 int retlist,
12487 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012488{
12489 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012490
Bram Moolenaar959a1432013-12-14 12:17:38 +010012491 rettv->v_type = VAR_STRING;
12492 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012493 if (retlist && rettv_list_alloc(rettv) == FAIL)
12494 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012495
12496 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12497 return;
12498
12499 if (!retlist)
12500 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012501 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12502 p = ml_get_buf(buf, start, FALSE);
12503 else
12504 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012505 rettv->vval.v_string = vim_strsave(p);
12506 }
12507 else
12508 {
12509 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012510 return;
12511
12512 if (start < 1)
12513 start = 1;
12514 if (end > buf->b_ml.ml_line_count)
12515 end = buf->b_ml.ml_line_count;
12516 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012517 if (list_append_string(rettv->vval.v_list,
12518 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012519 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012520 }
12521}
12522
12523/*
12524 * "getbufline()" function
12525 */
12526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012527f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012528{
12529 linenr_T lnum;
12530 linenr_T end;
12531 buf_T *buf;
12532
12533 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12534 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012535 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012536 --emsg_off;
12537
Bram Moolenaar661b1822005-07-28 22:36:45 +000012538 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012539 if (argvars[2].v_type == VAR_UNKNOWN)
12540 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012541 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012542 end = get_tv_lnum_buf(&argvars[2], buf);
12543
Bram Moolenaar342337a2005-07-21 21:11:17 +000012544 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012545}
12546
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547/*
12548 * "getbufvar()" function
12549 */
12550 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012551f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012552{
12553 buf_T *buf;
12554 buf_T *save_curbuf;
12555 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012556 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012557 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012559 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12560 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012561 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012562 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012563
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012564 rettv->v_type = VAR_STRING;
12565 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012566
12567 if (buf != NULL && varname != NULL)
12568 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012569 /* set curbuf to be our buf, temporarily */
12570 save_curbuf = curbuf;
12571 curbuf = buf;
12572
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012574 {
12575 if (get_option_tv(&varname, rettv, TRUE) == OK)
12576 done = TRUE;
12577 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012578 else if (STRCMP(varname, "changedtick") == 0)
12579 {
12580 rettv->v_type = VAR_NUMBER;
12581 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012582 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012583 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012584 else
12585 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012586 /* Look up the variable. */
12587 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12588 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12589 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012590 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012591 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012592 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012593 done = TRUE;
12594 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012595 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012596
12597 /* restore previous notion of curbuf */
12598 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012599 }
12600
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012601 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12602 /* use the default value */
12603 copy_tv(&argvars[2], rettv);
12604
Bram Moolenaar0d660222005-01-07 21:51:51 +000012605 --emsg_off;
12606}
12607
12608/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012609 * "getchar()" function
12610 */
12611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012612f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613{
12614 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012615 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012616
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012617 /* Position the cursor. Needed after a message that ends in a space. */
12618 windgoto(msg_row, msg_col);
12619
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620 ++no_mapping;
12621 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012622 for (;;)
12623 {
12624 if (argvars[0].v_type == VAR_UNKNOWN)
12625 /* getchar(): blocking wait. */
12626 n = safe_vgetc();
12627 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12628 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012629 n = vpeekc_any();
12630 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012631 /* illegal argument or getchar(0) and no char avail: return zero */
12632 n = 0;
12633 else
12634 /* getchar(0) and char avail: return char */
12635 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012636
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012637 if (n == K_IGNORE)
12638 continue;
12639 break;
12640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012641 --no_mapping;
12642 --allow_keys;
12643
Bram Moolenaar219b8702006-11-01 14:32:36 +000012644 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12645 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12646 vimvars[VV_MOUSE_COL].vv_nr = 0;
12647
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012649 if (IS_SPECIAL(n) || mod_mask != 0)
12650 {
12651 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12652 int i = 0;
12653
12654 /* Turn a special key into three bytes, plus modifier. */
12655 if (mod_mask != 0)
12656 {
12657 temp[i++] = K_SPECIAL;
12658 temp[i++] = KS_MODIFIER;
12659 temp[i++] = mod_mask;
12660 }
12661 if (IS_SPECIAL(n))
12662 {
12663 temp[i++] = K_SPECIAL;
12664 temp[i++] = K_SECOND(n);
12665 temp[i++] = K_THIRD(n);
12666 }
12667#ifdef FEAT_MBYTE
12668 else if (has_mbyte)
12669 i += (*mb_char2bytes)(n, temp + i);
12670#endif
12671 else
12672 temp[i++] = n;
12673 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012674 rettv->v_type = VAR_STRING;
12675 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012676
12677#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012678 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012679 {
12680 int row = mouse_row;
12681 int col = mouse_col;
12682 win_T *win;
12683 linenr_T lnum;
12684# ifdef FEAT_WINDOWS
12685 win_T *wp;
12686# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012687 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012688
12689 if (row >= 0 && col >= 0)
12690 {
12691 /* Find the window at the mouse coordinates and compute the
12692 * text position. */
12693 win = mouse_find_win(&row, &col);
12694 (void)mouse_comp_pos(win, &row, &col, &lnum);
12695# ifdef FEAT_WINDOWS
12696 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012697 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012698# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012699 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012700 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12701 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12702 }
12703 }
12704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012705 }
12706}
12707
12708/*
12709 * "getcharmod()" function
12710 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012712f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012713{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012714 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012718 * "getcharsearch()" function
12719 */
12720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012722{
12723 if (rettv_dict_alloc(rettv) != FAIL)
12724 {
12725 dict_T *dict = rettv->vval.v_dict;
12726
12727 dict_add_nr_str(dict, "char", 0L, last_csearch());
12728 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12729 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12730 }
12731}
12732
12733/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012734 * "getcmdline()" function
12735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012737f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012739 rettv->v_type = VAR_STRING;
12740 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012741}
12742
12743/*
12744 * "getcmdpos()" function
12745 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012747f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012748{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012749 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012750}
12751
12752/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012753 * "getcmdtype()" function
12754 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012756f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012757{
12758 rettv->v_type = VAR_STRING;
12759 rettv->vval.v_string = alloc(2);
12760 if (rettv->vval.v_string != NULL)
12761 {
12762 rettv->vval.v_string[0] = get_cmdline_type();
12763 rettv->vval.v_string[1] = NUL;
12764 }
12765}
12766
12767/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012768 * "getcmdwintype()" function
12769 */
12770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012771f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012772{
12773 rettv->v_type = VAR_STRING;
12774 rettv->vval.v_string = NULL;
12775#ifdef FEAT_CMDWIN
12776 rettv->vval.v_string = alloc(2);
12777 if (rettv->vval.v_string != NULL)
12778 {
12779 rettv->vval.v_string[0] = cmdwin_type;
12780 rettv->vval.v_string[1] = NUL;
12781 }
12782#endif
12783}
12784
12785/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012786 * "getcwd()" function
12787 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012789f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012790{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012791 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012792 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012794 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012795 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012796
12797 wp = find_tabwin(&argvars[0], &argvars[1]);
12798 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012799 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012800 if (wp->w_localdir != NULL)
12801 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12802 else if(globaldir != NULL)
12803 rettv->vval.v_string = vim_strsave(globaldir);
12804 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012805 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012806 cwd = alloc(MAXPATHL);
12807 if (cwd != NULL)
12808 {
12809 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12810 rettv->vval.v_string = vim_strsave(cwd);
12811 vim_free(cwd);
12812 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012813 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012814#ifdef BACKSLASH_IN_FILENAME
12815 if (rettv->vval.v_string != NULL)
12816 slash_adjust(rettv->vval.v_string);
12817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818 }
12819}
12820
12821/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012822 * "getfontname()" function
12823 */
12824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012825f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012826{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012827 rettv->v_type = VAR_STRING;
12828 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012829#ifdef FEAT_GUI
12830 if (gui.in_use)
12831 {
12832 GuiFont font;
12833 char_u *name = NULL;
12834
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012835 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012836 {
12837 /* Get the "Normal" font. Either the name saved by
12838 * hl_set_font_name() or from the font ID. */
12839 font = gui.norm_font;
12840 name = hl_get_font_name();
12841 }
12842 else
12843 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012845 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12846 return;
12847 font = gui_mch_get_font(name, FALSE);
12848 if (font == NOFONT)
12849 return; /* Invalid font name, return empty string. */
12850 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012851 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012852 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012853 gui_mch_free_font(font);
12854 }
12855#endif
12856}
12857
12858/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012859 * "getfperm({fname})" function
12860 */
12861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012862f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012863{
12864 char_u *fname;
12865 struct stat st;
12866 char_u *perm = NULL;
12867 char_u flags[] = "rwx";
12868 int i;
12869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012870 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012871
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012872 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012873 if (mch_stat((char *)fname, &st) >= 0)
12874 {
12875 perm = vim_strsave((char_u *)"---------");
12876 if (perm != NULL)
12877 {
12878 for (i = 0; i < 9; i++)
12879 {
12880 if (st.st_mode & (1 << (8 - i)))
12881 perm[i] = flags[i % 3];
12882 }
12883 }
12884 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012885 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012886}
12887
12888/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012889 * "getfsize({fname})" function
12890 */
12891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012892f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012893{
12894 char_u *fname;
12895 struct stat st;
12896
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012897 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012899 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012900
12901 if (mch_stat((char *)fname, &st) >= 0)
12902 {
12903 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012906 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012907 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012908
12909 /* non-perfect check for overflow */
12910 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12911 rettv->vval.v_number = -2;
12912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913 }
12914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012915 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916}
12917
12918/*
12919 * "getftime({fname})" function
12920 */
12921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012922f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012923{
12924 char_u *fname;
12925 struct stat st;
12926
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012927 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928
12929 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012930 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012932 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933}
12934
12935/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012936 * "getftype({fname})" function
12937 */
12938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012939f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012940{
12941 char_u *fname;
12942 struct stat st;
12943 char_u *type = NULL;
12944 char *t;
12945
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012946 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012947
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012948 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012949 if (mch_lstat((char *)fname, &st) >= 0)
12950 {
12951#ifdef S_ISREG
12952 if (S_ISREG(st.st_mode))
12953 t = "file";
12954 else if (S_ISDIR(st.st_mode))
12955 t = "dir";
12956# ifdef S_ISLNK
12957 else if (S_ISLNK(st.st_mode))
12958 t = "link";
12959# endif
12960# ifdef S_ISBLK
12961 else if (S_ISBLK(st.st_mode))
12962 t = "bdev";
12963# endif
12964# ifdef S_ISCHR
12965 else if (S_ISCHR(st.st_mode))
12966 t = "cdev";
12967# endif
12968# ifdef S_ISFIFO
12969 else if (S_ISFIFO(st.st_mode))
12970 t = "fifo";
12971# endif
12972# ifdef S_ISSOCK
12973 else if (S_ISSOCK(st.st_mode))
12974 t = "fifo";
12975# endif
12976 else
12977 t = "other";
12978#else
12979# ifdef S_IFMT
12980 switch (st.st_mode & S_IFMT)
12981 {
12982 case S_IFREG: t = "file"; break;
12983 case S_IFDIR: t = "dir"; break;
12984# ifdef S_IFLNK
12985 case S_IFLNK: t = "link"; break;
12986# endif
12987# ifdef S_IFBLK
12988 case S_IFBLK: t = "bdev"; break;
12989# endif
12990# ifdef S_IFCHR
12991 case S_IFCHR: t = "cdev"; break;
12992# endif
12993# ifdef S_IFIFO
12994 case S_IFIFO: t = "fifo"; break;
12995# endif
12996# ifdef S_IFSOCK
12997 case S_IFSOCK: t = "socket"; break;
12998# endif
12999 default: t = "other";
13000 }
13001# else
13002 if (mch_isdir(fname))
13003 t = "dir";
13004 else
13005 t = "file";
13006# endif
13007#endif
13008 type = vim_strsave((char_u *)t);
13009 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000013011}
13012
13013/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013014 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000013015 */
13016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013017f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000013018{
13019 linenr_T lnum;
13020 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013021 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000013022
13023 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013024 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000013025 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013026 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013027 retlist = FALSE;
13028 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013029 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013030 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013031 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013032 retlist = TRUE;
13033 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013034
Bram Moolenaar342337a2005-07-21 21:11:17 +000013035 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013036}
13037
13038/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013039 * "getmatches()" function
13040 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013042f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013043{
13044#ifdef FEAT_SEARCH_EXTRA
13045 dict_T *dict;
13046 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013047 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013048
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013049 if (rettv_list_alloc(rettv) == OK)
13050 {
13051 while (cur != NULL)
13052 {
13053 dict = dict_alloc();
13054 if (dict == NULL)
13055 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013056 if (cur->match.regprog == NULL)
13057 {
13058 /* match added with matchaddpos() */
13059 for (i = 0; i < MAXPOSMATCH; ++i)
13060 {
13061 llpos_T *llpos;
13062 char buf[6];
13063 list_T *l;
13064
13065 llpos = &cur->pos.pos[i];
13066 if (llpos->lnum == 0)
13067 break;
13068 l = list_alloc();
13069 if (l == NULL)
13070 break;
13071 list_append_number(l, (varnumber_T)llpos->lnum);
13072 if (llpos->col > 0)
13073 {
13074 list_append_number(l, (varnumber_T)llpos->col);
13075 list_append_number(l, (varnumber_T)llpos->len);
13076 }
13077 sprintf(buf, "pos%d", i + 1);
13078 dict_add_list(dict, buf, l);
13079 }
13080 }
13081 else
13082 {
13083 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13084 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013085 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013086 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13087 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013088# ifdef FEAT_CONCEAL
13089 if (cur->conceal_char)
13090 {
13091 char_u buf[MB_MAXBYTES + 1];
13092
13093 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13094 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13095 }
13096# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013097 list_append_dict(rettv->vval.v_list, dict);
13098 cur = cur->next;
13099 }
13100 }
13101#endif
13102}
13103
13104/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013105 * "getpid()" function
13106 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013108f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013109{
13110 rettv->vval.v_number = mch_get_pid();
13111}
13112
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013113static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013114
13115/*
13116 * "getcurpos()" function
13117 */
13118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013119f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013120{
13121 getpos_both(argvars, rettv, TRUE);
13122}
13123
Bram Moolenaar18081e32008-02-20 19:11:07 +000013124/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013125 * "getpos(string)" function
13126 */
13127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013128f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013129{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013130 getpos_both(argvars, rettv, FALSE);
13131}
13132
13133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013134getpos_both(
13135 typval_T *argvars,
13136 typval_T *rettv,
13137 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013138{
Bram Moolenaara5525202006-03-02 22:52:09 +000013139 pos_T *fp;
13140 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013141 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013142
13143 if (rettv_list_alloc(rettv) == OK)
13144 {
13145 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013146 if (getcurpos)
13147 fp = &curwin->w_cursor;
13148 else
13149 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013150 if (fnum != -1)
13151 list_append_number(l, (varnumber_T)fnum);
13152 else
13153 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013154 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13155 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013156 list_append_number(l, (fp != NULL)
13157 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013158 : (varnumber_T)0);
13159 list_append_number(l,
13160#ifdef FEAT_VIRTUALEDIT
13161 (fp != NULL) ? (varnumber_T)fp->coladd :
13162#endif
13163 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013164 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013165 {
13166 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013167 list_append_number(l, curwin->w_curswant == MAXCOL ?
13168 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013169 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013170 }
13171 else
13172 rettv->vval.v_number = FALSE;
13173}
13174
13175/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013176 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013177 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013179f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013180{
13181#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013182 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013183#endif
13184
Bram Moolenaar2641f772005-03-25 21:58:17 +000013185#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013186 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013187 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013188 wp = NULL;
13189 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13190 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013191 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013192 if (wp == NULL)
13193 return;
13194 }
13195
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013196 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013197 }
13198#endif
13199}
13200
13201/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013202 * "getreg()" function
13203 */
13204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013205f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013206{
13207 char_u *strregname;
13208 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013209 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013210 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013211 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013213 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013214 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013215 strregname = get_tv_string_chk(&argvars[0]);
13216 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013217 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013218 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013219 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013220 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13221 return_list = get_tv_number_chk(&argvars[2], &error);
13222 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013225 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013226
13227 if (error)
13228 return;
13229
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 regname = (strregname == NULL ? '"' : *strregname);
13231 if (regname == 0)
13232 regname = '"';
13233
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013234 if (return_list)
13235 {
13236 rettv->v_type = VAR_LIST;
13237 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13238 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013239 if (rettv->vval.v_list != NULL)
13240 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013241 }
13242 else
13243 {
13244 rettv->v_type = VAR_STRING;
13245 rettv->vval.v_string = get_reg_contents(regname,
13246 arg2 ? GREG_EXPR_SRC : 0);
13247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248}
13249
13250/*
13251 * "getregtype()" function
13252 */
13253 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013254f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013255{
13256 char_u *strregname;
13257 int regname;
13258 char_u buf[NUMBUFLEN + 2];
13259 long reglen = 0;
13260
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013261 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013262 {
13263 strregname = get_tv_string_chk(&argvars[0]);
13264 if (strregname == NULL) /* type error; errmsg already given */
13265 {
13266 rettv->v_type = VAR_STRING;
13267 rettv->vval.v_string = NULL;
13268 return;
13269 }
13270 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271 else
13272 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013273 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013274
13275 regname = (strregname == NULL ? '"' : *strregname);
13276 if (regname == 0)
13277 regname = '"';
13278
13279 buf[0] = NUL;
13280 buf[1] = NUL;
13281 switch (get_reg_type(regname, &reglen))
13282 {
13283 case MLINE: buf[0] = 'V'; break;
13284 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 case MBLOCK:
13286 buf[0] = Ctrl_V;
13287 sprintf((char *)buf + 1, "%ld", reglen + 1);
13288 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013290 rettv->v_type = VAR_STRING;
13291 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013292}
13293
13294/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013295 * "gettabvar()" function
13296 */
13297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013298f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013299{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013300 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013301 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013302 dictitem_T *v;
13303 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013304 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013305
13306 rettv->v_type = VAR_STRING;
13307 rettv->vval.v_string = NULL;
13308
13309 varname = get_tv_string_chk(&argvars[1]);
13310 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13311 if (tp != NULL && varname != NULL)
13312 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013313 /* Set tp to be our tabpage, temporarily. Also set the window to the
13314 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013315 if (switch_win(&oldcurwin, &oldtabpage,
13316 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013317 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013318 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013319 /* look up the variable */
13320 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13321 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13322 if (v != NULL)
13323 {
13324 copy_tv(&v->di_tv, rettv);
13325 done = TRUE;
13326 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013327 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013328
13329 /* restore previous notion of curwin */
13330 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013331 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013332
13333 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013334 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013335}
13336
13337/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013338 * "gettabwinvar()" function
13339 */
13340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013341f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013342{
13343 getwinvar(argvars, rettv, 1);
13344}
13345
13346/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347 * "getwinposx()" function
13348 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013350f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013351{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013352 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013353#ifdef FEAT_GUI
13354 if (gui.in_use)
13355 {
13356 int x, y;
13357
13358 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013359 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360 }
13361#endif
13362}
13363
13364/*
13365 * "getwinposy()" function
13366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013368f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013369{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013370 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013371#ifdef FEAT_GUI
13372 if (gui.in_use)
13373 {
13374 int x, y;
13375
13376 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013377 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013378 }
13379#endif
13380}
13381
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013382/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013383 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013384 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013385 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013386find_win_by_nr(
13387 typval_T *vp,
13388 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013389{
13390#ifdef FEAT_WINDOWS
13391 win_T *wp;
13392#endif
13393 int nr;
13394
13395 nr = get_tv_number_chk(vp, NULL);
13396
13397#ifdef FEAT_WINDOWS
13398 if (nr < 0)
13399 return NULL;
13400 if (nr == 0)
13401 return curwin;
13402
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013403 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13404 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013405 if (--nr <= 0)
13406 break;
13407 return wp;
13408#else
13409 if (nr == 0 || nr == 1)
13410 return curwin;
13411 return NULL;
13412#endif
13413}
13414
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013416 * Find window specified by "wvp" in tabpage "tvp".
13417 */
13418 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013419find_tabwin(
13420 typval_T *wvp, /* VAR_UNKNOWN for current window */
13421 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013422{
13423 win_T *wp = NULL;
13424 tabpage_T *tp = NULL;
13425 long n;
13426
13427 if (wvp->v_type != VAR_UNKNOWN)
13428 {
13429 if (tvp->v_type != VAR_UNKNOWN)
13430 {
13431 n = get_tv_number(tvp);
13432 if (n >= 0)
13433 tp = find_tabpage(n);
13434 }
13435 else
13436 tp = curtab;
13437
13438 if (tp != NULL)
13439 wp = find_win_by_nr(wvp, tp);
13440 }
13441 else
13442 wp = curwin;
13443
13444 return wp;
13445}
13446
13447/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 * "getwinvar()" function
13449 */
13450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013451f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013453 getwinvar(argvars, rettv, 0);
13454}
13455
13456/*
13457 * getwinvar() and gettabwinvar()
13458 */
13459 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013460getwinvar(
13461 typval_T *argvars,
13462 typval_T *rettv,
13463 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013464{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013465 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013467 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013468 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013469 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013470#ifdef FEAT_WINDOWS
13471 win_T *oldcurwin;
13472 tabpage_T *oldtabpage;
13473 int need_switch_win;
13474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013476#ifdef FEAT_WINDOWS
13477 if (off == 1)
13478 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13479 else
13480 tp = curtab;
13481#endif
13482 win = find_win_by_nr(&argvars[off], tp);
13483 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013484 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013485
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013486 rettv->v_type = VAR_STRING;
13487 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013488
13489 if (win != NULL && varname != NULL)
13490 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013491#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013492 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013493 * otherwise the window is not valid. Only do this when needed,
13494 * autocommands get blocked. */
13495 need_switch_win = !(tp == curtab && win == curwin);
13496 if (!need_switch_win
13497 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13498#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013499 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013500 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013501 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013502 if (get_option_tv(&varname, rettv, 1) == OK)
13503 done = TRUE;
13504 }
13505 else
13506 {
13507 /* Look up the variable. */
13508 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13509 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13510 varname, FALSE);
13511 if (v != NULL)
13512 {
13513 copy_tv(&v->di_tv, rettv);
13514 done = TRUE;
13515 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013518
Bram Moolenaarba117c22015-09-29 16:53:22 +020013519#ifdef FEAT_WINDOWS
13520 if (need_switch_win)
13521 /* restore previous notion of curwin */
13522 restore_win(oldcurwin, oldtabpage, TRUE);
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524 }
13525
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013526 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13527 /* use the default return value */
13528 copy_tv(&argvars[off + 2], rettv);
13529
Bram Moolenaar071d4272004-06-13 20:20:40 +000013530 --emsg_off;
13531}
13532
13533/*
13534 * "glob()" function
13535 */
13536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013537f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013538{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013539 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013540 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013541 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013543 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013544 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013545 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013546 if (argvars[1].v_type != VAR_UNKNOWN)
13547 {
13548 if (get_tv_number_chk(&argvars[1], &error))
13549 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013550 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013551 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013552 if (get_tv_number_chk(&argvars[2], &error))
13553 {
13554 rettv->v_type = VAR_LIST;
13555 rettv->vval.v_list = NULL;
13556 }
13557 if (argvars[3].v_type != VAR_UNKNOWN
13558 && get_tv_number_chk(&argvars[3], &error))
13559 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013560 }
13561 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013562 if (!error)
13563 {
13564 ExpandInit(&xpc);
13565 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013566 if (p_wic)
13567 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013568 if (rettv->v_type == VAR_STRING)
13569 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013570 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013571 else if (rettv_list_alloc(rettv) != FAIL)
13572 {
13573 int i;
13574
13575 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13576 NULL, options, WILD_ALL_KEEP);
13577 for (i = 0; i < xpc.xp_numfiles; i++)
13578 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13579
13580 ExpandCleanup(&xpc);
13581 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013582 }
13583 else
13584 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013585}
13586
13587/*
13588 * "globpath()" function
13589 */
13590 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013591f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013592{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013593 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013595 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013596 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013597 garray_T ga;
13598 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013599
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013600 /* When the optional second argument is non-zero, don't remove matches
13601 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013602 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013603 if (argvars[2].v_type != VAR_UNKNOWN)
13604 {
13605 if (get_tv_number_chk(&argvars[2], &error))
13606 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013607 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013608 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013609 if (get_tv_number_chk(&argvars[3], &error))
13610 {
13611 rettv->v_type = VAR_LIST;
13612 rettv->vval.v_list = NULL;
13613 }
13614 if (argvars[4].v_type != VAR_UNKNOWN
13615 && get_tv_number_chk(&argvars[4], &error))
13616 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013617 }
13618 }
13619 if (file != NULL && !error)
13620 {
13621 ga_init2(&ga, (int)sizeof(char_u *), 10);
13622 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13623 if (rettv->v_type == VAR_STRING)
13624 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13625 else if (rettv_list_alloc(rettv) != FAIL)
13626 for (i = 0; i < ga.ga_len; ++i)
13627 list_append_string(rettv->vval.v_list,
13628 ((char_u **)(ga.ga_data))[i], -1);
13629 ga_clear_strings(&ga);
13630 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013631 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013632 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633}
13634
13635/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013636 * "glob2regpat()" function
13637 */
13638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013639f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013640{
13641 char_u *pat = get_tv_string_chk(&argvars[0]);
13642
13643 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013644 rettv->vval.v_string = (pat == NULL)
13645 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013646}
13647
13648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013649 * "has()" function
13650 */
13651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013652f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013653{
13654 int i;
13655 char_u *name;
13656 int n = FALSE;
13657 static char *(has_list[]) =
13658 {
13659#ifdef AMIGA
13660 "amiga",
13661# ifdef FEAT_ARP
13662 "arp",
13663# endif
13664#endif
13665#ifdef __BEOS__
13666 "beos",
13667#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013668#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013669 "mac",
13670#endif
13671#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013672 "macunix", /* built with 'darwin' enabled */
13673#endif
13674#if defined(__APPLE__) && __APPLE__ == 1
13675 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677#ifdef __QNX__
13678 "qnx",
13679#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013680#ifdef UNIX
13681 "unix",
13682#endif
13683#ifdef VMS
13684 "vms",
13685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#ifdef WIN32
13687 "win32",
13688#endif
13689#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13690 "win32unix",
13691#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013692#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693 "win64",
13694#endif
13695#ifdef EBCDIC
13696 "ebcdic",
13697#endif
13698#ifndef CASE_INSENSITIVE_FILENAME
13699 "fname_case",
13700#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013701#ifdef HAVE_ACL
13702 "acl",
13703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_ARABIC
13705 "arabic",
13706#endif
13707#ifdef FEAT_AUTOCMD
13708 "autocmd",
13709#endif
13710#ifdef FEAT_BEVAL
13711 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013712# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13713 "balloon_multiline",
13714# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013715#endif
13716#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13717 "builtin_terms",
13718# ifdef ALL_BUILTIN_TCAPS
13719 "all_builtin_terms",
13720# endif
13721#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013722#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13723 || defined(FEAT_GUI_W32) \
13724 || defined(FEAT_GUI_MOTIF))
13725 "browsefilter",
13726#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013727#ifdef FEAT_BYTEOFF
13728 "byte_offset",
13729#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013730#ifdef FEAT_CHANNEL
13731 "channel",
13732#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013733#ifdef FEAT_CINDENT
13734 "cindent",
13735#endif
13736#ifdef FEAT_CLIENTSERVER
13737 "clientserver",
13738#endif
13739#ifdef FEAT_CLIPBOARD
13740 "clipboard",
13741#endif
13742#ifdef FEAT_CMDL_COMPL
13743 "cmdline_compl",
13744#endif
13745#ifdef FEAT_CMDHIST
13746 "cmdline_hist",
13747#endif
13748#ifdef FEAT_COMMENTS
13749 "comments",
13750#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013751#ifdef FEAT_CONCEAL
13752 "conceal",
13753#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013754#ifdef FEAT_CRYPT
13755 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013756 "crypt-blowfish",
13757 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013758#endif
13759#ifdef FEAT_CSCOPE
13760 "cscope",
13761#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013762#ifdef FEAT_CURSORBIND
13763 "cursorbind",
13764#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013765#ifdef CURSOR_SHAPE
13766 "cursorshape",
13767#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768#ifdef DEBUG
13769 "debug",
13770#endif
13771#ifdef FEAT_CON_DIALOG
13772 "dialog_con",
13773#endif
13774#ifdef FEAT_GUI_DIALOG
13775 "dialog_gui",
13776#endif
13777#ifdef FEAT_DIFF
13778 "diff",
13779#endif
13780#ifdef FEAT_DIGRAPHS
13781 "digraphs",
13782#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013783#ifdef FEAT_DIRECTX
13784 "directx",
13785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#ifdef FEAT_DND
13787 "dnd",
13788#endif
13789#ifdef FEAT_EMACS_TAGS
13790 "emacs_tags",
13791#endif
13792 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013793 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013794#ifdef FEAT_SEARCH_EXTRA
13795 "extra_search",
13796#endif
13797#ifdef FEAT_FKMAP
13798 "farsi",
13799#endif
13800#ifdef FEAT_SEARCHPATH
13801 "file_in_path",
13802#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013803#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013804 "filterpipe",
13805#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013806#ifdef FEAT_FIND_ID
13807 "find_in_path",
13808#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013809#ifdef FEAT_FLOAT
13810 "float",
13811#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013812#ifdef FEAT_FOLDING
13813 "folding",
13814#endif
13815#ifdef FEAT_FOOTER
13816 "footer",
13817#endif
13818#if !defined(USE_SYSTEM) && defined(UNIX)
13819 "fork",
13820#endif
13821#ifdef FEAT_GETTEXT
13822 "gettext",
13823#endif
13824#ifdef FEAT_GUI
13825 "gui",
13826#endif
13827#ifdef FEAT_GUI_ATHENA
13828# ifdef FEAT_GUI_NEXTAW
13829 "gui_neXtaw",
13830# else
13831 "gui_athena",
13832# endif
13833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013834#ifdef FEAT_GUI_GTK
13835 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013836# ifdef USE_GTK3
13837 "gui_gtk3",
13838# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013839 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013840# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013841#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013842#ifdef FEAT_GUI_GNOME
13843 "gui_gnome",
13844#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013845#ifdef FEAT_GUI_MAC
13846 "gui_mac",
13847#endif
13848#ifdef FEAT_GUI_MOTIF
13849 "gui_motif",
13850#endif
13851#ifdef FEAT_GUI_PHOTON
13852 "gui_photon",
13853#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013854#ifdef FEAT_GUI_W32
13855 "gui_win32",
13856#endif
13857#ifdef FEAT_HANGULIN
13858 "hangul_input",
13859#endif
13860#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13861 "iconv",
13862#endif
13863#ifdef FEAT_INS_EXPAND
13864 "insert_expand",
13865#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013866#ifdef FEAT_JOB
13867 "job",
13868#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013869#ifdef FEAT_JUMPLIST
13870 "jumplist",
13871#endif
13872#ifdef FEAT_KEYMAP
13873 "keymap",
13874#endif
13875#ifdef FEAT_LANGMAP
13876 "langmap",
13877#endif
13878#ifdef FEAT_LIBCALL
13879 "libcall",
13880#endif
13881#ifdef FEAT_LINEBREAK
13882 "linebreak",
13883#endif
13884#ifdef FEAT_LISP
13885 "lispindent",
13886#endif
13887#ifdef FEAT_LISTCMDS
13888 "listcmds",
13889#endif
13890#ifdef FEAT_LOCALMAP
13891 "localmap",
13892#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013893#ifdef FEAT_LUA
13894# ifndef DYNAMIC_LUA
13895 "lua",
13896# endif
13897#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013898#ifdef FEAT_MENU
13899 "menu",
13900#endif
13901#ifdef FEAT_SESSION
13902 "mksession",
13903#endif
13904#ifdef FEAT_MODIFY_FNAME
13905 "modify_fname",
13906#endif
13907#ifdef FEAT_MOUSE
13908 "mouse",
13909#endif
13910#ifdef FEAT_MOUSESHAPE
13911 "mouseshape",
13912#endif
13913#if defined(UNIX) || defined(VMS)
13914# ifdef FEAT_MOUSE_DEC
13915 "mouse_dec",
13916# endif
13917# ifdef FEAT_MOUSE_GPM
13918 "mouse_gpm",
13919# endif
13920# ifdef FEAT_MOUSE_JSB
13921 "mouse_jsbterm",
13922# endif
13923# ifdef FEAT_MOUSE_NET
13924 "mouse_netterm",
13925# endif
13926# ifdef FEAT_MOUSE_PTERM
13927 "mouse_pterm",
13928# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013929# ifdef FEAT_MOUSE_SGR
13930 "mouse_sgr",
13931# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013932# ifdef FEAT_SYSMOUSE
13933 "mouse_sysmouse",
13934# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013935# ifdef FEAT_MOUSE_URXVT
13936 "mouse_urxvt",
13937# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013938# ifdef FEAT_MOUSE_XTERM
13939 "mouse_xterm",
13940# endif
13941#endif
13942#ifdef FEAT_MBYTE
13943 "multi_byte",
13944#endif
13945#ifdef FEAT_MBYTE_IME
13946 "multi_byte_ime",
13947#endif
13948#ifdef FEAT_MULTI_LANG
13949 "multi_lang",
13950#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013951#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013952#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013953 "mzscheme",
13954#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013955#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013956#ifdef FEAT_OLE
13957 "ole",
13958#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013959 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013960#ifdef FEAT_PATH_EXTRA
13961 "path_extra",
13962#endif
13963#ifdef FEAT_PERL
13964#ifndef DYNAMIC_PERL
13965 "perl",
13966#endif
13967#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013968#ifdef FEAT_PERSISTENT_UNDO
13969 "persistent_undo",
13970#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013971#ifdef FEAT_PYTHON
13972#ifndef DYNAMIC_PYTHON
13973 "python",
13974#endif
13975#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013976#ifdef FEAT_PYTHON3
13977#ifndef DYNAMIC_PYTHON3
13978 "python3",
13979#endif
13980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981#ifdef FEAT_POSTSCRIPT
13982 "postscript",
13983#endif
13984#ifdef FEAT_PRINTER
13985 "printer",
13986#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013987#ifdef FEAT_PROFILE
13988 "profile",
13989#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013990#ifdef FEAT_RELTIME
13991 "reltime",
13992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993#ifdef FEAT_QUICKFIX
13994 "quickfix",
13995#endif
13996#ifdef FEAT_RIGHTLEFT
13997 "rightleft",
13998#endif
13999#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
14000 "ruby",
14001#endif
14002#ifdef FEAT_SCROLLBIND
14003 "scrollbind",
14004#endif
14005#ifdef FEAT_CMDL_INFO
14006 "showcmd",
14007 "cmdline_info",
14008#endif
14009#ifdef FEAT_SIGNS
14010 "signs",
14011#endif
14012#ifdef FEAT_SMARTINDENT
14013 "smartindent",
14014#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000014015#ifdef STARTUPTIME
14016 "startuptime",
14017#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018#ifdef FEAT_STL_OPT
14019 "statusline",
14020#endif
14021#ifdef FEAT_SUN_WORKSHOP
14022 "sun_workshop",
14023#endif
14024#ifdef FEAT_NETBEANS_INTG
14025 "netbeans_intg",
14026#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014027#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014028 "spell",
14029#endif
14030#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 "syntax",
14032#endif
14033#if defined(USE_SYSTEM) || !defined(UNIX)
14034 "system",
14035#endif
14036#ifdef FEAT_TAG_BINS
14037 "tag_binary",
14038#endif
14039#ifdef FEAT_TAG_OLDSTATIC
14040 "tag_old_static",
14041#endif
14042#ifdef FEAT_TAG_ANYWHITE
14043 "tag_any_white",
14044#endif
14045#ifdef FEAT_TCL
14046# ifndef DYNAMIC_TCL
14047 "tcl",
14048# endif
14049#endif
14050#ifdef TERMINFO
14051 "terminfo",
14052#endif
14053#ifdef FEAT_TERMRESPONSE
14054 "termresponse",
14055#endif
14056#ifdef FEAT_TEXTOBJ
14057 "textobjects",
14058#endif
14059#ifdef HAVE_TGETENT
14060 "tgetent",
14061#endif
14062#ifdef FEAT_TITLE
14063 "title",
14064#endif
14065#ifdef FEAT_TOOLBAR
14066 "toolbar",
14067#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014068#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14069 "unnamedplus",
14070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071#ifdef FEAT_USR_CMDS
14072 "user-commands", /* was accidentally included in 5.4 */
14073 "user_commands",
14074#endif
14075#ifdef FEAT_VIMINFO
14076 "viminfo",
14077#endif
14078#ifdef FEAT_VERTSPLIT
14079 "vertsplit",
14080#endif
14081#ifdef FEAT_VIRTUALEDIT
14082 "virtualedit",
14083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085#ifdef FEAT_VISUALEXTRA
14086 "visualextra",
14087#endif
14088#ifdef FEAT_VREPLACE
14089 "vreplace",
14090#endif
14091#ifdef FEAT_WILDIGN
14092 "wildignore",
14093#endif
14094#ifdef FEAT_WILDMENU
14095 "wildmenu",
14096#endif
14097#ifdef FEAT_WINDOWS
14098 "windows",
14099#endif
14100#ifdef FEAT_WAK
14101 "winaltkeys",
14102#endif
14103#ifdef FEAT_WRITEBACKUP
14104 "writebackup",
14105#endif
14106#ifdef FEAT_XIM
14107 "xim",
14108#endif
14109#ifdef FEAT_XFONTSET
14110 "xfontset",
14111#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014112#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014113 "xpm",
14114 "xpm_w32", /* for backward compatibility */
14115#else
14116# if defined(HAVE_XPM)
14117 "xpm",
14118# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014119#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014120#ifdef USE_XSMP
14121 "xsmp",
14122#endif
14123#ifdef USE_XSMP_INTERACT
14124 "xsmp_interact",
14125#endif
14126#ifdef FEAT_XCLIPBOARD
14127 "xterm_clipboard",
14128#endif
14129#ifdef FEAT_XTERM_SAVE
14130 "xterm_save",
14131#endif
14132#if defined(UNIX) && defined(FEAT_X11)
14133 "X11",
14134#endif
14135 NULL
14136 };
14137
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 for (i = 0; has_list[i] != NULL; ++i)
14140 if (STRICMP(name, has_list[i]) == 0)
14141 {
14142 n = TRUE;
14143 break;
14144 }
14145
14146 if (n == FALSE)
14147 {
14148 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014149 {
14150 if (name[5] == '-'
14151 && STRLEN(name) > 11
14152 && vim_isdigit(name[6])
14153 && vim_isdigit(name[8])
14154 && vim_isdigit(name[10]))
14155 {
14156 int major = atoi((char *)name + 6);
14157 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014158
14159 /* Expect "patch-9.9.01234". */
14160 n = (major < VIM_VERSION_MAJOR
14161 || (major == VIM_VERSION_MAJOR
14162 && (minor < VIM_VERSION_MINOR
14163 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014164 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014165 }
14166 else
14167 n = has_patch(atoi((char *)name + 5));
14168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169 else if (STRICMP(name, "vim_starting") == 0)
14170 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014171#ifdef FEAT_MBYTE
14172 else if (STRICMP(name, "multi_byte_encoding") == 0)
14173 n = has_mbyte;
14174#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014175#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14176 else if (STRICMP(name, "balloon_multiline") == 0)
14177 n = multiline_balloon_available();
14178#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014179#ifdef DYNAMIC_TCL
14180 else if (STRICMP(name, "tcl") == 0)
14181 n = tcl_enabled(FALSE);
14182#endif
14183#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14184 else if (STRICMP(name, "iconv") == 0)
14185 n = iconv_enabled(FALSE);
14186#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014187#ifdef DYNAMIC_LUA
14188 else if (STRICMP(name, "lua") == 0)
14189 n = lua_enabled(FALSE);
14190#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014191#ifdef DYNAMIC_MZSCHEME
14192 else if (STRICMP(name, "mzscheme") == 0)
14193 n = mzscheme_enabled(FALSE);
14194#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014195#ifdef DYNAMIC_RUBY
14196 else if (STRICMP(name, "ruby") == 0)
14197 n = ruby_enabled(FALSE);
14198#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014199#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200#ifdef DYNAMIC_PYTHON
14201 else if (STRICMP(name, "python") == 0)
14202 n = python_enabled(FALSE);
14203#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014204#endif
14205#ifdef FEAT_PYTHON3
14206#ifdef DYNAMIC_PYTHON3
14207 else if (STRICMP(name, "python3") == 0)
14208 n = python3_enabled(FALSE);
14209#endif
14210#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211#ifdef DYNAMIC_PERL
14212 else if (STRICMP(name, "perl") == 0)
14213 n = perl_enabled(FALSE);
14214#endif
14215#ifdef FEAT_GUI
14216 else if (STRICMP(name, "gui_running") == 0)
14217 n = (gui.in_use || gui.starting);
14218# ifdef FEAT_GUI_W32
14219 else if (STRICMP(name, "gui_win32s") == 0)
14220 n = gui_is_win32s();
14221# endif
14222# ifdef FEAT_BROWSE
14223 else if (STRICMP(name, "browse") == 0)
14224 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14225# endif
14226#endif
14227#ifdef FEAT_SYN_HL
14228 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014229 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230#endif
14231#if defined(WIN3264)
14232 else if (STRICMP(name, "win95") == 0)
14233 n = mch_windows95();
14234#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014235#ifdef FEAT_NETBEANS_INTG
14236 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014237 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 }
14240
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242}
14243
14244/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014245 * "has_key()" function
14246 */
14247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014248f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014249{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014250 if (argvars[0].v_type != VAR_DICT)
14251 {
14252 EMSG(_(e_dictreq));
14253 return;
14254 }
14255 if (argvars[0].vval.v_dict == NULL)
14256 return;
14257
14258 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014259 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014260}
14261
14262/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014263 * "haslocaldir()" function
14264 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014266f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014267{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014268 win_T *wp = NULL;
14269
14270 wp = find_tabwin(&argvars[0], &argvars[1]);
14271 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014272}
14273
14274/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 * "hasmapto()" function
14276 */
14277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014278f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279{
14280 char_u *name;
14281 char_u *mode;
14282 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014283 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014284
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014285 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014286 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014287 mode = (char_u *)"nvo";
14288 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014289 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014290 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014291 if (argvars[2].v_type != VAR_UNKNOWN)
14292 abbr = get_tv_number(&argvars[2]);
14293 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294
Bram Moolenaar2c932302006-03-18 21:42:09 +000014295 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014296 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014298 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299}
14300
14301/*
14302 * "histadd()" function
14303 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014305f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306{
14307#ifdef FEAT_CMDHIST
14308 int histype;
14309 char_u *str;
14310 char_u buf[NUMBUFLEN];
14311#endif
14312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 if (check_restricted() || check_secure())
14315 return;
14316#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14318 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 if (histype >= 0)
14320 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014321 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 if (*str != NUL)
14323 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014324 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014326 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014327 return;
14328 }
14329 }
14330#endif
14331}
14332
14333/*
14334 * "histdel()" function
14335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014337f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
14339#ifdef FEAT_CMDHIST
14340 int n;
14341 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014342 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14345 if (str == NULL)
14346 n = 0;
14347 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014349 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014350 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014352 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014353 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 else
14355 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014356 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 get_tv_string_buf(&argvars[1], buf));
14358 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359#endif
14360}
14361
14362/*
14363 * "histget()" function
14364 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014366f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367{
14368#ifdef FEAT_CMDHIST
14369 int type;
14370 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014372
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14374 if (str == NULL)
14375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 {
14378 type = get_histtype(str);
14379 if (argvars[1].v_type == VAR_UNKNOWN)
14380 idx = get_history_idx(type);
14381 else
14382 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14383 /* -1 on type error */
14384 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14385 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014387 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014388#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390}
14391
14392/*
14393 * "histnr()" function
14394 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014396f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397{
14398 int i;
14399
14400#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014401 char_u *history = get_tv_string_chk(&argvars[0]);
14402
14403 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404 if (i >= HIST_CMD && i < HIST_COUNT)
14405 i = get_history_idx(i);
14406 else
14407#endif
14408 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410}
14411
14412/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014413 * "highlightID(name)" function
14414 */
14415 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014416f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014417{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014418 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014419}
14420
14421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014422 * "highlight_exists()" function
14423 */
14424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014425f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014426{
14427 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14428}
14429
14430/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 * "hostname()" function
14432 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014434f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435{
14436 char_u hostname[256];
14437
14438 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 rettv->v_type = VAR_STRING;
14440 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441}
14442
14443/*
14444 * iconv() function
14445 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014446 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014447f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014448{
14449#ifdef FEAT_MBYTE
14450 char_u buf1[NUMBUFLEN];
14451 char_u buf2[NUMBUFLEN];
14452 char_u *from, *to, *str;
14453 vimconv_T vimconv;
14454#endif
14455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014456 rettv->v_type = VAR_STRING;
14457 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458
14459#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 str = get_tv_string(&argvars[0]);
14461 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14462 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 vimconv.vc_type = CONV_NONE;
14464 convert_setup(&vimconv, from, to);
14465
14466 /* If the encodings are equal, no conversion needed. */
14467 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014468 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014470 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014471
14472 convert_setup(&vimconv, NULL, NULL);
14473 vim_free(from);
14474 vim_free(to);
14475#endif
14476}
14477
14478/*
14479 * "indent()" function
14480 */
14481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014482f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014483{
14484 linenr_T lnum;
14485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014486 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014488 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014490 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491}
14492
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014493/*
14494 * "index()" function
14495 */
14496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014497f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014498{
Bram Moolenaar33570922005-01-25 22:26:29 +000014499 list_T *l;
14500 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014501 long idx = 0;
14502 int ic = FALSE;
14503
14504 rettv->vval.v_number = -1;
14505 if (argvars[0].v_type != VAR_LIST)
14506 {
14507 EMSG(_(e_listreq));
14508 return;
14509 }
14510 l = argvars[0].vval.v_list;
14511 if (l != NULL)
14512 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014513 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014514 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014515 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014516 int error = FALSE;
14517
Bram Moolenaar758711c2005-02-02 23:11:38 +000014518 /* Start at specified item. Use the cached index that list_find()
14519 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014520 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014521 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014522 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014523 ic = get_tv_number_chk(&argvars[3], &error);
14524 if (error)
14525 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014526 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014527
Bram Moolenaar758711c2005-02-02 23:11:38 +000014528 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014529 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014530 {
14531 rettv->vval.v_number = idx;
14532 break;
14533 }
14534 }
14535}
14536
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537static int inputsecret_flag = 0;
14538
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014539static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014540
Bram Moolenaar071d4272004-06-13 20:20:40 +000014541/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014542 * This function is used by f_input() and f_inputdialog() functions. The third
14543 * argument to f_input() specifies the type of completion to use at the
14544 * prompt. The third argument to f_inputdialog() specifies the value to return
14545 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014546 */
14547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014548get_user_input(
14549 typval_T *argvars,
14550 typval_T *rettv,
14551 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014552{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014553 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554 char_u *p = NULL;
14555 int c;
14556 char_u buf[NUMBUFLEN];
14557 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014559 int xp_type = EXPAND_NOTHING;
14560 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014562 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014563 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014564
14565#ifdef NO_CONSOLE_INPUT
14566 /* While starting up, there is no place to enter text. */
14567 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014568 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014569#endif
14570
14571 cmd_silent = FALSE; /* Want to see the prompt. */
14572 if (prompt != NULL)
14573 {
14574 /* Only the part of the message after the last NL is considered as
14575 * prompt for the command line */
14576 p = vim_strrchr(prompt, '\n');
14577 if (p == NULL)
14578 p = prompt;
14579 else
14580 {
14581 ++p;
14582 c = *p;
14583 *p = NUL;
14584 msg_start();
14585 msg_clr_eos();
14586 msg_puts_attr(prompt, echo_attr);
14587 msg_didout = FALSE;
14588 msg_starthere();
14589 *p = c;
14590 }
14591 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014593 if (argvars[1].v_type != VAR_UNKNOWN)
14594 {
14595 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14596 if (defstr != NULL)
14597 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014598
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014599 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014600 {
14601 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014602 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014603 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014604
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014605 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014606 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014607
Bram Moolenaar4463f292005-09-25 22:20:24 +000014608 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14609 if (xp_name == NULL)
14610 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014611
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014612 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014613
Bram Moolenaar4463f292005-09-25 22:20:24 +000014614 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14615 &xp_arg) == FAIL)
14616 return;
14617 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014618 }
14619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014620 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014621 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014622 int save_ex_normal_busy = ex_normal_busy;
14623 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014624 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014625 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14626 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014627 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014628 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014629 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014630 && argvars[1].v_type != VAR_UNKNOWN
14631 && argvars[2].v_type != VAR_UNKNOWN)
14632 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14633 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014634
14635 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014636
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014637 /* since the user typed this, no need to wait for return */
14638 need_wait_return = FALSE;
14639 msg_didout = FALSE;
14640 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641 cmd_silent = cmd_silent_save;
14642}
14643
14644/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014645 * "input()" function
14646 * Also handles inputsecret() when inputsecret is set.
14647 */
14648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014649f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014650{
14651 get_user_input(argvars, rettv, FALSE);
14652}
14653
14654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655 * "inputdialog()" function
14656 */
14657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014658f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659{
14660#if defined(FEAT_GUI_TEXTDIALOG)
14661 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14662 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14663 {
14664 char_u *message;
14665 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014666 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014668 message = get_tv_string_chk(&argvars[0]);
14669 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014670 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014671 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 else
14673 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014674 if (message != NULL && defstr != NULL
14675 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014676 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014677 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014678 else
14679 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014680 if (message != NULL && defstr != NULL
14681 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014682 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014683 rettv->vval.v_string = vim_strsave(
14684 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014686 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014688 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014689 }
14690 else
14691#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014692 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014695/*
14696 * "inputlist()" function
14697 */
14698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014699f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014700{
14701 listitem_T *li;
14702 int selected;
14703 int mouse_used;
14704
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014705#ifdef NO_CONSOLE_INPUT
14706 /* While starting up, there is no place to enter text. */
14707 if (no_console_input())
14708 return;
14709#endif
14710 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14711 {
14712 EMSG2(_(e_listarg), "inputlist()");
14713 return;
14714 }
14715
14716 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014717 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014718 lines_left = Rows; /* avoid more prompt */
14719 msg_scroll = TRUE;
14720 msg_clr_eos();
14721
14722 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14723 {
14724 msg_puts(get_tv_string(&li->li_tv));
14725 msg_putchar('\n');
14726 }
14727
14728 /* Ask for choice. */
14729 selected = prompt_for_number(&mouse_used);
14730 if (mouse_used)
14731 selected -= lines_left;
14732
14733 rettv->vval.v_number = selected;
14734}
14735
14736
Bram Moolenaar071d4272004-06-13 20:20:40 +000014737static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14738
14739/*
14740 * "inputrestore()" function
14741 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014743f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014744{
14745 if (ga_userinput.ga_len > 0)
14746 {
14747 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014748 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14749 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014750 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014751 }
14752 else if (p_verbose > 1)
14753 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014754 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014755 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756 }
14757}
14758
14759/*
14760 * "inputsave()" function
14761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014763f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014765 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014766 if (ga_grow(&ga_userinput, 1) == OK)
14767 {
14768 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14769 + ga_userinput.ga_len);
14770 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014771 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014772 }
14773 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014774 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014775}
14776
14777/*
14778 * "inputsecret()" function
14779 */
14780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014781f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014782{
14783 ++cmdline_star;
14784 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014785 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014786 --cmdline_star;
14787 --inputsecret_flag;
14788}
14789
14790/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014791 * "insert()" function
14792 */
14793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014794f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014795{
14796 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014797 listitem_T *item;
14798 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014799 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014800
14801 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014802 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014803 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014804 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014805 {
14806 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014807 before = get_tv_number_chk(&argvars[2], &error);
14808 if (error)
14809 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014810
Bram Moolenaar758711c2005-02-02 23:11:38 +000014811 if (before == l->lv_len)
14812 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014813 else
14814 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014815 item = list_find(l, before);
14816 if (item == NULL)
14817 {
14818 EMSGN(_(e_listidx), before);
14819 l = NULL;
14820 }
14821 }
14822 if (l != NULL)
14823 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014824 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014825 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014826 }
14827 }
14828}
14829
14830/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014831 * "invert(expr)" function
14832 */
14833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014834f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014835{
14836 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14837}
14838
14839/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014840 * "isdirectory()" function
14841 */
14842 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014843f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014844{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014845 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014846}
14847
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014848/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014849 * "islocked()" function
14850 */
14851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014852f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014853{
14854 lval_T lv;
14855 char_u *end;
14856 dictitem_T *di;
14857
14858 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014859 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14860 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014861 if (end != NULL && lv.ll_name != NULL)
14862 {
14863 if (*end != NUL)
14864 EMSG(_(e_trailing));
14865 else
14866 {
14867 if (lv.ll_tv == NULL)
14868 {
14869 if (check_changedtick(lv.ll_name))
14870 rettv->vval.v_number = 1; /* always locked */
14871 else
14872 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014873 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014874 if (di != NULL)
14875 {
14876 /* Consider a variable locked when:
14877 * 1. the variable itself is locked
14878 * 2. the value of the variable is locked.
14879 * 3. the List or Dict value is locked.
14880 */
14881 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14882 || tv_islocked(&di->di_tv));
14883 }
14884 }
14885 }
14886 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014887 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014888 else if (lv.ll_newkey != NULL)
14889 EMSG2(_(e_dictkey), lv.ll_newkey);
14890 else if (lv.ll_list != NULL)
14891 /* List item. */
14892 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14893 else
14894 /* Dictionary item. */
14895 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14896 }
14897 }
14898
14899 clear_lval(&lv);
14900}
14901
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014902#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14903/*
14904 * "isnan()" function
14905 */
14906 static void
14907f_isnan(typval_T *argvars, typval_T *rettv)
14908{
14909 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14910 && isnan(argvars[0].vval.v_float);
14911}
14912#endif
14913
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014914static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014915
14916/*
14917 * Turn a dict into a list:
14918 * "what" == 0: list of keys
14919 * "what" == 1: list of values
14920 * "what" == 2: list of items
14921 */
14922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014923dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014924{
Bram Moolenaar33570922005-01-25 22:26:29 +000014925 list_T *l2;
14926 dictitem_T *di;
14927 hashitem_T *hi;
14928 listitem_T *li;
14929 listitem_T *li2;
14930 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014931 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014932
Bram Moolenaar8c711452005-01-14 21:53:12 +000014933 if (argvars[0].v_type != VAR_DICT)
14934 {
14935 EMSG(_(e_dictreq));
14936 return;
14937 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014938 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014939 return;
14940
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014941 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014943
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014944 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014945 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014946 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014947 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014948 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014949 --todo;
14950 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014951
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014952 li = listitem_alloc();
14953 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014954 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014955 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014956
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014957 if (what == 0)
14958 {
14959 /* keys() */
14960 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014961 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014962 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14963 }
14964 else if (what == 1)
14965 {
14966 /* values() */
14967 copy_tv(&di->di_tv, &li->li_tv);
14968 }
14969 else
14970 {
14971 /* items() */
14972 l2 = list_alloc();
14973 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014974 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014975 li->li_tv.vval.v_list = l2;
14976 if (l2 == NULL)
14977 break;
14978 ++l2->lv_refcount;
14979
14980 li2 = listitem_alloc();
14981 if (li2 == NULL)
14982 break;
14983 list_append(l2, li2);
14984 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014985 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014986 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14987
14988 li2 = listitem_alloc();
14989 if (li2 == NULL)
14990 break;
14991 list_append(l2, li2);
14992 copy_tv(&di->di_tv, &li2->li_tv);
14993 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014994 }
14995 }
14996}
14997
14998/*
14999 * "items(dict)" function
15000 */
15001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015002f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015003{
15004 dict_list(argvars, rettv, 2);
15005}
15006
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015007#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015008/*
15009 * Get the job from the argument.
15010 * Returns NULL if the job is invalid.
15011 */
15012 static job_T *
15013get_job_arg(typval_T *tv)
15014{
15015 job_T *job;
15016
15017 if (tv->v_type != VAR_JOB)
15018 {
15019 EMSG2(_(e_invarg2), get_tv_string(tv));
15020 return NULL;
15021 }
15022 job = tv->vval.v_job;
15023
15024 if (job == NULL)
15025 EMSG(_("E916: not a valid job"));
15026 return job;
15027}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015028
15029# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015030/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015031 * "job_getchannel()" function
15032 */
15033 static void
15034f_job_getchannel(typval_T *argvars, typval_T *rettv)
15035{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015036 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015037
Bram Moolenaar65edff82016-02-21 16:40:11 +010015038 if (job != NULL)
15039 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015040 rettv->v_type = VAR_CHANNEL;
15041 rettv->vval.v_channel = job->jv_channel;
15042 if (job->jv_channel != NULL)
15043 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015044 }
15045}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015046# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015047
15048/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015049 * "job_setoptions()" function
15050 */
15051 static void
15052f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15053{
15054 job_T *job = get_job_arg(&argvars[0]);
15055 jobopt_T opt;
15056
15057 if (job == NULL)
15058 return;
15059 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015060 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015061 return;
15062 job_set_options(job, &opt);
15063}
15064
15065/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015066 * "job_start()" function
15067 */
15068 static void
15069f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15070{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015071 job_T *job;
15072 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015073#if defined(UNIX)
15074# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015075 char **argv = NULL;
15076 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015077#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015078 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015079#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015080 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015081
15082 rettv->v_type = VAR_JOB;
15083 job = job_alloc();
15084 rettv->vval.v_job = job;
15085 if (job == NULL)
15086 return;
15087
15088 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015089
15090 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015091 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015092 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015093 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015094 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015095 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015096 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015097 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015098
Bram Moolenaar835dc632016-02-07 14:27:38 +010015099#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015100 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015101#endif
15102
15103 if (argvars[0].v_type == VAR_STRING)
15104 {
15105 /* Command is a string. */
15106 cmd = argvars[0].vval.v_string;
15107#ifdef USE_ARGV
15108 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15109 return;
15110 argv[argc] = NULL;
15111#endif
15112 }
15113 else if (argvars[0].v_type != VAR_LIST
15114 || argvars[0].vval.v_list == NULL
15115 || argvars[0].vval.v_list->lv_len < 1)
15116 {
15117 EMSG(_(e_invarg));
15118 return;
15119 }
15120 else
15121 {
15122 list_T *l = argvars[0].vval.v_list;
15123 listitem_T *li;
15124 char_u *s;
15125
15126#ifdef USE_ARGV
15127 /* Pass argv[] to mch_call_shell(). */
15128 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15129 if (argv == NULL)
15130 return;
15131#endif
15132 for (li = l->lv_first; li != NULL; li = li->li_next)
15133 {
15134 s = get_tv_string_chk(&li->li_tv);
15135 if (s == NULL)
15136 goto theend;
15137#ifdef USE_ARGV
15138 argv[argc++] = (char *)s;
15139#else
15140 if (li != l->lv_first)
15141 {
15142 s = vim_strsave_shellescape(s, FALSE, TRUE);
15143 if (s == NULL)
15144 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015145 ga_concat(&ga, s);
15146 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015147 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015148 else
15149 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015150 if (li->li_next != NULL)
15151 ga_append(&ga, ' ');
15152#endif
15153 }
15154#ifdef USE_ARGV
15155 argv[argc] = NULL;
15156#else
15157 cmd = ga.ga_data;
15158#endif
15159 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015160
Bram Moolenaar835dc632016-02-07 14:27:38 +010015161#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015162# ifdef FEAT_CHANNEL
15163 if (ch_log_active())
15164 {
15165 garray_T ga;
15166 int i;
15167
15168 ga_init2(&ga, (int)sizeof(char), 200);
15169 for (i = 0; i < argc; ++i)
15170 {
15171 if (i > 0)
15172 ga_concat(&ga, (char_u *)" ");
15173 ga_concat(&ga, (char_u *)argv[i]);
15174 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015175 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015176 ga_clear(&ga);
15177 }
15178# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015179 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015180#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015181# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015182 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015183# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015184 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015185#endif
15186
15187theend:
15188#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015189 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015190#else
15191 vim_free(ga.ga_data);
15192#endif
15193}
15194
15195/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015196 * Get the status of "job" and invoke the exit callback when needed.
15197 * The returned string is not allocated.
15198 */
15199 static char *
15200job_status(job_T *job)
15201{
15202 char *result;
15203
15204 if (job->jv_status == JOB_ENDED)
15205 /* No need to check, dead is dead. */
15206 result = "dead";
15207 else if (job->jv_status == JOB_FAILED)
15208 result = "fail";
15209 else
15210 {
15211 result = mch_job_status(job);
15212# ifdef FEAT_CHANNEL
15213 if (job->jv_status == JOB_ENDED)
15214 ch_log(job->jv_channel, "Job ended");
15215# endif
15216 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15217 {
15218 typval_T argv[3];
15219 typval_T rettv;
15220 int dummy;
15221
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015222 /* invoke the exit callback; make sure the refcount is > 0 */
15223 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015224 argv[0].v_type = VAR_JOB;
15225 argv[0].vval.v_job = job;
15226 argv[1].v_type = VAR_NUMBER;
15227 argv[1].vval.v_number = job->jv_exitval;
15228 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15229 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15230 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015231 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015232 }
15233 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15234 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015235 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015236 * freed. Careful: caller must not use "job" after this! */
15237 job_free(job);
15238 }
15239 }
15240 return result;
15241}
15242
15243/*
15244 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15245 */
15246 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015247job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015248{
15249 static time_t last_check = 0;
15250 time_t now;
15251 job_T *job;
15252 job_T *next;
15253
15254 /* Only do this once in 10 seconds. */
15255 now = time(NULL);
15256 if (last_check + 10 < now)
15257 {
15258 last_check = now;
15259 for (job = first_job; job != NULL; job = next)
15260 {
15261 next = job->jv_next;
15262 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15263 job_status(job); /* may free "job" */
15264 }
15265 }
15266}
15267
15268/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015269 * "job_status()" function
15270 */
15271 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015272f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015273{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015274 job_T *job = get_job_arg(&argvars[0]);
15275 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015276
Bram Moolenaar65edff82016-02-21 16:40:11 +010015277 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015278 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015279 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015280 rettv->v_type = VAR_STRING;
15281 rettv->vval.v_string = vim_strsave((char_u *)result);
15282 }
15283}
15284
15285/*
15286 * "job_stop()" function
15287 */
15288 static void
15289f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15290{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015291 job_T *job = get_job_arg(&argvars[0]);
15292
15293 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015294 {
15295 char_u *arg;
15296
15297 if (argvars[1].v_type == VAR_UNKNOWN)
15298 arg = (char_u *)"";
15299 else
15300 {
15301 arg = get_tv_string_chk(&argvars[1]);
15302 if (arg == NULL)
15303 {
15304 EMSG(_(e_invarg));
15305 return;
15306 }
15307 }
Bram Moolenaard6051b52016-02-28 15:49:03 +010015308# ifdef FEAT_CHANNEL
15309 ch_logs(job->jv_channel, "Stopping job with '%s'", (char *)arg);
15310# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +010015311 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015312 rettv->vval.v_number = 0;
15313 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015314 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015315 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015316 /* Assume that "hup" does not kill the job. */
15317 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15318 job->jv_channel->ch_job_killed = TRUE;
15319 }
15320 /* We don't try freeing the job, obviously the caller still has a
15321 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015322 }
15323}
15324#endif
15325
Bram Moolenaar071d4272004-06-13 20:20:40 +000015326/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015327 * "join()" function
15328 */
15329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015330f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015331{
15332 garray_T ga;
15333 char_u *sep;
15334
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015335 if (argvars[0].v_type != VAR_LIST)
15336 {
15337 EMSG(_(e_listreq));
15338 return;
15339 }
15340 if (argvars[0].vval.v_list == NULL)
15341 return;
15342 if (argvars[1].v_type == VAR_UNKNOWN)
15343 sep = (char_u *)" ";
15344 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015345 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015346
15347 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015348
15349 if (sep != NULL)
15350 {
15351 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015352 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015353 ga_append(&ga, NUL);
15354 rettv->vval.v_string = (char_u *)ga.ga_data;
15355 }
15356 else
15357 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015358}
15359
15360/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015361 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015362 */
15363 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015364f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015365{
15366 js_read_T reader;
15367
15368 reader.js_buf = get_tv_string(&argvars[0]);
15369 reader.js_fill = NULL;
15370 reader.js_used = 0;
15371 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15372 EMSG(_(e_invarg));
15373}
15374
15375/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015376 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015377 */
15378 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015379f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015380{
15381 rettv->v_type = VAR_STRING;
15382 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15383}
15384
15385/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015386 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015387 */
15388 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015389f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015390{
15391 js_read_T reader;
15392
15393 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015394 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015395 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015396 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015397 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015398}
15399
15400/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015401 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015402 */
15403 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015404f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015405{
15406 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015407 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015408}
15409
15410/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015411 * "keys()" function
15412 */
15413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015414f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015415{
15416 dict_list(argvars, rettv, 0);
15417}
15418
15419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015420 * "last_buffer_nr()" function.
15421 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015423f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015424{
15425 int n = 0;
15426 buf_T *buf;
15427
15428 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15429 if (n < buf->b_fnum)
15430 n = buf->b_fnum;
15431
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015432 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015433}
15434
15435/*
15436 * "len()" function
15437 */
15438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015439f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440{
15441 switch (argvars[0].v_type)
15442 {
15443 case VAR_STRING:
15444 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015445 rettv->vval.v_number = (varnumber_T)STRLEN(
15446 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015447 break;
15448 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015451 case VAR_DICT:
15452 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15453 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015454 case VAR_UNKNOWN:
15455 case VAR_SPECIAL:
15456 case VAR_FLOAT:
15457 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015458 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015459 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015460 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015461 break;
15462 }
15463}
15464
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015465static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015466
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015469{
15470#ifdef FEAT_LIBCALL
15471 char_u *string_in;
15472 char_u **string_result;
15473 int nr_result;
15474#endif
15475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015476 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015477 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015478 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015479
15480 if (check_restricted() || check_secure())
15481 return;
15482
15483#ifdef FEAT_LIBCALL
15484 /* The first two args must be strings, otherwise its meaningless */
15485 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015487 string_in = NULL;
15488 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015489 string_in = argvars[2].vval.v_string;
15490 if (type == VAR_NUMBER)
15491 string_result = NULL;
15492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015494 if (mch_libcall(argvars[0].vval.v_string,
15495 argvars[1].vval.v_string,
15496 string_in,
15497 argvars[2].vval.v_number,
15498 string_result,
15499 &nr_result) == OK
15500 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015501 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015502 }
15503#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504}
15505
15506/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015507 * "libcall()" function
15508 */
15509 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015510f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015511{
15512 libcall_common(argvars, rettv, VAR_STRING);
15513}
15514
15515/*
15516 * "libcallnr()" function
15517 */
15518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015519f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015520{
15521 libcall_common(argvars, rettv, VAR_NUMBER);
15522}
15523
15524/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 * "line(string)" function
15526 */
15527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015528f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529{
15530 linenr_T lnum = 0;
15531 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015532 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015533
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015534 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015535 if (fp != NULL)
15536 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015537 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015538}
15539
15540/*
15541 * "line2byte(lnum)" function
15542 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015544f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545{
15546#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548#else
15549 linenr_T lnum;
15550
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015551 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015552 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015553 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015555 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15556 if (rettv->vval.v_number >= 0)
15557 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558#endif
15559}
15560
15561/*
15562 * "lispindent(lnum)" function
15563 */
15564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015565f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566{
15567#ifdef FEAT_LISP
15568 pos_T pos;
15569 linenr_T lnum;
15570
15571 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015572 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015573 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15574 {
15575 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015576 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015577 curwin->w_cursor = pos;
15578 }
15579 else
15580#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582}
15583
15584/*
15585 * "localtime()" function
15586 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015588f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015590 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015591}
15592
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015593static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015594
15595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015596get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015597{
15598 char_u *keys;
15599 char_u *which;
15600 char_u buf[NUMBUFLEN];
15601 char_u *keys_buf = NULL;
15602 char_u *rhs;
15603 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015604 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015605 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015606 mapblock_T *mp;
15607 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015608
15609 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015610 rettv->v_type = VAR_STRING;
15611 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015612
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015613 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614 if (*keys == NUL)
15615 return;
15616
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015617 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015620 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015621 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015622 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015623 if (argvars[3].v_type != VAR_UNKNOWN)
15624 get_dict = get_tv_number(&argvars[3]);
15625 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015626 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015627 else
15628 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015629 if (which == NULL)
15630 return;
15631
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632 mode = get_map_mode(&which, 0);
15633
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015634 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015635 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015636 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015637
15638 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015639 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015640 /* Return a string. */
15641 if (rhs != NULL)
15642 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643
Bram Moolenaarbd743252010-10-20 21:23:33 +020015644 }
15645 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15646 {
15647 /* Return a dictionary. */
15648 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15649 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15650 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015651
Bram Moolenaarbd743252010-10-20 21:23:33 +020015652 dict_add_nr_str(dict, "lhs", 0L, lhs);
15653 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15654 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15655 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15656 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15657 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15658 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015659 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015660 dict_add_nr_str(dict, "mode", 0L, mapmode);
15661
15662 vim_free(lhs);
15663 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 }
15665}
15666
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015667#ifdef FEAT_FLOAT
15668/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015669 * "log()" function
15670 */
15671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015672f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015673{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015674 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015675
15676 rettv->v_type = VAR_FLOAT;
15677 if (get_float_arg(argvars, &f) == OK)
15678 rettv->vval.v_float = log(f);
15679 else
15680 rettv->vval.v_float = 0.0;
15681}
15682
15683/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015684 * "log10()" function
15685 */
15686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015687f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015688{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015689 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015690
15691 rettv->v_type = VAR_FLOAT;
15692 if (get_float_arg(argvars, &f) == OK)
15693 rettv->vval.v_float = log10(f);
15694 else
15695 rettv->vval.v_float = 0.0;
15696}
15697#endif
15698
Bram Moolenaar1dced572012-04-05 16:54:08 +020015699#ifdef FEAT_LUA
15700/*
15701 * "luaeval()" function
15702 */
15703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015704f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015705{
15706 char_u *str;
15707 char_u buf[NUMBUFLEN];
15708
15709 str = get_tv_string_buf(&argvars[0], buf);
15710 do_luaeval(str, argvars + 1, rettv);
15711}
15712#endif
15713
Bram Moolenaar071d4272004-06-13 20:20:40 +000015714/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015715 * "map()" function
15716 */
15717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015718f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015719{
15720 filter_map(argvars, rettv, TRUE);
15721}
15722
15723/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015724 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015725 */
15726 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015727f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015728{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015729 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015730}
15731
15732/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015733 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015734 */
15735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015736f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015737{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015738 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015739}
15740
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015741static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015742
15743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015744find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015746 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015747 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015748 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 char_u *pat;
15750 regmatch_T regmatch;
15751 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015752 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015753 char_u *save_cpo;
15754 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015755 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015756 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015757 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015758 list_T *l = NULL;
15759 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015760 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015761 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015762
15763 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15764 save_cpo = p_cpo;
15765 p_cpo = (char_u *)"";
15766
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015767 rettv->vval.v_number = -1;
15768 if (type == 3)
15769 {
15770 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015771 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015772 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015773 }
15774 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015776 rettv->v_type = VAR_STRING;
15777 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015779
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015780 if (argvars[0].v_type == VAR_LIST)
15781 {
15782 if ((l = argvars[0].vval.v_list) == NULL)
15783 goto theend;
15784 li = l->lv_first;
15785 }
15786 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015787 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015788 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015789 len = (long)STRLEN(str);
15790 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015791
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015792 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15793 if (pat == NULL)
15794 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015795
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015796 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015798 int error = FALSE;
15799
15800 start = get_tv_number_chk(&argvars[2], &error);
15801 if (error)
15802 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015803 if (l != NULL)
15804 {
15805 li = list_find(l, start);
15806 if (li == NULL)
15807 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015808 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015809 }
15810 else
15811 {
15812 if (start < 0)
15813 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015814 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015815 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015816 /* When "count" argument is there ignore matches before "start",
15817 * otherwise skip part of the string. Differs when pattern is "^"
15818 * or "\<". */
15819 if (argvars[3].v_type != VAR_UNKNOWN)
15820 startcol = start;
15821 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015822 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015823 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015824 len -= start;
15825 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015826 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015827
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015828 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015829 nth = get_tv_number_chk(&argvars[3], &error);
15830 if (error)
15831 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 }
15833
15834 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15835 if (regmatch.regprog != NULL)
15836 {
15837 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015838
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015839 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015840 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015841 if (l != NULL)
15842 {
15843 if (li == NULL)
15844 {
15845 match = FALSE;
15846 break;
15847 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015848 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015849 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015850 if (str == NULL)
15851 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015852 }
15853
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015854 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015855
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015856 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015857 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015858 if (l == NULL && !match)
15859 break;
15860
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015861 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015862 if (l != NULL)
15863 {
15864 li = li->li_next;
15865 ++idx;
15866 }
15867 else
15868 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015869#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015870 startcol = (colnr_T)(regmatch.startp[0]
15871 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015872#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015873 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015874#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015875 if (startcol > (colnr_T)len
15876 || str + startcol <= regmatch.startp[0])
15877 {
15878 match = FALSE;
15879 break;
15880 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015881 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015882 }
15883
15884 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015885 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015886 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015887 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015888 int i;
15889
15890 /* return list with matched string and submatches */
15891 for (i = 0; i < NSUBEXP; ++i)
15892 {
15893 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015894 {
15895 if (list_append_string(rettv->vval.v_list,
15896 (char_u *)"", 0) == FAIL)
15897 break;
15898 }
15899 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015900 regmatch.startp[i],
15901 (int)(regmatch.endp[i] - regmatch.startp[i]))
15902 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015903 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015904 }
15905 }
15906 else if (type == 2)
15907 {
15908 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015909 if (l != NULL)
15910 copy_tv(&li->li_tv, rettv);
15911 else
15912 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015913 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015914 }
15915 else if (l != NULL)
15916 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015917 else
15918 {
15919 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015920 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 (varnumber_T)(regmatch.startp[0] - str);
15922 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015923 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015924 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015925 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015926 }
15927 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015928 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015929 }
15930
15931theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015932 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 p_cpo = save_cpo;
15934}
15935
15936/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015937 * "match()" function
15938 */
15939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015940f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015941{
15942 find_some_match(argvars, rettv, 1);
15943}
15944
15945/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015946 * "matchadd()" function
15947 */
15948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015950{
15951#ifdef FEAT_SEARCH_EXTRA
15952 char_u buf[NUMBUFLEN];
15953 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15954 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15955 int prio = 10; /* default priority */
15956 int id = -1;
15957 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015958 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015959
15960 rettv->vval.v_number = -1;
15961
15962 if (grp == NULL || pat == NULL)
15963 return;
15964 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015965 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015966 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015967 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015968 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015969 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015970 if (argvars[4].v_type != VAR_UNKNOWN)
15971 {
15972 if (argvars[4].v_type != VAR_DICT)
15973 {
15974 EMSG(_(e_dictreq));
15975 return;
15976 }
15977 if (dict_find(argvars[4].vval.v_dict,
15978 (char_u *)"conceal", -1) != NULL)
15979 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15980 (char_u *)"conceal", FALSE);
15981 }
15982 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015983 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015984 if (error == TRUE)
15985 return;
15986 if (id >= 1 && id <= 3)
15987 {
15988 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15989 return;
15990 }
15991
Bram Moolenaar6561d522015-07-21 15:48:27 +020015992 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15993 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015994#endif
15995}
15996
15997/*
15998 * "matchaddpos()" function
15999 */
16000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016001f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020016002{
16003#ifdef FEAT_SEARCH_EXTRA
16004 char_u buf[NUMBUFLEN];
16005 char_u *group;
16006 int prio = 10;
16007 int id = -1;
16008 int error = FALSE;
16009 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020016010 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020016011
16012 rettv->vval.v_number = -1;
16013
16014 group = get_tv_string_buf_chk(&argvars[0], buf);
16015 if (group == NULL)
16016 return;
16017
16018 if (argvars[1].v_type != VAR_LIST)
16019 {
16020 EMSG2(_(e_listarg), "matchaddpos()");
16021 return;
16022 }
16023 l = argvars[1].vval.v_list;
16024 if (l == NULL)
16025 return;
16026
16027 if (argvars[2].v_type != VAR_UNKNOWN)
16028 {
16029 prio = get_tv_number_chk(&argvars[2], &error);
16030 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016031 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016032 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016033 if (argvars[4].v_type != VAR_UNKNOWN)
16034 {
16035 if (argvars[4].v_type != VAR_DICT)
16036 {
16037 EMSG(_(e_dictreq));
16038 return;
16039 }
16040 if (dict_find(argvars[4].vval.v_dict,
16041 (char_u *)"conceal", -1) != NULL)
16042 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16043 (char_u *)"conceal", FALSE);
16044 }
16045 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016046 }
16047 if (error == TRUE)
16048 return;
16049
16050 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16051 if (id == 1 || id == 2)
16052 {
16053 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16054 return;
16055 }
16056
Bram Moolenaar6561d522015-07-21 15:48:27 +020016057 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16058 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016059#endif
16060}
16061
16062/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016063 * "matcharg()" function
16064 */
16065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016066f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016067{
16068 if (rettv_list_alloc(rettv) == OK)
16069 {
16070#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016071 int id = get_tv_number(&argvars[0]);
16072 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016073
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016074 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016075 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016076 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16077 {
16078 list_append_string(rettv->vval.v_list,
16079 syn_id2name(m->hlg_id), -1);
16080 list_append_string(rettv->vval.v_list, m->pattern, -1);
16081 }
16082 else
16083 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016084 list_append_string(rettv->vval.v_list, NULL, -1);
16085 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016086 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016087 }
16088#endif
16089 }
16090}
16091
16092/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016093 * "matchdelete()" function
16094 */
16095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016096f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016097{
16098#ifdef FEAT_SEARCH_EXTRA
16099 rettv->vval.v_number = match_delete(curwin,
16100 (int)get_tv_number(&argvars[0]), TRUE);
16101#endif
16102}
16103
16104/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016105 * "matchend()" function
16106 */
16107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016108f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016109{
16110 find_some_match(argvars, rettv, 0);
16111}
16112
16113/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016114 * "matchlist()" function
16115 */
16116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016117f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016118{
16119 find_some_match(argvars, rettv, 3);
16120}
16121
16122/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016123 * "matchstr()" function
16124 */
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016127{
16128 find_some_match(argvars, rettv, 2);
16129}
16130
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016131static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016132
16133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016134max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016135{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016136 long n = 0;
16137 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016138 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016139
16140 if (argvars[0].v_type == VAR_LIST)
16141 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016142 list_T *l;
16143 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016144
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016145 l = argvars[0].vval.v_list;
16146 if (l != NULL)
16147 {
16148 li = l->lv_first;
16149 if (li != NULL)
16150 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016152 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016153 {
16154 li = li->li_next;
16155 if (li == NULL)
16156 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016157 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016158 if (domax ? i > n : i < n)
16159 n = i;
16160 }
16161 }
16162 }
16163 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016164 else if (argvars[0].v_type == VAR_DICT)
16165 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016166 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016167 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016168 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016169 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016170
16171 d = argvars[0].vval.v_dict;
16172 if (d != NULL)
16173 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016174 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016175 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016177 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016178 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016179 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016180 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016181 if (first)
16182 {
16183 n = i;
16184 first = FALSE;
16185 }
16186 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016187 n = i;
16188 }
16189 }
16190 }
16191 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016192 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016193 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016194 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016195}
16196
16197/*
16198 * "max()" function
16199 */
16200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016201f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016202{
16203 max_min(argvars, rettv, TRUE);
16204}
16205
16206/*
16207 * "min()" function
16208 */
16209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016210f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016211{
16212 max_min(argvars, rettv, FALSE);
16213}
16214
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016215static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016216
16217/*
16218 * Create the directory in which "dir" is located, and higher levels when
16219 * needed.
16220 */
16221 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016223{
16224 char_u *p;
16225 char_u *updir;
16226 int r = FAIL;
16227
16228 /* Get end of directory name in "dir".
16229 * We're done when it's "/" or "c:/". */
16230 p = gettail_sep(dir);
16231 if (p <= get_past_head(dir))
16232 return OK;
16233
16234 /* If the directory exists we're done. Otherwise: create it.*/
16235 updir = vim_strnsave(dir, (int)(p - dir));
16236 if (updir == NULL)
16237 return FAIL;
16238 if (mch_isdir(updir))
16239 r = OK;
16240 else if (mkdir_recurse(updir, prot) == OK)
16241 r = vim_mkdir_emsg(updir, prot);
16242 vim_free(updir);
16243 return r;
16244}
16245
16246#ifdef vim_mkdir
16247/*
16248 * "mkdir()" function
16249 */
16250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016251f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016252{
16253 char_u *dir;
16254 char_u buf[NUMBUFLEN];
16255 int prot = 0755;
16256
16257 rettv->vval.v_number = FAIL;
16258 if (check_restricted() || check_secure())
16259 return;
16260
16261 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016262 if (*dir == NUL)
16263 rettv->vval.v_number = FAIL;
16264 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016265 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016266 if (*gettail(dir) == NUL)
16267 /* remove trailing slashes */
16268 *gettail_sep(dir) = NUL;
16269
16270 if (argvars[1].v_type != VAR_UNKNOWN)
16271 {
16272 if (argvars[2].v_type != VAR_UNKNOWN)
16273 prot = get_tv_number_chk(&argvars[2], NULL);
16274 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16275 mkdir_recurse(dir, prot);
16276 }
16277 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016278 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016279}
16280#endif
16281
Bram Moolenaar0d660222005-01-07 21:51:51 +000016282/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016283 * "mode()" function
16284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016286f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016287{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016288 char_u buf[3];
16289
16290 buf[1] = NUL;
16291 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292
Bram Moolenaar071d4272004-06-13 20:20:40 +000016293 if (VIsual_active)
16294 {
16295 if (VIsual_select)
16296 buf[0] = VIsual_mode + 's' - 'v';
16297 else
16298 buf[0] = VIsual_mode;
16299 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016300 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016301 || State == CONFIRM)
16302 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016303 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016304 if (State == ASKMORE)
16305 buf[1] = 'm';
16306 else if (State == CONFIRM)
16307 buf[1] = '?';
16308 }
16309 else if (State == EXTERNCMD)
16310 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016311 else if (State & INSERT)
16312 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016313#ifdef FEAT_VREPLACE
16314 if (State & VREPLACE_FLAG)
16315 {
16316 buf[0] = 'R';
16317 buf[1] = 'v';
16318 }
16319 else
16320#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 if (State & REPLACE_FLAG)
16322 buf[0] = 'R';
16323 else
16324 buf[0] = 'i';
16325 }
16326 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016327 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016328 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016329 if (exmode_active)
16330 buf[1] = 'v';
16331 }
16332 else if (exmode_active)
16333 {
16334 buf[0] = 'c';
16335 buf[1] = 'e';
16336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016338 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016339 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016340 if (finish_op)
16341 buf[1] = 'o';
16342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016343
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016344 /* Clear out the minor mode when the argument is not a non-zero number or
16345 * non-empty string. */
16346 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016347 buf[1] = NUL;
16348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016349 rettv->vval.v_string = vim_strsave(buf);
16350 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016351}
16352
Bram Moolenaar429fa852013-04-15 12:27:36 +020016353#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016354/*
16355 * "mzeval()" function
16356 */
16357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016358f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016359{
16360 char_u *str;
16361 char_u buf[NUMBUFLEN];
16362
16363 str = get_tv_string_buf(&argvars[0], buf);
16364 do_mzeval(str, rettv);
16365}
Bram Moolenaar75676462013-01-30 14:55:42 +010016366
16367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016368mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016369{
16370 typval_T argvars[3];
16371
16372 argvars[0].v_type = VAR_STRING;
16373 argvars[0].vval.v_string = name;
16374 copy_tv(args, &argvars[1]);
16375 argvars[2].v_type = VAR_UNKNOWN;
16376 f_call(argvars, rettv);
16377 clear_tv(&argvars[1]);
16378}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016379#endif
16380
Bram Moolenaar071d4272004-06-13 20:20:40 +000016381/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016382 * "nextnonblank()" function
16383 */
16384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016385f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386{
16387 linenr_T lnum;
16388
16389 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16390 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016391 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016392 {
16393 lnum = 0;
16394 break;
16395 }
16396 if (*skipwhite(ml_get(lnum)) != NUL)
16397 break;
16398 }
16399 rettv->vval.v_number = lnum;
16400}
16401
16402/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016403 * "nr2char()" function
16404 */
16405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016406f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016407{
16408 char_u buf[NUMBUFLEN];
16409
16410#ifdef FEAT_MBYTE
16411 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016412 {
16413 int utf8 = 0;
16414
16415 if (argvars[1].v_type != VAR_UNKNOWN)
16416 utf8 = get_tv_number_chk(&argvars[1], NULL);
16417 if (utf8)
16418 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16419 else
16420 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16421 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016422 else
16423#endif
16424 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016425 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016426 buf[1] = NUL;
16427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016428 rettv->v_type = VAR_STRING;
16429 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016430}
16431
16432/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016433 * "or(expr, expr)" function
16434 */
16435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016436f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016437{
16438 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16439 | get_tv_number_chk(&argvars[1], NULL);
16440}
16441
16442/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016443 * "pathshorten()" function
16444 */
16445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016446f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016447{
16448 char_u *p;
16449
16450 rettv->v_type = VAR_STRING;
16451 p = get_tv_string_chk(&argvars[0]);
16452 if (p == NULL)
16453 rettv->vval.v_string = NULL;
16454 else
16455 {
16456 p = vim_strsave(p);
16457 rettv->vval.v_string = p;
16458 if (p != NULL)
16459 shorten_dir(p);
16460 }
16461}
16462
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016463#ifdef FEAT_PERL
16464/*
16465 * "perleval()" function
16466 */
16467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016468f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016469{
16470 char_u *str;
16471 char_u buf[NUMBUFLEN];
16472
16473 str = get_tv_string_buf(&argvars[0], buf);
16474 do_perleval(str, rettv);
16475}
16476#endif
16477
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016478#ifdef FEAT_FLOAT
16479/*
16480 * "pow()" function
16481 */
16482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016483f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016484{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016485 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016486
16487 rettv->v_type = VAR_FLOAT;
16488 if (get_float_arg(argvars, &fx) == OK
16489 && get_float_arg(&argvars[1], &fy) == OK)
16490 rettv->vval.v_float = pow(fx, fy);
16491 else
16492 rettv->vval.v_float = 0.0;
16493}
16494#endif
16495
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016497 * "prevnonblank()" function
16498 */
16499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016500f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016501{
16502 linenr_T lnum;
16503
16504 lnum = get_tv_lnum(argvars);
16505 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16506 lnum = 0;
16507 else
16508 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16509 --lnum;
16510 rettv->vval.v_number = lnum;
16511}
16512
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016513/* This dummy va_list is here because:
16514 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16515 * - locally in the function results in a "used before set" warning
16516 * - using va_start() to initialize it gives "function with fixed args" error */
16517static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016518
Bram Moolenaar8c711452005-01-14 21:53:12 +000016519/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016520 * "printf()" function
16521 */
16522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016524{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016525 char_u buf[NUMBUFLEN];
16526 int len;
16527 char_u *s;
16528 int saved_did_emsg = did_emsg;
16529 char *fmt;
16530
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016531 rettv->v_type = VAR_STRING;
16532 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016533
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016534 /* Get the required length, allocate the buffer and do it for real. */
16535 did_emsg = FALSE;
16536 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16537 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16538 if (!did_emsg)
16539 {
16540 s = alloc(len + 1);
16541 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016542 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016543 rettv->vval.v_string = s;
16544 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016545 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016546 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016547 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016548}
16549
16550/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016551 * "pumvisible()" function
16552 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016554f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016555{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016556#ifdef FEAT_INS_EXPAND
16557 if (pum_visible())
16558 rettv->vval.v_number = 1;
16559#endif
16560}
16561
Bram Moolenaardb913952012-06-29 12:54:53 +020016562#ifdef FEAT_PYTHON3
16563/*
16564 * "py3eval()" function
16565 */
16566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016568{
16569 char_u *str;
16570 char_u buf[NUMBUFLEN];
16571
16572 str = get_tv_string_buf(&argvars[0], buf);
16573 do_py3eval(str, rettv);
16574}
16575#endif
16576
16577#ifdef FEAT_PYTHON
16578/*
16579 * "pyeval()" function
16580 */
16581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016582f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016583{
16584 char_u *str;
16585 char_u buf[NUMBUFLEN];
16586
16587 str = get_tv_string_buf(&argvars[0], buf);
16588 do_pyeval(str, rettv);
16589}
16590#endif
16591
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016592/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016593 * "range()" function
16594 */
16595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016596f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016597{
16598 long start;
16599 long end;
16600 long stride = 1;
16601 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016604 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016605 if (argvars[1].v_type == VAR_UNKNOWN)
16606 {
16607 end = start - 1;
16608 start = 0;
16609 }
16610 else
16611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016612 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016614 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016615 }
16616
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 if (error)
16618 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016619 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016620 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016621 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016622 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016623 else
16624 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016625 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016626 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016627 if (list_append_number(rettv->vval.v_list,
16628 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016629 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016630 }
16631}
16632
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016633/*
16634 * "readfile()" function
16635 */
16636 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016637f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016638{
16639 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016640 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 char_u *fname;
16642 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16644 int io_size = sizeof(buf);
16645 int readlen; /* size of last fread() */
16646 char_u *prev = NULL; /* previously read bytes, if any */
16647 long prevlen = 0; /* length of data in prev */
16648 long prevsize = 0; /* size of prev buffer */
16649 long maxline = MAXLNUM;
16650 long cnt = 0;
16651 char_u *p; /* position in buf */
16652 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016653
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016654 if (argvars[1].v_type != VAR_UNKNOWN)
16655 {
16656 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16657 binary = TRUE;
16658 if (argvars[2].v_type != VAR_UNKNOWN)
16659 maxline = get_tv_number(&argvars[2]);
16660 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016661
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016662 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016663 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016664
16665 /* Always open the file in binary mode, library functions have a mind of
16666 * their own about CR-LF conversion. */
16667 fname = get_tv_string(&argvars[0]);
16668 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16669 {
16670 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16671 return;
16672 }
16673
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016674 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016675 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016676 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016677
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016678 /* This for loop processes what was read, but is also entered at end
16679 * of file so that either:
16680 * - an incomplete line gets written
16681 * - a "binary" file gets an empty line at the end if it ends in a
16682 * newline. */
16683 for (p = buf, start = buf;
16684 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16685 ++p)
16686 {
16687 if (*p == '\n' || readlen <= 0)
16688 {
16689 listitem_T *li;
16690 char_u *s = NULL;
16691 long_u len = p - start;
16692
16693 /* Finished a line. Remove CRs before NL. */
16694 if (readlen > 0 && !binary)
16695 {
16696 while (len > 0 && start[len - 1] == '\r')
16697 --len;
16698 /* removal may cross back to the "prev" string */
16699 if (len == 0)
16700 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16701 --prevlen;
16702 }
16703 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016704 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016705 else
16706 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016707 /* Change "prev" buffer to be the right size. This way
16708 * the bytes are only copied once, and very long lines are
16709 * allocated only once. */
16710 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016711 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016712 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016713 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016714 prev = NULL; /* the list will own the string */
16715 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016716 }
16717 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016718 if (s == NULL)
16719 {
16720 do_outofmem_msg((long_u) prevlen + len + 1);
16721 failed = TRUE;
16722 break;
16723 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016724
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016725 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016726 {
16727 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016728 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016729 break;
16730 }
16731 li->li_tv.v_type = VAR_STRING;
16732 li->li_tv.v_lock = 0;
16733 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016734 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016736 start = p + 1; /* step over newline */
16737 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738 break;
16739 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016740 else if (*p == NUL)
16741 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016742#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016743 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16744 * when finding the BF and check the previous two bytes. */
16745 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016746 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016747 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16748 * + 1, these may be in the "prev" string. */
16749 char_u back1 = p >= buf + 1 ? p[-1]
16750 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16751 char_u back2 = p >= buf + 2 ? p[-2]
16752 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16753 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016754
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016755 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016756 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016757 char_u *dest = p - 2;
16758
16759 /* Usually a BOM is at the beginning of a file, and so at
16760 * the beginning of a line; then we can just step over it.
16761 */
16762 if (start == dest)
16763 start = p + 1;
16764 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016765 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016766 /* have to shuffle buf to close gap */
16767 int adjust_prevlen = 0;
16768
16769 if (dest < buf)
16770 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016771 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 dest = buf;
16773 }
16774 if (readlen > p - buf + 1)
16775 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16776 readlen -= 3 - adjust_prevlen;
16777 prevlen -= adjust_prevlen;
16778 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016779 }
16780 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016781 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016782#endif
16783 } /* for */
16784
16785 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16786 break;
16787 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016788 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016789 /* There's part of a line in buf, store it in "prev". */
16790 if (p - start + prevlen >= prevsize)
16791 {
16792 /* need bigger "prev" buffer */
16793 char_u *newprev;
16794
16795 /* A common use case is ordinary text files and "prev" gets a
16796 * fragment of a line, so the first allocation is made
16797 * small, to avoid repeatedly 'allocing' large and
16798 * 'reallocing' small. */
16799 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016800 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016801 else
16802 {
16803 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016804 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016805 prevsize = grow50pc > growmin ? grow50pc : growmin;
16806 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016807 newprev = prev == NULL ? alloc(prevsize)
16808 : vim_realloc(prev, prevsize);
16809 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016810 {
16811 do_outofmem_msg((long_u)prevsize);
16812 failed = TRUE;
16813 break;
16814 }
16815 prev = newprev;
16816 }
16817 /* Add the line part to end of "prev". */
16818 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016819 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016820 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016821 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016822
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016823 /*
16824 * For a negative line count use only the lines at the end of the file,
16825 * free the rest.
16826 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016827 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016828 while (cnt > -maxline)
16829 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016830 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016831 --cnt;
16832 }
16833
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016834 if (failed)
16835 {
16836 list_free(rettv->vval.v_list, TRUE);
16837 /* readfile doc says an empty list is returned on error */
16838 rettv->vval.v_list = list_alloc();
16839 }
16840
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016841 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016842 fclose(fd);
16843}
16844
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016845#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016846static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016847
16848/*
16849 * Convert a List to proftime_T.
16850 * Return FAIL when there is something wrong.
16851 */
16852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016853list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016854{
16855 long n1, n2;
16856 int error = FALSE;
16857
16858 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16859 || arg->vval.v_list->lv_len != 2)
16860 return FAIL;
16861 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16862 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16863# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016864 tm->HighPart = n1;
16865 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016866# else
16867 tm->tv_sec = n1;
16868 tm->tv_usec = n2;
16869# endif
16870 return error ? FAIL : OK;
16871}
16872#endif /* FEAT_RELTIME */
16873
16874/*
16875 * "reltime()" function
16876 */
16877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016878f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016879{
16880#ifdef FEAT_RELTIME
16881 proftime_T res;
16882 proftime_T start;
16883
16884 if (argvars[0].v_type == VAR_UNKNOWN)
16885 {
16886 /* No arguments: get current time. */
16887 profile_start(&res);
16888 }
16889 else if (argvars[1].v_type == VAR_UNKNOWN)
16890 {
16891 if (list2proftime(&argvars[0], &res) == FAIL)
16892 return;
16893 profile_end(&res);
16894 }
16895 else
16896 {
16897 /* Two arguments: compute the difference. */
16898 if (list2proftime(&argvars[0], &start) == FAIL
16899 || list2proftime(&argvars[1], &res) == FAIL)
16900 return;
16901 profile_sub(&res, &start);
16902 }
16903
16904 if (rettv_list_alloc(rettv) == OK)
16905 {
16906 long n1, n2;
16907
16908# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016909 n1 = res.HighPart;
16910 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016911# else
16912 n1 = res.tv_sec;
16913 n2 = res.tv_usec;
16914# endif
16915 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16916 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16917 }
16918#endif
16919}
16920
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016921#ifdef FEAT_FLOAT
16922/*
16923 * "reltimefloat()" function
16924 */
16925 static void
16926f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16927{
16928# ifdef FEAT_RELTIME
16929 proftime_T tm;
16930# endif
16931
16932 rettv->v_type = VAR_FLOAT;
16933 rettv->vval.v_float = 0;
16934# ifdef FEAT_RELTIME
16935 if (list2proftime(&argvars[0], &tm) == OK)
16936 rettv->vval.v_float = profile_float(&tm);
16937# endif
16938}
16939#endif
16940
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016941/*
16942 * "reltimestr()" function
16943 */
16944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016945f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016946{
16947#ifdef FEAT_RELTIME
16948 proftime_T tm;
16949#endif
16950
16951 rettv->v_type = VAR_STRING;
16952 rettv->vval.v_string = NULL;
16953#ifdef FEAT_RELTIME
16954 if (list2proftime(&argvars[0], &tm) == OK)
16955 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16956#endif
16957}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016958
Bram Moolenaar0d660222005-01-07 21:51:51 +000016959#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016960static void make_connection(void);
16961static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016962
16963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016964make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016965{
16966 if (X_DISPLAY == NULL
16967# ifdef FEAT_GUI
16968 && !gui.in_use
16969# endif
16970 )
16971 {
16972 x_force_connect = TRUE;
16973 setup_term_clip();
16974 x_force_connect = FALSE;
16975 }
16976}
16977
16978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016979check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016980{
16981 make_connection();
16982 if (X_DISPLAY == NULL)
16983 {
16984 EMSG(_("E240: No connection to Vim server"));
16985 return FAIL;
16986 }
16987 return OK;
16988}
16989#endif
16990
16991#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016992static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016993
16994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016995remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996{
16997 char_u *server_name;
16998 char_u *keys;
16999 char_u *r = NULL;
17000 char_u buf[NUMBUFLEN];
17001# ifdef WIN32
17002 HWND w;
17003# else
17004 Window w;
17005# endif
17006
17007 if (check_restricted() || check_secure())
17008 return;
17009
17010# ifdef FEAT_X11
17011 if (check_connection() == FAIL)
17012 return;
17013# endif
17014
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 server_name = get_tv_string_chk(&argvars[0]);
17016 if (server_name == NULL)
17017 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017018 keys = get_tv_string_buf(&argvars[1], buf);
17019# ifdef WIN32
17020 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
17021# else
17022 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
17023 < 0)
17024# endif
17025 {
17026 if (r != NULL)
17027 EMSG(r); /* sending worked but evaluation failed */
17028 else
17029 EMSG2(_("E241: Unable to send to %s"), server_name);
17030 return;
17031 }
17032
17033 rettv->vval.v_string = r;
17034
17035 if (argvars[2].v_type != VAR_UNKNOWN)
17036 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017037 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017038 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017039 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017040
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017041 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017042 v.di_tv.v_type = VAR_STRING;
17043 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017044 idvar = get_tv_string_chk(&argvars[2]);
17045 if (idvar != NULL)
17046 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017047 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017048 }
17049}
17050#endif
17051
17052/*
17053 * "remote_expr()" function
17054 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017056f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057{
17058 rettv->v_type = VAR_STRING;
17059 rettv->vval.v_string = NULL;
17060#ifdef FEAT_CLIENTSERVER
17061 remote_common(argvars, rettv, TRUE);
17062#endif
17063}
17064
17065/*
17066 * "remote_foreground()" function
17067 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017069f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017071#ifdef FEAT_CLIENTSERVER
17072# ifdef WIN32
17073 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017074 {
17075 char_u *server_name = get_tv_string_chk(&argvars[0]);
17076
17077 if (server_name != NULL)
17078 serverForeground(server_name);
17079 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017080# else
17081 /* Send a foreground() expression to the server. */
17082 argvars[1].v_type = VAR_STRING;
17083 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17084 argvars[2].v_type = VAR_UNKNOWN;
17085 remote_common(argvars, rettv, TRUE);
17086 vim_free(argvars[1].vval.v_string);
17087# endif
17088#endif
17089}
17090
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017092f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093{
17094#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017095 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 char_u *s = NULL;
17097# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017098 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017100 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017101
17102 if (check_restricted() || check_secure())
17103 {
17104 rettv->vval.v_number = -1;
17105 return;
17106 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017107 serverid = get_tv_string_chk(&argvars[0]);
17108 if (serverid == NULL)
17109 {
17110 rettv->vval.v_number = -1;
17111 return; /* type error; errmsg already given */
17112 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017113# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017114 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017115 if (n == 0)
17116 rettv->vval.v_number = -1;
17117 else
17118 {
17119 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17120 rettv->vval.v_number = (s != NULL);
17121 }
17122# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123 if (check_connection() == FAIL)
17124 return;
17125
17126 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017127 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017128# endif
17129
17130 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 char_u *retvar;
17133
Bram Moolenaar33570922005-01-25 22:26:29 +000017134 v.di_tv.v_type = VAR_STRING;
17135 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017136 retvar = get_tv_string_chk(&argvars[1]);
17137 if (retvar != NULL)
17138 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017139 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017140 }
17141#else
17142 rettv->vval.v_number = -1;
17143#endif
17144}
17145
Bram Moolenaar0d660222005-01-07 21:51:51 +000017146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017147f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017148{
17149 char_u *r = NULL;
17150
17151#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017152 char_u *serverid = get_tv_string_chk(&argvars[0]);
17153
17154 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017155 {
17156# ifdef WIN32
17157 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017158 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017159
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017160 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 if (n != 0)
17162 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17163 if (r == NULL)
17164# else
17165 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017167# endif
17168 EMSG(_("E277: Unable to read a server reply"));
17169 }
17170#endif
17171 rettv->v_type = VAR_STRING;
17172 rettv->vval.v_string = r;
17173}
17174
17175/*
17176 * "remote_send()" function
17177 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017179f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017180{
17181 rettv->v_type = VAR_STRING;
17182 rettv->vval.v_string = NULL;
17183#ifdef FEAT_CLIENTSERVER
17184 remote_common(argvars, rettv, FALSE);
17185#endif
17186}
17187
17188/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017189 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017190 */
17191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017192f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017193{
Bram Moolenaar33570922005-01-25 22:26:29 +000017194 list_T *l;
17195 listitem_T *item, *item2;
17196 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017197 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017198 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017199 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017200 dict_T *d;
17201 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017202 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017203
Bram Moolenaar8c711452005-01-14 21:53:12 +000017204 if (argvars[0].v_type == VAR_DICT)
17205 {
17206 if (argvars[2].v_type != VAR_UNKNOWN)
17207 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017208 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017209 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017210 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017211 key = get_tv_string_chk(&argvars[1]);
17212 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017213 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017214 di = dict_find(d, key, -1);
17215 if (di == NULL)
17216 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017217 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17218 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017219 {
17220 *rettv = di->di_tv;
17221 init_tv(&di->di_tv);
17222 dictitem_remove(d, di);
17223 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017224 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017225 }
17226 }
17227 else if (argvars[0].v_type != VAR_LIST)
17228 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017229 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017230 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017231 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017232 int error = FALSE;
17233
17234 idx = get_tv_number_chk(&argvars[1], &error);
17235 if (error)
17236 ; /* type error: do nothing, errmsg already given */
17237 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017238 EMSGN(_(e_listidx), idx);
17239 else
17240 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017241 if (argvars[2].v_type == VAR_UNKNOWN)
17242 {
17243 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017244 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017245 *rettv = item->li_tv;
17246 vim_free(item);
17247 }
17248 else
17249 {
17250 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017251 end = get_tv_number_chk(&argvars[2], &error);
17252 if (error)
17253 ; /* type error: do nothing */
17254 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017255 EMSGN(_(e_listidx), end);
17256 else
17257 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017258 int cnt = 0;
17259
17260 for (li = item; li != NULL; li = li->li_next)
17261 {
17262 ++cnt;
17263 if (li == item2)
17264 break;
17265 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017266 if (li == NULL) /* didn't find "item2" after "item" */
17267 EMSG(_(e_invrange));
17268 else
17269 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017270 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017271 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017272 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017273 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017274 l->lv_first = item;
17275 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017276 item->li_prev = NULL;
17277 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017278 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017279 }
17280 }
17281 }
17282 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017283 }
17284 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017285}
17286
17287/*
17288 * "rename({from}, {to})" function
17289 */
17290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017291f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017292{
17293 char_u buf[NUMBUFLEN];
17294
17295 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017296 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017297 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017298 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17299 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300}
17301
17302/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017303 * "repeat()" function
17304 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017306f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017307{
17308 char_u *p;
17309 int n;
17310 int slen;
17311 int len;
17312 char_u *r;
17313 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017314
17315 n = get_tv_number(&argvars[1]);
17316 if (argvars[0].v_type == VAR_LIST)
17317 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017318 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017319 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017320 if (list_extend(rettv->vval.v_list,
17321 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017322 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017323 }
17324 else
17325 {
17326 p = get_tv_string(&argvars[0]);
17327 rettv->v_type = VAR_STRING;
17328 rettv->vval.v_string = NULL;
17329
17330 slen = (int)STRLEN(p);
17331 len = slen * n;
17332 if (len <= 0)
17333 return;
17334
17335 r = alloc(len + 1);
17336 if (r != NULL)
17337 {
17338 for (i = 0; i < n; i++)
17339 mch_memmove(r + i * slen, p, (size_t)slen);
17340 r[len] = NUL;
17341 }
17342
17343 rettv->vval.v_string = r;
17344 }
17345}
17346
17347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017348 * "resolve()" function
17349 */
17350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017351f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017352{
17353 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017354#ifdef HAVE_READLINK
17355 char_u *buf = NULL;
17356#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017358 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359#ifdef FEAT_SHORTCUT
17360 {
17361 char_u *v = NULL;
17362
17363 v = mch_resolve_shortcut(p);
17364 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017365 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017367 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017368 }
17369#else
17370# ifdef HAVE_READLINK
17371 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 char_u *cpy;
17373 int len;
17374 char_u *remain = NULL;
17375 char_u *q;
17376 int is_relative_to_current = FALSE;
17377 int has_trailing_pathsep = FALSE;
17378 int limit = 100;
17379
17380 p = vim_strsave(p);
17381
17382 if (p[0] == '.' && (vim_ispathsep(p[1])
17383 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17384 is_relative_to_current = TRUE;
17385
17386 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017387 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017388 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017390 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392
17393 q = getnextcomp(p);
17394 if (*q != NUL)
17395 {
17396 /* Separate the first path component in "p", and keep the
17397 * remainder (beginning with the path separator). */
17398 remain = vim_strsave(q - 1);
17399 q[-1] = NUL;
17400 }
17401
Bram Moolenaard9462e32011-04-11 21:35:11 +020017402 buf = alloc(MAXPATHL + 1);
17403 if (buf == NULL)
17404 goto fail;
17405
Bram Moolenaar071d4272004-06-13 20:20:40 +000017406 for (;;)
17407 {
17408 for (;;)
17409 {
17410 len = readlink((char *)p, (char *)buf, MAXPATHL);
17411 if (len <= 0)
17412 break;
17413 buf[len] = NUL;
17414
17415 if (limit-- == 0)
17416 {
17417 vim_free(p);
17418 vim_free(remain);
17419 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017420 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 goto fail;
17422 }
17423
17424 /* Ensure that the result will have a trailing path separator
17425 * if the argument has one. */
17426 if (remain == NULL && has_trailing_pathsep)
17427 add_pathsep(buf);
17428
17429 /* Separate the first path component in the link value and
17430 * concatenate the remainders. */
17431 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17432 if (*q != NUL)
17433 {
17434 if (remain == NULL)
17435 remain = vim_strsave(q - 1);
17436 else
17437 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017438 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017439 if (cpy != NULL)
17440 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 vim_free(remain);
17442 remain = cpy;
17443 }
17444 }
17445 q[-1] = NUL;
17446 }
17447
17448 q = gettail(p);
17449 if (q > p && *q == NUL)
17450 {
17451 /* Ignore trailing path separator. */
17452 q[-1] = NUL;
17453 q = gettail(p);
17454 }
17455 if (q > p && !mch_isFullName(buf))
17456 {
17457 /* symlink is relative to directory of argument */
17458 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17459 if (cpy != NULL)
17460 {
17461 STRCPY(cpy, p);
17462 STRCPY(gettail(cpy), buf);
17463 vim_free(p);
17464 p = cpy;
17465 }
17466 }
17467 else
17468 {
17469 vim_free(p);
17470 p = vim_strsave(buf);
17471 }
17472 }
17473
17474 if (remain == NULL)
17475 break;
17476
17477 /* Append the first path component of "remain" to "p". */
17478 q = getnextcomp(remain + 1);
17479 len = q - remain - (*q != NUL);
17480 cpy = vim_strnsave(p, STRLEN(p) + len);
17481 if (cpy != NULL)
17482 {
17483 STRNCAT(cpy, remain, len);
17484 vim_free(p);
17485 p = cpy;
17486 }
17487 /* Shorten "remain". */
17488 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017489 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017490 else
17491 {
17492 vim_free(remain);
17493 remain = NULL;
17494 }
17495 }
17496
17497 /* If the result is a relative path name, make it explicitly relative to
17498 * the current directory if and only if the argument had this form. */
17499 if (!vim_ispathsep(*p))
17500 {
17501 if (is_relative_to_current
17502 && *p != NUL
17503 && !(p[0] == '.'
17504 && (p[1] == NUL
17505 || vim_ispathsep(p[1])
17506 || (p[1] == '.'
17507 && (p[2] == NUL
17508 || vim_ispathsep(p[2]))))))
17509 {
17510 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017511 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017512 if (cpy != NULL)
17513 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 vim_free(p);
17515 p = cpy;
17516 }
17517 }
17518 else if (!is_relative_to_current)
17519 {
17520 /* Strip leading "./". */
17521 q = p;
17522 while (q[0] == '.' && vim_ispathsep(q[1]))
17523 q += 2;
17524 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017525 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017526 }
17527 }
17528
17529 /* Ensure that the result will have no trailing path separator
17530 * if the argument had none. But keep "/" or "//". */
17531 if (!has_trailing_pathsep)
17532 {
17533 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017534 if (after_pathsep(p, q))
17535 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017536 }
17537
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017538 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 }
17540# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017541 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017542# endif
17543#endif
17544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017545 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017546
17547#ifdef HAVE_READLINK
17548fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017549 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017550#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017551 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017552}
17553
17554/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017555 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556 */
17557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017558f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559{
Bram Moolenaar33570922005-01-25 22:26:29 +000017560 list_T *l;
17561 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017562
Bram Moolenaar0d660222005-01-07 21:51:51 +000017563 if (argvars[0].v_type != VAR_LIST)
17564 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017565 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017566 && !tv_check_lock(l->lv_lock,
17567 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017568 {
17569 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017570 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017571 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017572 while (li != NULL)
17573 {
17574 ni = li->li_prev;
17575 list_append(l, li);
17576 li = ni;
17577 }
17578 rettv->vval.v_list = l;
17579 rettv->v_type = VAR_LIST;
17580 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017581 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583}
17584
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017585#define SP_NOMOVE 0x01 /* don't move cursor */
17586#define SP_REPEAT 0x02 /* repeat to find outer pair */
17587#define SP_RETCOUNT 0x04 /* return matchcount */
17588#define SP_SETPCMARK 0x08 /* set previous context mark */
17589#define SP_START 0x10 /* accept match at start position */
17590#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17591#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017592#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017593
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017594static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017595
17596/*
17597 * Get flags for a search function.
17598 * Possibly sets "p_ws".
17599 * Returns BACKWARD, FORWARD or zero (for an error).
17600 */
17601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017602get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603{
17604 int dir = FORWARD;
17605 char_u *flags;
17606 char_u nbuf[NUMBUFLEN];
17607 int mask;
17608
17609 if (varp->v_type != VAR_UNKNOWN)
17610 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017611 flags = get_tv_string_buf_chk(varp, nbuf);
17612 if (flags == NULL)
17613 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614 while (*flags != NUL)
17615 {
17616 switch (*flags)
17617 {
17618 case 'b': dir = BACKWARD; break;
17619 case 'w': p_ws = TRUE; break;
17620 case 'W': p_ws = FALSE; break;
17621 default: mask = 0;
17622 if (flagsp != NULL)
17623 switch (*flags)
17624 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017625 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017626 case 'e': mask = SP_END; break;
17627 case 'm': mask = SP_RETCOUNT; break;
17628 case 'n': mask = SP_NOMOVE; break;
17629 case 'p': mask = SP_SUBPAT; break;
17630 case 'r': mask = SP_REPEAT; break;
17631 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017632 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017633 }
17634 if (mask == 0)
17635 {
17636 EMSG2(_(e_invarg2), flags);
17637 dir = 0;
17638 }
17639 else
17640 *flagsp |= mask;
17641 }
17642 if (dir == 0)
17643 break;
17644 ++flags;
17645 }
17646 }
17647 return dir;
17648}
17649
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017651 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017652 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017654search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017656 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 char_u *pat;
17658 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017659 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 int save_p_ws = p_ws;
17661 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017662 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017663 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017664 proftime_T tm;
17665#ifdef FEAT_RELTIME
17666 long time_limit = 0;
17667#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017668 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017669 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017670
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017671 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017672 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017673 if (dir == 0)
17674 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017675 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017676 if (flags & SP_START)
17677 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017678 if (flags & SP_END)
17679 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017680 if (flags & SP_COLUMN)
17681 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017682
Bram Moolenaar76929292008-01-06 19:07:36 +000017683 /* Optional arguments: line number to stop searching and timeout. */
17684 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017685 {
17686 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17687 if (lnum_stop < 0)
17688 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017689#ifdef FEAT_RELTIME
17690 if (argvars[3].v_type != VAR_UNKNOWN)
17691 {
17692 time_limit = get_tv_number_chk(&argvars[3], NULL);
17693 if (time_limit < 0)
17694 goto theend;
17695 }
17696#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017697 }
17698
Bram Moolenaar76929292008-01-06 19:07:36 +000017699#ifdef FEAT_RELTIME
17700 /* Set the time limit, if there is one. */
17701 profile_setlimit(time_limit, &tm);
17702#endif
17703
Bram Moolenaar231334e2005-07-25 20:46:57 +000017704 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017705 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017706 * Check to make sure only those flags are set.
17707 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17708 * flags cannot be set. Check for that condition also.
17709 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017710 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017711 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017712 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017713 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017714 goto theend;
17715 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017717 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017718 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017719 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017720 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017722 if (flags & SP_SUBPAT)
17723 retval = subpatnum;
17724 else
17725 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017726 if (flags & SP_SETPCMARK)
17727 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017729 if (match_pos != NULL)
17730 {
17731 /* Store the match cursor position */
17732 match_pos->lnum = pos.lnum;
17733 match_pos->col = pos.col + 1;
17734 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017735 /* "/$" will put the cursor after the end of the line, may need to
17736 * correct that here */
17737 check_cursor();
17738 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017739
17740 /* If 'n' flag is used: restore cursor position. */
17741 if (flags & SP_NOMOVE)
17742 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017743 else
17744 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017745theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017747
17748 return retval;
17749}
17750
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017751#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017752
17753/*
17754 * round() is not in C90, use ceil() or floor() instead.
17755 */
17756 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017757vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017758{
17759 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17760}
17761
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017762/*
17763 * "round({float})" function
17764 */
17765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017766f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017767{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017768 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017769
17770 rettv->v_type = VAR_FLOAT;
17771 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017772 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017773 else
17774 rettv->vval.v_float = 0.0;
17775}
17776#endif
17777
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017778/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017779 * "screenattr()" function
17780 */
17781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017782f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017783{
17784 int row;
17785 int col;
17786 int c;
17787
17788 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17789 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17790 if (row < 0 || row >= screen_Rows
17791 || col < 0 || col >= screen_Columns)
17792 c = -1;
17793 else
17794 c = ScreenAttrs[LineOffset[row] + col];
17795 rettv->vval.v_number = c;
17796}
17797
17798/*
17799 * "screenchar()" function
17800 */
17801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017803{
17804 int row;
17805 int col;
17806 int off;
17807 int c;
17808
17809 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17810 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17811 if (row < 0 || row >= screen_Rows
17812 || col < 0 || col >= screen_Columns)
17813 c = -1;
17814 else
17815 {
17816 off = LineOffset[row] + col;
17817#ifdef FEAT_MBYTE
17818 if (enc_utf8 && ScreenLinesUC[off] != 0)
17819 c = ScreenLinesUC[off];
17820 else
17821#endif
17822 c = ScreenLines[off];
17823 }
17824 rettv->vval.v_number = c;
17825}
17826
17827/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017828 * "screencol()" function
17829 *
17830 * First column is 1 to be consistent with virtcol().
17831 */
17832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017833f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017834{
17835 rettv->vval.v_number = screen_screencol() + 1;
17836}
17837
17838/*
17839 * "screenrow()" function
17840 */
17841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017842f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017843{
17844 rettv->vval.v_number = screen_screenrow() + 1;
17845}
17846
17847/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017848 * "search()" function
17849 */
17850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017851f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017852{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017853 int flags = 0;
17854
17855 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856}
17857
Bram Moolenaar071d4272004-06-13 20:20:40 +000017858/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017859 * "searchdecl()" function
17860 */
17861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017862f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017863{
17864 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017865 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017866 int error = FALSE;
17867 char_u *name;
17868
17869 rettv->vval.v_number = 1; /* default: FAIL */
17870
17871 name = get_tv_string_chk(&argvars[0]);
17872 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017873 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017874 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017875 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17876 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17877 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017878 if (!error && name != NULL)
17879 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017880 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017881}
17882
17883/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017884 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017885 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017886 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017887searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017888{
17889 char_u *spat, *mpat, *epat;
17890 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017891 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017892 int dir;
17893 int flags = 0;
17894 char_u nbuf1[NUMBUFLEN];
17895 char_u nbuf2[NUMBUFLEN];
17896 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017897 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017898 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017899 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017900
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017902 spat = get_tv_string_chk(&argvars[0]);
17903 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17904 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17905 if (spat == NULL || mpat == NULL || epat == NULL)
17906 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017907
Bram Moolenaar071d4272004-06-13 20:20:40 +000017908 /* Handle the optional fourth argument: flags */
17909 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017910 if (dir == 0)
17911 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017912
17913 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017914 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17915 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017916 if ((flags & (SP_END | SP_SUBPAT)) != 0
17917 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017918 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017919 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017920 goto theend;
17921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017923 /* Using 'r' implies 'W', otherwise it doesn't work. */
17924 if (flags & SP_REPEAT)
17925 p_ws = FALSE;
17926
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017927 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017928 if (argvars[3].v_type == VAR_UNKNOWN
17929 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017930 skip = (char_u *)"";
17931 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017932 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017934 if (argvars[5].v_type != VAR_UNKNOWN)
17935 {
17936 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17937 if (lnum_stop < 0)
17938 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017939#ifdef FEAT_RELTIME
17940 if (argvars[6].v_type != VAR_UNKNOWN)
17941 {
17942 time_limit = get_tv_number_chk(&argvars[6], NULL);
17943 if (time_limit < 0)
17944 goto theend;
17945 }
17946#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017947 }
17948 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017949 if (skip == NULL)
17950 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017951
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017952 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017953 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017954
17955theend:
17956 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017957
17958 return retval;
17959}
17960
17961/*
17962 * "searchpair()" function
17963 */
17964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017965f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017966{
17967 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17968}
17969
17970/*
17971 * "searchpairpos()" function
17972 */
17973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017974f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017975{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017976 pos_T match_pos;
17977 int lnum = 0;
17978 int col = 0;
17979
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017980 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017981 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017982
17983 if (searchpair_cmn(argvars, &match_pos) > 0)
17984 {
17985 lnum = match_pos.lnum;
17986 col = match_pos.col;
17987 }
17988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017989 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17990 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017991}
17992
17993/*
17994 * Search for a start/middle/end thing.
17995 * Used by searchpair(), see its documentation for the details.
17996 * Returns 0 or -1 for no match,
17997 */
17998 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017999do_searchpair(
18000 char_u *spat, /* start pattern */
18001 char_u *mpat, /* middle pattern */
18002 char_u *epat, /* end pattern */
18003 int dir, /* BACKWARD or FORWARD */
18004 char_u *skip, /* skip expression */
18005 int flags, /* SP_SETPCMARK and other SP_ values */
18006 pos_T *match_pos,
18007 linenr_T lnum_stop, /* stop at this line if not zero */
18008 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018009{
18010 char_u *save_cpo;
18011 char_u *pat, *pat2 = NULL, *pat3 = NULL;
18012 long retval = 0;
18013 pos_T pos;
18014 pos_T firstpos;
18015 pos_T foundpos;
18016 pos_T save_cursor;
18017 pos_T save_pos;
18018 int n;
18019 int r;
18020 int nest = 1;
18021 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018022 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000018023 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018024
18025 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18026 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018027 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018028
Bram Moolenaar76929292008-01-06 19:07:36 +000018029#ifdef FEAT_RELTIME
18030 /* Set the time limit, if there is one. */
18031 profile_setlimit(time_limit, &tm);
18032#endif
18033
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018034 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18035 * start/middle/end (pat3, for the top pair). */
18036 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18037 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18038 if (pat2 == NULL || pat3 == NULL)
18039 goto theend;
18040 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18041 if (*mpat == NUL)
18042 STRCPY(pat3, pat2);
18043 else
18044 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18045 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018046 if (flags & SP_START)
18047 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018048
Bram Moolenaar071d4272004-06-13 20:20:40 +000018049 save_cursor = curwin->w_cursor;
18050 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018051 clearpos(&firstpos);
18052 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 pat = pat3;
18054 for (;;)
18055 {
18056 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018057 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018058 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18059 /* didn't find it or found the first match again: FAIL */
18060 break;
18061
18062 if (firstpos.lnum == 0)
18063 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018064 if (equalpos(pos, foundpos))
18065 {
18066 /* Found the same position again. Can happen with a pattern that
18067 * has "\zs" at the end and searching backwards. Advance one
18068 * character and try again. */
18069 if (dir == BACKWARD)
18070 decl(&pos);
18071 else
18072 incl(&pos);
18073 }
18074 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018075
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018076 /* clear the start flag to avoid getting stuck here */
18077 options &= ~SEARCH_START;
18078
Bram Moolenaar071d4272004-06-13 20:20:40 +000018079 /* If the skip pattern matches, ignore this match. */
18080 if (*skip != NUL)
18081 {
18082 save_pos = curwin->w_cursor;
18083 curwin->w_cursor = pos;
18084 r = eval_to_bool(skip, &err, NULL, FALSE);
18085 curwin->w_cursor = save_pos;
18086 if (err)
18087 {
18088 /* Evaluating {skip} caused an error, break here. */
18089 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018090 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018091 break;
18092 }
18093 if (r)
18094 continue;
18095 }
18096
18097 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18098 {
18099 /* Found end when searching backwards or start when searching
18100 * forward: nested pair. */
18101 ++nest;
18102 pat = pat2; /* nested, don't search for middle */
18103 }
18104 else
18105 {
18106 /* Found end when searching forward or start when searching
18107 * backward: end of (nested) pair; or found middle in outer pair. */
18108 if (--nest == 1)
18109 pat = pat3; /* outer level, search for middle */
18110 }
18111
18112 if (nest == 0)
18113 {
18114 /* Found the match: return matchcount or line number. */
18115 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018116 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018118 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018119 if (flags & SP_SETPCMARK)
18120 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018121 curwin->w_cursor = pos;
18122 if (!(flags & SP_REPEAT))
18123 break;
18124 nest = 1; /* search for next unmatched */
18125 }
18126 }
18127
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018128 if (match_pos != NULL)
18129 {
18130 /* Store the match cursor position */
18131 match_pos->lnum = curwin->w_cursor.lnum;
18132 match_pos->col = curwin->w_cursor.col + 1;
18133 }
18134
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018136 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018137 curwin->w_cursor = save_cursor;
18138
18139theend:
18140 vim_free(pat2);
18141 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018142 if (p_cpo == empty_option)
18143 p_cpo = save_cpo;
18144 else
18145 /* Darn, evaluating the {skip} expression changed the value. */
18146 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018147
18148 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149}
18150
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018151/*
18152 * "searchpos()" function
18153 */
18154 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018155f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018156{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018157 pos_T match_pos;
18158 int lnum = 0;
18159 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018160 int n;
18161 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018162
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018163 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018164 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018165
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018166 n = search_cmn(argvars, &match_pos, &flags);
18167 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018168 {
18169 lnum = match_pos.lnum;
18170 col = match_pos.col;
18171 }
18172
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018173 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18174 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018175 if (flags & SP_SUBPAT)
18176 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018177}
18178
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018180f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018182#ifdef FEAT_CLIENTSERVER
18183 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018184 char_u *server = get_tv_string_chk(&argvars[0]);
18185 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018186
Bram Moolenaar0d660222005-01-07 21:51:51 +000018187 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018188 if (server == NULL || reply == NULL)
18189 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018190 if (check_restricted() || check_secure())
18191 return;
18192# ifdef FEAT_X11
18193 if (check_connection() == FAIL)
18194 return;
18195# endif
18196
18197 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018199 EMSG(_("E258: Unable to send to client"));
18200 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018202 rettv->vval.v_number = 0;
18203#else
18204 rettv->vval.v_number = -1;
18205#endif
18206}
18207
Bram Moolenaar0d660222005-01-07 21:51:51 +000018208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018209f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018210{
18211 char_u *r = NULL;
18212
18213#ifdef FEAT_CLIENTSERVER
18214# ifdef WIN32
18215 r = serverGetVimNames();
18216# else
18217 make_connection();
18218 if (X_DISPLAY != NULL)
18219 r = serverGetVimNames(X_DISPLAY);
18220# endif
18221#endif
18222 rettv->v_type = VAR_STRING;
18223 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018224}
18225
18226/*
18227 * "setbufvar()" function
18228 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018230f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018231{
18232 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018233 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018234 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018235 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018236 char_u nbuf[NUMBUFLEN];
18237
18238 if (check_restricted() || check_secure())
18239 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018240 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18241 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018242 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018243 varp = &argvars[2];
18244
18245 if (buf != NULL && varname != NULL && varp != NULL)
18246 {
18247 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018249
18250 if (*varname == '&')
18251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018252 long numval;
18253 char_u *strval;
18254 int error = FALSE;
18255
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018257 numval = get_tv_number_chk(varp, &error);
18258 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018259 if (!error && strval != NULL)
18260 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 }
18262 else
18263 {
18264 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18265 if (bufvarname != NULL)
18266 {
18267 STRCPY(bufvarname, "b:");
18268 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018269 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 vim_free(bufvarname);
18271 }
18272 }
18273
18274 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277}
18278
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018280f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018281{
18282 dict_T *d;
18283 dictitem_T *di;
18284 char_u *csearch;
18285
18286 if (argvars[0].v_type != VAR_DICT)
18287 {
18288 EMSG(_(e_dictreq));
18289 return;
18290 }
18291
18292 if ((d = argvars[0].vval.v_dict) != NULL)
18293 {
18294 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18295 if (csearch != NULL)
18296 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018297#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018298 if (enc_utf8)
18299 {
18300 int pcc[MAX_MCO];
18301 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018302
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018303 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18304 }
18305 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018306#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018307 set_last_csearch(PTR2CHAR(csearch),
18308 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018309 }
18310
18311 di = dict_find(d, (char_u *)"forward", -1);
18312 if (di != NULL)
18313 set_csearch_direction(get_tv_number(&di->di_tv)
18314 ? FORWARD : BACKWARD);
18315
18316 di = dict_find(d, (char_u *)"until", -1);
18317 if (di != NULL)
18318 set_csearch_until(!!get_tv_number(&di->di_tv));
18319 }
18320}
18321
Bram Moolenaar071d4272004-06-13 20:20:40 +000018322/*
18323 * "setcmdpos()" function
18324 */
18325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018326f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018327{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018328 int pos = (int)get_tv_number(&argvars[0]) - 1;
18329
18330 if (pos >= 0)
18331 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018332}
18333
18334/*
18335 * "setline()" function
18336 */
18337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018338f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339{
18340 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018341 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018342 list_T *l = NULL;
18343 listitem_T *li = NULL;
18344 long added = 0;
18345 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018346
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018347 lnum = get_tv_lnum(&argvars[0]);
18348 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018349 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018350 l = argvars[1].vval.v_list;
18351 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018352 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018353 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018354 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018355
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018356 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018357 for (;;)
18358 {
18359 if (l != NULL)
18360 {
18361 /* list argument, get next string */
18362 if (li == NULL)
18363 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018364 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018365 li = li->li_next;
18366 }
18367
18368 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018369 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018370 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018371
18372 /* When coming here from Insert mode, sync undo, so that this can be
18373 * undone separately from what was previously inserted. */
18374 if (u_sync_once == 2)
18375 {
18376 u_sync_once = 1; /* notify that u_sync() was called */
18377 u_sync(TRUE);
18378 }
18379
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018380 if (lnum <= curbuf->b_ml.ml_line_count)
18381 {
18382 /* existing line, replace it */
18383 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18384 {
18385 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018386 if (lnum == curwin->w_cursor.lnum)
18387 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018388 rettv->vval.v_number = 0; /* OK */
18389 }
18390 }
18391 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18392 {
18393 /* lnum is one past the last line, append the line */
18394 ++added;
18395 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18396 rettv->vval.v_number = 0; /* OK */
18397 }
18398
18399 if (l == NULL) /* only one string argument */
18400 break;
18401 ++lnum;
18402 }
18403
18404 if (added > 0)
18405 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018406}
18407
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018408static 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 +000018409
Bram Moolenaar071d4272004-06-13 20:20:40 +000018410/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018411 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018412 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018414set_qf_ll_list(
18415 win_T *wp UNUSED,
18416 typval_T *list_arg UNUSED,
18417 typval_T *action_arg UNUSED,
18418 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018419{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018420#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018421 char_u *act;
18422 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018423#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018424
Bram Moolenaar2641f772005-03-25 21:58:17 +000018425 rettv->vval.v_number = -1;
18426
18427#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018428 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018429 EMSG(_(e_listreq));
18430 else
18431 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018432 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018433
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018434 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018435 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018436 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018437 if (act == NULL)
18438 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018439 if (*act == 'a' || *act == 'r')
18440 action = *act;
18441 }
18442
Bram Moolenaar81484f42012-12-05 15:16:47 +010018443 if (l != NULL && set_errorlist(wp, l, action,
18444 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018445 rettv->vval.v_number = 0;
18446 }
18447#endif
18448}
18449
18450/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018451 * "setloclist()" function
18452 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018454f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018455{
18456 win_T *win;
18457
18458 rettv->vval.v_number = -1;
18459
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018460 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018461 if (win != NULL)
18462 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18463}
18464
18465/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018466 * "setmatches()" function
18467 */
18468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018469f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018470{
18471#ifdef FEAT_SEARCH_EXTRA
18472 list_T *l;
18473 listitem_T *li;
18474 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018475 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018476
18477 rettv->vval.v_number = -1;
18478 if (argvars[0].v_type != VAR_LIST)
18479 {
18480 EMSG(_(e_listreq));
18481 return;
18482 }
18483 if ((l = argvars[0].vval.v_list) != NULL)
18484 {
18485
18486 /* To some extent make sure that we are dealing with a list from
18487 * "getmatches()". */
18488 li = l->lv_first;
18489 while (li != NULL)
18490 {
18491 if (li->li_tv.v_type != VAR_DICT
18492 || (d = li->li_tv.vval.v_dict) == NULL)
18493 {
18494 EMSG(_(e_invarg));
18495 return;
18496 }
18497 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018498 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18499 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018500 && dict_find(d, (char_u *)"priority", -1) != NULL
18501 && dict_find(d, (char_u *)"id", -1) != NULL))
18502 {
18503 EMSG(_(e_invarg));
18504 return;
18505 }
18506 li = li->li_next;
18507 }
18508
18509 clear_matches(curwin);
18510 li = l->lv_first;
18511 while (li != NULL)
18512 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018513 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018514 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018515 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018516 char_u *group;
18517 int priority;
18518 int id;
18519 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018520
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018521 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018522 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18523 {
18524 if (s == NULL)
18525 {
18526 s = list_alloc();
18527 if (s == NULL)
18528 return;
18529 }
18530
18531 /* match from matchaddpos() */
18532 for (i = 1; i < 9; i++)
18533 {
18534 sprintf((char *)buf, (char *)"pos%d", i);
18535 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18536 {
18537 if (di->di_tv.v_type != VAR_LIST)
18538 return;
18539
18540 list_append_tv(s, &di->di_tv);
18541 s->lv_refcount++;
18542 }
18543 else
18544 break;
18545 }
18546 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018547
18548 group = get_dict_string(d, (char_u *)"group", FALSE);
18549 priority = (int)get_dict_number(d, (char_u *)"priority");
18550 id = (int)get_dict_number(d, (char_u *)"id");
18551 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18552 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18553 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018554 if (i == 0)
18555 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018556 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018557 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018558 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018559 }
18560 else
18561 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018562 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018563 list_unref(s);
18564 s = NULL;
18565 }
18566
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018567 li = li->li_next;
18568 }
18569 rettv->vval.v_number = 0;
18570 }
18571#endif
18572}
18573
18574/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018575 * "setpos()" function
18576 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018578f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018579{
18580 pos_T pos;
18581 int fnum;
18582 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018583 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018584
Bram Moolenaar08250432008-02-13 11:42:46 +000018585 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018586 name = get_tv_string_chk(argvars);
18587 if (name != NULL)
18588 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018589 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018590 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018591 if (--pos.col < 0)
18592 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018593 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018594 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018595 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018596 if (fnum == curbuf->b_fnum)
18597 {
18598 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018599 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018600 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018601 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018602 curwin->w_set_curswant = FALSE;
18603 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018604 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018605 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018606 }
18607 else
18608 EMSG(_(e_invarg));
18609 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018610 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18611 {
18612 /* set mark */
18613 if (setmark_pos(name[1], &pos, fnum) == OK)
18614 rettv->vval.v_number = 0;
18615 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018616 else
18617 EMSG(_(e_invarg));
18618 }
18619 }
18620}
18621
18622/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018623 * "setqflist()" function
18624 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018626f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018627{
18628 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18629}
18630
18631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018632 * "setreg()" function
18633 */
18634 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018635f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018636{
18637 int regname;
18638 char_u *strregname;
18639 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018640 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641 int append;
18642 char_u yank_type;
18643 long block_len;
18644
18645 block_len = -1;
18646 yank_type = MAUTO;
18647 append = FALSE;
18648
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018649 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018650 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018651
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 if (strregname == NULL)
18653 return; /* type error; errmsg already given */
18654 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 if (regname == 0 || regname == '@')
18656 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018657
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018658 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018660 stropt = get_tv_string_chk(&argvars[2]);
18661 if (stropt == NULL)
18662 return; /* type error */
18663 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018664 switch (*stropt)
18665 {
18666 case 'a': case 'A': /* append */
18667 append = TRUE;
18668 break;
18669 case 'v': case 'c': /* character-wise selection */
18670 yank_type = MCHAR;
18671 break;
18672 case 'V': case 'l': /* line-wise selection */
18673 yank_type = MLINE;
18674 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018675 case 'b': case Ctrl_V: /* block-wise selection */
18676 yank_type = MBLOCK;
18677 if (VIM_ISDIGIT(stropt[1]))
18678 {
18679 ++stropt;
18680 block_len = getdigits(&stropt) - 1;
18681 --stropt;
18682 }
18683 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 }
18685 }
18686
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018687 if (argvars[1].v_type == VAR_LIST)
18688 {
18689 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018690 char_u **allocval;
18691 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018692 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018693 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018694 int len = argvars[1].vval.v_list->lv_len;
18695 listitem_T *li;
18696
Bram Moolenaar7d647822014-04-05 21:28:56 +020018697 /* First half: use for pointers to result lines; second half: use for
18698 * pointers to allocated copies. */
18699 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018700 if (lstval == NULL)
18701 return;
18702 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018703 allocval = lstval + len + 2;
18704 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018705
18706 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18707 li = li->li_next)
18708 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018709 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018710 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018711 goto free_lstval;
18712 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018713 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018714 /* Need to make a copy, next get_tv_string_buf_chk() will
18715 * overwrite the string. */
18716 strval = vim_strsave(buf);
18717 if (strval == NULL)
18718 goto free_lstval;
18719 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018720 }
18721 *curval++ = strval;
18722 }
18723 *curval++ = NULL;
18724
18725 write_reg_contents_lst(regname, lstval, -1,
18726 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018727free_lstval:
18728 while (curallocval > allocval)
18729 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018730 vim_free(lstval);
18731 }
18732 else
18733 {
18734 strval = get_tv_string_chk(&argvars[1]);
18735 if (strval == NULL)
18736 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018737 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018739 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018740 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741}
18742
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018743/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018744 * "settabvar()" function
18745 */
18746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018747f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018748{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018749#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018750 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018751 tabpage_T *tp;
18752#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018753 char_u *varname, *tabvarname;
18754 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018755
18756 rettv->vval.v_number = 0;
18757
18758 if (check_restricted() || check_secure())
18759 return;
18760
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018761#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018762 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018763#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018764 varname = get_tv_string_chk(&argvars[1]);
18765 varp = &argvars[2];
18766
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018767 if (varname != NULL && varp != NULL
18768#ifdef FEAT_WINDOWS
18769 && tp != NULL
18770#endif
18771 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018772 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018773#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018774 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018775 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018776#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018777
18778 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18779 if (tabvarname != NULL)
18780 {
18781 STRCPY(tabvarname, "t:");
18782 STRCPY(tabvarname + 2, varname);
18783 set_var(tabvarname, varp, TRUE);
18784 vim_free(tabvarname);
18785 }
18786
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018787#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018788 /* Restore current tabpage */
18789 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018790 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018791#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018792 }
18793}
18794
18795/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018796 * "settabwinvar()" function
18797 */
18798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018799f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018800{
18801 setwinvar(argvars, rettv, 1);
18802}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018803
18804/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018805 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018806 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018808f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018809{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018810 setwinvar(argvars, rettv, 0);
18811}
18812
18813/*
18814 * "setwinvar()" and "settabwinvar()" functions
18815 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018816
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018818setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018819{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018820 win_T *win;
18821#ifdef FEAT_WINDOWS
18822 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018823 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018824 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018825#endif
18826 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018827 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018829 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018830
18831 if (check_restricted() || check_secure())
18832 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018833
18834#ifdef FEAT_WINDOWS
18835 if (off == 1)
18836 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18837 else
18838 tp = curtab;
18839#endif
18840 win = find_win_by_nr(&argvars[off], tp);
18841 varname = get_tv_string_chk(&argvars[off + 1]);
18842 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018843
18844 if (win != NULL && varname != NULL && varp != NULL)
18845 {
18846#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018847 need_switch_win = !(tp == curtab && win == curwin);
18848 if (!need_switch_win
18849 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018851 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018852 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018853 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018854 long numval;
18855 char_u *strval;
18856 int error = FALSE;
18857
18858 ++varname;
18859 numval = get_tv_number_chk(varp, &error);
18860 strval = get_tv_string_buf_chk(varp, nbuf);
18861 if (!error && strval != NULL)
18862 set_option_value(varname, numval, strval, OPT_LOCAL);
18863 }
18864 else
18865 {
18866 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18867 if (winvarname != NULL)
18868 {
18869 STRCPY(winvarname, "w:");
18870 STRCPY(winvarname + 2, varname);
18871 set_var(winvarname, varp, TRUE);
18872 vim_free(winvarname);
18873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018874 }
18875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018876#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018877 if (need_switch_win)
18878 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018879#endif
18880 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018881}
18882
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018883#ifdef FEAT_CRYPT
18884/*
18885 * "sha256({string})" function
18886 */
18887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018888f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018889{
18890 char_u *p;
18891
18892 p = get_tv_string(&argvars[0]);
18893 rettv->vval.v_string = vim_strsave(
18894 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18895 rettv->v_type = VAR_STRING;
18896}
18897#endif /* FEAT_CRYPT */
18898
Bram Moolenaar071d4272004-06-13 20:20:40 +000018899/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018900 * "shellescape({string})" function
18901 */
18902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018903f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018904{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018905 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018906 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018907 rettv->v_type = VAR_STRING;
18908}
18909
18910/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018911 * shiftwidth() function
18912 */
18913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018914f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018915{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018916 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018917}
18918
18919/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018920 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 */
18922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018923f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018925 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018926
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927 p = get_tv_string(&argvars[0]);
18928 rettv->vval.v_string = vim_strsave(p);
18929 simplify_filename(rettv->vval.v_string); /* simplify in place */
18930 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018931}
18932
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018933#ifdef FEAT_FLOAT
18934/*
18935 * "sin()" function
18936 */
18937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018938f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018941
18942 rettv->v_type = VAR_FLOAT;
18943 if (get_float_arg(argvars, &f) == OK)
18944 rettv->vval.v_float = sin(f);
18945 else
18946 rettv->vval.v_float = 0.0;
18947}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018948
18949/*
18950 * "sinh()" function
18951 */
18952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018953f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018954{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018955 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018956
18957 rettv->v_type = VAR_FLOAT;
18958 if (get_float_arg(argvars, &f) == OK)
18959 rettv->vval.v_float = sinh(f);
18960 else
18961 rettv->vval.v_float = 0.0;
18962}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018963#endif
18964
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965static int
18966#ifdef __BORLANDC__
18967 _RTLENTRYF
18968#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018969 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018970static int
18971#ifdef __BORLANDC__
18972 _RTLENTRYF
18973#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018974 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018976/* struct used in the array that's given to qsort() */
18977typedef struct
18978{
18979 listitem_T *item;
18980 int idx;
18981} sortItem_T;
18982
Bram Moolenaar0b962472016-02-22 22:51:33 +010018983/* struct storing information about current sort */
18984typedef struct
18985{
18986 int item_compare_ic;
18987 int item_compare_numeric;
18988 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018989#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018990 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018991#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018992 char_u *item_compare_func;
18993 dict_T *item_compare_selfdict;
18994 int item_compare_func_err;
18995 int item_compare_keep_zero;
18996} sortinfo_T;
18997static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018998static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999#define ITEM_COMPARE_FAIL 999
19000
Bram Moolenaar071d4272004-06-13 20:20:40 +000019001/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019002 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000019003 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 static int
19005#ifdef __BORLANDC__
19006_RTLENTRYF
19007#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019008item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019010 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019011 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019012 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019013 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014 int res;
19015 char_u numbuf1[NUMBUFLEN];
19016 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019018 si1 = (sortItem_T *)s1;
19019 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019020 tv1 = &si1->item->li_tv;
19021 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019022
Bram Moolenaar0b962472016-02-22 22:51:33 +010019023 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019024 {
19025 long v1 = get_tv_number(tv1);
19026 long v2 = get_tv_number(tv2);
19027
19028 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19029 }
19030
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019031#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019032 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019033 {
19034 float_T v1 = get_tv_float(tv1);
19035 float_T v2 = get_tv_float(tv2);
19036
19037 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19038 }
19039#endif
19040
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019041 /* tv2string() puts quotes around a string and allocates memory. Don't do
19042 * that for string variables. Use a single quote when comparing with a
19043 * non-string to do what the docs promise. */
19044 if (tv1->v_type == VAR_STRING)
19045 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019046 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019047 p1 = (char_u *)"'";
19048 else
19049 p1 = tv1->vval.v_string;
19050 }
19051 else
19052 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19053 if (tv2->v_type == VAR_STRING)
19054 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019055 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019056 p2 = (char_u *)"'";
19057 else
19058 p2 = tv2->vval.v_string;
19059 }
19060 else
19061 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019062 if (p1 == NULL)
19063 p1 = (char_u *)"";
19064 if (p2 == NULL)
19065 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019067 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019069 res = STRICMP(p1, p2);
19070 else
19071 res = STRCMP(p1, p2);
19072 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019074 {
19075 double n1, n2;
19076 n1 = strtod((char *)p1, (char **)&p1);
19077 n2 = strtod((char *)p2, (char **)&p2);
19078 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19079 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019080
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019081 /* When the result would be zero, compare the item indexes. Makes the
19082 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019083 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019084 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019085
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 vim_free(tofree1);
19087 vim_free(tofree2);
19088 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089}
19090
19091 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019092#ifdef __BORLANDC__
19093_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019095item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019097 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019099 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019100 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019102
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019103 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105 return 0;
19106
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019107 si1 = (sortItem_T *)s1;
19108 si2 = (sortItem_T *)s2;
19109
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019110 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019111 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019112 copy_tv(&si1->item->li_tv, &argv[0]);
19113 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114
19115 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019117 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019118 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019119 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019120 clear_tv(&argv[0]);
19121 clear_tv(&argv[1]);
19122
19123 if (res == FAIL)
19124 res = ITEM_COMPARE_FAIL;
19125 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19127 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019128 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019130
19131 /* When the result would be zero, compare the pointers themselves. Makes
19132 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019133 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019134 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019135
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136 return res;
19137}
19138
19139/*
19140 * "sort({list})" function
19141 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019143do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019144{
Bram Moolenaar33570922005-01-25 22:26:29 +000019145 list_T *l;
19146 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019147 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019148 sortinfo_T *old_sortinfo;
19149 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019150 long len;
19151 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152
Bram Moolenaar0b962472016-02-22 22:51:33 +010019153 /* Pointer to current info struct used in compare function. Save and
19154 * restore the current one for nested calls. */
19155 old_sortinfo = sortinfo;
19156 sortinfo = &info;
19157
Bram Moolenaar0d660222005-01-07 21:51:51 +000019158 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019159 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160 else
19161 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019162 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019163 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019164 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19165 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019166 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019167 rettv->vval.v_list = l;
19168 rettv->v_type = VAR_LIST;
19169 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019170
Bram Moolenaar0d660222005-01-07 21:51:51 +000019171 len = list_len(l);
19172 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019173 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019174
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 info.item_compare_ic = FALSE;
19176 info.item_compare_numeric = FALSE;
19177 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019178#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019180#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019181 info.item_compare_func = NULL;
19182 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019183 if (argvars[1].v_type != VAR_UNKNOWN)
19184 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019185 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019186 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019187 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019188 else
19189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019190 int error = FALSE;
19191
19192 i = get_tv_number_chk(&argvars[1], &error);
19193 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019194 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019195 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019196 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019197 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func = get_tv_string(&argvars[1]);
19199 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019200 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019202 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 info.item_compare_func = NULL;
19204 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019205 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019206 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019207 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019208 info.item_compare_func = NULL;
19209 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019210 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019211#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019213 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019214 info.item_compare_func = NULL;
19215 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019216 }
19217#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019218 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019219 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019220 info.item_compare_func = NULL;
19221 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019222 }
19223 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019224 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019225
19226 if (argvars[2].v_type != VAR_UNKNOWN)
19227 {
19228 /* optional third argument: {dict} */
19229 if (argvars[2].v_type != VAR_DICT)
19230 {
19231 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019232 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019233 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019234 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019235 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019236 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019237
Bram Moolenaar0d660222005-01-07 21:51:51 +000019238 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019239 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019241 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019242
Bram Moolenaar327aa022014-03-25 18:24:23 +010019243 i = 0;
19244 if (sort)
19245 {
19246 /* sort(): ptrs will be the list to sort */
19247 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019248 {
19249 ptrs[i].item = li;
19250 ptrs[i].idx = i;
19251 ++i;
19252 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019253
Bram Moolenaar0b962472016-02-22 22:51:33 +010019254 info.item_compare_func_err = FALSE;
19255 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019257 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019258 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019259 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019260 EMSG(_("E702: Sort compare function failed"));
19261 else
19262 {
19263 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019264 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019265 info.item_compare_func == NULL
19266 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019267
Bram Moolenaar0b962472016-02-22 22:51:33 +010019268 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019269 {
19270 /* Clear the List and append the items in sorted order. */
19271 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19272 l->lv_len = 0;
19273 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019274 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019275 }
19276 }
19277 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019279 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019280 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019281
19282 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019283 info.item_compare_func_err = FALSE;
19284 info.item_compare_keep_zero = TRUE;
19285 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019286 ? item_compare2 : item_compare;
19287
19288 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19289 li = li->li_next)
19290 {
19291 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19292 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019293 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019294 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019295 {
19296 EMSG(_("E882: Uniq compare function failed"));
19297 break;
19298 }
19299 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019300
Bram Moolenaar0b962472016-02-22 22:51:33 +010019301 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019302 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019303 while (--i >= 0)
19304 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019305 li = ptrs[i].item->li_next;
19306 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019307 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019308 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019309 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019310 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019311 list_fix_watch(l, li);
19312 listitem_free(li);
19313 l->lv_len--;
19314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019315 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019316 }
19317
19318 vim_free(ptrs);
19319 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019320theend:
19321 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019322}
19323
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019324/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019325 * "sort({list})" function
19326 */
19327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019328f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019329{
19330 do_sort_uniq(argvars, rettv, TRUE);
19331}
19332
19333/*
19334 * "uniq({list})" function
19335 */
19336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019337f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019338{
19339 do_sort_uniq(argvars, rettv, FALSE);
19340}
19341
19342/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019343 * "soundfold({word})" function
19344 */
19345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019347{
19348 char_u *s;
19349
19350 rettv->v_type = VAR_STRING;
19351 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019352#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019353 rettv->vval.v_string = eval_soundfold(s);
19354#else
19355 rettv->vval.v_string = vim_strsave(s);
19356#endif
19357}
19358
19359/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019360 * "spellbadword()" function
19361 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019363f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019364{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019365 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019366 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019367 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019368
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019369 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019370 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019371
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019372#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019373 if (argvars[0].v_type == VAR_UNKNOWN)
19374 {
19375 /* Find the start and length of the badly spelled word. */
19376 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19377 if (len != 0)
19378 word = ml_get_cursor();
19379 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019380 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019381 {
19382 char_u *str = get_tv_string_chk(&argvars[0]);
19383 int capcol = -1;
19384
19385 if (str != NULL)
19386 {
19387 /* Check the argument for spelling. */
19388 while (*str != NUL)
19389 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019390 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019391 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019392 {
19393 word = str;
19394 break;
19395 }
19396 str += len;
19397 }
19398 }
19399 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019400#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019401
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019402 list_append_string(rettv->vval.v_list, word, len);
19403 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019404 attr == HLF_SPB ? "bad" :
19405 attr == HLF_SPR ? "rare" :
19406 attr == HLF_SPL ? "local" :
19407 attr == HLF_SPC ? "caps" :
19408 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019409}
19410
19411/*
19412 * "spellsuggest()" function
19413 */
19414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019415f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019416{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019417#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019418 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019419 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019420 int maxcount;
19421 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019422 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019423 listitem_T *li;
19424 int need_capital = FALSE;
19425#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019426
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019427 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019428 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019429
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019430#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019431 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019432 {
19433 str = get_tv_string(&argvars[0]);
19434 if (argvars[1].v_type != VAR_UNKNOWN)
19435 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019436 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019437 if (maxcount <= 0)
19438 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019439 if (argvars[2].v_type != VAR_UNKNOWN)
19440 {
19441 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19442 if (typeerr)
19443 return;
19444 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019445 }
19446 else
19447 maxcount = 25;
19448
Bram Moolenaar4770d092006-01-12 23:22:24 +000019449 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019450
19451 for (i = 0; i < ga.ga_len; ++i)
19452 {
19453 str = ((char_u **)ga.ga_data)[i];
19454
19455 li = listitem_alloc();
19456 if (li == NULL)
19457 vim_free(str);
19458 else
19459 {
19460 li->li_tv.v_type = VAR_STRING;
19461 li->li_tv.v_lock = 0;
19462 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019463 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019464 }
19465 }
19466 ga_clear(&ga);
19467 }
19468#endif
19469}
19470
Bram Moolenaar0d660222005-01-07 21:51:51 +000019471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019472f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019473{
19474 char_u *str;
19475 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019476 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477 regmatch_T regmatch;
19478 char_u patbuf[NUMBUFLEN];
19479 char_u *save_cpo;
19480 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019482 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019483 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019484
19485 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19486 save_cpo = p_cpo;
19487 p_cpo = (char_u *)"";
19488
19489 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019490 if (argvars[1].v_type != VAR_UNKNOWN)
19491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019492 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19493 if (pat == NULL)
19494 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019495 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019496 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019497 }
19498 if (pat == NULL || *pat == NUL)
19499 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019500
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019501 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019502 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019503 if (typeerr)
19504 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019505
Bram Moolenaar0d660222005-01-07 21:51:51 +000019506 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19507 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019508 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019509 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019510 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019511 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019512 if (*str == NUL)
19513 match = FALSE; /* empty item at the end */
19514 else
19515 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019516 if (match)
19517 end = regmatch.startp[0];
19518 else
19519 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019520 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19521 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019522 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019523 if (list_append_string(rettv->vval.v_list, str,
19524 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019525 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019526 }
19527 if (!match)
19528 break;
19529 /* Advance to just after the match. */
19530 if (regmatch.endp[0] > str)
19531 col = 0;
19532 else
19533 {
19534 /* Don't get stuck at the same match. */
19535#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019536 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019537#else
19538 col = 1;
19539#endif
19540 }
19541 str = regmatch.endp[0];
19542 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543
Bram Moolenaar473de612013-06-08 18:19:48 +020019544 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019546
Bram Moolenaar0d660222005-01-07 21:51:51 +000019547 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548}
19549
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019550#ifdef FEAT_FLOAT
19551/*
19552 * "sqrt()" function
19553 */
19554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019555f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019556{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019557 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019558
19559 rettv->v_type = VAR_FLOAT;
19560 if (get_float_arg(argvars, &f) == OK)
19561 rettv->vval.v_float = sqrt(f);
19562 else
19563 rettv->vval.v_float = 0.0;
19564}
19565
19566/*
19567 * "str2float()" function
19568 */
19569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019570f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019571{
19572 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19573
19574 if (*p == '+')
19575 p = skipwhite(p + 1);
19576 (void)string2float(p, &rettv->vval.v_float);
19577 rettv->v_type = VAR_FLOAT;
19578}
19579#endif
19580
Bram Moolenaar2c932302006-03-18 21:42:09 +000019581/*
19582 * "str2nr()" function
19583 */
19584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019585f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019586{
19587 int base = 10;
19588 char_u *p;
19589 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019590 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019591
19592 if (argvars[1].v_type != VAR_UNKNOWN)
19593 {
19594 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019595 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019596 {
19597 EMSG(_(e_invarg));
19598 return;
19599 }
19600 }
19601
19602 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019603 if (*p == '+')
19604 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019605 switch (base)
19606 {
19607 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19608 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19609 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19610 default: what = 0;
19611 }
19612 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019613 rettv->vval.v_number = n;
19614}
19615
Bram Moolenaar071d4272004-06-13 20:20:40 +000019616#ifdef HAVE_STRFTIME
19617/*
19618 * "strftime({format}[, {time}])" function
19619 */
19620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019621f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019622{
19623 char_u result_buf[256];
19624 struct tm *curtime;
19625 time_t seconds;
19626 char_u *p;
19627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019628 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019629
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019630 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019631 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019632 seconds = time(NULL);
19633 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019634 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635 curtime = localtime(&seconds);
19636 /* MSVC returns NULL for an invalid value of seconds. */
19637 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019639 else
19640 {
19641# ifdef FEAT_MBYTE
19642 vimconv_T conv;
19643 char_u *enc;
19644
19645 conv.vc_type = CONV_NONE;
19646 enc = enc_locale();
19647 convert_setup(&conv, p_enc, enc);
19648 if (conv.vc_type != CONV_NONE)
19649 p = string_convert(&conv, p, NULL);
19650# endif
19651 if (p != NULL)
19652 (void)strftime((char *)result_buf, sizeof(result_buf),
19653 (char *)p, curtime);
19654 else
19655 result_buf[0] = NUL;
19656
19657# ifdef FEAT_MBYTE
19658 if (conv.vc_type != CONV_NONE)
19659 vim_free(p);
19660 convert_setup(&conv, enc, p_enc);
19661 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019662 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663 else
19664# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019665 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666
19667# ifdef FEAT_MBYTE
19668 /* Release conversion descriptors */
19669 convert_setup(&conv, NULL, NULL);
19670 vim_free(enc);
19671# endif
19672 }
19673}
19674#endif
19675
19676/*
19677 * "stridx()" function
19678 */
19679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019680f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681{
19682 char_u buf[NUMBUFLEN];
19683 char_u *needle;
19684 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019685 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019686 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019687 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019689 needle = get_tv_string_chk(&argvars[1]);
19690 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019691 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019692 if (needle == NULL || haystack == NULL)
19693 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019694
Bram Moolenaar33570922005-01-25 22:26:29 +000019695 if (argvars[2].v_type != VAR_UNKNOWN)
19696 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019697 int error = FALSE;
19698
19699 start_idx = get_tv_number_chk(&argvars[2], &error);
19700 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019701 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019702 if (start_idx >= 0)
19703 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019704 }
19705
19706 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19707 if (pos != NULL)
19708 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709}
19710
19711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019712 * "string()" function
19713 */
19714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019715f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019716{
19717 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019718 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019719
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019720 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019721 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019722 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019723 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019724 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725}
19726
19727/*
19728 * "strlen()" function
19729 */
19730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019731f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019733 rettv->vval.v_number = (varnumber_T)(STRLEN(
19734 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019735}
19736
19737/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019738 * "strchars()" function
19739 */
19740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019741f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019742{
19743 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019744 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019745#ifdef FEAT_MBYTE
19746 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019747 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019748#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019749
19750 if (argvars[1].v_type != VAR_UNKNOWN)
19751 skipcc = get_tv_number_chk(&argvars[1], NULL);
19752 if (skipcc < 0 || skipcc > 1)
19753 EMSG(_(e_invarg));
19754 else
19755 {
19756#ifdef FEAT_MBYTE
19757 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19758 while (*s != NUL)
19759 {
19760 func_mb_ptr2char_adv(&s);
19761 ++len;
19762 }
19763 rettv->vval.v_number = len;
19764#else
19765 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19766#endif
19767 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019768}
19769
19770/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019771 * "strdisplaywidth()" function
19772 */
19773 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019774f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019775{
19776 char_u *s = get_tv_string(&argvars[0]);
19777 int col = 0;
19778
19779 if (argvars[1].v_type != VAR_UNKNOWN)
19780 col = get_tv_number(&argvars[1]);
19781
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019782 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019783}
19784
19785/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019786 * "strwidth()" function
19787 */
19788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019789f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019790{
19791 char_u *s = get_tv_string(&argvars[0]);
19792
19793 rettv->vval.v_number = (varnumber_T)(
19794#ifdef FEAT_MBYTE
19795 mb_string2cells(s, -1)
19796#else
19797 STRLEN(s)
19798#endif
19799 );
19800}
19801
19802/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019803 * "strpart()" function
19804 */
19805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019806f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019807{
19808 char_u *p;
19809 int n;
19810 int len;
19811 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019812 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019814 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019815 slen = (int)STRLEN(p);
19816
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019817 n = get_tv_number_chk(&argvars[1], &error);
19818 if (error)
19819 len = 0;
19820 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019821 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019822 else
19823 len = slen - n; /* default len: all bytes that are available. */
19824
19825 /*
19826 * Only return the overlap between the specified part and the actual
19827 * string.
19828 */
19829 if (n < 0)
19830 {
19831 len += n;
19832 n = 0;
19833 }
19834 else if (n > slen)
19835 n = slen;
19836 if (len < 0)
19837 len = 0;
19838 else if (n + len > slen)
19839 len = slen - n;
19840
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019841 rettv->v_type = VAR_STRING;
19842 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843}
19844
19845/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019846 * "strridx()" function
19847 */
19848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019849f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019850{
19851 char_u buf[NUMBUFLEN];
19852 char_u *needle;
19853 char_u *haystack;
19854 char_u *rest;
19855 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019856 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019857
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019858 needle = get_tv_string_chk(&argvars[1]);
19859 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019860
19861 rettv->vval.v_number = -1;
19862 if (needle == NULL || haystack == NULL)
19863 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019864
19865 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019866 if (argvars[2].v_type != VAR_UNKNOWN)
19867 {
19868 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019869 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019870 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019871 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019872 }
19873 else
19874 end_idx = haystack_len;
19875
Bram Moolenaar0d660222005-01-07 21:51:51 +000019876 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019877 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019878 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019879 lastmatch = haystack + end_idx;
19880 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019881 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019882 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019883 for (rest = haystack; *rest != '\0'; ++rest)
19884 {
19885 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019886 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019887 break;
19888 lastmatch = rest;
19889 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019890 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019891
19892 if (lastmatch == NULL)
19893 rettv->vval.v_number = -1;
19894 else
19895 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19896}
19897
19898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 * "strtrans()" function
19900 */
19901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019902f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019903{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019904 rettv->v_type = VAR_STRING;
19905 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906}
19907
19908/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019909 * "submatch()" function
19910 */
19911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019912f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019913{
Bram Moolenaar41571762014-04-02 19:00:58 +020019914 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019915 int no;
19916 int retList = 0;
19917
19918 no = (int)get_tv_number_chk(&argvars[0], &error);
19919 if (error)
19920 return;
19921 error = FALSE;
19922 if (argvars[1].v_type != VAR_UNKNOWN)
19923 retList = get_tv_number_chk(&argvars[1], &error);
19924 if (error)
19925 return;
19926
19927 if (retList == 0)
19928 {
19929 rettv->v_type = VAR_STRING;
19930 rettv->vval.v_string = reg_submatch(no);
19931 }
19932 else
19933 {
19934 rettv->v_type = VAR_LIST;
19935 rettv->vval.v_list = reg_submatch_list(no);
19936 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019937}
19938
19939/*
19940 * "substitute()" function
19941 */
19942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019943f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019944{
19945 char_u patbuf[NUMBUFLEN];
19946 char_u subbuf[NUMBUFLEN];
19947 char_u flagsbuf[NUMBUFLEN];
19948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019949 char_u *str = get_tv_string_chk(&argvars[0]);
19950 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19951 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19952 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19953
Bram Moolenaar0d660222005-01-07 21:51:51 +000019954 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019955 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19956 rettv->vval.v_string = NULL;
19957 else
19958 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019959}
19960
19961/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019962 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019965f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019966{
19967 int id = 0;
19968#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019969 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019970 long col;
19971 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019972 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019973
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019974 lnum = get_tv_lnum(argvars); /* -1 on type error */
19975 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19976 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019977
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019978 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019979 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019980 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019981#endif
19982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019983 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019984}
19985
19986/*
19987 * "synIDattr(id, what [, mode])" function
19988 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019990f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991{
19992 char_u *p = NULL;
19993#ifdef FEAT_SYN_HL
19994 int id;
19995 char_u *what;
19996 char_u *mode;
19997 char_u modebuf[NUMBUFLEN];
19998 int modec;
19999
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020000 id = get_tv_number(&argvars[0]);
20001 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020002 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020004 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020005 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020020006 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000020007 modec = 0; /* replace invalid with current */
20008 }
20009 else
20010 {
20011#ifdef FEAT_GUI
20012 if (gui.in_use)
20013 modec = 'g';
20014 else
20015#endif
20016 if (t_colors > 1)
20017 modec = 'c';
20018 else
20019 modec = 't';
20020 }
20021
20022
20023 switch (TOLOWER_ASC(what[0]))
20024 {
20025 case 'b':
20026 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
20027 p = highlight_color(id, what, modec);
20028 else /* bold */
20029 p = highlight_has_attr(id, HL_BOLD, modec);
20030 break;
20031
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020032 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020033 p = highlight_color(id, what, modec);
20034 break;
20035
20036 case 'i':
20037 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20038 p = highlight_has_attr(id, HL_INVERSE, modec);
20039 else /* italic */
20040 p = highlight_has_attr(id, HL_ITALIC, modec);
20041 break;
20042
20043 case 'n': /* name */
20044 p = get_highlight_name(NULL, id - 1);
20045 break;
20046
20047 case 'r': /* reverse */
20048 p = highlight_has_attr(id, HL_INVERSE, modec);
20049 break;
20050
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020051 case 's':
20052 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20053 p = highlight_color(id, what, modec);
20054 else /* standout */
20055 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056 break;
20057
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020058 case 'u':
20059 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20060 /* underline */
20061 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20062 else
20063 /* undercurl */
20064 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020065 break;
20066 }
20067
20068 if (p != NULL)
20069 p = vim_strsave(p);
20070#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020071 rettv->v_type = VAR_STRING;
20072 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020073}
20074
20075/*
20076 * "synIDtrans(id)" function
20077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020079f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020080{
20081 int id;
20082
20083#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020084 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020085
20086 if (id > 0)
20087 id = syn_get_final_id(id);
20088 else
20089#endif
20090 id = 0;
20091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020092 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093}
20094
20095/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020096 * "synconcealed(lnum, col)" function
20097 */
20098 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020099f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020100{
20101#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20102 long lnum;
20103 long col;
20104 int syntax_flags = 0;
20105 int cchar;
20106 int matchid = 0;
20107 char_u str[NUMBUFLEN];
20108#endif
20109
20110 rettv->v_type = VAR_LIST;
20111 rettv->vval.v_list = NULL;
20112
20113#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20114 lnum = get_tv_lnum(argvars); /* -1 on type error */
20115 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20116
20117 vim_memset(str, NUL, sizeof(str));
20118
20119 if (rettv_list_alloc(rettv) != FAIL)
20120 {
20121 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20122 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20123 && curwin->w_p_cole > 0)
20124 {
20125 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20126 syntax_flags = get_syntax_info(&matchid);
20127
20128 /* get the conceal character */
20129 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20130 {
20131 cchar = syn_get_sub_char();
20132 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20133 cchar = lcs_conceal;
20134 if (cchar != NUL)
20135 {
20136# ifdef FEAT_MBYTE
20137 if (has_mbyte)
20138 (*mb_char2bytes)(cchar, str);
20139 else
20140# endif
20141 str[0] = cchar;
20142 }
20143 }
20144 }
20145
20146 list_append_number(rettv->vval.v_list,
20147 (syntax_flags & HL_CONCEAL) != 0);
20148 /* -1 to auto-determine strlen */
20149 list_append_string(rettv->vval.v_list, str, -1);
20150 list_append_number(rettv->vval.v_list, matchid);
20151 }
20152#endif
20153}
20154
20155/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020156 * "synstack(lnum, col)" function
20157 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020159f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020160{
20161#ifdef FEAT_SYN_HL
20162 long lnum;
20163 long col;
20164 int i;
20165 int id;
20166#endif
20167
20168 rettv->v_type = VAR_LIST;
20169 rettv->vval.v_list = NULL;
20170
20171#ifdef FEAT_SYN_HL
20172 lnum = get_tv_lnum(argvars); /* -1 on type error */
20173 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20174
20175 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020176 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020177 && rettv_list_alloc(rettv) != FAIL)
20178 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020179 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020180 for (i = 0; ; ++i)
20181 {
20182 id = syn_get_stack_item(i);
20183 if (id < 0)
20184 break;
20185 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20186 break;
20187 }
20188 }
20189#endif
20190}
20191
Bram Moolenaar071d4272004-06-13 20:20:40 +000020192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020193get_cmd_output_as_rettv(
20194 typval_T *argvars,
20195 typval_T *rettv,
20196 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020198 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020200 char_u *infile = NULL;
20201 char_u buf[NUMBUFLEN];
20202 int err = FALSE;
20203 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020204 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020205 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020206
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020207 rettv->v_type = VAR_STRING;
20208 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020209 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020210 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020211
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020212 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020213 {
20214 /*
20215 * Write the string to a temp file, to be used for input of the shell
20216 * command.
20217 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020218 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020219 {
20220 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020221 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020222 }
20223
20224 fd = mch_fopen((char *)infile, WRITEBIN);
20225 if (fd == NULL)
20226 {
20227 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020228 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020229 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020230 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020231 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020232 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20233 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020234 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020235 else
20236 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020237 size_t len;
20238
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020239 p = get_tv_string_buf_chk(&argvars[1], buf);
20240 if (p == NULL)
20241 {
20242 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020243 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020244 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020245 len = STRLEN(p);
20246 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020247 err = TRUE;
20248 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020249 if (fclose(fd) != 0)
20250 err = TRUE;
20251 if (err)
20252 {
20253 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020254 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020255 }
20256 }
20257
Bram Moolenaar52a72462014-08-29 15:53:52 +020020258 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20259 * echoes typeahead, that messes up the display. */
20260 if (!msg_silent)
20261 flags += SHELL_COOKED;
20262
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020263 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020264 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265 int len;
20266 listitem_T *li;
20267 char_u *s = NULL;
20268 char_u *start;
20269 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020270 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271
Bram Moolenaar52a72462014-08-29 15:53:52 +020020272 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020273 if (res == NULL)
20274 goto errret;
20275
20276 list = list_alloc();
20277 if (list == NULL)
20278 goto errret;
20279
20280 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020281 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020282 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020283 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020284 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020285 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020286
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020287 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020288 if (s == NULL)
20289 goto errret;
20290
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020291 for (p = s; start < end; ++p, ++start)
20292 *p = *start == NUL ? NL : *start;
20293 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020294
20295 li = listitem_alloc();
20296 if (li == NULL)
20297 {
20298 vim_free(s);
20299 goto errret;
20300 }
20301 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020302 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020303 li->li_tv.vval.v_string = s;
20304 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020305 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020306
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020307 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020308 rettv->v_type = VAR_LIST;
20309 rettv->vval.v_list = list;
20310 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020311 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020312 else
20313 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020314 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020315#ifdef USE_CR
20316 /* translate <CR> into <NL> */
20317 if (res != NULL)
20318 {
20319 char_u *s;
20320
20321 for (s = res; *s; ++s)
20322 {
20323 if (*s == CAR)
20324 *s = NL;
20325 }
20326 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020327#else
20328# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020329 /* translate <CR><NL> into <NL> */
20330 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020331 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020332 char_u *s, *d;
20333
20334 d = res;
20335 for (s = res; *s; ++s)
20336 {
20337 if (s[0] == CAR && s[1] == NL)
20338 ++s;
20339 *d++ = *s;
20340 }
20341 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020343# endif
20344#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020345 rettv->vval.v_string = res;
20346 res = NULL;
20347 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020348
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020349errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020350 if (infile != NULL)
20351 {
20352 mch_remove(infile);
20353 vim_free(infile);
20354 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020355 if (res != NULL)
20356 vim_free(res);
20357 if (list != NULL)
20358 list_free(list, TRUE);
20359}
20360
20361/*
20362 * "system()" function
20363 */
20364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020365f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020366{
20367 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20368}
20369
20370/*
20371 * "systemlist()" function
20372 */
20373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020374f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020375{
20376 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020377}
20378
20379/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020380 * "tabpagebuflist()" function
20381 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020383f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020384{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020385#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020386 tabpage_T *tp;
20387 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020388
20389 if (argvars[0].v_type == VAR_UNKNOWN)
20390 wp = firstwin;
20391 else
20392 {
20393 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20394 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020395 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020396 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020397 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020398 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020399 for (; wp != NULL; wp = wp->w_next)
20400 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020401 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020402 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020403 }
20404#endif
20405}
20406
20407
20408/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020409 * "tabpagenr()" function
20410 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020411 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020412f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020413{
20414 int nr = 1;
20415#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020416 char_u *arg;
20417
20418 if (argvars[0].v_type != VAR_UNKNOWN)
20419 {
20420 arg = get_tv_string_chk(&argvars[0]);
20421 nr = 0;
20422 if (arg != NULL)
20423 {
20424 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020425 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020426 else
20427 EMSG2(_(e_invexpr2), arg);
20428 }
20429 }
20430 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020431 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020432#endif
20433 rettv->vval.v_number = nr;
20434}
20435
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020436
20437#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020438static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020439
20440/*
20441 * Common code for tabpagewinnr() and winnr().
20442 */
20443 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020444get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020445{
20446 win_T *twin;
20447 int nr = 1;
20448 win_T *wp;
20449 char_u *arg;
20450
20451 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20452 if (argvar->v_type != VAR_UNKNOWN)
20453 {
20454 arg = get_tv_string_chk(argvar);
20455 if (arg == NULL)
20456 nr = 0; /* type error; errmsg already given */
20457 else if (STRCMP(arg, "$") == 0)
20458 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20459 else if (STRCMP(arg, "#") == 0)
20460 {
20461 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20462 if (twin == NULL)
20463 nr = 0;
20464 }
20465 else
20466 {
20467 EMSG2(_(e_invexpr2), arg);
20468 nr = 0;
20469 }
20470 }
20471
20472 if (nr > 0)
20473 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20474 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020475 {
20476 if (wp == NULL)
20477 {
20478 /* didn't find it in this tabpage */
20479 nr = 0;
20480 break;
20481 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020482 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020483 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020484 return nr;
20485}
20486#endif
20487
20488/*
20489 * "tabpagewinnr()" function
20490 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020492f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020493{
20494 int nr = 1;
20495#ifdef FEAT_WINDOWS
20496 tabpage_T *tp;
20497
20498 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20499 if (tp == NULL)
20500 nr = 0;
20501 else
20502 nr = get_winnr(tp, &argvars[1]);
20503#endif
20504 rettv->vval.v_number = nr;
20505}
20506
20507
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020508/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020509 * "tagfiles()" function
20510 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020512f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020513{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020514 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020515 tagname_T tn;
20516 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020517
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020518 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020519 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020520 fname = alloc(MAXPATHL);
20521 if (fname == NULL)
20522 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020523
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020524 for (first = TRUE; ; first = FALSE)
20525 if (get_tagfname(&tn, first, fname) == FAIL
20526 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020527 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020528 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020529 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020530}
20531
20532/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020533 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020534 */
20535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020536f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020537{
20538 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020539
20540 tag_pattern = get_tv_string(&argvars[0]);
20541
20542 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020543 if (*tag_pattern == NUL)
20544 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020545
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020546 if (rettv_list_alloc(rettv) == OK)
20547 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020548}
20549
20550/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020551 * "tempname()" function
20552 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020554f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020555{
20556 static int x = 'A';
20557
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020558 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020559 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560
20561 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20562 * names. Skip 'I' and 'O', they are used for shell redirection. */
20563 do
20564 {
20565 if (x == 'Z')
20566 x = '0';
20567 else if (x == '9')
20568 x = 'A';
20569 else
20570 {
20571#ifdef EBCDIC
20572 if (x == 'I')
20573 x = 'J';
20574 else if (x == 'R')
20575 x = 'S';
20576 else
20577#endif
20578 ++x;
20579 }
20580 } while (x == 'I' || x == 'O');
20581}
20582
20583/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020584 * "test(list)" function: Just checking the walls...
20585 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020587f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020588{
20589 /* Used for unit testing. Change the code below to your liking. */
20590#if 0
20591 listitem_T *li;
20592 list_T *l;
20593 char_u *bad, *good;
20594
20595 if (argvars[0].v_type != VAR_LIST)
20596 return;
20597 l = argvars[0].vval.v_list;
20598 if (l == NULL)
20599 return;
20600 li = l->lv_first;
20601 if (li == NULL)
20602 return;
20603 bad = get_tv_string(&li->li_tv);
20604 li = li->li_next;
20605 if (li == NULL)
20606 return;
20607 good = get_tv_string(&li->li_tv);
20608 rettv->vval.v_number = test_edit_score(bad, good);
20609#endif
20610}
20611
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020612#ifdef FEAT_FLOAT
20613/*
20614 * "tan()" function
20615 */
20616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020617f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020618{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020619 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020620
20621 rettv->v_type = VAR_FLOAT;
20622 if (get_float_arg(argvars, &f) == OK)
20623 rettv->vval.v_float = tan(f);
20624 else
20625 rettv->vval.v_float = 0.0;
20626}
20627
20628/*
20629 * "tanh()" function
20630 */
20631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020632f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020633{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020634 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020635
20636 rettv->v_type = VAR_FLOAT;
20637 if (get_float_arg(argvars, &f) == OK)
20638 rettv->vval.v_float = tanh(f);
20639 else
20640 rettv->vval.v_float = 0.0;
20641}
20642#endif
20643
Bram Moolenaard52d9742005-08-21 22:20:28 +000020644/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 * "tolower(string)" function
20646 */
20647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020648f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020649{
20650 char_u *p;
20651
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020652 p = vim_strsave(get_tv_string(&argvars[0]));
20653 rettv->v_type = VAR_STRING;
20654 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655
20656 if (p != NULL)
20657 while (*p != NUL)
20658 {
20659#ifdef FEAT_MBYTE
20660 int l;
20661
20662 if (enc_utf8)
20663 {
20664 int c, lc;
20665
20666 c = utf_ptr2char(p);
20667 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020668 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 /* TODO: reallocate string when byte count changes. */
20670 if (utf_char2len(lc) == l)
20671 utf_char2bytes(lc, p);
20672 p += l;
20673 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020674 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675 p += l; /* skip multi-byte character */
20676 else
20677#endif
20678 {
20679 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20680 ++p;
20681 }
20682 }
20683}
20684
20685/*
20686 * "toupper(string)" function
20687 */
20688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020689f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020692 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693}
20694
20695/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020696 * "tr(string, fromstr, tostr)" function
20697 */
20698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020700{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020701 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020702 char_u *fromstr;
20703 char_u *tostr;
20704 char_u *p;
20705#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020706 int inlen;
20707 int fromlen;
20708 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020709 int idx;
20710 char_u *cpstr;
20711 int cplen;
20712 int first = TRUE;
20713#endif
20714 char_u buf[NUMBUFLEN];
20715 char_u buf2[NUMBUFLEN];
20716 garray_T ga;
20717
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020718 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020719 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20720 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020721
20722 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020723 rettv->v_type = VAR_STRING;
20724 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020725 if (fromstr == NULL || tostr == NULL)
20726 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020727 ga_init2(&ga, (int)sizeof(char), 80);
20728
20729#ifdef FEAT_MBYTE
20730 if (!has_mbyte)
20731#endif
20732 /* not multi-byte: fromstr and tostr must be the same length */
20733 if (STRLEN(fromstr) != STRLEN(tostr))
20734 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020735#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020736error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020737#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020738 EMSG2(_(e_invarg2), fromstr);
20739 ga_clear(&ga);
20740 return;
20741 }
20742
20743 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020744 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020745 {
20746#ifdef FEAT_MBYTE
20747 if (has_mbyte)
20748 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020749 inlen = (*mb_ptr2len)(in_str);
20750 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020751 cplen = inlen;
20752 idx = 0;
20753 for (p = fromstr; *p != NUL; p += fromlen)
20754 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020755 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020756 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020757 {
20758 for (p = tostr; *p != NUL; p += tolen)
20759 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020760 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020761 if (idx-- == 0)
20762 {
20763 cplen = tolen;
20764 cpstr = p;
20765 break;
20766 }
20767 }
20768 if (*p == NUL) /* tostr is shorter than fromstr */
20769 goto error;
20770 break;
20771 }
20772 ++idx;
20773 }
20774
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020775 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020776 {
20777 /* Check that fromstr and tostr have the same number of
20778 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020779 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020780 first = FALSE;
20781 for (p = tostr; *p != NUL; p += tolen)
20782 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020783 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020784 --idx;
20785 }
20786 if (idx != 0)
20787 goto error;
20788 }
20789
Bram Moolenaarcde88542015-08-11 19:14:00 +020020790 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020791 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020792 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020793
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020794 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020795 }
20796 else
20797#endif
20798 {
20799 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020800 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020801 if (p != NULL)
20802 ga_append(&ga, tostr[p - fromstr]);
20803 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020804 ga_append(&ga, *in_str);
20805 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020806 }
20807 }
20808
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020809 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020810 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020811 ga_append(&ga, NUL);
20812
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020813 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020814}
20815
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020816#ifdef FEAT_FLOAT
20817/*
20818 * "trunc({float})" function
20819 */
20820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020821f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020822{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020823 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020824
20825 rettv->v_type = VAR_FLOAT;
20826 if (get_float_arg(argvars, &f) == OK)
20827 /* trunc() is not in C90, use floor() or ceil() instead. */
20828 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20829 else
20830 rettv->vval.v_float = 0.0;
20831}
20832#endif
20833
Bram Moolenaar8299df92004-07-10 09:47:34 +000020834/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835 * "type(expr)" function
20836 */
20837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020838f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020840 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020841
20842 switch (argvars[0].v_type)
20843 {
20844 case VAR_NUMBER: n = 0; break;
20845 case VAR_STRING: n = 1; break;
20846 case VAR_FUNC: n = 2; break;
20847 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020848 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020849 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020850 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020851 if (argvars[0].vval.v_number == VVAL_FALSE
20852 || argvars[0].vval.v_number == VVAL_TRUE)
20853 n = 6;
20854 else
20855 n = 7;
20856 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020857 case VAR_JOB: n = 8; break;
20858 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020859 case VAR_UNKNOWN:
20860 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20861 n = -1;
20862 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020863 }
20864 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865}
20866
20867/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020868 * "undofile(name)" function
20869 */
20870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020871f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020872{
20873 rettv->v_type = VAR_STRING;
20874#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020875 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020876 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020877
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020878 if (*fname == NUL)
20879 {
20880 /* If there is no file name there will be no undo file. */
20881 rettv->vval.v_string = NULL;
20882 }
20883 else
20884 {
20885 char_u *ffname = FullName_save(fname, FALSE);
20886
20887 if (ffname != NULL)
20888 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20889 vim_free(ffname);
20890 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020891 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020892#else
20893 rettv->vval.v_string = NULL;
20894#endif
20895}
20896
20897/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020898 * "undotree()" function
20899 */
20900 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020901f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020902{
20903 if (rettv_dict_alloc(rettv) == OK)
20904 {
20905 dict_T *dict = rettv->vval.v_dict;
20906 list_T *list;
20907
Bram Moolenaar730cde92010-06-27 05:18:54 +020020908 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020909 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020910 dict_add_nr_str(dict, "save_last",
20911 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020912 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20913 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020914 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020915
20916 list = list_alloc();
20917 if (list != NULL)
20918 {
20919 u_eval_tree(curbuf->b_u_oldhead, list);
20920 dict_add_list(dict, "entries", list);
20921 }
20922 }
20923}
20924
20925/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020926 * "values(dict)" function
20927 */
20928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020929f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020930{
20931 dict_list(argvars, rettv, 1);
20932}
20933
20934/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 * "virtcol(string)" function
20936 */
20937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020938f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939{
20940 colnr_T vcol = 0;
20941 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020942 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020944 fp = var2fpos(&argvars[0], FALSE, &fnum);
20945 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20946 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020947 {
20948 getvvcol(curwin, fp, NULL, NULL, &vcol);
20949 ++vcol;
20950 }
20951
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020952 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953}
20954
20955/*
20956 * "visualmode()" function
20957 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020959f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961 char_u str[2];
20962
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020963 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 str[0] = curbuf->b_visual_mode_eval;
20965 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020966 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967
20968 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020969 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020971}
20972
20973/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020974 * "wildmenumode()" function
20975 */
20976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020977f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020978{
20979#ifdef FEAT_WILDMENU
20980 if (wild_menu_showing)
20981 rettv->vval.v_number = 1;
20982#endif
20983}
20984
20985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986 * "winbufnr(nr)" function
20987 */
20988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020989f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990{
20991 win_T *wp;
20992
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020993 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020996 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020997 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020998}
20999
21000/*
21001 * "wincol()" function
21002 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021004f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021005{
21006 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021007 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021008}
21009
21010/*
21011 * "winheight(nr)" function
21012 */
21013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021014f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021015{
21016 win_T *wp;
21017
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021018 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021019 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021020 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021021 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021022 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023}
21024
21025/*
21026 * "winline()" function
21027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021029f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030{
21031 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021032 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021033}
21034
21035/*
21036 * "winnr()" function
21037 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021039f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021040{
21041 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021042
Bram Moolenaar071d4272004-06-13 20:20:40 +000021043#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021044 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021045#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021046 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021047}
21048
21049/*
21050 * "winrestcmd()" function
21051 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021053f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021054{
21055#ifdef FEAT_WINDOWS
21056 win_T *wp;
21057 int winnr = 1;
21058 garray_T ga;
21059 char_u buf[50];
21060
21061 ga_init2(&ga, (int)sizeof(char), 70);
21062 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21063 {
21064 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21065 ga_concat(&ga, buf);
21066# ifdef FEAT_VERTSPLIT
21067 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21068 ga_concat(&ga, buf);
21069# endif
21070 ++winnr;
21071 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021072 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021073
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021074 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021075#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021076 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021077#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021078 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021079}
21080
21081/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021082 * "winrestview()" function
21083 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021084 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021085f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021086{
21087 dict_T *dict;
21088
21089 if (argvars[0].v_type != VAR_DICT
21090 || (dict = argvars[0].vval.v_dict) == NULL)
21091 EMSG(_(e_invarg));
21092 else
21093 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021094 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21095 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21096 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21097 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021098#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021099 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21100 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021101#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021102 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21103 {
21104 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21105 curwin->w_set_curswant = FALSE;
21106 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021107
Bram Moolenaar82c25852014-05-28 16:47:16 +020021108 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21109 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021110#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021111 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21112 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021113#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021114 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21115 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21116 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21117 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021118
21119 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021120 win_new_height(curwin, curwin->w_height);
21121# ifdef FEAT_VERTSPLIT
21122 win_new_width(curwin, W_WIDTH(curwin));
21123# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021124 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021125
Bram Moolenaarb851a962014-10-31 15:45:52 +010021126 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021127 curwin->w_topline = 1;
21128 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21129 curwin->w_topline = curbuf->b_ml.ml_line_count;
21130#ifdef FEAT_DIFF
21131 check_topfill(curwin, TRUE);
21132#endif
21133 }
21134}
21135
21136/*
21137 * "winsaveview()" function
21138 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021140f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021141{
21142 dict_T *dict;
21143
Bram Moolenaara800b422010-06-27 01:15:55 +020021144 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021145 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021146 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021147
21148 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21149 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21150#ifdef FEAT_VIRTUALEDIT
21151 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21152#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021153 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021154 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21155
21156 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21157#ifdef FEAT_DIFF
21158 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21159#endif
21160 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21161 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21162}
21163
21164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 * "winwidth(nr)" function
21166 */
21167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021168f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021169{
21170 win_T *wp;
21171
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021172 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021173 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021174 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021175 else
21176#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021177 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021179 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180#endif
21181}
21182
Bram Moolenaar071d4272004-06-13 20:20:40 +000021183/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021184 * "wordcount()" function
21185 */
21186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021187f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021188{
21189 if (rettv_dict_alloc(rettv) == FAIL)
21190 return;
21191 cursor_pos_info(rettv->vval.v_dict);
21192}
21193
21194/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021195 * Write list of strings to file
21196 */
21197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021198write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021199{
21200 listitem_T *li;
21201 int c;
21202 int ret = OK;
21203 char_u *s;
21204
21205 for (li = list->lv_first; li != NULL; li = li->li_next)
21206 {
21207 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21208 {
21209 if (*s == '\n')
21210 c = putc(NUL, fd);
21211 else
21212 c = putc(*s, fd);
21213 if (c == EOF)
21214 {
21215 ret = FAIL;
21216 break;
21217 }
21218 }
21219 if (!binary || li->li_next != NULL)
21220 if (putc('\n', fd) == EOF)
21221 {
21222 ret = FAIL;
21223 break;
21224 }
21225 if (ret == FAIL)
21226 {
21227 EMSG(_(e_write));
21228 break;
21229 }
21230 }
21231 return ret;
21232}
21233
21234/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021235 * "writefile()" function
21236 */
21237 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021238f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021239{
21240 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021241 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021242 char_u *fname;
21243 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021244 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021245
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021246 if (check_restricted() || check_secure())
21247 return;
21248
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021249 if (argvars[0].v_type != VAR_LIST)
21250 {
21251 EMSG2(_(e_listarg), "writefile()");
21252 return;
21253 }
21254 if (argvars[0].vval.v_list == NULL)
21255 return;
21256
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021257 if (argvars[2].v_type != VAR_UNKNOWN)
21258 {
21259 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21260 binary = TRUE;
21261 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21262 append = TRUE;
21263 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021264
21265 /* Always open the file in binary mode, library functions have a mind of
21266 * their own about CR-LF conversion. */
21267 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021268 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21269 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021270 {
21271 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21272 ret = -1;
21273 }
21274 else
21275 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021276 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21277 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021278 fclose(fd);
21279 }
21280
21281 rettv->vval.v_number = ret;
21282}
21283
21284/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021285 * "xor(expr, expr)" function
21286 */
21287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021288f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021289{
21290 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21291 ^ get_tv_number_chk(&argvars[1], NULL);
21292}
21293
21294
21295/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021296 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021297 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 */
21299 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021300var2fpos(
21301 typval_T *varp,
21302 int dollar_lnum, /* TRUE when $ is last line */
21303 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021304{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021305 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021306 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021307 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021308
Bram Moolenaara5525202006-03-02 22:52:09 +000021309 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021310 if (varp->v_type == VAR_LIST)
21311 {
21312 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021313 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021314 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021315 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021316
21317 l = varp->vval.v_list;
21318 if (l == NULL)
21319 return NULL;
21320
21321 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021322 pos.lnum = list_find_nr(l, 0L, &error);
21323 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021324 return NULL; /* invalid line number */
21325
21326 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021327 pos.col = list_find_nr(l, 1L, &error);
21328 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021329 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021330 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021331
21332 /* We accept "$" for the column number: last column. */
21333 li = list_find(l, 1L);
21334 if (li != NULL && li->li_tv.v_type == VAR_STRING
21335 && li->li_tv.vval.v_string != NULL
21336 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21337 pos.col = len + 1;
21338
Bram Moolenaara5525202006-03-02 22:52:09 +000021339 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021340 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021341 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021342 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021343
Bram Moolenaara5525202006-03-02 22:52:09 +000021344#ifdef FEAT_VIRTUALEDIT
21345 /* Get the virtual offset. Defaults to zero. */
21346 pos.coladd = list_find_nr(l, 2L, &error);
21347 if (error)
21348 pos.coladd = 0;
21349#endif
21350
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021351 return &pos;
21352 }
21353
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021354 name = get_tv_string_chk(varp);
21355 if (name == NULL)
21356 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021357 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021358 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021359 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21360 {
21361 if (VIsual_active)
21362 return &VIsual;
21363 return &curwin->w_cursor;
21364 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021365 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021366 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021367 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021368 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21369 return NULL;
21370 return pp;
21371 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021372
21373#ifdef FEAT_VIRTUALEDIT
21374 pos.coladd = 0;
21375#endif
21376
Bram Moolenaar477933c2007-07-17 14:32:23 +000021377 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021378 {
21379 pos.col = 0;
21380 if (name[1] == '0') /* "w0": first visible line */
21381 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021382 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021383 pos.lnum = curwin->w_topline;
21384 return &pos;
21385 }
21386 else if (name[1] == '$') /* "w$": last visible line */
21387 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021388 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021389 pos.lnum = curwin->w_botline - 1;
21390 return &pos;
21391 }
21392 }
21393 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021394 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021395 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396 {
21397 pos.lnum = curbuf->b_ml.ml_line_count;
21398 pos.col = 0;
21399 }
21400 else
21401 {
21402 pos.lnum = curwin->w_cursor.lnum;
21403 pos.col = (colnr_T)STRLEN(ml_get_curline());
21404 }
21405 return &pos;
21406 }
21407 return NULL;
21408}
21409
21410/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021411 * Convert list in "arg" into a position and optional file number.
21412 * When "fnump" is NULL there is no file number, only 3 items.
21413 * Note that the column is passed on as-is, the caller may want to decrement
21414 * it to use 1 for the first column.
21415 * Return FAIL when conversion is not possible, doesn't check the position for
21416 * validity.
21417 */
21418 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021419list2fpos(
21420 typval_T *arg,
21421 pos_T *posp,
21422 int *fnump,
21423 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021424{
21425 list_T *l = arg->vval.v_list;
21426 long i = 0;
21427 long n;
21428
Bram Moolenaar493c1782014-05-28 14:34:46 +020021429 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21430 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021431 if (arg->v_type != VAR_LIST
21432 || l == NULL
21433 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021434 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021435 return FAIL;
21436
21437 if (fnump != NULL)
21438 {
21439 n = list_find_nr(l, i++, NULL); /* fnum */
21440 if (n < 0)
21441 return FAIL;
21442 if (n == 0)
21443 n = curbuf->b_fnum; /* current buffer */
21444 *fnump = n;
21445 }
21446
21447 n = list_find_nr(l, i++, NULL); /* lnum */
21448 if (n < 0)
21449 return FAIL;
21450 posp->lnum = n;
21451
21452 n = list_find_nr(l, i++, NULL); /* col */
21453 if (n < 0)
21454 return FAIL;
21455 posp->col = n;
21456
21457#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021458 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021459 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021460 posp->coladd = 0;
21461 else
21462 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021463#endif
21464
Bram Moolenaar493c1782014-05-28 14:34:46 +020021465 if (curswantp != NULL)
21466 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21467
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021468 return OK;
21469}
21470
21471/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021472 * Get the length of an environment variable name.
21473 * Advance "arg" to the first character after the name.
21474 * Return 0 for error.
21475 */
21476 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021477get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478{
21479 char_u *p;
21480 int len;
21481
21482 for (p = *arg; vim_isIDc(*p); ++p)
21483 ;
21484 if (p == *arg) /* no name found */
21485 return 0;
21486
21487 len = (int)(p - *arg);
21488 *arg = p;
21489 return len;
21490}
21491
21492/*
21493 * Get the length of the name of a function or internal variable.
21494 * "arg" is advanced to the first non-white character after the name.
21495 * Return 0 if something is wrong.
21496 */
21497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021498get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499{
21500 char_u *p;
21501 int len;
21502
21503 /* Find the end of the name. */
21504 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021505 {
21506 if (*p == ':')
21507 {
21508 /* "s:" is start of "s:var", but "n:" is not and can be used in
21509 * slice "[n:]". Also "xx:" is not a namespace. */
21510 len = (int)(p - *arg);
21511 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21512 || len > 1)
21513 break;
21514 }
21515 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516 if (p == *arg) /* no name found */
21517 return 0;
21518
21519 len = (int)(p - *arg);
21520 *arg = skipwhite(p);
21521
21522 return len;
21523}
21524
21525/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021526 * Get the length of the name of a variable or function.
21527 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021529 * Return -1 if curly braces expansion failed.
21530 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531 * If the name contains 'magic' {}'s, expand them and return the
21532 * expanded name in an allocated string via 'alias' - caller must free.
21533 */
21534 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021535get_name_len(
21536 char_u **arg,
21537 char_u **alias,
21538 int evaluate,
21539 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021540{
21541 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021542 char_u *p;
21543 char_u *expr_start;
21544 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021545
21546 *alias = NULL; /* default to no alias */
21547
21548 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21549 && (*arg)[2] == (int)KE_SNR)
21550 {
21551 /* hard coded <SNR>, already translated */
21552 *arg += 3;
21553 return get_id_len(arg) + 3;
21554 }
21555 len = eval_fname_script(*arg);
21556 if (len > 0)
21557 {
21558 /* literal "<SID>", "s:" or "<SNR>" */
21559 *arg += len;
21560 }
21561
Bram Moolenaar071d4272004-06-13 20:20:40 +000021562 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021563 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021565 p = find_name_end(*arg, &expr_start, &expr_end,
21566 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567 if (expr_start != NULL)
21568 {
21569 char_u *temp_string;
21570
21571 if (!evaluate)
21572 {
21573 len += (int)(p - *arg);
21574 *arg = skipwhite(p);
21575 return len;
21576 }
21577
21578 /*
21579 * Include any <SID> etc in the expanded string:
21580 * Thus the -len here.
21581 */
21582 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21583 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021584 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 *alias = temp_string;
21586 *arg = skipwhite(p);
21587 return (int)STRLEN(temp_string);
21588 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589
21590 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021591 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 EMSG2(_(e_invexpr2), *arg);
21593
21594 return len;
21595}
21596
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021597/*
21598 * Find the end of a variable or function name, taking care of magic braces.
21599 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21600 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021601 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021602 * Return a pointer to just after the name. Equal to "arg" if there is no
21603 * valid name.
21604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021605 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021606find_name_end(
21607 char_u *arg,
21608 char_u **expr_start,
21609 char_u **expr_end,
21610 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021611{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021612 int mb_nest = 0;
21613 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021614 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021615 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021617 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021619 *expr_start = NULL;
21620 *expr_end = NULL;
21621 }
21622
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021623 /* Quick check for valid starting character. */
21624 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21625 return arg;
21626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021627 for (p = arg; *p != NUL
21628 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021629 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021630 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021631 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021632 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021633 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021634 if (*p == '\'')
21635 {
21636 /* skip over 'string' to avoid counting [ and ] inside it. */
21637 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21638 ;
21639 if (*p == NUL)
21640 break;
21641 }
21642 else if (*p == '"')
21643 {
21644 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21645 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21646 if (*p == '\\' && p[1] != NUL)
21647 ++p;
21648 if (*p == NUL)
21649 break;
21650 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021651 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21652 {
21653 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021654 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021655 len = (int)(p - arg);
21656 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021657 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021658 break;
21659 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021660
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021661 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021662 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021663 if (*p == '[')
21664 ++br_nest;
21665 else if (*p == ']')
21666 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021667 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021668
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021669 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021670 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021671 if (*p == '{')
21672 {
21673 mb_nest++;
21674 if (expr_start != NULL && *expr_start == NULL)
21675 *expr_start = p;
21676 }
21677 else if (*p == '}')
21678 {
21679 mb_nest--;
21680 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21681 *expr_end = p;
21682 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021683 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021684 }
21685
21686 return p;
21687}
21688
21689/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021690 * Expands out the 'magic' {}'s in a variable/function name.
21691 * Note that this can call itself recursively, to deal with
21692 * constructs like foo{bar}{baz}{bam}
21693 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21694 * "in_start" ^
21695 * "expr_start" ^
21696 * "expr_end" ^
21697 * "in_end" ^
21698 *
21699 * Returns a new allocated string, which the caller must free.
21700 * Returns NULL for failure.
21701 */
21702 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703make_expanded_name(
21704 char_u *in_start,
21705 char_u *expr_start,
21706 char_u *expr_end,
21707 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021708{
21709 char_u c1;
21710 char_u *retval = NULL;
21711 char_u *temp_result;
21712 char_u *nextcmd = NULL;
21713
21714 if (expr_end == NULL || in_end == NULL)
21715 return NULL;
21716 *expr_start = NUL;
21717 *expr_end = NUL;
21718 c1 = *in_end;
21719 *in_end = NUL;
21720
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021721 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021722 if (temp_result != NULL && nextcmd == NULL)
21723 {
21724 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21725 + (in_end - expr_end) + 1));
21726 if (retval != NULL)
21727 {
21728 STRCPY(retval, in_start);
21729 STRCAT(retval, temp_result);
21730 STRCAT(retval, expr_end + 1);
21731 }
21732 }
21733 vim_free(temp_result);
21734
21735 *in_end = c1; /* put char back for error messages */
21736 *expr_start = '{';
21737 *expr_end = '}';
21738
21739 if (retval != NULL)
21740 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021741 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021742 if (expr_start != NULL)
21743 {
21744 /* Further expansion! */
21745 temp_result = make_expanded_name(retval, expr_start,
21746 expr_end, temp_result);
21747 vim_free(retval);
21748 retval = temp_result;
21749 }
21750 }
21751
21752 return retval;
21753}
21754
21755/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021757 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021758 */
21759 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021760eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021762 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21763}
21764
21765/*
21766 * Return TRUE if character "c" can be used as the first character in a
21767 * variable or function name (excluding '{' and '}').
21768 */
21769 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021771{
21772 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773}
21774
21775/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 * Set number v: variable to "val".
21777 */
21778 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021779set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021781 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782}
21783
21784/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021785 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021786 */
21787 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021788get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021789{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021790 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021791}
21792
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021793/*
21794 * Get string v: variable value. Uses a static buffer, can only be used once.
21795 */
21796 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021797get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021798{
21799 return get_tv_string(&vimvars[idx].vv_tv);
21800}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021801
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021803 * Get List v: variable value. Caller must take care of reference count when
21804 * needed.
21805 */
21806 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021807get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021808{
21809 return vimvars[idx].vv_list;
21810}
21811
21812/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021813 * Set v:char to character "c".
21814 */
21815 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021816set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021817{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021818 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021819
21820#ifdef FEAT_MBYTE
21821 if (has_mbyte)
21822 buf[(*mb_char2bytes)(c, buf)] = NUL;
21823 else
21824#endif
21825 {
21826 buf[0] = c;
21827 buf[1] = NUL;
21828 }
21829 set_vim_var_string(VV_CHAR, buf, -1);
21830}
21831
21832/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021833 * Set v:count to "count" and v:count1 to "count1".
21834 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021835 */
21836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021837set_vcount(
21838 long count,
21839 long count1,
21840 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021841{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021842 if (set_prevcount)
21843 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021844 vimvars[VV_COUNT].vv_nr = count;
21845 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021846}
21847
21848/*
21849 * Set string v: variable to a copy of "val".
21850 */
21851 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021852set_vim_var_string(
21853 int idx,
21854 char_u *val,
21855 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856{
Bram Moolenaara542c682016-01-31 16:28:04 +010021857 clear_tv(&vimvars[idx].vv_di.di_tv);
21858 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021859 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021860 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021861 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021862 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021863 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021864 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021865}
21866
21867/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021868 * Set List v: variable to "val".
21869 */
21870 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021871set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021872{
Bram Moolenaara542c682016-01-31 16:28:04 +010021873 clear_tv(&vimvars[idx].vv_di.di_tv);
21874 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021875 vimvars[idx].vv_list = val;
21876 if (val != NULL)
21877 ++val->lv_refcount;
21878}
21879
21880/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021881 * Set Dictionary v: variable to "val".
21882 */
21883 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021884set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021885{
21886 int todo;
21887 hashitem_T *hi;
21888
Bram Moolenaara542c682016-01-31 16:28:04 +010021889 clear_tv(&vimvars[idx].vv_di.di_tv);
21890 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021891 vimvars[idx].vv_dict = val;
21892 if (val != NULL)
21893 {
21894 ++val->dv_refcount;
21895
21896 /* Set readonly */
21897 todo = (int)val->dv_hashtab.ht_used;
21898 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21899 {
21900 if (HASHITEM_EMPTY(hi))
21901 continue;
21902 --todo;
21903 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21904 }
21905 }
21906}
21907
21908/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 * Set v:register if needed.
21910 */
21911 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021912set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021913{
21914 char_u regname;
21915
21916 if (c == 0 || c == ' ')
21917 regname = '"';
21918 else
21919 regname = c;
21920 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021921 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 set_vim_var_string(VV_REG, &regname, 1);
21923}
21924
21925/*
21926 * Get or set v:exception. If "oldval" == NULL, return the current value.
21927 * Otherwise, restore the value to "oldval" and return NULL.
21928 * Must always be called in pairs to save and restore v:exception! Does not
21929 * take care of memory allocations.
21930 */
21931 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933{
21934 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021935 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936
Bram Moolenaare9a41262005-01-15 22:18:47 +000021937 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938 return NULL;
21939}
21940
21941/*
21942 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21943 * Otherwise, restore the value to "oldval" and return NULL.
21944 * Must always be called in pairs to save and restore v:throwpoint! Does not
21945 * take care of memory allocations.
21946 */
21947 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021948v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021949{
21950 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021951 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021952
Bram Moolenaare9a41262005-01-15 22:18:47 +000021953 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021954 return NULL;
21955}
21956
21957#if defined(FEAT_AUTOCMD) || defined(PROTO)
21958/*
21959 * Set v:cmdarg.
21960 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21961 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21962 * Must always be called in pairs!
21963 */
21964 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021965set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966{
21967 char_u *oldval;
21968 char_u *newval;
21969 unsigned len;
21970
Bram Moolenaare9a41262005-01-15 22:18:47 +000021971 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021972 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021973 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021974 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021975 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021976 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 }
21978
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021979 if (eap->force_bin == FORCE_BIN)
21980 len = 6;
21981 else if (eap->force_bin == FORCE_NOBIN)
21982 len = 8;
21983 else
21984 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021985
21986 if (eap->read_edit)
21987 len += 7;
21988
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021989 if (eap->force_ff != 0)
21990 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21991# ifdef FEAT_MBYTE
21992 if (eap->force_enc != 0)
21993 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021994 if (eap->bad_char != 0)
21995 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021996# endif
21997
21998 newval = alloc(len + 1);
21999 if (newval == NULL)
22000 return NULL;
22001
22002 if (eap->force_bin == FORCE_BIN)
22003 sprintf((char *)newval, " ++bin");
22004 else if (eap->force_bin == FORCE_NOBIN)
22005 sprintf((char *)newval, " ++nobin");
22006 else
22007 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022008
22009 if (eap->read_edit)
22010 STRCAT(newval, " ++edit");
22011
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022012 if (eap->force_ff != 0)
22013 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
22014 eap->cmd + eap->force_ff);
22015# ifdef FEAT_MBYTE
22016 if (eap->force_enc != 0)
22017 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
22018 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020022019 if (eap->bad_char == BAD_KEEP)
22020 STRCPY(newval + STRLEN(newval), " ++bad=keep");
22021 else if (eap->bad_char == BAD_DROP)
22022 STRCPY(newval + STRLEN(newval), " ++bad=drop");
22023 else if (eap->bad_char != 0)
22024 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022025# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000022026 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000022027 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028}
22029#endif
22030
22031/*
22032 * Get the value of internal variable "name".
22033 * Return OK or FAIL.
22034 */
22035 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022036get_var_tv(
22037 char_u *name,
22038 int len, /* length of "name" */
22039 typval_T *rettv, /* NULL when only checking existence */
22040 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22041 int verbose, /* may give error message */
22042 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022043{
22044 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022045 typval_T *tv = NULL;
22046 typval_T atv;
22047 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022048 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022049
22050 /* truncate the name, so that we can use strcmp() */
22051 cc = name[len];
22052 name[len] = NUL;
22053
22054 /*
22055 * Check for "b:changedtick".
22056 */
22057 if (STRCMP(name, "b:changedtick") == 0)
22058 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022059 atv.v_type = VAR_NUMBER;
22060 atv.vval.v_number = curbuf->b_changedtick;
22061 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022062 }
22063
22064 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022065 * Check for user-defined variables.
22066 */
22067 else
22068 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022069 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022070 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022071 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022072 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022073 if (dip != NULL)
22074 *dip = v;
22075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022076 }
22077
Bram Moolenaare9a41262005-01-15 22:18:47 +000022078 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022079 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022080 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022081 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082 ret = FAIL;
22083 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022084 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022085 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086
22087 name[len] = cc;
22088
22089 return ret;
22090}
22091
22092/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022093 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22094 * Also handle function call with Funcref variable: func(expr)
22095 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22096 */
22097 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022098handle_subscript(
22099 char_u **arg,
22100 typval_T *rettv,
22101 int evaluate, /* do more than finding the end */
22102 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022103{
22104 int ret = OK;
22105 dict_T *selfdict = NULL;
22106 char_u *s;
22107 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022108 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022109
22110 while (ret == OK
22111 && (**arg == '['
22112 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022113 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022114 && !vim_iswhite(*(*arg - 1)))
22115 {
22116 if (**arg == '(')
22117 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022118 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022119 if (evaluate)
22120 {
22121 functv = *rettv;
22122 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022123
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022124 /* Invoke the function. Recursive! */
22125 s = functv.vval.v_string;
22126 }
22127 else
22128 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022129 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022130 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22131 &len, evaluate, selfdict);
22132
22133 /* Clear the funcref afterwards, so that deleting it while
22134 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022135 if (evaluate)
22136 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022137
22138 /* Stop the expression evaluation when immediately aborting on
22139 * error, or when an interrupt occurred or an exception was thrown
22140 * but not caught. */
22141 if (aborting())
22142 {
22143 if (ret == OK)
22144 clear_tv(rettv);
22145 ret = FAIL;
22146 }
22147 dict_unref(selfdict);
22148 selfdict = NULL;
22149 }
22150 else /* **arg == '[' || **arg == '.' */
22151 {
22152 dict_unref(selfdict);
22153 if (rettv->v_type == VAR_DICT)
22154 {
22155 selfdict = rettv->vval.v_dict;
22156 if (selfdict != NULL)
22157 ++selfdict->dv_refcount;
22158 }
22159 else
22160 selfdict = NULL;
22161 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22162 {
22163 clear_tv(rettv);
22164 ret = FAIL;
22165 }
22166 }
22167 }
22168 dict_unref(selfdict);
22169 return ret;
22170}
22171
22172/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022173 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 * value).
22175 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022176 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022177alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022178{
Bram Moolenaar33570922005-01-25 22:26:29 +000022179 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022180}
22181
22182/*
22183 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 * The string "s" must have been allocated, it is consumed.
22185 * Return NULL for out of memory, the variable otherwise.
22186 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022187 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022188alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022189{
Bram Moolenaar33570922005-01-25 22:26:29 +000022190 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022192 rettv = alloc_tv();
22193 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022195 rettv->v_type = VAR_STRING;
22196 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022197 }
22198 else
22199 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022200 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201}
22202
22203/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022204 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022206 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022207free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022208{
22209 if (varp != NULL)
22210 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022211 switch (varp->v_type)
22212 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022213 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022214 func_unref(varp->vval.v_string);
22215 /*FALLTHROUGH*/
22216 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022217 vim_free(varp->vval.v_string);
22218 break;
22219 case VAR_LIST:
22220 list_unref(varp->vval.v_list);
22221 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022222 case VAR_DICT:
22223 dict_unref(varp->vval.v_dict);
22224 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022225 case VAR_JOB:
22226#ifdef FEAT_JOB
22227 job_unref(varp->vval.v_job);
22228 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022229#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022230 case VAR_CHANNEL:
22231#ifdef FEAT_CHANNEL
22232 channel_unref(varp->vval.v_channel);
22233 break;
22234#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022235 case VAR_NUMBER:
22236 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022237 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022238 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022239 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022241 vim_free(varp);
22242 }
22243}
22244
22245/*
22246 * Free the memory for a variable value and set the value to NULL or 0.
22247 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022248 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022249clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022250{
22251 if (varp != NULL)
22252 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022253 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022254 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022255 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022256 func_unref(varp->vval.v_string);
22257 /*FALLTHROUGH*/
22258 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022259 vim_free(varp->vval.v_string);
22260 varp->vval.v_string = NULL;
22261 break;
22262 case VAR_LIST:
22263 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022264 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022265 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022266 case VAR_DICT:
22267 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022268 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022269 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022270 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022271 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272 varp->vval.v_number = 0;
22273 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022274 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022275#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022276 varp->vval.v_float = 0.0;
22277 break;
22278#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022279 case VAR_JOB:
22280#ifdef FEAT_JOB
22281 job_unref(varp->vval.v_job);
22282 varp->vval.v_job = NULL;
22283#endif
22284 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022285 case VAR_CHANNEL:
22286#ifdef FEAT_CHANNEL
22287 channel_unref(varp->vval.v_channel);
22288 varp->vval.v_channel = NULL;
22289#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022290 case VAR_UNKNOWN:
22291 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022292 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022293 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 }
22295}
22296
22297/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022298 * Set the value of a variable to NULL without freeing items.
22299 */
22300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022301init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022302{
22303 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022304 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022305}
22306
22307/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 * Get the number value of a variable.
22309 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022310 * For incompatible types, return 0.
22311 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22312 * caller of incompatible types: it sets *denote to TRUE if "denote"
22313 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 */
22315 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022316get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022317{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022318 int error = FALSE;
22319
22320 return get_tv_number_chk(varp, &error); /* return 0L on error */
22321}
22322
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022323 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022324get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022325{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022326 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022328 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022330 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022331 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022332 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022333#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022334 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022335 break;
22336#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022337 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022338 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022339 break;
22340 case VAR_STRING:
22341 if (varp->vval.v_string != NULL)
22342 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022343 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022344 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022345 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022346 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022347 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022348 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022349 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022350 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022351 case VAR_SPECIAL:
22352 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22353 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022354 case VAR_JOB:
22355#ifdef FEAT_JOB
22356 EMSG(_("E910: Using a Job as a Number"));
22357 break;
22358#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022359 case VAR_CHANNEL:
22360#ifdef FEAT_CHANNEL
22361 EMSG(_("E913: Using a Channel as a Number"));
22362 break;
22363#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022364 case VAR_UNKNOWN:
22365 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022366 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022368 if (denote == NULL) /* useful for values that must be unsigned */
22369 n = -1;
22370 else
22371 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022372 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022373}
22374
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022375#ifdef FEAT_FLOAT
22376 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022377get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022378{
22379 switch (varp->v_type)
22380 {
22381 case VAR_NUMBER:
22382 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022383 case VAR_FLOAT:
22384 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022385 case VAR_FUNC:
22386 EMSG(_("E891: Using a Funcref as a Float"));
22387 break;
22388 case VAR_STRING:
22389 EMSG(_("E892: Using a String as a Float"));
22390 break;
22391 case VAR_LIST:
22392 EMSG(_("E893: Using a List as a Float"));
22393 break;
22394 case VAR_DICT:
22395 EMSG(_("E894: Using a Dictionary as a Float"));
22396 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022397 case VAR_SPECIAL:
22398 EMSG(_("E907: Using a special value as a Float"));
22399 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022400 case VAR_JOB:
22401# ifdef FEAT_JOB
22402 EMSG(_("E911: Using a Job as a Float"));
22403 break;
22404# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022405 case VAR_CHANNEL:
22406# ifdef FEAT_CHANNEL
22407 EMSG(_("E914: Using a Channel as a Float"));
22408 break;
22409# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022410 case VAR_UNKNOWN:
22411 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022412 break;
22413 }
22414 return 0;
22415}
22416#endif
22417
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022419 * Get the lnum from the first argument.
22420 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022421 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022422 */
22423 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022424get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022425{
Bram Moolenaar33570922005-01-25 22:26:29 +000022426 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022427 linenr_T lnum;
22428
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022429 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 if (lnum == 0) /* no valid number, try using line() */
22431 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022432 rettv.v_type = VAR_NUMBER;
22433 f_line(argvars, &rettv);
22434 lnum = rettv.vval.v_number;
22435 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 }
22437 return lnum;
22438}
22439
22440/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022441 * Get the lnum from the first argument.
22442 * Also accepts "$", then "buf" is used.
22443 * Returns 0 on error.
22444 */
22445 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022446get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022447{
22448 if (argvars[0].v_type == VAR_STRING
22449 && argvars[0].vval.v_string != NULL
22450 && argvars[0].vval.v_string[0] == '$'
22451 && buf != NULL)
22452 return buf->b_ml.ml_line_count;
22453 return get_tv_number_chk(&argvars[0], NULL);
22454}
22455
22456/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457 * Get the string value of a variable.
22458 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022459 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22460 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022461 * If the String variable has never been set, return an empty string.
22462 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022463 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22464 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022465 */
22466 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022467get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468{
22469 static char_u mybuf[NUMBUFLEN];
22470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022471 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022472}
22473
22474 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022475get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022476{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022477 char_u *res = get_tv_string_buf_chk(varp, buf);
22478
22479 return res != NULL ? res : (char_u *)"";
22480}
22481
Bram Moolenaar7d647822014-04-05 21:28:56 +020022482/*
22483 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22484 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022485 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022486get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022487{
22488 static char_u mybuf[NUMBUFLEN];
22489
22490 return get_tv_string_buf_chk(varp, mybuf);
22491}
22492
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022493 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022494get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022495{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022496 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022498 case VAR_NUMBER:
22499 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22500 return buf;
22501 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022502 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022503 break;
22504 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022505 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022506 break;
22507 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022508 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022509 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022511#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022512 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022513 break;
22514#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022515 case VAR_STRING:
22516 if (varp->vval.v_string != NULL)
22517 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022518 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022519 case VAR_SPECIAL:
22520 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22521 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022522 case VAR_JOB:
22523#ifdef FEAT_JOB
22524 {
22525 job_T *job = varp->vval.v_job;
22526 char *status = job->jv_status == JOB_FAILED ? "fail"
22527 : job->jv_status == JOB_ENDED ? "dead"
22528 : "run";
22529# ifdef UNIX
22530 vim_snprintf((char *)buf, NUMBUFLEN,
22531 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022532# elif defined(WIN32)
22533 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022534 "process %ld %s",
22535 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022536 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022537# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022538 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022539 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22540# endif
22541 return buf;
22542 }
22543#endif
22544 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022545 case VAR_CHANNEL:
22546#ifdef FEAT_CHANNEL
22547 {
22548 channel_T *channel = varp->vval.v_channel;
22549 char *status = channel_status(channel);
22550
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022551 if (channel == NULL)
22552 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22553 else
22554 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022555 "channel %d %s", channel->ch_id, status);
22556 return buf;
22557 }
22558#endif
22559 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022560 case VAR_UNKNOWN:
22561 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022562 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022563 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022564 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565}
22566
22567/*
22568 * Find variable "name" in the list of variables.
22569 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022570 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022571 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022574 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579
Bram Moolenaara7043832005-01-21 11:56:39 +000022580 ht = find_var_ht(name, &varname);
22581 if (htp != NULL)
22582 *htp = ht;
22583 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022584 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022585 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586}
22587
22588/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022589 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022590 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022591 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022593find_var_in_ht(
22594 hashtab_T *ht,
22595 int htname,
22596 char_u *varname,
22597 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022598{
Bram Moolenaar33570922005-01-25 22:26:29 +000022599 hashitem_T *hi;
22600
22601 if (*varname == NUL)
22602 {
22603 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022604 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022605 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022606 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022607 case 'g': return &globvars_var;
22608 case 'v': return &vimvars_var;
22609 case 'b': return &curbuf->b_bufvar;
22610 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022611#ifdef FEAT_WINDOWS
22612 case 't': return &curtab->tp_winvar;
22613#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022614 case 'l': return current_funccal == NULL
22615 ? NULL : &current_funccal->l_vars_var;
22616 case 'a': return current_funccal == NULL
22617 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022618 }
22619 return NULL;
22620 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022621
22622 hi = hash_find(ht, varname);
22623 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022624 {
22625 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022626 * worked find the variable again. Don't auto-load a script if it was
22627 * loaded already, otherwise it would be loaded every time when
22628 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022629 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022630 {
22631 /* Note: script_autoload() may make "hi" invalid. It must either
22632 * be obtained again or not used. */
22633 if (!script_autoload(varname, FALSE) || aborting())
22634 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022635 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022636 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022637 if (HASHITEM_EMPTY(hi))
22638 return NULL;
22639 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022641}
22642
22643/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022644 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022645 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022646 * Set "varname" to the start of name without ':'.
22647 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022648 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022649find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022651 hashitem_T *hi;
22652
Bram Moolenaar73627d02015-08-11 15:46:09 +020022653 if (name[0] == NUL)
22654 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 if (name[1] != ':')
22656 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022657 /* The name must not start with a colon or #. */
22658 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022659 return NULL;
22660 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022661
22662 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022663 hi = hash_find(&compat_hashtab, name);
22664 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022665 return &compat_hashtab;
22666
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022668 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022669 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 }
22671 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022672 if (*name == 'g') /* global variable */
22673 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022674 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22675 */
22676 if (vim_strchr(name + 2, ':') != NULL
22677 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022678 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022679 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022680 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022682 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022683#ifdef FEAT_WINDOWS
22684 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022685 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022686#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 if (*name == 'v') /* v: variable */
22688 return &vimvarht;
22689 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022690 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022691 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022692 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022693 if (*name == 's' /* script variable */
22694 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22695 return &SCRIPT_VARS(current_SID);
22696 return NULL;
22697}
22698
22699/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022700 * Get function call environment based on bactrace debug level
22701 */
22702 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022703get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022704{
22705 int i;
22706 funccall_T *funccal;
22707 funccall_T *temp_funccal;
22708
22709 funccal = current_funccal;
22710 if (debug_backtrace_level > 0)
22711 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022712 for (i = 0; i < debug_backtrace_level; i++)
22713 {
22714 temp_funccal = funccal->caller;
22715 if (temp_funccal)
22716 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022717 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022718 /* backtrace level overflow. reset to max */
22719 debug_backtrace_level = i;
22720 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022721 }
22722 return funccal;
22723}
22724
22725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022727 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022728 * Returns NULL when it doesn't exist.
22729 */
22730 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022731get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022732{
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022735 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022736 if (v == NULL)
22737 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022739}
22740
22741/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022742 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 * sourcing this script and when executing functions defined in the script.
22744 */
22745 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022746new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022747{
Bram Moolenaara7043832005-01-21 11:56:39 +000022748 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022749 hashtab_T *ht;
22750 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022751
Bram Moolenaar071d4272004-06-13 20:20:40 +000022752 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22753 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022754 /* Re-allocating ga_data means that an ht_array pointing to
22755 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022756 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022757 for (i = 1; i <= ga_scripts.ga_len; ++i)
22758 {
22759 ht = &SCRIPT_VARS(i);
22760 if (ht->ht_mask == HT_INIT_SIZE - 1)
22761 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022762 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022763 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022764 }
22765
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766 while (ga_scripts.ga_len < id)
22767 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022768 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022769 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022770 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 }
22773 }
22774}
22775
22776/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022777 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22778 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 */
22780 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022781init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782{
Bram Moolenaar33570922005-01-25 22:26:29 +000022783 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022784 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022785 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022786 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022787 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022788 dict_var->di_tv.vval.v_dict = dict;
22789 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022790 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022791 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22792 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793}
22794
22795/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022796 * Unreference a dictionary initialized by init_var_dict().
22797 */
22798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022800{
22801 /* Now the dict needs to be freed if no one else is using it, go back to
22802 * normal reference counting. */
22803 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22804 dict_unref(dict);
22805}
22806
22807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022808 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022809 * Frees all allocated variables and the value they contain.
22810 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022811 */
22812 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022813vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022814{
22815 vars_clear_ext(ht, TRUE);
22816}
22817
22818/*
22819 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22820 */
22821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022822vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022823{
Bram Moolenaara7043832005-01-21 11:56:39 +000022824 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022825 hashitem_T *hi;
22826 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827
Bram Moolenaar33570922005-01-25 22:26:29 +000022828 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022829 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022830 for (hi = ht->ht_array; todo > 0; ++hi)
22831 {
22832 if (!HASHITEM_EMPTY(hi))
22833 {
22834 --todo;
22835
Bram Moolenaar33570922005-01-25 22:26:29 +000022836 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022837 * ht_array might change then. hash_clear() takes care of it
22838 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022839 v = HI2DI(hi);
22840 if (free_val)
22841 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022842 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022844 }
22845 }
22846 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022847 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022848}
22849
Bram Moolenaara7043832005-01-21 11:56:39 +000022850/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 * Delete a variable from hashtab "ht" at item "hi".
22852 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022853 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022855delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022856{
Bram Moolenaar33570922005-01-25 22:26:29 +000022857 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022858
22859 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022860 clear_tv(&di->di_tv);
22861 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862}
22863
22864/*
22865 * List the value of one internal variable.
22866 */
22867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022868list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022869{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022870 char_u *tofree;
22871 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022872 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022873
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022874 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022875 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022876 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022877 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022878}
22879
Bram Moolenaar071d4272004-06-13 20:20:40 +000022880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022881list_one_var_a(
22882 char_u *prefix,
22883 char_u *name,
22884 int type,
22885 char_u *string,
22886 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022887{
Bram Moolenaar31859182007-08-14 20:41:13 +000022888 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22889 msg_start();
22890 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022891 if (name != NULL) /* "a:" vars don't have a name stored */
22892 msg_puts(name);
22893 msg_putchar(' ');
22894 msg_advance(22);
22895 if (type == VAR_NUMBER)
22896 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022897 else if (type == VAR_FUNC)
22898 msg_putchar('*');
22899 else if (type == VAR_LIST)
22900 {
22901 msg_putchar('[');
22902 if (*string == '[')
22903 ++string;
22904 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022905 else if (type == VAR_DICT)
22906 {
22907 msg_putchar('{');
22908 if (*string == '{')
22909 ++string;
22910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022911 else
22912 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022913
Bram Moolenaar071d4272004-06-13 20:20:40 +000022914 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022915
22916 if (type == VAR_FUNC)
22917 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022918 if (*first)
22919 {
22920 msg_clr_eos();
22921 *first = FALSE;
22922 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923}
22924
22925/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022926 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 * If the variable already exists, the value is updated.
22928 * Otherwise the variable is created.
22929 */
22930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022931set_var(
22932 char_u *name,
22933 typval_T *tv,
22934 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022935{
Bram Moolenaar33570922005-01-25 22:26:29 +000022936 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022937 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022938 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022939
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022940 ht = find_var_ht(name, &varname);
22941 if (ht == NULL || *varname == NUL)
22942 {
22943 EMSG2(_(e_illvar), name);
22944 return;
22945 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022946 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022947
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022948 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22949 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022950
Bram Moolenaar33570922005-01-25 22:26:29 +000022951 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022953 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022954 if (var_check_ro(v->di_flags, name, FALSE)
22955 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022956 return;
22957 if (v->di_tv.v_type != tv->v_type
22958 && !((v->di_tv.v_type == VAR_STRING
22959 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022960 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022961 || tv->v_type == VAR_NUMBER))
22962#ifdef FEAT_FLOAT
22963 && !((v->di_tv.v_type == VAR_NUMBER
22964 || v->di_tv.v_type == VAR_FLOAT)
22965 && (tv->v_type == VAR_NUMBER
22966 || tv->v_type == VAR_FLOAT))
22967#endif
22968 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022969 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022970 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022971 return;
22972 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022973
22974 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022975 * Handle setting internal v: variables separately where needed to
22976 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 */
22978 if (ht == &vimvarht)
22979 {
22980 if (v->di_tv.v_type == VAR_STRING)
22981 {
22982 vim_free(v->di_tv.vval.v_string);
22983 if (copy || tv->v_type != VAR_STRING)
22984 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22985 else
22986 {
22987 /* Take over the string to avoid an extra alloc/free. */
22988 v->di_tv.vval.v_string = tv->vval.v_string;
22989 tv->vval.v_string = NULL;
22990 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022991 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022992 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022993 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022994 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022996 if (STRCMP(varname, "searchforward") == 0)
22997 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022998#ifdef FEAT_SEARCH_EXTRA
22999 else if (STRCMP(varname, "hlsearch") == 0)
23000 {
23001 no_hlsearch = !v->di_tv.vval.v_number;
23002 redraw_all_later(SOME_VALID);
23003 }
23004#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023005 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023006 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020023007 else if (v->di_tv.v_type != tv->v_type)
23008 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000023009 }
23010
23011 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012 }
23013 else /* add a new variable */
23014 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000023015 /* Can't add "v:" variable. */
23016 if (ht == &vimvarht)
23017 {
23018 EMSG2(_(e_illvar), name);
23019 return;
23020 }
23021
Bram Moolenaar92124a32005-06-17 22:03:40 +000023022 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023023 if (!valid_varname(varname))
23024 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000023025
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023026 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
23027 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000023028 if (v == NULL)
23029 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023030 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023031 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023033 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023034 return;
23035 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023036 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023037 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023038
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023039 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023040 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023041 else
23042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023043 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023044 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023045 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047}
23048
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023049/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023050 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 * Also give an error message.
23052 */
23053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023054var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023055{
23056 if (flags & DI_FLAGS_RO)
23057 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023058 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023059 return TRUE;
23060 }
23061 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23062 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023063 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023064 return TRUE;
23065 }
23066 return FALSE;
23067}
23068
23069/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023070 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23071 * Also give an error message.
23072 */
23073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023074var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023075{
23076 if (flags & DI_FLAGS_FIX)
23077 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023078 EMSG2(_("E795: Cannot delete variable %s"),
23079 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023080 return TRUE;
23081 }
23082 return FALSE;
23083}
23084
23085/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023086 * Check if a funcref is assigned to a valid variable name.
23087 * Return TRUE and give an error if not.
23088 */
23089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023090var_check_func_name(
23091 char_u *name, /* points to start of variable name */
23092 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023093{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023094 /* Allow for w: b: s: and t:. */
23095 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023096 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23097 ? name[2] : name[0]))
23098 {
23099 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23100 name);
23101 return TRUE;
23102 }
23103 /* Don't allow hiding a function. When "v" is not NULL we might be
23104 * assigning another function to the same var, the type is checked
23105 * below. */
23106 if (new_var && function_exists(name))
23107 {
23108 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23109 name);
23110 return TRUE;
23111 }
23112 return FALSE;
23113}
23114
23115/*
23116 * Check if a variable name is valid.
23117 * Return FALSE and give an error if not.
23118 */
23119 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023120valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023121{
23122 char_u *p;
23123
23124 for (p = varname; *p != NUL; ++p)
23125 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23126 && *p != AUTOLOAD_CHAR)
23127 {
23128 EMSG2(_(e_illvar), varname);
23129 return FALSE;
23130 }
23131 return TRUE;
23132}
23133
23134/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023135 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023136 * Also give an error message, using "name" or _("name") when use_gettext is
23137 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023138 */
23139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023140tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023141{
23142 if (lock & VAR_LOCKED)
23143 {
23144 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023145 name == NULL ? (char_u *)_("Unknown")
23146 : use_gettext ? (char_u *)_(name)
23147 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023148 return TRUE;
23149 }
23150 if (lock & VAR_FIXED)
23151 {
23152 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023153 name == NULL ? (char_u *)_("Unknown")
23154 : use_gettext ? (char_u *)_(name)
23155 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023156 return TRUE;
23157 }
23158 return FALSE;
23159}
23160
23161/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023162 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023163 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023164 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023165 * It is OK for "from" and "to" to point to the same item. This is used to
23166 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023167 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023168 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023169copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023171 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023172 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023173 switch (from->v_type)
23174 {
23175 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023176 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023177 to->vval.v_number = from->vval.v_number;
23178 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023179 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023180#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023181 to->vval.v_float = from->vval.v_float;
23182 break;
23183#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023184 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023185#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023186 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023187 if (to->vval.v_job != NULL)
23188 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023189 break;
23190#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023191 case VAR_CHANNEL:
23192#ifdef FEAT_CHANNEL
23193 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023194 if (to->vval.v_channel != NULL)
23195 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023196 break;
23197#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023198 case VAR_STRING:
23199 case VAR_FUNC:
23200 if (from->vval.v_string == NULL)
23201 to->vval.v_string = NULL;
23202 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023203 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023204 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023205 if (from->v_type == VAR_FUNC)
23206 func_ref(to->vval.v_string);
23207 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023208 break;
23209 case VAR_LIST:
23210 if (from->vval.v_list == NULL)
23211 to->vval.v_list = NULL;
23212 else
23213 {
23214 to->vval.v_list = from->vval.v_list;
23215 ++to->vval.v_list->lv_refcount;
23216 }
23217 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023218 case VAR_DICT:
23219 if (from->vval.v_dict == NULL)
23220 to->vval.v_dict = NULL;
23221 else
23222 {
23223 to->vval.v_dict = from->vval.v_dict;
23224 ++to->vval.v_dict->dv_refcount;
23225 }
23226 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023227 case VAR_UNKNOWN:
23228 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023229 break;
23230 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231}
23232
23233/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023234 * Make a copy of an item.
23235 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023236 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23237 * reference to an already copied list/dict can be used.
23238 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023239 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023240 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023241item_copy(
23242 typval_T *from,
23243 typval_T *to,
23244 int deep,
23245 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023246{
23247 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023248 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023249
Bram Moolenaar33570922005-01-25 22:26:29 +000023250 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023251 {
23252 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023253 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023254 }
23255 ++recurse;
23256
23257 switch (from->v_type)
23258 {
23259 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023260 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023261 case VAR_STRING:
23262 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023263 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023264 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023265 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023266 copy_tv(from, to);
23267 break;
23268 case VAR_LIST:
23269 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023270 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023271 if (from->vval.v_list == NULL)
23272 to->vval.v_list = NULL;
23273 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23274 {
23275 /* use the copy made earlier */
23276 to->vval.v_list = from->vval.v_list->lv_copylist;
23277 ++to->vval.v_list->lv_refcount;
23278 }
23279 else
23280 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23281 if (to->vval.v_list == NULL)
23282 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023283 break;
23284 case VAR_DICT:
23285 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023286 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023287 if (from->vval.v_dict == NULL)
23288 to->vval.v_dict = NULL;
23289 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23290 {
23291 /* use the copy made earlier */
23292 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23293 ++to->vval.v_dict->dv_refcount;
23294 }
23295 else
23296 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23297 if (to->vval.v_dict == NULL)
23298 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023299 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023300 case VAR_UNKNOWN:
23301 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023302 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023303 }
23304 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023305 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023306}
23307
23308/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 * ":echo expr1 ..." print each argument separated with a space, add a
23310 * newline at the end.
23311 * ":echon expr1 ..." print each argument plain.
23312 */
23313 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023314ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315{
23316 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023317 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023318 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 char_u *p;
23320 int needclr = TRUE;
23321 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023322 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023323
23324 if (eap->skip)
23325 ++emsg_skip;
23326 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23327 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023328 /* If eval1() causes an error message the text from the command may
23329 * still need to be cleared. E.g., "echo 22,44". */
23330 need_clr_eos = needclr;
23331
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023333 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023334 {
23335 /*
23336 * Report the invalid expression unless the expression evaluation
23337 * has been cancelled due to an aborting error, an interrupt, or an
23338 * exception.
23339 */
23340 if (!aborting())
23341 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023342 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023343 break;
23344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023345 need_clr_eos = FALSE;
23346
Bram Moolenaar071d4272004-06-13 20:20:40 +000023347 if (!eap->skip)
23348 {
23349 if (atstart)
23350 {
23351 atstart = FALSE;
23352 /* Call msg_start() after eval1(), evaluating the expression
23353 * may cause a message to appear. */
23354 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023355 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023356 /* Mark the saved text as finishing the line, so that what
23357 * follows is displayed on a new line when scrolling back
23358 * at the more prompt. */
23359 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023362 }
23363 else if (eap->cmdidx == CMD_echo)
23364 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023365 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023366 if (p != NULL)
23367 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023369 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023370 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023371 if (*p != TAB && needclr)
23372 {
23373 /* remove any text still there from the command */
23374 msg_clr_eos();
23375 needclr = FALSE;
23376 }
23377 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023378 }
23379 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023380 {
23381#ifdef FEAT_MBYTE
23382 if (has_mbyte)
23383 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023384 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023385
23386 (void)msg_outtrans_len_attr(p, i, echo_attr);
23387 p += i - 1;
23388 }
23389 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023391 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023394 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023396 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 arg = skipwhite(arg);
23398 }
23399 eap->nextcmd = check_nextcmd(arg);
23400
23401 if (eap->skip)
23402 --emsg_skip;
23403 else
23404 {
23405 /* remove text that may still be there from the command */
23406 if (needclr)
23407 msg_clr_eos();
23408 if (eap->cmdidx == CMD_echo)
23409 msg_end();
23410 }
23411}
23412
23413/*
23414 * ":echohl {name}".
23415 */
23416 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023417ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023418{
23419 int id;
23420
23421 id = syn_name2id(eap->arg);
23422 if (id == 0)
23423 echo_attr = 0;
23424 else
23425 echo_attr = syn_id2attr(id);
23426}
23427
23428/*
23429 * ":execute expr1 ..." execute the result of an expression.
23430 * ":echomsg expr1 ..." Print a message
23431 * ":echoerr expr1 ..." Print an error
23432 * Each gets spaces around each argument and a newline at the end for
23433 * echo commands
23434 */
23435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023436ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437{
23438 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023439 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 int ret = OK;
23441 char_u *p;
23442 garray_T ga;
23443 int len;
23444 int save_did_emsg;
23445
23446 ga_init2(&ga, 1, 80);
23447
23448 if (eap->skip)
23449 ++emsg_skip;
23450 while (*arg != NUL && *arg != '|' && *arg != '\n')
23451 {
23452 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023453 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 {
23455 /*
23456 * Report the invalid expression unless the expression evaluation
23457 * has been cancelled due to an aborting error, an interrupt, or an
23458 * exception.
23459 */
23460 if (!aborting())
23461 EMSG2(_(e_invexpr2), p);
23462 ret = FAIL;
23463 break;
23464 }
23465
23466 if (!eap->skip)
23467 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023468 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 len = (int)STRLEN(p);
23470 if (ga_grow(&ga, len + 2) == FAIL)
23471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023472 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023473 ret = FAIL;
23474 break;
23475 }
23476 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 ga.ga_len += len;
23480 }
23481
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023482 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023483 arg = skipwhite(arg);
23484 }
23485
23486 if (ret != FAIL && ga.ga_data != NULL)
23487 {
23488 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023489 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023491 out_flush();
23492 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493 else if (eap->cmdidx == CMD_echoerr)
23494 {
23495 /* We don't want to abort following commands, restore did_emsg. */
23496 save_did_emsg = did_emsg;
23497 EMSG((char_u *)ga.ga_data);
23498 if (!force_abort)
23499 did_emsg = save_did_emsg;
23500 }
23501 else if (eap->cmdidx == CMD_execute)
23502 do_cmdline((char_u *)ga.ga_data,
23503 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23504 }
23505
23506 ga_clear(&ga);
23507
23508 if (eap->skip)
23509 --emsg_skip;
23510
23511 eap->nextcmd = check_nextcmd(arg);
23512}
23513
23514/*
23515 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23516 * "arg" points to the "&" or '+' when called, to "option" when returning.
23517 * Returns NULL when no option name found. Otherwise pointer to the char
23518 * after the option name.
23519 */
23520 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023521find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522{
23523 char_u *p = *arg;
23524
23525 ++p;
23526 if (*p == 'g' && p[1] == ':')
23527 {
23528 *opt_flags = OPT_GLOBAL;
23529 p += 2;
23530 }
23531 else if (*p == 'l' && p[1] == ':')
23532 {
23533 *opt_flags = OPT_LOCAL;
23534 p += 2;
23535 }
23536 else
23537 *opt_flags = 0;
23538
23539 if (!ASCII_ISALPHA(*p))
23540 return NULL;
23541 *arg = p;
23542
23543 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23544 p += 4; /* termcap option */
23545 else
23546 while (ASCII_ISALPHA(*p))
23547 ++p;
23548 return p;
23549}
23550
23551/*
23552 * ":function"
23553 */
23554 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023555ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023556{
23557 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023558 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023559 int j;
23560 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023561 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023562 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023563 char_u *name = NULL;
23564 char_u *p;
23565 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023566 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023567 garray_T newargs;
23568 garray_T newlines;
23569 int varargs = FALSE;
23570 int mustend = FALSE;
23571 int flags = 0;
23572 ufunc_T *fp;
23573 int indent;
23574 int nesting;
23575 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023576 dictitem_T *v;
23577 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023578 static int func_nr = 0; /* number for nameless function */
23579 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023580 hashtab_T *ht;
23581 int todo;
23582 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023583 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023584
23585 /*
23586 * ":function" without argument: list functions.
23587 */
23588 if (ends_excmd(*eap->arg))
23589 {
23590 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023591 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023592 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023593 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023594 {
23595 if (!HASHITEM_EMPTY(hi))
23596 {
23597 --todo;
23598 fp = HI2UF(hi);
23599 if (!isdigit(*fp->uf_name))
23600 list_func_head(fp, FALSE);
23601 }
23602 }
23603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 eap->nextcmd = check_nextcmd(eap->arg);
23605 return;
23606 }
23607
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023608 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023609 * ":function /pat": list functions matching pattern.
23610 */
23611 if (*eap->arg == '/')
23612 {
23613 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23614 if (!eap->skip)
23615 {
23616 regmatch_T regmatch;
23617
23618 c = *p;
23619 *p = NUL;
23620 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23621 *p = c;
23622 if (regmatch.regprog != NULL)
23623 {
23624 regmatch.rm_ic = p_ic;
23625
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023626 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023627 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23628 {
23629 if (!HASHITEM_EMPTY(hi))
23630 {
23631 --todo;
23632 fp = HI2UF(hi);
23633 if (!isdigit(*fp->uf_name)
23634 && vim_regexec(&regmatch, fp->uf_name, 0))
23635 list_func_head(fp, FALSE);
23636 }
23637 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023638 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023639 }
23640 }
23641 if (*p == '/')
23642 ++p;
23643 eap->nextcmd = check_nextcmd(p);
23644 return;
23645 }
23646
23647 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 * Get the function name. There are these situations:
23649 * func normal function name
23650 * "name" == func, "fudi.fd_dict" == NULL
23651 * dict.func new dictionary entry
23652 * "name" == NULL, "fudi.fd_dict" set,
23653 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23654 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023655 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023656 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23657 * dict.func existing dict entry that's not a Funcref
23658 * "name" == NULL, "fudi.fd_dict" set,
23659 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023660 * s:func script-local function name
23661 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023663 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023664 name = trans_function_name(&p, eap->skip, 0, &fudi);
23665 paren = (vim_strchr(p, '(') != NULL);
23666 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 {
23668 /*
23669 * Return on an invalid expression in braces, unless the expression
23670 * evaluation has been cancelled due to an aborting error, an
23671 * interrupt, or an exception.
23672 */
23673 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023674 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023675 if (!eap->skip && fudi.fd_newkey != NULL)
23676 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023677 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023678 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023679 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 else
23681 eap->skip = TRUE;
23682 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023683
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 /* An error in a function call during evaluation of an expression in magic
23685 * braces should not cause the function not to be defined. */
23686 saved_did_emsg = did_emsg;
23687 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023688
23689 /*
23690 * ":function func" with only function name: list function.
23691 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023692 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 {
23694 if (!ends_excmd(*skipwhite(p)))
23695 {
23696 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023697 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 }
23699 eap->nextcmd = check_nextcmd(p);
23700 if (eap->nextcmd != NULL)
23701 *p = NUL;
23702 if (!eap->skip && !got_int)
23703 {
23704 fp = find_func(name);
23705 if (fp != NULL)
23706 {
23707 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023708 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023710 if (FUNCLINE(fp, j) == NULL)
23711 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023712 msg_putchar('\n');
23713 msg_outnum((long)(j + 1));
23714 if (j < 9)
23715 msg_putchar(' ');
23716 if (j < 99)
23717 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023718 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023719 out_flush(); /* show a line at a time */
23720 ui_breakcheck();
23721 }
23722 if (!got_int)
23723 {
23724 msg_putchar('\n');
23725 msg_puts((char_u *)" endfunction");
23726 }
23727 }
23728 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023729 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023730 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023731 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023732 }
23733
23734 /*
23735 * ":function name(arg1, arg2)" Define function.
23736 */
23737 p = skipwhite(p);
23738 if (*p != '(')
23739 {
23740 if (!eap->skip)
23741 {
23742 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023743 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023744 }
23745 /* attempt to continue by skipping some text */
23746 if (vim_strchr(p, '(') != NULL)
23747 p = vim_strchr(p, '(');
23748 }
23749 p = skipwhite(p + 1);
23750
23751 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23752 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23753
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023754 if (!eap->skip)
23755 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023756 /* Check the name of the function. Unless it's a dictionary function
23757 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023758 if (name != NULL)
23759 arg = name;
23760 else
23761 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023762 if (arg != NULL && (fudi.fd_di == NULL
23763 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023764 {
23765 if (*arg == K_SPECIAL)
23766 j = 3;
23767 else
23768 j = 0;
23769 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23770 : eval_isnamec(arg[j])))
23771 ++j;
23772 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023773 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023774 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023775 /* Disallow using the g: dict. */
23776 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23777 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023778 }
23779
Bram Moolenaar071d4272004-06-13 20:20:40 +000023780 /*
23781 * Isolate the arguments: "arg1, arg2, ...)"
23782 */
23783 while (*p != ')')
23784 {
23785 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23786 {
23787 varargs = TRUE;
23788 p += 3;
23789 mustend = TRUE;
23790 }
23791 else
23792 {
23793 arg = p;
23794 while (ASCII_ISALNUM(*p) || *p == '_')
23795 ++p;
23796 if (arg == p || isdigit(*arg)
23797 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23798 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23799 {
23800 if (!eap->skip)
23801 EMSG2(_("E125: Illegal argument: %s"), arg);
23802 break;
23803 }
23804 if (ga_grow(&newargs, 1) == FAIL)
23805 goto erret;
23806 c = *p;
23807 *p = NUL;
23808 arg = vim_strsave(arg);
23809 if (arg == NULL)
23810 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023811
23812 /* Check for duplicate argument name. */
23813 for (i = 0; i < newargs.ga_len; ++i)
23814 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23815 {
23816 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023817 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023818 goto erret;
23819 }
23820
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23822 *p = c;
23823 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023824 if (*p == ',')
23825 ++p;
23826 else
23827 mustend = TRUE;
23828 }
23829 p = skipwhite(p);
23830 if (mustend && *p != ')')
23831 {
23832 if (!eap->skip)
23833 EMSG2(_(e_invarg2), eap->arg);
23834 break;
23835 }
23836 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023837 if (*p != ')')
23838 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023839 ++p; /* skip the ')' */
23840
Bram Moolenaare9a41262005-01-15 22:18:47 +000023841 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023842 for (;;)
23843 {
23844 p = skipwhite(p);
23845 if (STRNCMP(p, "range", 5) == 0)
23846 {
23847 flags |= FC_RANGE;
23848 p += 5;
23849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023850 else if (STRNCMP(p, "dict", 4) == 0)
23851 {
23852 flags |= FC_DICT;
23853 p += 4;
23854 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 else if (STRNCMP(p, "abort", 5) == 0)
23856 {
23857 flags |= FC_ABORT;
23858 p += 5;
23859 }
23860 else
23861 break;
23862 }
23863
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023864 /* When there is a line break use what follows for the function body.
23865 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23866 if (*p == '\n')
23867 line_arg = p + 1;
23868 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 EMSG(_(e_trailing));
23870
23871 /*
23872 * Read the body of the function, until ":endfunction" is found.
23873 */
23874 if (KeyTyped)
23875 {
23876 /* Check if the function already exists, don't let the user type the
23877 * whole function before telling him it doesn't work! For a script we
23878 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023879 if (!eap->skip && !eap->forceit)
23880 {
23881 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23882 EMSG(_(e_funcdict));
23883 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023884 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023887 if (!eap->skip && did_emsg)
23888 goto erret;
23889
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 msg_putchar('\n'); /* don't overwrite the function name */
23891 cmdline_row = msg_row;
23892 }
23893
23894 indent = 2;
23895 nesting = 0;
23896 for (;;)
23897 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023898 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023899 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023900 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023901 saved_wait_return = FALSE;
23902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023903 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023904 sourcing_lnum_off = sourcing_lnum;
23905
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023906 if (line_arg != NULL)
23907 {
23908 /* Use eap->arg, split up in parts by line breaks. */
23909 theline = line_arg;
23910 p = vim_strchr(theline, '\n');
23911 if (p == NULL)
23912 line_arg += STRLEN(line_arg);
23913 else
23914 {
23915 *p = NUL;
23916 line_arg = p + 1;
23917 }
23918 }
23919 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023920 theline = getcmdline(':', 0L, indent);
23921 else
23922 theline = eap->getline(':', eap->cookie, indent);
23923 if (KeyTyped)
23924 lines_left = Rows - 1;
23925 if (theline == NULL)
23926 {
23927 EMSG(_("E126: Missing :endfunction"));
23928 goto erret;
23929 }
23930
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023931 /* Detect line continuation: sourcing_lnum increased more than one. */
23932 if (sourcing_lnum > sourcing_lnum_off + 1)
23933 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23934 else
23935 sourcing_lnum_off = 0;
23936
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 if (skip_until != NULL)
23938 {
23939 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23940 * don't check for ":endfunc". */
23941 if (STRCMP(theline, skip_until) == 0)
23942 {
23943 vim_free(skip_until);
23944 skip_until = NULL;
23945 }
23946 }
23947 else
23948 {
23949 /* skip ':' and blanks*/
23950 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23951 ;
23952
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023953 /* Check for "endfunction". */
23954 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023955 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023956 if (line_arg == NULL)
23957 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 break;
23959 }
23960
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023961 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 * at "end". */
23963 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23964 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023965 else if (STRNCMP(p, "if", 2) == 0
23966 || STRNCMP(p, "wh", 2) == 0
23967 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023968 || STRNCMP(p, "try", 3) == 0)
23969 indent += 2;
23970
23971 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023972 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023973 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023974 if (*p == '!')
23975 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023976 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023977 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23978 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023980 ++nesting;
23981 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023982 }
23983 }
23984
23985 /* Check for ":append" or ":insert". */
23986 p = skip_range(p, NULL);
23987 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23988 || (p[0] == 'i'
23989 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23990 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23991 skip_until = vim_strsave((char_u *)".");
23992
23993 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23994 arg = skipwhite(skiptowhite(p));
23995 if (arg[0] == '<' && arg[1] =='<'
23996 && ((p[0] == 'p' && p[1] == 'y'
23997 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23998 || (p[0] == 'p' && p[1] == 'e'
23999 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
24000 || (p[0] == 't' && p[1] == 'c'
24001 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020024002 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
24003 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024004 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
24005 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000024006 || (p[0] == 'm' && p[1] == 'z'
24007 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 ))
24009 {
24010 /* ":python <<" continues until a dot, like ":append" */
24011 p = skipwhite(arg + 2);
24012 if (*p == NUL)
24013 skip_until = vim_strsave((char_u *)".");
24014 else
24015 skip_until = vim_strsave(p);
24016 }
24017 }
24018
24019 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024020 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024021 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024022 if (line_arg == NULL)
24023 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024024 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024025 }
24026
24027 /* Copy the line to newly allocated memory. get_one_sourceline()
24028 * allocates 250 bytes per line, this saves 80% on average. The cost
24029 * is an extra alloc/free. */
24030 p = vim_strsave(theline);
24031 if (p != NULL)
24032 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024033 if (line_arg == NULL)
24034 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024035 theline = p;
24036 }
24037
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024038 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24039
24040 /* Add NULL lines for continuation lines, so that the line count is
24041 * equal to the index in the growarray. */
24042 while (sourcing_lnum_off-- > 0)
24043 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024044
24045 /* Check for end of eap->arg. */
24046 if (line_arg != NULL && *line_arg == NUL)
24047 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 }
24049
24050 /* Don't define the function when skipping commands or when an error was
24051 * detected. */
24052 if (eap->skip || did_emsg)
24053 goto erret;
24054
24055 /*
24056 * If there are no errors, add the function
24057 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024058 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024059 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024060 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024061 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024063 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024064 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024065 goto erret;
24066 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024067
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024068 fp = find_func(name);
24069 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024070 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024071 if (!eap->forceit)
24072 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024073 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024074 goto erret;
24075 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024076 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024077 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024078 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024079 name);
24080 goto erret;
24081 }
24082 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024083 ga_clear_strings(&(fp->uf_args));
24084 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024085 vim_free(name);
24086 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024087 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024088 }
24089 else
24090 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024091 char numbuf[20];
24092
24093 fp = NULL;
24094 if (fudi.fd_newkey == NULL && !eap->forceit)
24095 {
24096 EMSG(_(e_funcdict));
24097 goto erret;
24098 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024099 if (fudi.fd_di == NULL)
24100 {
24101 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024102 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024103 goto erret;
24104 }
24105 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024106 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024107 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024108
24109 /* Give the function a sequential number. Can only be used with a
24110 * Funcref! */
24111 vim_free(name);
24112 sprintf(numbuf, "%d", ++func_nr);
24113 name = vim_strsave((char_u *)numbuf);
24114 if (name == NULL)
24115 goto erret;
24116 }
24117
24118 if (fp == NULL)
24119 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024120 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024121 {
24122 int slen, plen;
24123 char_u *scriptname;
24124
24125 /* Check that the autoload name matches the script name. */
24126 j = FAIL;
24127 if (sourcing_name != NULL)
24128 {
24129 scriptname = autoload_name(name);
24130 if (scriptname != NULL)
24131 {
24132 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024133 plen = (int)STRLEN(p);
24134 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024135 if (slen > plen && fnamecmp(p,
24136 sourcing_name + slen - plen) == 0)
24137 j = OK;
24138 vim_free(scriptname);
24139 }
24140 }
24141 if (j == FAIL)
24142 {
24143 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24144 goto erret;
24145 }
24146 }
24147
24148 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024149 if (fp == NULL)
24150 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151
24152 if (fudi.fd_dict != NULL)
24153 {
24154 if (fudi.fd_di == NULL)
24155 {
24156 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024157 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024158 if (fudi.fd_di == NULL)
24159 {
24160 vim_free(fp);
24161 goto erret;
24162 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024163 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24164 {
24165 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024166 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024167 goto erret;
24168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024169 }
24170 else
24171 /* overwrite existing dict entry */
24172 clear_tv(&fudi.fd_di->di_tv);
24173 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024174 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024175 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024176 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024177
24178 /* behave like "dict" was used */
24179 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024180 }
24181
Bram Moolenaar071d4272004-06-13 20:20:40 +000024182 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024184 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24185 {
24186 vim_free(fp);
24187 goto erret;
24188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024190 fp->uf_args = newargs;
24191 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024192#ifdef FEAT_PROFILE
24193 fp->uf_tml_count = NULL;
24194 fp->uf_tml_total = NULL;
24195 fp->uf_tml_self = NULL;
24196 fp->uf_profiling = FALSE;
24197 if (prof_def_func())
24198 func_do_profile(fp);
24199#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024200 fp->uf_varargs = varargs;
24201 fp->uf_flags = flags;
24202 fp->uf_calls = 0;
24203 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024204 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024205
24206erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024207 ga_clear_strings(&newargs);
24208 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024209ret_free:
24210 vim_free(skip_until);
24211 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024212 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024213 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024214 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215}
24216
24217/*
24218 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024219 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024221 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024222 * TFN_INT: internal function name OK
24223 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024224 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225 * Advances "pp" to just after the function name (if no error).
24226 */
24227 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024228trans_function_name(
24229 char_u **pp,
24230 int skip, /* only find the end, don't evaluate */
24231 int flags,
24232 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024234 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024235 char_u *start;
24236 char_u *end;
24237 int lead;
24238 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024240 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024241
24242 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024243 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024245
24246 /* Check for hard coded <SNR>: already translated function ID (from a user
24247 * command). */
24248 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24249 && (*pp)[2] == (int)KE_SNR)
24250 {
24251 *pp += 3;
24252 len = get_id_len(pp) + 3;
24253 return vim_strnsave(start, len);
24254 }
24255
24256 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24257 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024259 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024261
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024262 /* Note that TFN_ flags use the same values as GLV_ flags. */
24263 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024264 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024265 if (end == start)
24266 {
24267 if (!skip)
24268 EMSG(_("E129: Function name required"));
24269 goto theend;
24270 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024271 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024272 {
24273 /*
24274 * Report an invalid expression in braces, unless the expression
24275 * evaluation has been cancelled due to an aborting error, an
24276 * interrupt, or an exception.
24277 */
24278 if (!aborting())
24279 {
24280 if (end != NULL)
24281 EMSG2(_(e_invarg2), start);
24282 }
24283 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024284 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024285 goto theend;
24286 }
24287
24288 if (lv.ll_tv != NULL)
24289 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024290 if (fdp != NULL)
24291 {
24292 fdp->fd_dict = lv.ll_dict;
24293 fdp->fd_newkey = lv.ll_newkey;
24294 lv.ll_newkey = NULL;
24295 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024296 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024297 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24298 {
24299 name = vim_strsave(lv.ll_tv->vval.v_string);
24300 *pp = end;
24301 }
24302 else
24303 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024304 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24305 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024306 EMSG(_(e_funcref));
24307 else
24308 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024309 name = NULL;
24310 }
24311 goto theend;
24312 }
24313
24314 if (lv.ll_name == NULL)
24315 {
24316 /* Error found, but continue after the function name. */
24317 *pp = end;
24318 goto theend;
24319 }
24320
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024321 /* Check if the name is a Funcref. If so, use the value. */
24322 if (lv.ll_exp_name != NULL)
24323 {
24324 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024325 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024326 if (name == lv.ll_exp_name)
24327 name = NULL;
24328 }
24329 else
24330 {
24331 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024332 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024333 if (name == *pp)
24334 name = NULL;
24335 }
24336 if (name != NULL)
24337 {
24338 name = vim_strsave(name);
24339 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024340 if (STRNCMP(name, "<SNR>", 5) == 0)
24341 {
24342 /* Change "<SNR>" to the byte sequence. */
24343 name[0] = K_SPECIAL;
24344 name[1] = KS_EXTRA;
24345 name[2] = (int)KE_SNR;
24346 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24347 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024348 goto theend;
24349 }
24350
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024351 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024352 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024353 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024354 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24355 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24356 {
24357 /* When there was "s:" already or the name expanded to get a
24358 * leading "s:" then remove it. */
24359 lv.ll_name += 2;
24360 len -= 2;
24361 lead = 2;
24362 }
24363 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024364 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024365 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024366 /* skip over "s:" and "g:" */
24367 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024368 lv.ll_name += 2;
24369 len = (int)(end - lv.ll_name);
24370 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024371
24372 /*
24373 * Copy the function name to allocated memory.
24374 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24375 * Accept <SNR>123_name() outside a script.
24376 */
24377 if (skip)
24378 lead = 0; /* do nothing */
24379 else if (lead > 0)
24380 {
24381 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024382 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24383 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024384 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024385 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024386 if (current_SID <= 0)
24387 {
24388 EMSG(_(e_usingsid));
24389 goto theend;
24390 }
24391 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24392 lead += (int)STRLEN(sid_buf);
24393 }
24394 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024395 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024396 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024397 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024398 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024399 goto theend;
24400 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024401 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024402 {
24403 char_u *cp = vim_strchr(lv.ll_name, ':');
24404
24405 if (cp != NULL && cp < end)
24406 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024407 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024408 goto theend;
24409 }
24410 }
24411
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024412 name = alloc((unsigned)(len + lead + 1));
24413 if (name != NULL)
24414 {
24415 if (lead > 0)
24416 {
24417 name[0] = K_SPECIAL;
24418 name[1] = KS_EXTRA;
24419 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024420 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024421 STRCPY(name + 3, sid_buf);
24422 }
24423 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024424 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024425 }
24426 *pp = end;
24427
24428theend:
24429 clear_lval(&lv);
24430 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431}
24432
24433/*
24434 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24435 * Return 2 if "p" starts with "s:".
24436 * Return 0 otherwise.
24437 */
24438 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024439eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024440{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024441 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24442 * the standard library function. */
24443 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24444 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 return 5;
24446 if (p[0] == 's' && p[1] == ':')
24447 return 2;
24448 return 0;
24449}
24450
24451/*
24452 * Return TRUE if "p" starts with "<SID>" or "s:".
24453 * Only works if eval_fname_script() returned non-zero for "p"!
24454 */
24455 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024456eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024457{
24458 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24459}
24460
24461/*
24462 * List the head of the function: "name(arg1, arg2)".
24463 */
24464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024465list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024466{
24467 int j;
24468
24469 msg_start();
24470 if (indent)
24471 MSG_PUTS(" ");
24472 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024473 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474 {
24475 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024476 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024477 }
24478 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024479 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024480 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024481 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482 {
24483 if (j)
24484 MSG_PUTS(", ");
24485 msg_puts(FUNCARG(fp, j));
24486 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024487 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024488 {
24489 if (j)
24490 MSG_PUTS(", ");
24491 MSG_PUTS("...");
24492 }
24493 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024494 if (fp->uf_flags & FC_ABORT)
24495 MSG_PUTS(" abort");
24496 if (fp->uf_flags & FC_RANGE)
24497 MSG_PUTS(" range");
24498 if (fp->uf_flags & FC_DICT)
24499 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024500 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024501 if (p_verbose > 0)
24502 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024503}
24504
24505/*
24506 * Find a function by name, return pointer to it in ufuncs.
24507 * Return NULL for unknown function.
24508 */
24509 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024510find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024511{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024512 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024513
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024514 hi = hash_find(&func_hashtab, name);
24515 if (!HASHITEM_EMPTY(hi))
24516 return HI2UF(hi);
24517 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024518}
24519
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024520#if defined(EXITFREE) || defined(PROTO)
24521 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024522free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024523{
24524 hashitem_T *hi;
24525
24526 /* Need to start all over every time, because func_free() may change the
24527 * hash table. */
24528 while (func_hashtab.ht_used > 0)
24529 for (hi = func_hashtab.ht_array; ; ++hi)
24530 if (!HASHITEM_EMPTY(hi))
24531 {
24532 func_free(HI2UF(hi));
24533 break;
24534 }
24535}
24536#endif
24537
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024538 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024539translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024540{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024541 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024542 return find_internal_func(name) >= 0;
24543 return find_func(name) != NULL;
24544}
24545
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024546/*
24547 * Return TRUE if a function "name" exists.
24548 */
24549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024550function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024551{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024552 char_u *nm = name;
24553 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024554 int n = FALSE;
24555
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024556 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24557 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024558 nm = skipwhite(nm);
24559
24560 /* Only accept "funcname", "funcname ", "funcname (..." and
24561 * "funcname(...", not "funcname!...". */
24562 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024563 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024564 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024565 return n;
24566}
24567
Bram Moolenaara1544c02013-05-30 12:35:52 +020024568 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024570{
24571 char_u *nm = name;
24572 char_u *p;
24573
24574 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24575
24576 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024577 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024578 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024579
Bram Moolenaara1544c02013-05-30 12:35:52 +020024580 vim_free(p);
24581 return NULL;
24582}
24583
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024584/*
24585 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024586 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24587 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024588 */
24589 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024590builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024591{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024592 char_u *p;
24593
24594 if (!ASCII_ISLOWER(name[0]))
24595 return FALSE;
24596 p = vim_strchr(name, AUTOLOAD_CHAR);
24597 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024598}
24599
Bram Moolenaar05159a02005-02-26 23:04:13 +000024600#if defined(FEAT_PROFILE) || defined(PROTO)
24601/*
24602 * Start profiling function "fp".
24603 */
24604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024605func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024607 int len = fp->uf_lines.ga_len;
24608
24609 if (len == 0)
24610 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024611 fp->uf_tm_count = 0;
24612 profile_zero(&fp->uf_tm_self);
24613 profile_zero(&fp->uf_tm_total);
24614 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024615 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024616 if (fp->uf_tml_total == NULL)
24617 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024618 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024619 if (fp->uf_tml_self == NULL)
24620 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024621 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024622 fp->uf_tml_idx = -1;
24623 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24624 || fp->uf_tml_self == NULL)
24625 return; /* out of memory */
24626
24627 fp->uf_profiling = TRUE;
24628}
24629
24630/*
24631 * Dump the profiling results for all functions in file "fd".
24632 */
24633 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024634func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024635{
24636 hashitem_T *hi;
24637 int todo;
24638 ufunc_T *fp;
24639 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024640 ufunc_T **sorttab;
24641 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024642
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024643 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024644 if (todo == 0)
24645 return; /* nothing to dump */
24646
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024647 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024648
Bram Moolenaar05159a02005-02-26 23:04:13 +000024649 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24650 {
24651 if (!HASHITEM_EMPTY(hi))
24652 {
24653 --todo;
24654 fp = HI2UF(hi);
24655 if (fp->uf_profiling)
24656 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024657 if (sorttab != NULL)
24658 sorttab[st_len++] = fp;
24659
Bram Moolenaar05159a02005-02-26 23:04:13 +000024660 if (fp->uf_name[0] == K_SPECIAL)
24661 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24662 else
24663 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24664 if (fp->uf_tm_count == 1)
24665 fprintf(fd, "Called 1 time\n");
24666 else
24667 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24668 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24669 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24670 fprintf(fd, "\n");
24671 fprintf(fd, "count total (s) self (s)\n");
24672
24673 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24674 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024675 if (FUNCLINE(fp, i) == NULL)
24676 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024677 prof_func_line(fd, fp->uf_tml_count[i],
24678 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024679 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24680 }
24681 fprintf(fd, "\n");
24682 }
24683 }
24684 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024685
24686 if (sorttab != NULL && st_len > 0)
24687 {
24688 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24689 prof_total_cmp);
24690 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24691 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24692 prof_self_cmp);
24693 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24694 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024695
24696 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024697}
Bram Moolenaar73830342005-02-28 22:48:19 +000024698
24699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024700prof_sort_list(
24701 FILE *fd,
24702 ufunc_T **sorttab,
24703 int st_len,
24704 char *title,
24705 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024706{
24707 int i;
24708 ufunc_T *fp;
24709
24710 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24711 fprintf(fd, "count total (s) self (s) function\n");
24712 for (i = 0; i < 20 && i < st_len; ++i)
24713 {
24714 fp = sorttab[i];
24715 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24716 prefer_self);
24717 if (fp->uf_name[0] == K_SPECIAL)
24718 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24719 else
24720 fprintf(fd, " %s()\n", fp->uf_name);
24721 }
24722 fprintf(fd, "\n");
24723}
24724
24725/*
24726 * Print the count and times for one function or function line.
24727 */
24728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024729prof_func_line(
24730 FILE *fd,
24731 int count,
24732 proftime_T *total,
24733 proftime_T *self,
24734 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024735{
24736 if (count > 0)
24737 {
24738 fprintf(fd, "%5d ", count);
24739 if (prefer_self && profile_equal(total, self))
24740 fprintf(fd, " ");
24741 else
24742 fprintf(fd, "%s ", profile_msg(total));
24743 if (!prefer_self && profile_equal(total, self))
24744 fprintf(fd, " ");
24745 else
24746 fprintf(fd, "%s ", profile_msg(self));
24747 }
24748 else
24749 fprintf(fd, " ");
24750}
24751
24752/*
24753 * Compare function for total time sorting.
24754 */
24755 static int
24756#ifdef __BORLANDC__
24757_RTLENTRYF
24758#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024759prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024760{
24761 ufunc_T *p1, *p2;
24762
24763 p1 = *(ufunc_T **)s1;
24764 p2 = *(ufunc_T **)s2;
24765 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24766}
24767
24768/*
24769 * Compare function for self time sorting.
24770 */
24771 static int
24772#ifdef __BORLANDC__
24773_RTLENTRYF
24774#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024775prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024776{
24777 ufunc_T *p1, *p2;
24778
24779 p1 = *(ufunc_T **)s1;
24780 p2 = *(ufunc_T **)s2;
24781 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24782}
24783
Bram Moolenaar05159a02005-02-26 23:04:13 +000024784#endif
24785
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024786/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024787 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024788 * Return TRUE if a package was loaded.
24789 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024791script_autoload(
24792 char_u *name,
24793 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024794{
24795 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024796 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024797 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024798 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024799
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024800 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024801 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024802 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024803 return FALSE;
24804
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024805 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024806
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024807 /* Find the name in the list of previously loaded package names. Skip
24808 * "autoload/", it's always the same. */
24809 for (i = 0; i < ga_loaded.ga_len; ++i)
24810 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24811 break;
24812 if (!reload && i < ga_loaded.ga_len)
24813 ret = FALSE; /* was loaded already */
24814 else
24815 {
24816 /* Remember the name if it wasn't loaded already. */
24817 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24818 {
24819 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24820 tofree = NULL;
24821 }
24822
24823 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024824 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024825 ret = TRUE;
24826 }
24827
24828 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024829 return ret;
24830}
24831
24832/*
24833 * Return the autoload script name for a function or variable name.
24834 * Returns NULL when out of memory.
24835 */
24836 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024837autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024838{
24839 char_u *p;
24840 char_u *scriptname;
24841
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024842 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024843 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24844 if (scriptname == NULL)
24845 return FALSE;
24846 STRCPY(scriptname, "autoload/");
24847 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024848 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024849 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024850 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024851 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024852 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024853}
24854
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24856
24857/*
24858 * Function given to ExpandGeneric() to obtain the list of user defined
24859 * function names.
24860 */
24861 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024862get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024864 static long_u done;
24865 static hashitem_T *hi;
24866 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024867
24868 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024869 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024870 done = 0;
24871 hi = func_hashtab.ht_array;
24872 }
24873 if (done < func_hashtab.ht_used)
24874 {
24875 if (done++ > 0)
24876 ++hi;
24877 while (HASHITEM_EMPTY(hi))
24878 ++hi;
24879 fp = HI2UF(hi);
24880
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024881 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024882 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024883
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024884 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24885 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024886
24887 cat_func_name(IObuff, fp);
24888 if (xp->xp_context != EXPAND_USER_FUNC)
24889 {
24890 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024891 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024892 STRCAT(IObuff, ")");
24893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894 return IObuff;
24895 }
24896 return NULL;
24897}
24898
24899#endif /* FEAT_CMDL_COMPL */
24900
24901/*
24902 * Copy the function name of "fp" to buffer "buf".
24903 * "buf" must be able to hold the function name plus three bytes.
24904 * Takes care of script-local function names.
24905 */
24906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024907cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024908{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024909 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 {
24911 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024912 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 }
24914 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024915 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024916}
24917
24918/*
24919 * ":delfunction {name}"
24920 */
24921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024922ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024923{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024924 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024925 char_u *p;
24926 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024927 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928
24929 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024930 name = trans_function_name(&p, eap->skip, 0, &fudi);
24931 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024932 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024933 {
24934 if (fudi.fd_dict != NULL && !eap->skip)
24935 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024938 if (!ends_excmd(*skipwhite(p)))
24939 {
24940 vim_free(name);
24941 EMSG(_(e_trailing));
24942 return;
24943 }
24944 eap->nextcmd = check_nextcmd(p);
24945 if (eap->nextcmd != NULL)
24946 *p = NUL;
24947
24948 if (!eap->skip)
24949 fp = find_func(name);
24950 vim_free(name);
24951
24952 if (!eap->skip)
24953 {
24954 if (fp == NULL)
24955 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024956 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 return;
24958 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024959 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 {
24961 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24962 return;
24963 }
24964
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024965 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024967 /* Delete the dict item that refers to the function, it will
24968 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024969 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024970 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024971 else
24972 func_free(fp);
24973 }
24974}
24975
24976/*
24977 * Free a function and remove it from the list of functions.
24978 */
24979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024980func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024981{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024982 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024983
24984 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024985 ga_clear_strings(&(fp->uf_args));
24986 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024987#ifdef FEAT_PROFILE
24988 vim_free(fp->uf_tml_count);
24989 vim_free(fp->uf_tml_total);
24990 vim_free(fp->uf_tml_self);
24991#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024992
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024993 /* remove the function from the function hashtable */
24994 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24995 if (HASHITEM_EMPTY(hi))
24996 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024997 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024998 hash_remove(&func_hashtab, hi);
24999
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025000 vim_free(fp);
25001}
25002
25003/*
25004 * Unreference a Function: decrement the reference count and free it when it
25005 * becomes zero. Only for numbered functions.
25006 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025007 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025008func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025009{
25010 ufunc_T *fp;
25011
25012 if (name != NULL && isdigit(*name))
25013 {
25014 fp = find_func(name);
25015 if (fp == NULL)
25016 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025017 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025018 {
25019 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025020 * when "uf_calls" becomes zero. */
25021 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025022 func_free(fp);
25023 }
25024 }
25025}
25026
25027/*
25028 * Count a reference to a Function.
25029 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025031func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025032{
25033 ufunc_T *fp;
25034
25035 if (name != NULL && isdigit(*name))
25036 {
25037 fp = find_func(name);
25038 if (fp == NULL)
25039 EMSG2(_(e_intern2), "func_ref()");
25040 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025041 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 }
25043}
25044
25045/*
25046 * Call a user function.
25047 */
25048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025049call_user_func(
25050 ufunc_T *fp, /* pointer to function */
25051 int argcount, /* nr of args */
25052 typval_T *argvars, /* arguments */
25053 typval_T *rettv, /* return value */
25054 linenr_T firstline, /* first line of range */
25055 linenr_T lastline, /* last line of range */
25056 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025057{
Bram Moolenaar33570922005-01-25 22:26:29 +000025058 char_u *save_sourcing_name;
25059 linenr_T save_sourcing_lnum;
25060 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025061 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025062 int save_did_emsg;
25063 static int depth = 0;
25064 dictitem_T *v;
25065 int fixvar_idx = 0; /* index in fixvar[] */
25066 int i;
25067 int ai;
25068 char_u numbuf[NUMBUFLEN];
25069 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025070 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025071#ifdef FEAT_PROFILE
25072 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025073 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025074#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075
25076 /* If depth of calling is getting too high, don't execute the function */
25077 if (depth >= p_mfd)
25078 {
25079 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025080 rettv->v_type = VAR_NUMBER;
25081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 return;
25083 }
25084 ++depth;
25085
25086 line_breakcheck(); /* check for CTRL-C hit */
25087
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025088 fc = (funccall_T *)alloc(sizeof(funccall_T));
25089 fc->caller = current_funccal;
25090 current_funccal = fc;
25091 fc->func = fp;
25092 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025093 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025094 fc->linenr = 0;
25095 fc->returned = FALSE;
25096 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025097 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025098 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25099 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025100
Bram Moolenaar33570922005-01-25 22:26:29 +000025101 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025102 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025103 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25104 * each argument variable and saves a lot of time.
25105 */
25106 /*
25107 * Init l: variables.
25108 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025109 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025110 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025111 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025112 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25113 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025114 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025115 name = v->di_key;
25116 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025117 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025118 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025119 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025120 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025121 v->di_tv.vval.v_dict = selfdict;
25122 ++selfdict->dv_refcount;
25123 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025124
Bram Moolenaar33570922005-01-25 22:26:29 +000025125 /*
25126 * Init a: variables.
25127 * Set a:0 to "argcount".
25128 * Set a:000 to a list with room for the "..." arguments.
25129 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025130 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025131 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025132 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025133 /* Use "name" to avoid a warning from some compiler that checks the
25134 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025135 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025136 name = v->di_key;
25137 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025138 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025139 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025140 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025141 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025142 v->di_tv.vval.v_list = &fc->l_varlist;
25143 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25144 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25145 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025146
25147 /*
25148 * Set a:firstline to "firstline" and a:lastline to "lastline".
25149 * Set a:name to named arguments.
25150 * Set a:N to the "..." arguments.
25151 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025152 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025153 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025154 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025155 (varnumber_T)lastline);
25156 for (i = 0; i < argcount; ++i)
25157 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025158 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025159 if (ai < 0)
25160 /* named argument a:name */
25161 name = FUNCARG(fp, i);
25162 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025163 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025164 /* "..." argument a:1, a:2, etc. */
25165 sprintf((char *)numbuf, "%d", ai + 1);
25166 name = numbuf;
25167 }
25168 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25169 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025170 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025171 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25172 }
25173 else
25174 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025175 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25176 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025177 if (v == NULL)
25178 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025179 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025180 }
25181 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025182 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025183
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025184 /* Note: the values are copied directly to avoid alloc/free.
25185 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025187 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025188
25189 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25190 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025191 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25192 fc->l_listitems[ai].li_tv = argvars[i];
25193 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025194 }
25195 }
25196
Bram Moolenaar071d4272004-06-13 20:20:40 +000025197 /* Don't redraw while executing the function. */
25198 ++RedrawingDisabled;
25199 save_sourcing_name = sourcing_name;
25200 save_sourcing_lnum = sourcing_lnum;
25201 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025202 /* need space for function name + ("function " + 3) or "[number]" */
25203 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25204 + STRLEN(fp->uf_name) + 20;
25205 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 if (sourcing_name != NULL)
25207 {
25208 if (save_sourcing_name != NULL
25209 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025210 sprintf((char *)sourcing_name, "%s[%d]..",
25211 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025212 else
25213 STRCPY(sourcing_name, "function ");
25214 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25215
25216 if (p_verbose >= 12)
25217 {
25218 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025219 verbose_enter_scroll();
25220
Bram Moolenaar555b2802005-05-19 21:08:39 +000025221 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025222 if (p_verbose >= 14)
25223 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025224 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025225 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025226 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025227 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228
25229 msg_puts((char_u *)"(");
25230 for (i = 0; i < argcount; ++i)
25231 {
25232 if (i > 0)
25233 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025234 if (argvars[i].v_type == VAR_NUMBER)
25235 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 else
25237 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025238 /* Do not want errors such as E724 here. */
25239 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025240 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025241 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025242 if (s != NULL)
25243 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025244 if (vim_strsize(s) > MSG_BUF_CLEN)
25245 {
25246 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25247 s = buf;
25248 }
25249 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025250 vim_free(tofree);
25251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025252 }
25253 }
25254 msg_puts((char_u *)")");
25255 }
25256 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025257
25258 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025259 --no_wait_return;
25260 }
25261 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025262#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025263 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025264 {
25265 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25266 func_do_profile(fp);
25267 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025268 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025269 {
25270 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025271 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025272 profile_zero(&fp->uf_tm_children);
25273 }
25274 script_prof_save(&wait_start);
25275 }
25276#endif
25277
Bram Moolenaar071d4272004-06-13 20:20:40 +000025278 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025279 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 save_did_emsg = did_emsg;
25281 did_emsg = FALSE;
25282
25283 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025284 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25286
25287 --RedrawingDisabled;
25288
25289 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025290 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025292 clear_tv(rettv);
25293 rettv->v_type = VAR_NUMBER;
25294 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 }
25296
Bram Moolenaar05159a02005-02-26 23:04:13 +000025297#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025298 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025300 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025301 profile_end(&call_start);
25302 profile_sub_wait(&wait_start, &call_start);
25303 profile_add(&fp->uf_tm_total, &call_start);
25304 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025305 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025306 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025307 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25308 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025309 }
25310 }
25311#endif
25312
Bram Moolenaar071d4272004-06-13 20:20:40 +000025313 /* when being verbose, mention the return value */
25314 if (p_verbose >= 12)
25315 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025317 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318
Bram Moolenaar071d4272004-06-13 20:20:40 +000025319 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025320 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025321 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025322 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025323 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025324 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025325 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025326 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025327 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025328 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025329 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025330
Bram Moolenaar555b2802005-05-19 21:08:39 +000025331 /* The value may be very long. Skip the middle part, so that we
25332 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025333 * truncate it at the end. Don't want errors such as E724 here. */
25334 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025335 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025336 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025337 if (s != NULL)
25338 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025339 if (vim_strsize(s) > MSG_BUF_CLEN)
25340 {
25341 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25342 s = buf;
25343 }
25344 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025345 vim_free(tofree);
25346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347 }
25348 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025349
25350 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025351 --no_wait_return;
25352 }
25353
25354 vim_free(sourcing_name);
25355 sourcing_name = save_sourcing_name;
25356 sourcing_lnum = save_sourcing_lnum;
25357 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025358#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025359 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025360 script_prof_restore(&wait_start);
25361#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362
25363 if (p_verbose >= 12 && sourcing_name != NULL)
25364 {
25365 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025366 verbose_enter_scroll();
25367
Bram Moolenaar555b2802005-05-19 21:08:39 +000025368 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025370
25371 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 --no_wait_return;
25373 }
25374
25375 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025376 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025378
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025379 /* If the a:000 list and the l: and a: dicts are not referenced we can
25380 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025381 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25382 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25383 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25384 {
25385 free_funccal(fc, FALSE);
25386 }
25387 else
25388 {
25389 hashitem_T *hi;
25390 listitem_T *li;
25391 int todo;
25392
25393 /* "fc" is still in use. This can happen when returning "a:000" or
25394 * assigning "l:" to a global variable.
25395 * Link "fc" in the list for garbage collection later. */
25396 fc->caller = previous_funccal;
25397 previous_funccal = fc;
25398
25399 /* Make a copy of the a: variables, since we didn't do that above. */
25400 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25401 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25402 {
25403 if (!HASHITEM_EMPTY(hi))
25404 {
25405 --todo;
25406 v = HI2DI(hi);
25407 copy_tv(&v->di_tv, &v->di_tv);
25408 }
25409 }
25410
25411 /* Make a copy of the a:000 items, since we didn't do that above. */
25412 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25413 copy_tv(&li->li_tv, &li->li_tv);
25414 }
25415}
25416
25417/*
25418 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025419 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025420 */
25421 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025422can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025423{
25424 return (fc->l_varlist.lv_copyID != copyID
25425 && fc->l_vars.dv_copyID != copyID
25426 && fc->l_avars.dv_copyID != copyID);
25427}
25428
25429/*
25430 * Free "fc" and what it contains.
25431 */
25432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025433free_funccal(
25434 funccall_T *fc,
25435 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025436{
25437 listitem_T *li;
25438
25439 /* The a: variables typevals may not have been allocated, only free the
25440 * allocated variables. */
25441 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25442
25443 /* free all l: variables */
25444 vars_clear(&fc->l_vars.dv_hashtab);
25445
25446 /* Free the a:000 variables if they were allocated. */
25447 if (free_val)
25448 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25449 clear_tv(&li->li_tv);
25450
25451 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452}
25453
25454/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025455 * Add a number variable "name" to dict "dp" with value "nr".
25456 */
25457 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025458add_nr_var(
25459 dict_T *dp,
25460 dictitem_T *v,
25461 char *name,
25462 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025463{
25464 STRCPY(v->di_key, name);
25465 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25466 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25467 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025468 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025469 v->di_tv.vval.v_number = nr;
25470}
25471
25472/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025473 * ":return [expr]"
25474 */
25475 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025476ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477{
25478 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025479 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 int returning = FALSE;
25481
25482 if (current_funccal == NULL)
25483 {
25484 EMSG(_("E133: :return not inside a function"));
25485 return;
25486 }
25487
25488 if (eap->skip)
25489 ++emsg_skip;
25490
25491 eap->nextcmd = NULL;
25492 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025493 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025494 {
25495 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025496 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025497 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025498 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499 }
25500 /* It's safer to return also on error. */
25501 else if (!eap->skip)
25502 {
25503 /*
25504 * Return unless the expression evaluation has been cancelled due to an
25505 * aborting error, an interrupt, or an exception.
25506 */
25507 if (!aborting())
25508 returning = do_return(eap, FALSE, TRUE, NULL);
25509 }
25510
25511 /* When skipping or the return gets pending, advance to the next command
25512 * in this line (!returning). Otherwise, ignore the rest of the line.
25513 * Following lines will be ignored by get_func_line(). */
25514 if (returning)
25515 eap->nextcmd = NULL;
25516 else if (eap->nextcmd == NULL) /* no argument */
25517 eap->nextcmd = check_nextcmd(arg);
25518
25519 if (eap->skip)
25520 --emsg_skip;
25521}
25522
25523/*
25524 * Return from a function. Possibly makes the return pending. Also called
25525 * for a pending return at the ":endtry" or after returning from an extra
25526 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025527 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025528 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529 * FALSE when the return gets pending.
25530 */
25531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025532do_return(
25533 exarg_T *eap,
25534 int reanimate,
25535 int is_cmd,
25536 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537{
25538 int idx;
25539 struct condstack *cstack = eap->cstack;
25540
25541 if (reanimate)
25542 /* Undo the return. */
25543 current_funccal->returned = FALSE;
25544
25545 /*
25546 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25547 * not in its finally clause (which then is to be executed next) is found.
25548 * In this case, make the ":return" pending for execution at the ":endtry".
25549 * Otherwise, return normally.
25550 */
25551 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25552 if (idx >= 0)
25553 {
25554 cstack->cs_pending[idx] = CSTP_RETURN;
25555
25556 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025557 /* A pending return again gets pending. "rettv" points to an
25558 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025560 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561 else
25562 {
25563 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025564 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025566 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025568 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 {
25570 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025571 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025572 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025573 else
25574 EMSG(_(e_outofmem));
25575 }
25576 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025577 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578
25579 if (reanimate)
25580 {
25581 /* The pending return value could be overwritten by a ":return"
25582 * without argument in a finally clause; reset the default
25583 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025584 current_funccal->rettv->v_type = VAR_NUMBER;
25585 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586 }
25587 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025588 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025589 }
25590 else
25591 {
25592 current_funccal->returned = TRUE;
25593
25594 /* If the return is carried out now, store the return value. For
25595 * a return immediately after reanimation, the value is already
25596 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025597 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025599 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025600 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025601 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025602 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025603 }
25604 }
25605
25606 return idx < 0;
25607}
25608
25609/*
25610 * Free the variable with a pending return value.
25611 */
25612 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025613discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614{
Bram Moolenaar33570922005-01-25 22:26:29 +000025615 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025616}
25617
25618/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025619 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 * is an allocated string. Used by report_pending() for verbose messages.
25621 */
25622 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025623get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025624{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025625 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025626 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025627 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025629 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025630 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025631 if (s == NULL)
25632 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025633
25634 STRCPY(IObuff, ":return ");
25635 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25636 if (STRLEN(s) + 8 >= IOSIZE)
25637 STRCPY(IObuff + IOSIZE - 4, "...");
25638 vim_free(tofree);
25639 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640}
25641
25642/*
25643 * Get next function line.
25644 * Called by do_cmdline() to get the next line.
25645 * Returns allocated string, or NULL for end of function.
25646 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025648get_func_line(
25649 int c UNUSED,
25650 void *cookie,
25651 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025652{
Bram Moolenaar33570922005-01-25 22:26:29 +000025653 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025654 ufunc_T *fp = fcp->func;
25655 char_u *retval;
25656 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025657
25658 /* If breakpoints have been added/deleted need to check for it. */
25659 if (fcp->dbg_tick != debug_tick)
25660 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025661 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662 sourcing_lnum);
25663 fcp->dbg_tick = debug_tick;
25664 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025665#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025666 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025667 func_line_end(cookie);
25668#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025669
Bram Moolenaar05159a02005-02-26 23:04:13 +000025670 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025671 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25672 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 retval = NULL;
25674 else
25675 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025676 /* Skip NULL lines (continuation lines). */
25677 while (fcp->linenr < gap->ga_len
25678 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25679 ++fcp->linenr;
25680 if (fcp->linenr >= gap->ga_len)
25681 retval = NULL;
25682 else
25683 {
25684 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25685 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025686#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025687 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025688 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025689#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025691 }
25692
25693 /* Did we encounter a breakpoint? */
25694 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25695 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025696 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025698 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025699 sourcing_lnum);
25700 fcp->dbg_tick = debug_tick;
25701 }
25702
25703 return retval;
25704}
25705
Bram Moolenaar05159a02005-02-26 23:04:13 +000025706#if defined(FEAT_PROFILE) || defined(PROTO)
25707/*
25708 * Called when starting to read a function line.
25709 * "sourcing_lnum" must be correct!
25710 * When skipping lines it may not actually be executed, but we won't find out
25711 * until later and we need to store the time now.
25712 */
25713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025714func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025715{
25716 funccall_T *fcp = (funccall_T *)cookie;
25717 ufunc_T *fp = fcp->func;
25718
25719 if (fp->uf_profiling && sourcing_lnum >= 1
25720 && sourcing_lnum <= fp->uf_lines.ga_len)
25721 {
25722 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025723 /* Skip continuation lines. */
25724 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25725 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025726 fp->uf_tml_execed = FALSE;
25727 profile_start(&fp->uf_tml_start);
25728 profile_zero(&fp->uf_tml_children);
25729 profile_get_wait(&fp->uf_tml_wait);
25730 }
25731}
25732
25733/*
25734 * Called when actually executing a function line.
25735 */
25736 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025737func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025738{
25739 funccall_T *fcp = (funccall_T *)cookie;
25740 ufunc_T *fp = fcp->func;
25741
25742 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25743 fp->uf_tml_execed = TRUE;
25744}
25745
25746/*
25747 * Called when done with a function line.
25748 */
25749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025750func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025751{
25752 funccall_T *fcp = (funccall_T *)cookie;
25753 ufunc_T *fp = fcp->func;
25754
25755 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25756 {
25757 if (fp->uf_tml_execed)
25758 {
25759 ++fp->uf_tml_count[fp->uf_tml_idx];
25760 profile_end(&fp->uf_tml_start);
25761 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025762 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025763 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25764 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025765 }
25766 fp->uf_tml_idx = -1;
25767 }
25768}
25769#endif
25770
Bram Moolenaar071d4272004-06-13 20:20:40 +000025771/*
25772 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025773 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025774 */
25775 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025776func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777{
Bram Moolenaar33570922005-01-25 22:26:29 +000025778 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779
25780 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25781 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025782 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025783 || fcp->returned);
25784}
25785
25786/*
25787 * return TRUE if cookie indicates a function which "abort"s on errors.
25788 */
25789 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025790func_has_abort(
25791 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025792{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025793 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025794}
25795
25796#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25797typedef enum
25798{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025799 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25800 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25801 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802} var_flavour_T;
25803
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025804static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805
25806 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025807var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808{
25809 char_u *p = varname;
25810
25811 if (ASCII_ISUPPER(*p))
25812 {
25813 while (*(++p))
25814 if (ASCII_ISLOWER(*p))
25815 return VAR_FLAVOUR_SESSION;
25816 return VAR_FLAVOUR_VIMINFO;
25817 }
25818 else
25819 return VAR_FLAVOUR_DEFAULT;
25820}
25821#endif
25822
25823#if defined(FEAT_VIMINFO) || defined(PROTO)
25824/*
25825 * Restore global vars that start with a capital from the viminfo file
25826 */
25827 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025828read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829{
25830 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025831 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025832 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025833 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834
25835 if (!writing && (find_viminfo_parameter('!') != NULL))
25836 {
25837 tab = vim_strchr(virp->vir_line + 1, '\t');
25838 if (tab != NULL)
25839 {
25840 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025841 switch (*tab)
25842 {
25843 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025844#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025845 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025846#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025847 case 'D': type = VAR_DICT; break;
25848 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025849 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025851
25852 tab = vim_strchr(tab, '\t');
25853 if (tab != NULL)
25854 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025855 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025856 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025857 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025859#ifdef FEAT_FLOAT
25860 else if (type == VAR_FLOAT)
25861 (void)string2float(tab + 1, &tv.vval.v_float);
25862#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025863 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025864 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025865 if (type == VAR_DICT || type == VAR_LIST)
25866 {
25867 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25868
25869 if (etv == NULL)
25870 /* Failed to parse back the dict or list, use it as a
25871 * string. */
25872 tv.v_type = VAR_STRING;
25873 else
25874 {
25875 vim_free(tv.vval.v_string);
25876 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025877 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025878 }
25879 }
25880
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025881 /* when in a function use global variables */
25882 save_funccal = current_funccal;
25883 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025884 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025885 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025886
25887 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025888 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025889 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25890 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 }
25892 }
25893 }
25894
25895 return viminfo_readline(virp);
25896}
25897
25898/*
25899 * Write global vars that start with a capital to the viminfo file
25900 */
25901 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025902write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025903{
Bram Moolenaar33570922005-01-25 22:26:29 +000025904 hashitem_T *hi;
25905 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025906 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025907 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025908 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025909 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025910 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025911
25912 if (find_viminfo_parameter('!') == NULL)
25913 return;
25914
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025915 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025916
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025917 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025918 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025919 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025920 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025921 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025922 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025923 this_var = HI2DI(hi);
25924 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025925 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025926 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025927 {
25928 case VAR_STRING: s = "STR"; break;
25929 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025930 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025931 case VAR_DICT: s = "DIC"; break;
25932 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025933 case VAR_SPECIAL: s = "XPL"; break;
25934
25935 case VAR_UNKNOWN:
25936 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025937 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025938 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025939 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025940 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025941 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025942 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025943 if (p != NULL)
25944 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025945 vim_free(tofree);
25946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 }
25948 }
25949}
25950#endif
25951
25952#if defined(FEAT_SESSION) || defined(PROTO)
25953 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025954store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955{
Bram Moolenaar33570922005-01-25 22:26:29 +000025956 hashitem_T *hi;
25957 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025958 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 char_u *p, *t;
25960
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025961 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025962 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025963 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025964 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025965 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025966 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025967 this_var = HI2DI(hi);
25968 if ((this_var->di_tv.v_type == VAR_NUMBER
25969 || this_var->di_tv.v_type == VAR_STRING)
25970 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025971 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025972 /* Escape special characters with a backslash. Turn a LF and
25973 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025974 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025975 (char_u *)"\\\"\n\r");
25976 if (p == NULL) /* out of memory */
25977 break;
25978 for (t = p; *t != NUL; ++t)
25979 if (*t == '\n')
25980 *t = 'n';
25981 else if (*t == '\r')
25982 *t = 'r';
25983 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025984 this_var->di_key,
25985 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25986 : ' ',
25987 p,
25988 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25989 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025990 || put_eol(fd) == FAIL)
25991 {
25992 vim_free(p);
25993 return FAIL;
25994 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025995 vim_free(p);
25996 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025997#ifdef FEAT_FLOAT
25998 else if (this_var->di_tv.v_type == VAR_FLOAT
25999 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
26000 {
26001 float_T f = this_var->di_tv.vval.v_float;
26002 int sign = ' ';
26003
26004 if (f < 0)
26005 {
26006 f = -f;
26007 sign = '-';
26008 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010026009 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026010 this_var->di_key, sign, f) < 0)
26011 || put_eol(fd) == FAIL)
26012 return FAIL;
26013 }
26014#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015 }
26016 }
26017 return OK;
26018}
26019#endif
26020
Bram Moolenaar661b1822005-07-28 22:36:45 +000026021/*
26022 * Display script name where an item was last set.
26023 * Should only be invoked when 'verbose' is non-zero.
26024 */
26025 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026026last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000026027{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026028 char_u *p;
26029
Bram Moolenaar661b1822005-07-28 22:36:45 +000026030 if (scriptID != 0)
26031 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026032 p = home_replace_save(NULL, get_scriptname(scriptID));
26033 if (p != NULL)
26034 {
26035 verbose_enter();
26036 MSG_PUTS(_("\n\tLast set from "));
26037 MSG_PUTS(p);
26038 vim_free(p);
26039 verbose_leave();
26040 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026041 }
26042}
26043
Bram Moolenaard812df62008-11-09 12:46:09 +000026044/*
26045 * List v:oldfiles in a nice way.
26046 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026047 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026048ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026049{
26050 list_T *l = vimvars[VV_OLDFILES].vv_list;
26051 listitem_T *li;
26052 int nr = 0;
26053
26054 if (l == NULL)
26055 msg((char_u *)_("No old files"));
26056 else
26057 {
26058 msg_start();
26059 msg_scroll = TRUE;
26060 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26061 {
26062 msg_outnum((long)++nr);
26063 MSG_PUTS(": ");
26064 msg_outtrans(get_tv_string(&li->li_tv));
26065 msg_putchar('\n');
26066 out_flush(); /* output one line at a time */
26067 ui_breakcheck();
26068 }
26069 /* Assume "got_int" was set to truncate the listing. */
26070 got_int = FALSE;
26071
26072#ifdef FEAT_BROWSE_CMD
26073 if (cmdmod.browse)
26074 {
26075 quit_more = FALSE;
26076 nr = prompt_for_number(FALSE);
26077 msg_starthere();
26078 if (nr > 0)
26079 {
26080 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26081 (long)nr);
26082
26083 if (p != NULL)
26084 {
26085 p = expand_env_save(p);
26086 eap->arg = p;
26087 eap->cmdidx = CMD_edit;
26088 cmdmod.browse = FALSE;
26089 do_exedit(eap, NULL);
26090 vim_free(p);
26091 }
26092 }
26093 }
26094#endif
26095 }
26096}
26097
Bram Moolenaar53744302015-07-17 17:38:22 +020026098/* reset v:option_new, v:option_old and v:option_type */
26099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026100reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026101{
26102 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26103 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26104 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26105}
26106
26107
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108#endif /* FEAT_EVAL */
26109
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026111#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112
26113#ifdef WIN3264
26114/*
26115 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26116 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026117static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26118static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26119static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120
26121/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026122 * Get the short path (8.3) for the filename in "fnamep".
26123 * Only works for a valid file name.
26124 * When the path gets longer "fnamep" is changed and the allocated buffer
26125 * is put in "bufp".
26126 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26127 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026128 */
26129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026130get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026132 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026133 char_u *newbuf;
26134
26135 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026136 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026137 if (l > len - 1)
26138 {
26139 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026140 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 newbuf = vim_strnsave(*fnamep, l);
26142 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026143 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144
26145 vim_free(*bufp);
26146 *fnamep = *bufp = newbuf;
26147
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026148 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026149 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150 }
26151
26152 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026153 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154}
26155
26156/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026157 * Get the short path (8.3) for the filename in "fname". The converted
26158 * path is returned in "bufp".
26159 *
26160 * Some of the directories specified in "fname" may not exist. This function
26161 * will shorten the existing directories at the beginning of the path and then
26162 * append the remaining non-existing path.
26163 *
26164 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026165 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026166 * bufp - Pointer to an allocated buffer for the filename.
26167 * fnamelen - Length of the filename pointed to by fname
26168 *
26169 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 */
26171 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026172shortpath_for_invalid_fname(
26173 char_u **fname,
26174 char_u **bufp,
26175 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026176{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026177 char_u *short_fname, *save_fname, *pbuf_unused;
26178 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026179 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026180 int old_len, len;
26181 int new_len, sfx_len;
26182 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183
26184 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026185 old_len = *fnamelen;
26186 save_fname = vim_strnsave(*fname, old_len);
26187 pbuf_unused = NULL;
26188 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026189
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026190 endp = save_fname + old_len - 1; /* Find the end of the copy */
26191 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026193 /*
26194 * Try shortening the supplied path till it succeeds by removing one
26195 * directory at a time from the tail of the path.
26196 */
26197 len = 0;
26198 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026199 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026200 /* go back one path-separator */
26201 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26202 --endp;
26203 if (endp <= save_fname)
26204 break; /* processed the complete path */
26205
26206 /*
26207 * Replace the path separator with a NUL and try to shorten the
26208 * resulting path.
26209 */
26210 ch = *endp;
26211 *endp = 0;
26212 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026213 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026214 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26215 {
26216 retval = FAIL;
26217 goto theend;
26218 }
26219 *endp = ch; /* preserve the string */
26220
26221 if (len > 0)
26222 break; /* successfully shortened the path */
26223
26224 /* failed to shorten the path. Skip the path separator */
26225 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026226 }
26227
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026228 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026229 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026230 /*
26231 * Succeeded in shortening the path. Now concatenate the shortened
26232 * path with the remaining path at the tail.
26233 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026234
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026235 /* Compute the length of the new path. */
26236 sfx_len = (int)(save_endp - endp) + 1;
26237 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026238
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026239 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026240 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026241 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026242 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026243 /* There is not enough space in the currently allocated string,
26244 * copy it to a buffer big enough. */
26245 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026246 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026247 {
26248 retval = FAIL;
26249 goto theend;
26250 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026251 }
26252 else
26253 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026254 /* Transfer short_fname to the main buffer (it's big enough),
26255 * unless get_short_pathname() did its work in-place. */
26256 *fname = *bufp = save_fname;
26257 if (short_fname != save_fname)
26258 vim_strncpy(save_fname, short_fname, len);
26259 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026260 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026261
26262 /* concat the not-shortened part of the path */
26263 vim_strncpy(*fname + len, endp, sfx_len);
26264 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026265 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026266
26267theend:
26268 vim_free(pbuf_unused);
26269 vim_free(save_fname);
26270
26271 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026272}
26273
26274/*
26275 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026277 */
26278 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026279shortpath_for_partial(
26280 char_u **fnamep,
26281 char_u **bufp,
26282 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026283{
26284 int sepcount, len, tflen;
26285 char_u *p;
26286 char_u *pbuf, *tfname;
26287 int hasTilde;
26288
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026289 /* Count up the path separators from the RHS.. so we know which part
26290 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026291 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026292 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 if (vim_ispathsep(*p))
26294 ++sepcount;
26295
26296 /* Need full path first (use expand_env() to remove a "~/") */
26297 hasTilde = (**fnamep == '~');
26298 if (hasTilde)
26299 pbuf = tfname = expand_env_save(*fnamep);
26300 else
26301 pbuf = tfname = FullName_save(*fnamep, FALSE);
26302
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026303 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026304
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026305 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26306 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026307
26308 if (len == 0)
26309 {
26310 /* Don't have a valid filename, so shorten the rest of the
26311 * path if we can. This CAN give us invalid 8.3 filenames, but
26312 * there's not a lot of point in guessing what it might be.
26313 */
26314 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026315 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26316 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 }
26318
26319 /* Count the paths backward to find the beginning of the desired string. */
26320 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026321 {
26322#ifdef FEAT_MBYTE
26323 if (has_mbyte)
26324 p -= mb_head_off(tfname, p);
26325#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326 if (vim_ispathsep(*p))
26327 {
26328 if (sepcount == 0 || (hasTilde && sepcount == 1))
26329 break;
26330 else
26331 sepcount --;
26332 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026334 if (hasTilde)
26335 {
26336 --p;
26337 if (p >= tfname)
26338 *p = '~';
26339 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026341 }
26342 else
26343 ++p;
26344
26345 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26346 vim_free(*bufp);
26347 *fnamelen = (int)STRLEN(p);
26348 *bufp = pbuf;
26349 *fnamep = p;
26350
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026351 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352}
26353#endif /* WIN3264 */
26354
26355/*
26356 * Adjust a filename, according to a string of modifiers.
26357 * *fnamep must be NUL terminated when called. When returning, the length is
26358 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026359 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 * When there is an error, *fnamep is set to NULL.
26361 */
26362 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026363modify_fname(
26364 char_u *src, /* string with modifiers */
26365 int *usedlen, /* characters after src that are used */
26366 char_u **fnamep, /* file name so far */
26367 char_u **bufp, /* buffer for allocated file name or NULL */
26368 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369{
26370 int valid = 0;
26371 char_u *tail;
26372 char_u *s, *p, *pbuf;
26373 char_u dirname[MAXPATHL];
26374 int c;
26375 int has_fullname = 0;
26376#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026377 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026378 int has_shortname = 0;
26379#endif
26380
26381repeat:
26382 /* ":p" - full path/file_name */
26383 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26384 {
26385 has_fullname = 1;
26386
26387 valid |= VALID_PATH;
26388 *usedlen += 2;
26389
26390 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26391 if ((*fnamep)[0] == '~'
26392#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26393 && ((*fnamep)[1] == '/'
26394# ifdef BACKSLASH_IN_FILENAME
26395 || (*fnamep)[1] == '\\'
26396# endif
26397 || (*fnamep)[1] == NUL)
26398
26399#endif
26400 )
26401 {
26402 *fnamep = expand_env_save(*fnamep);
26403 vim_free(*bufp); /* free any allocated file name */
26404 *bufp = *fnamep;
26405 if (*fnamep == NULL)
26406 return -1;
26407 }
26408
26409 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026410 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026411 {
26412 if (vim_ispathsep(*p)
26413 && p[1] == '.'
26414 && (p[2] == NUL
26415 || vim_ispathsep(p[2])
26416 || (p[2] == '.'
26417 && (p[3] == NUL || vim_ispathsep(p[3])))))
26418 break;
26419 }
26420
26421 /* FullName_save() is slow, don't use it when not needed. */
26422 if (*p != NUL || !vim_isAbsName(*fnamep))
26423 {
26424 *fnamep = FullName_save(*fnamep, *p != NUL);
26425 vim_free(*bufp); /* free any allocated file name */
26426 *bufp = *fnamep;
26427 if (*fnamep == NULL)
26428 return -1;
26429 }
26430
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026431#ifdef WIN3264
26432# if _WIN32_WINNT >= 0x0500
26433 if (vim_strchr(*fnamep, '~') != NULL)
26434 {
26435 /* Expand 8.3 filename to full path. Needed to make sure the same
26436 * file does not have two different names.
26437 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26438 p = alloc(_MAX_PATH + 1);
26439 if (p != NULL)
26440 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026441 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026442 {
26443 vim_free(*bufp);
26444 *bufp = *fnamep = p;
26445 }
26446 else
26447 vim_free(p);
26448 }
26449 }
26450# endif
26451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026452 /* Append a path separator to a directory. */
26453 if (mch_isdir(*fnamep))
26454 {
26455 /* Make room for one or two extra characters. */
26456 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26457 vim_free(*bufp); /* free any allocated file name */
26458 *bufp = *fnamep;
26459 if (*fnamep == NULL)
26460 return -1;
26461 add_pathsep(*fnamep);
26462 }
26463 }
26464
26465 /* ":." - path relative to the current directory */
26466 /* ":~" - path relative to the home directory */
26467 /* ":8" - shortname path - postponed till after */
26468 while (src[*usedlen] == ':'
26469 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26470 {
26471 *usedlen += 2;
26472 if (c == '8')
26473 {
26474#ifdef WIN3264
26475 has_shortname = 1; /* Postpone this. */
26476#endif
26477 continue;
26478 }
26479 pbuf = NULL;
26480 /* Need full path first (use expand_env() to remove a "~/") */
26481 if (!has_fullname)
26482 {
26483 if (c == '.' && **fnamep == '~')
26484 p = pbuf = expand_env_save(*fnamep);
26485 else
26486 p = pbuf = FullName_save(*fnamep, FALSE);
26487 }
26488 else
26489 p = *fnamep;
26490
26491 has_fullname = 0;
26492
26493 if (p != NULL)
26494 {
26495 if (c == '.')
26496 {
26497 mch_dirname(dirname, MAXPATHL);
26498 s = shorten_fname(p, dirname);
26499 if (s != NULL)
26500 {
26501 *fnamep = s;
26502 if (pbuf != NULL)
26503 {
26504 vim_free(*bufp); /* free any allocated file name */
26505 *bufp = pbuf;
26506 pbuf = NULL;
26507 }
26508 }
26509 }
26510 else
26511 {
26512 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26513 /* Only replace it when it starts with '~' */
26514 if (*dirname == '~')
26515 {
26516 s = vim_strsave(dirname);
26517 if (s != NULL)
26518 {
26519 *fnamep = s;
26520 vim_free(*bufp);
26521 *bufp = s;
26522 }
26523 }
26524 }
26525 vim_free(pbuf);
26526 }
26527 }
26528
26529 tail = gettail(*fnamep);
26530 *fnamelen = (int)STRLEN(*fnamep);
26531
26532 /* ":h" - head, remove "/file_name", can be repeated */
26533 /* Don't remove the first "/" or "c:\" */
26534 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26535 {
26536 valid |= VALID_HEAD;
26537 *usedlen += 2;
26538 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026539 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026540 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026541 *fnamelen = (int)(tail - *fnamep);
26542#ifdef VMS
26543 if (*fnamelen > 0)
26544 *fnamelen += 1; /* the path separator is part of the path */
26545#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026546 if (*fnamelen == 0)
26547 {
26548 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26549 p = vim_strsave((char_u *)".");
26550 if (p == NULL)
26551 return -1;
26552 vim_free(*bufp);
26553 *bufp = *fnamep = tail = p;
26554 *fnamelen = 1;
26555 }
26556 else
26557 {
26558 while (tail > s && !after_pathsep(s, tail))
26559 mb_ptr_back(*fnamep, tail);
26560 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026561 }
26562
26563 /* ":8" - shortname */
26564 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26565 {
26566 *usedlen += 2;
26567#ifdef WIN3264
26568 has_shortname = 1;
26569#endif
26570 }
26571
26572#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026573 /*
26574 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 */
26576 if (has_shortname)
26577 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026578 /* Copy the string if it is shortened by :h and when it wasn't copied
26579 * yet, because we are going to change it in place. Avoids changing
26580 * the buffer name for "%:8". */
26581 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 {
26583 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026584 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026585 return -1;
26586 vim_free(*bufp);
26587 *bufp = *fnamep = p;
26588 }
26589
26590 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026591 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592 if (!has_fullname && !vim_isAbsName(*fnamep))
26593 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026594 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026595 return -1;
26596 }
26597 else
26598 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026599 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026600
Bram Moolenaardc935552011-08-17 15:23:23 +020026601 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026602 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026603 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026604 return -1;
26605
26606 if (l == 0)
26607 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026608 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026609 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026610 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026611 return -1;
26612 }
26613 *fnamelen = l;
26614 }
26615 }
26616#endif /* WIN3264 */
26617
26618 /* ":t" - tail, just the basename */
26619 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26620 {
26621 *usedlen += 2;
26622 *fnamelen -= (int)(tail - *fnamep);
26623 *fnamep = tail;
26624 }
26625
26626 /* ":e" - extension, can be repeated */
26627 /* ":r" - root, without extension, can be repeated */
26628 while (src[*usedlen] == ':'
26629 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26630 {
26631 /* find a '.' in the tail:
26632 * - for second :e: before the current fname
26633 * - otherwise: The last '.'
26634 */
26635 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26636 s = *fnamep - 2;
26637 else
26638 s = *fnamep + *fnamelen - 1;
26639 for ( ; s > tail; --s)
26640 if (s[0] == '.')
26641 break;
26642 if (src[*usedlen + 1] == 'e') /* :e */
26643 {
26644 if (s > tail)
26645 {
26646 *fnamelen += (int)(*fnamep - (s + 1));
26647 *fnamep = s + 1;
26648#ifdef VMS
26649 /* cut version from the extension */
26650 s = *fnamep + *fnamelen - 1;
26651 for ( ; s > *fnamep; --s)
26652 if (s[0] == ';')
26653 break;
26654 if (s > *fnamep)
26655 *fnamelen = s - *fnamep;
26656#endif
26657 }
26658 else if (*fnamep <= tail)
26659 *fnamelen = 0;
26660 }
26661 else /* :r */
26662 {
26663 if (s > tail) /* remove one extension */
26664 *fnamelen = (int)(s - *fnamep);
26665 }
26666 *usedlen += 2;
26667 }
26668
26669 /* ":s?pat?foo?" - substitute */
26670 /* ":gs?pat?foo?" - global substitute */
26671 if (src[*usedlen] == ':'
26672 && (src[*usedlen + 1] == 's'
26673 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26674 {
26675 char_u *str;
26676 char_u *pat;
26677 char_u *sub;
26678 int sep;
26679 char_u *flags;
26680 int didit = FALSE;
26681
26682 flags = (char_u *)"";
26683 s = src + *usedlen + 2;
26684 if (src[*usedlen + 1] == 'g')
26685 {
26686 flags = (char_u *)"g";
26687 ++s;
26688 }
26689
26690 sep = *s++;
26691 if (sep)
26692 {
26693 /* find end of pattern */
26694 p = vim_strchr(s, sep);
26695 if (p != NULL)
26696 {
26697 pat = vim_strnsave(s, (int)(p - s));
26698 if (pat != NULL)
26699 {
26700 s = p + 1;
26701 /* find end of substitution */
26702 p = vim_strchr(s, sep);
26703 if (p != NULL)
26704 {
26705 sub = vim_strnsave(s, (int)(p - s));
26706 str = vim_strnsave(*fnamep, *fnamelen);
26707 if (sub != NULL && str != NULL)
26708 {
26709 *usedlen = (int)(p + 1 - src);
26710 s = do_string_sub(str, pat, sub, flags);
26711 if (s != NULL)
26712 {
26713 *fnamep = s;
26714 *fnamelen = (int)STRLEN(s);
26715 vim_free(*bufp);
26716 *bufp = s;
26717 didit = TRUE;
26718 }
26719 }
26720 vim_free(sub);
26721 vim_free(str);
26722 }
26723 vim_free(pat);
26724 }
26725 }
26726 /* after using ":s", repeat all the modifiers */
26727 if (didit)
26728 goto repeat;
26729 }
26730 }
26731
Bram Moolenaar26df0922014-02-23 23:39:13 +010026732 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26733 {
26734 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26735 if (p == NULL)
26736 return -1;
26737 vim_free(*bufp);
26738 *bufp = *fnamep = p;
26739 *fnamelen = (int)STRLEN(p);
26740 *usedlen += 2;
26741 }
26742
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743 return valid;
26744}
26745
26746/*
26747 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26748 * "flags" can be "g" to do a global substitute.
26749 * Returns an allocated string, NULL for error.
26750 */
26751 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026752do_string_sub(
26753 char_u *str,
26754 char_u *pat,
26755 char_u *sub,
26756 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026757{
26758 int sublen;
26759 regmatch_T regmatch;
26760 int i;
26761 int do_all;
26762 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026763 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026764 garray_T ga;
26765 char_u *ret;
26766 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026767 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026768
26769 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26770 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026771 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026772
26773 ga_init2(&ga, 1, 200);
26774
26775 do_all = (flags[0] == 'g');
26776
26777 regmatch.rm_ic = p_ic;
26778 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26779 if (regmatch.regprog != NULL)
26780 {
26781 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026782 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026783 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26784 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026785 /* Skip empty match except for first match. */
26786 if (regmatch.startp[0] == regmatch.endp[0])
26787 {
26788 if (zero_width == regmatch.startp[0])
26789 {
26790 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026791 i = MB_PTR2LEN(tail);
26792 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26793 (size_t)i);
26794 ga.ga_len += i;
26795 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026796 continue;
26797 }
26798 zero_width = regmatch.startp[0];
26799 }
26800
Bram Moolenaar071d4272004-06-13 20:20:40 +000026801 /*
26802 * Get some space for a temporary buffer to do the substitution
26803 * into. It will contain:
26804 * - The text up to where the match is.
26805 * - The substituted text.
26806 * - The text after the match.
26807 */
26808 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026809 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026810 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26811 {
26812 ga_clear(&ga);
26813 break;
26814 }
26815
26816 /* copy the text up to where the match is */
26817 i = (int)(regmatch.startp[0] - tail);
26818 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26819 /* add the substituted text */
26820 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26821 + ga.ga_len + i, TRUE, TRUE, FALSE);
26822 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026823 tail = regmatch.endp[0];
26824 if (*tail == NUL)
26825 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026826 if (!do_all)
26827 break;
26828 }
26829
26830 if (ga.ga_data != NULL)
26831 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26832
Bram Moolenaar473de612013-06-08 18:19:48 +020026833 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026834 }
26835
26836 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26837 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026838 if (p_cpo == empty_option)
26839 p_cpo = save_cpo;
26840 else
26841 /* Darn, evaluating {sub} expression changed the value. */
26842 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026843
26844 return ret;
26845}
26846
26847#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */