blob: 323caf1e50af0cd3c5001b931d76499c19eef11d [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 Moolenaar071d4272004-06-13 20:20:40 +000013
14#include "vim.h"
15
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016#if defined(FEAT_EVAL) || defined(PROTO)
17
Bram Moolenaar071d4272004-06-13 20:20:40 +000018#ifdef AMIGA
19# include <time.h> /* for strftime() */
20#endif
21
Bram Moolenaar314f11d2010-08-09 22:07:08 +020022#ifdef VMS
23# include <float.h>
24#endif
25
Bram Moolenaar071d4272004-06-13 20:20:40 +000026#ifdef MACOS
27# include <time.h> /* for time_t */
28#endif
29
Bram Moolenaar3ea0f1a2016-02-23 22:07:32 +010030#if defined(FEAT_FLOAT)
31# include <float.h>
32# if defined(HAVE_MATH_H)
33# include <math.h>
34# endif
35# if defined(WIN32) && !defined(isnan)
36# define isnan(x) _isnan(x)
37# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000038#endif
39
Bram Moolenaar33570922005-01-25 22:26:29 +000040#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000041
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000042#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
43 be freed. */
44
Bram Moolenaar071d4272004-06-13 20:20:40 +000045/*
Bram Moolenaar33570922005-01-25 22:26:29 +000046 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
47 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000048 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
49 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
50 * HI2DI() converts a hashitem pointer to a dictitem pointer.
51 */
Bram Moolenaar33570922005-01-25 22:26:29 +000052static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000053#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000054#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000055#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000056
57/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000058 * Structure returned by get_lval() and used by set_var_lval().
59 * For a plain name:
60 * "name" points to the variable name.
61 * "exp_name" is NULL.
62 * "tv" is NULL
63 * For a magic braces name:
64 * "name" points to the expanded variable name.
65 * "exp_name" is non-NULL, to be freed later.
66 * "tv" is NULL
67 * For an index in a list:
68 * "name" points to the (expanded) variable name.
69 * "exp_name" NULL or non-NULL, to be freed later.
70 * "tv" points to the (first) list item value
71 * "li" points to the (first) list item
72 * "range", "n1", "n2" and "empty2" indicate what items are used.
73 * For an existing Dict item:
74 * "name" points to the (expanded) variable name.
75 * "exp_name" NULL or non-NULL, to be freed later.
76 * "tv" points to the dict item value
77 * "newkey" is NULL
78 * For a non-existing Dict item:
79 * "name" points to the (expanded) variable name.
80 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000081 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000082 * "newkey" is the key for the new item.
83 */
84typedef struct lval_S
85{
86 char_u *ll_name; /* start of variable name (can be NULL) */
87 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000089 isn't NULL it's the Dict to which to add
90 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091 listitem_T *ll_li; /* The list item or NULL. */
92 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000093 int ll_range; /* TRUE when a [i:j] range was used */
94 long ll_n1; /* First index for list */
95 long ll_n2; /* Second index for list range */
96 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000097 dict_T *ll_dict; /* The Dictionary or NULL */
98 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +0000100} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +0000101
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000102static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +0000103static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +0000104static char *e_undefvar = N_("E121: Undefined variable: %s");
105static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000106static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +0000107static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000108static char *e_listreq = N_("E714: List required");
109static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000110static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000111static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
112static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
113static char *e_funcdict = N_("E717: Dictionary entry already exists");
114static char *e_funcref = N_("E718: Funcref required");
115static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
116static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000117static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000118static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200119#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200120static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200121#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000122
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100123#define NAMESPACE_CHAR (char_u *)"abglstvw"
124
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200125static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000126#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000127
128/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000129 * Old Vim variables such as "v:version" are also available without the "v:".
130 * Also in functions. We need a special hashtable for them.
131 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000132static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000133
134/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000135 * When recursively copying lists and dicts we need to remember which ones we
136 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000137 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000138 */
139static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000140#define COPYID_INC 2
141#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000142
Bram Moolenaar8502c702014-06-17 12:51:16 +0200143/* Abort conversion to string after a recursion error. */
144static int did_echo_string_emsg = FALSE;
145
Bram Moolenaard9fba312005-06-26 22:34:35 +0000146/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000147 * Array to hold the hashtab with variables local to each sourced script.
148 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000149 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000150typedef struct
151{
152 dictitem_T sv_var;
153 dict_T sv_dict;
154} scriptvar_T;
155
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200156static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
157#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
158#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000159
160static int echo_attr = 0; /* attributes used for ":echo" */
161
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000162/* Values for trans_function_name() argument: */
163#define TFN_INT 1 /* internal function name OK */
164#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100165#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
166
167/* Values for get_lval() flags argument: */
168#define GLV_QUIET TFN_QUIET /* no error messages */
169#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000170
Bram Moolenaar071d4272004-06-13 20:20:40 +0000171/*
172 * Structure to hold info for a user function.
173 */
174typedef struct ufunc ufunc_T;
175
176struct ufunc
177{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000178 int uf_varargs; /* variable nr of arguments */
179 int uf_flags;
180 int uf_calls; /* nr of active calls */
181 garray_T uf_args; /* arguments */
182 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000183#ifdef FEAT_PROFILE
184 int uf_profiling; /* TRUE when func is being profiled */
185 /* profiling the function as a whole */
186 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000187 proftime_T uf_tm_total; /* time spent in function + children */
188 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000189 proftime_T uf_tm_children; /* time spent in children this call */
190 /* profiling the function per line */
191 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000192 proftime_T *uf_tml_total; /* time spent in a line + children */
193 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000194 proftime_T uf_tml_start; /* start time for current line */
195 proftime_T uf_tml_children; /* time spent in children for this line */
196 proftime_T uf_tml_wait; /* start wait time for current line */
197 int uf_tml_idx; /* index of line being timed; -1 if none */
198 int uf_tml_execed; /* line being timed was executed */
199#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000200 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000201 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000202 int uf_refcount; /* for numbered function: reference count */
203 char_u uf_name[1]; /* name of function (actually longer); can
204 start with <SNR>123_ (<SNR> is K_SPECIAL
205 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206};
207
208/* function flags */
209#define FC_ABORT 1 /* abort function on error */
210#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000211#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000212
213/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000214 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000216static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000217
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000218/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000219static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
220
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000221/* list heads for garbage collection */
222static dict_T *first_dict = NULL; /* list of all dicts */
223static list_T *first_list = NULL; /* list of all lists */
224
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000225/* From user function to hashitem and back. */
226static ufunc_T dumuf;
227#define UF2HIKEY(fp) ((fp)->uf_name)
228#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
229#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
230
231#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
232#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233
Bram Moolenaar33570922005-01-25 22:26:29 +0000234#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
235#define VAR_SHORT_LEN 20 /* short variable name length */
236#define FIXVAR_CNT 12 /* number of fixed variables */
237
Bram Moolenaar071d4272004-06-13 20:20:40 +0000238/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000239typedef struct funccall_S funccall_T;
240
241struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000242{
243 ufunc_T *func; /* function being called */
244 int linenr; /* next line to be executed */
245 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000246 struct /* fixed variables for arguments */
247 {
248 dictitem_T var; /* variable (without room for name) */
249 char_u room[VAR_SHORT_LEN]; /* room for the name */
250 } fixvar[FIXVAR_CNT];
251 dict_T l_vars; /* l: local function variables */
252 dictitem_T l_vars_var; /* variable for l: scope */
253 dict_T l_avars; /* a: argument variables */
254 dictitem_T l_avars_var; /* variable for a: scope */
255 list_T l_varlist; /* list for a:000 */
256 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
257 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000258 linenr_T breakpoint; /* next line with breakpoint or zero */
259 int dbg_tick; /* debug_tick when breakpoint was set */
260 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000261#ifdef FEAT_PROFILE
262 proftime_T prof_child; /* time spent in a child */
263#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000264 funccall_T *caller; /* calling function or NULL */
265};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000266
267/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268 * Info used by a ":for" loop.
269 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000270typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271{
272 int fi_semicolon; /* TRUE if ending in '; var]' */
273 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 listwatch_T fi_lw; /* keep an eye on the item used. */
275 list_T *fi_list; /* list being used */
276} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000277
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000278/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000279 * Struct used by trans_function_name()
280 */
281typedef struct
282{
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000284 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000285 dictitem_T *fd_di; /* Dictionary item used */
286} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000287
Bram Moolenaara7043832005-01-21 11:56:39 +0000288
289/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000290 * Array to hold the value of v: variables.
291 * The value is in a dictitem, so that it can also be used in the v: scope.
292 * The reason to use this table anyway is for very quick access to the
293 * variables with the VV_ defines.
294 */
295#include "version.h"
296
297/* values for vv_flags: */
298#define VV_COMPAT 1 /* compatible, also used without "v:" */
299#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000300#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000302#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000303
304static struct vimvar
305{
306 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000307 dictitem_T vv_di; /* value and name for key */
308 char vv_filler[16]; /* space for LONGEST name below!!! */
309 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
310} vimvars[VV_LEN] =
311{
312 /*
313 * The order here must match the VV_ defines in vim.h!
314 * Initializing a union does not work, leave tv.vval empty to get zero's.
315 */
316 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("count1", VAR_NUMBER), VV_RO},
318 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
319 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
320 {VV_NAME("warningmsg", VAR_STRING), 0},
321 {VV_NAME("statusmsg", VAR_STRING), 0},
322 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
323 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
324 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
325 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
326 {VV_NAME("termresponse", VAR_STRING), VV_RO},
327 {VV_NAME("fname", VAR_STRING), VV_RO},
328 {VV_NAME("lang", VAR_STRING), VV_RO},
329 {VV_NAME("lc_time", VAR_STRING), VV_RO},
330 {VV_NAME("ctype", VAR_STRING), VV_RO},
331 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
332 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
333 {VV_NAME("fname_in", VAR_STRING), VV_RO},
334 {VV_NAME("fname_out", VAR_STRING), VV_RO},
335 {VV_NAME("fname_new", VAR_STRING), VV_RO},
336 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
337 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
338 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
339 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
340 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
341 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
342 {VV_NAME("progname", VAR_STRING), VV_RO},
343 {VV_NAME("servername", VAR_STRING), VV_RO},
344 {VV_NAME("dying", VAR_NUMBER), VV_RO},
345 {VV_NAME("exception", VAR_STRING), VV_RO},
346 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
347 {VV_NAME("register", VAR_STRING), VV_RO},
348 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
349 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000350 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
351 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000352 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000353 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
354 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000355 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
356 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
357 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
358 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
359 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000360 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000361 {VV_NAME("swapname", VAR_STRING), VV_RO},
362 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000363 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200364 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000365 {VV_NAME("mouse_win", VAR_NUMBER), 0},
366 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
367 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000368 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000369 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100370 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000371 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200372 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200373 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200374 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200375 {VV_NAME("option_new", VAR_STRING), VV_RO},
376 {VV_NAME("option_old", VAR_STRING), VV_RO},
377 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100378 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100379 {VV_NAME("false", VAR_SPECIAL), VV_RO},
380 {VV_NAME("true", VAR_SPECIAL), VV_RO},
381 {VV_NAME("null", VAR_SPECIAL), VV_RO},
382 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000383};
384
385/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000386#define vv_type vv_di.di_tv.v_type
387#define vv_nr vv_di.di_tv.vval.v_number
388#define vv_float vv_di.di_tv.vval.v_float
389#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000390#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200391#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000392#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000393
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200394static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000395#define vimvarht vimvardict.dv_hashtab
396
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100397static void prepare_vimvar(int idx, typval_T *save_tv);
398static void restore_vimvar(int idx, typval_T *save_tv);
399static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
400static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
401static char_u *skip_var_one(char_u *arg);
402static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
403static void list_glob_vars(int *first);
404static void list_buf_vars(int *first);
405static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000406#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100407static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000408#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100409static void list_vim_vars(int *first);
410static void list_script_vars(int *first);
411static void list_func_vars(int *first);
412static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
413static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
414static int check_changedtick(char_u *arg);
415static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
416static void clear_lval(lval_T *lp);
417static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
418static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
419static void list_fix_watch(list_T *l, listitem_T *item);
420static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
421static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
422static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
423static void item_lock(typval_T *tv, int deep, int lock);
424static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
427static int eval1(char_u **arg, typval_T *rettv, int evaluate);
428static int eval2(char_u **arg, typval_T *rettv, int evaluate);
429static int eval3(char_u **arg, typval_T *rettv, int evaluate);
430static int eval4(char_u **arg, typval_T *rettv, int evaluate);
431static int eval5(char_u **arg, typval_T *rettv, int evaluate);
432static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
433static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000434
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100435static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
436static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
437static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
438static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
439static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
440static long list_len(list_T *l);
441static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
442static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
443static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
444static long list_find_nr(list_T *l, long idx, int *errorp);
445static long list_idx_of_item(list_T *l, listitem_T *item);
446static int list_append_number(list_T *l, varnumber_T n);
447static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
448static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
449static list_T *list_copy(list_T *orig, int deep, int copyID);
450static char_u *list2string(typval_T *tv, int copyID);
451static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
452static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
453static int free_unref_items(int copyID);
454static dictitem_T *dictitem_copy(dictitem_T *org);
455static void dictitem_remove(dict_T *dict, dictitem_T *item);
456static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
457static long dict_len(dict_T *d);
458static char_u *dict2string(typval_T *tv, int copyID);
459static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
460static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
461static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
462static char_u *string_quote(char_u *str, int function);
463static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
464static int find_internal_func(char_u *name);
465static char_u *deref_func_name(char_u *name, int *lenp, int no_autoload);
466static 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 +0100467static void emsg_funcname(char *ermsg, char_u *name);
468static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000469
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000470#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100471static void f_abs(typval_T *argvars, typval_T *rettv);
472static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000473#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100474static void f_add(typval_T *argvars, typval_T *rettv);
475static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
476static void f_and(typval_T *argvars, typval_T *rettv);
477static void f_append(typval_T *argvars, typval_T *rettv);
478static void f_argc(typval_T *argvars, typval_T *rettv);
479static void f_argidx(typval_T *argvars, typval_T *rettv);
480static void f_arglistid(typval_T *argvars, typval_T *rettv);
481static void f_argv(typval_T *argvars, typval_T *rettv);
482static void f_assert_equal(typval_T *argvars, typval_T *rettv);
483static void f_assert_exception(typval_T *argvars, typval_T *rettv);
484static void f_assert_fails(typval_T *argvars, typval_T *rettv);
485static void f_assert_false(typval_T *argvars, typval_T *rettv);
486static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000487#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100488static void f_asin(typval_T *argvars, typval_T *rettv);
489static void f_atan(typval_T *argvars, typval_T *rettv);
490static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000491#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100492static void f_browse(typval_T *argvars, typval_T *rettv);
493static void f_browsedir(typval_T *argvars, typval_T *rettv);
494static void f_bufexists(typval_T *argvars, typval_T *rettv);
495static void f_buflisted(typval_T *argvars, typval_T *rettv);
496static void f_bufloaded(typval_T *argvars, typval_T *rettv);
497static void f_bufname(typval_T *argvars, typval_T *rettv);
498static void f_bufnr(typval_T *argvars, typval_T *rettv);
499static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
500static void f_byte2line(typval_T *argvars, typval_T *rettv);
501static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
502static void f_byteidx(typval_T *argvars, typval_T *rettv);
503static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
504static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000505#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100506static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000507#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100508#ifdef FEAT_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100510static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
511static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100512# ifdef FEAT_JOB
513static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
514# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100515static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100516static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
517static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100518static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100519static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100520static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
521static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100522static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100523static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100524#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100525static void f_changenr(typval_T *argvars, typval_T *rettv);
526static void f_char2nr(typval_T *argvars, typval_T *rettv);
527static void f_cindent(typval_T *argvars, typval_T *rettv);
528static void f_clearmatches(typval_T *argvars, typval_T *rettv);
529static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000530#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_complete(typval_T *argvars, typval_T *rettv);
532static void f_complete_add(typval_T *argvars, typval_T *rettv);
533static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000534#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100535static void f_confirm(typval_T *argvars, typval_T *rettv);
536static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000537#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100538static void f_cos(typval_T *argvars, typval_T *rettv);
539static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000540#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100541static void f_count(typval_T *argvars, typval_T *rettv);
542static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
543static void f_cursor(typval_T *argsvars, typval_T *rettv);
544static void f_deepcopy(typval_T *argvars, typval_T *rettv);
545static void f_delete(typval_T *argvars, typval_T *rettv);
546static void f_did_filetype(typval_T *argvars, typval_T *rettv);
547static void f_diff_filler(typval_T *argvars, typval_T *rettv);
548static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100549static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_empty(typval_T *argvars, typval_T *rettv);
551static void f_escape(typval_T *argvars, typval_T *rettv);
552static void f_eval(typval_T *argvars, typval_T *rettv);
553static void f_eventhandler(typval_T *argvars, typval_T *rettv);
554static void f_executable(typval_T *argvars, typval_T *rettv);
555static void f_exepath(typval_T *argvars, typval_T *rettv);
556static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200559#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100560static void f_expand(typval_T *argvars, typval_T *rettv);
561static void f_extend(typval_T *argvars, typval_T *rettv);
562static void f_feedkeys(typval_T *argvars, typval_T *rettv);
563static void f_filereadable(typval_T *argvars, typval_T *rettv);
564static void f_filewritable(typval_T *argvars, typval_T *rettv);
565static void f_filter(typval_T *argvars, typval_T *rettv);
566static void f_finddir(typval_T *argvars, typval_T *rettv);
567static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000568#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100569static void f_float2nr(typval_T *argvars, typval_T *rettv);
570static void f_floor(typval_T *argvars, typval_T *rettv);
571static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000572#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100573static void f_fnameescape(typval_T *argvars, typval_T *rettv);
574static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
575static void f_foldclosed(typval_T *argvars, typval_T *rettv);
576static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
577static void f_foldlevel(typval_T *argvars, typval_T *rettv);
578static void f_foldtext(typval_T *argvars, typval_T *rettv);
579static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
580static void f_foreground(typval_T *argvars, typval_T *rettv);
581static void f_function(typval_T *argvars, typval_T *rettv);
582static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
583static void f_get(typval_T *argvars, typval_T *rettv);
584static void f_getbufline(typval_T *argvars, typval_T *rettv);
585static void f_getbufvar(typval_T *argvars, typval_T *rettv);
586static void f_getchar(typval_T *argvars, typval_T *rettv);
587static void f_getcharmod(typval_T *argvars, typval_T *rettv);
588static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
589static void f_getcmdline(typval_T *argvars, typval_T *rettv);
590static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
591static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
592static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
593static void f_getcwd(typval_T *argvars, typval_T *rettv);
594static void f_getfontname(typval_T *argvars, typval_T *rettv);
595static void f_getfperm(typval_T *argvars, typval_T *rettv);
596static void f_getfsize(typval_T *argvars, typval_T *rettv);
597static void f_getftime(typval_T *argvars, typval_T *rettv);
598static void f_getftype(typval_T *argvars, typval_T *rettv);
599static void f_getline(typval_T *argvars, typval_T *rettv);
600static void f_getmatches(typval_T *argvars, typval_T *rettv);
601static void f_getpid(typval_T *argvars, typval_T *rettv);
602static void f_getcurpos(typval_T *argvars, typval_T *rettv);
603static void f_getpos(typval_T *argvars, typval_T *rettv);
604static void f_getqflist(typval_T *argvars, typval_T *rettv);
605static void f_getreg(typval_T *argvars, typval_T *rettv);
606static void f_getregtype(typval_T *argvars, typval_T *rettv);
607static void f_gettabvar(typval_T *argvars, typval_T *rettv);
608static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
609static void f_getwinposx(typval_T *argvars, typval_T *rettv);
610static void f_getwinposy(typval_T *argvars, typval_T *rettv);
611static void f_getwinvar(typval_T *argvars, typval_T *rettv);
612static void f_glob(typval_T *argvars, typval_T *rettv);
613static void f_globpath(typval_T *argvars, typval_T *rettv);
614static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
615static void f_has(typval_T *argvars, typval_T *rettv);
616static void f_has_key(typval_T *argvars, typval_T *rettv);
617static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
618static void f_hasmapto(typval_T *argvars, typval_T *rettv);
619static void f_histadd(typval_T *argvars, typval_T *rettv);
620static void f_histdel(typval_T *argvars, typval_T *rettv);
621static void f_histget(typval_T *argvars, typval_T *rettv);
622static void f_histnr(typval_T *argvars, typval_T *rettv);
623static void f_hlID(typval_T *argvars, typval_T *rettv);
624static void f_hlexists(typval_T *argvars, typval_T *rettv);
625static void f_hostname(typval_T *argvars, typval_T *rettv);
626static void f_iconv(typval_T *argvars, typval_T *rettv);
627static void f_indent(typval_T *argvars, typval_T *rettv);
628static void f_index(typval_T *argvars, typval_T *rettv);
629static void f_input(typval_T *argvars, typval_T *rettv);
630static void f_inputdialog(typval_T *argvars, typval_T *rettv);
631static void f_inputlist(typval_T *argvars, typval_T *rettv);
632static void f_inputrestore(typval_T *argvars, typval_T *rettv);
633static void f_inputsave(typval_T *argvars, typval_T *rettv);
634static void f_inputsecret(typval_T *argvars, typval_T *rettv);
635static void f_insert(typval_T *argvars, typval_T *rettv);
636static void f_invert(typval_T *argvars, typval_T *rettv);
637static void f_isdirectory(typval_T *argvars, typval_T *rettv);
638static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100639#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
640static void f_isnan(typval_T *argvars, typval_T *rettv);
641#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100642static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100643#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100644# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100645static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100646# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100647static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100648static void f_job_start(typval_T *argvars, typval_T *rettv);
649static void f_job_stop(typval_T *argvars, typval_T *rettv);
650static void f_job_status(typval_T *argvars, typval_T *rettv);
651#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100652static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100653static void f_js_decode(typval_T *argvars, typval_T *rettv);
654static void f_js_encode(typval_T *argvars, typval_T *rettv);
655static void f_json_decode(typval_T *argvars, typval_T *rettv);
656static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100657static void f_keys(typval_T *argvars, typval_T *rettv);
658static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
659static void f_len(typval_T *argvars, typval_T *rettv);
660static void f_libcall(typval_T *argvars, typval_T *rettv);
661static void f_libcallnr(typval_T *argvars, typval_T *rettv);
662static void f_line(typval_T *argvars, typval_T *rettv);
663static void f_line2byte(typval_T *argvars, typval_T *rettv);
664static void f_lispindent(typval_T *argvars, typval_T *rettv);
665static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000666#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100667static void f_log(typval_T *argvars, typval_T *rettv);
668static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000669#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200672#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100673static void f_map(typval_T *argvars, typval_T *rettv);
674static void f_maparg(typval_T *argvars, typval_T *rettv);
675static void f_mapcheck(typval_T *argvars, typval_T *rettv);
676static void f_match(typval_T *argvars, typval_T *rettv);
677static void f_matchadd(typval_T *argvars, typval_T *rettv);
678static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
679static void f_matcharg(typval_T *argvars, typval_T *rettv);
680static void f_matchdelete(typval_T *argvars, typval_T *rettv);
681static void f_matchend(typval_T *argvars, typval_T *rettv);
682static void f_matchlist(typval_T *argvars, typval_T *rettv);
683static void f_matchstr(typval_T *argvars, typval_T *rettv);
684static void f_max(typval_T *argvars, typval_T *rettv);
685static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000688#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100690#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100692#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
694static void f_nr2char(typval_T *argvars, typval_T *rettv);
695static void f_or(typval_T *argvars, typval_T *rettv);
696static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100697#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100699#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
704static void f_printf(typval_T *argvars, typval_T *rettv);
705static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100707static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200708#endif
709#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200711#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100712static void f_range(typval_T *argvars, typval_T *rettv);
713static void f_readfile(typval_T *argvars, typval_T *rettv);
714static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100715#ifdef FEAT_FLOAT
716static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
717#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_reltimestr(typval_T *argvars, typval_T *rettv);
719static void f_remote_expr(typval_T *argvars, typval_T *rettv);
720static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
721static void f_remote_peek(typval_T *argvars, typval_T *rettv);
722static void f_remote_read(typval_T *argvars, typval_T *rettv);
723static void f_remote_send(typval_T *argvars, typval_T *rettv);
724static void f_remove(typval_T *argvars, typval_T *rettv);
725static void f_rename(typval_T *argvars, typval_T *rettv);
726static void f_repeat(typval_T *argvars, typval_T *rettv);
727static void f_resolve(typval_T *argvars, typval_T *rettv);
728static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000731#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100732static void f_screenattr(typval_T *argvars, typval_T *rettv);
733static void f_screenchar(typval_T *argvars, typval_T *rettv);
734static void f_screencol(typval_T *argvars, typval_T *rettv);
735static void f_screenrow(typval_T *argvars, typval_T *rettv);
736static void f_search(typval_T *argvars, typval_T *rettv);
737static void f_searchdecl(typval_T *argvars, typval_T *rettv);
738static void f_searchpair(typval_T *argvars, typval_T *rettv);
739static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
740static void f_searchpos(typval_T *argvars, typval_T *rettv);
741static void f_server2client(typval_T *argvars, typval_T *rettv);
742static void f_serverlist(typval_T *argvars, typval_T *rettv);
743static void f_setbufvar(typval_T *argvars, typval_T *rettv);
744static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
745static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
746static void f_setline(typval_T *argvars, typval_T *rettv);
747static void f_setloclist(typval_T *argvars, typval_T *rettv);
748static void f_setmatches(typval_T *argvars, typval_T *rettv);
749static void f_setpos(typval_T *argvars, typval_T *rettv);
750static void f_setqflist(typval_T *argvars, typval_T *rettv);
751static void f_setreg(typval_T *argvars, typval_T *rettv);
752static void f_settabvar(typval_T *argvars, typval_T *rettv);
753static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
754static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100755#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100757#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_shellescape(typval_T *argvars, typval_T *rettv);
759static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
760static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000761#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100762static void f_sin(typval_T *argvars, typval_T *rettv);
763static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sort(typval_T *argvars, typval_T *rettv);
766static void f_soundfold(typval_T *argvars, typval_T *rettv);
767static void f_spellbadword(typval_T *argvars, typval_T *rettv);
768static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
769static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000770#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_sqrt(typval_T *argvars, typval_T *rettv);
772static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000773#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100774static void f_str2nr(typval_T *argvars, typval_T *rettv);
775static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000776#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000778#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100779static void f_stridx(typval_T *argvars, typval_T *rettv);
780static void f_string(typval_T *argvars, typval_T *rettv);
781static void f_strlen(typval_T *argvars, typval_T *rettv);
782static void f_strpart(typval_T *argvars, typval_T *rettv);
783static void f_strridx(typval_T *argvars, typval_T *rettv);
784static void f_strtrans(typval_T *argvars, typval_T *rettv);
785static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
786static void f_strwidth(typval_T *argvars, typval_T *rettv);
787static void f_submatch(typval_T *argvars, typval_T *rettv);
788static void f_substitute(typval_T *argvars, typval_T *rettv);
789static void f_synID(typval_T *argvars, typval_T *rettv);
790static void f_synIDattr(typval_T *argvars, typval_T *rettv);
791static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
792static void f_synstack(typval_T *argvars, typval_T *rettv);
793static void f_synconcealed(typval_T *argvars, typval_T *rettv);
794static void f_system(typval_T *argvars, typval_T *rettv);
795static void f_systemlist(typval_T *argvars, typval_T *rettv);
796static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
797static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
798static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
799static void f_taglist(typval_T *argvars, typval_T *rettv);
800static void f_tagfiles(typval_T *argvars, typval_T *rettv);
801static void f_tempname(typval_T *argvars, typval_T *rettv);
802static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200803#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_tan(typval_T *argvars, typval_T *rettv);
805static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200806#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_tolower(typval_T *argvars, typval_T *rettv);
808static void f_toupper(typval_T *argvars, typval_T *rettv);
809static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000812#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100813static void f_type(typval_T *argvars, typval_T *rettv);
814static void f_undofile(typval_T *argvars, typval_T *rettv);
815static void f_undotree(typval_T *argvars, typval_T *rettv);
816static void f_uniq(typval_T *argvars, typval_T *rettv);
817static void f_values(typval_T *argvars, typval_T *rettv);
818static void f_virtcol(typval_T *argvars, typval_T *rettv);
819static void f_visualmode(typval_T *argvars, typval_T *rettv);
820static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
821static void f_winbufnr(typval_T *argvars, typval_T *rettv);
822static void f_wincol(typval_T *argvars, typval_T *rettv);
823static void f_winheight(typval_T *argvars, typval_T *rettv);
824static void f_winline(typval_T *argvars, typval_T *rettv);
825static void f_winnr(typval_T *argvars, typval_T *rettv);
826static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
827static void f_winrestview(typval_T *argvars, typval_T *rettv);
828static void f_winsaveview(typval_T *argvars, typval_T *rettv);
829static void f_winwidth(typval_T *argvars, typval_T *rettv);
830static void f_writefile(typval_T *argvars, typval_T *rettv);
831static void f_wordcount(typval_T *argvars, typval_T *rettv);
832static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000833
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100834static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
835static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
836static int get_env_len(char_u **arg);
837static int get_id_len(char_u **arg);
838static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
839static 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 +0000840#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
841#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
842 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100843static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
844static int eval_isnamec(int c);
845static int eval_isnamec1(int c);
846static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
847static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100848static typval_T *alloc_string_tv(char_u *string);
849static void init_tv(typval_T *varp);
850static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100851#ifdef FEAT_FLOAT
852static float_T get_tv_float(typval_T *varp);
853#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854static linenr_T get_tv_lnum(typval_T *argvars);
855static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
856static char_u *get_tv_string(typval_T *varp);
857static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
858static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
859static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
860static hashtab_T *find_var_ht(char_u *name, char_u **varname);
861static funccall_T *get_funccal(void);
862static void vars_clear_ext(hashtab_T *ht, int free_val);
863static void delete_var(hashtab_T *ht, hashitem_T *hi);
864static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
865static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
866static void set_var(char_u *name, typval_T *varp, int copy);
867static int var_check_ro(int flags, char_u *name, int use_gettext);
868static int var_check_fixed(int flags, char_u *name, int use_gettext);
869static int var_check_func_name(char_u *name, int new_var);
870static int valid_varname(char_u *varname);
871static int tv_check_lock(int lock, char_u *name, int use_gettext);
872static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
873static char_u *find_option_end(char_u **arg, int *opt_flags);
874static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
875static int eval_fname_script(char_u *p);
876static int eval_fname_sid(char_u *p);
877static void list_func_head(ufunc_T *fp, int indent);
878static ufunc_T *find_func(char_u *name);
879static int function_exists(char_u *name);
880static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000881#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static void func_do_profile(ufunc_T *fp);
883static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
884static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000885static int
886# ifdef __BORLANDC__
887 _RTLENTRYF
888# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000890static int
891# ifdef __BORLANDC__
892 _RTLENTRYF
893# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100896static int script_autoload(char_u *name, int reload);
897static char_u *autoload_name(char_u *name);
898static void cat_func_name(char_u *buf, ufunc_T *fp);
899static void func_free(ufunc_T *fp);
900static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
901static int can_free_funccal(funccall_T *fc, int copyID) ;
902static void free_funccal(funccall_T *fc, int free_val);
903static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
904static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
905static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
906static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
907static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
908static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
909static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
910static int write_list(FILE *fd, list_T *list, int binary);
911static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000912
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913
914#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100915static int compare_func_name(const void *s1, const void *s2);
916static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200917#endif
918
Bram Moolenaar33570922005-01-25 22:26:29 +0000919/*
920 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000921 */
922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100923eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 int i;
926 struct vimvar *p;
927
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200928 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
929 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200930 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000931 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000932 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933
934 for (i = 0; i < VV_LEN; ++i)
935 {
936 p = &vimvars[i];
937 STRCPY(p->vv_di.di_key, p->vv_name);
938 if (p->vv_flags & VV_RO)
939 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
940 else if (p->vv_flags & VV_RO_SBX)
941 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
942 else
943 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000944
945 /* add to v: scope dict, unless the value is not always available */
946 if (p->vv_type != VAR_UNKNOWN)
947 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000948 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000949 /* add to compat scope dict */
950 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000951 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100952 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
953
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000954 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100955 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200956 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100957 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100958
959 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
960 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
961 set_vim_var_nr(VV_NONE, VVAL_NONE);
962 set_vim_var_nr(VV_NULL, VVAL_NULL);
963
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200964 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200965
966#ifdef EBCDIC
967 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100968 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200969 */
970 sortFunctions();
971#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000972}
973
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000974#if defined(EXITFREE) || defined(PROTO)
975 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100976eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000977{
978 int i;
979 struct vimvar *p;
980
981 for (i = 0; i < VV_LEN; ++i)
982 {
983 p = &vimvars[i];
984 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000985 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000986 vim_free(p->vv_str);
987 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000988 }
989 else if (p->vv_di.di_tv.v_type == VAR_LIST)
990 {
991 list_unref(p->vv_list);
992 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000993 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000994 }
995 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000996 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000997 hash_clear(&compat_hashtab);
998
Bram Moolenaard9fba312005-06-26 22:34:35 +0000999 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001000# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001001 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001002# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001003
1004 /* global variables */
1005 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001006
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001007 /* autoloaded script names */
1008 ga_clear_strings(&ga_loaded);
1009
Bram Moolenaarcca74132013-09-25 21:00:28 +02001010 /* Script-local variables. First clear all the variables and in a second
1011 * loop free the scriptvar_T, because a variable in one script might hold
1012 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001013 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001015 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001016 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001017 ga_clear(&ga_scripts);
1018
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001019 /* unreferenced lists and dicts */
1020 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001021
1022 /* functions */
1023 free_all_functions();
1024 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001025}
1026#endif
1027
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001028/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001029 * Return the name of the executed function.
1030 */
1031 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001032func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001034 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001035}
1036
1037/*
1038 * Return the address holding the next breakpoint line for a funccall cookie.
1039 */
1040 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001041func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042{
Bram Moolenaar33570922005-01-25 22:26:29 +00001043 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001044}
1045
1046/*
1047 * Return the address holding the debug tick for a funccall cookie.
1048 */
1049 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001050func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051{
Bram Moolenaar33570922005-01-25 22:26:29 +00001052 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001053}
1054
1055/*
1056 * Return the nesting level for a funccall cookie.
1057 */
1058 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001059func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060{
Bram Moolenaar33570922005-01-25 22:26:29 +00001061 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001062}
1063
1064/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001065funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001067/* pointer to list of previously used funccal, still around because some
1068 * item in it is still being used. */
1069funccall_T *previous_funccal = NULL;
1070
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071/*
1072 * Return TRUE when a function was ended by a ":return" command.
1073 */
1074 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001075current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076{
1077 return current_funccal->returned;
1078}
1079
1080
1081/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082 * Set an internal variable to a string value. Creates the variable if it does
1083 * not already exist.
1084 */
1085 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001086set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001088 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001089 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001090
1091 val = vim_strsave(value);
1092 if (val != NULL)
1093 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001094 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001096 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001097 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001098 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001099 }
1100 }
1101}
1102
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001104static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001105static char_u *redir_endp = NULL;
1106static char_u *redir_varname = NULL;
1107
1108/*
1109 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001110 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001111 * Returns OK if successfully completed the setup. FAIL otherwise.
1112 */
1113 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001114var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001115{
1116 int save_emsg;
1117 int err;
1118 typval_T tv;
1119
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001120 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001121 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001122 {
1123 EMSG(_(e_invarg));
1124 return FAIL;
1125 }
1126
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001127 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001128 redir_varname = vim_strsave(name);
1129 if (redir_varname == NULL)
1130 return FAIL;
1131
1132 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1133 if (redir_lval == NULL)
1134 {
1135 var_redir_stop();
1136 return FAIL;
1137 }
1138
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001139 /* The output is stored in growarray "redir_ga" until redirection ends. */
1140 ga_init2(&redir_ga, (int)sizeof(char), 500);
1141
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001142 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001143 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001144 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001145 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1146 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001147 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001148 if (redir_endp != NULL && *redir_endp != NUL)
1149 /* Trailing characters are present after the variable name */
1150 EMSG(_(e_trailing));
1151 else
1152 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001153 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001154 var_redir_stop();
1155 return FAIL;
1156 }
1157
1158 /* check if we can write to the variable: set it to or append an empty
1159 * string */
1160 save_emsg = did_emsg;
1161 did_emsg = FALSE;
1162 tv.v_type = VAR_STRING;
1163 tv.vval.v_string = (char_u *)"";
1164 if (append)
1165 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1166 else
1167 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001168 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001170 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001171 if (err)
1172 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001173 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 var_redir_stop();
1175 return FAIL;
1176 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001177
1178 return OK;
1179}
1180
1181/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001182 * Append "value[value_len]" to the variable set by var_redir_start().
1183 * The actual appending is postponed until redirection ends, because the value
1184 * appended may in fact be the string we write to, changing it may cause freed
1185 * memory to be used:
1186 * :redir => foo
1187 * :let foo
1188 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189 */
1190 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001191var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001193 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194
1195 if (redir_lval == NULL)
1196 return;
1197
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001198 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001202
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001203 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001204 {
1205 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001206 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001207 }
1208 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210}
1211
1212/*
1213 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001214 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215 */
1216 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001217var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001218{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001219 typval_T tv;
1220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001221 if (redir_lval != NULL)
1222 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001223 /* If there was no error: assign the text to the variable. */
1224 if (redir_endp != NULL)
1225 {
1226 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1227 tv.v_type = VAR_STRING;
1228 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001229 /* Call get_lval() again, if it's inside a Dict or List it may
1230 * have changed. */
1231 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001232 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001233 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1234 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1235 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001236 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001237
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001238 /* free the collected output */
1239 vim_free(redir_ga.ga_data);
1240 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001241
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001242 vim_free(redir_lval);
1243 redir_lval = NULL;
1244 }
1245 vim_free(redir_varname);
1246 redir_varname = NULL;
1247}
1248
Bram Moolenaar071d4272004-06-13 20:20:40 +00001249# if defined(FEAT_MBYTE) || defined(PROTO)
1250 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001251eval_charconvert(
1252 char_u *enc_from,
1253 char_u *enc_to,
1254 char_u *fname_from,
1255 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001256{
1257 int err = FALSE;
1258
1259 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1260 set_vim_var_string(VV_CC_TO, enc_to, -1);
1261 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1262 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1263 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1264 err = TRUE;
1265 set_vim_var_string(VV_CC_FROM, NULL, -1);
1266 set_vim_var_string(VV_CC_TO, NULL, -1);
1267 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1268 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1269
1270 if (err)
1271 return FAIL;
1272 return OK;
1273}
1274# endif
1275
1276# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001278eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279{
1280 int err = FALSE;
1281
1282 set_vim_var_string(VV_FNAME_IN, fname, -1);
1283 set_vim_var_string(VV_CMDARG, args, -1);
1284 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1285 err = TRUE;
1286 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1287 set_vim_var_string(VV_CMDARG, NULL, -1);
1288
1289 if (err)
1290 {
1291 mch_remove(fname);
1292 return FAIL;
1293 }
1294 return OK;
1295}
1296# endif
1297
1298# if defined(FEAT_DIFF) || defined(PROTO)
1299 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001300eval_diff(
1301 char_u *origfile,
1302 char_u *newfile,
1303 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001304{
1305 int err = FALSE;
1306
1307 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1308 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1309 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1310 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1311 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1312 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1313 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1314}
1315
1316 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001317eval_patch(
1318 char_u *origfile,
1319 char_u *difffile,
1320 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001321{
1322 int err;
1323
1324 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1325 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1326 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1327 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1328 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1329 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1330 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1331}
1332# endif
1333
1334/*
1335 * Top level evaluation function, returning a boolean.
1336 * Sets "error" to TRUE if there was an error.
1337 * Return TRUE or FALSE.
1338 */
1339 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001340eval_to_bool(
1341 char_u *arg,
1342 int *error,
1343 char_u **nextcmd,
1344 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345{
Bram Moolenaar33570922005-01-25 22:26:29 +00001346 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 int retval = FALSE;
1348
1349 if (skip)
1350 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001351 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001353 else
1354 {
1355 *error = FALSE;
1356 if (!skip)
1357 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001358 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001359 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001360 }
1361 }
1362 if (skip)
1363 --emsg_skip;
1364
1365 return retval;
1366}
1367
1368/*
1369 * Top level evaluation function, returning a string. If "skip" is TRUE,
1370 * only parsing to "nextcmd" is done, without reporting errors. Return
1371 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1372 */
1373 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001374eval_to_string_skip(
1375 char_u *arg,
1376 char_u **nextcmd,
1377 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378{
Bram Moolenaar33570922005-01-25 22:26:29 +00001379 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 char_u *retval;
1381
1382 if (skip)
1383 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001384 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 retval = NULL;
1386 else
1387 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001388 retval = vim_strsave(get_tv_string(&tv));
1389 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 }
1391 if (skip)
1392 --emsg_skip;
1393
1394 return retval;
1395}
1396
1397/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398 * Skip over an expression at "*pp".
1399 * Return FAIL for an error, OK otherwise.
1400 */
1401 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001402skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403{
Bram Moolenaar33570922005-01-25 22:26:29 +00001404 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001405
1406 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001407 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001408}
1409
1410/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001411 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001412 * When "convert" is TRUE convert a List into a sequence of lines and convert
1413 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001414 * Return pointer to allocated memory, or NULL for failure.
1415 */
1416 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001417eval_to_string(
1418 char_u *arg,
1419 char_u **nextcmd,
1420 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421{
Bram Moolenaar33570922005-01-25 22:26:29 +00001422 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001424 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001425#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001426 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001429 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001430 retval = NULL;
1431 else
1432 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001433 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001434 {
1435 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001436 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001437 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001438 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001439 if (tv.vval.v_list->lv_len > 0)
1440 ga_append(&ga, NL);
1441 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001442 ga_append(&ga, NUL);
1443 retval = (char_u *)ga.ga_data;
1444 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001445#ifdef FEAT_FLOAT
1446 else if (convert && tv.v_type == VAR_FLOAT)
1447 {
1448 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1449 retval = vim_strsave(numbuf);
1450 }
1451#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001452 else
1453 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001454 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001455 }
1456
1457 return retval;
1458}
1459
1460/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001461 * Call eval_to_string() without using current local variables and using
1462 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001463 */
1464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001465eval_to_string_safe(
1466 char_u *arg,
1467 char_u **nextcmd,
1468 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001469{
1470 char_u *retval;
1471 void *save_funccalp;
1472
1473 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001474 if (use_sandbox)
1475 ++sandbox;
1476 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001477 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001478 if (use_sandbox)
1479 --sandbox;
1480 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001481 restore_funccal(save_funccalp);
1482 return retval;
1483}
1484
Bram Moolenaar071d4272004-06-13 20:20:40 +00001485/*
1486 * Top level evaluation function, returning a number.
1487 * Evaluates "expr" silently.
1488 * Returns -1 for an error.
1489 */
1490 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001491eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492{
Bram Moolenaar33570922005-01-25 22:26:29 +00001493 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001495 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001496
1497 ++emsg_off;
1498
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 retval = -1;
1501 else
1502 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001503 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 }
1506 --emsg_off;
1507
1508 return retval;
1509}
1510
Bram Moolenaara40058a2005-07-11 22:42:07 +00001511/*
1512 * Prepare v: variable "idx" to be used.
1513 * Save the current typeval in "save_tv".
1514 * When not used yet add the variable to the v: hashtable.
1515 */
1516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001517prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001518{
1519 *save_tv = vimvars[idx].vv_tv;
1520 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1521 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1522}
1523
1524/*
1525 * Restore v: variable "idx" to typeval "save_tv".
1526 * When no longer defined, remove the variable from the v: hashtable.
1527 */
1528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001529restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001530{
1531 hashitem_T *hi;
1532
Bram Moolenaara40058a2005-07-11 22:42:07 +00001533 vimvars[idx].vv_tv = *save_tv;
1534 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1535 {
1536 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1537 if (HASHITEM_EMPTY(hi))
1538 EMSG2(_(e_intern2), "restore_vimvar()");
1539 else
1540 hash_remove(&vimvarht, hi);
1541 }
1542}
1543
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001544#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001545/*
1546 * Evaluate an expression to a list with suggestions.
1547 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001548 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001549 */
1550 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001551eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001552{
1553 typval_T save_val;
1554 typval_T rettv;
1555 list_T *list = NULL;
1556 char_u *p = skipwhite(expr);
1557
1558 /* Set "v:val" to the bad word. */
1559 prepare_vimvar(VV_VAL, &save_val);
1560 vimvars[VV_VAL].vv_type = VAR_STRING;
1561 vimvars[VV_VAL].vv_str = badword;
1562 if (p_verbose == 0)
1563 ++emsg_off;
1564
1565 if (eval1(&p, &rettv, TRUE) == OK)
1566 {
1567 if (rettv.v_type != VAR_LIST)
1568 clear_tv(&rettv);
1569 else
1570 list = rettv.vval.v_list;
1571 }
1572
1573 if (p_verbose == 0)
1574 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001575 restore_vimvar(VV_VAL, &save_val);
1576
1577 return list;
1578}
1579
1580/*
1581 * "list" is supposed to contain two items: a word and a number. Return the
1582 * word in "pp" and the number as the return value.
1583 * Return -1 if anything isn't right.
1584 * Used to get the good word and score from the eval_spell_expr() result.
1585 */
1586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001587get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001588{
1589 listitem_T *li;
1590
1591 li = list->lv_first;
1592 if (li == NULL)
1593 return -1;
1594 *pp = get_tv_string(&li->li_tv);
1595
1596 li = li->li_next;
1597 if (li == NULL)
1598 return -1;
1599 return get_tv_number(&li->li_tv);
1600}
1601#endif
1602
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001603/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001604 * Top level evaluation function.
1605 * Returns an allocated typval_T with the result.
1606 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001607 */
1608 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001609eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610{
1611 typval_T *tv;
1612
1613 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001614 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001615 {
1616 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001617 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001618 }
1619
1620 return tv;
1621}
1622
1623
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001625 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001626 * Uses argv[argc] for the function arguments. Only Number and String
1627 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001628 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001630 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001631call_vim_function(
1632 char_u *func,
1633 int argc,
1634 char_u **argv,
1635 int safe, /* use the sandbox */
1636 int str_arg_only, /* all arguments are strings */
1637 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638{
Bram Moolenaar33570922005-01-25 22:26:29 +00001639 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001640 long n;
1641 int len;
1642 int i;
1643 int doesrange;
1644 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001645 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001647 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001649 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001650
1651 for (i = 0; i < argc; i++)
1652 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001653 /* Pass a NULL or empty argument as an empty string */
1654 if (argv[i] == NULL || *argv[i] == NUL)
1655 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001656 argvars[i].v_type = VAR_STRING;
1657 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001658 continue;
1659 }
1660
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001661 if (str_arg_only)
1662 len = 0;
1663 else
1664 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001665 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001666 if (len != 0 && len == (int)STRLEN(argv[i]))
1667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001668 argvars[i].v_type = VAR_NUMBER;
1669 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 else
1672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001673 argvars[i].v_type = VAR_STRING;
1674 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 }
1677
1678 if (safe)
1679 {
1680 save_funccalp = save_funccal();
1681 ++sandbox;
1682 }
1683
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001684 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1685 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001687 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001688 if (safe)
1689 {
1690 --sandbox;
1691 restore_funccal(save_funccalp);
1692 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001693 vim_free(argvars);
1694
1695 if (ret == FAIL)
1696 clear_tv(rettv);
1697
1698 return ret;
1699}
1700
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001701/*
1702 * Call vimL function "func" and return the result as a number.
1703 * Returns -1 when calling the function fails.
1704 * Uses argv[argc] for the function arguments.
1705 */
1706 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001707call_func_retnr(
1708 char_u *func,
1709 int argc,
1710 char_u **argv,
1711 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001712{
1713 typval_T rettv;
1714 long retval;
1715
1716 /* All arguments are passed as strings, no conversion to number. */
1717 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1718 return -1;
1719
1720 retval = get_tv_number_chk(&rettv, NULL);
1721 clear_tv(&rettv);
1722 return retval;
1723}
1724
1725#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1726 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1727
Bram Moolenaar4f688582007-07-24 12:34:30 +00001728# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001729/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001730 * Call vimL function "func" and return the result as a string.
1731 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001732 * Uses argv[argc] for the function arguments.
1733 */
1734 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001735call_func_retstr(
1736 char_u *func,
1737 int argc,
1738 char_u **argv,
1739 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001740{
1741 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001742 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001743
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001744 /* All arguments are passed as strings, no conversion to number. */
1745 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001746 return NULL;
1747
1748 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001749 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001750 return retval;
1751}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001752# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001754/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001755 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001757 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758 */
1759 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001760call_func_retlist(
1761 char_u *func,
1762 int argc,
1763 char_u **argv,
1764 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765{
1766 typval_T rettv;
1767
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001768 /* All arguments are passed as strings, no conversion to number. */
1769 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770 return NULL;
1771
1772 if (rettv.v_type != VAR_LIST)
1773 {
1774 clear_tv(&rettv);
1775 return NULL;
1776 }
1777
1778 return rettv.vval.v_list;
1779}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001780#endif
1781
1782/*
1783 * Save the current function call pointer, and set it to NULL.
1784 * Used when executing autocommands and for ":source".
1785 */
1786 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001787save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001789 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790
Bram Moolenaar071d4272004-06-13 20:20:40 +00001791 current_funccal = NULL;
1792 return (void *)fc;
1793}
1794
1795 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001796restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001797{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001798 funccall_T *fc = (funccall_T *)vfc;
1799
1800 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001801}
1802
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803#if defined(FEAT_PROFILE) || defined(PROTO)
1804/*
1805 * Prepare profiling for entering a child or something else that is not
1806 * counted for the script/function itself.
1807 * Should always be called in pair with prof_child_exit().
1808 */
1809 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001810prof_child_enter(
1811 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001812{
1813 funccall_T *fc = current_funccal;
1814
1815 if (fc != NULL && fc->func->uf_profiling)
1816 profile_start(&fc->prof_child);
1817 script_prof_save(tm);
1818}
1819
1820/*
1821 * Take care of time spent in a child.
1822 * Should always be called after prof_child_enter().
1823 */
1824 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001825prof_child_exit(
1826 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001827{
1828 funccall_T *fc = current_funccal;
1829
1830 if (fc != NULL && fc->func->uf_profiling)
1831 {
1832 profile_end(&fc->prof_child);
1833 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1834 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1835 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1836 }
1837 script_prof_restore(tm);
1838}
1839#endif
1840
1841
Bram Moolenaar071d4272004-06-13 20:20:40 +00001842#ifdef FEAT_FOLDING
1843/*
1844 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1845 * it in "*cp". Doesn't give error messages.
1846 */
1847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001848eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849{
Bram Moolenaar33570922005-01-25 22:26:29 +00001850 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001851 int retval;
1852 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001853 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1854 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855
1856 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001857 if (use_sandbox)
1858 ++sandbox;
1859 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001862 retval = 0;
1863 else
1864 {
1865 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 if (tv.v_type == VAR_NUMBER)
1867 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001868 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001869 retval = 0;
1870 else
1871 {
1872 /* If the result is a string, check if there is a non-digit before
1873 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 if (!VIM_ISDIGIT(*s) && *s != '-')
1876 *cp = *s++;
1877 retval = atol((char *)s);
1878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 }
1881 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001882 if (use_sandbox)
1883 --sandbox;
1884 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885
1886 return retval;
1887}
1888#endif
1889
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001891 * ":let" list all variable values
1892 * ":let var1 var2" list variable values
1893 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001894 * ":let var += expr" assignment command.
1895 * ":let var -= expr" assignment command.
1896 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001897 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001898 */
1899 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001900ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001901{
1902 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001903 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001904 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001906 int var_count = 0;
1907 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001908 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001909 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001910 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001911
Bram Moolenaardb552d602006-03-23 22:59:57 +00001912 argend = skip_var_list(arg, &var_count, &semicolon);
1913 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001914 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001915 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1916 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001917 expr = skipwhite(argend);
1918 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1919 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001920 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001921 /*
1922 * ":let" without "=": list variables
1923 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001924 if (*arg == '[')
1925 EMSG(_(e_invarg));
1926 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001928 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001930 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001931 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001932 list_glob_vars(&first);
1933 list_buf_vars(&first);
1934 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001935#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001937#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001938 list_script_vars(&first);
1939 list_func_vars(&first);
1940 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001941 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 eap->nextcmd = check_nextcmd(arg);
1943 }
1944 else
1945 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001946 op[0] = '=';
1947 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001948 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001949 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001950 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1951 op[0] = *expr; /* +=, -= or .= */
1952 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001953 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001954 else
1955 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001956
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 if (eap->skip)
1958 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001959 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (eap->skip)
1961 {
1962 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001963 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 --emsg_skip;
1965 }
1966 else if (i != FAIL)
1967 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001968 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001969 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001970 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001971 }
1972 }
1973}
1974
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001975/*
1976 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1977 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001978 * When "nextchars" is not NULL it points to a string with characters that
1979 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1980 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001981 * Returns OK or FAIL;
1982 */
1983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001984ex_let_vars(
1985 char_u *arg_start,
1986 typval_T *tv,
1987 int copy, /* copy values from "tv", don't move */
1988 int semicolon, /* from skip_var_list() */
1989 int var_count, /* from skip_var_list() */
1990 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001991{
1992 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001993 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001994 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001995 listitem_T *item;
1996 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001997
1998 if (*arg != '[')
1999 {
2000 /*
2001 * ":let var = expr" or ":for var in list"
2002 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002003 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002004 return FAIL;
2005 return OK;
2006 }
2007
2008 /*
2009 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2010 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002011 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002012 {
2013 EMSG(_(e_listreq));
2014 return FAIL;
2015 }
2016
2017 i = list_len(l);
2018 if (semicolon == 0 && var_count < i)
2019 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002020 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 return FAIL;
2022 }
2023 if (var_count - semicolon > i)
2024 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002025 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 return FAIL;
2027 }
2028
2029 item = l->lv_first;
2030 while (*arg != ']')
2031 {
2032 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002033 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002034 item = item->li_next;
2035 if (arg == NULL)
2036 return FAIL;
2037
2038 arg = skipwhite(arg);
2039 if (*arg == ';')
2040 {
2041 /* Put the rest of the list (may be empty) in the var after ';'.
2042 * Create a new list for this. */
2043 l = list_alloc();
2044 if (l == NULL)
2045 return FAIL;
2046 while (item != NULL)
2047 {
2048 list_append_tv(l, &item->li_tv);
2049 item = item->li_next;
2050 }
2051
2052 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002053 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 ltv.vval.v_list = l;
2055 l->lv_refcount = 1;
2056
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002057 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2058 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 clear_tv(&ltv);
2060 if (arg == NULL)
2061 return FAIL;
2062 break;
2063 }
2064 else if (*arg != ',' && *arg != ']')
2065 {
2066 EMSG2(_(e_intern2), "ex_let_vars()");
2067 return FAIL;
2068 }
2069 }
2070
2071 return OK;
2072}
2073
2074/*
2075 * Skip over assignable variable "var" or list of variables "[var, var]".
2076 * Used for ":let varvar = expr" and ":for varvar in expr".
2077 * For "[var, var]" increment "*var_count" for each variable.
2078 * for "[var, var; var]" set "semicolon".
2079 * Return NULL for an error.
2080 */
2081 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002082skip_var_list(
2083 char_u *arg,
2084 int *var_count,
2085 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002086{
2087 char_u *p, *s;
2088
2089 if (*arg == '[')
2090 {
2091 /* "[var, var]": find the matching ']'. */
2092 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002093 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002094 {
2095 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2096 s = skip_var_one(p);
2097 if (s == p)
2098 {
2099 EMSG2(_(e_invarg2), p);
2100 return NULL;
2101 }
2102 ++*var_count;
2103
2104 p = skipwhite(s);
2105 if (*p == ']')
2106 break;
2107 else if (*p == ';')
2108 {
2109 if (*semicolon == 1)
2110 {
2111 EMSG(_("Double ; in list of variables"));
2112 return NULL;
2113 }
2114 *semicolon = 1;
2115 }
2116 else if (*p != ',')
2117 {
2118 EMSG2(_(e_invarg2), p);
2119 return NULL;
2120 }
2121 }
2122 return p + 1;
2123 }
2124 else
2125 return skip_var_one(arg);
2126}
2127
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002128/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002129 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002130 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002131 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002133skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002135 if (*arg == '@' && arg[1] != NUL)
2136 return arg + 2;
2137 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2138 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139}
2140
Bram Moolenaara7043832005-01-21 11:56:39 +00002141/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002142 * List variables for hashtab "ht" with prefix "prefix".
2143 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002144 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002145 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002146list_hashtable_vars(
2147 hashtab_T *ht,
2148 char_u *prefix,
2149 int empty,
2150 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002151{
Bram Moolenaar33570922005-01-25 22:26:29 +00002152 hashitem_T *hi;
2153 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002154 int todo;
2155
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002156 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002157 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2158 {
2159 if (!HASHITEM_EMPTY(hi))
2160 {
2161 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002162 di = HI2DI(hi);
2163 if (empty || di->di_tv.v_type != VAR_STRING
2164 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002165 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002166 }
2167 }
2168}
2169
2170/*
2171 * List global variables.
2172 */
2173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002174list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002175{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002176 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002177}
2178
2179/*
2180 * List buffer variables.
2181 */
2182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002183list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002184{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002185 char_u numbuf[NUMBUFLEN];
2186
Bram Moolenaar429fa852013-04-15 12:27:36 +02002187 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002188 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002189
2190 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002191 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2192 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002193}
2194
2195/*
2196 * List window variables.
2197 */
2198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002199list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002200{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002201 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002202 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002203}
2204
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002205#ifdef FEAT_WINDOWS
2206/*
2207 * List tab page variables.
2208 */
2209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002210list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002211{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002212 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002213 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002214}
2215#endif
2216
Bram Moolenaara7043832005-01-21 11:56:39 +00002217/*
2218 * List Vim variables.
2219 */
2220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002221list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002223 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002224}
2225
2226/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002227 * List script-local variables, if there is a script.
2228 */
2229 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002230list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002231{
2232 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002233 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2234 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002235}
2236
2237/*
2238 * List function variables, if there is a function.
2239 */
2240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002241list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002242{
2243 if (current_funccal != NULL)
2244 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002245 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002246}
2247
2248/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002249 * List variables in "arg".
2250 */
2251 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002252list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002253{
2254 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002257 char_u *name_start;
2258 char_u *arg_subsc;
2259 char_u *tofree;
2260 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261
2262 while (!ends_excmd(*arg) && !got_int)
2263 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002264 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002265 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002266 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002267 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2268 {
2269 emsg_severe = TRUE;
2270 EMSG(_(e_trailing));
2271 break;
2272 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* get_name_len() takes care of expanding curly braces */
2277 name_start = name = arg;
2278 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2279 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 /* This is mainly to keep test 49 working: when expanding
2282 * curly braces fails overrule the exception error message. */
2283 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002284 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002285 emsg_severe = TRUE;
2286 EMSG2(_(e_invarg2), arg);
2287 break;
2288 }
2289 error = TRUE;
2290 }
2291 else
2292 {
2293 if (tofree != NULL)
2294 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002295 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002297 else
2298 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002299 /* handle d.key, l[idx], f(expr) */
2300 arg_subsc = arg;
2301 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002303 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002307 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002308 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002309 case 'g': list_glob_vars(first); break;
2310 case 'b': list_buf_vars(first); break;
2311 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002314#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002315 case 'v': list_vim_vars(first); break;
2316 case 's': list_script_vars(first); break;
2317 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 default:
2319 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002321 }
2322 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 {
2324 char_u numbuf[NUMBUFLEN];
2325 char_u *tf;
2326 int c;
2327 char_u *s;
2328
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002329 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002330 c = *arg;
2331 *arg = NUL;
2332 list_one_var_a((char_u *)"",
2333 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002334 tv.v_type,
2335 s == NULL ? (char_u *)"" : s,
2336 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002337 *arg = c;
2338 vim_free(tf);
2339 }
2340 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002341 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002342 }
2343 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002344
2345 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002346 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002347
2348 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002349 }
2350
2351 return arg;
2352}
2353
2354/*
2355 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2356 * Returns a pointer to the char just after the var name.
2357 * Returns NULL if there is an error.
2358 */
2359 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002360ex_let_one(
2361 char_u *arg, /* points to variable name */
2362 typval_T *tv, /* value to assign to variable */
2363 int copy, /* copy value from "tv" */
2364 char_u *endchars, /* valid chars after variable name or NULL */
2365 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002366{
2367 int c1;
2368 char_u *name;
2369 char_u *p;
2370 char_u *arg_end = NULL;
2371 int len;
2372 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002373 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002374
2375 /*
2376 * ":let $VAR = expr": Set environment variable.
2377 */
2378 if (*arg == '$')
2379 {
2380 /* Find the end of the name. */
2381 ++arg;
2382 name = arg;
2383 len = get_env_len(&arg);
2384 if (len == 0)
2385 EMSG2(_(e_invarg2), name - 1);
2386 else
2387 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002388 if (op != NULL && (*op == '+' || *op == '-'))
2389 EMSG2(_(e_letwrong), op);
2390 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002391 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002393 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002394 {
2395 c1 = name[len];
2396 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002397 p = get_tv_string_chk(tv);
2398 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002399 {
2400 int mustfree = FALSE;
2401 char_u *s = vim_getenv(name, &mustfree);
2402
2403 if (s != NULL)
2404 {
2405 p = tofree = concat_str(s, p);
2406 if (mustfree)
2407 vim_free(s);
2408 }
2409 }
2410 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002412 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002413 if (STRICMP(name, "HOME") == 0)
2414 init_homedir();
2415 else if (didset_vim && STRICMP(name, "VIM") == 0)
2416 didset_vim = FALSE;
2417 else if (didset_vimruntime
2418 && STRICMP(name, "VIMRUNTIME") == 0)
2419 didset_vimruntime = FALSE;
2420 arg_end = arg;
2421 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002423 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002424 }
2425 }
2426 }
2427
2428 /*
2429 * ":let &option = expr": Set option value.
2430 * ":let &l:option = expr": Set local option value.
2431 * ":let &g:option = expr": Set global option value.
2432 */
2433 else if (*arg == '&')
2434 {
2435 /* Find the end of the name. */
2436 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002437 if (p == NULL || (endchars != NULL
2438 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002439 EMSG(_(e_letunexp));
2440 else
2441 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002442 long n;
2443 int opt_type;
2444 long numval;
2445 char_u *stringval = NULL;
2446 char_u *s;
2447
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002448 c1 = *p;
2449 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002450
2451 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002452 s = get_tv_string_chk(tv); /* != NULL if number or string */
2453 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002454 {
2455 opt_type = get_option_value(arg, &numval,
2456 &stringval, opt_flags);
2457 if ((opt_type == 1 && *op == '.')
2458 || (opt_type == 0 && *op != '.'))
2459 EMSG2(_(e_letwrong), op);
2460 else
2461 {
2462 if (opt_type == 1) /* number */
2463 {
2464 if (*op == '+')
2465 n = numval + n;
2466 else
2467 n = numval - n;
2468 }
2469 else if (opt_type == 0 && stringval != NULL) /* string */
2470 {
2471 s = concat_str(stringval, s);
2472 vim_free(stringval);
2473 stringval = s;
2474 }
2475 }
2476 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002477 if (s != NULL)
2478 {
2479 set_option_value(arg, n, s, opt_flags);
2480 arg_end = p;
2481 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002483 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002484 }
2485 }
2486
2487 /*
2488 * ":let @r = expr": Set register contents.
2489 */
2490 else if (*arg == '@')
2491 {
2492 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002493 if (op != NULL && (*op == '+' || *op == '-'))
2494 EMSG2(_(e_letwrong), op);
2495 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002496 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002497 EMSG(_(e_letunexp));
2498 else
2499 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002500 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002501 char_u *s;
2502
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002503 p = get_tv_string_chk(tv);
2504 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002506 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002507 if (s != NULL)
2508 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002509 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 vim_free(s);
2511 }
2512 }
2513 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002516 arg_end = arg + 1;
2517 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002518 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002519 }
2520 }
2521
2522 /*
2523 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002524 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002526 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002528 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002529
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002530 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2534 EMSG(_(e_letunexp));
2535 else
2536 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002537 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 arg_end = p;
2539 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002541 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002542 }
2543
2544 else
2545 EMSG2(_(e_invarg2), arg);
2546
2547 return arg_end;
2548}
2549
2550/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002551 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2552 */
2553 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002554check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002555{
2556 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2557 {
2558 EMSG2(_(e_readonlyvar), arg);
2559 return TRUE;
2560 }
2561 return FALSE;
2562}
2563
2564/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002565 * Get an lval: variable, Dict item or List item that can be assigned a value
2566 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2567 * "name.key", "name.key[expr]" etc.
2568 * Indexing only works if "name" is an existing List or Dictionary.
2569 * "name" points to the start of the name.
2570 * If "rettv" is not NULL it points to the value to be assigned.
2571 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2572 * wrong; must end in space or cmd separator.
2573 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002574 * flags:
2575 * GLV_QUIET: do not give error messages
2576 * GLV_NO_AUTOLOAD: do not use script autoloading
2577 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002579 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002580 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002581 */
2582 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002583get_lval(
2584 char_u *name,
2585 typval_T *rettv,
2586 lval_T *lp,
2587 int unlet,
2588 int skip,
2589 int flags, /* GLV_ values */
2590 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002592 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 char_u *expr_start, *expr_end;
2594 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002595 dictitem_T *v;
2596 typval_T var1;
2597 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002599 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002601 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002602 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002603 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002604
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002606 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002607
2608 if (skip)
2609 {
2610 /* When skipping just find the end of the name. */
2611 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002612 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002613 }
2614
2615 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002616 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002617 if (expr_start != NULL)
2618 {
2619 /* Don't expand the name when we already know there is an error. */
2620 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2621 && *p != '[' && *p != '.')
2622 {
2623 EMSG(_(e_trailing));
2624 return NULL;
2625 }
2626
2627 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2628 if (lp->ll_exp_name == NULL)
2629 {
2630 /* Report an invalid expression in braces, unless the
2631 * expression evaluation has been cancelled due to an
2632 * aborting error, an interrupt, or an exception. */
2633 if (!aborting() && !quiet)
2634 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002635 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002636 EMSG2(_(e_invarg2), name);
2637 return NULL;
2638 }
2639 }
2640 lp->ll_name = lp->ll_exp_name;
2641 }
2642 else
2643 lp->ll_name = name;
2644
2645 /* Without [idx] or .key we are done. */
2646 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2647 return p;
2648
2649 cc = *p;
2650 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002651 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002652 if (v == NULL && !quiet)
2653 EMSG2(_(e_undefvar), lp->ll_name);
2654 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002655 if (v == NULL)
2656 return NULL;
2657
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002658 /*
2659 * Loop until no more [idx] or .key is following.
2660 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002661 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002663 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002664 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2665 && !(lp->ll_tv->v_type == VAR_DICT
2666 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002667 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002668 if (!quiet)
2669 EMSG(_("E689: Can only index a List or Dictionary"));
2670 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002673 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002674 if (!quiet)
2675 EMSG(_("E708: [:] must come last"));
2676 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002677 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002678
Bram Moolenaar8c711452005-01-14 21:53:12 +00002679 len = -1;
2680 if (*p == '.')
2681 {
2682 key = p + 1;
2683 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2684 ;
2685 if (len == 0)
2686 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002687 if (!quiet)
2688 EMSG(_(e_emptykey));
2689 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 }
2691 p = key + len;
2692 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002693 else
2694 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002696 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002697 if (*p == ':')
2698 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002699 else
2700 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002701 empty1 = FALSE;
2702 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002703 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002704 if (get_tv_string_chk(&var1) == NULL)
2705 {
2706 /* not a number or string */
2707 clear_tv(&var1);
2708 return NULL;
2709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002710 }
2711
2712 /* Optionally get the second index [ :expr]. */
2713 if (*p == ':')
2714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002716 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002717 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002718 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002719 if (!empty1)
2720 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002722 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002723 if (rettv != NULL && (rettv->v_type != VAR_LIST
2724 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 if (!quiet)
2727 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002728 if (!empty1)
2729 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002730 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002731 }
2732 p = skipwhite(p + 1);
2733 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002734 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 else
2736 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2739 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 if (!empty1)
2741 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002744 if (get_tv_string_chk(&var2) == NULL)
2745 {
2746 /* not a number or string */
2747 if (!empty1)
2748 clear_tv(&var1);
2749 clear_tv(&var2);
2750 return NULL;
2751 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002752 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002753 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002755 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002756 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002757
Bram Moolenaar8c711452005-01-14 21:53:12 +00002758 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002759 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002760 if (!quiet)
2761 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 if (!empty1)
2763 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002766 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 }
2768
2769 /* Skip to past ']'. */
2770 ++p;
2771 }
2772
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002773 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 {
2775 if (len == -1)
2776 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002777 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002778 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 if (*key == NUL)
2780 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002781 if (!quiet)
2782 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002784 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002785 }
2786 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002787 lp->ll_list = NULL;
2788 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002789 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 /* When assigning to a scope dictionary check that a function and
2792 * variable name is valid (only variable name unless it is l: or
2793 * g: dictionary). Disallow overwriting a builtin function. */
2794 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 int prevval;
2797 int wrong;
2798
2799 if (len != -1)
2800 {
2801 prevval = key[len];
2802 key[len] = NUL;
2803 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002804 else
2805 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002806 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2807 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002809 || !valid_varname(key);
2810 if (len != -1)
2811 key[len] = prevval;
2812 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 return NULL;
2814 }
2815
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002816 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002817 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 /* Can't add "v:" variable. */
2819 if (lp->ll_dict == &vimvardict)
2820 {
2821 EMSG2(_(e_illvar), name);
2822 return NULL;
2823 }
2824
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002825 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002827 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002828 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002829 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002830 if (len == -1)
2831 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 }
2834 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 if (len == -1)
2839 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 p = NULL;
2842 break;
2843 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002845 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002846 return NULL;
2847
Bram Moolenaar8c711452005-01-14 21:53:12 +00002848 if (len == -1)
2849 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002850 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002851 }
2852 else
2853 {
2854 /*
2855 * Get the number and item for the only or first index of the List.
2856 */
2857 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002858 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002859 else
2860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002861 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002862 clear_tv(&var1);
2863 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002864 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002865 lp->ll_list = lp->ll_tv->vval.v_list;
2866 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2867 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002868 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002869 if (lp->ll_n1 < 0)
2870 {
2871 lp->ll_n1 = 0;
2872 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2873 }
2874 }
2875 if (lp->ll_li == NULL)
2876 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002877 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002878 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002879 if (!quiet)
2880 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002881 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002882 }
2883
2884 /*
2885 * May need to find the item or absolute index for the second
2886 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002887 * When no index given: "lp->ll_empty2" is TRUE.
2888 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002890 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002892 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002897 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002898 {
2899 if (!quiet)
2900 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002902 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002903 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002904 }
2905
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2907 if (lp->ll_n1 < 0)
2908 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2909 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002910 {
2911 if (!quiet)
2912 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002913 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002914 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002915 }
2916
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002917 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002918 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002919 }
2920
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 return p;
2922}
2923
2924/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002925 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 */
2927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002928clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002929{
2930 vim_free(lp->ll_exp_name);
2931 vim_free(lp->ll_newkey);
2932}
2933
2934/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002935 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002937 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002938 */
2939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002940set_var_lval(
2941 lval_T *lp,
2942 char_u *endp,
2943 typval_T *rettv,
2944 int copy,
2945 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002946{
2947 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002948 listitem_T *ri;
2949 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950
2951 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002954 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955 cc = *endp;
2956 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002957 if (op != NULL && *op != '=')
2958 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002959 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002961 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002963 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002964 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002966 if ((di == NULL
2967 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2968 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2969 FALSE)))
2970 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002971 set_var(lp->ll_name, &tv, FALSE);
2972 clear_tv(&tv);
2973 }
2974 }
2975 else
2976 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002978 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002980 else if (tv_check_lock(lp->ll_newkey == NULL
2981 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002982 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002983 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 else if (lp->ll_range)
2985 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002986 listitem_T *ll_li = lp->ll_li;
2987 int ll_n1 = lp->ll_n1;
2988
2989 /*
2990 * Check whether any of the list items is locked
2991 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002992 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002994 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002995 return;
2996 ri = ri->li_next;
2997 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2998 break;
2999 ll_li = ll_li->li_next;
3000 ++ll_n1;
3001 }
3002
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003003 /*
3004 * Assign the List values to the list items.
3005 */
3006 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003007 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003008 if (op != NULL && *op != '=')
3009 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3010 else
3011 {
3012 clear_tv(&lp->ll_li->li_tv);
3013 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3014 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 ri = ri->li_next;
3016 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3017 break;
3018 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003019 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003021 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003022 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003023 ri = NULL;
3024 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003026 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003027 lp->ll_li = lp->ll_li->li_next;
3028 ++lp->ll_n1;
3029 }
3030 if (ri != NULL)
3031 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003032 else if (lp->ll_empty2
3033 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003034 : lp->ll_n1 != lp->ll_n2)
3035 EMSG(_("E711: List value has not enough items"));
3036 }
3037 else
3038 {
3039 /*
3040 * Assign to a List or Dictionary item.
3041 */
3042 if (lp->ll_newkey != NULL)
3043 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003044 if (op != NULL && *op != '=')
3045 {
3046 EMSG2(_(e_letwrong), op);
3047 return;
3048 }
3049
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003051 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003052 if (di == NULL)
3053 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003054 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3055 {
3056 vim_free(di);
3057 return;
3058 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003059 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003060 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003061 else if (op != NULL && *op != '=')
3062 {
3063 tv_op(lp->ll_tv, rettv, op);
3064 return;
3065 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003066 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003068
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003069 /*
3070 * Assign the value to the variable or list item.
3071 */
3072 if (copy)
3073 copy_tv(rettv, lp->ll_tv);
3074 else
3075 {
3076 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003077 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003078 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079 }
3080 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003081}
3082
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003083/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3085 * Returns OK or FAIL.
3086 */
3087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003088tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089{
3090 long n;
3091 char_u numbuf[NUMBUFLEN];
3092 char_u *s;
3093
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003094 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3095 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3096 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003097 {
3098 switch (tv1->v_type)
3099 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003100 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003101 case VAR_DICT:
3102 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003103 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003104 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003105 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 break;
3107
3108 case VAR_LIST:
3109 if (*op != '+' || tv2->v_type != VAR_LIST)
3110 break;
3111 /* List += List */
3112 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3113 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3114 return OK;
3115
3116 case VAR_NUMBER:
3117 case VAR_STRING:
3118 if (tv2->v_type == VAR_LIST)
3119 break;
3120 if (*op == '+' || *op == '-')
3121 {
3122 /* nr += nr or nr -= nr*/
3123 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124#ifdef FEAT_FLOAT
3125 if (tv2->v_type == VAR_FLOAT)
3126 {
3127 float_T f = n;
3128
3129 if (*op == '+')
3130 f += tv2->vval.v_float;
3131 else
3132 f -= tv2->vval.v_float;
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_FLOAT;
3135 tv1->vval.v_float = f;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#endif
3139 {
3140 if (*op == '+')
3141 n += get_tv_number(tv2);
3142 else
3143 n -= get_tv_number(tv2);
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_NUMBER;
3146 tv1->vval.v_number = n;
3147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 else
3150 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151 if (tv2->v_type == VAR_FLOAT)
3152 break;
3153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 /* str .= str */
3155 s = get_tv_string(tv1);
3156 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3157 clear_tv(tv1);
3158 tv1->v_type = VAR_STRING;
3159 tv1->vval.v_string = s;
3160 }
3161 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162
3163#ifdef FEAT_FLOAT
3164 case VAR_FLOAT:
3165 {
3166 float_T f;
3167
3168 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3169 && tv2->v_type != VAR_NUMBER
3170 && tv2->v_type != VAR_STRING))
3171 break;
3172 if (tv2->v_type == VAR_FLOAT)
3173 f = tv2->vval.v_float;
3174 else
3175 f = get_tv_number(tv2);
3176 if (*op == '+')
3177 tv1->vval.v_float += f;
3178 else
3179 tv1->vval.v_float -= f;
3180 }
3181 return OK;
3182#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003183 }
3184 }
3185
3186 EMSG2(_(e_letwrong), op);
3187 return FAIL;
3188}
3189
3190/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * Add a watcher to a list.
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195{
3196 lw->lw_next = l->lv_watch;
3197 l->lv_watch = lw;
3198}
3199
3200/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003201 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 * No warning when it isn't found...
3203 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 lwp = &l->lv_watch;
3210 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3211 {
3212 if (lw == lwrem)
3213 {
3214 *lwp = lw->lw_next;
3215 break;
3216 }
3217 lwp = &lw->lw_next;
3218 }
3219}
3220
3221/*
3222 * Just before removing an item from a list: advance watchers to the next
3223 * item.
3224 */
3225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3231 if (lw->lw_item == item)
3232 lw->lw_item = item->li_next;
3233}
3234
3235/*
3236 * Evaluate the expression used in a ":for var in expr" command.
3237 * "arg" points to "var".
3238 * Set "*errp" to TRUE for an error, FALSE otherwise;
3239 * Return a pointer that holds the info. Null when there is an error.
3240 */
3241 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003242eval_for_line(
3243 char_u *arg,
3244 int *errp,
3245 char_u **nextcmdp,
3246 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 typval_T tv;
3251 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 *errp = TRUE; /* default: there is an error */
3254
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 if (fi == NULL)
3257 return NULL;
3258
3259 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3260 if (expr == NULL)
3261 return fi;
3262
3263 expr = skipwhite(expr);
3264 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3265 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003266 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 return fi;
3268 }
3269
3270 if (skip)
3271 ++emsg_skip;
3272 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3273 {
3274 *errp = FALSE;
3275 if (!skip)
3276 {
3277 l = tv.vval.v_list;
3278 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003281 clear_tv(&tv);
3282 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 else
3284 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003285 /* No need to increment the refcount, it's already set for the
3286 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 fi->fi_list = l;
3288 list_add_watch(l, &fi->fi_lw);
3289 fi->fi_lw.lw_item = l->lv_first;
3290 }
3291 }
3292 }
3293 if (skip)
3294 --emsg_skip;
3295
3296 return fi;
3297}
3298
3299/*
3300 * Use the first item in a ":for" list. Advance to the next.
3301 * Assign the values to the variable (list). "arg" points to the first one.
3302 * Return TRUE when a valid item was found, FALSE when at end of list or
3303 * something wrong.
3304 */
3305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003308 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311
3312 item = fi->fi_lw.lw_item;
3313 if (item == NULL)
3314 result = FALSE;
3315 else
3316 {
3317 fi->fi_lw.lw_item = item->li_next;
3318 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3319 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3320 }
3321 return result;
3322}
3323
3324/*
3325 * Free the structure used to store info used by ":for".
3326 */
3327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329{
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003332 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003335 list_unref(fi->fi_list);
3336 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 vim_free(fi);
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3341
3342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343set_context_for_expression(
3344 expand_T *xp,
3345 char_u *arg,
3346 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 int got_eq = FALSE;
3349 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (cmdidx == CMD_let)
3353 {
3354 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003358 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 {
3360 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 if (vim_iswhite(*p))
3363 break;
3364 }
3365 return;
3366 }
3367 }
3368 else
3369 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3370 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 while ((xp->xp_pattern = vim_strpbrk(arg,
3372 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3373 {
3374 c = *xp->xp_pattern;
3375 if (c == '&')
3376 {
3377 c = xp->xp_pattern[1];
3378 if (c == '&')
3379 {
3380 ++xp->xp_pattern;
3381 xp->xp_context = cmdidx != CMD_let || got_eq
3382 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3383 }
3384 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003387 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3388 xp->xp_pattern += 2;
3389
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 else if (c == '$')
3393 {
3394 /* environment variable */
3395 xp->xp_context = EXPAND_ENV_VARS;
3396 }
3397 else if (c == '=')
3398 {
3399 got_eq = TRUE;
3400 xp->xp_context = EXPAND_EXPRESSION;
3401 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 && xp->xp_context == EXPAND_FUNCTIONS
3404 && vim_strchr(xp->xp_pattern, '(') == NULL)
3405 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003406 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003407 break;
3408 }
3409 else if (cmdidx != CMD_let || got_eq)
3410 {
3411 if (c == '"') /* string */
3412 {
3413 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3414 if (c == '\\' && xp->xp_pattern[1] != NUL)
3415 ++xp->xp_pattern;
3416 xp->xp_context = EXPAND_NOTHING;
3417 }
3418 else if (c == '\'') /* literal string */
3419 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003420 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003421 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3422 /* skip */ ;
3423 xp->xp_context = EXPAND_NOTHING;
3424 }
3425 else if (c == '|')
3426 {
3427 if (xp->xp_pattern[1] == '|')
3428 {
3429 ++xp->xp_pattern;
3430 xp->xp_context = EXPAND_EXPRESSION;
3431 }
3432 else
3433 xp->xp_context = EXPAND_COMMANDS;
3434 }
3435 else
3436 xp->xp_context = EXPAND_EXPRESSION;
3437 }
3438 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003439 /* Doesn't look like something valid, expand as an expression
3440 * anyway. */
3441 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 arg = xp->xp_pattern;
3443 if (*arg != NUL)
3444 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3445 /* skip */ ;
3446 }
3447 xp->xp_pattern = arg;
3448}
3449
3450#endif /* FEAT_CMDL_COMPL */
3451
3452/*
3453 * ":1,25call func(arg1, arg2)" function call.
3454 */
3455 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003456ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003457{
3458 char_u *arg = eap->arg;
3459 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003461 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003464 linenr_T lnum;
3465 int doesrange;
3466 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003467 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 if (eap->skip)
3470 {
3471 /* trans_function_name() doesn't work well when skipping, use eval0()
3472 * instead to skip to any following command, e.g. for:
3473 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003474 ++emsg_skip;
3475 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3476 clear_tv(&rettv);
3477 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003478 return;
3479 }
3480
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003481 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003482 if (fudi.fd_newkey != NULL)
3483 {
3484 /* Still need to give an error message for missing key. */
3485 EMSG2(_(e_dictkey), fudi.fd_newkey);
3486 vim_free(fudi.fd_newkey);
3487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 if (tofree == NULL)
3489 return;
3490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003491 /* Increase refcount on dictionary, it could get deleted when evaluating
3492 * the arguments. */
3493 if (fudi.fd_dict != NULL)
3494 ++fudi.fd_dict->dv_refcount;
3495
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003496 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003497 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003498 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003499
Bram Moolenaar532c7802005-01-27 14:44:31 +00003500 /* Skip white space to allow ":call func ()". Not good, but required for
3501 * backward compatibility. */
3502 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003503 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003504
3505 if (*startarg != '(')
3506 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003507 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003508 goto end;
3509 }
3510
3511 /*
3512 * When skipping, evaluate the function once, to find the end of the
3513 * arguments.
3514 * When the function takes a range, this is discovered after the first
3515 * call, and the loop is broken.
3516 */
3517 if (eap->skip)
3518 {
3519 ++emsg_skip;
3520 lnum = eap->line2; /* do it once, also with an invalid range */
3521 }
3522 else
3523 lnum = eap->line1;
3524 for ( ; lnum <= eap->line2; ++lnum)
3525 {
3526 if (!eap->skip && eap->addr_count > 0)
3527 {
3528 curwin->w_cursor.lnum = lnum;
3529 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003530#ifdef FEAT_VIRTUALEDIT
3531 curwin->w_cursor.coladd = 0;
3532#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003533 }
3534 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003535 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003536 eap->line1, eap->line2, &doesrange,
3537 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003538 {
3539 failed = TRUE;
3540 break;
3541 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003542
3543 /* Handle a function returning a Funcref, Dictionary or List. */
3544 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3545 {
3546 failed = TRUE;
3547 break;
3548 }
3549
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003550 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 if (doesrange || eap->skip)
3552 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003553
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003557 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003558 if (aborting())
3559 break;
3560 }
3561 if (eap->skip)
3562 --emsg_skip;
3563
3564 if (!failed)
3565 {
3566 /* Check for trailing illegal characters and a following command. */
3567 if (!ends_excmd(*arg))
3568 {
3569 emsg_severe = TRUE;
3570 EMSG(_(e_trailing));
3571 }
3572 else
3573 eap->nextcmd = check_nextcmd(arg);
3574 }
3575
3576end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003577 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003578 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003579}
3580
3581/*
3582 * ":unlet[!] var1 ... " command.
3583 */
3584 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003585ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003586{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003587 ex_unletlock(eap, eap->arg, 0);
3588}
3589
3590/*
3591 * ":lockvar" and ":unlockvar" commands
3592 */
3593 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003594ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 int deep = 2;
3598
3599 if (eap->forceit)
3600 deep = -1;
3601 else if (vim_isdigit(*arg))
3602 {
3603 deep = getdigits(&arg);
3604 arg = skipwhite(arg);
3605 }
3606
3607 ex_unletlock(eap, arg, deep);
3608}
3609
3610/*
3611 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3612 */
3613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003614ex_unletlock(
3615 exarg_T *eap,
3616 char_u *argstart,
3617 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003618{
3619 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003622 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623
3624 do
3625 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003626 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003627 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003628 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 if (lv.ll_name == NULL)
3630 error = TRUE; /* error but continue parsing */
3631 if (name_end == NULL || (!vim_iswhite(*name_end)
3632 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003634 if (name_end != NULL)
3635 {
3636 emsg_severe = TRUE;
3637 EMSG(_(e_trailing));
3638 }
3639 if (!(eap->skip || error))
3640 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003641 break;
3642 }
3643
3644 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003645 {
3646 if (eap->cmdidx == CMD_unlet)
3647 {
3648 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3649 error = TRUE;
3650 }
3651 else
3652 {
3653 if (do_lock_var(&lv, name_end, deep,
3654 eap->cmdidx == CMD_lockvar) == FAIL)
3655 error = TRUE;
3656 }
3657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003658
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003659 if (!eap->skip)
3660 clear_lval(&lv);
3661
Bram Moolenaar071d4272004-06-13 20:20:40 +00003662 arg = skipwhite(name_end);
3663 } while (!ends_excmd(*arg));
3664
3665 eap->nextcmd = check_nextcmd(arg);
3666}
3667
Bram Moolenaar8c711452005-01-14 21:53:12 +00003668 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003669do_unlet_var(
3670 lval_T *lp,
3671 char_u *name_end,
3672 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003673{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003674 int ret = OK;
3675 int cc;
3676
3677 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003679 cc = *name_end;
3680 *name_end = NUL;
3681
3682 /* Normal name or expanded name. */
3683 if (check_changedtick(lp->ll_name))
3684 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003685 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003686 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003687 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003691 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003692 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003693 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003694 else if (lp->ll_range)
3695 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003696 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003698 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003699
3700 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3701 {
3702 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003703 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003704 return FAIL;
3705 ll_li = li;
3706 ++ll_n1;
3707 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003708
3709 /* Delete a range of List items. */
3710 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3711 {
3712 li = lp->ll_li->li_next;
3713 listitem_remove(lp->ll_list, lp->ll_li);
3714 lp->ll_li = li;
3715 ++lp->ll_n1;
3716 }
3717 }
3718 else
3719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003720 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 /* unlet a List item. */
3722 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003725 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 }
3727
3728 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003729}
3730
Bram Moolenaar071d4272004-06-13 20:20:40 +00003731/*
3732 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003733 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734 */
3735 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003736do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737{
Bram Moolenaar33570922005-01-25 22:26:29 +00003738 hashtab_T *ht;
3739 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003740 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003741 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003742 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 ht = find_var_ht(name, &varname);
3745 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003747 if (ht == &globvarht)
3748 d = &globvardict;
3749 else if (current_funccal != NULL
3750 && ht == &current_funccal->l_vars.dv_hashtab)
3751 d = &current_funccal->l_vars;
3752 else if (ht == &compat_hashtab)
3753 d = &vimvardict;
3754 else
3755 {
3756 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3757 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3758 }
3759 if (d == NULL)
3760 {
3761 EMSG2(_(e_intern2), "do_unlet()");
3762 return FAIL;
3763 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003764 hi = hash_find(ht, varname);
3765 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003766 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003767 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003768 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003769 || var_check_ro(di->di_flags, name, FALSE)
3770 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003771 return FAIL;
3772
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003773 delete_var(ht, hi);
3774 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003775 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003776 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003777 if (forceit)
3778 return OK;
3779 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003780 return FAIL;
3781}
3782
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783/*
3784 * Lock or unlock variable indicated by "lp".
3785 * "deep" is the levels to go (-1 for unlimited);
3786 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3787 */
3788 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003789do_lock_var(
3790 lval_T *lp,
3791 char_u *name_end,
3792 int deep,
3793 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003794{
3795 int ret = OK;
3796 int cc;
3797 dictitem_T *di;
3798
3799 if (deep == 0) /* nothing to do */
3800 return OK;
3801
3802 if (lp->ll_tv == NULL)
3803 {
3804 cc = *name_end;
3805 *name_end = NUL;
3806
3807 /* Normal name or expanded name. */
3808 if (check_changedtick(lp->ll_name))
3809 ret = FAIL;
3810 else
3811 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003812 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003813 if (di == NULL)
3814 ret = FAIL;
3815 else
3816 {
3817 if (lock)
3818 di->di_flags |= DI_FLAGS_LOCK;
3819 else
3820 di->di_flags &= ~DI_FLAGS_LOCK;
3821 item_lock(&di->di_tv, deep, lock);
3822 }
3823 }
3824 *name_end = cc;
3825 }
3826 else if (lp->ll_range)
3827 {
3828 listitem_T *li = lp->ll_li;
3829
3830 /* (un)lock a range of List items. */
3831 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3832 {
3833 item_lock(&li->li_tv, deep, lock);
3834 li = li->li_next;
3835 ++lp->ll_n1;
3836 }
3837 }
3838 else if (lp->ll_list != NULL)
3839 /* (un)lock a List item. */
3840 item_lock(&lp->ll_li->li_tv, deep, lock);
3841 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003842 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003843 item_lock(&lp->ll_di->di_tv, deep, lock);
3844
3845 return ret;
3846}
3847
3848/*
3849 * Lock or unlock an item. "deep" is nr of levels to go.
3850 */
3851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003852item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003853{
3854 static int recurse = 0;
3855 list_T *l;
3856 listitem_T *li;
3857 dict_T *d;
3858 hashitem_T *hi;
3859 int todo;
3860
3861 if (recurse >= DICT_MAXNEST)
3862 {
3863 EMSG(_("E743: variable nested too deep for (un)lock"));
3864 return;
3865 }
3866 if (deep == 0)
3867 return;
3868 ++recurse;
3869
3870 /* lock/unlock the item itself */
3871 if (lock)
3872 tv->v_lock |= VAR_LOCKED;
3873 else
3874 tv->v_lock &= ~VAR_LOCKED;
3875
3876 switch (tv->v_type)
3877 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003878 case VAR_UNKNOWN:
3879 case VAR_NUMBER:
3880 case VAR_STRING:
3881 case VAR_FUNC:
3882 case VAR_FLOAT:
3883 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003884 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003885 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003886 break;
3887
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003888 case VAR_LIST:
3889 if ((l = tv->vval.v_list) != NULL)
3890 {
3891 if (lock)
3892 l->lv_lock |= VAR_LOCKED;
3893 else
3894 l->lv_lock &= ~VAR_LOCKED;
3895 if (deep < 0 || deep > 1)
3896 /* recursive: lock/unlock the items the List contains */
3897 for (li = l->lv_first; li != NULL; li = li->li_next)
3898 item_lock(&li->li_tv, deep - 1, lock);
3899 }
3900 break;
3901 case VAR_DICT:
3902 if ((d = tv->vval.v_dict) != NULL)
3903 {
3904 if (lock)
3905 d->dv_lock |= VAR_LOCKED;
3906 else
3907 d->dv_lock &= ~VAR_LOCKED;
3908 if (deep < 0 || deep > 1)
3909 {
3910 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003911 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003912 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3913 {
3914 if (!HASHITEM_EMPTY(hi))
3915 {
3916 --todo;
3917 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3918 }
3919 }
3920 }
3921 }
3922 }
3923 --recurse;
3924}
3925
Bram Moolenaara40058a2005-07-11 22:42:07 +00003926/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003927 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3928 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003929 */
3930 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003931tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003932{
3933 return (tv->v_lock & VAR_LOCKED)
3934 || (tv->v_type == VAR_LIST
3935 && tv->vval.v_list != NULL
3936 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3937 || (tv->v_type == VAR_DICT
3938 && tv->vval.v_dict != NULL
3939 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3940}
3941
Bram Moolenaar071d4272004-06-13 20:20:40 +00003942#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3943/*
3944 * Delete all "menutrans_" variables.
3945 */
3946 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003947del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003948{
Bram Moolenaar33570922005-01-25 22:26:29 +00003949 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003950 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003951
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003953 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003954 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003955 {
3956 if (!HASHITEM_EMPTY(hi))
3957 {
3958 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3960 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 }
3962 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003964}
3965#endif
3966
3967#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3968
3969/*
3970 * Local string buffer for the next two functions to store a variable name
3971 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3972 * get_user_var_name().
3973 */
3974
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003975static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003976
3977static char_u *varnamebuf = NULL;
3978static int varnamebuflen = 0;
3979
3980/*
3981 * Function to concatenate a prefix and a variable name.
3982 */
3983 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003984cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003985{
3986 int len;
3987
3988 len = (int)STRLEN(name) + 3;
3989 if (len > varnamebuflen)
3990 {
3991 vim_free(varnamebuf);
3992 len += 10; /* some additional space */
3993 varnamebuf = alloc(len);
3994 if (varnamebuf == NULL)
3995 {
3996 varnamebuflen = 0;
3997 return NULL;
3998 }
3999 varnamebuflen = len;
4000 }
4001 *varnamebuf = prefix;
4002 varnamebuf[1] = ':';
4003 STRCPY(varnamebuf + 2, name);
4004 return varnamebuf;
4005}
4006
4007/*
4008 * Function given to ExpandGeneric() to obtain the list of user defined
4009 * (global/buffer/window/built-in) variable names.
4010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004012get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004013{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004014 static long_u gdone;
4015 static long_u bdone;
4016 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004017#ifdef FEAT_WINDOWS
4018 static long_u tdone;
4019#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004020 static int vidx;
4021 static hashitem_T *hi;
4022 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004023
4024 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004026 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004027#ifdef FEAT_WINDOWS
4028 tdone = 0;
4029#endif
4030 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004031
4032 /* Global variables */
4033 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004035 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004036 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 else
4038 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 while (HASHITEM_EMPTY(hi))
4040 ++hi;
4041 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4042 return cat_prefix_varname('g', hi->hi_key);
4043 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004045
4046 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004047 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004048 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004049 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004051 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004052 else
4053 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 while (HASHITEM_EMPTY(hi))
4055 ++hi;
4056 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004060 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 return (char_u *)"b:changedtick";
4062 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004063
4064 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004065 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004066 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004067 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004068 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004070 else
4071 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004072 while (HASHITEM_EMPTY(hi))
4073 ++hi;
4074 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004076
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004077#ifdef FEAT_WINDOWS
4078 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004079 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004080 if (tdone < ht->ht_used)
4081 {
4082 if (tdone++ == 0)
4083 hi = ht->ht_array;
4084 else
4085 ++hi;
4086 while (HASHITEM_EMPTY(hi))
4087 ++hi;
4088 return cat_prefix_varname('t', hi->hi_key);
4089 }
4090#endif
4091
Bram Moolenaar33570922005-01-25 22:26:29 +00004092 /* v: variables */
4093 if (vidx < VV_LEN)
4094 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004095
4096 vim_free(varnamebuf);
4097 varnamebuf = NULL;
4098 varnamebuflen = 0;
4099 return NULL;
4100}
4101
4102#endif /* FEAT_CMDL_COMPL */
4103
4104/*
4105 * types for expressions.
4106 */
4107typedef enum
4108{
4109 TYPE_UNKNOWN = 0
4110 , TYPE_EQUAL /* == */
4111 , TYPE_NEQUAL /* != */
4112 , TYPE_GREATER /* > */
4113 , TYPE_GEQUAL /* >= */
4114 , TYPE_SMALLER /* < */
4115 , TYPE_SEQUAL /* <= */
4116 , TYPE_MATCH /* =~ */
4117 , TYPE_NOMATCH /* !~ */
4118} exptype_T;
4119
4120/*
4121 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004122 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004123 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4124 */
4125
4126/*
4127 * Handle zero level expression.
4128 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004130 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004131 * Return OK or FAIL.
4132 */
4133 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004134eval0(
4135 char_u *arg,
4136 typval_T *rettv,
4137 char_u **nextcmd,
4138 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004139{
4140 int ret;
4141 char_u *p;
4142
4143 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004144 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004145 if (ret == FAIL || !ends_excmd(*p))
4146 {
4147 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 /*
4150 * Report the invalid expression unless the expression evaluation has
4151 * been cancelled due to an aborting error, an interrupt, or an
4152 * exception.
4153 */
4154 if (!aborting())
4155 EMSG2(_(e_invexpr2), arg);
4156 ret = FAIL;
4157 }
4158 if (nextcmd != NULL)
4159 *nextcmd = check_nextcmd(p);
4160
4161 return ret;
4162}
4163
4164/*
4165 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004166 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 *
4168 * "arg" must point to the first non-white of the expression.
4169 * "arg" is advanced to the next non-white after the recognized expression.
4170 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004171 * Note: "rettv.v_lock" is not set.
4172 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * Return OK or FAIL.
4174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177{
4178 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004179 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180
4181 /*
4182 * Get the first variable.
4183 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 return FAIL;
4186
4187 if ((*arg)[0] == '?')
4188 {
4189 result = FALSE;
4190 if (evaluate)
4191 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004192 int error = FALSE;
4193
4194 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004195 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004196 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004197 if (error)
4198 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 }
4200
4201 /*
4202 * Get the second variable.
4203 */
4204 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004205 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 return FAIL;
4207
4208 /*
4209 * Check for the ":".
4210 */
4211 if ((*arg)[0] != ':')
4212 {
4213 EMSG(_("E109: Missing ':' after '?'"));
4214 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004215 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216 return FAIL;
4217 }
4218
4219 /*
4220 * Get the third variable.
4221 */
4222 *arg = skipwhite(*arg + 1);
4223 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4224 {
4225 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228 }
4229 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 }
4232
4233 return OK;
4234}
4235
4236/*
4237 * Handle first level expression:
4238 * expr2 || expr2 || expr2 logical OR
4239 *
4240 * "arg" must point to the first non-white of the expression.
4241 * "arg" is advanced to the next non-white after the recognized expression.
4242 *
4243 * Return OK or FAIL.
4244 */
4245 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004246eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247{
Bram Moolenaar33570922005-01-25 22:26:29 +00004248 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004249 long result;
4250 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004251 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252
4253 /*
4254 * Get the first variable.
4255 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004256 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004257 return FAIL;
4258
4259 /*
4260 * Repeat until there is no following "||".
4261 */
4262 first = TRUE;
4263 result = FALSE;
4264 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4265 {
4266 if (evaluate && first)
4267 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004268 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004270 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004271 if (error)
4272 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 first = FALSE;
4274 }
4275
4276 /*
4277 * Get the second variable.
4278 */
4279 *arg = skipwhite(*arg + 2);
4280 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4281 return FAIL;
4282
4283 /*
4284 * Compute the result.
4285 */
4286 if (evaluate && !result)
4287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004288 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004290 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004291 if (error)
4292 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 }
4294 if (evaluate)
4295 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004296 rettv->v_type = VAR_NUMBER;
4297 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004298 }
4299 }
4300
4301 return OK;
4302}
4303
4304/*
4305 * Handle second level expression:
4306 * expr3 && expr3 && expr3 logical AND
4307 *
4308 * "arg" must point to the first non-white of the expression.
4309 * "arg" is advanced to the next non-white after the recognized expression.
4310 *
4311 * Return OK or FAIL.
4312 */
4313 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004314eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315{
Bram Moolenaar33570922005-01-25 22:26:29 +00004316 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004317 long result;
4318 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004319 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004320
4321 /*
4322 * Get the first variable.
4323 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004324 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 return FAIL;
4326
4327 /*
4328 * Repeat until there is no following "&&".
4329 */
4330 first = TRUE;
4331 result = TRUE;
4332 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4333 {
4334 if (evaluate && first)
4335 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004336 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004337 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004339 if (error)
4340 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 first = FALSE;
4342 }
4343
4344 /*
4345 * Get the second variable.
4346 */
4347 *arg = skipwhite(*arg + 2);
4348 if (eval4(arg, &var2, evaluate && result) == FAIL)
4349 return FAIL;
4350
4351 /*
4352 * Compute the result.
4353 */
4354 if (evaluate && result)
4355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004356 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004358 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004359 if (error)
4360 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 }
4362 if (evaluate)
4363 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004364 rettv->v_type = VAR_NUMBER;
4365 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004366 }
4367 }
4368
4369 return OK;
4370}
4371
4372/*
4373 * Handle third level expression:
4374 * var1 == var2
4375 * var1 =~ var2
4376 * var1 != var2
4377 * var1 !~ var2
4378 * var1 > var2
4379 * var1 >= var2
4380 * var1 < var2
4381 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004382 * var1 is var2
4383 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 *
4385 * "arg" must point to the first non-white of the expression.
4386 * "arg" is advanced to the next non-white after the recognized expression.
4387 *
4388 * Return OK or FAIL.
4389 */
4390 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004391eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392{
Bram Moolenaar33570922005-01-25 22:26:29 +00004393 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 char_u *p;
4395 int i;
4396 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004397 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 int len = 2;
4399 long n1, n2;
4400 char_u *s1, *s2;
4401 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4402 regmatch_T regmatch;
4403 int ic;
4404 char_u *save_cpo;
4405
4406 /*
4407 * Get the first variable.
4408 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004409 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004410 return FAIL;
4411
4412 p = *arg;
4413 switch (p[0])
4414 {
4415 case '=': if (p[1] == '=')
4416 type = TYPE_EQUAL;
4417 else if (p[1] == '~')
4418 type = TYPE_MATCH;
4419 break;
4420 case '!': if (p[1] == '=')
4421 type = TYPE_NEQUAL;
4422 else if (p[1] == '~')
4423 type = TYPE_NOMATCH;
4424 break;
4425 case '>': if (p[1] != '=')
4426 {
4427 type = TYPE_GREATER;
4428 len = 1;
4429 }
4430 else
4431 type = TYPE_GEQUAL;
4432 break;
4433 case '<': if (p[1] != '=')
4434 {
4435 type = TYPE_SMALLER;
4436 len = 1;
4437 }
4438 else
4439 type = TYPE_SEQUAL;
4440 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004441 case 'i': if (p[1] == 's')
4442 {
4443 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4444 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004445 i = p[len];
4446 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004447 {
4448 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4449 type_is = TRUE;
4450 }
4451 }
4452 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004453 }
4454
4455 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004456 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
4458 if (type != TYPE_UNKNOWN)
4459 {
4460 /* extra question mark appended: ignore case */
4461 if (p[len] == '?')
4462 {
4463 ic = TRUE;
4464 ++len;
4465 }
4466 /* extra '#' appended: match case */
4467 else if (p[len] == '#')
4468 {
4469 ic = FALSE;
4470 ++len;
4471 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004472 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 else
4474 ic = p_ic;
4475
4476 /*
4477 * Get the second variable.
4478 */
4479 *arg = skipwhite(p + len);
4480 if (eval5(arg, &var2, evaluate) == FAIL)
4481 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004482 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 return FAIL;
4484 }
4485
4486 if (evaluate)
4487 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004488 if (type_is && rettv->v_type != var2.v_type)
4489 {
4490 /* For "is" a different type always means FALSE, for "notis"
4491 * it means TRUE. */
4492 n1 = (type == TYPE_NEQUAL);
4493 }
4494 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4495 {
4496 if (type_is)
4497 {
4498 n1 = (rettv->v_type == var2.v_type
4499 && rettv->vval.v_list == var2.vval.v_list);
4500 if (type == TYPE_NEQUAL)
4501 n1 = !n1;
4502 }
4503 else if (rettv->v_type != var2.v_type
4504 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4505 {
4506 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004507 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004509 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004510 clear_tv(rettv);
4511 clear_tv(&var2);
4512 return FAIL;
4513 }
4514 else
4515 {
4516 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004517 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4518 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004519 if (type == TYPE_NEQUAL)
4520 n1 = !n1;
4521 }
4522 }
4523
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004524 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4525 {
4526 if (type_is)
4527 {
4528 n1 = (rettv->v_type == var2.v_type
4529 && rettv->vval.v_dict == var2.vval.v_dict);
4530 if (type == TYPE_NEQUAL)
4531 n1 = !n1;
4532 }
4533 else if (rettv->v_type != var2.v_type
4534 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4535 {
4536 if (rettv->v_type != var2.v_type)
4537 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4538 else
4539 EMSG(_("E736: Invalid operation for Dictionary"));
4540 clear_tv(rettv);
4541 clear_tv(&var2);
4542 return FAIL;
4543 }
4544 else
4545 {
4546 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004547 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4548 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004549 if (type == TYPE_NEQUAL)
4550 n1 = !n1;
4551 }
4552 }
4553
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004554 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4555 {
4556 if (rettv->v_type != var2.v_type
4557 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4558 {
4559 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004562 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 clear_tv(rettv);
4564 clear_tv(&var2);
4565 return FAIL;
4566 }
4567 else
4568 {
4569 /* Compare two Funcrefs for being equal or unequal. */
4570 if (rettv->vval.v_string == NULL
4571 || var2.vval.v_string == NULL)
4572 n1 = FALSE;
4573 else
4574 n1 = STRCMP(rettv->vval.v_string,
4575 var2.vval.v_string) == 0;
4576 if (type == TYPE_NEQUAL)
4577 n1 = !n1;
4578 }
4579 }
4580
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004581#ifdef FEAT_FLOAT
4582 /*
4583 * If one of the two variables is a float, compare as a float.
4584 * When using "=~" or "!~", always compare as string.
4585 */
4586 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4587 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4588 {
4589 float_T f1, f2;
4590
4591 if (rettv->v_type == VAR_FLOAT)
4592 f1 = rettv->vval.v_float;
4593 else
4594 f1 = get_tv_number(rettv);
4595 if (var2.v_type == VAR_FLOAT)
4596 f2 = var2.vval.v_float;
4597 else
4598 f2 = get_tv_number(&var2);
4599 n1 = FALSE;
4600 switch (type)
4601 {
4602 case TYPE_EQUAL: n1 = (f1 == f2); break;
4603 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4604 case TYPE_GREATER: n1 = (f1 > f2); break;
4605 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4606 case TYPE_SMALLER: n1 = (f1 < f2); break;
4607 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4608 case TYPE_UNKNOWN:
4609 case TYPE_MATCH:
4610 case TYPE_NOMATCH: break; /* avoid gcc warning */
4611 }
4612 }
4613#endif
4614
Bram Moolenaar071d4272004-06-13 20:20:40 +00004615 /*
4616 * If one of the two variables is a number, compare as a number.
4617 * When using "=~" or "!~", always compare as string.
4618 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004619 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004620 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4621 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004622 n1 = get_tv_number(rettv);
4623 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004624 switch (type)
4625 {
4626 case TYPE_EQUAL: n1 = (n1 == n2); break;
4627 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4628 case TYPE_GREATER: n1 = (n1 > n2); break;
4629 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4630 case TYPE_SMALLER: n1 = (n1 < n2); break;
4631 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4632 case TYPE_UNKNOWN:
4633 case TYPE_MATCH:
4634 case TYPE_NOMATCH: break; /* avoid gcc warning */
4635 }
4636 }
4637 else
4638 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004639 s1 = get_tv_string_buf(rettv, buf1);
4640 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004641 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4642 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4643 else
4644 i = 0;
4645 n1 = FALSE;
4646 switch (type)
4647 {
4648 case TYPE_EQUAL: n1 = (i == 0); break;
4649 case TYPE_NEQUAL: n1 = (i != 0); break;
4650 case TYPE_GREATER: n1 = (i > 0); break;
4651 case TYPE_GEQUAL: n1 = (i >= 0); break;
4652 case TYPE_SMALLER: n1 = (i < 0); break;
4653 case TYPE_SEQUAL: n1 = (i <= 0); break;
4654
4655 case TYPE_MATCH:
4656 case TYPE_NOMATCH:
4657 /* avoid 'l' flag in 'cpoptions' */
4658 save_cpo = p_cpo;
4659 p_cpo = (char_u *)"";
4660 regmatch.regprog = vim_regcomp(s2,
4661 RE_MAGIC + RE_STRING);
4662 regmatch.rm_ic = ic;
4663 if (regmatch.regprog != NULL)
4664 {
4665 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004666 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004667 if (type == TYPE_NOMATCH)
4668 n1 = !n1;
4669 }
4670 p_cpo = save_cpo;
4671 break;
4672
4673 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4674 }
4675 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004676 clear_tv(rettv);
4677 clear_tv(&var2);
4678 rettv->v_type = VAR_NUMBER;
4679 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004680 }
4681 }
4682
4683 return OK;
4684}
4685
4686/*
4687 * Handle fourth level expression:
4688 * + number addition
4689 * - number subtraction
4690 * . string concatenation
4691 *
4692 * "arg" must point to the first non-white of the expression.
4693 * "arg" is advanced to the next non-white after the recognized expression.
4694 *
4695 * Return OK or FAIL.
4696 */
4697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004698eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699{
Bram Moolenaar33570922005-01-25 22:26:29 +00004700 typval_T var2;
4701 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004702 int op;
4703 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004704#ifdef FEAT_FLOAT
4705 float_T f1 = 0, f2 = 0;
4706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 char_u *s1, *s2;
4708 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4709 char_u *p;
4710
4711 /*
4712 * Get the first variable.
4713 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004714 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004715 return FAIL;
4716
4717 /*
4718 * Repeat computing, until no '+', '-' or '.' is following.
4719 */
4720 for (;;)
4721 {
4722 op = **arg;
4723 if (op != '+' && op != '-' && op != '.')
4724 break;
4725
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004726 if ((op != '+' || rettv->v_type != VAR_LIST)
4727#ifdef FEAT_FLOAT
4728 && (op == '.' || rettv->v_type != VAR_FLOAT)
4729#endif
4730 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004731 {
4732 /* For "list + ...", an illegal use of the first operand as
4733 * a number cannot be determined before evaluating the 2nd
4734 * operand: if this is also a list, all is ok.
4735 * For "something . ...", "something - ..." or "non-list + ...",
4736 * we know that the first operand needs to be a string or number
4737 * without evaluating the 2nd operand. So check before to avoid
4738 * side effects after an error. */
4739 if (evaluate && get_tv_string_chk(rettv) == NULL)
4740 {
4741 clear_tv(rettv);
4742 return FAIL;
4743 }
4744 }
4745
Bram Moolenaar071d4272004-06-13 20:20:40 +00004746 /*
4747 * Get the second variable.
4748 */
4749 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004750 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004752 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004753 return FAIL;
4754 }
4755
4756 if (evaluate)
4757 {
4758 /*
4759 * Compute the result.
4760 */
4761 if (op == '.')
4762 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004763 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4764 s2 = get_tv_string_buf_chk(&var2, buf2);
4765 if (s2 == NULL) /* type error ? */
4766 {
4767 clear_tv(rettv);
4768 clear_tv(&var2);
4769 return FAIL;
4770 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004771 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004772 clear_tv(rettv);
4773 rettv->v_type = VAR_STRING;
4774 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004776 else if (op == '+' && rettv->v_type == VAR_LIST
4777 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004778 {
4779 /* concatenate Lists */
4780 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4781 &var3) == FAIL)
4782 {
4783 clear_tv(rettv);
4784 clear_tv(&var2);
4785 return FAIL;
4786 }
4787 clear_tv(rettv);
4788 *rettv = var3;
4789 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004790 else
4791 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004792 int error = FALSE;
4793
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004794#ifdef FEAT_FLOAT
4795 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004796 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004797 f1 = rettv->vval.v_float;
4798 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004799 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004800 else
4801#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004802 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004803 n1 = get_tv_number_chk(rettv, &error);
4804 if (error)
4805 {
4806 /* This can only happen for "list + non-list". For
4807 * "non-list + ..." or "something - ...", we returned
4808 * before evaluating the 2nd operand. */
4809 clear_tv(rettv);
4810 return FAIL;
4811 }
4812#ifdef FEAT_FLOAT
4813 if (var2.v_type == VAR_FLOAT)
4814 f1 = n1;
4815#endif
4816 }
4817#ifdef FEAT_FLOAT
4818 if (var2.v_type == VAR_FLOAT)
4819 {
4820 f2 = var2.vval.v_float;
4821 n2 = 0;
4822 }
4823 else
4824#endif
4825 {
4826 n2 = get_tv_number_chk(&var2, &error);
4827 if (error)
4828 {
4829 clear_tv(rettv);
4830 clear_tv(&var2);
4831 return FAIL;
4832 }
4833#ifdef FEAT_FLOAT
4834 if (rettv->v_type == VAR_FLOAT)
4835 f2 = n2;
4836#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004837 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004838 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004839
4840#ifdef FEAT_FLOAT
4841 /* If there is a float on either side the result is a float. */
4842 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4843 {
4844 if (op == '+')
4845 f1 = f1 + f2;
4846 else
4847 f1 = f1 - f2;
4848 rettv->v_type = VAR_FLOAT;
4849 rettv->vval.v_float = f1;
4850 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004851 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004852#endif
4853 {
4854 if (op == '+')
4855 n1 = n1 + n2;
4856 else
4857 n1 = n1 - n2;
4858 rettv->v_type = VAR_NUMBER;
4859 rettv->vval.v_number = n1;
4860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004862 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 }
4864 }
4865 return OK;
4866}
4867
4868/*
4869 * Handle fifth level expression:
4870 * * number multiplication
4871 * / number division
4872 * % number modulo
4873 *
4874 * "arg" must point to the first non-white of the expression.
4875 * "arg" is advanced to the next non-white after the recognized expression.
4876 *
4877 * Return OK or FAIL.
4878 */
4879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004880eval6(
4881 char_u **arg,
4882 typval_T *rettv,
4883 int evaluate,
4884 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885{
Bram Moolenaar33570922005-01-25 22:26:29 +00004886 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004887 int op;
4888 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004889#ifdef FEAT_FLOAT
4890 int use_float = FALSE;
4891 float_T f1 = 0, f2;
4892#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004893 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004894
4895 /*
4896 * Get the first variable.
4897 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004898 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899 return FAIL;
4900
4901 /*
4902 * Repeat computing, until no '*', '/' or '%' is following.
4903 */
4904 for (;;)
4905 {
4906 op = **arg;
4907 if (op != '*' && op != '/' && op != '%')
4908 break;
4909
4910 if (evaluate)
4911 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004912#ifdef FEAT_FLOAT
4913 if (rettv->v_type == VAR_FLOAT)
4914 {
4915 f1 = rettv->vval.v_float;
4916 use_float = TRUE;
4917 n1 = 0;
4918 }
4919 else
4920#endif
4921 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004922 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004923 if (error)
4924 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004925 }
4926 else
4927 n1 = 0;
4928
4929 /*
4930 * Get the second variable.
4931 */
4932 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004933 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 return FAIL;
4935
4936 if (evaluate)
4937 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004938#ifdef FEAT_FLOAT
4939 if (var2.v_type == VAR_FLOAT)
4940 {
4941 if (!use_float)
4942 {
4943 f1 = n1;
4944 use_float = TRUE;
4945 }
4946 f2 = var2.vval.v_float;
4947 n2 = 0;
4948 }
4949 else
4950#endif
4951 {
4952 n2 = get_tv_number_chk(&var2, &error);
4953 clear_tv(&var2);
4954 if (error)
4955 return FAIL;
4956#ifdef FEAT_FLOAT
4957 if (use_float)
4958 f2 = n2;
4959#endif
4960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004961
4962 /*
4963 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004966#ifdef FEAT_FLOAT
4967 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004968 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004969 if (op == '*')
4970 f1 = f1 * f2;
4971 else if (op == '/')
4972 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973# ifdef VMS
4974 /* VMS crashes on divide by zero, work around it */
4975 if (f2 == 0.0)
4976 {
4977 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004978 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004982 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004983 }
4984 else
4985 f1 = f1 / f2;
4986# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 /* We rely on the floating point library to handle divide
4988 * by zero to result in "inf" and not a crash. */
4989 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004994 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 return FAIL;
4996 }
4997 rettv->v_type = VAR_FLOAT;
4998 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004999 }
5000 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005002 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005003 if (op == '*')
5004 n1 = n1 * n2;
5005 else if (op == '/')
5006 {
5007 if (n2 == 0) /* give an error message? */
5008 {
5009 if (n1 == 0)
5010 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5011 else if (n1 < 0)
5012 n1 = -0x7fffffffL;
5013 else
5014 n1 = 0x7fffffffL;
5015 }
5016 else
5017 n1 = n1 / n2;
5018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 {
5021 if (n2 == 0) /* give an error message? */
5022 n1 = 0;
5023 else
5024 n1 = n1 % n2;
5025 }
5026 rettv->v_type = VAR_NUMBER;
5027 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005029 }
5030 }
5031
5032 return OK;
5033}
5034
5035/*
5036 * Handle sixth level expression:
5037 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005038 * "string" string constant
5039 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 * &option-name option value
5041 * @r register contents
5042 * identifier variable value
5043 * function() function call
5044 * $VAR environment variable
5045 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005046 * [expr, expr] List
5047 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005048 *
5049 * Also handle:
5050 * ! in front logical NOT
5051 * - in front unary minus
5052 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005053 * trailing [] subscript in String or List
5054 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005055 *
5056 * "arg" must point to the first non-white of the expression.
5057 * "arg" is advanced to the next non-white after the recognized expression.
5058 *
5059 * Return OK or FAIL.
5060 */
5061 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005062eval7(
5063 char_u **arg,
5064 typval_T *rettv,
5065 int evaluate,
5066 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005067{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005068 long n;
5069 int len;
5070 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071 char_u *start_leader, *end_leader;
5072 int ret = OK;
5073 char_u *alias;
5074
5075 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005076 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005077 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005079 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080
5081 /*
5082 * Skip '!' and '-' characters. They are handled later.
5083 */
5084 start_leader = *arg;
5085 while (**arg == '!' || **arg == '-' || **arg == '+')
5086 *arg = skipwhite(*arg + 1);
5087 end_leader = *arg;
5088
5089 switch (**arg)
5090 {
5091 /*
5092 * Number constant.
5093 */
5094 case '0':
5095 case '1':
5096 case '2':
5097 case '3':
5098 case '4':
5099 case '5':
5100 case '6':
5101 case '7':
5102 case '8':
5103 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005104 {
5105#ifdef FEAT_FLOAT
5106 char_u *p = skipdigits(*arg + 1);
5107 int get_float = FALSE;
5108
5109 /* We accept a float when the format matches
5110 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005111 * strict to avoid backwards compatibility problems.
5112 * Don't look for a float after the "." operator, so that
5113 * ":let vers = 1.2.3" doesn't fail. */
5114 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005116 get_float = TRUE;
5117 p = skipdigits(p + 2);
5118 if (*p == 'e' || *p == 'E')
5119 {
5120 ++p;
5121 if (*p == '-' || *p == '+')
5122 ++p;
5123 if (!vim_isdigit(*p))
5124 get_float = FALSE;
5125 else
5126 p = skipdigits(p + 1);
5127 }
5128 if (ASCII_ISALPHA(*p) || *p == '.')
5129 get_float = FALSE;
5130 }
5131 if (get_float)
5132 {
5133 float_T f;
5134
5135 *arg += string2float(*arg, &f);
5136 if (evaluate)
5137 {
5138 rettv->v_type = VAR_FLOAT;
5139 rettv->vval.v_float = f;
5140 }
5141 }
5142 else
5143#endif
5144 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005145 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 *arg += len;
5147 if (evaluate)
5148 {
5149 rettv->v_type = VAR_NUMBER;
5150 rettv->vval.v_number = n;
5151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 }
5153 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155
5156 /*
5157 * String constant: "string".
5158 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005159 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005160 break;
5161
5162 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005163 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005165 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005166 break;
5167
5168 /*
5169 * List: [expr, expr]
5170 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005171 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172 break;
5173
5174 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005175 * Dictionary: {key: val, key: val}
5176 */
5177 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5178 break;
5179
5180 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005181 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005183 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005184 break;
5185
5186 /*
5187 * Environment variable: $VAR.
5188 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005189 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005190 break;
5191
5192 /*
5193 * Register contents: @r.
5194 */
5195 case '@': ++*arg;
5196 if (evaluate)
5197 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005198 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005199 rettv->vval.v_string = get_reg_contents(**arg,
5200 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 }
5202 if (**arg != NUL)
5203 ++*arg;
5204 break;
5205
5206 /*
5207 * nested expression: (expression).
5208 */
5209 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005210 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005211 if (**arg == ')')
5212 ++*arg;
5213 else if (ret == OK)
5214 {
5215 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005216 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005217 ret = FAIL;
5218 }
5219 break;
5220
Bram Moolenaar8c711452005-01-14 21:53:12 +00005221 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 break;
5223 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005224
5225 if (ret == NOTDONE)
5226 {
5227 /*
5228 * Must be a variable or function name.
5229 * Can also be a curly-braces kind of name: {expr}.
5230 */
5231 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005232 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005233 if (alias != NULL)
5234 s = alias;
5235
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005236 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005237 ret = FAIL;
5238 else
5239 {
5240 if (**arg == '(') /* recursive! */
5241 {
5242 /* If "s" is the name of a variable of type VAR_FUNC
5243 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005244 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005245
5246 /* Invoke the function. */
5247 ret = get_func_tv(s, len, rettv, arg,
5248 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005249 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005250
5251 /* If evaluate is FALSE rettv->v_type was not set in
5252 * get_func_tv, but it's needed in handle_subscript() to parse
5253 * what follows. So set it here. */
5254 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5255 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005256 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005257 rettv->v_type = VAR_FUNC;
5258 }
5259
Bram Moolenaar8c711452005-01-14 21:53:12 +00005260 /* Stop the expression evaluation when immediately
5261 * aborting on error, or when an interrupt occurred or
5262 * an exception was thrown but not caught. */
5263 if (aborting())
5264 {
5265 if (ret == OK)
5266 clear_tv(rettv);
5267 ret = FAIL;
5268 }
5269 }
5270 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005271 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005272 else
5273 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005275 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 }
5277
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278 *arg = skipwhite(*arg);
5279
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005280 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5281 * expr(expr). */
5282 if (ret == OK)
5283 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005284
5285 /*
5286 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5287 */
5288 if (ret == OK && evaluate && end_leader > start_leader)
5289 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005290 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005291 int val = 0;
5292#ifdef FEAT_FLOAT
5293 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005295 if (rettv->v_type == VAR_FLOAT)
5296 f = rettv->vval.v_float;
5297 else
5298#endif
5299 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005302 clear_tv(rettv);
5303 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005304 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005305 else
5306 {
5307 while (end_leader > start_leader)
5308 {
5309 --end_leader;
5310 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 {
5312#ifdef FEAT_FLOAT
5313 if (rettv->v_type == VAR_FLOAT)
5314 f = !f;
5315 else
5316#endif
5317 val = !val;
5318 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005320 {
5321#ifdef FEAT_FLOAT
5322 if (rettv->v_type == VAR_FLOAT)
5323 f = -f;
5324 else
5325#endif
5326 val = -val;
5327 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005328 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005329#ifdef FEAT_FLOAT
5330 if (rettv->v_type == VAR_FLOAT)
5331 {
5332 clear_tv(rettv);
5333 rettv->vval.v_float = f;
5334 }
5335 else
5336#endif
5337 {
5338 clear_tv(rettv);
5339 rettv->v_type = VAR_NUMBER;
5340 rettv->vval.v_number = val;
5341 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005342 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005343 }
5344
5345 return ret;
5346}
5347
5348/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005349 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5350 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005351 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5352 */
5353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005354eval_index(
5355 char_u **arg,
5356 typval_T *rettv,
5357 int evaluate,
5358 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359{
5360 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005361 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005362 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005363 long len = -1;
5364 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005366 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005369 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005370 case VAR_FUNC:
5371 if (verbose)
5372 EMSG(_("E695: Cannot index a Funcref"));
5373 return FAIL;
5374 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005375#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005376 if (verbose)
5377 EMSG(_(e_float_as_string));
5378 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005379#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005380 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005381 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005382 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005383 if (verbose)
5384 EMSG(_("E909: Cannot index a special variable"));
5385 return FAIL;
5386 case VAR_UNKNOWN:
5387 if (evaluate)
5388 return FAIL;
5389 /* FALLTHROUGH */
5390
5391 case VAR_STRING:
5392 case VAR_NUMBER:
5393 case VAR_LIST:
5394 case VAR_DICT:
5395 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005396 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005397
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005398 init_tv(&var1);
5399 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005401 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005402 /*
5403 * dict.name
5404 */
5405 key = *arg + 1;
5406 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5407 ;
5408 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005410 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 }
5412 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005413 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 /*
5415 * something[idx]
5416 *
5417 * Get the (first) variable from inside the [].
5418 */
5419 *arg = skipwhite(*arg + 1);
5420 if (**arg == ':')
5421 empty1 = TRUE;
5422 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5423 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005424 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5425 {
5426 /* not a number or string */
5427 clear_tv(&var1);
5428 return FAIL;
5429 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430
5431 /*
5432 * Get the second variable from inside the [:].
5433 */
5434 if (**arg == ':')
5435 {
5436 range = TRUE;
5437 *arg = skipwhite(*arg + 1);
5438 if (**arg == ']')
5439 empty2 = TRUE;
5440 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5441 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005442 if (!empty1)
5443 clear_tv(&var1);
5444 return FAIL;
5445 }
5446 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5447 {
5448 /* not a number or string */
5449 if (!empty1)
5450 clear_tv(&var1);
5451 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005452 return FAIL;
5453 }
5454 }
5455
5456 /* Check for the ']'. */
5457 if (**arg != ']')
5458 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005459 if (verbose)
5460 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005461 clear_tv(&var1);
5462 if (range)
5463 clear_tv(&var2);
5464 return FAIL;
5465 }
5466 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005467 }
5468
5469 if (evaluate)
5470 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005471 n1 = 0;
5472 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005473 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005474 n1 = get_tv_number(&var1);
5475 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005476 }
5477 if (range)
5478 {
5479 if (empty2)
5480 n2 = -1;
5481 else
5482 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 n2 = get_tv_number(&var2);
5484 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005485 }
5486 }
5487
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005489 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005490 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005491 case VAR_FUNC:
5492 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005493 case VAR_SPECIAL:
5494 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005495 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005496 break; /* not evaluating, skipping over subscript */
5497
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005498 case VAR_NUMBER:
5499 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005501 len = (long)STRLEN(s);
5502 if (range)
5503 {
5504 /* The resulting variable is a substring. If the indexes
5505 * are out of range the result is empty. */
5506 if (n1 < 0)
5507 {
5508 n1 = len + n1;
5509 if (n1 < 0)
5510 n1 = 0;
5511 }
5512 if (n2 < 0)
5513 n2 = len + n2;
5514 else if (n2 >= len)
5515 n2 = len;
5516 if (n1 >= len || n2 < 0 || n1 > n2)
5517 s = NULL;
5518 else
5519 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5520 }
5521 else
5522 {
5523 /* The resulting variable is a string of a single
5524 * character. If the index is too big or negative the
5525 * result is empty. */
5526 if (n1 >= len || n1 < 0)
5527 s = NULL;
5528 else
5529 s = vim_strnsave(s + n1, 1);
5530 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005531 clear_tv(rettv);
5532 rettv->v_type = VAR_STRING;
5533 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 break;
5535
5536 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005537 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005538 if (n1 < 0)
5539 n1 = len + n1;
5540 if (!empty1 && (n1 < 0 || n1 >= len))
5541 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005542 /* For a range we allow invalid values and return an empty
5543 * list. A list index out of range is an error. */
5544 if (!range)
5545 {
5546 if (verbose)
5547 EMSGN(_(e_listidx), n1);
5548 return FAIL;
5549 }
5550 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005551 }
5552 if (range)
5553 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005554 list_T *l;
5555 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556
5557 if (n2 < 0)
5558 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005559 else if (n2 >= len)
5560 n2 = len - 1;
5561 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005562 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 l = list_alloc();
5564 if (l == NULL)
5565 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005566 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005567 n1 <= n2; ++n1)
5568 {
5569 if (list_append_tv(l, &item->li_tv) == FAIL)
5570 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005571 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 return FAIL;
5573 }
5574 item = item->li_next;
5575 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005576 clear_tv(rettv);
5577 rettv->v_type = VAR_LIST;
5578 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005579 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005580 }
5581 else
5582 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005583 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 clear_tv(rettv);
5585 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005586 }
5587 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005588
5589 case VAR_DICT:
5590 if (range)
5591 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005592 if (verbose)
5593 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005594 if (len == -1)
5595 clear_tv(&var1);
5596 return FAIL;
5597 }
5598 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005599 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005600
5601 if (len == -1)
5602 {
5603 key = get_tv_string(&var1);
5604 if (*key == NUL)
5605 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005606 if (verbose)
5607 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005608 clear_tv(&var1);
5609 return FAIL;
5610 }
5611 }
5612
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005613 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005614
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005615 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005616 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005617 if (len == -1)
5618 clear_tv(&var1);
5619 if (item == NULL)
5620 return FAIL;
5621
5622 copy_tv(&item->di_tv, &var1);
5623 clear_tv(rettv);
5624 *rettv = var1;
5625 }
5626 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005627 }
5628 }
5629
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005630 return OK;
5631}
5632
5633/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005634 * Get an option value.
5635 * "arg" points to the '&' or '+' before the option name.
5636 * "arg" is advanced to character after the option name.
5637 * Return OK or FAIL.
5638 */
5639 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005640get_option_tv(
5641 char_u **arg,
5642 typval_T *rettv, /* when NULL, only check if option exists */
5643 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005644{
5645 char_u *option_end;
5646 long numval;
5647 char_u *stringval;
5648 int opt_type;
5649 int c;
5650 int working = (**arg == '+'); /* has("+option") */
5651 int ret = OK;
5652 int opt_flags;
5653
5654 /*
5655 * Isolate the option name and find its value.
5656 */
5657 option_end = find_option_end(arg, &opt_flags);
5658 if (option_end == NULL)
5659 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005660 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005661 EMSG2(_("E112: Option name missing: %s"), *arg);
5662 return FAIL;
5663 }
5664
5665 if (!evaluate)
5666 {
5667 *arg = option_end;
5668 return OK;
5669 }
5670
5671 c = *option_end;
5672 *option_end = NUL;
5673 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675
5676 if (opt_type == -3) /* invalid name */
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 EMSG2(_("E113: Unknown option: %s"), *arg);
5680 ret = FAIL;
5681 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005683 {
5684 if (opt_type == -2) /* hidden string option */
5685 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005686 rettv->v_type = VAR_STRING;
5687 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005688 }
5689 else if (opt_type == -1) /* hidden number option */
5690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005691 rettv->v_type = VAR_NUMBER;
5692 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693 }
5694 else if (opt_type == 1) /* number option */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 rettv->v_type = VAR_NUMBER;
5697 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 }
5699 else /* string option */
5700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 rettv->v_type = VAR_STRING;
5702 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 }
5705 else if (working && (opt_type == -2 || opt_type == -1))
5706 ret = FAIL;
5707
5708 *option_end = c; /* put back for error messages */
5709 *arg = option_end;
5710
5711 return ret;
5712}
5713
5714/*
5715 * Allocate a variable for a string constant.
5716 * Return OK or FAIL.
5717 */
5718 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005719get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005720{
5721 char_u *p;
5722 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005723 int extra = 0;
5724
5725 /*
5726 * Find the end of the string, skipping backslashed characters.
5727 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005728 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005729 {
5730 if (*p == '\\' && p[1] != NUL)
5731 {
5732 ++p;
5733 /* A "\<x>" form occupies at least 4 characters, and produces up
5734 * to 6 characters: reserve space for 2 extra */
5735 if (*p == '<')
5736 extra += 2;
5737 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 }
5739
5740 if (*p != '"')
5741 {
5742 EMSG2(_("E114: Missing quote: %s"), *arg);
5743 return FAIL;
5744 }
5745
5746 /* If only parsing, set *arg and return here */
5747 if (!evaluate)
5748 {
5749 *arg = p + 1;
5750 return OK;
5751 }
5752
5753 /*
5754 * Copy the string into allocated memory, handling backslashed
5755 * characters.
5756 */
5757 name = alloc((unsigned)(p - *arg + extra));
5758 if (name == NULL)
5759 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005760 rettv->v_type = VAR_STRING;
5761 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762
Bram Moolenaar8c711452005-01-14 21:53:12 +00005763 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005764 {
5765 if (*p == '\\')
5766 {
5767 switch (*++p)
5768 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005769 case 'b': *name++ = BS; ++p; break;
5770 case 'e': *name++ = ESC; ++p; break;
5771 case 'f': *name++ = FF; ++p; break;
5772 case 'n': *name++ = NL; ++p; break;
5773 case 'r': *name++ = CAR; ++p; break;
5774 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005775
5776 case 'X': /* hex: "\x1", "\x12" */
5777 case 'x':
5778 case 'u': /* Unicode: "\u0023" */
5779 case 'U':
5780 if (vim_isxdigit(p[1]))
5781 {
5782 int n, nr;
5783 int c = toupper(*p);
5784
5785 if (c == 'X')
5786 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005787 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005788 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005789 else
5790 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005791 nr = 0;
5792 while (--n >= 0 && vim_isxdigit(p[1]))
5793 {
5794 ++p;
5795 nr = (nr << 4) + hex2nr(*p);
5796 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005797 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005798#ifdef FEAT_MBYTE
5799 /* For "\u" store the number according to
5800 * 'encoding'. */
5801 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005802 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 else
5804#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005805 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005807 break;
5808
5809 /* octal: "\1", "\12", "\123" */
5810 case '0':
5811 case '1':
5812 case '2':
5813 case '3':
5814 case '4':
5815 case '5':
5816 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 case '7': *name = *p++ - '0';
5818 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 *name = (*name << 3) + *p++ - '0';
5821 if (*p >= '0' && *p <= '7')
5822 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826
5827 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005828 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005829 if (extra != 0)
5830 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005831 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005832 break;
5833 }
5834 /* FALLTHROUGH */
5835
Bram Moolenaar8c711452005-01-14 21:53:12 +00005836 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 break;
5838 }
5839 }
5840 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005844 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 *arg = p + 1;
5846
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 return OK;
5848}
5849
5850/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 * Return OK or FAIL.
5853 */
5854 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005855get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005856{
5857 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005858 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 int reduce = 0;
5860
5861 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 */
5864 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005868 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005869 break;
5870 ++reduce;
5871 ++p;
5872 }
5873 }
5874
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 return FAIL;
5879 }
5880
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 if (!evaluate)
5883 {
5884 *arg = p + 1;
5885 return OK;
5886 }
5887
5888 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 */
5891 str = alloc((unsigned)((p - *arg) - reduce));
5892 if (str == NULL)
5893 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005894 rettv->v_type = VAR_STRING;
5895 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 break;
5903 ++p;
5904 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 *arg = p + 1;
5909
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005910 return OK;
5911}
5912
5913/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005914 * Allocate a variable for a List and fill it from "*arg".
5915 * Return OK or FAIL.
5916 */
5917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005918get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919{
Bram Moolenaar33570922005-01-25 22:26:29 +00005920 list_T *l = NULL;
5921 typval_T tv;
5922 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005923
5924 if (evaluate)
5925 {
5926 l = list_alloc();
5927 if (l == NULL)
5928 return FAIL;
5929 }
5930
5931 *arg = skipwhite(*arg + 1);
5932 while (**arg != ']' && **arg != NUL)
5933 {
5934 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5935 goto failret;
5936 if (evaluate)
5937 {
5938 item = listitem_alloc();
5939 if (item != NULL)
5940 {
5941 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005942 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 list_append(l, item);
5944 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005945 else
5946 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005947 }
5948
5949 if (**arg == ']')
5950 break;
5951 if (**arg != ',')
5952 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005953 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005954 goto failret;
5955 }
5956 *arg = skipwhite(*arg + 1);
5957 }
5958
5959 if (**arg != ']')
5960 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005961 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962failret:
5963 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005964 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 return FAIL;
5966 }
5967
5968 *arg = skipwhite(*arg + 1);
5969 if (evaluate)
5970 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005971 rettv->v_type = VAR_LIST;
5972 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005973 ++l->lv_refcount;
5974 }
5975
5976 return OK;
5977}
5978
5979/*
5980 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005981 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005982 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005983 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005984list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005985{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005986 list_T *l;
5987
5988 l = (list_T *)alloc_clear(sizeof(list_T));
5989 if (l != NULL)
5990 {
5991 /* Prepend the list to the list of lists for garbage collection. */
5992 if (first_list != NULL)
5993 first_list->lv_used_prev = l;
5994 l->lv_used_prev = NULL;
5995 l->lv_used_next = first_list;
5996 first_list = l;
5997 }
5998 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005999}
6000
6001/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006002 * Allocate an empty list for a return value.
6003 * Returns OK or FAIL.
6004 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006005 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006006rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006007{
6008 list_T *l = list_alloc();
6009
6010 if (l == NULL)
6011 return FAIL;
6012
6013 rettv->vval.v_list = l;
6014 rettv->v_type = VAR_LIST;
6015 ++l->lv_refcount;
6016 return OK;
6017}
6018
6019/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020 * Unreference a list: decrement the reference count and free it when it
6021 * becomes zero.
6022 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006023 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006025{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006026 if (l != NULL && --l->lv_refcount <= 0)
6027 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028}
6029
6030/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006031 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006032 * Ignores the reference count.
6033 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006034 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006035list_free(
6036 list_T *l,
6037 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038{
Bram Moolenaar33570922005-01-25 22:26:29 +00006039 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006041 /* Remove the list from the list of lists for garbage collection. */
6042 if (l->lv_used_prev == NULL)
6043 first_list = l->lv_used_next;
6044 else
6045 l->lv_used_prev->lv_used_next = l->lv_used_next;
6046 if (l->lv_used_next != NULL)
6047 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6048
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006051 /* Remove the item before deleting it. */
6052 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006053 if (recurse || (item->li_tv.v_type != VAR_LIST
6054 && item->li_tv.v_type != VAR_DICT))
6055 clear_tv(&item->li_tv);
6056 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006057 }
6058 vim_free(l);
6059}
6060
6061/*
6062 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006063 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006064 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006065 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006066listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067{
Bram Moolenaar33570922005-01-25 22:26:29 +00006068 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069}
6070
6071/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006072 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006074 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006075listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006077 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006078 vim_free(item);
6079}
6080
6081/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082 * Remove a list item from a List and free it. Also clears the value.
6083 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006084 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006085listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006086{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006087 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006088 listitem_free(item);
6089}
6090
6091/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092 * Get the number of items in a list.
6093 */
6094 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006095list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006097 if (l == NULL)
6098 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006099 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100}
6101
6102/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006103 * Return TRUE when two lists have exactly the same values.
6104 */
6105 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006106list_equal(
6107 list_T *l1,
6108 list_T *l2,
6109 int ic, /* ignore case for strings */
6110 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111{
Bram Moolenaar33570922005-01-25 22:26:29 +00006112 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006113
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006114 if (l1 == NULL || l2 == NULL)
6115 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006116 if (l1 == l2)
6117 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006118 if (list_len(l1) != list_len(l2))
6119 return FALSE;
6120
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 for (item1 = l1->lv_first, item2 = l2->lv_first;
6122 item1 != NULL && item2 != NULL;
6123 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006124 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 return FALSE;
6126 return item1 == NULL && item2 == NULL;
6127}
6128
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006129/*
6130 * Return the dictitem that an entry in a hashtable points to.
6131 */
6132 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006133dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006134{
6135 return HI2DI(hi);
6136}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006137
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006138/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006139 * Return TRUE when two dictionaries have exactly the same key/values.
6140 */
6141 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006142dict_equal(
6143 dict_T *d1,
6144 dict_T *d2,
6145 int ic, /* ignore case for strings */
6146 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147{
Bram Moolenaar33570922005-01-25 22:26:29 +00006148 hashitem_T *hi;
6149 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006150 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006151
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006152 if (d1 == NULL || d2 == NULL)
6153 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006154 if (d1 == d2)
6155 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006156 if (dict_len(d1) != dict_len(d2))
6157 return FALSE;
6158
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006159 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006160 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006162 if (!HASHITEM_EMPTY(hi))
6163 {
6164 item2 = dict_find(d2, hi->hi_key, -1);
6165 if (item2 == NULL)
6166 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006167 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 return FALSE;
6169 --todo;
6170 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171 }
6172 return TRUE;
6173}
6174
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006175static int tv_equal_recurse_limit;
6176
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006177/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006178 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006179 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006180 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006181 */
6182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006183tv_equal(
6184 typval_T *tv1,
6185 typval_T *tv2,
6186 int ic, /* ignore case */
6187 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006188{
6189 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006190 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006192 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006194 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006197 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006198 * recursiveness to a limit. We guess they are equal then.
6199 * A fixed limit has the problem of still taking an awful long time.
6200 * Reduce the limit every time running into it. That should work fine for
6201 * deeply linked structures that are not recursively linked and catch
6202 * recursiveness quickly. */
6203 if (!recursive)
6204 tv_equal_recurse_limit = 1000;
6205 if (recursive_cnt >= tv_equal_recurse_limit)
6206 {
6207 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006208 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006210
6211 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006212 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214 ++recursive_cnt;
6215 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6216 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006217 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006218
6219 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006220 ++recursive_cnt;
6221 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6222 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006223 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006224
6225 case VAR_FUNC:
6226 return (tv1->vval.v_string != NULL
6227 && tv2->vval.v_string != NULL
6228 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6229
6230 case VAR_NUMBER:
6231 return tv1->vval.v_number == tv2->vval.v_number;
6232
6233 case VAR_STRING:
6234 s1 = get_tv_string_buf(tv1, buf1);
6235 s2 = get_tv_string_buf(tv2, buf2);
6236 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006237
6238 case VAR_SPECIAL:
6239 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006240
6241 case VAR_FLOAT:
6242#ifdef FEAT_FLOAT
6243 return tv1->vval.v_float == tv2->vval.v_float;
6244#endif
6245 case VAR_JOB:
6246#ifdef FEAT_JOB
6247 return tv1->vval.v_job == tv2->vval.v_job;
6248#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006249 case VAR_CHANNEL:
6250#ifdef FEAT_CHANNEL
6251 return tv1->vval.v_channel == tv2->vval.v_channel;
6252#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006253 case VAR_UNKNOWN:
6254 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006255 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006256
Bram Moolenaara03f2332016-02-06 18:09:59 +01006257 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6258 * does not equal anything, not even itself. */
6259 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006260}
6261
6262/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006263 * Locate item with index "n" in list "l" and return it.
6264 * A negative index is counted from the end; -1 is the last item.
6265 * Returns NULL when "n" is out of range.
6266 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006267 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006268list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269{
Bram Moolenaar33570922005-01-25 22:26:29 +00006270 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006271 long idx;
6272
6273 if (l == NULL)
6274 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006275
6276 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006278 n = l->lv_len + n;
6279
6280 /* Check for index out of range. */
6281 if (n < 0 || n >= l->lv_len)
6282 return NULL;
6283
6284 /* When there is a cached index may start search from there. */
6285 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006286 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006287 if (n < l->lv_idx / 2)
6288 {
6289 /* closest to the start of the list */
6290 item = l->lv_first;
6291 idx = 0;
6292 }
6293 else if (n > (l->lv_idx + l->lv_len) / 2)
6294 {
6295 /* closest to the end of the list */
6296 item = l->lv_last;
6297 idx = l->lv_len - 1;
6298 }
6299 else
6300 {
6301 /* closest to the cached index */
6302 item = l->lv_idx_item;
6303 idx = l->lv_idx;
6304 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006305 }
6306 else
6307 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006308 if (n < l->lv_len / 2)
6309 {
6310 /* closest to the start of the list */
6311 item = l->lv_first;
6312 idx = 0;
6313 }
6314 else
6315 {
6316 /* closest to the end of the list */
6317 item = l->lv_last;
6318 idx = l->lv_len - 1;
6319 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006320 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006321
6322 while (n > idx)
6323 {
6324 /* search forward */
6325 item = item->li_next;
6326 ++idx;
6327 }
6328 while (n < idx)
6329 {
6330 /* search backward */
6331 item = item->li_prev;
6332 --idx;
6333 }
6334
6335 /* cache the used index */
6336 l->lv_idx = idx;
6337 l->lv_idx_item = item;
6338
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006339 return item;
6340}
6341
6342/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006343 * Get list item "l[idx]" as a number.
6344 */
6345 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006346list_find_nr(
6347 list_T *l,
6348 long idx,
6349 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006350{
6351 listitem_T *li;
6352
6353 li = list_find(l, idx);
6354 if (li == NULL)
6355 {
6356 if (errorp != NULL)
6357 *errorp = TRUE;
6358 return -1L;
6359 }
6360 return get_tv_number_chk(&li->li_tv, errorp);
6361}
6362
6363/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006364 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6365 */
6366 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006367list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006368{
6369 listitem_T *li;
6370
6371 li = list_find(l, idx - 1);
6372 if (li == NULL)
6373 {
6374 EMSGN(_(e_listidx), idx);
6375 return NULL;
6376 }
6377 return get_tv_string(&li->li_tv);
6378}
6379
6380/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006381 * Locate "item" list "l" and return its index.
6382 * Returns -1 when "item" is not in the list.
6383 */
6384 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006385list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006386{
6387 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006388 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006389
6390 if (l == NULL)
6391 return -1;
6392 idx = 0;
6393 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6394 ++idx;
6395 if (li == NULL)
6396 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006397 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006398}
6399
6400/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006401 * Append item "item" to the end of list "l".
6402 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006403 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006404list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006405{
6406 if (l->lv_last == NULL)
6407 {
6408 /* empty list */
6409 l->lv_first = item;
6410 l->lv_last = item;
6411 item->li_prev = NULL;
6412 }
6413 else
6414 {
6415 l->lv_last->li_next = item;
6416 item->li_prev = l->lv_last;
6417 l->lv_last = item;
6418 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006419 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006420 item->li_next = NULL;
6421}
6422
6423/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006424 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006425 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006428list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006434 copy_tv(tv, &li->li_tv);
6435 list_append(l, li);
6436 return OK;
6437}
6438
6439/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006440 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006441 * Return FAIL when out of memory.
6442 */
6443 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006444list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006445{
6446 listitem_T *li = listitem_alloc();
6447
6448 if (li == NULL)
6449 return FAIL;
6450 li->li_tv.v_type = VAR_DICT;
6451 li->li_tv.v_lock = 0;
6452 li->li_tv.vval.v_dict = dict;
6453 list_append(list, li);
6454 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 return OK;
6456}
6457
6458/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006460 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006461 * Returns FAIL when out of memory.
6462 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006463 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006464list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006465{
6466 listitem_T *li = listitem_alloc();
6467
6468 if (li == NULL)
6469 return FAIL;
6470 list_append(l, li);
6471 li->li_tv.v_type = VAR_STRING;
6472 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006473 if (str == NULL)
6474 li->li_tv.vval.v_string = NULL;
6475 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006476 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006477 return FAIL;
6478 return OK;
6479}
6480
6481/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006482 * Append "n" to list "l".
6483 * Returns FAIL when out of memory.
6484 */
6485 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006487{
6488 listitem_T *li;
6489
6490 li = listitem_alloc();
6491 if (li == NULL)
6492 return FAIL;
6493 li->li_tv.v_type = VAR_NUMBER;
6494 li->li_tv.v_lock = 0;
6495 li->li_tv.vval.v_number = n;
6496 list_append(l, li);
6497 return OK;
6498}
6499
6500/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006501 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006502 * If "item" is NULL append at the end.
6503 * Return FAIL when out of memory.
6504 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006505 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006506list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507{
Bram Moolenaar33570922005-01-25 22:26:29 +00006508 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006509
6510 if (ni == NULL)
6511 return FAIL;
6512 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006513 list_insert(l, ni, item);
6514 return OK;
6515}
6516
6517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006519{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006520 if (item == NULL)
6521 /* Append new item at end of list. */
6522 list_append(l, ni);
6523 else
6524 {
6525 /* Insert new item before existing item. */
6526 ni->li_prev = item->li_prev;
6527 ni->li_next = item;
6528 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006530 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006531 ++l->lv_idx;
6532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006536 l->lv_idx_item = NULL;
6537 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006539 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006540 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541}
6542
6543/*
6544 * Extend "l1" with "l2".
6545 * If "bef" is NULL append at the end, otherwise insert before this item.
6546 * Returns FAIL when out of memory.
6547 */
6548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006549list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006550{
Bram Moolenaar33570922005-01-25 22:26:29 +00006551 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006552 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006553
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006554 /* We also quit the loop when we have inserted the original item count of
6555 * the list, avoid a hang when we extend a list with itself. */
6556 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6558 return FAIL;
6559 return OK;
6560}
6561
6562/*
6563 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6564 * Return FAIL when out of memory.
6565 */
6566 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006567list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568{
Bram Moolenaar33570922005-01-25 22:26:29 +00006569 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006571 if (l1 == NULL || l2 == NULL)
6572 return FAIL;
6573
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006575 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576 if (l == NULL)
6577 return FAIL;
6578 tv->v_type = VAR_LIST;
6579 tv->vval.v_list = l;
6580
6581 /* append all items from the second list */
6582 return list_extend(l, l2, NULL);
6583}
6584
6585/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006586 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006588 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006589 * Returns NULL when out of memory.
6590 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593{
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 list_T *copy;
6595 listitem_T *item;
6596 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006597
6598 if (orig == NULL)
6599 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006600
6601 copy = list_alloc();
6602 if (copy != NULL)
6603 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006604 if (copyID != 0)
6605 {
6606 /* Do this before adding the items, because one of the items may
6607 * refer back to this list. */
6608 orig->lv_copyID = copyID;
6609 orig->lv_copylist = copy;
6610 }
6611 for (item = orig->lv_first; item != NULL && !got_int;
6612 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 {
6614 ni = listitem_alloc();
6615 if (ni == NULL)
6616 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006617 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006618 {
6619 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6620 {
6621 vim_free(ni);
6622 break;
6623 }
6624 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006626 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006627 list_append(copy, ni);
6628 }
6629 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 if (item != NULL)
6631 {
6632 list_unref(copy);
6633 copy = NULL;
6634 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 }
6636
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006637 return copy;
6638}
6639
6640/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006641 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006642 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006643 * This used to be called list_remove, but that conflicts with a Sun header
6644 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006646 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006647vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006648{
Bram Moolenaar33570922005-01-25 22:26:29 +00006649 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006651 /* notify watchers */
6652 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006654 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006655 list_fix_watch(l, ip);
6656 if (ip == item2)
6657 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006658 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006659
6660 if (item2->li_next == NULL)
6661 l->lv_last = item->li_prev;
6662 else
6663 item2->li_next->li_prev = item->li_prev;
6664 if (item->li_prev == NULL)
6665 l->lv_first = item2->li_next;
6666 else
6667 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006668 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669}
6670
6671/*
6672 * Return an allocated string with the string representation of a list.
6673 * May return NULL.
6674 */
6675 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006676list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677{
6678 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679
6680 if (tv->vval.v_list == NULL)
6681 return NULL;
6682 ga_init2(&ga, (int)sizeof(char), 80);
6683 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006684 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006685 {
6686 vim_free(ga.ga_data);
6687 return NULL;
6688 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006689 ga_append(&ga, ']');
6690 ga_append(&ga, NUL);
6691 return (char_u *)ga.ga_data;
6692}
6693
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006694typedef struct join_S {
6695 char_u *s;
6696 char_u *tofree;
6697} join_T;
6698
6699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006700list_join_inner(
6701 garray_T *gap, /* to store the result in */
6702 list_T *l,
6703 char_u *sep,
6704 int echo_style,
6705 int copyID,
6706 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006707{
6708 int i;
6709 join_T *p;
6710 int len;
6711 int sumlen = 0;
6712 int first = TRUE;
6713 char_u *tofree;
6714 char_u numbuf[NUMBUFLEN];
6715 listitem_T *item;
6716 char_u *s;
6717
6718 /* Stringify each item in the list. */
6719 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6720 {
6721 if (echo_style)
6722 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6723 else
6724 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6725 if (s == NULL)
6726 return FAIL;
6727
6728 len = (int)STRLEN(s);
6729 sumlen += len;
6730
Bram Moolenaarcde88542015-08-11 19:14:00 +02006731 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6733 if (tofree != NULL || s != numbuf)
6734 {
6735 p->s = s;
6736 p->tofree = tofree;
6737 }
6738 else
6739 {
6740 p->s = vim_strnsave(s, len);
6741 p->tofree = p->s;
6742 }
6743
6744 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006745 if (did_echo_string_emsg) /* recursion error, bail out */
6746 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006747 }
6748
6749 /* Allocate result buffer with its total size, avoid re-allocation and
6750 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6751 if (join_gap->ga_len >= 2)
6752 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6753 if (ga_grow(gap, sumlen + 2) == FAIL)
6754 return FAIL;
6755
6756 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6757 {
6758 if (first)
6759 first = FALSE;
6760 else
6761 ga_concat(gap, sep);
6762 p = ((join_T *)join_gap->ga_data) + i;
6763
6764 if (p->s != NULL)
6765 ga_concat(gap, p->s);
6766 line_breakcheck();
6767 }
6768
6769 return OK;
6770}
6771
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006772/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006773 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006774 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006775 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006776 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006777 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006778list_join(
6779 garray_T *gap,
6780 list_T *l,
6781 char_u *sep,
6782 int echo_style,
6783 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006784{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006785 garray_T join_ga;
6786 int retval;
6787 join_T *p;
6788 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006789
Bram Moolenaard39a7512015-04-16 22:51:22 +02006790 if (l->lv_len < 1)
6791 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006792 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6793 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6794
6795 /* Dispose each item in join_ga. */
6796 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006797 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 p = (join_T *)join_ga.ga_data;
6799 for (i = 0; i < join_ga.ga_len; ++i)
6800 {
6801 vim_free(p->tofree);
6802 ++p;
6803 }
6804 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006806
6807 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808}
6809
6810/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006811 * Return the next (unique) copy ID.
6812 * Used for serializing nested structures.
6813 */
6814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006815get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006816{
6817 current_copyID += COPYID_INC;
6818 return current_copyID;
6819}
6820
6821/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006822 * Garbage collection for lists and dictionaries.
6823 *
6824 * We use reference counts to be able to free most items right away when they
6825 * are no longer used. But for composite items it's possible that it becomes
6826 * unused while the reference count is > 0: When there is a recursive
6827 * reference. Example:
6828 * :let l = [1, 2, 3]
6829 * :let d = {9: l}
6830 * :let l[1] = d
6831 *
6832 * Since this is quite unusual we handle this with garbage collection: every
6833 * once in a while find out which lists and dicts are not referenced from any
6834 * variable.
6835 *
6836 * Here is a good reference text about garbage collection (refers to Python
6837 * but it applies to all reference-counting mechanisms):
6838 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006839 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006840
6841/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006842 * Do garbage collection for lists and dicts.
6843 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006844 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006846garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006847{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006848 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006849 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006850 buf_T *buf;
6851 win_T *wp;
6852 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006853 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006854 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006855 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006856#ifdef FEAT_WINDOWS
6857 tabpage_T *tp;
6858#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006859
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006860 /* Only do this once. */
6861 want_garbage_collect = FALSE;
6862 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006863 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006864
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006865 /* We advance by two because we add one for items referenced through
6866 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006867 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006868
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006869 /*
6870 * 1. Go through all accessible variables and mark all lists and dicts
6871 * with copyID.
6872 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873
6874 /* Don't free variables in the previous_funccal list unless they are only
6875 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006876 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6878 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006879 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6880 NULL);
6881 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6882 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 }
6884
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885 /* script-local variables */
6886 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006887 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006888
6889 /* buffer-local variables */
6890 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006891 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6892 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006893
6894 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006895 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006896 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6897 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006898#ifdef FEAT_AUTOCMD
6899 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006900 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6901 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006902#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006903
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006904#ifdef FEAT_WINDOWS
6905 /* tabpage-local variables */
6906 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006907 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6908 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006909#endif
6910
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
6914 /* function-local variables */
6915 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6916 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6918 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919 }
6920
Bram Moolenaard812df62008-11-09 12:46:09 +00006921 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006923
Bram Moolenaar1dced572012-04-05 16:54:08 +02006924#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006926#endif
6927
Bram Moolenaardb913952012-06-29 12:54:53 +02006928#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006930#endif
6931
6932#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#endif
6935
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006936#ifdef FEAT_CHANNEL
6937 abort = abort || set_ref_in_channel(copyID);
6938#endif
6939
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006941 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 /*
6943 * 2. Free lists and dictionaries that are not referenced.
6944 */
6945 did_free = free_unref_items(copyID);
6946
6947 /*
6948 * 3. Check if any funccal can be freed now.
6949 */
6950 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006951 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006952 if (can_free_funccal(*pfc, copyID))
6953 {
6954 fc = *pfc;
6955 *pfc = fc->caller;
6956 free_funccal(fc, TRUE);
6957 did_free = TRUE;
6958 did_free_funccal = TRUE;
6959 }
6960 else
6961 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006962 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006963 if (did_free_funccal)
6964 /* When a funccal was freed some more items might be garbage
6965 * collected, so run again. */
6966 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 else if (p_verbose > 0)
6969 {
6970 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6971 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006972
6973 return did_free;
6974}
6975
6976/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006977 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978 */
6979 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006980free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006981{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006982 dict_T *dd, *dd_next;
6983 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 int did_free = FALSE;
6985
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006988 */
6989 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006990 {
6991 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006993 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006994 /* Free the Dictionary and ordinary items it contains, but don't
6995 * recurse into Lists and Dictionaries, they will be in the list
6996 * of dicts or list of lists. */
6997 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006998 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007000 dd = dd_next;
7001 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007002
7003 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 * Go through the list of lists and free items without the copyID.
7005 * But don't free a list that has a watcher (used in a for loop), these
7006 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007007 */
7008 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007009 {
7010 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007011 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7012 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007014 /* Free the List and ordinary items it contains, but don't recurse
7015 * into Lists and Dictionaries, they will be in the list of dicts
7016 * or list of lists. */
7017 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007020 ll = ll_next;
7021 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007022
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 return did_free;
7024}
7025
7026/*
7027 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007028 * "list_stack" is used to add lists to be marked. Can be NULL.
7029 *
7030 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007032 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007033set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034{
7035 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 hashtab_T *cur_ht;
7039 ht_stack_T *ht_stack = NULL;
7040 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 cur_ht = ht;
7043 for (;;)
7044 {
7045 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007046 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007047 /* Mark each item in the hashtab. If the item contains a hashtab
7048 * it is added to ht_stack, if it contains a list it is added to
7049 * list_stack. */
7050 todo = (int)cur_ht->ht_used;
7051 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7052 if (!HASHITEM_EMPTY(hi))
7053 {
7054 --todo;
7055 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7056 &ht_stack, list_stack);
7057 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007058 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007059
7060 if (ht_stack == NULL)
7061 break;
7062
7063 /* take an item from the stack */
7064 cur_ht = ht_stack->ht;
7065 tempitem = ht_stack;
7066 ht_stack = ht_stack->prev;
7067 free(tempitem);
7068 }
7069
7070 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071}
7072
7073/*
7074 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007075 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7076 *
7077 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007080set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007081{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007082 listitem_T *li;
7083 int abort = FALSE;
7084 list_T *cur_l;
7085 list_stack_T *list_stack = NULL;
7086 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 cur_l = l;
7089 for (;;)
7090 {
7091 if (!abort)
7092 /* Mark each item in the list. If the item contains a hashtab
7093 * it is added to ht_stack, if it contains a list it is added to
7094 * list_stack. */
7095 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7096 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7097 ht_stack, &list_stack);
7098 if (list_stack == NULL)
7099 break;
7100
7101 /* take an item from the stack */
7102 cur_l = list_stack->list;
7103 tempitem = list_stack;
7104 list_stack = list_stack->prev;
7105 free(tempitem);
7106 }
7107
7108 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109}
7110
7111/*
7112 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 * "list_stack" is used to add lists to be marked. Can be NULL.
7114 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7115 *
7116 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007117 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007118 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007119set_ref_in_item(
7120 typval_T *tv,
7121 int copyID,
7122 ht_stack_T **ht_stack,
7123 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007124{
7125 dict_T *dd;
7126 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007127 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128
Bram Moolenaara03f2332016-02-06 18:09:59 +01007129 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007130 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007131 dd = tv->vval.v_dict;
7132 if (dd != NULL && dd->dv_copyID != copyID)
7133 {
7134 /* Didn't see this dict yet. */
7135 dd->dv_copyID = copyID;
7136 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007137 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007138 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7139 }
7140 else
7141 {
7142 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7143 if (newitem == NULL)
7144 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 else
7146 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007147 newitem->ht = &dd->dv_hashtab;
7148 newitem->prev = *ht_stack;
7149 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007151 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007152 }
7153 }
7154 else if (tv->v_type == VAR_LIST)
7155 {
7156 ll = tv->vval.v_list;
7157 if (ll != NULL && ll->lv_copyID != copyID)
7158 {
7159 /* Didn't see this list yet. */
7160 ll->lv_copyID = copyID;
7161 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 abort = set_ref_in_list(ll, copyID, ht_stack);
7164 }
7165 else
7166 {
7167 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007168 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 if (newitem == NULL)
7170 abort = TRUE;
7171 else
7172 {
7173 newitem->list = ll;
7174 newitem->prev = *list_stack;
7175 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007176 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007177 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007178 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007181}
7182
7183/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007184 * Allocate an empty header for a dictionary.
7185 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007186 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007187dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007188{
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190
Bram Moolenaar33570922005-01-25 22:26:29 +00007191 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007192 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007193 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007194 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007195 if (first_dict != NULL)
7196 first_dict->dv_used_prev = d;
7197 d->dv_used_next = first_dict;
7198 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007199 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007200
Bram Moolenaar33570922005-01-25 22:26:29 +00007201 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007202 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007203 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007205 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007206 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007207 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007208}
7209
7210/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007211 * Allocate an empty dict for a return value.
7212 * Returns OK or FAIL.
7213 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007214 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007215rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007216{
7217 dict_T *d = dict_alloc();
7218
7219 if (d == NULL)
7220 return FAIL;
7221
7222 rettv->vval.v_dict = d;
7223 rettv->v_type = VAR_DICT;
7224 ++d->dv_refcount;
7225 return OK;
7226}
7227
7228
7229/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230 * Unreference a Dictionary: decrement the reference count and free it when it
7231 * becomes zero.
7232 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007233 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007234dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007235{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007236 if (d != NULL && --d->dv_refcount <= 0)
7237 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007238}
7239
7240/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007241 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007242 * Ignores the reference count.
7243 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007244 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007245dict_free(
7246 dict_T *d,
7247 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007248{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007250 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007251 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007253 /* Remove the dict from the list of dicts for garbage collection. */
7254 if (d->dv_used_prev == NULL)
7255 first_dict = d->dv_used_next;
7256 else
7257 d->dv_used_prev->dv_used_next = d->dv_used_next;
7258 if (d->dv_used_next != NULL)
7259 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7260
7261 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007262 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007263 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007266 if (!HASHITEM_EMPTY(hi))
7267 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007268 /* Remove the item before deleting it, just in case there is
7269 * something recursive causing trouble. */
7270 di = HI2DI(hi);
7271 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007272 if (recurse || (di->di_tv.v_type != VAR_LIST
7273 && di->di_tv.v_type != VAR_DICT))
7274 clear_tv(&di->di_tv);
7275 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007276 --todo;
7277 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007279 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007280 vim_free(d);
7281}
7282
7283/*
7284 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007285 * The "key" is copied to the new item.
7286 * Note that the value of the item "di_tv" still needs to be initialized!
7287 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007289 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291{
Bram Moolenaar33570922005-01-25 22:26:29 +00007292 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007294 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007296 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007297 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007298 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007299 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007300 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007301}
7302
7303/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007304 * Make a copy of a Dictionary item.
7305 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007307dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308{
Bram Moolenaar33570922005-01-25 22:26:29 +00007309 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007310
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007311 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7312 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007313 if (di != NULL)
7314 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007316 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007317 copy_tv(&org->di_tv, &di->di_tv);
7318 }
7319 return di;
7320}
7321
7322/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007323 * Remove item "item" from Dictionary "dict" and free it.
7324 */
7325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007326dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327{
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329
Bram Moolenaar33570922005-01-25 22:26:29 +00007330 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007331 if (HASHITEM_EMPTY(hi))
7332 EMSG2(_(e_intern2), "dictitem_remove()");
7333 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007334 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007335 dictitem_free(item);
7336}
7337
7338/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339 * Free a dict item. Also clears the value.
7340 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007342dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007343{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007344 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007345 if (item->di_flags & DI_FLAGS_ALLOC)
7346 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347}
7348
7349/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007350 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7351 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007352 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007353 * Returns NULL when out of memory.
7354 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007355 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007356dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007357{
Bram Moolenaar33570922005-01-25 22:26:29 +00007358 dict_T *copy;
7359 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007362
7363 if (orig == NULL)
7364 return NULL;
7365
7366 copy = dict_alloc();
7367 if (copy != NULL)
7368 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007369 if (copyID != 0)
7370 {
7371 orig->dv_copyID = copyID;
7372 orig->dv_copydict = copy;
7373 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007374 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007375 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007378 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 --todo;
7380
7381 di = dictitem_alloc(hi->hi_key);
7382 if (di == NULL)
7383 break;
7384 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007385 {
7386 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7387 copyID) == FAIL)
7388 {
7389 vim_free(di);
7390 break;
7391 }
7392 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007393 else
7394 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7395 if (dict_add(copy, di) == FAIL)
7396 {
7397 dictitem_free(di);
7398 break;
7399 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402
Bram Moolenaare9a41262005-01-15 22:18:47 +00007403 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007404 if (todo > 0)
7405 {
7406 dict_unref(copy);
7407 copy = NULL;
7408 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007409 }
7410
7411 return copy;
7412}
7413
7414/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007416 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007417 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007418 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007419dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420{
Bram Moolenaar33570922005-01-25 22:26:29 +00007421 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422}
7423
Bram Moolenaar8c711452005-01-14 21:53:12 +00007424/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007425 * Add a number or string entry to dictionary "d".
7426 * When "str" is NULL use number "nr", otherwise use "str".
7427 * Returns FAIL when out of memory and when key already exists.
7428 */
7429 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007430dict_add_nr_str(
7431 dict_T *d,
7432 char *key,
7433 long nr,
7434 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007435{
7436 dictitem_T *item;
7437
7438 item = dictitem_alloc((char_u *)key);
7439 if (item == NULL)
7440 return FAIL;
7441 item->di_tv.v_lock = 0;
7442 if (str == NULL)
7443 {
7444 item->di_tv.v_type = VAR_NUMBER;
7445 item->di_tv.vval.v_number = nr;
7446 }
7447 else
7448 {
7449 item->di_tv.v_type = VAR_STRING;
7450 item->di_tv.vval.v_string = vim_strsave(str);
7451 }
7452 if (dict_add(d, item) == FAIL)
7453 {
7454 dictitem_free(item);
7455 return FAIL;
7456 }
7457 return OK;
7458}
7459
7460/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007461 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007462 * Returns FAIL when out of memory and when key already exists.
7463 */
7464 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007465dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007466{
7467 dictitem_T *item;
7468
7469 item = dictitem_alloc((char_u *)key);
7470 if (item == NULL)
7471 return FAIL;
7472 item->di_tv.v_lock = 0;
7473 item->di_tv.v_type = VAR_LIST;
7474 item->di_tv.vval.v_list = list;
7475 if (dict_add(d, item) == FAIL)
7476 {
7477 dictitem_free(item);
7478 return FAIL;
7479 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007480 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007481 return OK;
7482}
7483
7484/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007485 * Get the number of items in a Dictionary.
7486 */
7487 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007489{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007490 if (d == NULL)
7491 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007492 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007493}
7494
7495/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007496 * Find item "key[len]" in Dictionary "d".
7497 * If "len" is negative use strlen(key).
7498 * Returns NULL when not found.
7499 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007500 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007501dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007502{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007503#define AKEYLEN 200
7504 char_u buf[AKEYLEN];
7505 char_u *akey;
7506 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007507 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007508
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007509 if (len < 0)
7510 akey = key;
7511 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007512 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007513 tofree = akey = vim_strnsave(key, len);
7514 if (akey == NULL)
7515 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007516 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007517 else
7518 {
7519 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007520 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007521 akey = buf;
7522 }
7523
Bram Moolenaar33570922005-01-25 22:26:29 +00007524 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525 vim_free(tofree);
7526 if (HASHITEM_EMPTY(hi))
7527 return NULL;
7528 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007529}
7530
7531/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007532 * Get a string item from a dictionary.
7533 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007534 * Returns NULL if the entry doesn't exist or out of memory.
7535 */
7536 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007537get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007538{
7539 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007540 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007541
7542 di = dict_find(d, key, -1);
7543 if (di == NULL)
7544 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007545 s = get_tv_string(&di->di_tv);
7546 if (save && s != NULL)
7547 s = vim_strsave(s);
7548 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007549}
7550
7551/*
7552 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007553 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007554 */
7555 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007556get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007557{
7558 dictitem_T *di;
7559
7560 di = dict_find(d, key, -1);
7561 if (di == NULL)
7562 return 0;
7563 return get_tv_number(&di->di_tv);
7564}
7565
7566/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007567 * Return an allocated string with the string representation of a Dictionary.
7568 * May return NULL.
7569 */
7570 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007571dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007572{
7573 garray_T ga;
7574 int first = TRUE;
7575 char_u *tofree;
7576 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007578 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007579 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583 return NULL;
7584 ga_init2(&ga, (int)sizeof(char), 80);
7585 ga_append(&ga, '{');
7586
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007587 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007588 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007591 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 --todo;
7593
7594 if (first)
7595 first = FALSE;
7596 else
7597 ga_concat(&ga, (char_u *)", ");
7598
7599 tofree = string_quote(hi->hi_key, FALSE);
7600 if (tofree != NULL)
7601 {
7602 ga_concat(&ga, tofree);
7603 vim_free(tofree);
7604 }
7605 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007606 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007607 if (s != NULL)
7608 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007609 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007610 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007611 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007612 line_breakcheck();
7613
Bram Moolenaar8c711452005-01-14 21:53:12 +00007614 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007616 if (todo > 0)
7617 {
7618 vim_free(ga.ga_data);
7619 return NULL;
7620 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007621
7622 ga_append(&ga, '}');
7623 ga_append(&ga, NUL);
7624 return (char_u *)ga.ga_data;
7625}
7626
7627/*
7628 * Allocate a variable for a Dictionary and fill it from "*arg".
7629 * Return OK or FAIL. Returns NOTDONE for {expr}.
7630 */
7631 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007632get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007633{
Bram Moolenaar33570922005-01-25 22:26:29 +00007634 dict_T *d = NULL;
7635 typval_T tvkey;
7636 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007637 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007638 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641
7642 /*
7643 * First check if it's not a curly-braces thing: {expr}.
7644 * Must do this without evaluating, otherwise a function may be called
7645 * twice. Unfortunately this means we need to call eval1() twice for the
7646 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007649 if (*start != '}')
7650 {
7651 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7652 return FAIL;
7653 if (*start == '}')
7654 return NOTDONE;
7655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
7657 if (evaluate)
7658 {
7659 d = dict_alloc();
7660 if (d == NULL)
7661 return FAIL;
7662 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007663 tvkey.v_type = VAR_UNKNOWN;
7664 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007665
7666 *arg = skipwhite(*arg + 1);
7667 while (**arg != '}' && **arg != NUL)
7668 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007669 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 goto failret;
7671 if (**arg != ':')
7672 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007673 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007675 goto failret;
7676 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007677 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007679 key = get_tv_string_buf_chk(&tvkey, buf);
7680 if (key == NULL || *key == NUL)
7681 {
7682 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7683 if (key != NULL)
7684 EMSG(_(e_emptykey));
7685 clear_tv(&tvkey);
7686 goto failret;
7687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689
7690 *arg = skipwhite(*arg + 1);
7691 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7692 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007693 if (evaluate)
7694 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007695 goto failret;
7696 }
7697 if (evaluate)
7698 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007699 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 if (item != NULL)
7701 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007702 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007703 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704 clear_tv(&tv);
7705 goto failret;
7706 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007707 item = dictitem_alloc(key);
7708 clear_tv(&tvkey);
7709 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007712 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007713 if (dict_add(d, item) == FAIL)
7714 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 }
7716 }
7717
7718 if (**arg == '}')
7719 break;
7720 if (**arg != ',')
7721 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007722 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 goto failret;
7724 }
7725 *arg = skipwhite(*arg + 1);
7726 }
7727
7728 if (**arg != '}')
7729 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007730 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731failret:
7732 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007733 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734 return FAIL;
7735 }
7736
7737 *arg = skipwhite(*arg + 1);
7738 if (evaluate)
7739 {
7740 rettv->v_type = VAR_DICT;
7741 rettv->vval.v_dict = d;
7742 ++d->dv_refcount;
7743 }
7744
7745 return OK;
7746}
7747
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007748#if defined(FEAT_CHANNEL) || defined(PROTO)
7749/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007750 * Decrement the reference count on "channel" and maybe free it when it goes
7751 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007752 * Returns TRUE when the channel was freed.
7753 */
7754 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007755channel_unref(channel_T *channel)
7756{
7757 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007758 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007759 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007760 return TRUE;
7761 }
7762 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007763}
7764#endif
7765
Bram Moolenaar65edff82016-02-21 16:40:11 +01007766#if defined(FEAT_JOB) || defined(PROTO)
7767static job_T *first_job = NULL;
7768
Bram Moolenaar835dc632016-02-07 14:27:38 +01007769 static void
7770job_free(job_T *job)
7771{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007772# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007773 if (job->jv_channel != NULL)
7774 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007775 /* The link from the channel to the job doesn't count as a reference,
7776 * thus don't decrement the refcount of the job. The reference from
7777 * the job to the channel does count the refrence, decrement it and
7778 * NULL the reference. We don't set ch_job_killed, unreferencing the
7779 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007780 job->jv_channel->ch_job = NULL;
7781 channel_unref(job->jv_channel);
7782 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007783# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007784 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007785
7786 if (job->jv_next != NULL)
7787 job->jv_next->jv_prev = job->jv_prev;
7788 if (job->jv_prev == NULL)
7789 first_job = job->jv_next;
7790 else
7791 job->jv_prev->jv_next = job->jv_next;
7792
7793 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007794 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007795 vim_free(job);
7796}
7797
7798 static void
7799job_unref(job_T *job)
7800{
7801 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007802 {
7803 /* Do not free the job when it has not ended yet and there is a
7804 * "stoponexit" flag or an exit callback. */
7805 if (job->jv_status != JOB_STARTED
7806 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7807 job_free(job);
7808 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007809}
7810
7811/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007812 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007813 */
7814 static job_T *
7815job_alloc(void)
7816{
7817 job_T *job;
7818
7819 job = (job_T *)alloc_clear(sizeof(job_T));
7820 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007821 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007822 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007823 job->jv_stoponexit = vim_strsave((char_u *)"term");
7824
7825 if (first_job != NULL)
7826 {
7827 first_job->jv_prev = job;
7828 job->jv_next = first_job;
7829 }
7830 first_job = job;
7831 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007832 return job;
7833}
7834
Bram Moolenaar65edff82016-02-21 16:40:11 +01007835 static void
7836job_set_options(job_T *job, jobopt_T *opt)
7837{
7838 if (opt->jo_set & JO_STOPONEXIT)
7839 {
7840 vim_free(job->jv_stoponexit);
7841 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7842 job->jv_stoponexit = NULL;
7843 else
7844 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7845 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007846 if (opt->jo_set & JO_EXIT_CB)
7847 {
7848 vim_free(job->jv_exit_cb);
7849 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7850 job->jv_exit_cb = NULL;
7851 else
7852 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7853 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007854}
7855
7856/*
7857 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7858 */
7859 void
7860job_stop_on_exit()
7861{
7862 job_T *job;
7863
7864 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007865 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007866 mch_stop_job(job, job->jv_stoponexit);
7867}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007868#endif
7869
Bram Moolenaar17a13432016-01-24 14:22:10 +01007870 static char *
7871get_var_special_name(int nr)
7872{
7873 switch (nr)
7874 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007875 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007876 case VVAL_TRUE: return "v:true";
7877 case VVAL_NONE: return "v:none";
7878 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007879 }
7880 EMSG2(_(e_intern2), "get_var_special_name()");
7881 return "42";
7882}
7883
Bram Moolenaar8c711452005-01-14 21:53:12 +00007884/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007885 * Return a string with the string representation of a variable.
7886 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007887 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007888 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007889 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007890 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007891 */
7892 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007893echo_string(
7894 typval_T *tv,
7895 char_u **tofree,
7896 char_u *numbuf,
7897 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007898{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007899 static int recurse = 0;
7900 char_u *r = NULL;
7901
Bram Moolenaar33570922005-01-25 22:26:29 +00007902 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007903 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007904 if (!did_echo_string_emsg)
7905 {
7906 /* Only give this message once for a recursive call to avoid
7907 * flooding the user with errors. And stop iterating over lists
7908 * and dicts. */
7909 did_echo_string_emsg = TRUE;
7910 EMSG(_("E724: variable nested too deep for displaying"));
7911 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007913 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007914 }
7915 ++recurse;
7916
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007917 switch (tv->v_type)
7918 {
7919 case VAR_FUNC:
7920 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007921 r = tv->vval.v_string;
7922 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007923
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007924 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007925 if (tv->vval.v_list == NULL)
7926 {
7927 *tofree = NULL;
7928 r = NULL;
7929 }
7930 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7931 {
7932 *tofree = NULL;
7933 r = (char_u *)"[...]";
7934 }
7935 else
7936 {
7937 tv->vval.v_list->lv_copyID = copyID;
7938 *tofree = list2string(tv, copyID);
7939 r = *tofree;
7940 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007941 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007942
Bram Moolenaar8c711452005-01-14 21:53:12 +00007943 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007944 if (tv->vval.v_dict == NULL)
7945 {
7946 *tofree = NULL;
7947 r = NULL;
7948 }
7949 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7950 {
7951 *tofree = NULL;
7952 r = (char_u *)"{...}";
7953 }
7954 else
7955 {
7956 tv->vval.v_dict->dv_copyID = copyID;
7957 *tofree = dict2string(tv, copyID);
7958 r = *tofree;
7959 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007960 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007961
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007962 case VAR_STRING:
7963 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007964 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007965 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007966 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007967 *tofree = NULL;
7968 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007969 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007970
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007971 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007972#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973 *tofree = NULL;
7974 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7975 r = numbuf;
7976 break;
7977#endif
7978
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007979 case VAR_SPECIAL:
7980 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007981 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007982 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007983 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007984
Bram Moolenaar8502c702014-06-17 12:51:16 +02007985 if (--recurse == 0)
7986 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007987 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988}
7989
7990/*
7991 * Return a string with the string representation of a variable.
7992 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7993 * "numbuf" is used for a number.
7994 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007995 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007996 */
7997 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007998tv2string(
7999 typval_T *tv,
8000 char_u **tofree,
8001 char_u *numbuf,
8002 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008003{
8004 switch (tv->v_type)
8005 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008006 case VAR_FUNC:
8007 *tofree = string_quote(tv->vval.v_string, TRUE);
8008 return *tofree;
8009 case VAR_STRING:
8010 *tofree = string_quote(tv->vval.v_string, FALSE);
8011 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008012#ifdef FEAT_FLOAT
8013 case VAR_FLOAT:
8014 *tofree = NULL;
8015 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8016 return numbuf;
8017#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008018 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008019 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008020 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008021 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008022 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008023 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008024 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008025 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008026 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008027 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008028}
8029
8030/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008031 * Return string "str" in ' quotes, doubling ' characters.
8032 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 */
8035 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008036string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037{
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008039 char_u *p, *r, *s;
8040
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 len = (function ? 13 : 3);
8042 if (str != NULL)
8043 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008044 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008045 for (p = str; *p != NUL; mb_ptr_adv(p))
8046 if (*p == '\'')
8047 ++len;
8048 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008049 s = r = alloc(len);
8050 if (r != NULL)
8051 {
8052 if (function)
8053 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008054 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008055 r += 10;
8056 }
8057 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008058 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008059 if (str != NULL)
8060 for (p = str; *p != NUL; )
8061 {
8062 if (*p == '\'')
8063 *r++ = '\'';
8064 MB_COPY_CHAR(p, r);
8065 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008066 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008067 if (function)
8068 *r++ = ')';
8069 *r++ = NUL;
8070 }
8071 return s;
8072}
8073
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008074#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075/*
8076 * Convert the string "text" to a floating point number.
8077 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8078 * this always uses a decimal point.
8079 * Returns the length of the text that was consumed.
8080 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008081 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008082string2float(
8083 char_u *text,
8084 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008085{
8086 char *s = (char *)text;
8087 float_T f;
8088
8089 f = strtod(s, &s);
8090 *value = f;
8091 return (int)((char_u *)s - text);
8092}
8093#endif
8094
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008095/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008096 * Get the value of an environment variable.
8097 * "arg" is pointing to the '$'. It is advanced to after the name.
8098 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008099 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100 */
8101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008102get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103{
8104 char_u *string = NULL;
8105 int len;
8106 int cc;
8107 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008108 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109
8110 ++*arg;
8111 name = *arg;
8112 len = get_env_len(arg);
8113 if (evaluate)
8114 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008115 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008116 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008117
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 cc = name[len];
8119 name[len] = NUL;
8120 /* first try vim_getenv(), fast for normal environment vars */
8121 string = vim_getenv(name, &mustfree);
8122 if (string != NULL && *string != NUL)
8123 {
8124 if (!mustfree)
8125 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008126 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008127 else
8128 {
8129 if (mustfree)
8130 vim_free(string);
8131
8132 /* next try expanding things like $VIM and ${HOME} */
8133 string = expand_env_save(name - 1);
8134 if (string != NULL && *string == '$')
8135 {
8136 vim_free(string);
8137 string = NULL;
8138 }
8139 }
8140 name[len] = cc;
8141
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008142 rettv->v_type = VAR_STRING;
8143 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008144 }
8145
8146 return OK;
8147}
8148
8149/*
8150 * Array with names and number of arguments of all internal functions
8151 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8152 */
8153static struct fst
8154{
8155 char *f_name; /* function name */
8156 char f_min_argc; /* minimal number of arguments */
8157 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008158 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008159 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008160} functions[] =
8161{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#ifdef FEAT_FLOAT
8163 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008164 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008166 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008167 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008168 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008169 {"append", 2, 2, f_append},
8170 {"argc", 0, 0, f_argc},
8171 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008172 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008173 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008174#ifdef FEAT_FLOAT
8175 {"asin", 1, 1, f_asin}, /* WJMc */
8176#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008177 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008178 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008179 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_false", 1, 2, f_assert_false},
8181 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008182#ifdef FEAT_FLOAT
8183 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008184 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008185#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008187 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008188 {"bufexists", 1, 1, f_bufexists},
8189 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8190 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8191 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8192 {"buflisted", 1, 1, f_buflisted},
8193 {"bufloaded", 1, 1, f_bufloaded},
8194 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008195 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008196 {"bufwinnr", 1, 1, f_bufwinnr},
8197 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008198 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008199 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008200 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008201#ifdef FEAT_FLOAT
8202 {"ceil", 1, 1, f_ceil},
8203#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008204#ifdef FEAT_CHANNEL
8205 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008206 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8207 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008208# ifdef FEAT_JOB
8209 {"ch_getjob", 1, 1, f_ch_getjob},
8210# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008211 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008212 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008213 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008214 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008215 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008216 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8217 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008218 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008219 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008221 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008222 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008224 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008226#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008227 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008228 {"complete_add", 1, 1, f_complete_add},
8229 {"complete_check", 0, 0, f_complete_check},
8230#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008232 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008233#ifdef FEAT_FLOAT
8234 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008235 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008236#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008237 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008239 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008240 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008241 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008243 {"diff_filler", 1, 1, f_diff_filler},
8244 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008245 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008246 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008248 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"eventhandler", 0, 0, f_eventhandler},
8250 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008251 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008252 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008253#ifdef FEAT_FLOAT
8254 {"exp", 1, 1, f_exp},
8255#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008256 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008257 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008258 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008259 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8260 {"filereadable", 1, 1, f_filereadable},
8261 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008262 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008263 {"finddir", 1, 3, f_finddir},
8264 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265#ifdef FEAT_FLOAT
8266 {"float2nr", 1, 1, f_float2nr},
8267 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008268 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008270 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"fnamemodify", 2, 2, f_fnamemodify},
8272 {"foldclosed", 1, 1, f_foldclosed},
8273 {"foldclosedend", 1, 1, f_foldclosedend},
8274 {"foldlevel", 1, 1, f_foldlevel},
8275 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008276 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008277 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008278 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008279 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008280 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008281 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008282 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"getchar", 0, 1, f_getchar},
8284 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008285 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getcmdline", 0, 0, f_getcmdline},
8287 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008288 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008289 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008290 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008291 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008292 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008293 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008294 {"getfsize", 1, 1, f_getfsize},
8295 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008296 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008297 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008298 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008299 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008300 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008301 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008302 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008303 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008305 {"gettabvar", 2, 3, f_gettabvar},
8306 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008307 {"getwinposx", 0, 0, f_getwinposx},
8308 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008310 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008311 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008312 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008314 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008315 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008316 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8318 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8319 {"histadd", 2, 2, f_histadd},
8320 {"histdel", 1, 2, f_histdel},
8321 {"histget", 1, 2, f_histget},
8322 {"histnr", 1, 1, f_histnr},
8323 {"hlID", 1, 1, f_hlID},
8324 {"hlexists", 1, 1, f_hlexists},
8325 {"hostname", 0, 0, f_hostname},
8326 {"iconv", 3, 3, f_iconv},
8327 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008328 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008329 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008331 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008332 {"inputrestore", 0, 0, f_inputrestore},
8333 {"inputsave", 0, 0, f_inputsave},
8334 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008335 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008336 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008337 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008338 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008339#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8340 {"isnan", 1, 1, f_isnan},
8341#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008342 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008343#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008344# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008345 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008346# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008347 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008348 {"job_start", 1, 2, f_job_start},
8349 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008350 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008351#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008352 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008353 {"js_decode", 1, 1, f_js_decode},
8354 {"js_encode", 1, 1, f_js_encode},
8355 {"json_decode", 1, 1, f_json_decode},
8356 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008357 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008359 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008360 {"libcall", 3, 3, f_libcall},
8361 {"libcallnr", 3, 3, f_libcallnr},
8362 {"line", 1, 1, f_line},
8363 {"line2byte", 1, 1, f_line2byte},
8364 {"lispindent", 1, 1, f_lispindent},
8365 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008366#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008367 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008368 {"log10", 1, 1, f_log10},
8369#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008370#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008371 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008372#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008373 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008374 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008375 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008376 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008377 {"matchadd", 2, 5, f_matchadd},
8378 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008379 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008380 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008381 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008382 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008383 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008384 {"max", 1, 1, f_max},
8385 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008386#ifdef vim_mkdir
8387 {"mkdir", 1, 3, f_mkdir},
8388#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008389 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008390#ifdef FEAT_MZSCHEME
8391 {"mzeval", 1, 1, f_mzeval},
8392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008393 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008394 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008395 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008396 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008397#ifdef FEAT_PERL
8398 {"perleval", 1, 1, f_perleval},
8399#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008400#ifdef FEAT_FLOAT
8401 {"pow", 2, 2, f_pow},
8402#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008404 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008405 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008406#ifdef FEAT_PYTHON3
8407 {"py3eval", 1, 1, f_py3eval},
8408#endif
8409#ifdef FEAT_PYTHON
8410 {"pyeval", 1, 1, f_pyeval},
8411#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008412 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008413 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008414 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008415 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008416 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"remote_expr", 2, 3, f_remote_expr},
8418 {"remote_foreground", 1, 1, f_remote_foreground},
8419 {"remote_peek", 1, 2, f_remote_peek},
8420 {"remote_read", 1, 1, f_remote_read},
8421 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008422 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008424 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008426 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008427#ifdef FEAT_FLOAT
8428 {"round", 1, 1, f_round},
8429#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008430 {"screenattr", 2, 2, f_screenattr},
8431 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008432 {"screencol", 0, 0, f_screencol},
8433 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008434 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008435 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008436 {"searchpair", 3, 7, f_searchpair},
8437 {"searchpairpos", 3, 7, f_searchpairpos},
8438 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"server2client", 2, 2, f_server2client},
8440 {"serverlist", 0, 0, f_serverlist},
8441 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008442 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008443 {"setcmdpos", 1, 1, f_setcmdpos},
8444 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008445 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008446 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008447 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008448 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008450 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008451 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008453#ifdef FEAT_CRYPT
8454 {"sha256", 1, 1, f_sha256},
8455#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008456 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008457 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008459#ifdef FEAT_FLOAT
8460 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008461 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008463 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008464 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008465 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008466 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008467 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008468#ifdef FEAT_FLOAT
8469 {"sqrt", 1, 1, f_sqrt},
8470 {"str2float", 1, 1, f_str2float},
8471#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008472 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008473 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008474 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475#ifdef HAVE_STRFTIME
8476 {"strftime", 1, 2, f_strftime},
8477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008478 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"strlen", 1, 1, f_strlen},
8481 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008482 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008484 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008485 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"substitute", 4, 4, f_substitute},
8487 {"synID", 3, 3, f_synID},
8488 {"synIDattr", 2, 3, f_synIDattr},
8489 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008490 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008491 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008492 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008493 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008494 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008495 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008496 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008497 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008498 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008499#ifdef FEAT_FLOAT
8500 {"tan", 1, 1, f_tan},
8501 {"tanh", 1, 1, f_tanh},
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008504 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008505 {"tolower", 1, 1, f_tolower},
8506 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008507 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008508#ifdef FEAT_FLOAT
8509 {"trunc", 1, 1, f_trunc},
8510#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008511 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008512 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008513 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008514 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008515 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {"virtcol", 1, 1, f_virtcol},
8517 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008518 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008519 {"winbufnr", 1, 1, f_winbufnr},
8520 {"wincol", 0, 0, f_wincol},
8521 {"winheight", 1, 1, f_winheight},
8522 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008523 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008524 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008525 {"winrestview", 1, 1, f_winrestview},
8526 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008528 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008529 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008530 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531};
8532
8533#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8534
8535/*
8536 * Function given to ExpandGeneric() to obtain the list of internal
8537 * or user defined function names.
8538 */
8539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008540get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008541{
8542 static int intidx = -1;
8543 char_u *name;
8544
8545 if (idx == 0)
8546 intidx = -1;
8547 if (intidx < 0)
8548 {
8549 name = get_user_func_name(xp, idx);
8550 if (name != NULL)
8551 return name;
8552 }
8553 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8554 {
8555 STRCPY(IObuff, functions[intidx].f_name);
8556 STRCAT(IObuff, "(");
8557 if (functions[intidx].f_max_argc == 0)
8558 STRCAT(IObuff, ")");
8559 return IObuff;
8560 }
8561
8562 return NULL;
8563}
8564
8565/*
8566 * Function given to ExpandGeneric() to obtain the list of internal or
8567 * user defined variable or function names.
8568 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008569 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008570get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008571{
8572 static int intidx = -1;
8573 char_u *name;
8574
8575 if (idx == 0)
8576 intidx = -1;
8577 if (intidx < 0)
8578 {
8579 name = get_function_name(xp, idx);
8580 if (name != NULL)
8581 return name;
8582 }
8583 return get_user_var_name(xp, ++intidx);
8584}
8585
8586#endif /* FEAT_CMDL_COMPL */
8587
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008588#if defined(EBCDIC) || defined(PROTO)
8589/*
8590 * Compare struct fst by function name.
8591 */
8592 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008593compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008594{
8595 struct fst *p1 = (struct fst *)s1;
8596 struct fst *p2 = (struct fst *)s2;
8597
8598 return STRCMP(p1->f_name, p2->f_name);
8599}
8600
8601/*
8602 * Sort the function table by function name.
8603 * The sorting of the table above is ASCII dependant.
8604 * On machines using EBCDIC we have to sort it.
8605 */
8606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008607sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008608{
8609 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8610
8611 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8612}
8613#endif
8614
8615
Bram Moolenaar071d4272004-06-13 20:20:40 +00008616/*
8617 * Find internal function in table above.
8618 * Return index, or -1 if not found
8619 */
8620 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008621find_internal_func(
8622 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623{
8624 int first = 0;
8625 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8626 int cmp;
8627 int x;
8628
8629 /*
8630 * Find the function name in the table. Binary search.
8631 */
8632 while (first <= last)
8633 {
8634 x = first + ((unsigned)(last - first) >> 1);
8635 cmp = STRCMP(name, functions[x].f_name);
8636 if (cmp < 0)
8637 last = x - 1;
8638 else if (cmp > 0)
8639 first = x + 1;
8640 else
8641 return x;
8642 }
8643 return -1;
8644}
8645
8646/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008647 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8648 * name it contains, otherwise return "name".
8649 */
8650 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008651deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008652{
Bram Moolenaar33570922005-01-25 22:26:29 +00008653 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 int cc;
8655
8656 cc = name[*lenp];
8657 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008658 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008660 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663 {
8664 *lenp = 0;
8665 return (char_u *)""; /* just in case */
8666 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008667 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008668 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008669 }
8670
8671 return name;
8672}
8673
8674/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675 * Allocate a variable for the result of a function.
8676 * Return OK or FAIL.
8677 */
8678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008679get_func_tv(
8680 char_u *name, /* name of the function */
8681 int len, /* length of "name" */
8682 typval_T *rettv,
8683 char_u **arg, /* argument, pointing to the '(' */
8684 linenr_T firstline, /* first line of range */
8685 linenr_T lastline, /* last line of range */
8686 int *doesrange, /* return: function handled range */
8687 int evaluate,
8688 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689{
8690 char_u *argp;
8691 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008692 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008693 int argcount = 0; /* number of arguments found */
8694
8695 /*
8696 * Get the arguments.
8697 */
8698 argp = *arg;
8699 while (argcount < MAX_FUNC_ARGS)
8700 {
8701 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8702 if (*argp == ')' || *argp == ',' || *argp == NUL)
8703 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8705 {
8706 ret = FAIL;
8707 break;
8708 }
8709 ++argcount;
8710 if (*argp != ',')
8711 break;
8712 }
8713 if (*argp == ')')
8714 ++argp;
8715 else
8716 ret = FAIL;
8717
8718 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008719 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008720 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008722 {
8723 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008724 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008725 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008726 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008727 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728
8729 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008731
8732 *arg = skipwhite(argp);
8733 return ret;
8734}
8735
8736
8737/*
8738 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008739 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008740 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008743call_func(
8744 char_u *funcname, /* name of the function */
8745 int len, /* length of "name" */
8746 typval_T *rettv, /* return value goes here */
8747 int argcount, /* number of "argvars" */
8748 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008749 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008750 linenr_T firstline, /* first line of range */
8751 linenr_T lastline, /* last line of range */
8752 int *doesrange, /* return: function handled range */
8753 int evaluate,
8754 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008755{
8756 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008757#define ERROR_UNKNOWN 0
8758#define ERROR_TOOMANY 1
8759#define ERROR_TOOFEW 2
8760#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008761#define ERROR_DICT 4
8762#define ERROR_NONE 5
8763#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764 int error = ERROR_NONE;
8765 int i;
8766 int llen;
8767 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768#define FLEN_FIXED 40
8769 char_u fname_buf[FLEN_FIXED + 1];
8770 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008771 char_u *name;
8772
8773 /* Make a copy of the name, if it comes from a funcref variable it could
8774 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008775 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008776 if (name == NULL)
8777 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008778
8779 /*
8780 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8781 * Change <SNR>123_name() to K_SNR 123_name().
8782 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8783 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008784 llen = eval_fname_script(name);
8785 if (llen > 0)
8786 {
8787 fname_buf[0] = K_SPECIAL;
8788 fname_buf[1] = KS_EXTRA;
8789 fname_buf[2] = (int)KE_SNR;
8790 i = 3;
8791 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8792 {
8793 if (current_SID <= 0)
8794 error = ERROR_SCRIPT;
8795 else
8796 {
8797 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8798 i = (int)STRLEN(fname_buf);
8799 }
8800 }
8801 if (i + STRLEN(name + llen) < FLEN_FIXED)
8802 {
8803 STRCPY(fname_buf + i, name + llen);
8804 fname = fname_buf;
8805 }
8806 else
8807 {
8808 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8809 if (fname == NULL)
8810 error = ERROR_OTHER;
8811 else
8812 {
8813 mch_memmove(fname, fname_buf, (size_t)i);
8814 STRCPY(fname + i, name + llen);
8815 }
8816 }
8817 }
8818 else
8819 fname = name;
8820
8821 *doesrange = FALSE;
8822
8823
8824 /* execute the function if no errors detected and executing */
8825 if (evaluate && error == ERROR_NONE)
8826 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008827 char_u *rfname = fname;
8828
8829 /* Ignore "g:" before a function name. */
8830 if (fname[0] == 'g' && fname[1] == ':')
8831 rfname = fname + 2;
8832
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008833 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8834 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 error = ERROR_UNKNOWN;
8836
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008837 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008838 {
8839 /*
8840 * User defined function.
8841 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008842 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008843
Bram Moolenaar071d4272004-06-13 20:20:40 +00008844#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008845 /* Trigger FuncUndefined event, may load the function. */
8846 if (fp == NULL
8847 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008848 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008849 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008851 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 }
8854#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008855 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008856 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008857 {
8858 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008859 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008860 }
8861
Bram Moolenaar071d4272004-06-13 20:20:40 +00008862 if (fp != NULL)
8863 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008864 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008866 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008868 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008870 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008871 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 else
8873 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008874 int did_save_redo = FALSE;
8875
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876 /*
8877 * Call the user function.
8878 * Save and restore search patterns, script variables and
8879 * redo buffer.
8880 */
8881 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008882#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008883 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008884#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008885 {
8886 saveRedobuff();
8887 did_save_redo = TRUE;
8888 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008889 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008890 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008891 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008892 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8893 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8894 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008895 /* Function was unreferenced while being used, free it
8896 * now. */
8897 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008898 if (did_save_redo)
8899 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900 restore_search_patterns();
8901 error = ERROR_NONE;
8902 }
8903 }
8904 }
8905 else
8906 {
8907 /*
8908 * Find the function name in the table, call its implementation.
8909 */
8910 i = find_internal_func(fname);
8911 if (i >= 0)
8912 {
8913 if (argcount < functions[i].f_min_argc)
8914 error = ERROR_TOOFEW;
8915 else if (argcount > functions[i].f_max_argc)
8916 error = ERROR_TOOMANY;
8917 else
8918 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008919 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008920 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 error = ERROR_NONE;
8922 }
8923 }
8924 }
8925 /*
8926 * The function call (or "FuncUndefined" autocommand sequence) might
8927 * have been aborted by an error, an interrupt, or an explicitly thrown
8928 * exception that has not been caught so far. This situation can be
8929 * tested for by calling aborting(). For an error in an internal
8930 * function or for the "E132" error in call_user_func(), however, the
8931 * throw point at which the "force_abort" flag (temporarily reset by
8932 * emsg()) is normally updated has not been reached yet. We need to
8933 * update that flag first to make aborting() reliable.
8934 */
8935 update_force_abort();
8936 }
8937 if (error == ERROR_NONE)
8938 ret = OK;
8939
8940 /*
8941 * Report an error unless the argument evaluation or function call has been
8942 * cancelled due to an aborting error, an interrupt, or an exception.
8943 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008944 if (!aborting())
8945 {
8946 switch (error)
8947 {
8948 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008949 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008950 break;
8951 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008952 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008953 break;
8954 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008955 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008956 name);
8957 break;
8958 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008959 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008960 name);
8961 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008962 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008963 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008964 name);
8965 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008966 }
8967 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
Bram Moolenaar071d4272004-06-13 20:20:40 +00008969 if (fname != name && fname != fname_buf)
8970 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008971 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972
8973 return ret;
8974}
8975
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008976/*
8977 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008978 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008979 */
8980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008981emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008982{
8983 char_u *p;
8984
8985 if (*name == K_SPECIAL)
8986 p = concat_str((char_u *)"<SNR>", name + 3);
8987 else
8988 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008989 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008990 if (p != name)
8991 vim_free(p);
8992}
8993
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008994/*
8995 * Return TRUE for a non-zero Number and a non-empty String.
8996 */
8997 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008998non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008999{
9000 return ((argvars[0].v_type == VAR_NUMBER
9001 && argvars[0].vval.v_number != 0)
9002 || (argvars[0].v_type == VAR_STRING
9003 && argvars[0].vval.v_string != NULL
9004 && *argvars[0].vval.v_string != NUL));
9005}
9006
Bram Moolenaar071d4272004-06-13 20:20:40 +00009007/*********************************************
9008 * Implementation of the built-in functions
9009 */
9010
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009011#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009012static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009013
9014/*
9015 * Get the float value of "argvars[0]" into "f".
9016 * Returns FAIL when the argument is not a Number or Float.
9017 */
9018 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009019get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009020{
9021 if (argvars[0].v_type == VAR_FLOAT)
9022 {
9023 *f = argvars[0].vval.v_float;
9024 return OK;
9025 }
9026 if (argvars[0].v_type == VAR_NUMBER)
9027 {
9028 *f = (float_T)argvars[0].vval.v_number;
9029 return OK;
9030 }
9031 EMSG(_("E808: Number or Float required"));
9032 return FAIL;
9033}
9034
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009035/*
9036 * "abs(expr)" function
9037 */
9038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009039f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009040{
9041 if (argvars[0].v_type == VAR_FLOAT)
9042 {
9043 rettv->v_type = VAR_FLOAT;
9044 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9045 }
9046 else
9047 {
9048 varnumber_T n;
9049 int error = FALSE;
9050
9051 n = get_tv_number_chk(&argvars[0], &error);
9052 if (error)
9053 rettv->vval.v_number = -1;
9054 else if (n > 0)
9055 rettv->vval.v_number = n;
9056 else
9057 rettv->vval.v_number = -n;
9058 }
9059}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009060
9061/*
9062 * "acos()" function
9063 */
9064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009065f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009066{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009067 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009068
9069 rettv->v_type = VAR_FLOAT;
9070 if (get_float_arg(argvars, &f) == OK)
9071 rettv->vval.v_float = acos(f);
9072 else
9073 rettv->vval.v_float = 0.0;
9074}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009075#endif
9076
Bram Moolenaar071d4272004-06-13 20:20:40 +00009077/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009078 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009079 */
9080 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009081f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009082{
Bram Moolenaar33570922005-01-25 22:26:29 +00009083 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009084
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009085 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009086 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009087 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009088 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009089 && !tv_check_lock(l->lv_lock,
9090 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009091 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009092 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009093 }
9094 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009095 EMSG(_(e_listreq));
9096}
9097
9098/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009099 * "alloc_fail(id, countdown, repeat)" function
9100 */
9101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009102f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009103{
9104 if (argvars[0].v_type != VAR_NUMBER
9105 || argvars[0].vval.v_number <= 0
9106 || argvars[1].v_type != VAR_NUMBER
9107 || argvars[1].vval.v_number < 0
9108 || argvars[2].v_type != VAR_NUMBER)
9109 EMSG(_(e_invarg));
9110 else
9111 {
9112 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009113 if (alloc_fail_id >= aid_last)
9114 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009115 alloc_fail_countdown = argvars[1].vval.v_number;
9116 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009117 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009118 }
9119}
9120
9121/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009122 * "and(expr, expr)" function
9123 */
9124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009125f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009126{
9127 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9128 & get_tv_number_chk(&argvars[1], NULL);
9129}
9130
9131/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009132 * "append(lnum, string/list)" function
9133 */
9134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009135f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009136{
9137 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009138 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009139 list_T *l = NULL;
9140 listitem_T *li = NULL;
9141 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142 long added = 0;
9143
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009144 /* When coming here from Insert mode, sync undo, so that this can be
9145 * undone separately from what was previously inserted. */
9146 if (u_sync_once == 2)
9147 {
9148 u_sync_once = 1; /* notify that u_sync() was called */
9149 u_sync(TRUE);
9150 }
9151
Bram Moolenaar0d660222005-01-07 21:51:51 +00009152 lnum = get_tv_lnum(argvars);
9153 if (lnum >= 0
9154 && lnum <= curbuf->b_ml.ml_line_count
9155 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009156 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009157 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009158 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009159 l = argvars[1].vval.v_list;
9160 if (l == NULL)
9161 return;
9162 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009163 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 for (;;)
9165 {
9166 if (l == NULL)
9167 tv = &argvars[1]; /* append a string */
9168 else if (li == NULL)
9169 break; /* end of list */
9170 else
9171 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009172 line = get_tv_string_chk(tv);
9173 if (line == NULL) /* type error */
9174 {
9175 rettv->vval.v_number = 1; /* Failed */
9176 break;
9177 }
9178 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009179 ++added;
9180 if (l == NULL)
9181 break;
9182 li = li->li_next;
9183 }
9184
9185 appended_lines_mark(lnum, added);
9186 if (curwin->w_cursor.lnum > lnum)
9187 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009188 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009189 else
9190 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009191}
9192
9193/*
9194 * "argc()" function
9195 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009197f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009198{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009199 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009200}
9201
9202/*
9203 * "argidx()" function
9204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009206f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009207{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009208 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009209}
9210
9211/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009212 * "arglistid()" function
9213 */
9214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009215f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009216{
9217 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009218
9219 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009220 wp = find_tabwin(&argvars[0], &argvars[1]);
9221 if (wp != NULL)
9222 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009223}
9224
9225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226 * "argv(nr)" function
9227 */
9228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009229f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009230{
9231 int idx;
9232
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009233 if (argvars[0].v_type != VAR_UNKNOWN)
9234 {
9235 idx = get_tv_number_chk(&argvars[0], NULL);
9236 if (idx >= 0 && idx < ARGCOUNT)
9237 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9238 else
9239 rettv->vval.v_string = NULL;
9240 rettv->v_type = VAR_STRING;
9241 }
9242 else if (rettv_list_alloc(rettv) == OK)
9243 for (idx = 0; idx < ARGCOUNT; ++idx)
9244 list_append_string(rettv->vval.v_list,
9245 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009246}
9247
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009248static void prepare_assert_error(garray_T*gap);
9249static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9250static void assert_error(garray_T *gap);
9251static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009252
9253/*
9254 * Prepare "gap" for an assert error and add the sourcing position.
9255 */
9256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009257prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009258{
9259 char buf[NUMBUFLEN];
9260
9261 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009262 if (sourcing_name != NULL)
9263 {
9264 ga_concat(gap, sourcing_name);
9265 if (sourcing_lnum > 0)
9266 ga_concat(gap, (char_u *)" ");
9267 }
9268 if (sourcing_lnum > 0)
9269 {
9270 sprintf(buf, "line %ld", (long)sourcing_lnum);
9271 ga_concat(gap, (char_u *)buf);
9272 }
9273 if (sourcing_name != NULL || sourcing_lnum > 0)
9274 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009275}
9276
9277/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009278 * Append "str" to "gap", escaping unprintable characters.
9279 * Changes NL to \n, CR to \r, etc.
9280 */
9281 static void
9282ga_concat_esc(garray_T *gap, char_u *str)
9283{
9284 char_u *p;
9285 char_u buf[NUMBUFLEN];
9286
9287 for (p = str; *p != NUL; ++p)
9288 switch (*p)
9289 {
9290 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9291 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9292 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9293 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9294 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9295 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9296 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9297 default:
9298 if (*p < ' ')
9299 {
9300 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9301 ga_concat(gap, buf);
9302 }
9303 else
9304 ga_append(gap, *p);
9305 break;
9306 }
9307}
9308
9309/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310 * Fill "gap" with information about an assert error.
9311 */
9312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009313fill_assert_error(
9314 garray_T *gap,
9315 typval_T *opt_msg_tv,
9316 char_u *exp_str,
9317 typval_T *exp_tv,
9318 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009319{
9320 char_u numbuf[NUMBUFLEN];
9321 char_u *tofree;
9322
9323 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9324 {
9325 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9326 vim_free(tofree);
9327 }
9328 else
9329 {
9330 ga_concat(gap, (char_u *)"Expected ");
9331 if (exp_str == NULL)
9332 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009333 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009334 vim_free(tofree);
9335 }
9336 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009337 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009338 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009339 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009340 vim_free(tofree);
9341 }
9342}
Bram Moolenaar43345542015-11-29 17:35:35 +01009343
9344/*
9345 * Add an assert error to v:errors.
9346 */
9347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009348assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009349{
9350 struct vimvar *vp = &vimvars[VV_ERRORS];
9351
9352 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9353 /* Make sure v:errors is a list. */
9354 set_vim_var_list(VV_ERRORS, list_alloc());
9355 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9356}
9357
Bram Moolenaar43345542015-11-29 17:35:35 +01009358/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009359 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009360 */
9361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009362f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009363{
9364 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009365
9366 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9367 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368 prepare_assert_error(&ga);
9369 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9370 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009371 ga_clear(&ga);
9372 }
9373}
9374
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009375/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009376 * "assert_exception(string[, msg])" function
9377 */
9378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009379f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009380{
9381 garray_T ga;
9382 char *error;
9383
9384 error = (char *)get_tv_string_chk(&argvars[0]);
9385 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9386 {
9387 prepare_assert_error(&ga);
9388 ga_concat(&ga, (char_u *)"v:exception is not set");
9389 assert_error(&ga);
9390 ga_clear(&ga);
9391 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009392 else if (error != NULL
9393 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009394 {
9395 prepare_assert_error(&ga);
9396 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9397 &vimvars[VV_EXCEPTION].vv_tv);
9398 assert_error(&ga);
9399 ga_clear(&ga);
9400 }
9401}
9402
9403/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009404 * "assert_fails(cmd [, error])" function
9405 */
9406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009407f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009408{
9409 char_u *cmd = get_tv_string_chk(&argvars[0]);
9410 garray_T ga;
9411
9412 called_emsg = FALSE;
9413 suppress_errthrow = TRUE;
9414 emsg_silent = TRUE;
9415 do_cmdline_cmd(cmd);
9416 if (!called_emsg)
9417 {
9418 prepare_assert_error(&ga);
9419 ga_concat(&ga, (char_u *)"command did not fail: ");
9420 ga_concat(&ga, cmd);
9421 assert_error(&ga);
9422 ga_clear(&ga);
9423 }
9424 else if (argvars[1].v_type != VAR_UNKNOWN)
9425 {
9426 char_u buf[NUMBUFLEN];
9427 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9428
9429 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9430 {
9431 prepare_assert_error(&ga);
9432 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9433 &vimvars[VV_ERRMSG].vv_tv);
9434 assert_error(&ga);
9435 ga_clear(&ga);
9436 }
9437 }
9438
9439 called_emsg = FALSE;
9440 suppress_errthrow = FALSE;
9441 emsg_silent = FALSE;
9442 emsg_on_display = FALSE;
9443 set_vim_var_string(VV_ERRMSG, NULL, 0);
9444}
9445
9446/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009447 * Common for assert_true() and assert_false().
9448 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009450assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009451{
9452 int error = FALSE;
9453 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009454
Bram Moolenaar37127922016-02-06 20:29:28 +01009455 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009456 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009457 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009458 if (argvars[0].v_type != VAR_NUMBER
9459 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9460 || error)
9461 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 prepare_assert_error(&ga);
9463 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009464 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009465 NULL, &argvars[0]);
9466 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009467 ga_clear(&ga);
9468 }
9469}
9470
9471/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009472 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009473 */
9474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009475f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009476{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009477 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009478}
9479
9480/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009481 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009482 */
9483 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009484f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009485{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009486 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009487}
9488
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009489#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009490/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009491 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009492 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009494f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009496 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009497
9498 rettv->v_type = VAR_FLOAT;
9499 if (get_float_arg(argvars, &f) == OK)
9500 rettv->vval.v_float = asin(f);
9501 else
9502 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009503}
9504
9505/*
9506 * "atan()" function
9507 */
9508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009510{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009511 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009512
9513 rettv->v_type = VAR_FLOAT;
9514 if (get_float_arg(argvars, &f) == OK)
9515 rettv->vval.v_float = atan(f);
9516 else
9517 rettv->vval.v_float = 0.0;
9518}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009519
9520/*
9521 * "atan2()" function
9522 */
9523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009524f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009525{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009526 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009527
9528 rettv->v_type = VAR_FLOAT;
9529 if (get_float_arg(argvars, &fx) == OK
9530 && get_float_arg(&argvars[1], &fy) == OK)
9531 rettv->vval.v_float = atan2(fx, fy);
9532 else
9533 rettv->vval.v_float = 0.0;
9534}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009535#endif
9536
Bram Moolenaar071d4272004-06-13 20:20:40 +00009537/*
9538 * "browse(save, title, initdir, default)" function
9539 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009541f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009542{
9543#ifdef FEAT_BROWSE
9544 int save;
9545 char_u *title;
9546 char_u *initdir;
9547 char_u *defname;
9548 char_u buf[NUMBUFLEN];
9549 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009550 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009551
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009552 save = get_tv_number_chk(&argvars[0], &error);
9553 title = get_tv_string_chk(&argvars[1]);
9554 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9555 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009556
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009557 if (error || title == NULL || initdir == NULL || defname == NULL)
9558 rettv->vval.v_string = NULL;
9559 else
9560 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009561 do_browse(save ? BROWSE_SAVE : 0,
9562 title, defname, NULL, initdir, NULL, curbuf);
9563#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009564 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009565#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009566 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009567}
9568
9569/*
9570 * "browsedir(title, initdir)" function
9571 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009573f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574{
9575#ifdef FEAT_BROWSE
9576 char_u *title;
9577 char_u *initdir;
9578 char_u buf[NUMBUFLEN];
9579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009580 title = get_tv_string_chk(&argvars[0]);
9581 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009582
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009583 if (title == NULL || initdir == NULL)
9584 rettv->vval.v_string = NULL;
9585 else
9586 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009587 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009589 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009590#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009591 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592}
9593
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009594static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596/*
9597 * Find a buffer by number or exact name.
9598 */
9599 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009604 if (avar->v_type == VAR_NUMBER)
9605 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009606 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009607 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009608 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009609 if (buf == NULL)
9610 {
9611 /* No full path name match, try a match with a URL or a "nofile"
9612 * buffer, these don't use the full path. */
9613 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9614 if (buf->b_fname != NULL
9615 && (path_with_url(buf->b_fname)
9616#ifdef FEAT_QUICKFIX
9617 || bt_nofile(buf)
9618#endif
9619 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009620 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009621 break;
9622 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009623 }
9624 return buf;
9625}
9626
9627/*
9628 * "bufexists(expr)" function
9629 */
9630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009631f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009632{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009633 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009634}
9635
9636/*
9637 * "buflisted(expr)" function
9638 */
9639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009640f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009641{
9642 buf_T *buf;
9643
9644 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009645 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646}
9647
9648/*
9649 * "bufloaded(expr)" function
9650 */
9651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009652f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009653{
9654 buf_T *buf;
9655
9656 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009657 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658}
9659
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009660static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009661
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662/*
9663 * Get buffer by number or pattern.
9664 */
9665 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009666get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009668 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009669 int save_magic;
9670 char_u *save_cpo;
9671 buf_T *buf;
9672
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009673 if (tv->v_type == VAR_NUMBER)
9674 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009675 if (tv->v_type != VAR_STRING)
9676 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009677 if (name == NULL || *name == NUL)
9678 return curbuf;
9679 if (name[0] == '$' && name[1] == NUL)
9680 return lastbuf;
9681
9682 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9683 save_magic = p_magic;
9684 p_magic = TRUE;
9685 save_cpo = p_cpo;
9686 p_cpo = (char_u *)"";
9687
9688 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009689 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009690
9691 p_magic = save_magic;
9692 p_cpo = save_cpo;
9693
9694 /* If not found, try expanding the name, like done for bufexists(). */
9695 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009696 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009697
9698 return buf;
9699}
9700
9701/*
9702 * "bufname(expr)" function
9703 */
9704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009705f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706{
9707 buf_T *buf;
9708
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009709 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009710 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009711 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009714 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717 --emsg_off;
9718}
9719
9720/*
9721 * "bufnr(expr)" function
9722 */
9723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009724f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725{
9726 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009727 int error = FALSE;
9728 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009730 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009732 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009733 --emsg_off;
9734
9735 /* If the buffer isn't found and the second argument is not zero create a
9736 * new buffer. */
9737 if (buf == NULL
9738 && argvars[1].v_type != VAR_UNKNOWN
9739 && get_tv_number_chk(&argvars[1], &error) != 0
9740 && !error
9741 && (name = get_tv_string_chk(&argvars[0])) != NULL
9742 && !error)
9743 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9744
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009746 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009748 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749}
9750
9751/*
9752 * "bufwinnr(nr)" function
9753 */
9754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009755f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009756{
9757#ifdef FEAT_WINDOWS
9758 win_T *wp;
9759 int winnr = 0;
9760#endif
9761 buf_T *buf;
9762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009763 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009765 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766#ifdef FEAT_WINDOWS
9767 for (wp = firstwin; wp; wp = wp->w_next)
9768 {
9769 ++winnr;
9770 if (wp->w_buffer == buf)
9771 break;
9772 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009773 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009774#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009775 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776#endif
9777 --emsg_off;
9778}
9779
9780/*
9781 * "byte2line(byte)" function
9782 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009784f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009785{
9786#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009787 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788#else
9789 long boff = 0;
9790
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009791 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009793 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009794 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009795 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009796 (linenr_T)0, &boff);
9797#endif
9798}
9799
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009801byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009802{
9803#ifdef FEAT_MBYTE
9804 char_u *t;
9805#endif
9806 char_u *str;
9807 long idx;
9808
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009809 str = get_tv_string_chk(&argvars[0]);
9810 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009812 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009813 return;
9814
9815#ifdef FEAT_MBYTE
9816 t = str;
9817 for ( ; idx > 0; idx--)
9818 {
9819 if (*t == NUL) /* EOL reached */
9820 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009821 if (enc_utf8 && comp)
9822 t += utf_ptr2len(t);
9823 else
9824 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009825 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009826 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009827#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009828 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009830#endif
9831}
9832
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009833/*
9834 * "byteidx()" function
9835 */
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009838{
9839 byteidx(argvars, rettv, FALSE);
9840}
9841
9842/*
9843 * "byteidxcomp()" function
9844 */
9845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009846f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009847{
9848 byteidx(argvars, rettv, TRUE);
9849}
9850
Bram Moolenaardb913952012-06-29 12:54:53 +02009851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009852func_call(
9853 char_u *name,
9854 typval_T *args,
9855 dict_T *selfdict,
9856 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009857{
9858 listitem_T *item;
9859 typval_T argv[MAX_FUNC_ARGS + 1];
9860 int argc = 0;
9861 int dummy;
9862 int r = 0;
9863
9864 for (item = args->vval.v_list->lv_first; item != NULL;
9865 item = item->li_next)
9866 {
9867 if (argc == MAX_FUNC_ARGS)
9868 {
9869 EMSG(_("E699: Too many arguments"));
9870 break;
9871 }
9872 /* Make a copy of each argument. This is needed to be able to set
9873 * v_lock to VAR_FIXED in the copy without changing the original list.
9874 */
9875 copy_tv(&item->li_tv, &argv[argc++]);
9876 }
9877
9878 if (item == NULL)
9879 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9881 &dummy, TRUE, selfdict);
9882
9883 /* Free the arguments. */
9884 while (argc > 0)
9885 clear_tv(&argv[--argc]);
9886
9887 return r;
9888}
9889
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009890/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891 * "call(func, arglist)" function
9892 */
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895{
9896 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009897 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009898
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899 if (argvars[1].v_type != VAR_LIST)
9900 {
9901 EMSG(_(e_listreq));
9902 return;
9903 }
9904 if (argvars[1].vval.v_list == NULL)
9905 return;
9906
9907 if (argvars[0].v_type == VAR_FUNC)
9908 func = argvars[0].vval.v_string;
9909 else
9910 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009911 if (*func == NUL)
9912 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009913
Bram Moolenaare9a41262005-01-15 22:18:47 +00009914 if (argvars[2].v_type != VAR_UNKNOWN)
9915 {
9916 if (argvars[2].v_type != VAR_DICT)
9917 {
9918 EMSG(_(e_dictreq));
9919 return;
9920 }
9921 selfdict = argvars[2].vval.v_dict;
9922 }
9923
Bram Moolenaardb913952012-06-29 12:54:53 +02009924 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009925}
9926
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009927#ifdef FEAT_FLOAT
9928/*
9929 * "ceil({float})" function
9930 */
9931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009932f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009934 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009935
9936 rettv->v_type = VAR_FLOAT;
9937 if (get_float_arg(argvars, &f) == OK)
9938 rettv->vval.v_float = ceil(f);
9939 else
9940 rettv->vval.v_float = 0.0;
9941}
9942#endif
9943
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009944#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009945/*
9946 * Get a callback from "arg". It can be a Funcref or a function name.
9947 * When "arg" is zero return an empty string.
9948 * Return NULL for an invalid argument.
9949 */
9950 static char_u *
9951get_callback(typval_T *arg)
9952{
9953 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9954 return arg->vval.v_string;
9955 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9956 return (char_u *)"";
9957 EMSG(_("E999: Invalid callback argument"));
9958 return NULL;
9959}
9960
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009961 static int
9962handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9963{
9964 char_u *val = get_tv_string(item);
9965
9966 opt->jo_set |= jo;
9967 if (STRCMP(val, "nl") == 0)
9968 *modep = MODE_NL;
9969 else if (STRCMP(val, "raw") == 0)
9970 *modep = MODE_RAW;
9971 else if (STRCMP(val, "js") == 0)
9972 *modep = MODE_JS;
9973 else if (STRCMP(val, "json") == 0)
9974 *modep = MODE_JSON;
9975 else
9976 {
9977 EMSG2(_(e_invarg2), val);
9978 return FAIL;
9979 }
9980 return OK;
9981}
9982
Bram Moolenaar187db502016-02-27 14:44:26 +01009983 static int
9984handle_io(typval_T *item, int part, jobopt_T *opt)
9985{
9986 char_u *val = get_tv_string(item);
9987
9988 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9989 if (STRCMP(val, "null") == 0)
9990 opt->jo_io[part] = JIO_NULL;
9991 else if (STRCMP(val, "pipe") == 0)
9992 opt->jo_io[part] = JIO_PIPE;
9993 else if (STRCMP(val, "file") == 0)
9994 opt->jo_io[part] = JIO_FILE;
9995 else if (STRCMP(val, "buffer") == 0)
9996 opt->jo_io[part] = JIO_BUFFER;
9997 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9998 opt->jo_io[part] = JIO_OUT;
9999 else
10000 {
10001 EMSG2(_(e_invarg2), val);
10002 return FAIL;
10003 }
10004 return OK;
10005}
10006
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010007 static void
10008clear_job_options(jobopt_T *opt)
10009{
10010 vim_memset(opt, 0, sizeof(jobopt_T));
10011}
10012
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010013/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010014 * Get the PART_ number from the first character of an option name.
10015 */
10016 static int
10017part_from_char(int c)
10018{
10019 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10020}
10021
10022/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010023 * Get the option entries from the dict in "tv", parse them and put the result
10024 * in "opt".
10025 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010026 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010027 */
10028 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010029get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010030{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010031 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010032 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010033 dict_T *dict;
10034 int todo;
10035 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010036 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010037
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010038 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010039 if (tv->v_type == VAR_UNKNOWN)
10040 return OK;
10041 if (tv->v_type != VAR_DICT)
10042 {
10043 EMSG(_(e_invarg));
10044 return FAIL;
10045 }
10046 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010047 if (dict == NULL)
10048 return OK;
10049
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010050 todo = (int)dict->dv_hashtab.ht_used;
10051 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10052 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010053 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010054 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010055
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010056 if (STRCMP(hi->hi_key, "mode") == 0)
10057 {
10058 if (!(supported & JO_MODE))
10059 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010060 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010061 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010062 }
10063 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10064 {
10065 if (!(supported & JO_IN_MODE))
10066 break;
10067 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10068 == FAIL)
10069 return FAIL;
10070 }
10071 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10072 {
10073 if (!(supported & JO_OUT_MODE))
10074 break;
10075 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10076 == FAIL)
10077 return FAIL;
10078 }
10079 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10080 {
10081 if (!(supported & JO_ERR_MODE))
10082 break;
10083 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10084 == FAIL)
10085 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010086 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010087 else if (STRCMP(hi->hi_key, "in-io") == 0
10088 || STRCMP(hi->hi_key, "out-io") == 0
10089 || STRCMP(hi->hi_key, "err-io") == 0)
10090 {
10091 if (!(supported & JO_OUT_IO))
10092 break;
10093 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10094 return FAIL;
10095 }
10096 else if (STRCMP(hi->hi_key, "in-name") == 0
10097 || STRCMP(hi->hi_key, "out-name") == 0
10098 || STRCMP(hi->hi_key, "err-name") == 0)
10099 {
10100 part = part_from_char(*hi->hi_key);
10101
10102 if (!(supported & JO_OUT_IO))
10103 break;
10104 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10105 opt->jo_io_name[part] =
10106 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10107 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010108 else if (STRCMP(hi->hi_key, "callback") == 0)
10109 {
10110 if (!(supported & JO_CALLBACK))
10111 break;
10112 opt->jo_set |= JO_CALLBACK;
10113 opt->jo_callback = get_callback(item);
10114 if (opt->jo_callback == NULL)
10115 {
10116 EMSG2(_(e_invarg2), "callback");
10117 return FAIL;
10118 }
10119 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010120 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10121 {
10122 if (!(supported & JO_OUT_CALLBACK))
10123 break;
10124 opt->jo_set |= JO_OUT_CALLBACK;
10125 opt->jo_out_cb = get_callback(item);
10126 if (opt->jo_out_cb == NULL)
10127 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010128 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010129 return FAIL;
10130 }
10131 }
10132 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10133 {
10134 if (!(supported & JO_ERR_CALLBACK))
10135 break;
10136 opt->jo_set |= JO_ERR_CALLBACK;
10137 opt->jo_err_cb = get_callback(item);
10138 if (opt->jo_err_cb == NULL)
10139 {
10140 EMSG2(_(e_invarg2), "err-cb");
10141 return FAIL;
10142 }
10143 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010144 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10145 {
10146 if (!(supported & JO_CLOSE_CALLBACK))
10147 break;
10148 opt->jo_set |= JO_CLOSE_CALLBACK;
10149 opt->jo_close_cb = get_callback(item);
10150 if (opt->jo_close_cb == NULL)
10151 {
10152 EMSG2(_(e_invarg2), "close-cb");
10153 return FAIL;
10154 }
10155 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010156 else if (STRCMP(hi->hi_key, "waittime") == 0)
10157 {
10158 if (!(supported & JO_WAITTIME))
10159 break;
10160 opt->jo_set |= JO_WAITTIME;
10161 opt->jo_waittime = get_tv_number(item);
10162 }
10163 else if (STRCMP(hi->hi_key, "timeout") == 0)
10164 {
10165 if (!(supported & JO_TIMEOUT))
10166 break;
10167 opt->jo_set |= JO_TIMEOUT;
10168 opt->jo_timeout = get_tv_number(item);
10169 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010170 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10171 {
10172 if (!(supported & JO_OUT_TIMEOUT))
10173 break;
10174 opt->jo_set |= JO_OUT_TIMEOUT;
10175 opt->jo_out_timeout = get_tv_number(item);
10176 }
10177 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10178 {
10179 if (!(supported & JO_ERR_TIMEOUT))
10180 break;
10181 opt->jo_set |= JO_ERR_TIMEOUT;
10182 opt->jo_err_timeout = get_tv_number(item);
10183 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010184 else if (STRCMP(hi->hi_key, "part") == 0)
10185 {
10186 if (!(supported & JO_PART))
10187 break;
10188 opt->jo_set |= JO_PART;
10189 val = get_tv_string(item);
10190 if (STRCMP(val, "err") == 0)
10191 opt->jo_part = PART_ERR;
10192 else
10193 {
10194 EMSG2(_(e_invarg2), val);
10195 return FAIL;
10196 }
10197 }
10198 else if (STRCMP(hi->hi_key, "id") == 0)
10199 {
10200 if (!(supported & JO_ID))
10201 break;
10202 opt->jo_set |= JO_ID;
10203 opt->jo_id = get_tv_number(item);
10204 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010205 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10206 {
10207 if (!(supported & JO_STOPONEXIT))
10208 break;
10209 opt->jo_set |= JO_STOPONEXIT;
10210 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10211 opt->jo_soe_buf);
10212 if (opt->jo_stoponexit == NULL)
10213 {
10214 EMSG2(_(e_invarg2), "stoponexit");
10215 return FAIL;
10216 }
10217 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010218 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10219 {
10220 if (!(supported & JO_EXIT_CB))
10221 break;
10222 opt->jo_set |= JO_EXIT_CB;
10223 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010224 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010225 {
10226 EMSG2(_(e_invarg2), "exit-cb");
10227 return FAIL;
10228 }
10229 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010230 else
10231 break;
10232 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010233 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010234 if (todo > 0)
10235 {
10236 EMSG2(_(e_invarg2), hi->hi_key);
10237 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010238 }
10239
Bram Moolenaar187db502016-02-27 14:44:26 +010010240 for (part = PART_OUT; part <= PART_IN; ++part)
10241 if (opt->jo_io[part] == JIO_BUFFER && opt->jo_io_name[part] == NULL)
10242 {
10243 EMSG(_("E915: Missing name for buffer"));
10244 return FAIL;
10245 }
10246
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010247 return OK;
10248}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010249#endif
10250
10251#ifdef FEAT_CHANNEL
10252/*
10253 * Get the channel from the argument.
10254 * Returns NULL if the handle is invalid.
10255 */
10256 static channel_T *
10257get_channel_arg(typval_T *tv)
10258{
10259 channel_T *channel;
10260
10261 if (tv->v_type != VAR_CHANNEL)
10262 {
10263 EMSG2(_(e_invarg2), get_tv_string(tv));
10264 return NULL;
10265 }
10266 channel = tv->vval.v_channel;
10267
10268 if (channel == NULL || !channel_is_open(channel))
10269 {
10270 EMSG(_("E906: not an open channel"));
10271 return NULL;
10272 }
10273 return channel;
10274}
10275
10276/*
10277 * "ch_close()" function
10278 */
10279 static void
10280f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10281{
10282 channel_T *channel = get_channel_arg(&argvars[0]);
10283
10284 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010285 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010286 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010287 channel_clear(channel);
10288 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010289}
10290
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010291# ifdef FEAT_JOB
10292/*
10293 * "ch_getjob()" function
10294 */
10295 static void
10296f_ch_getjob(typval_T *argvars, typval_T *rettv)
10297{
10298 channel_T *channel = get_channel_arg(&argvars[0]);
10299
10300 if (channel != NULL)
10301 {
10302 rettv->v_type = VAR_JOB;
10303 rettv->vval.v_job = channel->ch_job;
10304 if (channel->ch_job != NULL)
10305 ++channel->ch_job->jv_refcount;
10306 }
10307}
10308# endif
10309
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010310/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010311 * "ch_log()" function
10312 */
10313 static void
10314f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10315{
10316 char_u *msg = get_tv_string(&argvars[0]);
10317 channel_T *channel = NULL;
10318
10319 if (argvars[1].v_type != VAR_UNKNOWN)
10320 channel = get_channel_arg(&argvars[1]);
10321
10322 ch_log(channel, (char *)msg);
10323}
10324
10325/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010326 * "ch_logfile()" function
10327 */
10328 static void
10329f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10330{
10331 char_u *fname;
10332 char_u *opt = (char_u *)"";
10333 char_u buf[NUMBUFLEN];
10334 FILE *file = NULL;
10335
10336 fname = get_tv_string(&argvars[0]);
10337 if (argvars[1].v_type == VAR_STRING)
10338 opt = get_tv_string_buf(&argvars[1], buf);
10339 if (*fname != NUL)
10340 {
10341 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10342 if (file == NULL)
10343 {
10344 EMSG2(_(e_notopen), fname);
10345 return;
10346 }
10347 }
10348 ch_logfile(file);
10349}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010350
10351/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010352 * "ch_open()" function
10353 */
10354 static void
10355f_ch_open(typval_T *argvars, typval_T *rettv)
10356{
10357 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010358 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010359 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010360 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010361 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010362 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010363
10364 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010365 rettv->v_type = VAR_CHANNEL;
10366 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010367
10368 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010369 if (argvars[1].v_type != VAR_UNKNOWN
10370 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010371 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010372 EMSG(_(e_invarg));
10373 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010374 }
10375
10376 /* parse address */
10377 p = vim_strchr(address, ':');
10378 if (p == NULL)
10379 {
10380 EMSG2(_(e_invarg2), address);
10381 return;
10382 }
10383 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010384 port = strtol((char *)p, &rest, 10);
10385 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010386 {
10387 p[-1] = ':';
10388 EMSG2(_(e_invarg2), address);
10389 return;
10390 }
10391
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010392 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010393 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010394 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010395 opt.jo_timeout = 2000;
10396 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010397 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010398 return;
10399 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010400 {
10401 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010402 return;
10403 }
10404
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010405 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010406 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010407 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010408 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010409 opt.jo_set = JO_ALL;
10410 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010411 }
10412}
10413
10414/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010415 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010416 */
10417 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010418common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010419{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010420 channel_T *channel;
10421 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010422 jobopt_T opt;
10423 int mode;
10424 int timeout;
10425 int id = -1;
10426 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010427
10428 /* return an empty string by default */
10429 rettv->v_type = VAR_STRING;
10430 rettv->vval.v_string = NULL;
10431
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010432 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010433 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10434 == FAIL)
10435 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010436
Bram Moolenaar77073442016-02-13 23:23:53 +010010437 channel = get_channel_arg(&argvars[0]);
10438 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010439 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010440 if (opt.jo_set & JO_PART)
10441 part = opt.jo_part;
10442 else
10443 part = channel_part_read(channel);
10444 mode = channel_get_mode(channel, part);
10445 timeout = channel_get_timeout(channel, part);
10446 if (opt.jo_set & JO_TIMEOUT)
10447 timeout = opt.jo_timeout;
10448
10449 if (raw || mode == MODE_RAW || mode == MODE_NL)
10450 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10451 else
10452 {
10453 if (opt.jo_set & JO_ID)
10454 id = opt.jo_id;
10455 channel_read_json_block(channel, part, timeout, id, &listtv);
10456 if (listtv != NULL)
10457 *rettv = *listtv;
10458 else
10459 {
10460 rettv->v_type = VAR_SPECIAL;
10461 rettv->vval.v_number = VVAL_NONE;
10462 }
10463 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010464 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010465}
10466
10467/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010468 * "ch_read()" function
10469 */
10470 static void
10471f_ch_read(typval_T *argvars, typval_T *rettv)
10472{
10473 common_channel_read(argvars, rettv, FALSE);
10474}
10475
10476/*
10477 * "ch_readraw()" function
10478 */
10479 static void
10480f_ch_readraw(typval_T *argvars, typval_T *rettv)
10481{
10482 common_channel_read(argvars, rettv, TRUE);
10483}
10484
10485/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010486 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010487 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010488 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010489 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010490 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010491 static channel_T *
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010492send_common(
10493 typval_T *argvars,
10494 char_u *text,
10495 int id,
10496 int eval,
10497 char *fun,
10498 int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010499{
Bram Moolenaar77073442016-02-13 23:23:53 +010010500 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010501 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010502 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010503
Bram Moolenaar77073442016-02-13 23:23:53 +010010504 channel = get_channel_arg(&argvars[0]);
10505 if (channel == NULL)
10506 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010507 part_send = channel_part_send(channel);
10508 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010509
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010510 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010511 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10512 return NULL;
10513
Bram Moolenaara07fec92016-02-05 21:04:08 +010010514 /* Set the callback. An empty callback means no callback and not reading
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010515 * the response. With "ch_evalexpr()" and "ch_evalraw()" a callback is not
10516 * allowed. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010517 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010518 {
10519 if (eval)
10520 {
10521 EMSG2(_("E917: Cannot use a callback with %s()"), fun);
10522 return NULL;
10523 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010524 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010525 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010526
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010527 if (channel_send(channel, part_send, text, fun) == OK
10528 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010529 return channel;
10530 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010531}
10532
10533/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010534 * common for "ch_evalexpr()" and "ch_sendexpr()"
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010535 */
10536 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010537ch_expr_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010538{
10539 char_u *text;
10540 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010541 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010542 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010543 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010544 int part_send;
10545 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010546 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010547
10548 /* return an empty string by default */
10549 rettv->v_type = VAR_STRING;
10550 rettv->vval.v_string = NULL;
10551
Bram Moolenaar77073442016-02-13 23:23:53 +010010552 channel = get_channel_arg(&argvars[0]);
10553 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010554 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010555 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010556
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010557 ch_mode = channel_get_mode(channel, part_send);
10558 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010559 {
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010560 EMSG(_("E912: cannot use ch_evalexpr()/ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010561 return;
10562 }
10563
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010564 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010565 text = json_encode_nr_expr(id, &argvars[1],
10566 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010567 if (text == NULL)
10568 return;
10569
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010570 channel = send_common(argvars, text, id, eval,
10571 eval ? "ch_evalexpr" : "ch_sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010572 vim_free(text);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010573 if (channel != NULL && eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010574 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010575 /* TODO: timeout from options */
10576 timeout = channel_get_timeout(channel, part_read);
10577 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10578 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010579 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010580 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010581
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010582 /* Move the item from the list and then change the type to
10583 * avoid the value being freed. */
10584 *rettv = list->lv_last->li_tv;
10585 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010586 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010587 }
10588 }
10589}
10590
10591/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010592 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010593 */
10594 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010595f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10596{
10597 ch_expr_common(argvars, rettv, TRUE);
10598}
10599
10600/*
10601 * "ch_sendexpr()" function
10602 */
10603 static void
10604f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10605{
10606 ch_expr_common(argvars, rettv, FALSE);
10607}
10608
10609/*
10610 * common for "ch_evalraw()" and "ch_sendraw()"
10611 */
10612 static void
10613ch_raw_common(typval_T *argvars, typval_T *rettv, int eval)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010614{
10615 char_u buf[NUMBUFLEN];
10616 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010617 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010618 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010619 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010620
10621 /* return an empty string by default */
10622 rettv->v_type = VAR_STRING;
10623 rettv->vval.v_string = NULL;
10624
10625 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010626 channel = send_common(argvars, text, 0, eval,
10627 eval ? "ch_evalraw" : "ch_sendraw", &part_read);
10628 if (channel != NULL && eval)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010629 {
10630 /* TODO: timeout from options */
10631 timeout = channel_get_timeout(channel, part_read);
10632 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10633 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010634}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010635
10636/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010637 * "ch_evalraw()" function
10638 */
10639 static void
10640f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10641{
10642 ch_raw_common(argvars, rettv, TRUE);
10643}
10644
10645/*
10646 * "ch_sendraw()" function
10647 */
10648 static void
10649f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10650{
10651 ch_raw_common(argvars, rettv, FALSE);
10652}
10653
10654/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010655 * "ch_setoptions()" function
10656 */
10657 static void
10658f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10659{
10660 channel_T *channel;
10661 jobopt_T opt;
10662
10663 channel = get_channel_arg(&argvars[0]);
10664 if (channel == NULL)
10665 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010666 clear_job_options(&opt);
10667 if (get_job_options(&argvars[1], &opt,
10668 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010669 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010670 channel_set_options(channel, &opt);
10671}
10672
10673/*
10674 * "ch_status()" function
10675 */
10676 static void
10677f_ch_status(typval_T *argvars, typval_T *rettv)
10678{
10679 /* return an empty string by default */
10680 rettv->v_type = VAR_STRING;
10681
10682 if (argvars[0].v_type != VAR_CHANNEL)
10683 {
10684 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10685 rettv->vval.v_string = NULL;
10686 }
10687 else
10688 rettv->vval.v_string = vim_strsave(
10689 (char_u *)channel_status(argvars[0].vval.v_channel));
10690}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010691#endif
10692
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010693/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010694 * "changenr()" function
10695 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010697f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010698{
10699 rettv->vval.v_number = curbuf->b_u_seq_cur;
10700}
10701
10702/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703 * "char2nr(string)" function
10704 */
10705 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010706f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010707{
10708#ifdef FEAT_MBYTE
10709 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010710 {
10711 int utf8 = 0;
10712
10713 if (argvars[1].v_type != VAR_UNKNOWN)
10714 utf8 = get_tv_number_chk(&argvars[1], NULL);
10715
10716 if (utf8)
10717 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10718 else
10719 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10720 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010721 else
10722#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010723 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
10726/*
10727 * "cindent(lnum)" function
10728 */
10729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010730f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732#ifdef FEAT_CINDENT
10733 pos_T pos;
10734 linenr_T lnum;
10735
10736 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010737 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010738 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10739 {
10740 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010741 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 curwin->w_cursor = pos;
10743 }
10744 else
10745#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747}
10748
10749/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010750 * "clearmatches()" function
10751 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010753f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010754{
10755#ifdef FEAT_SEARCH_EXTRA
10756 clear_matches(curwin);
10757#endif
10758}
10759
10760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010761 * "col(string)" function
10762 */
10763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010764f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010765{
10766 colnr_T col = 0;
10767 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010768 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010769
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010770 fp = var2fpos(&argvars[0], FALSE, &fnum);
10771 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010772 {
10773 if (fp->col == MAXCOL)
10774 {
10775 /* '> can be MAXCOL, get the length of the line then */
10776 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010777 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 else
10779 col = MAXCOL;
10780 }
10781 else
10782 {
10783 col = fp->col + 1;
10784#ifdef FEAT_VIRTUALEDIT
10785 /* col(".") when the cursor is on the NUL at the end of the line
10786 * because of "coladd" can be seen as an extra column. */
10787 if (virtual_active() && fp == &curwin->w_cursor)
10788 {
10789 char_u *p = ml_get_cursor();
10790
10791 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10792 curwin->w_virtcol - curwin->w_cursor.coladd))
10793 {
10794# ifdef FEAT_MBYTE
10795 int l;
10796
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010797 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010798 col += l;
10799# else
10800 if (*p != NUL && p[1] == NUL)
10801 ++col;
10802# endif
10803 }
10804 }
10805#endif
10806 }
10807 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010808 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010809}
10810
Bram Moolenaar572cb562005-08-05 21:35:02 +000010811#if defined(FEAT_INS_EXPAND)
10812/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010813 * "complete()" function
10814 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010816f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010817{
10818 int startcol;
10819
10820 if ((State & INSERT) == 0)
10821 {
10822 EMSG(_("E785: complete() can only be used in Insert mode"));
10823 return;
10824 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010825
10826 /* Check for undo allowed here, because if something was already inserted
10827 * the line was already saved for undo and this check isn't done. */
10828 if (!undo_allowed())
10829 return;
10830
Bram Moolenaarade00832006-03-10 21:46:58 +000010831 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10832 {
10833 EMSG(_(e_invarg));
10834 return;
10835 }
10836
10837 startcol = get_tv_number_chk(&argvars[0], NULL);
10838 if (startcol <= 0)
10839 return;
10840
10841 set_completion(startcol - 1, argvars[1].vval.v_list);
10842}
10843
10844/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010845 * "complete_add()" function
10846 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010848f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010849{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010850 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010851}
10852
10853/*
10854 * "complete_check()" function
10855 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010857f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010858{
10859 int saved = RedrawingDisabled;
10860
10861 RedrawingDisabled = 0;
10862 ins_compl_check_keys(0);
10863 rettv->vval.v_number = compl_interrupted;
10864 RedrawingDisabled = saved;
10865}
10866#endif
10867
Bram Moolenaar071d4272004-06-13 20:20:40 +000010868/*
10869 * "confirm(message, buttons[, default [, type]])" function
10870 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010872f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
10874#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10875 char_u *message;
10876 char_u *buttons = NULL;
10877 char_u buf[NUMBUFLEN];
10878 char_u buf2[NUMBUFLEN];
10879 int def = 1;
10880 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010881 char_u *typestr;
10882 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010883
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010884 message = get_tv_string_chk(&argvars[0]);
10885 if (message == NULL)
10886 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010887 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010888 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010889 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10890 if (buttons == NULL)
10891 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010892 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010893 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010894 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010895 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010897 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10898 if (typestr == NULL)
10899 error = TRUE;
10900 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010901 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010902 switch (TOUPPER_ASC(*typestr))
10903 {
10904 case 'E': type = VIM_ERROR; break;
10905 case 'Q': type = VIM_QUESTION; break;
10906 case 'I': type = VIM_INFO; break;
10907 case 'W': type = VIM_WARNING; break;
10908 case 'G': type = VIM_GENERIC; break;
10909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010910 }
10911 }
10912 }
10913 }
10914
10915 if (buttons == NULL || *buttons == NUL)
10916 buttons = (char_u *)_("&Ok");
10917
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010918 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010919 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010920 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921#endif
10922}
10923
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010924/*
10925 * "copy()" function
10926 */
10927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010928f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010929{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010930 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010931}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010933#ifdef FEAT_FLOAT
10934/*
10935 * "cos()" function
10936 */
10937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010938f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010941
10942 rettv->v_type = VAR_FLOAT;
10943 if (get_float_arg(argvars, &f) == OK)
10944 rettv->vval.v_float = cos(f);
10945 else
10946 rettv->vval.v_float = 0.0;
10947}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010948
10949/*
10950 * "cosh()" function
10951 */
10952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010953f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010954{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010955 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010956
10957 rettv->v_type = VAR_FLOAT;
10958 if (get_float_arg(argvars, &f) == OK)
10959 rettv->vval.v_float = cosh(f);
10960 else
10961 rettv->vval.v_float = 0.0;
10962}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010963#endif
10964
Bram Moolenaar071d4272004-06-13 20:20:40 +000010965/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010966 * "count()" function
10967 */
10968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010969f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010970{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010971 long n = 0;
10972 int ic = FALSE;
10973
Bram Moolenaare9a41262005-01-15 22:18:47 +000010974 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010975 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010976 listitem_T *li;
10977 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010979
Bram Moolenaare9a41262005-01-15 22:18:47 +000010980 if ((l = argvars[0].vval.v_list) != NULL)
10981 {
10982 li = l->lv_first;
10983 if (argvars[2].v_type != VAR_UNKNOWN)
10984 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010985 int error = FALSE;
10986
10987 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010988 if (argvars[3].v_type != VAR_UNKNOWN)
10989 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010990 idx = get_tv_number_chk(&argvars[3], &error);
10991 if (!error)
10992 {
10993 li = list_find(l, idx);
10994 if (li == NULL)
10995 EMSGN(_(e_listidx), idx);
10996 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010997 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010998 if (error)
10999 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011000 }
11001
11002 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011003 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011004 ++n;
11005 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011006 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011007 else if (argvars[0].v_type == VAR_DICT)
11008 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011009 int todo;
11010 dict_T *d;
11011 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011012
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011013 if ((d = argvars[0].vval.v_dict) != NULL)
11014 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011015 int error = FALSE;
11016
Bram Moolenaare9a41262005-01-15 22:18:47 +000011017 if (argvars[2].v_type != VAR_UNKNOWN)
11018 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011019 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011020 if (argvars[3].v_type != VAR_UNKNOWN)
11021 EMSG(_(e_invarg));
11022 }
11023
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011024 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000011025 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011026 {
11027 if (!HASHITEM_EMPTY(hi))
11028 {
11029 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010011030 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011031 ++n;
11032 }
11033 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011034 }
11035 }
11036 else
11037 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011038 rettv->vval.v_number = n;
11039}
11040
11041/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011042 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
11043 *
11044 * Checks the existence of a cscope connection.
11045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011047f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049#ifdef FEAT_CSCOPE
11050 int num = 0;
11051 char_u *dbpath = NULL;
11052 char_u *prepend = NULL;
11053 char_u buf[NUMBUFLEN];
11054
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011055 if (argvars[0].v_type != VAR_UNKNOWN
11056 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011058 num = (int)get_tv_number(&argvars[0]);
11059 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011060 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011061 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011064 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065#endif
11066}
11067
11068/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011069 * "cursor(lnum, col)" function, or
11070 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011072 * Moves the cursor to the specified line and column.
11073 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011074 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011076f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077{
11078 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011079#ifdef FEAT_VIRTUALEDIT
11080 long coladd = 0;
11081#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011082 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011084 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011085 if (argvars[1].v_type == VAR_UNKNOWN)
11086 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011087 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011088 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011089
Bram Moolenaar493c1782014-05-28 14:34:46 +020011090 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011091 {
11092 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011093 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011094 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011095 line = pos.lnum;
11096 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011097#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011098 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011099#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011100 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011101 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011102 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011103 set_curswant = FALSE;
11104 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011105 }
11106 else
11107 {
11108 line = get_tv_lnum(argvars);
11109 col = get_tv_number_chk(&argvars[1], NULL);
11110#ifdef FEAT_VIRTUALEDIT
11111 if (argvars[2].v_type != VAR_UNKNOWN)
11112 coladd = get_tv_number_chk(&argvars[2], NULL);
11113#endif
11114 }
11115 if (line < 0 || col < 0
11116#ifdef FEAT_VIRTUALEDIT
11117 || coladd < 0
11118#endif
11119 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011120 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 if (line > 0)
11122 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011123 if (col > 0)
11124 curwin->w_cursor.col = col - 1;
11125#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011126 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127#endif
11128
11129 /* Make sure the cursor is in a valid position. */
11130 check_cursor();
11131#ifdef FEAT_MBYTE
11132 /* Correct cursor for multi-byte character. */
11133 if (has_mbyte)
11134 mb_adjust_cursor();
11135#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011136
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011137 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011138 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011139}
11140
11141/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011142 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011143 */
11144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011145f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011147 int noref = 0;
11148
11149 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011150 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011151 if (noref < 0 || noref > 1)
11152 EMSG(_(e_invarg));
11153 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011154 {
11155 current_copyID += COPYID_INC;
11156 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11157 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158}
11159
11160/*
11161 * "delete()" function
11162 */
11163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011164f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011165{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011166 char_u nbuf[NUMBUFLEN];
11167 char_u *name;
11168 char_u *flags;
11169
11170 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011171 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011172 return;
11173
11174 name = get_tv_string(&argvars[0]);
11175 if (name == NULL || *name == NUL)
11176 {
11177 EMSG(_(e_invarg));
11178 return;
11179 }
11180
11181 if (argvars[1].v_type != VAR_UNKNOWN)
11182 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011183 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011184 flags = (char_u *)"";
11185
11186 if (*flags == NUL)
11187 /* delete a file */
11188 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11189 else if (STRCMP(flags, "d") == 0)
11190 /* delete an empty directory */
11191 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11192 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011193 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011194 rettv->vval.v_number = delete_recursive(name);
11195 else
11196 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011197}
11198
11199/*
11200 * "did_filetype()" function
11201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011203f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011204{
11205#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011206 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011207#endif
11208}
11209
11210/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011211 * "diff_filler()" function
11212 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011214f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011215{
11216#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011217 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011218#endif
11219}
11220
11221/*
11222 * "diff_hlID()" function
11223 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011225f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011226{
11227#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011228 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011229 static linenr_T prev_lnum = 0;
11230 static int changedtick = 0;
11231 static int fnum = 0;
11232 static int change_start = 0;
11233 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011234 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011235 int filler_lines;
11236 int col;
11237
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011238 if (lnum < 0) /* ignore type error in {lnum} arg */
11239 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011240 if (lnum != prev_lnum
11241 || changedtick != curbuf->b_changedtick
11242 || fnum != curbuf->b_fnum)
11243 {
11244 /* New line, buffer, change: need to get the values. */
11245 filler_lines = diff_check(curwin, lnum);
11246 if (filler_lines < 0)
11247 {
11248 if (filler_lines == -1)
11249 {
11250 change_start = MAXCOL;
11251 change_end = -1;
11252 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11253 hlID = HLF_ADD; /* added line */
11254 else
11255 hlID = HLF_CHD; /* changed line */
11256 }
11257 else
11258 hlID = HLF_ADD; /* added line */
11259 }
11260 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011261 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011262 prev_lnum = lnum;
11263 changedtick = curbuf->b_changedtick;
11264 fnum = curbuf->b_fnum;
11265 }
11266
11267 if (hlID == HLF_CHD || hlID == HLF_TXD)
11268 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011269 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011270 if (col >= change_start && col <= change_end)
11271 hlID = HLF_TXD; /* changed text */
11272 else
11273 hlID = HLF_CHD; /* changed line */
11274 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011275 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011276#endif
11277}
11278
11279/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011280 * "disable_char_avail_for_testing({expr})" function
11281 */
11282 static void
11283f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11284{
11285 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11286}
11287
11288/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011289 * "empty({expr})" function
11290 */
11291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011292f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011293{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011294 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011295
11296 switch (argvars[0].v_type)
11297 {
11298 case VAR_STRING:
11299 case VAR_FUNC:
11300 n = argvars[0].vval.v_string == NULL
11301 || *argvars[0].vval.v_string == NUL;
11302 break;
11303 case VAR_NUMBER:
11304 n = argvars[0].vval.v_number == 0;
11305 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011306 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011307#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011308 n = argvars[0].vval.v_float == 0.0;
11309 break;
11310#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011311 case VAR_LIST:
11312 n = argvars[0].vval.v_list == NULL
11313 || argvars[0].vval.v_list->lv_first == NULL;
11314 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011315 case VAR_DICT:
11316 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011317 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011318 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011319 case VAR_SPECIAL:
11320 n = argvars[0].vval.v_number != VVAL_TRUE;
11321 break;
11322
Bram Moolenaar835dc632016-02-07 14:27:38 +010011323 case VAR_JOB:
11324#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011325 n = argvars[0].vval.v_job == NULL
11326 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11327 break;
11328#endif
11329 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011330#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011331 n = argvars[0].vval.v_channel == NULL
11332 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011333 break;
11334#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011335 case VAR_UNKNOWN:
11336 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11337 n = TRUE;
11338 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011339 }
11340
11341 rettv->vval.v_number = n;
11342}
11343
11344/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011345 * "escape({string}, {chars})" function
11346 */
11347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011348f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011349{
11350 char_u buf[NUMBUFLEN];
11351
Bram Moolenaar758711c2005-02-02 23:11:38 +000011352 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11353 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011354 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011355}
11356
11357/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011358 * "eval()" function
11359 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011361f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011362{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011363 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011364
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011365 s = get_tv_string_chk(&argvars[0]);
11366 if (s != NULL)
11367 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011368
Bram Moolenaar615b9972015-01-14 17:15:05 +010011369 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011370 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11371 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011372 if (p != NULL && !aborting())
11373 EMSG2(_(e_invexpr2), p);
11374 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011375 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011376 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011377 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011378 else if (*s != NUL)
11379 EMSG(_(e_trailing));
11380}
11381
11382/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 * "eventhandler()" function
11384 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011386f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011387{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011388 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011389}
11390
11391/*
11392 * "executable()" function
11393 */
11394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011395f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011397 char_u *name = get_tv_string(&argvars[0]);
11398
11399 /* Check in $PATH and also check directly if there is a directory name. */
11400 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11401 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011402}
11403
11404/*
11405 * "exepath()" function
11406 */
11407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011408f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011409{
11410 char_u *p = NULL;
11411
Bram Moolenaarb5971142015-03-21 17:32:19 +010011412 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011413 rettv->v_type = VAR_STRING;
11414 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011415}
11416
11417/*
11418 * "exists()" function
11419 */
11420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011421f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011422{
11423 char_u *p;
11424 char_u *name;
11425 int n = FALSE;
11426 int len = 0;
11427
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011428 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 if (*p == '$') /* environment variable */
11430 {
11431 /* first try "normal" environment variables (fast) */
11432 if (mch_getenv(p + 1) != NULL)
11433 n = TRUE;
11434 else
11435 {
11436 /* try expanding things like $VIM and ${HOME} */
11437 p = expand_env_save(p);
11438 if (p != NULL && *p != '$')
11439 n = TRUE;
11440 vim_free(p);
11441 }
11442 }
11443 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011444 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011445 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011446 if (*skipwhite(p) != NUL)
11447 n = FALSE; /* trailing garbage */
11448 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011449 else if (*p == '*') /* internal or user defined function */
11450 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011451 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011452 }
11453 else if (*p == ':')
11454 {
11455 n = cmd_exists(p + 1);
11456 }
11457 else if (*p == '#')
11458 {
11459#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011460 if (p[1] == '#')
11461 n = autocmd_supported(p + 2);
11462 else
11463 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011464#endif
11465 }
11466 else /* internal variable */
11467 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011468 char_u *tofree;
11469 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011471 /* get_name_len() takes care of expanding curly braces */
11472 name = p;
11473 len = get_name_len(&p, &tofree, TRUE, FALSE);
11474 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011476 if (tofree != NULL)
11477 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011478 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011479 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011480 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011481 /* handle d.key, l[idx], f(expr) */
11482 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11483 if (n)
11484 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011485 }
11486 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011487 if (*p != NUL)
11488 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011489
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011490 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011491 }
11492
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011493 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011494}
11495
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011496#ifdef FEAT_FLOAT
11497/*
11498 * "exp()" function
11499 */
11500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011501f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011502{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011503 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011504
11505 rettv->v_type = VAR_FLOAT;
11506 if (get_float_arg(argvars, &f) == OK)
11507 rettv->vval.v_float = exp(f);
11508 else
11509 rettv->vval.v_float = 0.0;
11510}
11511#endif
11512
Bram Moolenaar071d4272004-06-13 20:20:40 +000011513/*
11514 * "expand()" function
11515 */
11516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011517f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011518{
11519 char_u *s;
11520 int len;
11521 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011522 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011523 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011524 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011525 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011526
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011527 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011528 if (argvars[1].v_type != VAR_UNKNOWN
11529 && argvars[2].v_type != VAR_UNKNOWN
11530 && get_tv_number_chk(&argvars[2], &error)
11531 && !error)
11532 {
11533 rettv->v_type = VAR_LIST;
11534 rettv->vval.v_list = NULL;
11535 }
11536
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011537 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011538 if (*s == '%' || *s == '#' || *s == '<')
11539 {
11540 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011541 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011542 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011543 if (rettv->v_type == VAR_LIST)
11544 {
11545 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11546 list_append_string(rettv->vval.v_list, result, -1);
11547 else
11548 vim_free(result);
11549 }
11550 else
11551 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011552 }
11553 else
11554 {
11555 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011556 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011557 if (argvars[1].v_type != VAR_UNKNOWN
11558 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011559 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011560 if (!error)
11561 {
11562 ExpandInit(&xpc);
11563 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011564 if (p_wic)
11565 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011566 if (rettv->v_type == VAR_STRING)
11567 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11568 options, WILD_ALL);
11569 else if (rettv_list_alloc(rettv) != FAIL)
11570 {
11571 int i;
11572
11573 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11574 for (i = 0; i < xpc.xp_numfiles; i++)
11575 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11576 ExpandCleanup(&xpc);
11577 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011578 }
11579 else
11580 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011581 }
11582}
11583
11584/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011585 * Go over all entries in "d2" and add them to "d1".
11586 * When "action" is "error" then a duplicate key is an error.
11587 * When "action" is "force" then a duplicate key is overwritten.
11588 * Otherwise duplicate keys are ignored ("action" is "keep").
11589 */
11590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011591dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011592{
11593 dictitem_T *di1;
11594 hashitem_T *hi2;
11595 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011596 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011597
11598 todo = (int)d2->dv_hashtab.ht_used;
11599 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11600 {
11601 if (!HASHITEM_EMPTY(hi2))
11602 {
11603 --todo;
11604 di1 = dict_find(d1, hi2->hi_key, -1);
11605 if (d1->dv_scope != 0)
11606 {
11607 /* Disallow replacing a builtin function in l: and g:.
11608 * Check the key to be valid when adding to any
11609 * scope. */
11610 if (d1->dv_scope == VAR_DEF_SCOPE
11611 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11612 && var_check_func_name(hi2->hi_key,
11613 di1 == NULL))
11614 break;
11615 if (!valid_varname(hi2->hi_key))
11616 break;
11617 }
11618 if (di1 == NULL)
11619 {
11620 di1 = dictitem_copy(HI2DI(hi2));
11621 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11622 dictitem_free(di1);
11623 }
11624 else if (*action == 'e')
11625 {
11626 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11627 break;
11628 }
11629 else if (*action == 'f' && HI2DI(hi2) != di1)
11630 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011631 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11632 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011633 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011634 clear_tv(&di1->di_tv);
11635 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11636 }
11637 }
11638 }
11639}
11640
11641/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011642 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011643 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011644 */
11645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011646f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011647{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011648 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011649
Bram Moolenaare9a41262005-01-15 22:18:47 +000011650 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011651 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011652 list_T *l1, *l2;
11653 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011654 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011655 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011656
Bram Moolenaare9a41262005-01-15 22:18:47 +000011657 l1 = argvars[0].vval.v_list;
11658 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011659 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011660 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011661 {
11662 if (argvars[2].v_type != VAR_UNKNOWN)
11663 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011664 before = get_tv_number_chk(&argvars[2], &error);
11665 if (error)
11666 return; /* type error; errmsg already given */
11667
Bram Moolenaar758711c2005-02-02 23:11:38 +000011668 if (before == l1->lv_len)
11669 item = NULL;
11670 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011671 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011672 item = list_find(l1, before);
11673 if (item == NULL)
11674 {
11675 EMSGN(_(e_listidx), before);
11676 return;
11677 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011678 }
11679 }
11680 else
11681 item = NULL;
11682 list_extend(l1, l2, item);
11683
Bram Moolenaare9a41262005-01-15 22:18:47 +000011684 copy_tv(&argvars[0], rettv);
11685 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011686 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011687 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11688 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011689 dict_T *d1, *d2;
11690 char_u *action;
11691 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011692
11693 d1 = argvars[0].vval.v_dict;
11694 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011695 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011696 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011697 {
11698 /* Check the third argument. */
11699 if (argvars[2].v_type != VAR_UNKNOWN)
11700 {
11701 static char *(av[]) = {"keep", "force", "error"};
11702
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011703 action = get_tv_string_chk(&argvars[2]);
11704 if (action == NULL)
11705 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011706 for (i = 0; i < 3; ++i)
11707 if (STRCMP(action, av[i]) == 0)
11708 break;
11709 if (i == 3)
11710 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011711 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011712 return;
11713 }
11714 }
11715 else
11716 action = (char_u *)"force";
11717
Bram Moolenaara9922d62013-05-30 13:01:18 +020011718 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011719
Bram Moolenaare9a41262005-01-15 22:18:47 +000011720 copy_tv(&argvars[0], rettv);
11721 }
11722 }
11723 else
11724 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011725}
11726
11727/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011728 * "feedkeys()" function
11729 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011731f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011732{
11733 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011734 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011735 char_u *keys, *flags;
11736 char_u nbuf[NUMBUFLEN];
11737 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011738 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011739 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011740
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011741 /* This is not allowed in the sandbox. If the commands would still be
11742 * executed in the sandbox it would be OK, but it probably happens later,
11743 * when "sandbox" is no longer set. */
11744 if (check_secure())
11745 return;
11746
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011747 keys = get_tv_string(&argvars[0]);
11748 if (*keys != NUL)
11749 {
11750 if (argvars[1].v_type != VAR_UNKNOWN)
11751 {
11752 flags = get_tv_string_buf(&argvars[1], nbuf);
11753 for ( ; *flags != NUL; ++flags)
11754 {
11755 switch (*flags)
11756 {
11757 case 'n': remap = FALSE; break;
11758 case 'm': remap = TRUE; break;
11759 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011760 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011761 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011762 }
11763 }
11764 }
11765
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011766 /* Need to escape K_SPECIAL and CSI before putting the string in the
11767 * typeahead buffer. */
11768 keys_esc = vim_strsave_escape_csi(keys);
11769 if (keys_esc != NULL)
11770 {
11771 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011772 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011773 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011774 if (vgetc_busy)
11775 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011776 if (execute)
11777 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011778 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011779 }
11780}
11781
11782/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 * "filereadable()" function
11784 */
11785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011786f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011788 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 char_u *p;
11790 int n;
11791
Bram Moolenaarc236c162008-07-13 17:41:49 +000011792#ifndef O_NONBLOCK
11793# define O_NONBLOCK 0
11794#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011796 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11797 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 {
11799 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011800 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011801 }
11802 else
11803 n = FALSE;
11804
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011805 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806}
11807
11808/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011809 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810 * rights to write into.
11811 */
11812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011813f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011814{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011815 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011816}
11817
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011818 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011819findfilendir(
11820 typval_T *argvars UNUSED,
11821 typval_T *rettv,
11822 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011823{
11824#ifdef FEAT_SEARCHPATH
11825 char_u *fname;
11826 char_u *fresult = NULL;
11827 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11828 char_u *p;
11829 char_u pathbuf[NUMBUFLEN];
11830 int count = 1;
11831 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011832 int error = FALSE;
11833#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011834
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011835 rettv->vval.v_string = NULL;
11836 rettv->v_type = VAR_STRING;
11837
11838#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011839 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011840
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011841 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011843 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11844 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011845 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011846 else
11847 {
11848 if (*p != NUL)
11849 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011850
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011851 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011852 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011853 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011854 }
11855
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011856 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11857 error = TRUE;
11858
11859 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011860 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011861 do
11862 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011863 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011864 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011865 fresult = find_file_in_path_option(first ? fname : NULL,
11866 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011867 0, first, path,
11868 find_what,
11869 curbuf->b_ffname,
11870 find_what == FINDFILE_DIR
11871 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011872 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011873
11874 if (fresult != NULL && rettv->v_type == VAR_LIST)
11875 list_append_string(rettv->vval.v_list, fresult, -1);
11876
11877 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011878 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011879
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011880 if (rettv->v_type == VAR_STRING)
11881 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011882#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011883}
11884
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011885static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11886static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011887
11888/*
11889 * Implementation of map() and filter().
11890 */
11891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011892filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011893{
11894 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011895 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011896 listitem_T *li, *nli;
11897 list_T *l = NULL;
11898 dictitem_T *di;
11899 hashtab_T *ht;
11900 hashitem_T *hi;
11901 dict_T *d = NULL;
11902 typval_T save_val;
11903 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011904 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011905 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011906 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011907 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011908 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011909 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011910 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011911
Bram Moolenaare9a41262005-01-15 22:18:47 +000011912 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011913 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011914 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011915 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011916 return;
11917 }
11918 else if (argvars[0].v_type == VAR_DICT)
11919 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011920 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011921 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011922 return;
11923 }
11924 else
11925 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011926 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011927 return;
11928 }
11929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011930 expr = get_tv_string_buf_chk(&argvars[1], buf);
11931 /* On type errors, the preceding call has already displayed an error
11932 * message. Avoid a misleading error message for an empty string that
11933 * was not passed as argument. */
11934 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011935 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011936 prepare_vimvar(VV_VAL, &save_val);
11937 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011938
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011939 /* We reset "did_emsg" to be able to detect whether an error
11940 * occurred during evaluation of the expression. */
11941 save_did_emsg = did_emsg;
11942 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011943
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011944 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011945 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011946 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011947 vimvars[VV_KEY].vv_type = VAR_STRING;
11948
11949 ht = &d->dv_hashtab;
11950 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011951 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011952 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011953 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011954 if (!HASHITEM_EMPTY(hi))
11955 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011956 int r;
11957
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011958 --todo;
11959 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011960 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011961 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11962 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011963 break;
11964 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011965 r = filter_map_one(&di->di_tv, expr, map, &rem);
11966 clear_tv(&vimvars[VV_KEY].vv_tv);
11967 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011968 break;
11969 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011970 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011971 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11972 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011973 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011974 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011975 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011976 }
11977 }
11978 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011979 }
11980 else
11981 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011982 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11983
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011984 for (li = l->lv_first; li != NULL; li = nli)
11985 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011986 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011987 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011988 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011989 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011990 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011991 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011992 break;
11993 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011994 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011995 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011996 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011997 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011998
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011999 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012000 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012001
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000012002 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012003 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012004
12005 copy_tv(&argvars[0], rettv);
12006}
12007
12008 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010012009filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012010{
Bram Moolenaar33570922005-01-25 22:26:29 +000012011 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012012 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012013 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012014
Bram Moolenaar33570922005-01-25 22:26:29 +000012015 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012016 s = expr;
12017 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012018 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012019 if (*s != NUL) /* check for trailing chars after expr */
12020 {
12021 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010012022 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012023 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012024 }
12025 if (map)
12026 {
12027 /* map(): replace the list item value */
12028 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000012029 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012030 *tv = rettv;
12031 }
12032 else
12033 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012034 int error = FALSE;
12035
Bram Moolenaare9a41262005-01-15 22:18:47 +000012036 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012037 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012039 /* On type error, nothing has been removed; return FAIL to stop the
12040 * loop. The error message was given by get_tv_number_chk(). */
12041 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012042 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000012043 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012044 retval = OK;
12045theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000012046 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000012047 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012048}
12049
12050/*
12051 * "filter()" function
12052 */
12053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012054f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012055{
12056 filter_map(argvars, rettv, FALSE);
12057}
12058
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012059/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012060 * "finddir({fname}[, {path}[, {count}]])" function
12061 */
12062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012063f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012064{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012065 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012066}
12067
12068/*
12069 * "findfile({fname}[, {path}[, {count}]])" function
12070 */
12071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012072f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012074 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012075}
12076
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012077#ifdef FEAT_FLOAT
12078/*
12079 * "float2nr({float})" function
12080 */
12081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012082f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012083{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012084 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012085
12086 if (get_float_arg(argvars, &f) == OK)
12087 {
12088 if (f < -0x7fffffff)
12089 rettv->vval.v_number = -0x7fffffff;
12090 else if (f > 0x7fffffff)
12091 rettv->vval.v_number = 0x7fffffff;
12092 else
12093 rettv->vval.v_number = (varnumber_T)f;
12094 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012095}
12096
12097/*
12098 * "floor({float})" function
12099 */
12100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012101f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012102{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012103 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012104
12105 rettv->v_type = VAR_FLOAT;
12106 if (get_float_arg(argvars, &f) == OK)
12107 rettv->vval.v_float = floor(f);
12108 else
12109 rettv->vval.v_float = 0.0;
12110}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012111
12112/*
12113 * "fmod()" function
12114 */
12115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012116f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012117{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012118 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012119
12120 rettv->v_type = VAR_FLOAT;
12121 if (get_float_arg(argvars, &fx) == OK
12122 && get_float_arg(&argvars[1], &fy) == OK)
12123 rettv->vval.v_float = fmod(fx, fy);
12124 else
12125 rettv->vval.v_float = 0.0;
12126}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012127#endif
12128
Bram Moolenaar0d660222005-01-07 21:51:51 +000012129/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012130 * "fnameescape({string})" function
12131 */
12132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012133f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012134{
12135 rettv->vval.v_string = vim_strsave_fnameescape(
12136 get_tv_string(&argvars[0]), FALSE);
12137 rettv->v_type = VAR_STRING;
12138}
12139
12140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012141 * "fnamemodify({fname}, {mods})" function
12142 */
12143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012144f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012145{
12146 char_u *fname;
12147 char_u *mods;
12148 int usedlen = 0;
12149 int len;
12150 char_u *fbuf = NULL;
12151 char_u buf[NUMBUFLEN];
12152
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012153 fname = get_tv_string_chk(&argvars[0]);
12154 mods = get_tv_string_buf_chk(&argvars[1], buf);
12155 if (fname == NULL || mods == NULL)
12156 fname = NULL;
12157 else
12158 {
12159 len = (int)STRLEN(fname);
12160 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12161 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012163 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012164 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012165 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012166 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012167 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012168 vim_free(fbuf);
12169}
12170
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012171static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012172
12173/*
12174 * "foldclosed()" function
12175 */
12176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012177foldclosed_both(
12178 typval_T *argvars UNUSED,
12179 typval_T *rettv,
12180 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012181{
12182#ifdef FEAT_FOLDING
12183 linenr_T lnum;
12184 linenr_T first, last;
12185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012186 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012187 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12188 {
12189 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12190 {
12191 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012192 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012193 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012194 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 return;
12196 }
12197 }
12198#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012199 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200}
12201
12202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012203 * "foldclosed()" function
12204 */
12205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012206f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012207{
12208 foldclosed_both(argvars, rettv, FALSE);
12209}
12210
12211/*
12212 * "foldclosedend()" function
12213 */
12214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012215f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216{
12217 foldclosed_both(argvars, rettv, TRUE);
12218}
12219
12220/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012221 * "foldlevel()" function
12222 */
12223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012224f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012225{
12226#ifdef FEAT_FOLDING
12227 linenr_T lnum;
12228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012229 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012230 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012231 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012232#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012233}
12234
12235/*
12236 * "foldtext()" function
12237 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012239f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012240{
12241#ifdef FEAT_FOLDING
12242 linenr_T lnum;
12243 char_u *s;
12244 char_u *r;
12245 int len;
12246 char *txt;
12247#endif
12248
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012249 rettv->v_type = VAR_STRING;
12250 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012251#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012252 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12253 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12254 <= curbuf->b_ml.ml_line_count
12255 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256 {
12257 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012258 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12259 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012260 {
12261 if (!linewhite(lnum))
12262 break;
12263 ++lnum;
12264 }
12265
12266 /* Find interesting text in this line. */
12267 s = skipwhite(ml_get(lnum));
12268 /* skip C comment-start */
12269 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012270 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012271 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012272 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012273 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012274 {
12275 s = skipwhite(ml_get(lnum + 1));
12276 if (*s == '*')
12277 s = skipwhite(s + 1);
12278 }
12279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 txt = _("+-%s%3ld lines: ");
12281 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012282 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283 + 20 /* for %3ld */
12284 + STRLEN(s))); /* concatenated */
12285 if (r != NULL)
12286 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012287 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12288 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12289 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012290 len = (int)STRLEN(r);
12291 STRCAT(r, s);
12292 /* remove 'foldmarker' and 'commentstring' */
12293 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012294 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012295 }
12296 }
12297#endif
12298}
12299
12300/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012301 * "foldtextresult(lnum)" function
12302 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012304f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012305{
12306#ifdef FEAT_FOLDING
12307 linenr_T lnum;
12308 char_u *text;
12309 char_u buf[51];
12310 foldinfo_T foldinfo;
12311 int fold_count;
12312#endif
12313
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012314 rettv->v_type = VAR_STRING;
12315 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012316#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012318 /* treat illegal types and illegal string values for {lnum} the same */
12319 if (lnum < 0)
12320 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012321 fold_count = foldedCount(curwin, lnum, &foldinfo);
12322 if (fold_count > 0)
12323 {
12324 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12325 &foldinfo, buf);
12326 if (text == buf)
12327 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012328 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012329 }
12330#endif
12331}
12332
12333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334 * "foreground()" function
12335 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012337f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012338{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339#ifdef FEAT_GUI
12340 if (gui.in_use)
12341 gui_mch_set_foreground();
12342#else
12343# ifdef WIN32
12344 win32_set_foreground();
12345# endif
12346#endif
12347}
12348
12349/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012350 * "function()" function
12351 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012353f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012354{
12355 char_u *s;
12356
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012358 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012359 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012360 /* Don't check an autoload name for existence here. */
12361 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012362 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012363 else
12364 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012365 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012366 {
12367 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012368 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012369
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012370 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12371 * also be called from another script. Using trans_function_name()
12372 * would also work, but some plugins depend on the name being
12373 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012374 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012375 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012376 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012377 if (rettv->vval.v_string != NULL)
12378 {
12379 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012380 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012381 }
12382 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012383 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012384 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012385 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012386 }
12387}
12388
12389/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012390 * "garbagecollect()" function
12391 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012393f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012394{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012395 /* This is postponed until we are back at the toplevel, because we may be
12396 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12397 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012398
12399 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12400 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012401}
12402
12403/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012404 * "get()" function
12405 */
12406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012407f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012408{
Bram Moolenaar33570922005-01-25 22:26:29 +000012409 listitem_T *li;
12410 list_T *l;
12411 dictitem_T *di;
12412 dict_T *d;
12413 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012414
Bram Moolenaare9a41262005-01-15 22:18:47 +000012415 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012416 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012417 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012418 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012419 int error = FALSE;
12420
12421 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12422 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012423 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012424 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012425 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012426 else if (argvars[0].v_type == VAR_DICT)
12427 {
12428 if ((d = argvars[0].vval.v_dict) != NULL)
12429 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012430 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012431 if (di != NULL)
12432 tv = &di->di_tv;
12433 }
12434 }
12435 else
12436 EMSG2(_(e_listdictarg), "get()");
12437
12438 if (tv == NULL)
12439 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012440 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012441 copy_tv(&argvars[2], rettv);
12442 }
12443 else
12444 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012445}
12446
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012447static 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 +000012448
12449/*
12450 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012451 * Return a range (from start to end) of lines in rettv from the specified
12452 * buffer.
12453 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012454 */
12455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012456get_buffer_lines(
12457 buf_T *buf,
12458 linenr_T start,
12459 linenr_T end,
12460 int retlist,
12461 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012462{
12463 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012464
Bram Moolenaar959a1432013-12-14 12:17:38 +010012465 rettv->v_type = VAR_STRING;
12466 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012467 if (retlist && rettv_list_alloc(rettv) == FAIL)
12468 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012469
12470 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12471 return;
12472
12473 if (!retlist)
12474 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012475 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12476 p = ml_get_buf(buf, start, FALSE);
12477 else
12478 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012479 rettv->vval.v_string = vim_strsave(p);
12480 }
12481 else
12482 {
12483 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012484 return;
12485
12486 if (start < 1)
12487 start = 1;
12488 if (end > buf->b_ml.ml_line_count)
12489 end = buf->b_ml.ml_line_count;
12490 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012491 if (list_append_string(rettv->vval.v_list,
12492 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012493 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012494 }
12495}
12496
12497/*
12498 * "getbufline()" function
12499 */
12500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012501f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012502{
12503 linenr_T lnum;
12504 linenr_T end;
12505 buf_T *buf;
12506
12507 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12508 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012509 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012510 --emsg_off;
12511
Bram Moolenaar661b1822005-07-28 22:36:45 +000012512 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012513 if (argvars[2].v_type == VAR_UNKNOWN)
12514 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012515 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012516 end = get_tv_lnum_buf(&argvars[2], buf);
12517
Bram Moolenaar342337a2005-07-21 21:11:17 +000012518 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012519}
12520
Bram Moolenaar0d660222005-01-07 21:51:51 +000012521/*
12522 * "getbufvar()" function
12523 */
12524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012525f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012526{
12527 buf_T *buf;
12528 buf_T *save_curbuf;
12529 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012530 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012531 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012532
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12534 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012535 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012536 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012537
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012538 rettv->v_type = VAR_STRING;
12539 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012540
12541 if (buf != NULL && varname != NULL)
12542 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012543 /* set curbuf to be our buf, temporarily */
12544 save_curbuf = curbuf;
12545 curbuf = buf;
12546
Bram Moolenaar0d660222005-01-07 21:51:51 +000012547 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012548 {
12549 if (get_option_tv(&varname, rettv, TRUE) == OK)
12550 done = TRUE;
12551 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012552 else if (STRCMP(varname, "changedtick") == 0)
12553 {
12554 rettv->v_type = VAR_NUMBER;
12555 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012556 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012557 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012558 else
12559 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012560 /* Look up the variable. */
12561 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12562 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12563 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012564 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012565 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012566 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012567 done = TRUE;
12568 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012569 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012570
12571 /* restore previous notion of curbuf */
12572 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012573 }
12574
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012575 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12576 /* use the default value */
12577 copy_tv(&argvars[2], rettv);
12578
Bram Moolenaar0d660222005-01-07 21:51:51 +000012579 --emsg_off;
12580}
12581
12582/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583 * "getchar()" function
12584 */
12585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012586f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012587{
12588 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012589 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012591 /* Position the cursor. Needed after a message that ends in a space. */
12592 windgoto(msg_row, msg_col);
12593
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594 ++no_mapping;
12595 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012596 for (;;)
12597 {
12598 if (argvars[0].v_type == VAR_UNKNOWN)
12599 /* getchar(): blocking wait. */
12600 n = safe_vgetc();
12601 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12602 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012603 n = vpeekc_any();
12604 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012605 /* illegal argument or getchar(0) and no char avail: return zero */
12606 n = 0;
12607 else
12608 /* getchar(0) and char avail: return char */
12609 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012610
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012611 if (n == K_IGNORE)
12612 continue;
12613 break;
12614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615 --no_mapping;
12616 --allow_keys;
12617
Bram Moolenaar219b8702006-11-01 14:32:36 +000012618 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12619 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12620 vimvars[VV_MOUSE_COL].vv_nr = 0;
12621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 if (IS_SPECIAL(n) || mod_mask != 0)
12624 {
12625 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12626 int i = 0;
12627
12628 /* Turn a special key into three bytes, plus modifier. */
12629 if (mod_mask != 0)
12630 {
12631 temp[i++] = K_SPECIAL;
12632 temp[i++] = KS_MODIFIER;
12633 temp[i++] = mod_mask;
12634 }
12635 if (IS_SPECIAL(n))
12636 {
12637 temp[i++] = K_SPECIAL;
12638 temp[i++] = K_SECOND(n);
12639 temp[i++] = K_THIRD(n);
12640 }
12641#ifdef FEAT_MBYTE
12642 else if (has_mbyte)
12643 i += (*mb_char2bytes)(n, temp + i);
12644#endif
12645 else
12646 temp[i++] = n;
12647 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012648 rettv->v_type = VAR_STRING;
12649 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012650
12651#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012652 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012653 {
12654 int row = mouse_row;
12655 int col = mouse_col;
12656 win_T *win;
12657 linenr_T lnum;
12658# ifdef FEAT_WINDOWS
12659 win_T *wp;
12660# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012661 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012662
12663 if (row >= 0 && col >= 0)
12664 {
12665 /* Find the window at the mouse coordinates and compute the
12666 * text position. */
12667 win = mouse_find_win(&row, &col);
12668 (void)mouse_comp_pos(win, &row, &col, &lnum);
12669# ifdef FEAT_WINDOWS
12670 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012671 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012672# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012673 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012674 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12675 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12676 }
12677 }
12678#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012679 }
12680}
12681
12682/*
12683 * "getcharmod()" function
12684 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012686f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012687{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012688 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012689}
12690
12691/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012692 * "getcharsearch()" function
12693 */
12694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012695f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012696{
12697 if (rettv_dict_alloc(rettv) != FAIL)
12698 {
12699 dict_T *dict = rettv->vval.v_dict;
12700
12701 dict_add_nr_str(dict, "char", 0L, last_csearch());
12702 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12703 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12704 }
12705}
12706
12707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708 * "getcmdline()" function
12709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012711f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012712{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012713 rettv->v_type = VAR_STRING;
12714 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012715}
12716
12717/*
12718 * "getcmdpos()" function
12719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012721f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012722{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012723 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012724}
12725
12726/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012727 * "getcmdtype()" function
12728 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012730f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012731{
12732 rettv->v_type = VAR_STRING;
12733 rettv->vval.v_string = alloc(2);
12734 if (rettv->vval.v_string != NULL)
12735 {
12736 rettv->vval.v_string[0] = get_cmdline_type();
12737 rettv->vval.v_string[1] = NUL;
12738 }
12739}
12740
12741/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012742 * "getcmdwintype()" function
12743 */
12744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012745f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012746{
12747 rettv->v_type = VAR_STRING;
12748 rettv->vval.v_string = NULL;
12749#ifdef FEAT_CMDWIN
12750 rettv->vval.v_string = alloc(2);
12751 if (rettv->vval.v_string != NULL)
12752 {
12753 rettv->vval.v_string[0] = cmdwin_type;
12754 rettv->vval.v_string[1] = NUL;
12755 }
12756#endif
12757}
12758
12759/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012760 * "getcwd()" function
12761 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012763f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012764{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012765 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012766 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012767
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012768 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012769 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012770
12771 wp = find_tabwin(&argvars[0], &argvars[1]);
12772 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012773 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012774 if (wp->w_localdir != NULL)
12775 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12776 else if(globaldir != NULL)
12777 rettv->vval.v_string = vim_strsave(globaldir);
12778 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012779 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012780 cwd = alloc(MAXPATHL);
12781 if (cwd != NULL)
12782 {
12783 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12784 rettv->vval.v_string = vim_strsave(cwd);
12785 vim_free(cwd);
12786 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012787 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012788#ifdef BACKSLASH_IN_FILENAME
12789 if (rettv->vval.v_string != NULL)
12790 slash_adjust(rettv->vval.v_string);
12791#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792 }
12793}
12794
12795/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012796 * "getfontname()" function
12797 */
12798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012799f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012800{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012801 rettv->v_type = VAR_STRING;
12802 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012803#ifdef FEAT_GUI
12804 if (gui.in_use)
12805 {
12806 GuiFont font;
12807 char_u *name = NULL;
12808
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012809 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012810 {
12811 /* Get the "Normal" font. Either the name saved by
12812 * hl_set_font_name() or from the font ID. */
12813 font = gui.norm_font;
12814 name = hl_get_font_name();
12815 }
12816 else
12817 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012818 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012819 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12820 return;
12821 font = gui_mch_get_font(name, FALSE);
12822 if (font == NOFONT)
12823 return; /* Invalid font name, return empty string. */
12824 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012826 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012827 gui_mch_free_font(font);
12828 }
12829#endif
12830}
12831
12832/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012833 * "getfperm({fname})" function
12834 */
12835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012836f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012837{
12838 char_u *fname;
12839 struct stat st;
12840 char_u *perm = NULL;
12841 char_u flags[] = "rwx";
12842 int i;
12843
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012844 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012845
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012846 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012847 if (mch_stat((char *)fname, &st) >= 0)
12848 {
12849 perm = vim_strsave((char_u *)"---------");
12850 if (perm != NULL)
12851 {
12852 for (i = 0; i < 9; i++)
12853 {
12854 if (st.st_mode & (1 << (8 - i)))
12855 perm[i] = flags[i % 3];
12856 }
12857 }
12858 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012859 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012860}
12861
12862/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012863 * "getfsize({fname})" function
12864 */
12865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012866f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867{
12868 char_u *fname;
12869 struct stat st;
12870
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012871 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012872
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012873 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012874
12875 if (mch_stat((char *)fname, &st) >= 0)
12876 {
12877 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012878 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012879 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012880 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012881 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012882
12883 /* non-perfect check for overflow */
12884 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12885 rettv->vval.v_number = -2;
12886 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012887 }
12888 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012889 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012890}
12891
12892/*
12893 * "getftime({fname})" function
12894 */
12895 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012896f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012897{
12898 char_u *fname;
12899 struct stat st;
12900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012901 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
12903 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012904 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012905 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907}
12908
12909/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012910 * "getftype({fname})" function
12911 */
12912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012913f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012914{
12915 char_u *fname;
12916 struct stat st;
12917 char_u *type = NULL;
12918 char *t;
12919
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012920 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012921
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012922 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012923 if (mch_lstat((char *)fname, &st) >= 0)
12924 {
12925#ifdef S_ISREG
12926 if (S_ISREG(st.st_mode))
12927 t = "file";
12928 else if (S_ISDIR(st.st_mode))
12929 t = "dir";
12930# ifdef S_ISLNK
12931 else if (S_ISLNK(st.st_mode))
12932 t = "link";
12933# endif
12934# ifdef S_ISBLK
12935 else if (S_ISBLK(st.st_mode))
12936 t = "bdev";
12937# endif
12938# ifdef S_ISCHR
12939 else if (S_ISCHR(st.st_mode))
12940 t = "cdev";
12941# endif
12942# ifdef S_ISFIFO
12943 else if (S_ISFIFO(st.st_mode))
12944 t = "fifo";
12945# endif
12946# ifdef S_ISSOCK
12947 else if (S_ISSOCK(st.st_mode))
12948 t = "fifo";
12949# endif
12950 else
12951 t = "other";
12952#else
12953# ifdef S_IFMT
12954 switch (st.st_mode & S_IFMT)
12955 {
12956 case S_IFREG: t = "file"; break;
12957 case S_IFDIR: t = "dir"; break;
12958# ifdef S_IFLNK
12959 case S_IFLNK: t = "link"; break;
12960# endif
12961# ifdef S_IFBLK
12962 case S_IFBLK: t = "bdev"; break;
12963# endif
12964# ifdef S_IFCHR
12965 case S_IFCHR: t = "cdev"; break;
12966# endif
12967# ifdef S_IFIFO
12968 case S_IFIFO: t = "fifo"; break;
12969# endif
12970# ifdef S_IFSOCK
12971 case S_IFSOCK: t = "socket"; break;
12972# endif
12973 default: t = "other";
12974 }
12975# else
12976 if (mch_isdir(fname))
12977 t = "dir";
12978 else
12979 t = "file";
12980# endif
12981#endif
12982 type = vim_strsave((char_u *)t);
12983 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012984 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012985}
12986
12987/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012988 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012989 */
12990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012991f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012992{
12993 linenr_T lnum;
12994 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012995 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012996
12997 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012998 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012999 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013000 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000013001 retlist = FALSE;
13002 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000013003 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000013004 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000013005 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000013006 retlist = TRUE;
13007 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000013008
Bram Moolenaar342337a2005-07-21 21:11:17 +000013009 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000013010}
13011
13012/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013013 * "getmatches()" function
13014 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013016f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013017{
13018#ifdef FEAT_SEARCH_EXTRA
13019 dict_T *dict;
13020 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013021 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013022
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013023 if (rettv_list_alloc(rettv) == OK)
13024 {
13025 while (cur != NULL)
13026 {
13027 dict = dict_alloc();
13028 if (dict == NULL)
13029 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020013030 if (cur->match.regprog == NULL)
13031 {
13032 /* match added with matchaddpos() */
13033 for (i = 0; i < MAXPOSMATCH; ++i)
13034 {
13035 llpos_T *llpos;
13036 char buf[6];
13037 list_T *l;
13038
13039 llpos = &cur->pos.pos[i];
13040 if (llpos->lnum == 0)
13041 break;
13042 l = list_alloc();
13043 if (l == NULL)
13044 break;
13045 list_append_number(l, (varnumber_T)llpos->lnum);
13046 if (llpos->col > 0)
13047 {
13048 list_append_number(l, (varnumber_T)llpos->col);
13049 list_append_number(l, (varnumber_T)llpos->len);
13050 }
13051 sprintf(buf, "pos%d", i + 1);
13052 dict_add_list(dict, buf, l);
13053 }
13054 }
13055 else
13056 {
13057 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13058 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013059 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013060 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13061 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013062# ifdef FEAT_CONCEAL
13063 if (cur->conceal_char)
13064 {
13065 char_u buf[MB_MAXBYTES + 1];
13066
13067 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13068 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13069 }
13070# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013071 list_append_dict(rettv->vval.v_list, dict);
13072 cur = cur->next;
13073 }
13074 }
13075#endif
13076}
13077
13078/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013079 * "getpid()" function
13080 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013082f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013083{
13084 rettv->vval.v_number = mch_get_pid();
13085}
13086
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013087static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013088
13089/*
13090 * "getcurpos()" function
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013094{
13095 getpos_both(argvars, rettv, TRUE);
13096}
13097
Bram Moolenaar18081e32008-02-20 19:11:07 +000013098/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013099 * "getpos(string)" function
13100 */
13101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013102f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013103{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013104 getpos_both(argvars, rettv, FALSE);
13105}
13106
13107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013108getpos_both(
13109 typval_T *argvars,
13110 typval_T *rettv,
13111 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013112{
Bram Moolenaara5525202006-03-02 22:52:09 +000013113 pos_T *fp;
13114 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013115 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013116
13117 if (rettv_list_alloc(rettv) == OK)
13118 {
13119 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013120 if (getcurpos)
13121 fp = &curwin->w_cursor;
13122 else
13123 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013124 if (fnum != -1)
13125 list_append_number(l, (varnumber_T)fnum);
13126 else
13127 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013128 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13129 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013130 list_append_number(l, (fp != NULL)
13131 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013132 : (varnumber_T)0);
13133 list_append_number(l,
13134#ifdef FEAT_VIRTUALEDIT
13135 (fp != NULL) ? (varnumber_T)fp->coladd :
13136#endif
13137 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013138 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013139 {
13140 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013141 list_append_number(l, curwin->w_curswant == MAXCOL ?
13142 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013143 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013144 }
13145 else
13146 rettv->vval.v_number = FALSE;
13147}
13148
13149/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013150 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013151 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013153f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013154{
13155#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013156 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013157#endif
13158
Bram Moolenaar2641f772005-03-25 21:58:17 +000013159#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013160 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013161 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013162 wp = NULL;
13163 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13164 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013165 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013166 if (wp == NULL)
13167 return;
13168 }
13169
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013170 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013171 }
13172#endif
13173}
13174
13175/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013176 * "getreg()" function
13177 */
13178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013179f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013180{
13181 char_u *strregname;
13182 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013183 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013184 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013185 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013186
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013187 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013188 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013189 strregname = get_tv_string_chk(&argvars[0]);
13190 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013191 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013192 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013193 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013194 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13195 return_list = get_tv_number_chk(&argvars[2], &error);
13196 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013197 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013198 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013199 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013200
13201 if (error)
13202 return;
13203
Bram Moolenaar071d4272004-06-13 20:20:40 +000013204 regname = (strregname == NULL ? '"' : *strregname);
13205 if (regname == 0)
13206 regname = '"';
13207
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013208 if (return_list)
13209 {
13210 rettv->v_type = VAR_LIST;
13211 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13212 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013213 if (rettv->vval.v_list != NULL)
13214 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013215 }
13216 else
13217 {
13218 rettv->v_type = VAR_STRING;
13219 rettv->vval.v_string = get_reg_contents(regname,
13220 arg2 ? GREG_EXPR_SRC : 0);
13221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222}
13223
13224/*
13225 * "getregtype()" function
13226 */
13227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013228f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013229{
13230 char_u *strregname;
13231 int regname;
13232 char_u buf[NUMBUFLEN + 2];
13233 long reglen = 0;
13234
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013235 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013236 {
13237 strregname = get_tv_string_chk(&argvars[0]);
13238 if (strregname == NULL) /* type error; errmsg already given */
13239 {
13240 rettv->v_type = VAR_STRING;
13241 rettv->vval.v_string = NULL;
13242 return;
13243 }
13244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013245 else
13246 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013247 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013248
13249 regname = (strregname == NULL ? '"' : *strregname);
13250 if (regname == 0)
13251 regname = '"';
13252
13253 buf[0] = NUL;
13254 buf[1] = NUL;
13255 switch (get_reg_type(regname, &reglen))
13256 {
13257 case MLINE: buf[0] = 'V'; break;
13258 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013259 case MBLOCK:
13260 buf[0] = Ctrl_V;
13261 sprintf((char *)buf + 1, "%ld", reglen + 1);
13262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013264 rettv->v_type = VAR_STRING;
13265 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266}
13267
13268/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013269 * "gettabvar()" function
13270 */
13271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013272f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013273{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013274 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013275 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013276 dictitem_T *v;
13277 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013278 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013279
13280 rettv->v_type = VAR_STRING;
13281 rettv->vval.v_string = NULL;
13282
13283 varname = get_tv_string_chk(&argvars[1]);
13284 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13285 if (tp != NULL && varname != NULL)
13286 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013287 /* Set tp to be our tabpage, temporarily. Also set the window to the
13288 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013289 if (switch_win(&oldcurwin, &oldtabpage,
13290 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013291 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013292 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013293 /* look up the variable */
13294 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13295 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13296 if (v != NULL)
13297 {
13298 copy_tv(&v->di_tv, rettv);
13299 done = TRUE;
13300 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013301 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013302
13303 /* restore previous notion of curwin */
13304 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013305 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013306
13307 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013308 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013309}
13310
13311/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013312 * "gettabwinvar()" function
13313 */
13314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013315f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013316{
13317 getwinvar(argvars, rettv, 1);
13318}
13319
13320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013321 * "getwinposx()" function
13322 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013324f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013325{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013326 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013327#ifdef FEAT_GUI
13328 if (gui.in_use)
13329 {
13330 int x, y;
13331
13332 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013333 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013334 }
13335#endif
13336}
13337
13338/*
13339 * "getwinposy()" function
13340 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013342f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013344 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013345#ifdef FEAT_GUI
13346 if (gui.in_use)
13347 {
13348 int x, y;
13349
13350 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013351 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013352 }
13353#endif
13354}
13355
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013356/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013357 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013358 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013359 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013360find_win_by_nr(
13361 typval_T *vp,
13362 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013363{
13364#ifdef FEAT_WINDOWS
13365 win_T *wp;
13366#endif
13367 int nr;
13368
13369 nr = get_tv_number_chk(vp, NULL);
13370
13371#ifdef FEAT_WINDOWS
13372 if (nr < 0)
13373 return NULL;
13374 if (nr == 0)
13375 return curwin;
13376
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013377 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13378 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013379 if (--nr <= 0)
13380 break;
13381 return wp;
13382#else
13383 if (nr == 0 || nr == 1)
13384 return curwin;
13385 return NULL;
13386#endif
13387}
13388
Bram Moolenaar071d4272004-06-13 20:20:40 +000013389/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013390 * Find window specified by "wvp" in tabpage "tvp".
13391 */
13392 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013393find_tabwin(
13394 typval_T *wvp, /* VAR_UNKNOWN for current window */
13395 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013396{
13397 win_T *wp = NULL;
13398 tabpage_T *tp = NULL;
13399 long n;
13400
13401 if (wvp->v_type != VAR_UNKNOWN)
13402 {
13403 if (tvp->v_type != VAR_UNKNOWN)
13404 {
13405 n = get_tv_number(tvp);
13406 if (n >= 0)
13407 tp = find_tabpage(n);
13408 }
13409 else
13410 tp = curtab;
13411
13412 if (tp != NULL)
13413 wp = find_win_by_nr(wvp, tp);
13414 }
13415 else
13416 wp = curwin;
13417
13418 return wp;
13419}
13420
13421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013422 * "getwinvar()" function
13423 */
13424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013425f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013426{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013427 getwinvar(argvars, rettv, 0);
13428}
13429
13430/*
13431 * getwinvar() and gettabwinvar()
13432 */
13433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013434getwinvar(
13435 typval_T *argvars,
13436 typval_T *rettv,
13437 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013438{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013439 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013440 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013441 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013442 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013443 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013444#ifdef FEAT_WINDOWS
13445 win_T *oldcurwin;
13446 tabpage_T *oldtabpage;
13447 int need_switch_win;
13448#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013449
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013450#ifdef FEAT_WINDOWS
13451 if (off == 1)
13452 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13453 else
13454 tp = curtab;
13455#endif
13456 win = find_win_by_nr(&argvars[off], tp);
13457 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013458 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013459
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013460 rettv->v_type = VAR_STRING;
13461 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013462
13463 if (win != NULL && varname != NULL)
13464 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013465#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013466 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013467 * otherwise the window is not valid. Only do this when needed,
13468 * autocommands get blocked. */
13469 need_switch_win = !(tp == curtab && win == curwin);
13470 if (!need_switch_win
13471 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13472#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013473 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013474 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013475 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013476 if (get_option_tv(&varname, rettv, 1) == OK)
13477 done = TRUE;
13478 }
13479 else
13480 {
13481 /* Look up the variable. */
13482 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13483 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13484 varname, FALSE);
13485 if (v != NULL)
13486 {
13487 copy_tv(&v->di_tv, rettv);
13488 done = TRUE;
13489 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013490 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013492
Bram Moolenaarba117c22015-09-29 16:53:22 +020013493#ifdef FEAT_WINDOWS
13494 if (need_switch_win)
13495 /* restore previous notion of curwin */
13496 restore_win(oldcurwin, oldtabpage, TRUE);
13497#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013498 }
13499
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013500 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13501 /* use the default return value */
13502 copy_tv(&argvars[off + 2], rettv);
13503
Bram Moolenaar071d4272004-06-13 20:20:40 +000013504 --emsg_off;
13505}
13506
13507/*
13508 * "glob()" function
13509 */
13510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013511f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013513 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013514 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013515 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013517 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013518 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013519 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013520 if (argvars[1].v_type != VAR_UNKNOWN)
13521 {
13522 if (get_tv_number_chk(&argvars[1], &error))
13523 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013524 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013525 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013526 if (get_tv_number_chk(&argvars[2], &error))
13527 {
13528 rettv->v_type = VAR_LIST;
13529 rettv->vval.v_list = NULL;
13530 }
13531 if (argvars[3].v_type != VAR_UNKNOWN
13532 && get_tv_number_chk(&argvars[3], &error))
13533 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013534 }
13535 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013536 if (!error)
13537 {
13538 ExpandInit(&xpc);
13539 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013540 if (p_wic)
13541 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013542 if (rettv->v_type == VAR_STRING)
13543 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013544 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013545 else if (rettv_list_alloc(rettv) != FAIL)
13546 {
13547 int i;
13548
13549 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13550 NULL, options, WILD_ALL_KEEP);
13551 for (i = 0; i < xpc.xp_numfiles; i++)
13552 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13553
13554 ExpandCleanup(&xpc);
13555 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013556 }
13557 else
13558 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559}
13560
13561/*
13562 * "globpath()" function
13563 */
13564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013565f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013566{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013567 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013568 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013569 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013570 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013571 garray_T ga;
13572 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013574 /* When the optional second argument is non-zero, don't remove matches
13575 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013576 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013577 if (argvars[2].v_type != VAR_UNKNOWN)
13578 {
13579 if (get_tv_number_chk(&argvars[2], &error))
13580 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013581 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013582 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013583 if (get_tv_number_chk(&argvars[3], &error))
13584 {
13585 rettv->v_type = VAR_LIST;
13586 rettv->vval.v_list = NULL;
13587 }
13588 if (argvars[4].v_type != VAR_UNKNOWN
13589 && get_tv_number_chk(&argvars[4], &error))
13590 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013591 }
13592 }
13593 if (file != NULL && !error)
13594 {
13595 ga_init2(&ga, (int)sizeof(char_u *), 10);
13596 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13597 if (rettv->v_type == VAR_STRING)
13598 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13599 else if (rettv_list_alloc(rettv) != FAIL)
13600 for (i = 0; i < ga.ga_len; ++i)
13601 list_append_string(rettv->vval.v_list,
13602 ((char_u **)(ga.ga_data))[i], -1);
13603 ga_clear_strings(&ga);
13604 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013605 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013606 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013607}
13608
13609/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013610 * "glob2regpat()" function
13611 */
13612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013613f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013614{
13615 char_u *pat = get_tv_string_chk(&argvars[0]);
13616
13617 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013618 rettv->vval.v_string = (pat == NULL)
13619 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013620}
13621
13622/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013623 * "has()" function
13624 */
13625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013626f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013627{
13628 int i;
13629 char_u *name;
13630 int n = FALSE;
13631 static char *(has_list[]) =
13632 {
13633#ifdef AMIGA
13634 "amiga",
13635# ifdef FEAT_ARP
13636 "arp",
13637# endif
13638#endif
13639#ifdef __BEOS__
13640 "beos",
13641#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013642#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013643 "mac",
13644#endif
13645#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013646 "macunix", /* built with 'darwin' enabled */
13647#endif
13648#if defined(__APPLE__) && __APPLE__ == 1
13649 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651#ifdef __QNX__
13652 "qnx",
13653#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013654#ifdef UNIX
13655 "unix",
13656#endif
13657#ifdef VMS
13658 "vms",
13659#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013660#ifdef WIN32
13661 "win32",
13662#endif
13663#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13664 "win32unix",
13665#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013666#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013667 "win64",
13668#endif
13669#ifdef EBCDIC
13670 "ebcdic",
13671#endif
13672#ifndef CASE_INSENSITIVE_FILENAME
13673 "fname_case",
13674#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013675#ifdef HAVE_ACL
13676 "acl",
13677#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013678#ifdef FEAT_ARABIC
13679 "arabic",
13680#endif
13681#ifdef FEAT_AUTOCMD
13682 "autocmd",
13683#endif
13684#ifdef FEAT_BEVAL
13685 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013686# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13687 "balloon_multiline",
13688# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013689#endif
13690#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13691 "builtin_terms",
13692# ifdef ALL_BUILTIN_TCAPS
13693 "all_builtin_terms",
13694# endif
13695#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013696#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13697 || defined(FEAT_GUI_W32) \
13698 || defined(FEAT_GUI_MOTIF))
13699 "browsefilter",
13700#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013701#ifdef FEAT_BYTEOFF
13702 "byte_offset",
13703#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013704#ifdef FEAT_CHANNEL
13705 "channel",
13706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707#ifdef FEAT_CINDENT
13708 "cindent",
13709#endif
13710#ifdef FEAT_CLIENTSERVER
13711 "clientserver",
13712#endif
13713#ifdef FEAT_CLIPBOARD
13714 "clipboard",
13715#endif
13716#ifdef FEAT_CMDL_COMPL
13717 "cmdline_compl",
13718#endif
13719#ifdef FEAT_CMDHIST
13720 "cmdline_hist",
13721#endif
13722#ifdef FEAT_COMMENTS
13723 "comments",
13724#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013725#ifdef FEAT_CONCEAL
13726 "conceal",
13727#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013728#ifdef FEAT_CRYPT
13729 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013730 "crypt-blowfish",
13731 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#endif
13733#ifdef FEAT_CSCOPE
13734 "cscope",
13735#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013736#ifdef FEAT_CURSORBIND
13737 "cursorbind",
13738#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013739#ifdef CURSOR_SHAPE
13740 "cursorshape",
13741#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013742#ifdef DEBUG
13743 "debug",
13744#endif
13745#ifdef FEAT_CON_DIALOG
13746 "dialog_con",
13747#endif
13748#ifdef FEAT_GUI_DIALOG
13749 "dialog_gui",
13750#endif
13751#ifdef FEAT_DIFF
13752 "diff",
13753#endif
13754#ifdef FEAT_DIGRAPHS
13755 "digraphs",
13756#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013757#ifdef FEAT_DIRECTX
13758 "directx",
13759#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013760#ifdef FEAT_DND
13761 "dnd",
13762#endif
13763#ifdef FEAT_EMACS_TAGS
13764 "emacs_tags",
13765#endif
13766 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013767 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768#ifdef FEAT_SEARCH_EXTRA
13769 "extra_search",
13770#endif
13771#ifdef FEAT_FKMAP
13772 "farsi",
13773#endif
13774#ifdef FEAT_SEARCHPATH
13775 "file_in_path",
13776#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013777#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013778 "filterpipe",
13779#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013780#ifdef FEAT_FIND_ID
13781 "find_in_path",
13782#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013783#ifdef FEAT_FLOAT
13784 "float",
13785#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013786#ifdef FEAT_FOLDING
13787 "folding",
13788#endif
13789#ifdef FEAT_FOOTER
13790 "footer",
13791#endif
13792#if !defined(USE_SYSTEM) && defined(UNIX)
13793 "fork",
13794#endif
13795#ifdef FEAT_GETTEXT
13796 "gettext",
13797#endif
13798#ifdef FEAT_GUI
13799 "gui",
13800#endif
13801#ifdef FEAT_GUI_ATHENA
13802# ifdef FEAT_GUI_NEXTAW
13803 "gui_neXtaw",
13804# else
13805 "gui_athena",
13806# endif
13807#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013808#ifdef FEAT_GUI_GTK
13809 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013810# ifdef USE_GTK3
13811 "gui_gtk3",
13812# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013814# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013816#ifdef FEAT_GUI_GNOME
13817 "gui_gnome",
13818#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819#ifdef FEAT_GUI_MAC
13820 "gui_mac",
13821#endif
13822#ifdef FEAT_GUI_MOTIF
13823 "gui_motif",
13824#endif
13825#ifdef FEAT_GUI_PHOTON
13826 "gui_photon",
13827#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013828#ifdef FEAT_GUI_W32
13829 "gui_win32",
13830#endif
13831#ifdef FEAT_HANGULIN
13832 "hangul_input",
13833#endif
13834#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13835 "iconv",
13836#endif
13837#ifdef FEAT_INS_EXPAND
13838 "insert_expand",
13839#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013840#ifdef FEAT_JOB
13841 "job",
13842#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013843#ifdef FEAT_JUMPLIST
13844 "jumplist",
13845#endif
13846#ifdef FEAT_KEYMAP
13847 "keymap",
13848#endif
13849#ifdef FEAT_LANGMAP
13850 "langmap",
13851#endif
13852#ifdef FEAT_LIBCALL
13853 "libcall",
13854#endif
13855#ifdef FEAT_LINEBREAK
13856 "linebreak",
13857#endif
13858#ifdef FEAT_LISP
13859 "lispindent",
13860#endif
13861#ifdef FEAT_LISTCMDS
13862 "listcmds",
13863#endif
13864#ifdef FEAT_LOCALMAP
13865 "localmap",
13866#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013867#ifdef FEAT_LUA
13868# ifndef DYNAMIC_LUA
13869 "lua",
13870# endif
13871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872#ifdef FEAT_MENU
13873 "menu",
13874#endif
13875#ifdef FEAT_SESSION
13876 "mksession",
13877#endif
13878#ifdef FEAT_MODIFY_FNAME
13879 "modify_fname",
13880#endif
13881#ifdef FEAT_MOUSE
13882 "mouse",
13883#endif
13884#ifdef FEAT_MOUSESHAPE
13885 "mouseshape",
13886#endif
13887#if defined(UNIX) || defined(VMS)
13888# ifdef FEAT_MOUSE_DEC
13889 "mouse_dec",
13890# endif
13891# ifdef FEAT_MOUSE_GPM
13892 "mouse_gpm",
13893# endif
13894# ifdef FEAT_MOUSE_JSB
13895 "mouse_jsbterm",
13896# endif
13897# ifdef FEAT_MOUSE_NET
13898 "mouse_netterm",
13899# endif
13900# ifdef FEAT_MOUSE_PTERM
13901 "mouse_pterm",
13902# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013903# ifdef FEAT_MOUSE_SGR
13904 "mouse_sgr",
13905# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013906# ifdef FEAT_SYSMOUSE
13907 "mouse_sysmouse",
13908# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013909# ifdef FEAT_MOUSE_URXVT
13910 "mouse_urxvt",
13911# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013912# ifdef FEAT_MOUSE_XTERM
13913 "mouse_xterm",
13914# endif
13915#endif
13916#ifdef FEAT_MBYTE
13917 "multi_byte",
13918#endif
13919#ifdef FEAT_MBYTE_IME
13920 "multi_byte_ime",
13921#endif
13922#ifdef FEAT_MULTI_LANG
13923 "multi_lang",
13924#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013925#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013926#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013927 "mzscheme",
13928#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930#ifdef FEAT_OLE
13931 "ole",
13932#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013933 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013934#ifdef FEAT_PATH_EXTRA
13935 "path_extra",
13936#endif
13937#ifdef FEAT_PERL
13938#ifndef DYNAMIC_PERL
13939 "perl",
13940#endif
13941#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013942#ifdef FEAT_PERSISTENT_UNDO
13943 "persistent_undo",
13944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013945#ifdef FEAT_PYTHON
13946#ifndef DYNAMIC_PYTHON
13947 "python",
13948#endif
13949#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013950#ifdef FEAT_PYTHON3
13951#ifndef DYNAMIC_PYTHON3
13952 "python3",
13953#endif
13954#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955#ifdef FEAT_POSTSCRIPT
13956 "postscript",
13957#endif
13958#ifdef FEAT_PRINTER
13959 "printer",
13960#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013961#ifdef FEAT_PROFILE
13962 "profile",
13963#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013964#ifdef FEAT_RELTIME
13965 "reltime",
13966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013967#ifdef FEAT_QUICKFIX
13968 "quickfix",
13969#endif
13970#ifdef FEAT_RIGHTLEFT
13971 "rightleft",
13972#endif
13973#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13974 "ruby",
13975#endif
13976#ifdef FEAT_SCROLLBIND
13977 "scrollbind",
13978#endif
13979#ifdef FEAT_CMDL_INFO
13980 "showcmd",
13981 "cmdline_info",
13982#endif
13983#ifdef FEAT_SIGNS
13984 "signs",
13985#endif
13986#ifdef FEAT_SMARTINDENT
13987 "smartindent",
13988#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013989#ifdef STARTUPTIME
13990 "startuptime",
13991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992#ifdef FEAT_STL_OPT
13993 "statusline",
13994#endif
13995#ifdef FEAT_SUN_WORKSHOP
13996 "sun_workshop",
13997#endif
13998#ifdef FEAT_NETBEANS_INTG
13999 "netbeans_intg",
14000#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000014001#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000014002 "spell",
14003#endif
14004#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 "syntax",
14006#endif
14007#if defined(USE_SYSTEM) || !defined(UNIX)
14008 "system",
14009#endif
14010#ifdef FEAT_TAG_BINS
14011 "tag_binary",
14012#endif
14013#ifdef FEAT_TAG_OLDSTATIC
14014 "tag_old_static",
14015#endif
14016#ifdef FEAT_TAG_ANYWHITE
14017 "tag_any_white",
14018#endif
14019#ifdef FEAT_TCL
14020# ifndef DYNAMIC_TCL
14021 "tcl",
14022# endif
14023#endif
14024#ifdef TERMINFO
14025 "terminfo",
14026#endif
14027#ifdef FEAT_TERMRESPONSE
14028 "termresponse",
14029#endif
14030#ifdef FEAT_TEXTOBJ
14031 "textobjects",
14032#endif
14033#ifdef HAVE_TGETENT
14034 "tgetent",
14035#endif
14036#ifdef FEAT_TITLE
14037 "title",
14038#endif
14039#ifdef FEAT_TOOLBAR
14040 "toolbar",
14041#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010014042#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
14043 "unnamedplus",
14044#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045#ifdef FEAT_USR_CMDS
14046 "user-commands", /* was accidentally included in 5.4 */
14047 "user_commands",
14048#endif
14049#ifdef FEAT_VIMINFO
14050 "viminfo",
14051#endif
14052#ifdef FEAT_VERTSPLIT
14053 "vertsplit",
14054#endif
14055#ifdef FEAT_VIRTUALEDIT
14056 "virtualedit",
14057#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059#ifdef FEAT_VISUALEXTRA
14060 "visualextra",
14061#endif
14062#ifdef FEAT_VREPLACE
14063 "vreplace",
14064#endif
14065#ifdef FEAT_WILDIGN
14066 "wildignore",
14067#endif
14068#ifdef FEAT_WILDMENU
14069 "wildmenu",
14070#endif
14071#ifdef FEAT_WINDOWS
14072 "windows",
14073#endif
14074#ifdef FEAT_WAK
14075 "winaltkeys",
14076#endif
14077#ifdef FEAT_WRITEBACKUP
14078 "writebackup",
14079#endif
14080#ifdef FEAT_XIM
14081 "xim",
14082#endif
14083#ifdef FEAT_XFONTSET
14084 "xfontset",
14085#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014086#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014087 "xpm",
14088 "xpm_w32", /* for backward compatibility */
14089#else
14090# if defined(HAVE_XPM)
14091 "xpm",
14092# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014093#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094#ifdef USE_XSMP
14095 "xsmp",
14096#endif
14097#ifdef USE_XSMP_INTERACT
14098 "xsmp_interact",
14099#endif
14100#ifdef FEAT_XCLIPBOARD
14101 "xterm_clipboard",
14102#endif
14103#ifdef FEAT_XTERM_SAVE
14104 "xterm_save",
14105#endif
14106#if defined(UNIX) && defined(FEAT_X11)
14107 "X11",
14108#endif
14109 NULL
14110 };
14111
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014112 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113 for (i = 0; has_list[i] != NULL; ++i)
14114 if (STRICMP(name, has_list[i]) == 0)
14115 {
14116 n = TRUE;
14117 break;
14118 }
14119
14120 if (n == FALSE)
14121 {
14122 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014123 {
14124 if (name[5] == '-'
14125 && STRLEN(name) > 11
14126 && vim_isdigit(name[6])
14127 && vim_isdigit(name[8])
14128 && vim_isdigit(name[10]))
14129 {
14130 int major = atoi((char *)name + 6);
14131 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014132
14133 /* Expect "patch-9.9.01234". */
14134 n = (major < VIM_VERSION_MAJOR
14135 || (major == VIM_VERSION_MAJOR
14136 && (minor < VIM_VERSION_MINOR
14137 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014138 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014139 }
14140 else
14141 n = has_patch(atoi((char *)name + 5));
14142 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014143 else if (STRICMP(name, "vim_starting") == 0)
14144 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014145#ifdef FEAT_MBYTE
14146 else if (STRICMP(name, "multi_byte_encoding") == 0)
14147 n = has_mbyte;
14148#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014149#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14150 else if (STRICMP(name, "balloon_multiline") == 0)
14151 n = multiline_balloon_available();
14152#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153#ifdef DYNAMIC_TCL
14154 else if (STRICMP(name, "tcl") == 0)
14155 n = tcl_enabled(FALSE);
14156#endif
14157#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14158 else if (STRICMP(name, "iconv") == 0)
14159 n = iconv_enabled(FALSE);
14160#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014161#ifdef DYNAMIC_LUA
14162 else if (STRICMP(name, "lua") == 0)
14163 n = lua_enabled(FALSE);
14164#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014165#ifdef DYNAMIC_MZSCHEME
14166 else if (STRICMP(name, "mzscheme") == 0)
14167 n = mzscheme_enabled(FALSE);
14168#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014169#ifdef DYNAMIC_RUBY
14170 else if (STRICMP(name, "ruby") == 0)
14171 n = ruby_enabled(FALSE);
14172#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014173#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014174#ifdef DYNAMIC_PYTHON
14175 else if (STRICMP(name, "python") == 0)
14176 n = python_enabled(FALSE);
14177#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014178#endif
14179#ifdef FEAT_PYTHON3
14180#ifdef DYNAMIC_PYTHON3
14181 else if (STRICMP(name, "python3") == 0)
14182 n = python3_enabled(FALSE);
14183#endif
14184#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014185#ifdef DYNAMIC_PERL
14186 else if (STRICMP(name, "perl") == 0)
14187 n = perl_enabled(FALSE);
14188#endif
14189#ifdef FEAT_GUI
14190 else if (STRICMP(name, "gui_running") == 0)
14191 n = (gui.in_use || gui.starting);
14192# ifdef FEAT_GUI_W32
14193 else if (STRICMP(name, "gui_win32s") == 0)
14194 n = gui_is_win32s();
14195# endif
14196# ifdef FEAT_BROWSE
14197 else if (STRICMP(name, "browse") == 0)
14198 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14199# endif
14200#endif
14201#ifdef FEAT_SYN_HL
14202 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014203 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204#endif
14205#if defined(WIN3264)
14206 else if (STRICMP(name, "win95") == 0)
14207 n = mch_windows95();
14208#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014209#ifdef FEAT_NETBEANS_INTG
14210 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014211 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014212#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213 }
14214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014215 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014216}
14217
14218/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014219 * "has_key()" function
14220 */
14221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014222f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014223{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014224 if (argvars[0].v_type != VAR_DICT)
14225 {
14226 EMSG(_(e_dictreq));
14227 return;
14228 }
14229 if (argvars[0].vval.v_dict == NULL)
14230 return;
14231
14232 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014233 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014234}
14235
14236/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014237 * "haslocaldir()" function
14238 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014240f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014241{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014242 win_T *wp = NULL;
14243
14244 wp = find_tabwin(&argvars[0], &argvars[1]);
14245 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014246}
14247
14248/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014249 * "hasmapto()" function
14250 */
14251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014252f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014253{
14254 char_u *name;
14255 char_u *mode;
14256 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014257 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014258
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014259 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014260 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 mode = (char_u *)"nvo";
14262 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014263 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014264 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014265 if (argvars[2].v_type != VAR_UNKNOWN)
14266 abbr = get_tv_number(&argvars[2]);
14267 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014268
Bram Moolenaar2c932302006-03-18 21:42:09 +000014269 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014270 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014271 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014272 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014273}
14274
14275/*
14276 * "histadd()" function
14277 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014278 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014279f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280{
14281#ifdef FEAT_CMDHIST
14282 int histype;
14283 char_u *str;
14284 char_u buf[NUMBUFLEN];
14285#endif
14286
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014287 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 if (check_restricted() || check_secure())
14289 return;
14290#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14292 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293 if (histype >= 0)
14294 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014295 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 if (*str != NUL)
14297 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014298 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014299 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014300 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301 return;
14302 }
14303 }
14304#endif
14305}
14306
14307/*
14308 * "histdel()" function
14309 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014311f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312{
14313#ifdef FEAT_CMDHIST
14314 int n;
14315 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014317
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014318 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14319 if (str == NULL)
14320 n = 0;
14321 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014323 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014324 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014326 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328 else
14329 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014331 get_tv_string_buf(&argvars[1], buf));
14332 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014333#endif
14334}
14335
14336/*
14337 * "histget()" function
14338 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014340f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014341{
14342#ifdef FEAT_CMDHIST
14343 int type;
14344 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014345 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014347 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14348 if (str == NULL)
14349 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014350 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014351 {
14352 type = get_histtype(str);
14353 if (argvars[1].v_type == VAR_UNKNOWN)
14354 idx = get_history_idx(type);
14355 else
14356 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14357 /* -1 on type error */
14358 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14359 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014361 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014363 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364}
14365
14366/*
14367 * "histnr()" function
14368 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014370f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371{
14372 int i;
14373
14374#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 char_u *history = get_tv_string_chk(&argvars[0]);
14376
14377 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 if (i >= HIST_CMD && i < HIST_COUNT)
14379 i = get_history_idx(i);
14380 else
14381#endif
14382 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014383 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384}
14385
14386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 * "highlightID(name)" function
14388 */
14389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014390f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014392 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014393}
14394
14395/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014396 * "highlight_exists()" function
14397 */
14398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014399f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014400{
14401 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14402}
14403
14404/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 * "hostname()" function
14406 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014408f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409{
14410 char_u hostname[256];
14411
14412 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014413 rettv->v_type = VAR_STRING;
14414 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014415}
14416
14417/*
14418 * iconv() function
14419 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014421f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422{
14423#ifdef FEAT_MBYTE
14424 char_u buf1[NUMBUFLEN];
14425 char_u buf2[NUMBUFLEN];
14426 char_u *from, *to, *str;
14427 vimconv_T vimconv;
14428#endif
14429
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->v_type = VAR_STRING;
14431 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014432
14433#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 str = get_tv_string(&argvars[0]);
14435 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14436 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437 vimconv.vc_type = CONV_NONE;
14438 convert_setup(&vimconv, from, to);
14439
14440 /* If the encodings are equal, no conversion needed. */
14441 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014442 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014443 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014444 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014445
14446 convert_setup(&vimconv, NULL, NULL);
14447 vim_free(from);
14448 vim_free(to);
14449#endif
14450}
14451
14452/*
14453 * "indent()" function
14454 */
14455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014456f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
14458 linenr_T lnum;
14459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014460 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014461 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014462 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014464 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465}
14466
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014467/*
14468 * "index()" function
14469 */
14470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014471f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014472{
Bram Moolenaar33570922005-01-25 22:26:29 +000014473 list_T *l;
14474 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014475 long idx = 0;
14476 int ic = FALSE;
14477
14478 rettv->vval.v_number = -1;
14479 if (argvars[0].v_type != VAR_LIST)
14480 {
14481 EMSG(_(e_listreq));
14482 return;
14483 }
14484 l = argvars[0].vval.v_list;
14485 if (l != NULL)
14486 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014487 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014488 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014489 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014490 int error = FALSE;
14491
Bram Moolenaar758711c2005-02-02 23:11:38 +000014492 /* Start at specified item. Use the cached index that list_find()
14493 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014494 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014495 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014496 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014497 ic = get_tv_number_chk(&argvars[3], &error);
14498 if (error)
14499 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014500 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014501
Bram Moolenaar758711c2005-02-02 23:11:38 +000014502 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014503 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014504 {
14505 rettv->vval.v_number = idx;
14506 break;
14507 }
14508 }
14509}
14510
Bram Moolenaar071d4272004-06-13 20:20:40 +000014511static int inputsecret_flag = 0;
14512
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014513static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014514
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014516 * This function is used by f_input() and f_inputdialog() functions. The third
14517 * argument to f_input() specifies the type of completion to use at the
14518 * prompt. The third argument to f_inputdialog() specifies the value to return
14519 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014520 */
14521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014522get_user_input(
14523 typval_T *argvars,
14524 typval_T *rettv,
14525 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014527 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 char_u *p = NULL;
14529 int c;
14530 char_u buf[NUMBUFLEN];
14531 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014532 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014533 int xp_type = EXPAND_NOTHING;
14534 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014535
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014537 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014538
14539#ifdef NO_CONSOLE_INPUT
14540 /* While starting up, there is no place to enter text. */
14541 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014542 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014543#endif
14544
14545 cmd_silent = FALSE; /* Want to see the prompt. */
14546 if (prompt != NULL)
14547 {
14548 /* Only the part of the message after the last NL is considered as
14549 * prompt for the command line */
14550 p = vim_strrchr(prompt, '\n');
14551 if (p == NULL)
14552 p = prompt;
14553 else
14554 {
14555 ++p;
14556 c = *p;
14557 *p = NUL;
14558 msg_start();
14559 msg_clr_eos();
14560 msg_puts_attr(prompt, echo_attr);
14561 msg_didout = FALSE;
14562 msg_starthere();
14563 *p = c;
14564 }
14565 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014567 if (argvars[1].v_type != VAR_UNKNOWN)
14568 {
14569 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14570 if (defstr != NULL)
14571 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014572
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014573 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014574 {
14575 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014576 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014577 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014578
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014579 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014580 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014581
Bram Moolenaar4463f292005-09-25 22:20:24 +000014582 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14583 if (xp_name == NULL)
14584 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014585
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014586 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014587
Bram Moolenaar4463f292005-09-25 22:20:24 +000014588 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14589 &xp_arg) == FAIL)
14590 return;
14591 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014592 }
14593
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014594 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014595 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014596 int save_ex_normal_busy = ex_normal_busy;
14597 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014599 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14600 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014601 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014602 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014603 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014604 && argvars[1].v_type != VAR_UNKNOWN
14605 && argvars[2].v_type != VAR_UNKNOWN)
14606 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14607 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014608
14609 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014611 /* since the user typed this, no need to wait for return */
14612 need_wait_return = FALSE;
14613 msg_didout = FALSE;
14614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014615 cmd_silent = cmd_silent_save;
14616}
14617
14618/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014619 * "input()" function
14620 * Also handles inputsecret() when inputsecret is set.
14621 */
14622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014623f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014624{
14625 get_user_input(argvars, rettv, FALSE);
14626}
14627
14628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014629 * "inputdialog()" function
14630 */
14631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014632f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014633{
14634#if defined(FEAT_GUI_TEXTDIALOG)
14635 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14636 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14637 {
14638 char_u *message;
14639 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014640 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014642 message = get_tv_string_chk(&argvars[0]);
14643 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014644 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014645 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014646 else
14647 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014648 if (message != NULL && defstr != NULL
14649 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014650 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014651 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014652 else
14653 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014654 if (message != NULL && defstr != NULL
14655 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014656 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014657 rettv->vval.v_string = vim_strsave(
14658 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014659 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014660 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014661 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014662 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 }
14664 else
14665#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014666 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014667}
14668
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014669/*
14670 * "inputlist()" function
14671 */
14672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014673f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014674{
14675 listitem_T *li;
14676 int selected;
14677 int mouse_used;
14678
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014679#ifdef NO_CONSOLE_INPUT
14680 /* While starting up, there is no place to enter text. */
14681 if (no_console_input())
14682 return;
14683#endif
14684 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14685 {
14686 EMSG2(_(e_listarg), "inputlist()");
14687 return;
14688 }
14689
14690 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014691 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014692 lines_left = Rows; /* avoid more prompt */
14693 msg_scroll = TRUE;
14694 msg_clr_eos();
14695
14696 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14697 {
14698 msg_puts(get_tv_string(&li->li_tv));
14699 msg_putchar('\n');
14700 }
14701
14702 /* Ask for choice. */
14703 selected = prompt_for_number(&mouse_used);
14704 if (mouse_used)
14705 selected -= lines_left;
14706
14707 rettv->vval.v_number = selected;
14708}
14709
14710
Bram Moolenaar071d4272004-06-13 20:20:40 +000014711static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14712
14713/*
14714 * "inputrestore()" function
14715 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014717f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014718{
14719 if (ga_userinput.ga_len > 0)
14720 {
14721 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014722 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14723 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014724 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014725 }
14726 else if (p_verbose > 1)
14727 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014728 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014729 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014730 }
14731}
14732
14733/*
14734 * "inputsave()" function
14735 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014737f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014738{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014739 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014740 if (ga_grow(&ga_userinput, 1) == OK)
14741 {
14742 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14743 + ga_userinput.ga_len);
14744 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014745 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014746 }
14747 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014748 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749}
14750
14751/*
14752 * "inputsecret()" function
14753 */
14754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014755f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014756{
14757 ++cmdline_star;
14758 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014759 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014760 --cmdline_star;
14761 --inputsecret_flag;
14762}
14763
14764/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014765 * "insert()" function
14766 */
14767 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014768f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014769{
14770 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014771 listitem_T *item;
14772 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014773 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014774
14775 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014776 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014777 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014778 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014779 {
14780 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014781 before = get_tv_number_chk(&argvars[2], &error);
14782 if (error)
14783 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014784
Bram Moolenaar758711c2005-02-02 23:11:38 +000014785 if (before == l->lv_len)
14786 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014787 else
14788 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014789 item = list_find(l, before);
14790 if (item == NULL)
14791 {
14792 EMSGN(_(e_listidx), before);
14793 l = NULL;
14794 }
14795 }
14796 if (l != NULL)
14797 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014798 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014799 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014800 }
14801 }
14802}
14803
14804/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014805 * "invert(expr)" function
14806 */
14807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014808f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014809{
14810 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14811}
14812
14813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014814 * "isdirectory()" function
14815 */
14816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014817f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014819 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014820}
14821
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014822/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014823 * "islocked()" function
14824 */
14825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014826f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014827{
14828 lval_T lv;
14829 char_u *end;
14830 dictitem_T *di;
14831
14832 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014833 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14834 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014835 if (end != NULL && lv.ll_name != NULL)
14836 {
14837 if (*end != NUL)
14838 EMSG(_(e_trailing));
14839 else
14840 {
14841 if (lv.ll_tv == NULL)
14842 {
14843 if (check_changedtick(lv.ll_name))
14844 rettv->vval.v_number = 1; /* always locked */
14845 else
14846 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014847 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014848 if (di != NULL)
14849 {
14850 /* Consider a variable locked when:
14851 * 1. the variable itself is locked
14852 * 2. the value of the variable is locked.
14853 * 3. the List or Dict value is locked.
14854 */
14855 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14856 || tv_islocked(&di->di_tv));
14857 }
14858 }
14859 }
14860 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014861 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014862 else if (lv.ll_newkey != NULL)
14863 EMSG2(_(e_dictkey), lv.ll_newkey);
14864 else if (lv.ll_list != NULL)
14865 /* List item. */
14866 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14867 else
14868 /* Dictionary item. */
14869 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14870 }
14871 }
14872
14873 clear_lval(&lv);
14874}
14875
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014876#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14877/*
14878 * "isnan()" function
14879 */
14880 static void
14881f_isnan(typval_T *argvars, typval_T *rettv)
14882{
14883 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14884 && isnan(argvars[0].vval.v_float);
14885}
14886#endif
14887
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014888static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014889
14890/*
14891 * Turn a dict into a list:
14892 * "what" == 0: list of keys
14893 * "what" == 1: list of values
14894 * "what" == 2: list of items
14895 */
14896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014897dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014898{
Bram Moolenaar33570922005-01-25 22:26:29 +000014899 list_T *l2;
14900 dictitem_T *di;
14901 hashitem_T *hi;
14902 listitem_T *li;
14903 listitem_T *li2;
14904 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014905 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014906
Bram Moolenaar8c711452005-01-14 21:53:12 +000014907 if (argvars[0].v_type != VAR_DICT)
14908 {
14909 EMSG(_(e_dictreq));
14910 return;
14911 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014912 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014913 return;
14914
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014915 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014916 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014917
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014921 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014922 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014923 --todo;
14924 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014925
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014926 li = listitem_alloc();
14927 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014928 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014929 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014930
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014931 if (what == 0)
14932 {
14933 /* keys() */
14934 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014935 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014936 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14937 }
14938 else if (what == 1)
14939 {
14940 /* values() */
14941 copy_tv(&di->di_tv, &li->li_tv);
14942 }
14943 else
14944 {
14945 /* items() */
14946 l2 = list_alloc();
14947 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014948 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014949 li->li_tv.vval.v_list = l2;
14950 if (l2 == NULL)
14951 break;
14952 ++l2->lv_refcount;
14953
14954 li2 = listitem_alloc();
14955 if (li2 == NULL)
14956 break;
14957 list_append(l2, li2);
14958 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014959 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014960 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14961
14962 li2 = listitem_alloc();
14963 if (li2 == NULL)
14964 break;
14965 list_append(l2, li2);
14966 copy_tv(&di->di_tv, &li2->li_tv);
14967 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014968 }
14969 }
14970}
14971
14972/*
14973 * "items(dict)" function
14974 */
14975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014976f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014977{
14978 dict_list(argvars, rettv, 2);
14979}
14980
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014981#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014982/*
14983 * Get the job from the argument.
14984 * Returns NULL if the job is invalid.
14985 */
14986 static job_T *
14987get_job_arg(typval_T *tv)
14988{
14989 job_T *job;
14990
14991 if (tv->v_type != VAR_JOB)
14992 {
14993 EMSG2(_(e_invarg2), get_tv_string(tv));
14994 return NULL;
14995 }
14996 job = tv->vval.v_job;
14997
14998 if (job == NULL)
14999 EMSG(_("E916: not a valid job"));
15000 return job;
15001}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015002
15003# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010015004/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015005 * "job_getchannel()" function
15006 */
15007 static void
15008f_job_getchannel(typval_T *argvars, typval_T *rettv)
15009{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015010 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015011
Bram Moolenaar65edff82016-02-21 16:40:11 +010015012 if (job != NULL)
15013 {
Bram Moolenaar77073442016-02-13 23:23:53 +010015014 rettv->v_type = VAR_CHANNEL;
15015 rettv->vval.v_channel = job->jv_channel;
15016 if (job->jv_channel != NULL)
15017 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015018 }
15019}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010015020# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015021
15022/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010015023 * "job_setoptions()" function
15024 */
15025 static void
15026f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
15027{
15028 job_T *job = get_job_arg(&argvars[0]);
15029 jobopt_T opt;
15030
15031 if (job == NULL)
15032 return;
15033 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015034 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010015035 return;
15036 job_set_options(job, &opt);
15037}
15038
15039/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015040 * "job_start()" function
15041 */
15042 static void
15043f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
15044{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015045 job_T *job;
15046 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015047#if defined(UNIX)
15048# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015049 char **argv = NULL;
15050 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015051#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015052 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015053#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015054 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015055
15056 rettv->v_type = VAR_JOB;
15057 job = job_alloc();
15058 rettv->vval.v_job = job;
15059 if (job == NULL)
15060 return;
15061
15062 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015063
15064 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015065 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015066 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015067 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015068 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015069 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015070 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015071 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015072
Bram Moolenaar835dc632016-02-07 14:27:38 +010015073#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015074 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015075#endif
15076
15077 if (argvars[0].v_type == VAR_STRING)
15078 {
15079 /* Command is a string. */
15080 cmd = argvars[0].vval.v_string;
15081#ifdef USE_ARGV
15082 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15083 return;
15084 argv[argc] = NULL;
15085#endif
15086 }
15087 else if (argvars[0].v_type != VAR_LIST
15088 || argvars[0].vval.v_list == NULL
15089 || argvars[0].vval.v_list->lv_len < 1)
15090 {
15091 EMSG(_(e_invarg));
15092 return;
15093 }
15094 else
15095 {
15096 list_T *l = argvars[0].vval.v_list;
15097 listitem_T *li;
15098 char_u *s;
15099
15100#ifdef USE_ARGV
15101 /* Pass argv[] to mch_call_shell(). */
15102 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15103 if (argv == NULL)
15104 return;
15105#endif
15106 for (li = l->lv_first; li != NULL; li = li->li_next)
15107 {
15108 s = get_tv_string_chk(&li->li_tv);
15109 if (s == NULL)
15110 goto theend;
15111#ifdef USE_ARGV
15112 argv[argc++] = (char *)s;
15113#else
15114 if (li != l->lv_first)
15115 {
15116 s = vim_strsave_shellescape(s, FALSE, TRUE);
15117 if (s == NULL)
15118 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015119 ga_concat(&ga, s);
15120 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015121 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015122 else
15123 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015124 if (li->li_next != NULL)
15125 ga_append(&ga, ' ');
15126#endif
15127 }
15128#ifdef USE_ARGV
15129 argv[argc] = NULL;
15130#else
15131 cmd = ga.ga_data;
15132#endif
15133 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015134
Bram Moolenaar835dc632016-02-07 14:27:38 +010015135#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015136# ifdef FEAT_CHANNEL
15137 if (ch_log_active())
15138 {
15139 garray_T ga;
15140 int i;
15141
15142 ga_init2(&ga, (int)sizeof(char), 200);
15143 for (i = 0; i < argc; ++i)
15144 {
15145 if (i > 0)
15146 ga_concat(&ga, (char_u *)" ");
15147 ga_concat(&ga, (char_u *)argv[i]);
15148 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015149 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015150 ga_clear(&ga);
15151 }
15152# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015153 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015154#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015155# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015156 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015157# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015158 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015159#endif
15160
15161theend:
15162#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015163 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015164#else
15165 vim_free(ga.ga_data);
15166#endif
15167}
15168
15169/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015170 * Get the status of "job" and invoke the exit callback when needed.
15171 * The returned string is not allocated.
15172 */
15173 static char *
15174job_status(job_T *job)
15175{
15176 char *result;
15177
15178 if (job->jv_status == JOB_ENDED)
15179 /* No need to check, dead is dead. */
15180 result = "dead";
15181 else if (job->jv_status == JOB_FAILED)
15182 result = "fail";
15183 else
15184 {
15185 result = mch_job_status(job);
15186# ifdef FEAT_CHANNEL
15187 if (job->jv_status == JOB_ENDED)
15188 ch_log(job->jv_channel, "Job ended");
15189# endif
15190 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15191 {
15192 typval_T argv[3];
15193 typval_T rettv;
15194 int dummy;
15195
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015196 /* invoke the exit callback; make sure the refcount is > 0 */
15197 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015198 argv[0].v_type = VAR_JOB;
15199 argv[0].vval.v_job = job;
15200 argv[1].v_type = VAR_NUMBER;
15201 argv[1].vval.v_number = job->jv_exitval;
15202 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15203 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15204 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015205 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015206 }
15207 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15208 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015209 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015210 * freed. Careful: caller must not use "job" after this! */
15211 job_free(job);
15212 }
15213 }
15214 return result;
15215}
15216
15217/*
15218 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15219 */
15220 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015221job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015222{
15223 static time_t last_check = 0;
15224 time_t now;
15225 job_T *job;
15226 job_T *next;
15227
15228 /* Only do this once in 10 seconds. */
15229 now = time(NULL);
15230 if (last_check + 10 < now)
15231 {
15232 last_check = now;
15233 for (job = first_job; job != NULL; job = next)
15234 {
15235 next = job->jv_next;
15236 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15237 job_status(job); /* may free "job" */
15238 }
15239 }
15240}
15241
15242/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015243 * "job_status()" function
15244 */
15245 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015246f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015247{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015248 job_T *job = get_job_arg(&argvars[0]);
15249 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015250
Bram Moolenaar65edff82016-02-21 16:40:11 +010015251 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015252 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015253 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015254 rettv->v_type = VAR_STRING;
15255 rettv->vval.v_string = vim_strsave((char_u *)result);
15256 }
15257}
15258
15259/*
15260 * "job_stop()" function
15261 */
15262 static void
15263f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15264{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015265 job_T *job = get_job_arg(&argvars[0]);
15266
15267 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015268 {
15269 char_u *arg;
15270
15271 if (argvars[1].v_type == VAR_UNKNOWN)
15272 arg = (char_u *)"";
15273 else
15274 {
15275 arg = get_tv_string_chk(&argvars[1]);
15276 if (arg == NULL)
15277 {
15278 EMSG(_(e_invarg));
15279 return;
15280 }
15281 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015282 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015283 rettv->vval.v_number = 0;
15284 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015285 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015286 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015287 /* Assume that "hup" does not kill the job. */
15288 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15289 job->jv_channel->ch_job_killed = TRUE;
15290 }
15291 /* We don't try freeing the job, obviously the caller still has a
15292 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015293 }
15294}
15295#endif
15296
Bram Moolenaar071d4272004-06-13 20:20:40 +000015297/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015298 * "join()" function
15299 */
15300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015301f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015302{
15303 garray_T ga;
15304 char_u *sep;
15305
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015306 if (argvars[0].v_type != VAR_LIST)
15307 {
15308 EMSG(_(e_listreq));
15309 return;
15310 }
15311 if (argvars[0].vval.v_list == NULL)
15312 return;
15313 if (argvars[1].v_type == VAR_UNKNOWN)
15314 sep = (char_u *)" ";
15315 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015316 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015317
15318 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015319
15320 if (sep != NULL)
15321 {
15322 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015323 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015324 ga_append(&ga, NUL);
15325 rettv->vval.v_string = (char_u *)ga.ga_data;
15326 }
15327 else
15328 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015329}
15330
15331/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015332 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015333 */
15334 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015335f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015336{
15337 js_read_T reader;
15338
15339 reader.js_buf = get_tv_string(&argvars[0]);
15340 reader.js_fill = NULL;
15341 reader.js_used = 0;
15342 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15343 EMSG(_(e_invarg));
15344}
15345
15346/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015347 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015348 */
15349 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015350f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015351{
15352 rettv->v_type = VAR_STRING;
15353 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15354}
15355
15356/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015357 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015358 */
15359 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015360f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015361{
15362 js_read_T reader;
15363
15364 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015365 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015366 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015367 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015368 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015369}
15370
15371/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015372 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015373 */
15374 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015375f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015376{
15377 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015378 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015379}
15380
15381/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015382 * "keys()" function
15383 */
15384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015385f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015386{
15387 dict_list(argvars, rettv, 0);
15388}
15389
15390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015391 * "last_buffer_nr()" function.
15392 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015394f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015395{
15396 int n = 0;
15397 buf_T *buf;
15398
15399 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15400 if (n < buf->b_fnum)
15401 n = buf->b_fnum;
15402
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015403 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404}
15405
15406/*
15407 * "len()" function
15408 */
15409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015410f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015411{
15412 switch (argvars[0].v_type)
15413 {
15414 case VAR_STRING:
15415 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = (varnumber_T)STRLEN(
15417 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015418 break;
15419 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015420 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015421 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015422 case VAR_DICT:
15423 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15424 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015425 case VAR_UNKNOWN:
15426 case VAR_SPECIAL:
15427 case VAR_FLOAT:
15428 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015429 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015430 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015431 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015432 break;
15433 }
15434}
15435
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015436static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015437
15438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015439libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015440{
15441#ifdef FEAT_LIBCALL
15442 char_u *string_in;
15443 char_u **string_result;
15444 int nr_result;
15445#endif
15446
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015447 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015448 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015449 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015450
15451 if (check_restricted() || check_secure())
15452 return;
15453
15454#ifdef FEAT_LIBCALL
15455 /* The first two args must be strings, otherwise its meaningless */
15456 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15457 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015458 string_in = NULL;
15459 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015460 string_in = argvars[2].vval.v_string;
15461 if (type == VAR_NUMBER)
15462 string_result = NULL;
15463 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015464 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015465 if (mch_libcall(argvars[0].vval.v_string,
15466 argvars[1].vval.v_string,
15467 string_in,
15468 argvars[2].vval.v_number,
15469 string_result,
15470 &nr_result) == OK
15471 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015472 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015473 }
15474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015475}
15476
15477/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015478 * "libcall()" function
15479 */
15480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015481f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015482{
15483 libcall_common(argvars, rettv, VAR_STRING);
15484}
15485
15486/*
15487 * "libcallnr()" function
15488 */
15489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015490f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015491{
15492 libcall_common(argvars, rettv, VAR_NUMBER);
15493}
15494
15495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 * "line(string)" function
15497 */
15498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015499f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500{
15501 linenr_T lnum = 0;
15502 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015503 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015505 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506 if (fp != NULL)
15507 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509}
15510
15511/*
15512 * "line2byte(lnum)" function
15513 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015515f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015516{
15517#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015519#else
15520 linenr_T lnum;
15521
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015522 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015524 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015525 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015526 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15527 if (rettv->vval.v_number >= 0)
15528 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529#endif
15530}
15531
15532/*
15533 * "lispindent(lnum)" function
15534 */
15535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015536f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015537{
15538#ifdef FEAT_LISP
15539 pos_T pos;
15540 linenr_T lnum;
15541
15542 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015543 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015544 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15545 {
15546 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015547 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015548 curwin->w_cursor = pos;
15549 }
15550 else
15551#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015552 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015553}
15554
15555/*
15556 * "localtime()" function
15557 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015559f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015560{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015561 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015562}
15563
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015564static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015565
15566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015567get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015568{
15569 char_u *keys;
15570 char_u *which;
15571 char_u buf[NUMBUFLEN];
15572 char_u *keys_buf = NULL;
15573 char_u *rhs;
15574 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015575 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015576 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015577 mapblock_T *mp;
15578 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579
15580 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015581 rettv->v_type = VAR_STRING;
15582 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015584 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015585 if (*keys == NUL)
15586 return;
15587
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015588 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015589 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015590 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015591 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015592 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015593 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015594 if (argvars[3].v_type != VAR_UNKNOWN)
15595 get_dict = get_tv_number(&argvars[3]);
15596 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015598 else
15599 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015600 if (which == NULL)
15601 return;
15602
Bram Moolenaar071d4272004-06-13 20:20:40 +000015603 mode = get_map_mode(&which, 0);
15604
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015605 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015606 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015607 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015608
15609 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015610 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015611 /* Return a string. */
15612 if (rhs != NULL)
15613 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015614
Bram Moolenaarbd743252010-10-20 21:23:33 +020015615 }
15616 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15617 {
15618 /* Return a dictionary. */
15619 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15620 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15621 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015622
Bram Moolenaarbd743252010-10-20 21:23:33 +020015623 dict_add_nr_str(dict, "lhs", 0L, lhs);
15624 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15625 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15626 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15627 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15628 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15629 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015630 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015631 dict_add_nr_str(dict, "mode", 0L, mapmode);
15632
15633 vim_free(lhs);
15634 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015635 }
15636}
15637
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015638#ifdef FEAT_FLOAT
15639/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015640 * "log()" function
15641 */
15642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015643f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015644{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015645 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015646
15647 rettv->v_type = VAR_FLOAT;
15648 if (get_float_arg(argvars, &f) == OK)
15649 rettv->vval.v_float = log(f);
15650 else
15651 rettv->vval.v_float = 0.0;
15652}
15653
15654/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015655 * "log10()" function
15656 */
15657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015658f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015659{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015660 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015661
15662 rettv->v_type = VAR_FLOAT;
15663 if (get_float_arg(argvars, &f) == OK)
15664 rettv->vval.v_float = log10(f);
15665 else
15666 rettv->vval.v_float = 0.0;
15667}
15668#endif
15669
Bram Moolenaar1dced572012-04-05 16:54:08 +020015670#ifdef FEAT_LUA
15671/*
15672 * "luaeval()" function
15673 */
15674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015675f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015676{
15677 char_u *str;
15678 char_u buf[NUMBUFLEN];
15679
15680 str = get_tv_string_buf(&argvars[0], buf);
15681 do_luaeval(str, argvars + 1, rettv);
15682}
15683#endif
15684
Bram Moolenaar071d4272004-06-13 20:20:40 +000015685/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015686 * "map()" function
15687 */
15688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015689f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015690{
15691 filter_map(argvars, rettv, TRUE);
15692}
15693
15694/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015695 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 */
15697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015698f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015699{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015700 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015701}
15702
15703/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015704 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015705 */
15706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015707f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015708{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015709 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015710}
15711
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015712static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015713
15714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015715find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015717 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015718 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015719 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015720 char_u *pat;
15721 regmatch_T regmatch;
15722 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015723 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015724 char_u *save_cpo;
15725 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015726 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015727 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015728 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015729 list_T *l = NULL;
15730 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015731 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015732 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015733
15734 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15735 save_cpo = p_cpo;
15736 p_cpo = (char_u *)"";
15737
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015738 rettv->vval.v_number = -1;
15739 if (type == 3)
15740 {
15741 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015742 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015743 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015744 }
15745 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015746 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015747 rettv->v_type = VAR_STRING;
15748 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015749 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015751 if (argvars[0].v_type == VAR_LIST)
15752 {
15753 if ((l = argvars[0].vval.v_list) == NULL)
15754 goto theend;
15755 li = l->lv_first;
15756 }
15757 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015758 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015759 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015760 len = (long)STRLEN(str);
15761 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015762
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015763 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15764 if (pat == NULL)
15765 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015766
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015767 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015769 int error = FALSE;
15770
15771 start = get_tv_number_chk(&argvars[2], &error);
15772 if (error)
15773 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015774 if (l != NULL)
15775 {
15776 li = list_find(l, start);
15777 if (li == NULL)
15778 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015779 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015780 }
15781 else
15782 {
15783 if (start < 0)
15784 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015785 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015786 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015787 /* When "count" argument is there ignore matches before "start",
15788 * otherwise skip part of the string. Differs when pattern is "^"
15789 * or "\<". */
15790 if (argvars[3].v_type != VAR_UNKNOWN)
15791 startcol = start;
15792 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015793 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015794 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015795 len -= start;
15796 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015797 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015798
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015799 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015800 nth = get_tv_number_chk(&argvars[3], &error);
15801 if (error)
15802 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 }
15804
15805 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15806 if (regmatch.regprog != NULL)
15807 {
15808 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015809
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015810 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015811 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015812 if (l != NULL)
15813 {
15814 if (li == NULL)
15815 {
15816 match = FALSE;
15817 break;
15818 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015820 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015821 if (str == NULL)
15822 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015823 }
15824
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015825 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015826
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015828 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015829 if (l == NULL && !match)
15830 break;
15831
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015832 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015833 if (l != NULL)
15834 {
15835 li = li->li_next;
15836 ++idx;
15837 }
15838 else
15839 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015840#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015841 startcol = (colnr_T)(regmatch.startp[0]
15842 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015843#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015844 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015845#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015846 if (startcol > (colnr_T)len
15847 || str + startcol <= regmatch.startp[0])
15848 {
15849 match = FALSE;
15850 break;
15851 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015852 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015853 }
15854
15855 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015857 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015858 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015859 int i;
15860
15861 /* return list with matched string and submatches */
15862 for (i = 0; i < NSUBEXP; ++i)
15863 {
15864 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015865 {
15866 if (list_append_string(rettv->vval.v_list,
15867 (char_u *)"", 0) == FAIL)
15868 break;
15869 }
15870 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015871 regmatch.startp[i],
15872 (int)(regmatch.endp[i] - regmatch.startp[i]))
15873 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015874 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015875 }
15876 }
15877 else if (type == 2)
15878 {
15879 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015880 if (l != NULL)
15881 copy_tv(&li->li_tv, rettv);
15882 else
15883 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015885 }
15886 else if (l != NULL)
15887 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888 else
15889 {
15890 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015891 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015892 (varnumber_T)(regmatch.startp[0] - str);
15893 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015894 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015896 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015897 }
15898 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015899 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015900 }
15901
15902theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015903 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015904 p_cpo = save_cpo;
15905}
15906
15907/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015908 * "match()" function
15909 */
15910 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015911f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912{
15913 find_some_match(argvars, rettv, 1);
15914}
15915
15916/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015917 * "matchadd()" function
15918 */
15919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015920f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015921{
15922#ifdef FEAT_SEARCH_EXTRA
15923 char_u buf[NUMBUFLEN];
15924 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15925 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15926 int prio = 10; /* default priority */
15927 int id = -1;
15928 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015929 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015930
15931 rettv->vval.v_number = -1;
15932
15933 if (grp == NULL || pat == NULL)
15934 return;
15935 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015936 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015937 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015938 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015939 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015940 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015941 if (argvars[4].v_type != VAR_UNKNOWN)
15942 {
15943 if (argvars[4].v_type != VAR_DICT)
15944 {
15945 EMSG(_(e_dictreq));
15946 return;
15947 }
15948 if (dict_find(argvars[4].vval.v_dict,
15949 (char_u *)"conceal", -1) != NULL)
15950 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15951 (char_u *)"conceal", FALSE);
15952 }
15953 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015954 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015955 if (error == TRUE)
15956 return;
15957 if (id >= 1 && id <= 3)
15958 {
15959 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15960 return;
15961 }
15962
Bram Moolenaar6561d522015-07-21 15:48:27 +020015963 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15964 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015965#endif
15966}
15967
15968/*
15969 * "matchaddpos()" function
15970 */
15971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015972f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015973{
15974#ifdef FEAT_SEARCH_EXTRA
15975 char_u buf[NUMBUFLEN];
15976 char_u *group;
15977 int prio = 10;
15978 int id = -1;
15979 int error = FALSE;
15980 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015981 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015982
15983 rettv->vval.v_number = -1;
15984
15985 group = get_tv_string_buf_chk(&argvars[0], buf);
15986 if (group == NULL)
15987 return;
15988
15989 if (argvars[1].v_type != VAR_LIST)
15990 {
15991 EMSG2(_(e_listarg), "matchaddpos()");
15992 return;
15993 }
15994 l = argvars[1].vval.v_list;
15995 if (l == NULL)
15996 return;
15997
15998 if (argvars[2].v_type != VAR_UNKNOWN)
15999 {
16000 prio = get_tv_number_chk(&argvars[2], &error);
16001 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020016002 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020016003 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020016004 if (argvars[4].v_type != VAR_UNKNOWN)
16005 {
16006 if (argvars[4].v_type != VAR_DICT)
16007 {
16008 EMSG(_(e_dictreq));
16009 return;
16010 }
16011 if (dict_find(argvars[4].vval.v_dict,
16012 (char_u *)"conceal", -1) != NULL)
16013 conceal_char = get_dict_string(argvars[4].vval.v_dict,
16014 (char_u *)"conceal", FALSE);
16015 }
16016 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020016017 }
16018 if (error == TRUE)
16019 return;
16020
16021 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
16022 if (id == 1 || id == 2)
16023 {
16024 EMSGN("E798: ID is reserved for \":match\": %ld", id);
16025 return;
16026 }
16027
Bram Moolenaar6561d522015-07-21 15:48:27 +020016028 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
16029 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016030#endif
16031}
16032
16033/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016034 * "matcharg()" function
16035 */
16036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016037f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016038{
16039 if (rettv_list_alloc(rettv) == OK)
16040 {
16041#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016042 int id = get_tv_number(&argvars[0]);
16043 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016044
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016045 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016046 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016047 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
16048 {
16049 list_append_string(rettv->vval.v_list,
16050 syn_id2name(m->hlg_id), -1);
16051 list_append_string(rettv->vval.v_list, m->pattern, -1);
16052 }
16053 else
16054 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016055 list_append_string(rettv->vval.v_list, NULL, -1);
16056 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016057 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016058 }
16059#endif
16060 }
16061}
16062
16063/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016064 * "matchdelete()" function
16065 */
16066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016067f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016068{
16069#ifdef FEAT_SEARCH_EXTRA
16070 rettv->vval.v_number = match_delete(curwin,
16071 (int)get_tv_number(&argvars[0]), TRUE);
16072#endif
16073}
16074
16075/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016076 * "matchend()" function
16077 */
16078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016079f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016080{
16081 find_some_match(argvars, rettv, 0);
16082}
16083
16084/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085 * "matchlist()" function
16086 */
16087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016088f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016089{
16090 find_some_match(argvars, rettv, 3);
16091}
16092
16093/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016094 * "matchstr()" function
16095 */
16096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016097f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016098{
16099 find_some_match(argvars, rettv, 2);
16100}
16101
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016102static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016103
16104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016105max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016106{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016107 long n = 0;
16108 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016110
16111 if (argvars[0].v_type == VAR_LIST)
16112 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016113 list_T *l;
16114 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016115
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016116 l = argvars[0].vval.v_list;
16117 if (l != NULL)
16118 {
16119 li = l->lv_first;
16120 if (li != NULL)
16121 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016122 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016123 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016124 {
16125 li = li->li_next;
16126 if (li == NULL)
16127 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016128 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016129 if (domax ? i > n : i < n)
16130 n = i;
16131 }
16132 }
16133 }
16134 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016135 else if (argvars[0].v_type == VAR_DICT)
16136 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016137 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016138 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016139 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016140 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016141
16142 d = argvars[0].vval.v_dict;
16143 if (d != NULL)
16144 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016145 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016146 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016148 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016149 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016150 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016151 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016152 if (first)
16153 {
16154 n = i;
16155 first = FALSE;
16156 }
16157 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016158 n = i;
16159 }
16160 }
16161 }
16162 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016163 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016164 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016165 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016166}
16167
16168/*
16169 * "max()" function
16170 */
16171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016172f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016173{
16174 max_min(argvars, rettv, TRUE);
16175}
16176
16177/*
16178 * "min()" function
16179 */
16180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016181f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016182{
16183 max_min(argvars, rettv, FALSE);
16184}
16185
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016186static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016187
16188/*
16189 * Create the directory in which "dir" is located, and higher levels when
16190 * needed.
16191 */
16192 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016193mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016194{
16195 char_u *p;
16196 char_u *updir;
16197 int r = FAIL;
16198
16199 /* Get end of directory name in "dir".
16200 * We're done when it's "/" or "c:/". */
16201 p = gettail_sep(dir);
16202 if (p <= get_past_head(dir))
16203 return OK;
16204
16205 /* If the directory exists we're done. Otherwise: create it.*/
16206 updir = vim_strnsave(dir, (int)(p - dir));
16207 if (updir == NULL)
16208 return FAIL;
16209 if (mch_isdir(updir))
16210 r = OK;
16211 else if (mkdir_recurse(updir, prot) == OK)
16212 r = vim_mkdir_emsg(updir, prot);
16213 vim_free(updir);
16214 return r;
16215}
16216
16217#ifdef vim_mkdir
16218/*
16219 * "mkdir()" function
16220 */
16221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016222f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016223{
16224 char_u *dir;
16225 char_u buf[NUMBUFLEN];
16226 int prot = 0755;
16227
16228 rettv->vval.v_number = FAIL;
16229 if (check_restricted() || check_secure())
16230 return;
16231
16232 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016233 if (*dir == NUL)
16234 rettv->vval.v_number = FAIL;
16235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016236 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016237 if (*gettail(dir) == NUL)
16238 /* remove trailing slashes */
16239 *gettail_sep(dir) = NUL;
16240
16241 if (argvars[1].v_type != VAR_UNKNOWN)
16242 {
16243 if (argvars[2].v_type != VAR_UNKNOWN)
16244 prot = get_tv_number_chk(&argvars[2], NULL);
16245 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16246 mkdir_recurse(dir, prot);
16247 }
16248 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016249 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016250}
16251#endif
16252
Bram Moolenaar0d660222005-01-07 21:51:51 +000016253/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 * "mode()" function
16255 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016257f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016259 char_u buf[3];
16260
16261 buf[1] = NUL;
16262 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016263
Bram Moolenaar071d4272004-06-13 20:20:40 +000016264 if (VIsual_active)
16265 {
16266 if (VIsual_select)
16267 buf[0] = VIsual_mode + 's' - 'v';
16268 else
16269 buf[0] = VIsual_mode;
16270 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016271 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016272 || State == CONFIRM)
16273 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016274 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016275 if (State == ASKMORE)
16276 buf[1] = 'm';
16277 else if (State == CONFIRM)
16278 buf[1] = '?';
16279 }
16280 else if (State == EXTERNCMD)
16281 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016282 else if (State & INSERT)
16283 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016284#ifdef FEAT_VREPLACE
16285 if (State & VREPLACE_FLAG)
16286 {
16287 buf[0] = 'R';
16288 buf[1] = 'v';
16289 }
16290 else
16291#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016292 if (State & REPLACE_FLAG)
16293 buf[0] = 'R';
16294 else
16295 buf[0] = 'i';
16296 }
16297 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016298 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016300 if (exmode_active)
16301 buf[1] = 'v';
16302 }
16303 else if (exmode_active)
16304 {
16305 buf[0] = 'c';
16306 buf[1] = 'e';
16307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016308 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016309 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016310 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016311 if (finish_op)
16312 buf[1] = 'o';
16313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016314
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016315 /* Clear out the minor mode when the argument is not a non-zero number or
16316 * non-empty string. */
16317 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016318 buf[1] = NUL;
16319
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016320 rettv->vval.v_string = vim_strsave(buf);
16321 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322}
16323
Bram Moolenaar429fa852013-04-15 12:27:36 +020016324#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016325/*
16326 * "mzeval()" function
16327 */
16328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016329f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016330{
16331 char_u *str;
16332 char_u buf[NUMBUFLEN];
16333
16334 str = get_tv_string_buf(&argvars[0], buf);
16335 do_mzeval(str, rettv);
16336}
Bram Moolenaar75676462013-01-30 14:55:42 +010016337
16338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016339mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016340{
16341 typval_T argvars[3];
16342
16343 argvars[0].v_type = VAR_STRING;
16344 argvars[0].vval.v_string = name;
16345 copy_tv(args, &argvars[1]);
16346 argvars[2].v_type = VAR_UNKNOWN;
16347 f_call(argvars, rettv);
16348 clear_tv(&argvars[1]);
16349}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016350#endif
16351
Bram Moolenaar071d4272004-06-13 20:20:40 +000016352/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016353 * "nextnonblank()" function
16354 */
16355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016356f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016357{
16358 linenr_T lnum;
16359
16360 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16361 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016362 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016363 {
16364 lnum = 0;
16365 break;
16366 }
16367 if (*skipwhite(ml_get(lnum)) != NUL)
16368 break;
16369 }
16370 rettv->vval.v_number = lnum;
16371}
16372
16373/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016374 * "nr2char()" function
16375 */
16376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016377f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016378{
16379 char_u buf[NUMBUFLEN];
16380
16381#ifdef FEAT_MBYTE
16382 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016383 {
16384 int utf8 = 0;
16385
16386 if (argvars[1].v_type != VAR_UNKNOWN)
16387 utf8 = get_tv_number_chk(&argvars[1], NULL);
16388 if (utf8)
16389 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16390 else
16391 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016393 else
16394#endif
16395 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016396 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016397 buf[1] = NUL;
16398 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016399 rettv->v_type = VAR_STRING;
16400 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016401}
16402
16403/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016404 * "or(expr, expr)" function
16405 */
16406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016407f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016408{
16409 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16410 | get_tv_number_chk(&argvars[1], NULL);
16411}
16412
16413/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016414 * "pathshorten()" function
16415 */
16416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016417f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016418{
16419 char_u *p;
16420
16421 rettv->v_type = VAR_STRING;
16422 p = get_tv_string_chk(&argvars[0]);
16423 if (p == NULL)
16424 rettv->vval.v_string = NULL;
16425 else
16426 {
16427 p = vim_strsave(p);
16428 rettv->vval.v_string = p;
16429 if (p != NULL)
16430 shorten_dir(p);
16431 }
16432}
16433
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016434#ifdef FEAT_PERL
16435/*
16436 * "perleval()" function
16437 */
16438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016439f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016440{
16441 char_u *str;
16442 char_u buf[NUMBUFLEN];
16443
16444 str = get_tv_string_buf(&argvars[0], buf);
16445 do_perleval(str, rettv);
16446}
16447#endif
16448
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016449#ifdef FEAT_FLOAT
16450/*
16451 * "pow()" function
16452 */
16453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016454f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016455{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016456 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016457
16458 rettv->v_type = VAR_FLOAT;
16459 if (get_float_arg(argvars, &fx) == OK
16460 && get_float_arg(&argvars[1], &fy) == OK)
16461 rettv->vval.v_float = pow(fx, fy);
16462 else
16463 rettv->vval.v_float = 0.0;
16464}
16465#endif
16466
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016467/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016468 * "prevnonblank()" function
16469 */
16470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016471f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472{
16473 linenr_T lnum;
16474
16475 lnum = get_tv_lnum(argvars);
16476 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16477 lnum = 0;
16478 else
16479 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16480 --lnum;
16481 rettv->vval.v_number = lnum;
16482}
16483
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016484/* This dummy va_list is here because:
16485 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16486 * - locally in the function results in a "used before set" warning
16487 * - using va_start() to initialize it gives "function with fixed args" error */
16488static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016489
Bram Moolenaar8c711452005-01-14 21:53:12 +000016490/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016491 * "printf()" function
16492 */
16493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016495{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016496 char_u buf[NUMBUFLEN];
16497 int len;
16498 char_u *s;
16499 int saved_did_emsg = did_emsg;
16500 char *fmt;
16501
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016502 rettv->v_type = VAR_STRING;
16503 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016504
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016505 /* Get the required length, allocate the buffer and do it for real. */
16506 did_emsg = FALSE;
16507 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16508 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16509 if (!did_emsg)
16510 {
16511 s = alloc(len + 1);
16512 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016513 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016514 rettv->vval.v_string = s;
16515 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016516 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016517 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016518 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016519}
16520
16521/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016522 * "pumvisible()" function
16523 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016526{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016527#ifdef FEAT_INS_EXPAND
16528 if (pum_visible())
16529 rettv->vval.v_number = 1;
16530#endif
16531}
16532
Bram Moolenaardb913952012-06-29 12:54:53 +020016533#ifdef FEAT_PYTHON3
16534/*
16535 * "py3eval()" function
16536 */
16537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016538f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016539{
16540 char_u *str;
16541 char_u buf[NUMBUFLEN];
16542
16543 str = get_tv_string_buf(&argvars[0], buf);
16544 do_py3eval(str, rettv);
16545}
16546#endif
16547
16548#ifdef FEAT_PYTHON
16549/*
16550 * "pyeval()" function
16551 */
16552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016553f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016554{
16555 char_u *str;
16556 char_u buf[NUMBUFLEN];
16557
16558 str = get_tv_string_buf(&argvars[0], buf);
16559 do_pyeval(str, rettv);
16560}
16561#endif
16562
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016563/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016564 * "range()" function
16565 */
16566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016567f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016568{
16569 long start;
16570 long end;
16571 long stride = 1;
16572 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016573 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016575 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016576 if (argvars[1].v_type == VAR_UNKNOWN)
16577 {
16578 end = start - 1;
16579 start = 0;
16580 }
16581 else
16582 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016583 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016584 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016585 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016586 }
16587
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016588 if (error)
16589 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016590 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016591 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016592 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016593 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016594 else
16595 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016596 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016597 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016598 if (list_append_number(rettv->vval.v_list,
16599 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016600 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016601 }
16602}
16603
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016604/*
16605 * "readfile()" function
16606 */
16607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016608f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016609{
16610 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016611 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016612 char_u *fname;
16613 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016614 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16615 int io_size = sizeof(buf);
16616 int readlen; /* size of last fread() */
16617 char_u *prev = NULL; /* previously read bytes, if any */
16618 long prevlen = 0; /* length of data in prev */
16619 long prevsize = 0; /* size of prev buffer */
16620 long maxline = MAXLNUM;
16621 long cnt = 0;
16622 char_u *p; /* position in buf */
16623 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016624
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016625 if (argvars[1].v_type != VAR_UNKNOWN)
16626 {
16627 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16628 binary = TRUE;
16629 if (argvars[2].v_type != VAR_UNKNOWN)
16630 maxline = get_tv_number(&argvars[2]);
16631 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016632
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016633 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016634 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016635
16636 /* Always open the file in binary mode, library functions have a mind of
16637 * their own about CR-LF conversion. */
16638 fname = get_tv_string(&argvars[0]);
16639 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16640 {
16641 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16642 return;
16643 }
16644
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016645 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016646 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016647 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016648
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016649 /* This for loop processes what was read, but is also entered at end
16650 * of file so that either:
16651 * - an incomplete line gets written
16652 * - a "binary" file gets an empty line at the end if it ends in a
16653 * newline. */
16654 for (p = buf, start = buf;
16655 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16656 ++p)
16657 {
16658 if (*p == '\n' || readlen <= 0)
16659 {
16660 listitem_T *li;
16661 char_u *s = NULL;
16662 long_u len = p - start;
16663
16664 /* Finished a line. Remove CRs before NL. */
16665 if (readlen > 0 && !binary)
16666 {
16667 while (len > 0 && start[len - 1] == '\r')
16668 --len;
16669 /* removal may cross back to the "prev" string */
16670 if (len == 0)
16671 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16672 --prevlen;
16673 }
16674 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016675 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016676 else
16677 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016678 /* Change "prev" buffer to be the right size. This way
16679 * the bytes are only copied once, and very long lines are
16680 * allocated only once. */
16681 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016682 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016683 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016684 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016685 prev = NULL; /* the list will own the string */
16686 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016687 }
16688 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016689 if (s == NULL)
16690 {
16691 do_outofmem_msg((long_u) prevlen + len + 1);
16692 failed = TRUE;
16693 break;
16694 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016695
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016696 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016697 {
16698 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016699 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016700 break;
16701 }
16702 li->li_tv.v_type = VAR_STRING;
16703 li->li_tv.v_lock = 0;
16704 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016705 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016706
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016707 start = p + 1; /* step over newline */
16708 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016709 break;
16710 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016711 else if (*p == NUL)
16712 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016713#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016714 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16715 * when finding the BF and check the previous two bytes. */
16716 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016717 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016718 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16719 * + 1, these may be in the "prev" string. */
16720 char_u back1 = p >= buf + 1 ? p[-1]
16721 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16722 char_u back2 = p >= buf + 2 ? p[-2]
16723 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16724 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016725
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016726 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016727 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016728 char_u *dest = p - 2;
16729
16730 /* Usually a BOM is at the beginning of a file, and so at
16731 * the beginning of a line; then we can just step over it.
16732 */
16733 if (start == dest)
16734 start = p + 1;
16735 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016736 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016737 /* have to shuffle buf to close gap */
16738 int adjust_prevlen = 0;
16739
16740 if (dest < buf)
16741 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016742 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016743 dest = buf;
16744 }
16745 if (readlen > p - buf + 1)
16746 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16747 readlen -= 3 - adjust_prevlen;
16748 prevlen -= adjust_prevlen;
16749 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016750 }
16751 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016752 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016753#endif
16754 } /* for */
16755
16756 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16757 break;
16758 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016759 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016760 /* There's part of a line in buf, store it in "prev". */
16761 if (p - start + prevlen >= prevsize)
16762 {
16763 /* need bigger "prev" buffer */
16764 char_u *newprev;
16765
16766 /* A common use case is ordinary text files and "prev" gets a
16767 * fragment of a line, so the first allocation is made
16768 * small, to avoid repeatedly 'allocing' large and
16769 * 'reallocing' small. */
16770 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016771 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016772 else
16773 {
16774 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016775 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016776 prevsize = grow50pc > growmin ? grow50pc : growmin;
16777 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016778 newprev = prev == NULL ? alloc(prevsize)
16779 : vim_realloc(prev, prevsize);
16780 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016781 {
16782 do_outofmem_msg((long_u)prevsize);
16783 failed = TRUE;
16784 break;
16785 }
16786 prev = newprev;
16787 }
16788 /* Add the line part to end of "prev". */
16789 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016790 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016791 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016792 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016793
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016794 /*
16795 * For a negative line count use only the lines at the end of the file,
16796 * free the rest.
16797 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016798 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016799 while (cnt > -maxline)
16800 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016802 --cnt;
16803 }
16804
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016805 if (failed)
16806 {
16807 list_free(rettv->vval.v_list, TRUE);
16808 /* readfile doc says an empty list is returned on error */
16809 rettv->vval.v_list = list_alloc();
16810 }
16811
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016812 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016813 fclose(fd);
16814}
16815
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016816#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016817static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016818
16819/*
16820 * Convert a List to proftime_T.
16821 * Return FAIL when there is something wrong.
16822 */
16823 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016824list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016825{
16826 long n1, n2;
16827 int error = FALSE;
16828
16829 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16830 || arg->vval.v_list->lv_len != 2)
16831 return FAIL;
16832 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16833 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16834# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016835 tm->HighPart = n1;
16836 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016837# else
16838 tm->tv_sec = n1;
16839 tm->tv_usec = n2;
16840# endif
16841 return error ? FAIL : OK;
16842}
16843#endif /* FEAT_RELTIME */
16844
16845/*
16846 * "reltime()" function
16847 */
16848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016849f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016850{
16851#ifdef FEAT_RELTIME
16852 proftime_T res;
16853 proftime_T start;
16854
16855 if (argvars[0].v_type == VAR_UNKNOWN)
16856 {
16857 /* No arguments: get current time. */
16858 profile_start(&res);
16859 }
16860 else if (argvars[1].v_type == VAR_UNKNOWN)
16861 {
16862 if (list2proftime(&argvars[0], &res) == FAIL)
16863 return;
16864 profile_end(&res);
16865 }
16866 else
16867 {
16868 /* Two arguments: compute the difference. */
16869 if (list2proftime(&argvars[0], &start) == FAIL
16870 || list2proftime(&argvars[1], &res) == FAIL)
16871 return;
16872 profile_sub(&res, &start);
16873 }
16874
16875 if (rettv_list_alloc(rettv) == OK)
16876 {
16877 long n1, n2;
16878
16879# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016880 n1 = res.HighPart;
16881 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016882# else
16883 n1 = res.tv_sec;
16884 n2 = res.tv_usec;
16885# endif
16886 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16887 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16888 }
16889#endif
16890}
16891
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016892#ifdef FEAT_FLOAT
16893/*
16894 * "reltimefloat()" function
16895 */
16896 static void
16897f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16898{
16899# ifdef FEAT_RELTIME
16900 proftime_T tm;
16901# endif
16902
16903 rettv->v_type = VAR_FLOAT;
16904 rettv->vval.v_float = 0;
16905# ifdef FEAT_RELTIME
16906 if (list2proftime(&argvars[0], &tm) == OK)
16907 rettv->vval.v_float = profile_float(&tm);
16908# endif
16909}
16910#endif
16911
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016912/*
16913 * "reltimestr()" function
16914 */
16915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016916f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016917{
16918#ifdef FEAT_RELTIME
16919 proftime_T tm;
16920#endif
16921
16922 rettv->v_type = VAR_STRING;
16923 rettv->vval.v_string = NULL;
16924#ifdef FEAT_RELTIME
16925 if (list2proftime(&argvars[0], &tm) == OK)
16926 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16927#endif
16928}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016929
Bram Moolenaar0d660222005-01-07 21:51:51 +000016930#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016931static void make_connection(void);
16932static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933
16934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936{
16937 if (X_DISPLAY == NULL
16938# ifdef FEAT_GUI
16939 && !gui.in_use
16940# endif
16941 )
16942 {
16943 x_force_connect = TRUE;
16944 setup_term_clip();
16945 x_force_connect = FALSE;
16946 }
16947}
16948
16949 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016950check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016951{
16952 make_connection();
16953 if (X_DISPLAY == NULL)
16954 {
16955 EMSG(_("E240: No connection to Vim server"));
16956 return FAIL;
16957 }
16958 return OK;
16959}
16960#endif
16961
16962#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016963static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016964
16965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016966remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016967{
16968 char_u *server_name;
16969 char_u *keys;
16970 char_u *r = NULL;
16971 char_u buf[NUMBUFLEN];
16972# ifdef WIN32
16973 HWND w;
16974# else
16975 Window w;
16976# endif
16977
16978 if (check_restricted() || check_secure())
16979 return;
16980
16981# ifdef FEAT_X11
16982 if (check_connection() == FAIL)
16983 return;
16984# endif
16985
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016986 server_name = get_tv_string_chk(&argvars[0]);
16987 if (server_name == NULL)
16988 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989 keys = get_tv_string_buf(&argvars[1], buf);
16990# ifdef WIN32
16991 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16992# else
16993 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16994 < 0)
16995# endif
16996 {
16997 if (r != NULL)
16998 EMSG(r); /* sending worked but evaluation failed */
16999 else
17000 EMSG2(_("E241: Unable to send to %s"), server_name);
17001 return;
17002 }
17003
17004 rettv->vval.v_string = r;
17005
17006 if (argvars[2].v_type != VAR_UNKNOWN)
17007 {
Bram Moolenaar33570922005-01-25 22:26:29 +000017008 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000017009 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017010 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017012 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 v.di_tv.v_type = VAR_STRING;
17014 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 idvar = get_tv_string_chk(&argvars[2]);
17016 if (idvar != NULL)
17017 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017018 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019 }
17020}
17021#endif
17022
17023/*
17024 * "remote_expr()" function
17025 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017027f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028{
17029 rettv->v_type = VAR_STRING;
17030 rettv->vval.v_string = NULL;
17031#ifdef FEAT_CLIENTSERVER
17032 remote_common(argvars, rettv, TRUE);
17033#endif
17034}
17035
17036/*
17037 * "remote_foreground()" function
17038 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017040f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017042#ifdef FEAT_CLIENTSERVER
17043# ifdef WIN32
17044 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 {
17046 char_u *server_name = get_tv_string_chk(&argvars[0]);
17047
17048 if (server_name != NULL)
17049 serverForeground(server_name);
17050 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017051# else
17052 /* Send a foreground() expression to the server. */
17053 argvars[1].v_type = VAR_STRING;
17054 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17055 argvars[2].v_type = VAR_UNKNOWN;
17056 remote_common(argvars, rettv, TRUE);
17057 vim_free(argvars[1].vval.v_string);
17058# endif
17059#endif
17060}
17061
Bram Moolenaar0d660222005-01-07 21:51:51 +000017062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017063f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064{
17065#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017066 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017067 char_u *s = NULL;
17068# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017069 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017071 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017072
17073 if (check_restricted() || check_secure())
17074 {
17075 rettv->vval.v_number = -1;
17076 return;
17077 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017078 serverid = get_tv_string_chk(&argvars[0]);
17079 if (serverid == NULL)
17080 {
17081 rettv->vval.v_number = -1;
17082 return; /* type error; errmsg already given */
17083 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017085 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017086 if (n == 0)
17087 rettv->vval.v_number = -1;
17088 else
17089 {
17090 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17091 rettv->vval.v_number = (s != NULL);
17092 }
17093# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017094 if (check_connection() == FAIL)
17095 return;
17096
17097 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017098 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017099# endif
17100
17101 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17102 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017103 char_u *retvar;
17104
Bram Moolenaar33570922005-01-25 22:26:29 +000017105 v.di_tv.v_type = VAR_STRING;
17106 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017107 retvar = get_tv_string_chk(&argvars[1]);
17108 if (retvar != NULL)
17109 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017110 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017111 }
17112#else
17113 rettv->vval.v_number = -1;
17114#endif
17115}
17116
Bram Moolenaar0d660222005-01-07 21:51:51 +000017117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017118f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017119{
17120 char_u *r = NULL;
17121
17122#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017123 char_u *serverid = get_tv_string_chk(&argvars[0]);
17124
17125 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017126 {
17127# ifdef WIN32
17128 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017129 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017130
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017131 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017132 if (n != 0)
17133 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17134 if (r == NULL)
17135# else
17136 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017138# endif
17139 EMSG(_("E277: Unable to read a server reply"));
17140 }
17141#endif
17142 rettv->v_type = VAR_STRING;
17143 rettv->vval.v_string = r;
17144}
17145
17146/*
17147 * "remote_send()" function
17148 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017150f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017151{
17152 rettv->v_type = VAR_STRING;
17153 rettv->vval.v_string = NULL;
17154#ifdef FEAT_CLIENTSERVER
17155 remote_common(argvars, rettv, FALSE);
17156#endif
17157}
17158
17159/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017160 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017161 */
17162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017163f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017164{
Bram Moolenaar33570922005-01-25 22:26:29 +000017165 list_T *l;
17166 listitem_T *item, *item2;
17167 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017168 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017169 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017170 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017171 dict_T *d;
17172 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017173 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017174
Bram Moolenaar8c711452005-01-14 21:53:12 +000017175 if (argvars[0].v_type == VAR_DICT)
17176 {
17177 if (argvars[2].v_type != VAR_UNKNOWN)
17178 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017179 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017180 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017181 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017182 key = get_tv_string_chk(&argvars[1]);
17183 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017184 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017185 di = dict_find(d, key, -1);
17186 if (di == NULL)
17187 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017188 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17189 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017190 {
17191 *rettv = di->di_tv;
17192 init_tv(&di->di_tv);
17193 dictitem_remove(d, di);
17194 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017195 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017196 }
17197 }
17198 else if (argvars[0].v_type != VAR_LIST)
17199 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017200 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017201 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017202 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017203 int error = FALSE;
17204
17205 idx = get_tv_number_chk(&argvars[1], &error);
17206 if (error)
17207 ; /* type error: do nothing, errmsg already given */
17208 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017209 EMSGN(_(e_listidx), idx);
17210 else
17211 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017212 if (argvars[2].v_type == VAR_UNKNOWN)
17213 {
17214 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017215 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017216 *rettv = item->li_tv;
17217 vim_free(item);
17218 }
17219 else
17220 {
17221 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017222 end = get_tv_number_chk(&argvars[2], &error);
17223 if (error)
17224 ; /* type error: do nothing */
17225 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017226 EMSGN(_(e_listidx), end);
17227 else
17228 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017229 int cnt = 0;
17230
17231 for (li = item; li != NULL; li = li->li_next)
17232 {
17233 ++cnt;
17234 if (li == item2)
17235 break;
17236 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017237 if (li == NULL) /* didn't find "item2" after "item" */
17238 EMSG(_(e_invrange));
17239 else
17240 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017241 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017242 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017243 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017244 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017245 l->lv_first = item;
17246 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017247 item->li_prev = NULL;
17248 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017249 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017250 }
17251 }
17252 }
17253 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017254 }
17255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256}
17257
17258/*
17259 * "rename({from}, {to})" function
17260 */
17261 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017262f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263{
17264 char_u buf[NUMBUFLEN];
17265
17266 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017267 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017268 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017269 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17270 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017271}
17272
17273/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017274 * "repeat()" function
17275 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017277f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017278{
17279 char_u *p;
17280 int n;
17281 int slen;
17282 int len;
17283 char_u *r;
17284 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017285
17286 n = get_tv_number(&argvars[1]);
17287 if (argvars[0].v_type == VAR_LIST)
17288 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017289 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017290 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017291 if (list_extend(rettv->vval.v_list,
17292 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017293 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017294 }
17295 else
17296 {
17297 p = get_tv_string(&argvars[0]);
17298 rettv->v_type = VAR_STRING;
17299 rettv->vval.v_string = NULL;
17300
17301 slen = (int)STRLEN(p);
17302 len = slen * n;
17303 if (len <= 0)
17304 return;
17305
17306 r = alloc(len + 1);
17307 if (r != NULL)
17308 {
17309 for (i = 0; i < n; i++)
17310 mch_memmove(r + i * slen, p, (size_t)slen);
17311 r[len] = NUL;
17312 }
17313
17314 rettv->vval.v_string = r;
17315 }
17316}
17317
17318/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 * "resolve()" function
17320 */
17321 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017322f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017323{
17324 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017325#ifdef HAVE_READLINK
17326 char_u *buf = NULL;
17327#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017328
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017329 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330#ifdef FEAT_SHORTCUT
17331 {
17332 char_u *v = NULL;
17333
17334 v = mch_resolve_shortcut(p);
17335 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017336 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 }
17340#else
17341# ifdef HAVE_READLINK
17342 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017343 char_u *cpy;
17344 int len;
17345 char_u *remain = NULL;
17346 char_u *q;
17347 int is_relative_to_current = FALSE;
17348 int has_trailing_pathsep = FALSE;
17349 int limit = 100;
17350
17351 p = vim_strsave(p);
17352
17353 if (p[0] == '.' && (vim_ispathsep(p[1])
17354 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17355 is_relative_to_current = TRUE;
17356
17357 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017358 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017359 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017360 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017361 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17362 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017363
17364 q = getnextcomp(p);
17365 if (*q != NUL)
17366 {
17367 /* Separate the first path component in "p", and keep the
17368 * remainder (beginning with the path separator). */
17369 remain = vim_strsave(q - 1);
17370 q[-1] = NUL;
17371 }
17372
Bram Moolenaard9462e32011-04-11 21:35:11 +020017373 buf = alloc(MAXPATHL + 1);
17374 if (buf == NULL)
17375 goto fail;
17376
Bram Moolenaar071d4272004-06-13 20:20:40 +000017377 for (;;)
17378 {
17379 for (;;)
17380 {
17381 len = readlink((char *)p, (char *)buf, MAXPATHL);
17382 if (len <= 0)
17383 break;
17384 buf[len] = NUL;
17385
17386 if (limit-- == 0)
17387 {
17388 vim_free(p);
17389 vim_free(remain);
17390 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017391 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017392 goto fail;
17393 }
17394
17395 /* Ensure that the result will have a trailing path separator
17396 * if the argument has one. */
17397 if (remain == NULL && has_trailing_pathsep)
17398 add_pathsep(buf);
17399
17400 /* Separate the first path component in the link value and
17401 * concatenate the remainders. */
17402 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17403 if (*q != NUL)
17404 {
17405 if (remain == NULL)
17406 remain = vim_strsave(q - 1);
17407 else
17408 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017409 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017410 if (cpy != NULL)
17411 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017412 vim_free(remain);
17413 remain = cpy;
17414 }
17415 }
17416 q[-1] = NUL;
17417 }
17418
17419 q = gettail(p);
17420 if (q > p && *q == NUL)
17421 {
17422 /* Ignore trailing path separator. */
17423 q[-1] = NUL;
17424 q = gettail(p);
17425 }
17426 if (q > p && !mch_isFullName(buf))
17427 {
17428 /* symlink is relative to directory of argument */
17429 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17430 if (cpy != NULL)
17431 {
17432 STRCPY(cpy, p);
17433 STRCPY(gettail(cpy), buf);
17434 vim_free(p);
17435 p = cpy;
17436 }
17437 }
17438 else
17439 {
17440 vim_free(p);
17441 p = vim_strsave(buf);
17442 }
17443 }
17444
17445 if (remain == NULL)
17446 break;
17447
17448 /* Append the first path component of "remain" to "p". */
17449 q = getnextcomp(remain + 1);
17450 len = q - remain - (*q != NUL);
17451 cpy = vim_strnsave(p, STRLEN(p) + len);
17452 if (cpy != NULL)
17453 {
17454 STRNCAT(cpy, remain, len);
17455 vim_free(p);
17456 p = cpy;
17457 }
17458 /* Shorten "remain". */
17459 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017460 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461 else
17462 {
17463 vim_free(remain);
17464 remain = NULL;
17465 }
17466 }
17467
17468 /* If the result is a relative path name, make it explicitly relative to
17469 * the current directory if and only if the argument had this form. */
17470 if (!vim_ispathsep(*p))
17471 {
17472 if (is_relative_to_current
17473 && *p != NUL
17474 && !(p[0] == '.'
17475 && (p[1] == NUL
17476 || vim_ispathsep(p[1])
17477 || (p[1] == '.'
17478 && (p[2] == NUL
17479 || vim_ispathsep(p[2]))))))
17480 {
17481 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017482 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017483 if (cpy != NULL)
17484 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 vim_free(p);
17486 p = cpy;
17487 }
17488 }
17489 else if (!is_relative_to_current)
17490 {
17491 /* Strip leading "./". */
17492 q = p;
17493 while (q[0] == '.' && vim_ispathsep(q[1]))
17494 q += 2;
17495 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017496 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017497 }
17498 }
17499
17500 /* Ensure that the result will have no trailing path separator
17501 * if the argument had none. But keep "/" or "//". */
17502 if (!has_trailing_pathsep)
17503 {
17504 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017505 if (after_pathsep(p, q))
17506 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017507 }
17508
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017509 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017510 }
17511# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017512 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017513# endif
17514#endif
17515
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017516 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017517
17518#ifdef HAVE_READLINK
17519fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017520 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017521#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017522 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017523}
17524
17525/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017526 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017527 */
17528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017529f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530{
Bram Moolenaar33570922005-01-25 22:26:29 +000017531 list_T *l;
17532 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017533
Bram Moolenaar0d660222005-01-07 21:51:51 +000017534 if (argvars[0].v_type != VAR_LIST)
17535 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017536 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017537 && !tv_check_lock(l->lv_lock,
17538 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017539 {
17540 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017541 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017542 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017543 while (li != NULL)
17544 {
17545 ni = li->li_prev;
17546 list_append(l, li);
17547 li = ni;
17548 }
17549 rettv->vval.v_list = l;
17550 rettv->v_type = VAR_LIST;
17551 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017552 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017554}
17555
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017556#define SP_NOMOVE 0x01 /* don't move cursor */
17557#define SP_REPEAT 0x02 /* repeat to find outer pair */
17558#define SP_RETCOUNT 0x04 /* return matchcount */
17559#define SP_SETPCMARK 0x08 /* set previous context mark */
17560#define SP_START 0x10 /* accept match at start position */
17561#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17562#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017563#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017564
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017565static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017566
17567/*
17568 * Get flags for a search function.
17569 * Possibly sets "p_ws".
17570 * Returns BACKWARD, FORWARD or zero (for an error).
17571 */
17572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017573get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017574{
17575 int dir = FORWARD;
17576 char_u *flags;
17577 char_u nbuf[NUMBUFLEN];
17578 int mask;
17579
17580 if (varp->v_type != VAR_UNKNOWN)
17581 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017582 flags = get_tv_string_buf_chk(varp, nbuf);
17583 if (flags == NULL)
17584 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017585 while (*flags != NUL)
17586 {
17587 switch (*flags)
17588 {
17589 case 'b': dir = BACKWARD; break;
17590 case 'w': p_ws = TRUE; break;
17591 case 'W': p_ws = FALSE; break;
17592 default: mask = 0;
17593 if (flagsp != NULL)
17594 switch (*flags)
17595 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017596 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017597 case 'e': mask = SP_END; break;
17598 case 'm': mask = SP_RETCOUNT; break;
17599 case 'n': mask = SP_NOMOVE; break;
17600 case 'p': mask = SP_SUBPAT; break;
17601 case 'r': mask = SP_REPEAT; break;
17602 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017603 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017604 }
17605 if (mask == 0)
17606 {
17607 EMSG2(_(e_invarg2), flags);
17608 dir = 0;
17609 }
17610 else
17611 *flagsp |= mask;
17612 }
17613 if (dir == 0)
17614 break;
17615 ++flags;
17616 }
17617 }
17618 return dir;
17619}
17620
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017622 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017623 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017624 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017625search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017626{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017627 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017628 char_u *pat;
17629 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017630 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631 int save_p_ws = p_ws;
17632 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017633 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017634 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017635 proftime_T tm;
17636#ifdef FEAT_RELTIME
17637 long time_limit = 0;
17638#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017639 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017640 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017641
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017642 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017643 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017644 if (dir == 0)
17645 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017646 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017647 if (flags & SP_START)
17648 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017649 if (flags & SP_END)
17650 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017651 if (flags & SP_COLUMN)
17652 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017653
Bram Moolenaar76929292008-01-06 19:07:36 +000017654 /* Optional arguments: line number to stop searching and timeout. */
17655 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017656 {
17657 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17658 if (lnum_stop < 0)
17659 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017660#ifdef FEAT_RELTIME
17661 if (argvars[3].v_type != VAR_UNKNOWN)
17662 {
17663 time_limit = get_tv_number_chk(&argvars[3], NULL);
17664 if (time_limit < 0)
17665 goto theend;
17666 }
17667#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017668 }
17669
Bram Moolenaar76929292008-01-06 19:07:36 +000017670#ifdef FEAT_RELTIME
17671 /* Set the time limit, if there is one. */
17672 profile_setlimit(time_limit, &tm);
17673#endif
17674
Bram Moolenaar231334e2005-07-25 20:46:57 +000017675 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017676 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017677 * Check to make sure only those flags are set.
17678 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17679 * flags cannot be set. Check for that condition also.
17680 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017681 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017682 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017684 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017685 goto theend;
17686 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017687
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017688 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017689 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017690 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017691 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017692 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017693 if (flags & SP_SUBPAT)
17694 retval = subpatnum;
17695 else
17696 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017697 if (flags & SP_SETPCMARK)
17698 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017700 if (match_pos != NULL)
17701 {
17702 /* Store the match cursor position */
17703 match_pos->lnum = pos.lnum;
17704 match_pos->col = pos.col + 1;
17705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017706 /* "/$" will put the cursor after the end of the line, may need to
17707 * correct that here */
17708 check_cursor();
17709 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017710
17711 /* If 'n' flag is used: restore cursor position. */
17712 if (flags & SP_NOMOVE)
17713 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017714 else
17715 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017716theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017718
17719 return retval;
17720}
17721
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017722#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017723
17724/*
17725 * round() is not in C90, use ceil() or floor() instead.
17726 */
17727 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017728vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017729{
17730 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17731}
17732
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017733/*
17734 * "round({float})" function
17735 */
17736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017737f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017738{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017739 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017740
17741 rettv->v_type = VAR_FLOAT;
17742 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017743 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017744 else
17745 rettv->vval.v_float = 0.0;
17746}
17747#endif
17748
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017749/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017750 * "screenattr()" function
17751 */
17752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017753f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017754{
17755 int row;
17756 int col;
17757 int c;
17758
17759 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17760 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17761 if (row < 0 || row >= screen_Rows
17762 || col < 0 || col >= screen_Columns)
17763 c = -1;
17764 else
17765 c = ScreenAttrs[LineOffset[row] + col];
17766 rettv->vval.v_number = c;
17767}
17768
17769/*
17770 * "screenchar()" function
17771 */
17772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017773f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017774{
17775 int row;
17776 int col;
17777 int off;
17778 int c;
17779
17780 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17781 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17782 if (row < 0 || row >= screen_Rows
17783 || col < 0 || col >= screen_Columns)
17784 c = -1;
17785 else
17786 {
17787 off = LineOffset[row] + col;
17788#ifdef FEAT_MBYTE
17789 if (enc_utf8 && ScreenLinesUC[off] != 0)
17790 c = ScreenLinesUC[off];
17791 else
17792#endif
17793 c = ScreenLines[off];
17794 }
17795 rettv->vval.v_number = c;
17796}
17797
17798/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017799 * "screencol()" function
17800 *
17801 * First column is 1 to be consistent with virtcol().
17802 */
17803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017804f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017805{
17806 rettv->vval.v_number = screen_screencol() + 1;
17807}
17808
17809/*
17810 * "screenrow()" function
17811 */
17812 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017813f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017814{
17815 rettv->vval.v_number = screen_screenrow() + 1;
17816}
17817
17818/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017819 * "search()" function
17820 */
17821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017822f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017823{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017824 int flags = 0;
17825
17826 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827}
17828
Bram Moolenaar071d4272004-06-13 20:20:40 +000017829/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017830 * "searchdecl()" function
17831 */
17832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017833f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017834{
17835 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017836 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017837 int error = FALSE;
17838 char_u *name;
17839
17840 rettv->vval.v_number = 1; /* default: FAIL */
17841
17842 name = get_tv_string_chk(&argvars[0]);
17843 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017844 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017845 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017846 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17847 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17848 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017849 if (!error && name != NULL)
17850 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017851 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017852}
17853
17854/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017855 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017858searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859{
17860 char_u *spat, *mpat, *epat;
17861 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863 int dir;
17864 int flags = 0;
17865 char_u nbuf1[NUMBUFLEN];
17866 char_u nbuf2[NUMBUFLEN];
17867 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017868 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017869 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017870 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017871
Bram Moolenaar071d4272004-06-13 20:20:40 +000017872 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017873 spat = get_tv_string_chk(&argvars[0]);
17874 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17875 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17876 if (spat == NULL || mpat == NULL || epat == NULL)
17877 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017878
Bram Moolenaar071d4272004-06-13 20:20:40 +000017879 /* Handle the optional fourth argument: flags */
17880 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017881 if (dir == 0)
17882 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017883
17884 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017885 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17886 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017887 if ((flags & (SP_END | SP_SUBPAT)) != 0
17888 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017889 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017890 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017891 goto theend;
17892 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017893
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017894 /* Using 'r' implies 'W', otherwise it doesn't work. */
17895 if (flags & SP_REPEAT)
17896 p_ws = FALSE;
17897
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017898 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017899 if (argvars[3].v_type == VAR_UNKNOWN
17900 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 skip = (char_u *)"";
17902 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017903 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017904 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017905 if (argvars[5].v_type != VAR_UNKNOWN)
17906 {
17907 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17908 if (lnum_stop < 0)
17909 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017910#ifdef FEAT_RELTIME
17911 if (argvars[6].v_type != VAR_UNKNOWN)
17912 {
17913 time_limit = get_tv_number_chk(&argvars[6], NULL);
17914 if (time_limit < 0)
17915 goto theend;
17916 }
17917#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017918 }
17919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 if (skip == NULL)
17921 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017922
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017923 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017924 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017925
17926theend:
17927 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017928
17929 return retval;
17930}
17931
17932/*
17933 * "searchpair()" function
17934 */
17935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017936f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017937{
17938 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17939}
17940
17941/*
17942 * "searchpairpos()" function
17943 */
17944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017945f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017946{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017947 pos_T match_pos;
17948 int lnum = 0;
17949 int col = 0;
17950
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017951 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017952 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017953
17954 if (searchpair_cmn(argvars, &match_pos) > 0)
17955 {
17956 lnum = match_pos.lnum;
17957 col = match_pos.col;
17958 }
17959
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017960 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17961 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017962}
17963
17964/*
17965 * Search for a start/middle/end thing.
17966 * Used by searchpair(), see its documentation for the details.
17967 * Returns 0 or -1 for no match,
17968 */
17969 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017970do_searchpair(
17971 char_u *spat, /* start pattern */
17972 char_u *mpat, /* middle pattern */
17973 char_u *epat, /* end pattern */
17974 int dir, /* BACKWARD or FORWARD */
17975 char_u *skip, /* skip expression */
17976 int flags, /* SP_SETPCMARK and other SP_ values */
17977 pos_T *match_pos,
17978 linenr_T lnum_stop, /* stop at this line if not zero */
17979 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017980{
17981 char_u *save_cpo;
17982 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17983 long retval = 0;
17984 pos_T pos;
17985 pos_T firstpos;
17986 pos_T foundpos;
17987 pos_T save_cursor;
17988 pos_T save_pos;
17989 int n;
17990 int r;
17991 int nest = 1;
17992 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017993 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017994 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017995
17996 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17997 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017998 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017999
Bram Moolenaar76929292008-01-06 19:07:36 +000018000#ifdef FEAT_RELTIME
18001 /* Set the time limit, if there is one. */
18002 profile_setlimit(time_limit, &tm);
18003#endif
18004
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018005 /* Make two search patterns: start/end (pat2, for in nested pairs) and
18006 * start/middle/end (pat3, for the top pair). */
18007 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
18008 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
18009 if (pat2 == NULL || pat3 == NULL)
18010 goto theend;
18011 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
18012 if (*mpat == NUL)
18013 STRCPY(pat3, pat2);
18014 else
18015 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
18016 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018017 if (flags & SP_START)
18018 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018019
Bram Moolenaar071d4272004-06-13 20:20:40 +000018020 save_cursor = curwin->w_cursor;
18021 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000018022 clearpos(&firstpos);
18023 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018024 pat = pat3;
18025 for (;;)
18026 {
18027 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000018028 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018029 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
18030 /* didn't find it or found the first match again: FAIL */
18031 break;
18032
18033 if (firstpos.lnum == 0)
18034 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000018035 if (equalpos(pos, foundpos))
18036 {
18037 /* Found the same position again. Can happen with a pattern that
18038 * has "\zs" at the end and searching backwards. Advance one
18039 * character and try again. */
18040 if (dir == BACKWARD)
18041 decl(&pos);
18042 else
18043 incl(&pos);
18044 }
18045 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018046
Bram Moolenaar92de73d2008-01-22 10:59:38 +000018047 /* clear the start flag to avoid getting stuck here */
18048 options &= ~SEARCH_START;
18049
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 /* If the skip pattern matches, ignore this match. */
18051 if (*skip != NUL)
18052 {
18053 save_pos = curwin->w_cursor;
18054 curwin->w_cursor = pos;
18055 r = eval_to_bool(skip, &err, NULL, FALSE);
18056 curwin->w_cursor = save_pos;
18057 if (err)
18058 {
18059 /* Evaluating {skip} caused an error, break here. */
18060 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018061 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018062 break;
18063 }
18064 if (r)
18065 continue;
18066 }
18067
18068 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18069 {
18070 /* Found end when searching backwards or start when searching
18071 * forward: nested pair. */
18072 ++nest;
18073 pat = pat2; /* nested, don't search for middle */
18074 }
18075 else
18076 {
18077 /* Found end when searching forward or start when searching
18078 * backward: end of (nested) pair; or found middle in outer pair. */
18079 if (--nest == 1)
18080 pat = pat3; /* outer level, search for middle */
18081 }
18082
18083 if (nest == 0)
18084 {
18085 /* Found the match: return matchcount or line number. */
18086 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018087 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018088 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018089 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018090 if (flags & SP_SETPCMARK)
18091 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 curwin->w_cursor = pos;
18093 if (!(flags & SP_REPEAT))
18094 break;
18095 nest = 1; /* search for next unmatched */
18096 }
18097 }
18098
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018099 if (match_pos != NULL)
18100 {
18101 /* Store the match cursor position */
18102 match_pos->lnum = curwin->w_cursor.lnum;
18103 match_pos->col = curwin->w_cursor.col + 1;
18104 }
18105
Bram Moolenaar071d4272004-06-13 20:20:40 +000018106 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018107 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018108 curwin->w_cursor = save_cursor;
18109
18110theend:
18111 vim_free(pat2);
18112 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018113 if (p_cpo == empty_option)
18114 p_cpo = save_cpo;
18115 else
18116 /* Darn, evaluating the {skip} expression changed the value. */
18117 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018118
18119 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018120}
18121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018122/*
18123 * "searchpos()" function
18124 */
18125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018126f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018127{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018128 pos_T match_pos;
18129 int lnum = 0;
18130 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018131 int n;
18132 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018134 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018135 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018136
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018137 n = search_cmn(argvars, &match_pos, &flags);
18138 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018139 {
18140 lnum = match_pos.lnum;
18141 col = match_pos.col;
18142 }
18143
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018144 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18145 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018146 if (flags & SP_SUBPAT)
18147 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018148}
18149
Bram Moolenaar0d660222005-01-07 21:51:51 +000018150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018151f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018153#ifdef FEAT_CLIENTSERVER
18154 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018155 char_u *server = get_tv_string_chk(&argvars[0]);
18156 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018157
Bram Moolenaar0d660222005-01-07 21:51:51 +000018158 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018159 if (server == NULL || reply == NULL)
18160 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018161 if (check_restricted() || check_secure())
18162 return;
18163# ifdef FEAT_X11
18164 if (check_connection() == FAIL)
18165 return;
18166# endif
18167
18168 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018169 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018170 EMSG(_("E258: Unable to send to client"));
18171 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018173 rettv->vval.v_number = 0;
18174#else
18175 rettv->vval.v_number = -1;
18176#endif
18177}
18178
Bram Moolenaar0d660222005-01-07 21:51:51 +000018179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018180f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018181{
18182 char_u *r = NULL;
18183
18184#ifdef FEAT_CLIENTSERVER
18185# ifdef WIN32
18186 r = serverGetVimNames();
18187# else
18188 make_connection();
18189 if (X_DISPLAY != NULL)
18190 r = serverGetVimNames(X_DISPLAY);
18191# endif
18192#endif
18193 rettv->v_type = VAR_STRING;
18194 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195}
18196
18197/*
18198 * "setbufvar()" function
18199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018201f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202{
18203 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018204 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018206 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 char_u nbuf[NUMBUFLEN];
18208
18209 if (check_restricted() || check_secure())
18210 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018211 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18212 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018213 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018214 varp = &argvars[2];
18215
18216 if (buf != NULL && varname != NULL && varp != NULL)
18217 {
18218 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018220
18221 if (*varname == '&')
18222 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018223 long numval;
18224 char_u *strval;
18225 int error = FALSE;
18226
Bram Moolenaar071d4272004-06-13 20:20:40 +000018227 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018228 numval = get_tv_number_chk(varp, &error);
18229 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018230 if (!error && strval != NULL)
18231 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018232 }
18233 else
18234 {
18235 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18236 if (bufvarname != NULL)
18237 {
18238 STRCPY(bufvarname, "b:");
18239 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018240 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 vim_free(bufvarname);
18242 }
18243 }
18244
18245 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018246 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248}
18249
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018250 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018251f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018252{
18253 dict_T *d;
18254 dictitem_T *di;
18255 char_u *csearch;
18256
18257 if (argvars[0].v_type != VAR_DICT)
18258 {
18259 EMSG(_(e_dictreq));
18260 return;
18261 }
18262
18263 if ((d = argvars[0].vval.v_dict) != NULL)
18264 {
18265 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18266 if (csearch != NULL)
18267 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018268#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018269 if (enc_utf8)
18270 {
18271 int pcc[MAX_MCO];
18272 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018273
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018274 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18275 }
18276 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018277#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018278 set_last_csearch(PTR2CHAR(csearch),
18279 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018280 }
18281
18282 di = dict_find(d, (char_u *)"forward", -1);
18283 if (di != NULL)
18284 set_csearch_direction(get_tv_number(&di->di_tv)
18285 ? FORWARD : BACKWARD);
18286
18287 di = dict_find(d, (char_u *)"until", -1);
18288 if (di != NULL)
18289 set_csearch_until(!!get_tv_number(&di->di_tv));
18290 }
18291}
18292
Bram Moolenaar071d4272004-06-13 20:20:40 +000018293/*
18294 * "setcmdpos()" function
18295 */
18296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018297f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018298{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018299 int pos = (int)get_tv_number(&argvars[0]) - 1;
18300
18301 if (pos >= 0)
18302 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303}
18304
18305/*
18306 * "setline()" function
18307 */
18308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018309f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310{
18311 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018312 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018313 list_T *l = NULL;
18314 listitem_T *li = NULL;
18315 long added = 0;
18316 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018317
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018318 lnum = get_tv_lnum(&argvars[0]);
18319 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018321 l = argvars[1].vval.v_list;
18322 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018324 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018325 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018326
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018327 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018328 for (;;)
18329 {
18330 if (l != NULL)
18331 {
18332 /* list argument, get next string */
18333 if (li == NULL)
18334 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018335 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018336 li = li->li_next;
18337 }
18338
18339 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018340 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018341 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018342
18343 /* When coming here from Insert mode, sync undo, so that this can be
18344 * undone separately from what was previously inserted. */
18345 if (u_sync_once == 2)
18346 {
18347 u_sync_once = 1; /* notify that u_sync() was called */
18348 u_sync(TRUE);
18349 }
18350
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018351 if (lnum <= curbuf->b_ml.ml_line_count)
18352 {
18353 /* existing line, replace it */
18354 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18355 {
18356 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018357 if (lnum == curwin->w_cursor.lnum)
18358 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018359 rettv->vval.v_number = 0; /* OK */
18360 }
18361 }
18362 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18363 {
18364 /* lnum is one past the last line, append the line */
18365 ++added;
18366 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18367 rettv->vval.v_number = 0; /* OK */
18368 }
18369
18370 if (l == NULL) /* only one string argument */
18371 break;
18372 ++lnum;
18373 }
18374
18375 if (added > 0)
18376 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018377}
18378
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018379static 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 +000018380
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018382 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018383 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018385set_qf_ll_list(
18386 win_T *wp UNUSED,
18387 typval_T *list_arg UNUSED,
18388 typval_T *action_arg UNUSED,
18389 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018390{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018391#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018392 char_u *act;
18393 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018394#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018395
Bram Moolenaar2641f772005-03-25 21:58:17 +000018396 rettv->vval.v_number = -1;
18397
18398#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018399 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018400 EMSG(_(e_listreq));
18401 else
18402 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018403 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018404
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018405 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018406 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018407 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018408 if (act == NULL)
18409 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018410 if (*act == 'a' || *act == 'r')
18411 action = *act;
18412 }
18413
Bram Moolenaar81484f42012-12-05 15:16:47 +010018414 if (l != NULL && set_errorlist(wp, l, action,
18415 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018416 rettv->vval.v_number = 0;
18417 }
18418#endif
18419}
18420
18421/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018422 * "setloclist()" function
18423 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018425f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018426{
18427 win_T *win;
18428
18429 rettv->vval.v_number = -1;
18430
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018431 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018432 if (win != NULL)
18433 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18434}
18435
18436/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018437 * "setmatches()" function
18438 */
18439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018441{
18442#ifdef FEAT_SEARCH_EXTRA
18443 list_T *l;
18444 listitem_T *li;
18445 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018446 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018447
18448 rettv->vval.v_number = -1;
18449 if (argvars[0].v_type != VAR_LIST)
18450 {
18451 EMSG(_(e_listreq));
18452 return;
18453 }
18454 if ((l = argvars[0].vval.v_list) != NULL)
18455 {
18456
18457 /* To some extent make sure that we are dealing with a list from
18458 * "getmatches()". */
18459 li = l->lv_first;
18460 while (li != NULL)
18461 {
18462 if (li->li_tv.v_type != VAR_DICT
18463 || (d = li->li_tv.vval.v_dict) == NULL)
18464 {
18465 EMSG(_(e_invarg));
18466 return;
18467 }
18468 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018469 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18470 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018471 && dict_find(d, (char_u *)"priority", -1) != NULL
18472 && dict_find(d, (char_u *)"id", -1) != NULL))
18473 {
18474 EMSG(_(e_invarg));
18475 return;
18476 }
18477 li = li->li_next;
18478 }
18479
18480 clear_matches(curwin);
18481 li = l->lv_first;
18482 while (li != NULL)
18483 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018484 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018485 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018486 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018487 char_u *group;
18488 int priority;
18489 int id;
18490 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018491
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018492 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018493 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18494 {
18495 if (s == NULL)
18496 {
18497 s = list_alloc();
18498 if (s == NULL)
18499 return;
18500 }
18501
18502 /* match from matchaddpos() */
18503 for (i = 1; i < 9; i++)
18504 {
18505 sprintf((char *)buf, (char *)"pos%d", i);
18506 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18507 {
18508 if (di->di_tv.v_type != VAR_LIST)
18509 return;
18510
18511 list_append_tv(s, &di->di_tv);
18512 s->lv_refcount++;
18513 }
18514 else
18515 break;
18516 }
18517 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018518
18519 group = get_dict_string(d, (char_u *)"group", FALSE);
18520 priority = (int)get_dict_number(d, (char_u *)"priority");
18521 id = (int)get_dict_number(d, (char_u *)"id");
18522 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18523 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18524 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018525 if (i == 0)
18526 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018527 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018528 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018529 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018530 }
18531 else
18532 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018533 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018534 list_unref(s);
18535 s = NULL;
18536 }
18537
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018538 li = li->li_next;
18539 }
18540 rettv->vval.v_number = 0;
18541 }
18542#endif
18543}
18544
18545/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018546 * "setpos()" function
18547 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018549f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018550{
18551 pos_T pos;
18552 int fnum;
18553 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018554 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018555
Bram Moolenaar08250432008-02-13 11:42:46 +000018556 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018557 name = get_tv_string_chk(argvars);
18558 if (name != NULL)
18559 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018560 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018561 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018562 if (--pos.col < 0)
18563 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018564 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018565 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018566 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018567 if (fnum == curbuf->b_fnum)
18568 {
18569 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018570 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018571 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018572 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018573 curwin->w_set_curswant = FALSE;
18574 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018575 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018576 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018577 }
18578 else
18579 EMSG(_(e_invarg));
18580 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018581 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18582 {
18583 /* set mark */
18584 if (setmark_pos(name[1], &pos, fnum) == OK)
18585 rettv->vval.v_number = 0;
18586 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018587 else
18588 EMSG(_(e_invarg));
18589 }
18590 }
18591}
18592
18593/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018594 * "setqflist()" function
18595 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018597f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018598{
18599 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18600}
18601
18602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018603 * "setreg()" function
18604 */
18605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018606f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607{
18608 int regname;
18609 char_u *strregname;
18610 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018611 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612 int append;
18613 char_u yank_type;
18614 long block_len;
18615
18616 block_len = -1;
18617 yank_type = MAUTO;
18618 append = FALSE;
18619
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018620 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018621 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 if (strregname == NULL)
18624 return; /* type error; errmsg already given */
18625 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018626 if (regname == 0 || regname == '@')
18627 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018628
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018629 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018631 stropt = get_tv_string_chk(&argvars[2]);
18632 if (stropt == NULL)
18633 return; /* type error */
18634 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018635 switch (*stropt)
18636 {
18637 case 'a': case 'A': /* append */
18638 append = TRUE;
18639 break;
18640 case 'v': case 'c': /* character-wise selection */
18641 yank_type = MCHAR;
18642 break;
18643 case 'V': case 'l': /* line-wise selection */
18644 yank_type = MLINE;
18645 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018646 case 'b': case Ctrl_V: /* block-wise selection */
18647 yank_type = MBLOCK;
18648 if (VIM_ISDIGIT(stropt[1]))
18649 {
18650 ++stropt;
18651 block_len = getdigits(&stropt) - 1;
18652 --stropt;
18653 }
18654 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018655 }
18656 }
18657
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018658 if (argvars[1].v_type == VAR_LIST)
18659 {
18660 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018661 char_u **allocval;
18662 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018663 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018664 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018665 int len = argvars[1].vval.v_list->lv_len;
18666 listitem_T *li;
18667
Bram Moolenaar7d647822014-04-05 21:28:56 +020018668 /* First half: use for pointers to result lines; second half: use for
18669 * pointers to allocated copies. */
18670 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018671 if (lstval == NULL)
18672 return;
18673 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018674 allocval = lstval + len + 2;
18675 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018676
18677 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18678 li = li->li_next)
18679 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018680 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018681 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018682 goto free_lstval;
18683 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018684 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018685 /* Need to make a copy, next get_tv_string_buf_chk() will
18686 * overwrite the string. */
18687 strval = vim_strsave(buf);
18688 if (strval == NULL)
18689 goto free_lstval;
18690 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018691 }
18692 *curval++ = strval;
18693 }
18694 *curval++ = NULL;
18695
18696 write_reg_contents_lst(regname, lstval, -1,
18697 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018698free_lstval:
18699 while (curallocval > allocval)
18700 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018701 vim_free(lstval);
18702 }
18703 else
18704 {
18705 strval = get_tv_string_chk(&argvars[1]);
18706 if (strval == NULL)
18707 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018708 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018709 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018711 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018712}
18713
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018714/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018715 * "settabvar()" function
18716 */
18717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018718f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018719{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018720#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018721 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018722 tabpage_T *tp;
18723#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018724 char_u *varname, *tabvarname;
18725 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018726
18727 rettv->vval.v_number = 0;
18728
18729 if (check_restricted() || check_secure())
18730 return;
18731
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018732#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018733 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018734#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018735 varname = get_tv_string_chk(&argvars[1]);
18736 varp = &argvars[2];
18737
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018738 if (varname != NULL && varp != NULL
18739#ifdef FEAT_WINDOWS
18740 && tp != NULL
18741#endif
18742 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018743 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018744#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018745 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018746 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018747#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018748
18749 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18750 if (tabvarname != NULL)
18751 {
18752 STRCPY(tabvarname, "t:");
18753 STRCPY(tabvarname + 2, varname);
18754 set_var(tabvarname, varp, TRUE);
18755 vim_free(tabvarname);
18756 }
18757
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018758#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018759 /* Restore current tabpage */
18760 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018761 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018762#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018763 }
18764}
18765
18766/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018767 * "settabwinvar()" function
18768 */
18769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018770f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018771{
18772 setwinvar(argvars, rettv, 1);
18773}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
18775/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018776 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018779f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018780{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018781 setwinvar(argvars, rettv, 0);
18782}
18783
18784/*
18785 * "setwinvar()" and "settabwinvar()" functions
18786 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018787
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018789setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018790{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791 win_T *win;
18792#ifdef FEAT_WINDOWS
18793 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018794 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018795 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796#endif
18797 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018798 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018800 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
18802 if (check_restricted() || check_secure())
18803 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018804
18805#ifdef FEAT_WINDOWS
18806 if (off == 1)
18807 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18808 else
18809 tp = curtab;
18810#endif
18811 win = find_win_by_nr(&argvars[off], tp);
18812 varname = get_tv_string_chk(&argvars[off + 1]);
18813 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814
18815 if (win != NULL && varname != NULL && varp != NULL)
18816 {
18817#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018818 need_switch_win = !(tp == curtab && win == curwin);
18819 if (!need_switch_win
18820 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018822 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018823 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018824 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018825 long numval;
18826 char_u *strval;
18827 int error = FALSE;
18828
18829 ++varname;
18830 numval = get_tv_number_chk(varp, &error);
18831 strval = get_tv_string_buf_chk(varp, nbuf);
18832 if (!error && strval != NULL)
18833 set_option_value(varname, numval, strval, OPT_LOCAL);
18834 }
18835 else
18836 {
18837 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18838 if (winvarname != NULL)
18839 {
18840 STRCPY(winvarname, "w:");
18841 STRCPY(winvarname + 2, varname);
18842 set_var(winvarname, varp, TRUE);
18843 vim_free(winvarname);
18844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018845 }
18846 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018847#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018848 if (need_switch_win)
18849 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018850#endif
18851 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018852}
18853
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018854#ifdef FEAT_CRYPT
18855/*
18856 * "sha256({string})" function
18857 */
18858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018859f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018860{
18861 char_u *p;
18862
18863 p = get_tv_string(&argvars[0]);
18864 rettv->vval.v_string = vim_strsave(
18865 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18866 rettv->v_type = VAR_STRING;
18867}
18868#endif /* FEAT_CRYPT */
18869
Bram Moolenaar071d4272004-06-13 20:20:40 +000018870/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018871 * "shellescape({string})" function
18872 */
18873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018874f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018875{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018876 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018877 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018878 rettv->v_type = VAR_STRING;
18879}
18880
18881/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018882 * shiftwidth() function
18883 */
18884 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018885f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018886{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018887 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018888}
18889
18890/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018891 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018892 */
18893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018894f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018895{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018896 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018897
Bram Moolenaar0d660222005-01-07 21:51:51 +000018898 p = get_tv_string(&argvars[0]);
18899 rettv->vval.v_string = vim_strsave(p);
18900 simplify_filename(rettv->vval.v_string); /* simplify in place */
18901 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018902}
18903
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018904#ifdef FEAT_FLOAT
18905/*
18906 * "sin()" function
18907 */
18908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018909f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018910{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018911 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018912
18913 rettv->v_type = VAR_FLOAT;
18914 if (get_float_arg(argvars, &f) == OK)
18915 rettv->vval.v_float = sin(f);
18916 else
18917 rettv->vval.v_float = 0.0;
18918}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018919
18920/*
18921 * "sinh()" function
18922 */
18923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018925{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018926 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018927
18928 rettv->v_type = VAR_FLOAT;
18929 if (get_float_arg(argvars, &f) == OK)
18930 rettv->vval.v_float = sinh(f);
18931 else
18932 rettv->vval.v_float = 0.0;
18933}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018934#endif
18935
Bram Moolenaar0d660222005-01-07 21:51:51 +000018936static int
18937#ifdef __BORLANDC__
18938 _RTLENTRYF
18939#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018940 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018941static int
18942#ifdef __BORLANDC__
18943 _RTLENTRYF
18944#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018945 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018946
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018947/* struct used in the array that's given to qsort() */
18948typedef struct
18949{
18950 listitem_T *item;
18951 int idx;
18952} sortItem_T;
18953
Bram Moolenaar0b962472016-02-22 22:51:33 +010018954/* struct storing information about current sort */
18955typedef struct
18956{
18957 int item_compare_ic;
18958 int item_compare_numeric;
18959 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018960#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018961 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018962#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018963 char_u *item_compare_func;
18964 dict_T *item_compare_selfdict;
18965 int item_compare_func_err;
18966 int item_compare_keep_zero;
18967} sortinfo_T;
18968static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018969static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018970#define ITEM_COMPARE_FAIL 999
18971
Bram Moolenaar071d4272004-06-13 20:20:40 +000018972/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018973 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018974 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018975 static int
18976#ifdef __BORLANDC__
18977_RTLENTRYF
18978#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018979item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018980{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018981 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018982 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018984 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018985 int res;
18986 char_u numbuf1[NUMBUFLEN];
18987 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018989 si1 = (sortItem_T *)s1;
18990 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018991 tv1 = &si1->item->li_tv;
18992 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018993
Bram Moolenaar0b962472016-02-22 22:51:33 +010018994 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018995 {
18996 long v1 = get_tv_number(tv1);
18997 long v2 = get_tv_number(tv2);
18998
18999 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19000 }
19001
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019002#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019003 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019004 {
19005 float_T v1 = get_tv_float(tv1);
19006 float_T v2 = get_tv_float(tv2);
19007
19008 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
19009 }
19010#endif
19011
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019012 /* tv2string() puts quotes around a string and allocates memory. Don't do
19013 * that for string variables. Use a single quote when comparing with a
19014 * non-string to do what the docs promise. */
19015 if (tv1->v_type == VAR_STRING)
19016 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019017 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019018 p1 = (char_u *)"'";
19019 else
19020 p1 = tv1->vval.v_string;
19021 }
19022 else
19023 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
19024 if (tv2->v_type == VAR_STRING)
19025 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019026 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019027 p2 = (char_u *)"'";
19028 else
19029 p2 = tv2->vval.v_string;
19030 }
19031 else
19032 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019033 if (p1 == NULL)
19034 p1 = (char_u *)"";
19035 if (p2 == NULL)
19036 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019038 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019039 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019040 res = STRICMP(p1, p2);
19041 else
19042 res = STRCMP(p1, p2);
19043 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019044 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020019045 {
19046 double n1, n2;
19047 n1 = strtod((char *)p1, (char **)&p1);
19048 n2 = strtod((char *)p2, (char **)&p2);
19049 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
19050 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019051
Bram Moolenaar1b338d22014-08-22 13:13:27 +020019052 /* When the result would be zero, compare the item indexes. Makes the
19053 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019054 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019055 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019056
Bram Moolenaar0d660222005-01-07 21:51:51 +000019057 vim_free(tofree1);
19058 vim_free(tofree2);
19059 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060}
19061
19062 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019063#ifdef __BORLANDC__
19064_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019065#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019066item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019067{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019068 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019069 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019070 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019071 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019072 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019074 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019075 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019076 return 0;
19077
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019078 si1 = (sortItem_T *)s1;
19079 si2 = (sortItem_T *)s2;
19080
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019081 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019082 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019083 copy_tv(&si1->item->li_tv, &argv[0]);
19084 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085
19086 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019087 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019088 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019089 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019090 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019091 clear_tv(&argv[0]);
19092 clear_tv(&argv[1]);
19093
19094 if (res == FAIL)
19095 res = ITEM_COMPARE_FAIL;
19096 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19098 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019099 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019100 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019101
19102 /* When the result would be zero, compare the pointers themselves. Makes
19103 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019104 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019105 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019106
Bram Moolenaar0d660222005-01-07 21:51:51 +000019107 return res;
19108}
19109
19110/*
19111 * "sort({list})" function
19112 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019113 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019114do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115{
Bram Moolenaar33570922005-01-25 22:26:29 +000019116 list_T *l;
19117 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019118 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019119 sortinfo_T *old_sortinfo;
19120 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019121 long len;
19122 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019123
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 /* Pointer to current info struct used in compare function. Save and
19125 * restore the current one for nested calls. */
19126 old_sortinfo = sortinfo;
19127 sortinfo = &info;
19128
Bram Moolenaar0d660222005-01-07 21:51:51 +000019129 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019130 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019131 else
19132 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019133 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019134 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019135 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19136 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019137 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019138 rettv->vval.v_list = l;
19139 rettv->v_type = VAR_LIST;
19140 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019141
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 len = list_len(l);
19143 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019144 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145
Bram Moolenaar0b962472016-02-22 22:51:33 +010019146 info.item_compare_ic = FALSE;
19147 info.item_compare_numeric = FALSE;
19148 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019149#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019151#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 info.item_compare_func = NULL;
19153 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019154 if (argvars[1].v_type != VAR_UNKNOWN)
19155 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019156 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019157 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019158 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 else
19160 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019161 int error = FALSE;
19162
19163 i = get_tv_number_chk(&argvars[1], &error);
19164 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019165 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019166 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019167 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 info.item_compare_func = get_tv_string(&argvars[1]);
19170 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019171 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019173 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019174 info.item_compare_func = NULL;
19175 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019176 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019177 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019178 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019179 info.item_compare_func = NULL;
19180 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019181 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019182#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019183 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019184 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019185 info.item_compare_func = NULL;
19186 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019187 }
19188#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019189 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019190 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019191 info.item_compare_func = NULL;
19192 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019193 }
19194 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019195 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019196
19197 if (argvars[2].v_type != VAR_UNKNOWN)
19198 {
19199 /* optional third argument: {dict} */
19200 if (argvars[2].v_type != VAR_DICT)
19201 {
19202 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019203 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019204 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019205 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019206 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019207 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208
Bram Moolenaar0d660222005-01-07 21:51:51 +000019209 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019210 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019211 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019213
Bram Moolenaar327aa022014-03-25 18:24:23 +010019214 i = 0;
19215 if (sort)
19216 {
19217 /* sort(): ptrs will be the list to sort */
19218 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019219 {
19220 ptrs[i].item = li;
19221 ptrs[i].idx = i;
19222 ++i;
19223 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019224
Bram Moolenaar0b962472016-02-22 22:51:33 +010019225 info.item_compare_func_err = FALSE;
19226 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019228 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019229 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019230 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019231 EMSG(_("E702: Sort compare function failed"));
19232 else
19233 {
19234 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019235 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019236 info.item_compare_func == NULL
19237 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019238
Bram Moolenaar0b962472016-02-22 22:51:33 +010019239 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019240 {
19241 /* Clear the List and append the items in sorted order. */
19242 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19243 l->lv_len = 0;
19244 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019245 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019246 }
19247 }
19248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019250 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019251 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019252
19253 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019254 info.item_compare_func_err = FALSE;
19255 info.item_compare_keep_zero = TRUE;
19256 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019257 ? item_compare2 : item_compare;
19258
19259 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19260 li = li->li_next)
19261 {
19262 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19263 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019264 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019265 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019266 {
19267 EMSG(_("E882: Uniq compare function failed"));
19268 break;
19269 }
19270 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019271
Bram Moolenaar0b962472016-02-22 22:51:33 +010019272 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019273 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019274 while (--i >= 0)
19275 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019276 li = ptrs[i].item->li_next;
19277 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019278 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019279 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019280 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019281 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019282 list_fix_watch(l, li);
19283 listitem_free(li);
19284 l->lv_len--;
19285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019287 }
19288
19289 vim_free(ptrs);
19290 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019291theend:
19292 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019293}
19294
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019295/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019296 * "sort({list})" function
19297 */
19298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019299f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019300{
19301 do_sort_uniq(argvars, rettv, TRUE);
19302}
19303
19304/*
19305 * "uniq({list})" function
19306 */
19307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019308f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019309{
19310 do_sort_uniq(argvars, rettv, FALSE);
19311}
19312
19313/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019314 * "soundfold({word})" function
19315 */
19316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019317f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019318{
19319 char_u *s;
19320
19321 rettv->v_type = VAR_STRING;
19322 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019323#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019324 rettv->vval.v_string = eval_soundfold(s);
19325#else
19326 rettv->vval.v_string = vim_strsave(s);
19327#endif
19328}
19329
19330/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331 * "spellbadword()" function
19332 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019334f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019336 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019337 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019338 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019339
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019340 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019341 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019342
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019343#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019344 if (argvars[0].v_type == VAR_UNKNOWN)
19345 {
19346 /* Find the start and length of the badly spelled word. */
19347 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19348 if (len != 0)
19349 word = ml_get_cursor();
19350 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019351 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019352 {
19353 char_u *str = get_tv_string_chk(&argvars[0]);
19354 int capcol = -1;
19355
19356 if (str != NULL)
19357 {
19358 /* Check the argument for spelling. */
19359 while (*str != NUL)
19360 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019361 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019362 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019363 {
19364 word = str;
19365 break;
19366 }
19367 str += len;
19368 }
19369 }
19370 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019371#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019372
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019373 list_append_string(rettv->vval.v_list, word, len);
19374 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019375 attr == HLF_SPB ? "bad" :
19376 attr == HLF_SPR ? "rare" :
19377 attr == HLF_SPL ? "local" :
19378 attr == HLF_SPC ? "caps" :
19379 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019380}
19381
19382/*
19383 * "spellsuggest()" function
19384 */
19385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019386f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019387{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019388#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019389 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019390 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019391 int maxcount;
19392 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019393 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019394 listitem_T *li;
19395 int need_capital = FALSE;
19396#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019397
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019398 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019399 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019400
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019401#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019402 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019403 {
19404 str = get_tv_string(&argvars[0]);
19405 if (argvars[1].v_type != VAR_UNKNOWN)
19406 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019407 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019408 if (maxcount <= 0)
19409 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019410 if (argvars[2].v_type != VAR_UNKNOWN)
19411 {
19412 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19413 if (typeerr)
19414 return;
19415 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019416 }
19417 else
19418 maxcount = 25;
19419
Bram Moolenaar4770d092006-01-12 23:22:24 +000019420 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019421
19422 for (i = 0; i < ga.ga_len; ++i)
19423 {
19424 str = ((char_u **)ga.ga_data)[i];
19425
19426 li = listitem_alloc();
19427 if (li == NULL)
19428 vim_free(str);
19429 else
19430 {
19431 li->li_tv.v_type = VAR_STRING;
19432 li->li_tv.v_lock = 0;
19433 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019434 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019435 }
19436 }
19437 ga_clear(&ga);
19438 }
19439#endif
19440}
19441
Bram Moolenaar0d660222005-01-07 21:51:51 +000019442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019443f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019444{
19445 char_u *str;
19446 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019447 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019448 regmatch_T regmatch;
19449 char_u patbuf[NUMBUFLEN];
19450 char_u *save_cpo;
19451 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019453 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019454 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455
19456 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19457 save_cpo = p_cpo;
19458 p_cpo = (char_u *)"";
19459
19460 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019461 if (argvars[1].v_type != VAR_UNKNOWN)
19462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019463 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19464 if (pat == NULL)
19465 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019467 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019468 }
19469 if (pat == NULL || *pat == NUL)
19470 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019471
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019472 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019473 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019474 if (typeerr)
19475 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019476
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19478 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019479 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019480 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019481 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019482 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019483 if (*str == NUL)
19484 match = FALSE; /* empty item at the end */
19485 else
19486 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019487 if (match)
19488 end = regmatch.startp[0];
19489 else
19490 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019491 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19492 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019493 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019494 if (list_append_string(rettv->vval.v_list, str,
19495 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019496 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019497 }
19498 if (!match)
19499 break;
19500 /* Advance to just after the match. */
19501 if (regmatch.endp[0] > str)
19502 col = 0;
19503 else
19504 {
19505 /* Don't get stuck at the same match. */
19506#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019507 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019508#else
19509 col = 1;
19510#endif
19511 }
19512 str = regmatch.endp[0];
19513 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019514
Bram Moolenaar473de612013-06-08 18:19:48 +020019515 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517
Bram Moolenaar0d660222005-01-07 21:51:51 +000019518 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019519}
19520
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019521#ifdef FEAT_FLOAT
19522/*
19523 * "sqrt()" function
19524 */
19525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019526f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019527{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019528 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019529
19530 rettv->v_type = VAR_FLOAT;
19531 if (get_float_arg(argvars, &f) == OK)
19532 rettv->vval.v_float = sqrt(f);
19533 else
19534 rettv->vval.v_float = 0.0;
19535}
19536
19537/*
19538 * "str2float()" function
19539 */
19540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019541f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019542{
19543 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19544
19545 if (*p == '+')
19546 p = skipwhite(p + 1);
19547 (void)string2float(p, &rettv->vval.v_float);
19548 rettv->v_type = VAR_FLOAT;
19549}
19550#endif
19551
Bram Moolenaar2c932302006-03-18 21:42:09 +000019552/*
19553 * "str2nr()" function
19554 */
19555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019557{
19558 int base = 10;
19559 char_u *p;
19560 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019561 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019562
19563 if (argvars[1].v_type != VAR_UNKNOWN)
19564 {
19565 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019566 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019567 {
19568 EMSG(_(e_invarg));
19569 return;
19570 }
19571 }
19572
19573 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019574 if (*p == '+')
19575 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019576 switch (base)
19577 {
19578 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19579 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19580 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19581 default: what = 0;
19582 }
19583 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019584 rettv->vval.v_number = n;
19585}
19586
Bram Moolenaar071d4272004-06-13 20:20:40 +000019587#ifdef HAVE_STRFTIME
19588/*
19589 * "strftime({format}[, {time}])" function
19590 */
19591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019592f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593{
19594 char_u result_buf[256];
19595 struct tm *curtime;
19596 time_t seconds;
19597 char_u *p;
19598
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019599 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019600
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019601 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019602 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603 seconds = time(NULL);
19604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019605 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606 curtime = localtime(&seconds);
19607 /* MSVC returns NULL for an invalid value of seconds. */
19608 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019609 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610 else
19611 {
19612# ifdef FEAT_MBYTE
19613 vimconv_T conv;
19614 char_u *enc;
19615
19616 conv.vc_type = CONV_NONE;
19617 enc = enc_locale();
19618 convert_setup(&conv, p_enc, enc);
19619 if (conv.vc_type != CONV_NONE)
19620 p = string_convert(&conv, p, NULL);
19621# endif
19622 if (p != NULL)
19623 (void)strftime((char *)result_buf, sizeof(result_buf),
19624 (char *)p, curtime);
19625 else
19626 result_buf[0] = NUL;
19627
19628# ifdef FEAT_MBYTE
19629 if (conv.vc_type != CONV_NONE)
19630 vim_free(p);
19631 convert_setup(&conv, enc, p_enc);
19632 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019633 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019634 else
19635# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019636 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019637
19638# ifdef FEAT_MBYTE
19639 /* Release conversion descriptors */
19640 convert_setup(&conv, NULL, NULL);
19641 vim_free(enc);
19642# endif
19643 }
19644}
19645#endif
19646
19647/*
19648 * "stridx()" function
19649 */
19650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019651f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019652{
19653 char_u buf[NUMBUFLEN];
19654 char_u *needle;
19655 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019656 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019657 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019658 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019660 needle = get_tv_string_chk(&argvars[1]);
19661 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019662 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019663 if (needle == NULL || haystack == NULL)
19664 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019665
Bram Moolenaar33570922005-01-25 22:26:29 +000019666 if (argvars[2].v_type != VAR_UNKNOWN)
19667 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019668 int error = FALSE;
19669
19670 start_idx = get_tv_number_chk(&argvars[2], &error);
19671 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019672 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019673 if (start_idx >= 0)
19674 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019675 }
19676
19677 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19678 if (pos != NULL)
19679 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680}
19681
19682/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019683 * "string()" function
19684 */
19685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019686f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019687{
19688 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019689 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019690
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019691 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019692 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019693 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019694 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019695 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019696}
19697
19698/*
19699 * "strlen()" function
19700 */
19701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019702f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019703{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019704 rettv->vval.v_number = (varnumber_T)(STRLEN(
19705 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019706}
19707
19708/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019709 * "strchars()" function
19710 */
19711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019712f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019713{
19714 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019715 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019716#ifdef FEAT_MBYTE
19717 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019718 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019719#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019720
19721 if (argvars[1].v_type != VAR_UNKNOWN)
19722 skipcc = get_tv_number_chk(&argvars[1], NULL);
19723 if (skipcc < 0 || skipcc > 1)
19724 EMSG(_(e_invarg));
19725 else
19726 {
19727#ifdef FEAT_MBYTE
19728 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19729 while (*s != NUL)
19730 {
19731 func_mb_ptr2char_adv(&s);
19732 ++len;
19733 }
19734 rettv->vval.v_number = len;
19735#else
19736 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19737#endif
19738 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019739}
19740
19741/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019742 * "strdisplaywidth()" function
19743 */
19744 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019745f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019746{
19747 char_u *s = get_tv_string(&argvars[0]);
19748 int col = 0;
19749
19750 if (argvars[1].v_type != VAR_UNKNOWN)
19751 col = get_tv_number(&argvars[1]);
19752
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019753 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019754}
19755
19756/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019757 * "strwidth()" function
19758 */
19759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019760f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019761{
19762 char_u *s = get_tv_string(&argvars[0]);
19763
19764 rettv->vval.v_number = (varnumber_T)(
19765#ifdef FEAT_MBYTE
19766 mb_string2cells(s, -1)
19767#else
19768 STRLEN(s)
19769#endif
19770 );
19771}
19772
19773/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019774 * "strpart()" function
19775 */
19776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019777f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019778{
19779 char_u *p;
19780 int n;
19781 int len;
19782 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019783 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019784
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019785 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019786 slen = (int)STRLEN(p);
19787
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019788 n = get_tv_number_chk(&argvars[1], &error);
19789 if (error)
19790 len = 0;
19791 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019792 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019793 else
19794 len = slen - n; /* default len: all bytes that are available. */
19795
19796 /*
19797 * Only return the overlap between the specified part and the actual
19798 * string.
19799 */
19800 if (n < 0)
19801 {
19802 len += n;
19803 n = 0;
19804 }
19805 else if (n > slen)
19806 n = slen;
19807 if (len < 0)
19808 len = 0;
19809 else if (n + len > slen)
19810 len = slen - n;
19811
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019812 rettv->v_type = VAR_STRING;
19813 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814}
19815
19816/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019817 * "strridx()" function
19818 */
19819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019820f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019821{
19822 char_u buf[NUMBUFLEN];
19823 char_u *needle;
19824 char_u *haystack;
19825 char_u *rest;
19826 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019827 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019829 needle = get_tv_string_chk(&argvars[1]);
19830 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019831
19832 rettv->vval.v_number = -1;
19833 if (needle == NULL || haystack == NULL)
19834 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019835
19836 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019837 if (argvars[2].v_type != VAR_UNKNOWN)
19838 {
19839 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019840 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019841 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019842 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019843 }
19844 else
19845 end_idx = haystack_len;
19846
Bram Moolenaar0d660222005-01-07 21:51:51 +000019847 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019848 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019849 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019850 lastmatch = haystack + end_idx;
19851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019852 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019853 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019854 for (rest = haystack; *rest != '\0'; ++rest)
19855 {
19856 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019857 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019858 break;
19859 lastmatch = rest;
19860 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019861 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019862
19863 if (lastmatch == NULL)
19864 rettv->vval.v_number = -1;
19865 else
19866 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19867}
19868
19869/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019870 * "strtrans()" function
19871 */
19872 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019873f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019874{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019875 rettv->v_type = VAR_STRING;
19876 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019877}
19878
19879/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019880 * "submatch()" function
19881 */
19882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019883f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019884{
Bram Moolenaar41571762014-04-02 19:00:58 +020019885 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019886 int no;
19887 int retList = 0;
19888
19889 no = (int)get_tv_number_chk(&argvars[0], &error);
19890 if (error)
19891 return;
19892 error = FALSE;
19893 if (argvars[1].v_type != VAR_UNKNOWN)
19894 retList = get_tv_number_chk(&argvars[1], &error);
19895 if (error)
19896 return;
19897
19898 if (retList == 0)
19899 {
19900 rettv->v_type = VAR_STRING;
19901 rettv->vval.v_string = reg_submatch(no);
19902 }
19903 else
19904 {
19905 rettv->v_type = VAR_LIST;
19906 rettv->vval.v_list = reg_submatch_list(no);
19907 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019908}
19909
19910/*
19911 * "substitute()" function
19912 */
19913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019914f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019915{
19916 char_u patbuf[NUMBUFLEN];
19917 char_u subbuf[NUMBUFLEN];
19918 char_u flagsbuf[NUMBUFLEN];
19919
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019920 char_u *str = get_tv_string_chk(&argvars[0]);
19921 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19922 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19923 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19924
Bram Moolenaar0d660222005-01-07 21:51:51 +000019925 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019926 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19927 rettv->vval.v_string = NULL;
19928 else
19929 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019930}
19931
19932/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019933 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019936f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019937{
19938 int id = 0;
19939#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019940 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019941 long col;
19942 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019943 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019944
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019945 lnum = get_tv_lnum(argvars); /* -1 on type error */
19946 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19947 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019949 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019950 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019951 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019952#endif
19953
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019954 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955}
19956
19957/*
19958 * "synIDattr(id, what [, mode])" function
19959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019961f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019962{
19963 char_u *p = NULL;
19964#ifdef FEAT_SYN_HL
19965 int id;
19966 char_u *what;
19967 char_u *mode;
19968 char_u modebuf[NUMBUFLEN];
19969 int modec;
19970
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019971 id = get_tv_number(&argvars[0]);
19972 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019973 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019975 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019976 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019977 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019978 modec = 0; /* replace invalid with current */
19979 }
19980 else
19981 {
19982#ifdef FEAT_GUI
19983 if (gui.in_use)
19984 modec = 'g';
19985 else
19986#endif
19987 if (t_colors > 1)
19988 modec = 'c';
19989 else
19990 modec = 't';
19991 }
19992
19993
19994 switch (TOLOWER_ASC(what[0]))
19995 {
19996 case 'b':
19997 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19998 p = highlight_color(id, what, modec);
19999 else /* bold */
20000 p = highlight_has_attr(id, HL_BOLD, modec);
20001 break;
20002
Bram Moolenaar12682fd2010-03-10 13:43:49 +010020003 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020004 p = highlight_color(id, what, modec);
20005 break;
20006
20007 case 'i':
20008 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
20009 p = highlight_has_attr(id, HL_INVERSE, modec);
20010 else /* italic */
20011 p = highlight_has_attr(id, HL_ITALIC, modec);
20012 break;
20013
20014 case 'n': /* name */
20015 p = get_highlight_name(NULL, id - 1);
20016 break;
20017
20018 case 'r': /* reverse */
20019 p = highlight_has_attr(id, HL_INVERSE, modec);
20020 break;
20021
Bram Moolenaar6f507d62008-11-28 10:16:05 +000020022 case 's':
20023 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
20024 p = highlight_color(id, what, modec);
20025 else /* standout */
20026 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020027 break;
20028
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000020029 case 'u':
20030 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
20031 /* underline */
20032 p = highlight_has_attr(id, HL_UNDERLINE, modec);
20033 else
20034 /* undercurl */
20035 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 break;
20037 }
20038
20039 if (p != NULL)
20040 p = vim_strsave(p);
20041#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020042 rettv->v_type = VAR_STRING;
20043 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020044}
20045
20046/*
20047 * "synIDtrans(id)" function
20048 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020050f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020051{
20052 int id;
20053
20054#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020055 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020056
20057 if (id > 0)
20058 id = syn_get_final_id(id);
20059 else
20060#endif
20061 id = 0;
20062
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020063 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020064}
20065
20066/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020067 * "synconcealed(lnum, col)" function
20068 */
20069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020070f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020071{
20072#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20073 long lnum;
20074 long col;
20075 int syntax_flags = 0;
20076 int cchar;
20077 int matchid = 0;
20078 char_u str[NUMBUFLEN];
20079#endif
20080
20081 rettv->v_type = VAR_LIST;
20082 rettv->vval.v_list = NULL;
20083
20084#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20085 lnum = get_tv_lnum(argvars); /* -1 on type error */
20086 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20087
20088 vim_memset(str, NUL, sizeof(str));
20089
20090 if (rettv_list_alloc(rettv) != FAIL)
20091 {
20092 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20093 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20094 && curwin->w_p_cole > 0)
20095 {
20096 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20097 syntax_flags = get_syntax_info(&matchid);
20098
20099 /* get the conceal character */
20100 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20101 {
20102 cchar = syn_get_sub_char();
20103 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20104 cchar = lcs_conceal;
20105 if (cchar != NUL)
20106 {
20107# ifdef FEAT_MBYTE
20108 if (has_mbyte)
20109 (*mb_char2bytes)(cchar, str);
20110 else
20111# endif
20112 str[0] = cchar;
20113 }
20114 }
20115 }
20116
20117 list_append_number(rettv->vval.v_list,
20118 (syntax_flags & HL_CONCEAL) != 0);
20119 /* -1 to auto-determine strlen */
20120 list_append_string(rettv->vval.v_list, str, -1);
20121 list_append_number(rettv->vval.v_list, matchid);
20122 }
20123#endif
20124}
20125
20126/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020127 * "synstack(lnum, col)" function
20128 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020129 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020130f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020131{
20132#ifdef FEAT_SYN_HL
20133 long lnum;
20134 long col;
20135 int i;
20136 int id;
20137#endif
20138
20139 rettv->v_type = VAR_LIST;
20140 rettv->vval.v_list = NULL;
20141
20142#ifdef FEAT_SYN_HL
20143 lnum = get_tv_lnum(argvars); /* -1 on type error */
20144 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20145
20146 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020147 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020148 && rettv_list_alloc(rettv) != FAIL)
20149 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020150 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020151 for (i = 0; ; ++i)
20152 {
20153 id = syn_get_stack_item(i);
20154 if (id < 0)
20155 break;
20156 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20157 break;
20158 }
20159 }
20160#endif
20161}
20162
Bram Moolenaar071d4272004-06-13 20:20:40 +000020163 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020164get_cmd_output_as_rettv(
20165 typval_T *argvars,
20166 typval_T *rettv,
20167 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020168{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020169 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020170 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020171 char_u *infile = NULL;
20172 char_u buf[NUMBUFLEN];
20173 int err = FALSE;
20174 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020175 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020176 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020177
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020178 rettv->v_type = VAR_STRING;
20179 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020180 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020181 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020182
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020183 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020184 {
20185 /*
20186 * Write the string to a temp file, to be used for input of the shell
20187 * command.
20188 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020189 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020190 {
20191 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020192 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020193 }
20194
20195 fd = mch_fopen((char *)infile, WRITEBIN);
20196 if (fd == NULL)
20197 {
20198 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020199 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020200 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020201 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020202 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020203 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20204 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020205 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020206 else
20207 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020208 size_t len;
20209
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020210 p = get_tv_string_buf_chk(&argvars[1], buf);
20211 if (p == NULL)
20212 {
20213 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020214 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020215 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020216 len = STRLEN(p);
20217 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020218 err = TRUE;
20219 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020220 if (fclose(fd) != 0)
20221 err = TRUE;
20222 if (err)
20223 {
20224 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020225 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020226 }
20227 }
20228
Bram Moolenaar52a72462014-08-29 15:53:52 +020020229 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20230 * echoes typeahead, that messes up the display. */
20231 if (!msg_silent)
20232 flags += SHELL_COOKED;
20233
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020234 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020235 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020236 int len;
20237 listitem_T *li;
20238 char_u *s = NULL;
20239 char_u *start;
20240 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020241 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242
Bram Moolenaar52a72462014-08-29 15:53:52 +020020243 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020244 if (res == NULL)
20245 goto errret;
20246
20247 list = list_alloc();
20248 if (list == NULL)
20249 goto errret;
20250
20251 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020253 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020254 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020255 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020256 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020257
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020258 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020259 if (s == NULL)
20260 goto errret;
20261
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020262 for (p = s; start < end; ++p, ++start)
20263 *p = *start == NUL ? NL : *start;
20264 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020265
20266 li = listitem_alloc();
20267 if (li == NULL)
20268 {
20269 vim_free(s);
20270 goto errret;
20271 }
20272 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020273 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020274 li->li_tv.vval.v_string = s;
20275 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020276 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020277
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020278 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020279 rettv->v_type = VAR_LIST;
20280 rettv->vval.v_list = list;
20281 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020282 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020283 else
20284 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020285 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020286#ifdef USE_CR
20287 /* translate <CR> into <NL> */
20288 if (res != NULL)
20289 {
20290 char_u *s;
20291
20292 for (s = res; *s; ++s)
20293 {
20294 if (*s == CAR)
20295 *s = NL;
20296 }
20297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020298#else
20299# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020300 /* translate <CR><NL> into <NL> */
20301 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020302 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020303 char_u *s, *d;
20304
20305 d = res;
20306 for (s = res; *s; ++s)
20307 {
20308 if (s[0] == CAR && s[1] == NL)
20309 ++s;
20310 *d++ = *s;
20311 }
20312 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020313 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314# endif
20315#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020316 rettv->vval.v_string = res;
20317 res = NULL;
20318 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020319
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020320errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020321 if (infile != NULL)
20322 {
20323 mch_remove(infile);
20324 vim_free(infile);
20325 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020326 if (res != NULL)
20327 vim_free(res);
20328 if (list != NULL)
20329 list_free(list, TRUE);
20330}
20331
20332/*
20333 * "system()" function
20334 */
20335 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020336f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020337{
20338 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20339}
20340
20341/*
20342 * "systemlist()" function
20343 */
20344 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020345f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020346{
20347 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020348}
20349
20350/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020351 * "tabpagebuflist()" function
20352 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020354f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020355{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020356#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020357 tabpage_T *tp;
20358 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020359
20360 if (argvars[0].v_type == VAR_UNKNOWN)
20361 wp = firstwin;
20362 else
20363 {
20364 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20365 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020366 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020367 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020368 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020369 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020370 for (; wp != NULL; wp = wp->w_next)
20371 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020372 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020373 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020374 }
20375#endif
20376}
20377
20378
20379/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020380 * "tabpagenr()" function
20381 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020383f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020384{
20385 int nr = 1;
20386#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020387 char_u *arg;
20388
20389 if (argvars[0].v_type != VAR_UNKNOWN)
20390 {
20391 arg = get_tv_string_chk(&argvars[0]);
20392 nr = 0;
20393 if (arg != NULL)
20394 {
20395 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020396 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020397 else
20398 EMSG2(_(e_invexpr2), arg);
20399 }
20400 }
20401 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020402 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020403#endif
20404 rettv->vval.v_number = nr;
20405}
20406
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020407
20408#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020409static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020410
20411/*
20412 * Common code for tabpagewinnr() and winnr().
20413 */
20414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020415get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020416{
20417 win_T *twin;
20418 int nr = 1;
20419 win_T *wp;
20420 char_u *arg;
20421
20422 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20423 if (argvar->v_type != VAR_UNKNOWN)
20424 {
20425 arg = get_tv_string_chk(argvar);
20426 if (arg == NULL)
20427 nr = 0; /* type error; errmsg already given */
20428 else if (STRCMP(arg, "$") == 0)
20429 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20430 else if (STRCMP(arg, "#") == 0)
20431 {
20432 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20433 if (twin == NULL)
20434 nr = 0;
20435 }
20436 else
20437 {
20438 EMSG2(_(e_invexpr2), arg);
20439 nr = 0;
20440 }
20441 }
20442
20443 if (nr > 0)
20444 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20445 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020446 {
20447 if (wp == NULL)
20448 {
20449 /* didn't find it in this tabpage */
20450 nr = 0;
20451 break;
20452 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020453 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020454 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020455 return nr;
20456}
20457#endif
20458
20459/*
20460 * "tabpagewinnr()" function
20461 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020463f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020464{
20465 int nr = 1;
20466#ifdef FEAT_WINDOWS
20467 tabpage_T *tp;
20468
20469 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20470 if (tp == NULL)
20471 nr = 0;
20472 else
20473 nr = get_winnr(tp, &argvars[1]);
20474#endif
20475 rettv->vval.v_number = nr;
20476}
20477
20478
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020479/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020480 * "tagfiles()" function
20481 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020483f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020484{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020485 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020486 tagname_T tn;
20487 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020488
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020489 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020490 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020491 fname = alloc(MAXPATHL);
20492 if (fname == NULL)
20493 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020494
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020495 for (first = TRUE; ; first = FALSE)
20496 if (get_tagfname(&tn, first, fname) == FAIL
20497 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020498 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020499 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020500 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020501}
20502
20503/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020504 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020505 */
20506 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020507f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020508{
20509 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020510
20511 tag_pattern = get_tv_string(&argvars[0]);
20512
20513 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020514 if (*tag_pattern == NUL)
20515 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020516
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020517 if (rettv_list_alloc(rettv) == OK)
20518 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020519}
20520
20521/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020522 * "tempname()" function
20523 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020525f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020526{
20527 static int x = 'A';
20528
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020529 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020530 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531
20532 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20533 * names. Skip 'I' and 'O', they are used for shell redirection. */
20534 do
20535 {
20536 if (x == 'Z')
20537 x = '0';
20538 else if (x == '9')
20539 x = 'A';
20540 else
20541 {
20542#ifdef EBCDIC
20543 if (x == 'I')
20544 x = 'J';
20545 else if (x == 'R')
20546 x = 'S';
20547 else
20548#endif
20549 ++x;
20550 }
20551 } while (x == 'I' || x == 'O');
20552}
20553
20554/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020555 * "test(list)" function: Just checking the walls...
20556 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020558f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020559{
20560 /* Used for unit testing. Change the code below to your liking. */
20561#if 0
20562 listitem_T *li;
20563 list_T *l;
20564 char_u *bad, *good;
20565
20566 if (argvars[0].v_type != VAR_LIST)
20567 return;
20568 l = argvars[0].vval.v_list;
20569 if (l == NULL)
20570 return;
20571 li = l->lv_first;
20572 if (li == NULL)
20573 return;
20574 bad = get_tv_string(&li->li_tv);
20575 li = li->li_next;
20576 if (li == NULL)
20577 return;
20578 good = get_tv_string(&li->li_tv);
20579 rettv->vval.v_number = test_edit_score(bad, good);
20580#endif
20581}
20582
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020583#ifdef FEAT_FLOAT
20584/*
20585 * "tan()" function
20586 */
20587 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020588f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020589{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020590 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020591
20592 rettv->v_type = VAR_FLOAT;
20593 if (get_float_arg(argvars, &f) == OK)
20594 rettv->vval.v_float = tan(f);
20595 else
20596 rettv->vval.v_float = 0.0;
20597}
20598
20599/*
20600 * "tanh()" function
20601 */
20602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020603f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020604{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020605 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020606
20607 rettv->v_type = VAR_FLOAT;
20608 if (get_float_arg(argvars, &f) == OK)
20609 rettv->vval.v_float = tanh(f);
20610 else
20611 rettv->vval.v_float = 0.0;
20612}
20613#endif
20614
Bram Moolenaard52d9742005-08-21 22:20:28 +000020615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020616 * "tolower(string)" function
20617 */
20618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020619f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620{
20621 char_u *p;
20622
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020623 p = vim_strsave(get_tv_string(&argvars[0]));
20624 rettv->v_type = VAR_STRING;
20625 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626
20627 if (p != NULL)
20628 while (*p != NUL)
20629 {
20630#ifdef FEAT_MBYTE
20631 int l;
20632
20633 if (enc_utf8)
20634 {
20635 int c, lc;
20636
20637 c = utf_ptr2char(p);
20638 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020639 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640 /* TODO: reallocate string when byte count changes. */
20641 if (utf_char2len(lc) == l)
20642 utf_char2bytes(lc, p);
20643 p += l;
20644 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020645 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020646 p += l; /* skip multi-byte character */
20647 else
20648#endif
20649 {
20650 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20651 ++p;
20652 }
20653 }
20654}
20655
20656/*
20657 * "toupper(string)" function
20658 */
20659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020660f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020662 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020663 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664}
20665
20666/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020667 * "tr(string, fromstr, tostr)" function
20668 */
20669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020670f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020671{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020672 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020673 char_u *fromstr;
20674 char_u *tostr;
20675 char_u *p;
20676#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020677 int inlen;
20678 int fromlen;
20679 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020680 int idx;
20681 char_u *cpstr;
20682 int cplen;
20683 int first = TRUE;
20684#endif
20685 char_u buf[NUMBUFLEN];
20686 char_u buf2[NUMBUFLEN];
20687 garray_T ga;
20688
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020689 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020690 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20691 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020692
20693 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020694 rettv->v_type = VAR_STRING;
20695 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020696 if (fromstr == NULL || tostr == NULL)
20697 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020698 ga_init2(&ga, (int)sizeof(char), 80);
20699
20700#ifdef FEAT_MBYTE
20701 if (!has_mbyte)
20702#endif
20703 /* not multi-byte: fromstr and tostr must be the same length */
20704 if (STRLEN(fromstr) != STRLEN(tostr))
20705 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020706#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020707error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020708#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020709 EMSG2(_(e_invarg2), fromstr);
20710 ga_clear(&ga);
20711 return;
20712 }
20713
20714 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020715 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020716 {
20717#ifdef FEAT_MBYTE
20718 if (has_mbyte)
20719 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020720 inlen = (*mb_ptr2len)(in_str);
20721 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020722 cplen = inlen;
20723 idx = 0;
20724 for (p = fromstr; *p != NUL; p += fromlen)
20725 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020726 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020727 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020728 {
20729 for (p = tostr; *p != NUL; p += tolen)
20730 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020731 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020732 if (idx-- == 0)
20733 {
20734 cplen = tolen;
20735 cpstr = p;
20736 break;
20737 }
20738 }
20739 if (*p == NUL) /* tostr is shorter than fromstr */
20740 goto error;
20741 break;
20742 }
20743 ++idx;
20744 }
20745
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020746 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020747 {
20748 /* Check that fromstr and tostr have the same number of
20749 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020750 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020751 first = FALSE;
20752 for (p = tostr; *p != NUL; p += tolen)
20753 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020754 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020755 --idx;
20756 }
20757 if (idx != 0)
20758 goto error;
20759 }
20760
Bram Moolenaarcde88542015-08-11 19:14:00 +020020761 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020762 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020763 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020764
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020765 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020766 }
20767 else
20768#endif
20769 {
20770 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020771 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020772 if (p != NULL)
20773 ga_append(&ga, tostr[p - fromstr]);
20774 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020775 ga_append(&ga, *in_str);
20776 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020777 }
20778 }
20779
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020780 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020781 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020782 ga_append(&ga, NUL);
20783
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020784 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020785}
20786
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020787#ifdef FEAT_FLOAT
20788/*
20789 * "trunc({float})" function
20790 */
20791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020792f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020793{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020794 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020795
20796 rettv->v_type = VAR_FLOAT;
20797 if (get_float_arg(argvars, &f) == OK)
20798 /* trunc() is not in C90, use floor() or ceil() instead. */
20799 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20800 else
20801 rettv->vval.v_float = 0.0;
20802}
20803#endif
20804
Bram Moolenaar8299df92004-07-10 09:47:34 +000020805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020806 * "type(expr)" function
20807 */
20808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020809f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020810{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020811 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020812
20813 switch (argvars[0].v_type)
20814 {
20815 case VAR_NUMBER: n = 0; break;
20816 case VAR_STRING: n = 1; break;
20817 case VAR_FUNC: n = 2; break;
20818 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020819 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020820 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020821 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020822 if (argvars[0].vval.v_number == VVAL_FALSE
20823 || argvars[0].vval.v_number == VVAL_TRUE)
20824 n = 6;
20825 else
20826 n = 7;
20827 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020828 case VAR_JOB: n = 8; break;
20829 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020830 case VAR_UNKNOWN:
20831 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20832 n = -1;
20833 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020834 }
20835 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020836}
20837
20838/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020839 * "undofile(name)" function
20840 */
20841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020842f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020843{
20844 rettv->v_type = VAR_STRING;
20845#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020846 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020847 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020848
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020849 if (*fname == NUL)
20850 {
20851 /* If there is no file name there will be no undo file. */
20852 rettv->vval.v_string = NULL;
20853 }
20854 else
20855 {
20856 char_u *ffname = FullName_save(fname, FALSE);
20857
20858 if (ffname != NULL)
20859 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20860 vim_free(ffname);
20861 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020862 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020863#else
20864 rettv->vval.v_string = NULL;
20865#endif
20866}
20867
20868/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020869 * "undotree()" function
20870 */
20871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020872f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020873{
20874 if (rettv_dict_alloc(rettv) == OK)
20875 {
20876 dict_T *dict = rettv->vval.v_dict;
20877 list_T *list;
20878
Bram Moolenaar730cde92010-06-27 05:18:54 +020020879 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020880 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020881 dict_add_nr_str(dict, "save_last",
20882 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020883 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20884 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020885 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020886
20887 list = list_alloc();
20888 if (list != NULL)
20889 {
20890 u_eval_tree(curbuf->b_u_oldhead, list);
20891 dict_add_list(dict, "entries", list);
20892 }
20893 }
20894}
20895
20896/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020897 * "values(dict)" function
20898 */
20899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020900f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020901{
20902 dict_list(argvars, rettv, 1);
20903}
20904
20905/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020906 * "virtcol(string)" function
20907 */
20908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020909f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020910{
20911 colnr_T vcol = 0;
20912 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020913 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020915 fp = var2fpos(&argvars[0], FALSE, &fnum);
20916 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20917 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 {
20919 getvvcol(curwin, fp, NULL, NULL, &vcol);
20920 ++vcol;
20921 }
20922
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020923 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020924}
20925
20926/*
20927 * "visualmode()" function
20928 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020929 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020930f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020931{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020932 char_u str[2];
20933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020935 str[0] = curbuf->b_visual_mode_eval;
20936 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020937 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938
20939 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020940 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020942}
20943
20944/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020945 * "wildmenumode()" function
20946 */
20947 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020948f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020949{
20950#ifdef FEAT_WILDMENU
20951 if (wild_menu_showing)
20952 rettv->vval.v_number = 1;
20953#endif
20954}
20955
20956/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020957 * "winbufnr(nr)" function
20958 */
20959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020960f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961{
20962 win_T *wp;
20963
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020964 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020966 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020968 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969}
20970
20971/*
20972 * "wincol()" function
20973 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020975f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020976{
20977 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020978 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020979}
20980
20981/*
20982 * "winheight(nr)" function
20983 */
20984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020985f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020986{
20987 win_T *wp;
20988
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020989 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020991 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994}
20995
20996/*
20997 * "winline()" function
20998 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021000f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021001{
21002 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021003 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021004}
21005
21006/*
21007 * "winnr()" function
21008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021010f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011{
21012 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021013
Bram Moolenaar071d4272004-06-13 20:20:40 +000021014#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000021015 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021017 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018}
21019
21020/*
21021 * "winrestcmd()" function
21022 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021024f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021025{
21026#ifdef FEAT_WINDOWS
21027 win_T *wp;
21028 int winnr = 1;
21029 garray_T ga;
21030 char_u buf[50];
21031
21032 ga_init2(&ga, (int)sizeof(char), 70);
21033 for (wp = firstwin; wp != NULL; wp = wp->w_next)
21034 {
21035 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
21036 ga_concat(&ga, buf);
21037# ifdef FEAT_VERTSPLIT
21038 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
21039 ga_concat(&ga, buf);
21040# endif
21041 ++winnr;
21042 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000021043 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021044
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021045 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021046#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021047 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021048#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021049 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021050}
21051
21052/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021053 * "winrestview()" function
21054 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021056f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021057{
21058 dict_T *dict;
21059
21060 if (argvars[0].v_type != VAR_DICT
21061 || (dict = argvars[0].vval.v_dict) == NULL)
21062 EMSG(_(e_invarg));
21063 else
21064 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021065 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21066 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21067 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21068 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021070 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21071 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021072#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021073 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21074 {
21075 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21076 curwin->w_set_curswant = FALSE;
21077 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021078
Bram Moolenaar82c25852014-05-28 16:47:16 +020021079 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21080 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021081#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021082 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21083 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021084#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021085 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21086 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21087 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21088 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021089
21090 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021091 win_new_height(curwin, curwin->w_height);
21092# ifdef FEAT_VERTSPLIT
21093 win_new_width(curwin, W_WIDTH(curwin));
21094# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021095 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021096
Bram Moolenaarb851a962014-10-31 15:45:52 +010021097 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021098 curwin->w_topline = 1;
21099 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21100 curwin->w_topline = curbuf->b_ml.ml_line_count;
21101#ifdef FEAT_DIFF
21102 check_topfill(curwin, TRUE);
21103#endif
21104 }
21105}
21106
21107/*
21108 * "winsaveview()" function
21109 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021111f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021112{
21113 dict_T *dict;
21114
Bram Moolenaara800b422010-06-27 01:15:55 +020021115 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021116 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021117 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021118
21119 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21120 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21121#ifdef FEAT_VIRTUALEDIT
21122 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21123#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021124 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021125 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21126
21127 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21128#ifdef FEAT_DIFF
21129 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21130#endif
21131 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21132 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21133}
21134
21135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 * "winwidth(nr)" function
21137 */
21138 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021139f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021140{
21141 win_T *wp;
21142
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021143 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021145 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 else
21147#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021148 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021150 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021151#endif
21152}
21153
Bram Moolenaar071d4272004-06-13 20:20:40 +000021154/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021155 * "wordcount()" function
21156 */
21157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021158f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021159{
21160 if (rettv_dict_alloc(rettv) == FAIL)
21161 return;
21162 cursor_pos_info(rettv->vval.v_dict);
21163}
21164
21165/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021166 * Write list of strings to file
21167 */
21168 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021169write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021170{
21171 listitem_T *li;
21172 int c;
21173 int ret = OK;
21174 char_u *s;
21175
21176 for (li = list->lv_first; li != NULL; li = li->li_next)
21177 {
21178 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21179 {
21180 if (*s == '\n')
21181 c = putc(NUL, fd);
21182 else
21183 c = putc(*s, fd);
21184 if (c == EOF)
21185 {
21186 ret = FAIL;
21187 break;
21188 }
21189 }
21190 if (!binary || li->li_next != NULL)
21191 if (putc('\n', fd) == EOF)
21192 {
21193 ret = FAIL;
21194 break;
21195 }
21196 if (ret == FAIL)
21197 {
21198 EMSG(_(e_write));
21199 break;
21200 }
21201 }
21202 return ret;
21203}
21204
21205/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021206 * "writefile()" function
21207 */
21208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021209f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021210{
21211 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021212 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021213 char_u *fname;
21214 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021215 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021216
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021217 if (check_restricted() || check_secure())
21218 return;
21219
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021220 if (argvars[0].v_type != VAR_LIST)
21221 {
21222 EMSG2(_(e_listarg), "writefile()");
21223 return;
21224 }
21225 if (argvars[0].vval.v_list == NULL)
21226 return;
21227
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021228 if (argvars[2].v_type != VAR_UNKNOWN)
21229 {
21230 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21231 binary = TRUE;
21232 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21233 append = TRUE;
21234 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021235
21236 /* Always open the file in binary mode, library functions have a mind of
21237 * their own about CR-LF conversion. */
21238 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021239 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21240 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021241 {
21242 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21243 ret = -1;
21244 }
21245 else
21246 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021247 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21248 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021249 fclose(fd);
21250 }
21251
21252 rettv->vval.v_number = ret;
21253}
21254
21255/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021256 * "xor(expr, expr)" function
21257 */
21258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021259f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021260{
21261 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21262 ^ get_tv_number_chk(&argvars[1], NULL);
21263}
21264
21265
21266/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021267 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021268 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 */
21270 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021271var2fpos(
21272 typval_T *varp,
21273 int dollar_lnum, /* TRUE when $ is last line */
21274 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021276 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021278 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021279
Bram Moolenaara5525202006-03-02 22:52:09 +000021280 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021281 if (varp->v_type == VAR_LIST)
21282 {
21283 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021284 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021285 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021286 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021287
21288 l = varp->vval.v_list;
21289 if (l == NULL)
21290 return NULL;
21291
21292 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021293 pos.lnum = list_find_nr(l, 0L, &error);
21294 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021295 return NULL; /* invalid line number */
21296
21297 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021298 pos.col = list_find_nr(l, 1L, &error);
21299 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021300 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021301 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021302
21303 /* We accept "$" for the column number: last column. */
21304 li = list_find(l, 1L);
21305 if (li != NULL && li->li_tv.v_type == VAR_STRING
21306 && li->li_tv.vval.v_string != NULL
21307 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21308 pos.col = len + 1;
21309
Bram Moolenaara5525202006-03-02 22:52:09 +000021310 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021311 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021312 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021313 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021314
Bram Moolenaara5525202006-03-02 22:52:09 +000021315#ifdef FEAT_VIRTUALEDIT
21316 /* Get the virtual offset. Defaults to zero. */
21317 pos.coladd = list_find_nr(l, 2L, &error);
21318 if (error)
21319 pos.coladd = 0;
21320#endif
21321
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021322 return &pos;
21323 }
21324
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021325 name = get_tv_string_chk(varp);
21326 if (name == NULL)
21327 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021328 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021329 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021330 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21331 {
21332 if (VIsual_active)
21333 return &VIsual;
21334 return &curwin->w_cursor;
21335 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021336 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021337 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021338 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021339 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21340 return NULL;
21341 return pp;
21342 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021343
21344#ifdef FEAT_VIRTUALEDIT
21345 pos.coladd = 0;
21346#endif
21347
Bram Moolenaar477933c2007-07-17 14:32:23 +000021348 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021349 {
21350 pos.col = 0;
21351 if (name[1] == '0') /* "w0": first visible line */
21352 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021353 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021354 pos.lnum = curwin->w_topline;
21355 return &pos;
21356 }
21357 else if (name[1] == '$') /* "w$": last visible line */
21358 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021359 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021360 pos.lnum = curwin->w_botline - 1;
21361 return &pos;
21362 }
21363 }
21364 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021365 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021366 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021367 {
21368 pos.lnum = curbuf->b_ml.ml_line_count;
21369 pos.col = 0;
21370 }
21371 else
21372 {
21373 pos.lnum = curwin->w_cursor.lnum;
21374 pos.col = (colnr_T)STRLEN(ml_get_curline());
21375 }
21376 return &pos;
21377 }
21378 return NULL;
21379}
21380
21381/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021382 * Convert list in "arg" into a position and optional file number.
21383 * When "fnump" is NULL there is no file number, only 3 items.
21384 * Note that the column is passed on as-is, the caller may want to decrement
21385 * it to use 1 for the first column.
21386 * Return FAIL when conversion is not possible, doesn't check the position for
21387 * validity.
21388 */
21389 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021390list2fpos(
21391 typval_T *arg,
21392 pos_T *posp,
21393 int *fnump,
21394 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021395{
21396 list_T *l = arg->vval.v_list;
21397 long i = 0;
21398 long n;
21399
Bram Moolenaar493c1782014-05-28 14:34:46 +020021400 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21401 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021402 if (arg->v_type != VAR_LIST
21403 || l == NULL
21404 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021405 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021406 return FAIL;
21407
21408 if (fnump != NULL)
21409 {
21410 n = list_find_nr(l, i++, NULL); /* fnum */
21411 if (n < 0)
21412 return FAIL;
21413 if (n == 0)
21414 n = curbuf->b_fnum; /* current buffer */
21415 *fnump = n;
21416 }
21417
21418 n = list_find_nr(l, i++, NULL); /* lnum */
21419 if (n < 0)
21420 return FAIL;
21421 posp->lnum = n;
21422
21423 n = list_find_nr(l, i++, NULL); /* col */
21424 if (n < 0)
21425 return FAIL;
21426 posp->col = n;
21427
21428#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021429 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021430 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021431 posp->coladd = 0;
21432 else
21433 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021434#endif
21435
Bram Moolenaar493c1782014-05-28 14:34:46 +020021436 if (curswantp != NULL)
21437 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21438
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021439 return OK;
21440}
21441
21442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 * Get the length of an environment variable name.
21444 * Advance "arg" to the first character after the name.
21445 * Return 0 for error.
21446 */
21447 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021448get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449{
21450 char_u *p;
21451 int len;
21452
21453 for (p = *arg; vim_isIDc(*p); ++p)
21454 ;
21455 if (p == *arg) /* no name found */
21456 return 0;
21457
21458 len = (int)(p - *arg);
21459 *arg = p;
21460 return len;
21461}
21462
21463/*
21464 * Get the length of the name of a function or internal variable.
21465 * "arg" is advanced to the first non-white character after the name.
21466 * Return 0 if something is wrong.
21467 */
21468 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021469get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021470{
21471 char_u *p;
21472 int len;
21473
21474 /* Find the end of the name. */
21475 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021476 {
21477 if (*p == ':')
21478 {
21479 /* "s:" is start of "s:var", but "n:" is not and can be used in
21480 * slice "[n:]". Also "xx:" is not a namespace. */
21481 len = (int)(p - *arg);
21482 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21483 || len > 1)
21484 break;
21485 }
21486 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021487 if (p == *arg) /* no name found */
21488 return 0;
21489
21490 len = (int)(p - *arg);
21491 *arg = skipwhite(p);
21492
21493 return len;
21494}
21495
21496/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021497 * Get the length of the name of a variable or function.
21498 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021500 * Return -1 if curly braces expansion failed.
21501 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502 * If the name contains 'magic' {}'s, expand them and return the
21503 * expanded name in an allocated string via 'alias' - caller must free.
21504 */
21505 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021506get_name_len(
21507 char_u **arg,
21508 char_u **alias,
21509 int evaluate,
21510 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511{
21512 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021513 char_u *p;
21514 char_u *expr_start;
21515 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021516
21517 *alias = NULL; /* default to no alias */
21518
21519 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21520 && (*arg)[2] == (int)KE_SNR)
21521 {
21522 /* hard coded <SNR>, already translated */
21523 *arg += 3;
21524 return get_id_len(arg) + 3;
21525 }
21526 len = eval_fname_script(*arg);
21527 if (len > 0)
21528 {
21529 /* literal "<SID>", "s:" or "<SNR>" */
21530 *arg += len;
21531 }
21532
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021534 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021535 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021536 p = find_name_end(*arg, &expr_start, &expr_end,
21537 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021538 if (expr_start != NULL)
21539 {
21540 char_u *temp_string;
21541
21542 if (!evaluate)
21543 {
21544 len += (int)(p - *arg);
21545 *arg = skipwhite(p);
21546 return len;
21547 }
21548
21549 /*
21550 * Include any <SID> etc in the expanded string:
21551 * Thus the -len here.
21552 */
21553 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21554 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021555 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021556 *alias = temp_string;
21557 *arg = skipwhite(p);
21558 return (int)STRLEN(temp_string);
21559 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021560
21561 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021562 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021563 EMSG2(_(e_invexpr2), *arg);
21564
21565 return len;
21566}
21567
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021568/*
21569 * Find the end of a variable or function name, taking care of magic braces.
21570 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21571 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021572 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021573 * Return a pointer to just after the name. Equal to "arg" if there is no
21574 * valid name.
21575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021577find_name_end(
21578 char_u *arg,
21579 char_u **expr_start,
21580 char_u **expr_end,
21581 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021583 int mb_nest = 0;
21584 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021586 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021588 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021589 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021590 *expr_start = NULL;
21591 *expr_end = NULL;
21592 }
21593
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021594 /* Quick check for valid starting character. */
21595 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21596 return arg;
21597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021598 for (p = arg; *p != NUL
21599 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021600 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021601 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021602 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021603 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021604 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021605 if (*p == '\'')
21606 {
21607 /* skip over 'string' to avoid counting [ and ] inside it. */
21608 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21609 ;
21610 if (*p == NUL)
21611 break;
21612 }
21613 else if (*p == '"')
21614 {
21615 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21616 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21617 if (*p == '\\' && p[1] != NUL)
21618 ++p;
21619 if (*p == NUL)
21620 break;
21621 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021622 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21623 {
21624 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021625 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021626 len = (int)(p - arg);
21627 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021628 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021629 break;
21630 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021632 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021634 if (*p == '[')
21635 ++br_nest;
21636 else if (*p == ']')
21637 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021638 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021640 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021642 if (*p == '{')
21643 {
21644 mb_nest++;
21645 if (expr_start != NULL && *expr_start == NULL)
21646 *expr_start = p;
21647 }
21648 else if (*p == '}')
21649 {
21650 mb_nest--;
21651 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21652 *expr_end = p;
21653 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021655 }
21656
21657 return p;
21658}
21659
21660/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021661 * Expands out the 'magic' {}'s in a variable/function name.
21662 * Note that this can call itself recursively, to deal with
21663 * constructs like foo{bar}{baz}{bam}
21664 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21665 * "in_start" ^
21666 * "expr_start" ^
21667 * "expr_end" ^
21668 * "in_end" ^
21669 *
21670 * Returns a new allocated string, which the caller must free.
21671 * Returns NULL for failure.
21672 */
21673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021674make_expanded_name(
21675 char_u *in_start,
21676 char_u *expr_start,
21677 char_u *expr_end,
21678 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021679{
21680 char_u c1;
21681 char_u *retval = NULL;
21682 char_u *temp_result;
21683 char_u *nextcmd = NULL;
21684
21685 if (expr_end == NULL || in_end == NULL)
21686 return NULL;
21687 *expr_start = NUL;
21688 *expr_end = NUL;
21689 c1 = *in_end;
21690 *in_end = NUL;
21691
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021692 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021693 if (temp_result != NULL && nextcmd == NULL)
21694 {
21695 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21696 + (in_end - expr_end) + 1));
21697 if (retval != NULL)
21698 {
21699 STRCPY(retval, in_start);
21700 STRCAT(retval, temp_result);
21701 STRCAT(retval, expr_end + 1);
21702 }
21703 }
21704 vim_free(temp_result);
21705
21706 *in_end = c1; /* put char back for error messages */
21707 *expr_start = '{';
21708 *expr_end = '}';
21709
21710 if (retval != NULL)
21711 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021712 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021713 if (expr_start != NULL)
21714 {
21715 /* Further expansion! */
21716 temp_result = make_expanded_name(retval, expr_start,
21717 expr_end, temp_result);
21718 vim_free(retval);
21719 retval = temp_result;
21720 }
21721 }
21722
21723 return retval;
21724}
21725
21726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021727 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021728 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 */
21730 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021732{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021733 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21734}
21735
21736/*
21737 * Return TRUE if character "c" can be used as the first character in a
21738 * variable or function name (excluding '{' and '}').
21739 */
21740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021741eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021742{
21743 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021744}
21745
21746/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021747 * Set number v: variable to "val".
21748 */
21749 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021750set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021751{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021752 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753}
21754
21755/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021756 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021757 */
21758 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021759get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021760{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021761 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021762}
21763
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021764/*
21765 * Get string v: variable value. Uses a static buffer, can only be used once.
21766 */
21767 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021768get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021769{
21770 return get_tv_string(&vimvars[idx].vv_tv);
21771}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021772
Bram Moolenaar071d4272004-06-13 20:20:40 +000021773/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021774 * Get List v: variable value. Caller must take care of reference count when
21775 * needed.
21776 */
21777 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021779{
21780 return vimvars[idx].vv_list;
21781}
21782
21783/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021784 * Set v:char to character "c".
21785 */
21786 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021787set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021788{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021789 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021790
21791#ifdef FEAT_MBYTE
21792 if (has_mbyte)
21793 buf[(*mb_char2bytes)(c, buf)] = NUL;
21794 else
21795#endif
21796 {
21797 buf[0] = c;
21798 buf[1] = NUL;
21799 }
21800 set_vim_var_string(VV_CHAR, buf, -1);
21801}
21802
21803/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021804 * Set v:count to "count" and v:count1 to "count1".
21805 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 */
21807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021808set_vcount(
21809 long count,
21810 long count1,
21811 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021812{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021813 if (set_prevcount)
21814 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021815 vimvars[VV_COUNT].vv_nr = count;
21816 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021817}
21818
21819/*
21820 * Set string v: variable to a copy of "val".
21821 */
21822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021823set_vim_var_string(
21824 int idx,
21825 char_u *val,
21826 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827{
Bram Moolenaara542c682016-01-31 16:28:04 +010021828 clear_tv(&vimvars[idx].vv_di.di_tv);
21829 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021830 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021831 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021832 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021833 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021834 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021835 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021836}
21837
21838/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021839 * Set List v: variable to "val".
21840 */
21841 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021842set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021843{
Bram Moolenaara542c682016-01-31 16:28:04 +010021844 clear_tv(&vimvars[idx].vv_di.di_tv);
21845 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021846 vimvars[idx].vv_list = val;
21847 if (val != NULL)
21848 ++val->lv_refcount;
21849}
21850
21851/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021852 * Set Dictionary v: variable to "val".
21853 */
21854 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021855set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021856{
21857 int todo;
21858 hashitem_T *hi;
21859
Bram Moolenaara542c682016-01-31 16:28:04 +010021860 clear_tv(&vimvars[idx].vv_di.di_tv);
21861 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021862 vimvars[idx].vv_dict = val;
21863 if (val != NULL)
21864 {
21865 ++val->dv_refcount;
21866
21867 /* Set readonly */
21868 todo = (int)val->dv_hashtab.ht_used;
21869 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21870 {
21871 if (HASHITEM_EMPTY(hi))
21872 continue;
21873 --todo;
21874 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21875 }
21876 }
21877}
21878
21879/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021880 * Set v:register if needed.
21881 */
21882 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
21885 char_u regname;
21886
21887 if (c == 0 || c == ' ')
21888 regname = '"';
21889 else
21890 regname = c;
21891 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021892 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 set_vim_var_string(VV_REG, &regname, 1);
21894}
21895
21896/*
21897 * Get or set v:exception. If "oldval" == NULL, return the current value.
21898 * Otherwise, restore the value to "oldval" and return NULL.
21899 * Must always be called in pairs to save and restore v:exception! Does not
21900 * take care of memory allocations.
21901 */
21902 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021903v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021904{
21905 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021906 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021907
Bram Moolenaare9a41262005-01-15 22:18:47 +000021908 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021909 return NULL;
21910}
21911
21912/*
21913 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21914 * Otherwise, restore the value to "oldval" and return NULL.
21915 * Must always be called in pairs to save and restore v:throwpoint! Does not
21916 * take care of memory allocations.
21917 */
21918 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021919v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021920{
21921 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021922 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923
Bram Moolenaare9a41262005-01-15 22:18:47 +000021924 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925 return NULL;
21926}
21927
21928#if defined(FEAT_AUTOCMD) || defined(PROTO)
21929/*
21930 * Set v:cmdarg.
21931 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21932 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21933 * Must always be called in pairs!
21934 */
21935 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021936set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937{
21938 char_u *oldval;
21939 char_u *newval;
21940 unsigned len;
21941
Bram Moolenaare9a41262005-01-15 22:18:47 +000021942 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021943 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021945 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021946 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021947 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021948 }
21949
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021950 if (eap->force_bin == FORCE_BIN)
21951 len = 6;
21952 else if (eap->force_bin == FORCE_NOBIN)
21953 len = 8;
21954 else
21955 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021956
21957 if (eap->read_edit)
21958 len += 7;
21959
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021960 if (eap->force_ff != 0)
21961 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21962# ifdef FEAT_MBYTE
21963 if (eap->force_enc != 0)
21964 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021965 if (eap->bad_char != 0)
21966 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021967# endif
21968
21969 newval = alloc(len + 1);
21970 if (newval == NULL)
21971 return NULL;
21972
21973 if (eap->force_bin == FORCE_BIN)
21974 sprintf((char *)newval, " ++bin");
21975 else if (eap->force_bin == FORCE_NOBIN)
21976 sprintf((char *)newval, " ++nobin");
21977 else
21978 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021979
21980 if (eap->read_edit)
21981 STRCAT(newval, " ++edit");
21982
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021983 if (eap->force_ff != 0)
21984 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21985 eap->cmd + eap->force_ff);
21986# ifdef FEAT_MBYTE
21987 if (eap->force_enc != 0)
21988 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21989 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021990 if (eap->bad_char == BAD_KEEP)
21991 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21992 else if (eap->bad_char == BAD_DROP)
21993 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21994 else if (eap->bad_char != 0)
21995 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021996# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021997 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021998 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021999}
22000#endif
22001
22002/*
22003 * Get the value of internal variable "name".
22004 * Return OK or FAIL.
22005 */
22006 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022007get_var_tv(
22008 char_u *name,
22009 int len, /* length of "name" */
22010 typval_T *rettv, /* NULL when only checking existence */
22011 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
22012 int verbose, /* may give error message */
22013 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022014{
22015 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000022016 typval_T *tv = NULL;
22017 typval_T atv;
22018 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022019 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022020
22021 /* truncate the name, so that we can use strcmp() */
22022 cc = name[len];
22023 name[len] = NUL;
22024
22025 /*
22026 * Check for "b:changedtick".
22027 */
22028 if (STRCMP(name, "b:changedtick") == 0)
22029 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000022030 atv.v_type = VAR_NUMBER;
22031 atv.vval.v_number = curbuf->b_changedtick;
22032 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022033 }
22034
22035 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 * Check for user-defined variables.
22037 */
22038 else
22039 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022040 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022041 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022042 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022043 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022044 if (dip != NULL)
22045 *dip = v;
22046 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022047 }
22048
Bram Moolenaare9a41262005-01-15 22:18:47 +000022049 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022051 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022052 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022053 ret = FAIL;
22054 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022055 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022056 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022057
22058 name[len] = cc;
22059
22060 return ret;
22061}
22062
22063/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022064 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22065 * Also handle function call with Funcref variable: func(expr)
22066 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22067 */
22068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022069handle_subscript(
22070 char_u **arg,
22071 typval_T *rettv,
22072 int evaluate, /* do more than finding the end */
22073 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022074{
22075 int ret = OK;
22076 dict_T *selfdict = NULL;
22077 char_u *s;
22078 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022079 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022080
22081 while (ret == OK
22082 && (**arg == '['
22083 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022084 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022085 && !vim_iswhite(*(*arg - 1)))
22086 {
22087 if (**arg == '(')
22088 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022089 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022090 if (evaluate)
22091 {
22092 functv = *rettv;
22093 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022094
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022095 /* Invoke the function. Recursive! */
22096 s = functv.vval.v_string;
22097 }
22098 else
22099 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022100 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022101 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22102 &len, evaluate, selfdict);
22103
22104 /* Clear the funcref afterwards, so that deleting it while
22105 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022106 if (evaluate)
22107 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022108
22109 /* Stop the expression evaluation when immediately aborting on
22110 * error, or when an interrupt occurred or an exception was thrown
22111 * but not caught. */
22112 if (aborting())
22113 {
22114 if (ret == OK)
22115 clear_tv(rettv);
22116 ret = FAIL;
22117 }
22118 dict_unref(selfdict);
22119 selfdict = NULL;
22120 }
22121 else /* **arg == '[' || **arg == '.' */
22122 {
22123 dict_unref(selfdict);
22124 if (rettv->v_type == VAR_DICT)
22125 {
22126 selfdict = rettv->vval.v_dict;
22127 if (selfdict != NULL)
22128 ++selfdict->dv_refcount;
22129 }
22130 else
22131 selfdict = NULL;
22132 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22133 {
22134 clear_tv(rettv);
22135 ret = FAIL;
22136 }
22137 }
22138 }
22139 dict_unref(selfdict);
22140 return ret;
22141}
22142
22143/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022144 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022145 * value).
22146 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022147 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022148alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022149{
Bram Moolenaar33570922005-01-25 22:26:29 +000022150 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022151}
22152
22153/*
22154 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022155 * The string "s" must have been allocated, it is consumed.
22156 * Return NULL for out of memory, the variable otherwise.
22157 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022158 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022159alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022160{
Bram Moolenaar33570922005-01-25 22:26:29 +000022161 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022163 rettv = alloc_tv();
22164 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022166 rettv->v_type = VAR_STRING;
22167 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 }
22169 else
22170 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022171 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172}
22173
22174/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022175 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022177 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022179{
22180 if (varp != NULL)
22181 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022182 switch (varp->v_type)
22183 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022184 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022185 func_unref(varp->vval.v_string);
22186 /*FALLTHROUGH*/
22187 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022188 vim_free(varp->vval.v_string);
22189 break;
22190 case VAR_LIST:
22191 list_unref(varp->vval.v_list);
22192 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022193 case VAR_DICT:
22194 dict_unref(varp->vval.v_dict);
22195 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022196 case VAR_JOB:
22197#ifdef FEAT_JOB
22198 job_unref(varp->vval.v_job);
22199 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022200#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022201 case VAR_CHANNEL:
22202#ifdef FEAT_CHANNEL
22203 channel_unref(varp->vval.v_channel);
22204 break;
22205#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022206 case VAR_NUMBER:
22207 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022208 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022209 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022210 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 vim_free(varp);
22213 }
22214}
22215
22216/*
22217 * Free the memory for a variable value and set the value to NULL or 0.
22218 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022219 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022220clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022221{
22222 if (varp != NULL)
22223 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022224 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022225 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022226 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022227 func_unref(varp->vval.v_string);
22228 /*FALLTHROUGH*/
22229 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022230 vim_free(varp->vval.v_string);
22231 varp->vval.v_string = NULL;
22232 break;
22233 case VAR_LIST:
22234 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022235 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022236 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022237 case VAR_DICT:
22238 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022239 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022240 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022241 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022242 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 varp->vval.v_number = 0;
22244 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022245 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022246#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022247 varp->vval.v_float = 0.0;
22248 break;
22249#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022250 case VAR_JOB:
22251#ifdef FEAT_JOB
22252 job_unref(varp->vval.v_job);
22253 varp->vval.v_job = NULL;
22254#endif
22255 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022256 case VAR_CHANNEL:
22257#ifdef FEAT_CHANNEL
22258 channel_unref(varp->vval.v_channel);
22259 varp->vval.v_channel = NULL;
22260#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022261 case VAR_UNKNOWN:
22262 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022264 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 }
22266}
22267
22268/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022269 * Set the value of a variable to NULL without freeing items.
22270 */
22271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022272init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273{
22274 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022275 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022276}
22277
22278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 * Get the number value of a variable.
22280 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022281 * For incompatible types, return 0.
22282 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22283 * caller of incompatible types: it sets *denote to TRUE if "denote"
22284 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 */
22286 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022287get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022289 int error = FALSE;
22290
22291 return get_tv_number_chk(varp, &error); /* return 0L on error */
22292}
22293
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022294 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022295get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022296{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022297 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022298
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022299 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022300 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022301 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022302 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022303 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022304#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022305 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022306 break;
22307#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022308 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022309 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022310 break;
22311 case VAR_STRING:
22312 if (varp->vval.v_string != NULL)
22313 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022314 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022315 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022316 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022317 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022318 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022319 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022320 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022321 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022322 case VAR_SPECIAL:
22323 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22324 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022325 case VAR_JOB:
22326#ifdef FEAT_JOB
22327 EMSG(_("E910: Using a Job as a Number"));
22328 break;
22329#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022330 case VAR_CHANNEL:
22331#ifdef FEAT_CHANNEL
22332 EMSG(_("E913: Using a Channel as a Number"));
22333 break;
22334#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022335 case VAR_UNKNOWN:
22336 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022337 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022338 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022339 if (denote == NULL) /* useful for values that must be unsigned */
22340 n = -1;
22341 else
22342 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022343 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344}
22345
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022346#ifdef FEAT_FLOAT
22347 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022348get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022349{
22350 switch (varp->v_type)
22351 {
22352 case VAR_NUMBER:
22353 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022354 case VAR_FLOAT:
22355 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022356 case VAR_FUNC:
22357 EMSG(_("E891: Using a Funcref as a Float"));
22358 break;
22359 case VAR_STRING:
22360 EMSG(_("E892: Using a String as a Float"));
22361 break;
22362 case VAR_LIST:
22363 EMSG(_("E893: Using a List as a Float"));
22364 break;
22365 case VAR_DICT:
22366 EMSG(_("E894: Using a Dictionary as a Float"));
22367 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022368 case VAR_SPECIAL:
22369 EMSG(_("E907: Using a special value as a Float"));
22370 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022371 case VAR_JOB:
22372# ifdef FEAT_JOB
22373 EMSG(_("E911: Using a Job as a Float"));
22374 break;
22375# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022376 case VAR_CHANNEL:
22377# ifdef FEAT_CHANNEL
22378 EMSG(_("E914: Using a Channel as a Float"));
22379 break;
22380# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022381 case VAR_UNKNOWN:
22382 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022383 break;
22384 }
22385 return 0;
22386}
22387#endif
22388
Bram Moolenaar071d4272004-06-13 20:20:40 +000022389/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022390 * Get the lnum from the first argument.
22391 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022392 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393 */
22394 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022395get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022396{
Bram Moolenaar33570922005-01-25 22:26:29 +000022397 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 linenr_T lnum;
22399
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022400 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401 if (lnum == 0) /* no valid number, try using line() */
22402 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022403 rettv.v_type = VAR_NUMBER;
22404 f_line(argvars, &rettv);
22405 lnum = rettv.vval.v_number;
22406 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022407 }
22408 return lnum;
22409}
22410
22411/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022412 * Get the lnum from the first argument.
22413 * Also accepts "$", then "buf" is used.
22414 * Returns 0 on error.
22415 */
22416 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022417get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022418{
22419 if (argvars[0].v_type == VAR_STRING
22420 && argvars[0].vval.v_string != NULL
22421 && argvars[0].vval.v_string[0] == '$'
22422 && buf != NULL)
22423 return buf->b_ml.ml_line_count;
22424 return get_tv_number_chk(&argvars[0], NULL);
22425}
22426
22427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 * Get the string value of a variable.
22429 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022430 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22431 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 * If the String variable has never been set, return an empty string.
22433 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022434 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22435 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436 */
22437 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022438get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022439{
22440 static char_u mybuf[NUMBUFLEN];
22441
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022442 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022443}
22444
22445 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022446get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022447{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022448 char_u *res = get_tv_string_buf_chk(varp, buf);
22449
22450 return res != NULL ? res : (char_u *)"";
22451}
22452
Bram Moolenaar7d647822014-04-05 21:28:56 +020022453/*
22454 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22455 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022456 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022457get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022458{
22459 static char_u mybuf[NUMBUFLEN];
22460
22461 return get_tv_string_buf_chk(varp, mybuf);
22462}
22463
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022464 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022465get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022466{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022467 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022469 case VAR_NUMBER:
22470 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22471 return buf;
22472 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022473 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022474 break;
22475 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022476 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022477 break;
22478 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022479 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022481 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022482#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022483 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022484 break;
22485#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022486 case VAR_STRING:
22487 if (varp->vval.v_string != NULL)
22488 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022489 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022490 case VAR_SPECIAL:
22491 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22492 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022493 case VAR_JOB:
22494#ifdef FEAT_JOB
22495 {
22496 job_T *job = varp->vval.v_job;
22497 char *status = job->jv_status == JOB_FAILED ? "fail"
22498 : job->jv_status == JOB_ENDED ? "dead"
22499 : "run";
22500# ifdef UNIX
22501 vim_snprintf((char *)buf, NUMBUFLEN,
22502 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022503# elif defined(WIN32)
22504 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022505 "process %ld %s",
22506 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022507 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022508# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022509 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022510 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22511# endif
22512 return buf;
22513 }
22514#endif
22515 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022516 case VAR_CHANNEL:
22517#ifdef FEAT_CHANNEL
22518 {
22519 channel_T *channel = varp->vval.v_channel;
22520 char *status = channel_status(channel);
22521
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022522 if (channel == NULL)
22523 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22524 else
22525 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022526 "channel %d %s", channel->ch_id, status);
22527 return buf;
22528 }
22529#endif
22530 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022531 case VAR_UNKNOWN:
22532 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022533 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022534 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022535 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022536}
22537
22538/*
22539 * Find variable "name" in the list of variables.
22540 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022541 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022542 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022543 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022546find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022547{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022548 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550
Bram Moolenaara7043832005-01-21 11:56:39 +000022551 ht = find_var_ht(name, &varname);
22552 if (htp != NULL)
22553 *htp = ht;
22554 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022555 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022556 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022557}
22558
22559/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022560 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022561 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022564find_var_in_ht(
22565 hashtab_T *ht,
22566 int htname,
22567 char_u *varname,
22568 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022569{
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 hashitem_T *hi;
22571
22572 if (*varname == NUL)
22573 {
22574 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022575 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022577 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 case 'g': return &globvars_var;
22579 case 'v': return &vimvars_var;
22580 case 'b': return &curbuf->b_bufvar;
22581 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022582#ifdef FEAT_WINDOWS
22583 case 't': return &curtab->tp_winvar;
22584#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022585 case 'l': return current_funccal == NULL
22586 ? NULL : &current_funccal->l_vars_var;
22587 case 'a': return current_funccal == NULL
22588 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 }
22590 return NULL;
22591 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022592
22593 hi = hash_find(ht, varname);
22594 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022595 {
22596 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022597 * worked find the variable again. Don't auto-load a script if it was
22598 * loaded already, otherwise it would be loaded every time when
22599 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022600 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022601 {
22602 /* Note: script_autoload() may make "hi" invalid. It must either
22603 * be obtained again or not used. */
22604 if (!script_autoload(varname, FALSE) || aborting())
22605 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022606 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022607 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022608 if (HASHITEM_EMPTY(hi))
22609 return NULL;
22610 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022612}
22613
22614/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022615 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022616 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022617 * Set "varname" to the start of name without ':'.
22618 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022619 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022620find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022621{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022622 hashitem_T *hi;
22623
Bram Moolenaar73627d02015-08-11 15:46:09 +020022624 if (name[0] == NUL)
22625 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022626 if (name[1] != ':')
22627 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022628 /* The name must not start with a colon or #. */
22629 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630 return NULL;
22631 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022632
22633 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022634 hi = hash_find(&compat_hashtab, name);
22635 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022636 return &compat_hashtab;
22637
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022639 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022640 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 }
22642 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022643 if (*name == 'g') /* global variable */
22644 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022645 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22646 */
22647 if (vim_strchr(name + 2, ':') != NULL
22648 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022649 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022651 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022653 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022654#ifdef FEAT_WINDOWS
22655 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022656 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022657#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 if (*name == 'v') /* v: variable */
22659 return &vimvarht;
22660 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022661 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022662 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022663 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 if (*name == 's' /* script variable */
22665 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22666 return &SCRIPT_VARS(current_SID);
22667 return NULL;
22668}
22669
22670/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022671 * Get function call environment based on bactrace debug level
22672 */
22673 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022674get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022675{
22676 int i;
22677 funccall_T *funccal;
22678 funccall_T *temp_funccal;
22679
22680 funccal = current_funccal;
22681 if (debug_backtrace_level > 0)
22682 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022683 for (i = 0; i < debug_backtrace_level; i++)
22684 {
22685 temp_funccal = funccal->caller;
22686 if (temp_funccal)
22687 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022688 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022689 /* backtrace level overflow. reset to max */
22690 debug_backtrace_level = i;
22691 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022692 }
22693 return funccal;
22694}
22695
22696/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022698 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 * Returns NULL when it doesn't exist.
22700 */
22701 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022702get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022703{
Bram Moolenaar33570922005-01-25 22:26:29 +000022704 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022705
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022706 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022707 if (v == NULL)
22708 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022710}
22711
22712/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022714 * sourcing this script and when executing functions defined in the script.
22715 */
22716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022717new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718{
Bram Moolenaara7043832005-01-21 11:56:39 +000022719 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022720 hashtab_T *ht;
22721 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022722
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22724 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022725 /* Re-allocating ga_data means that an ht_array pointing to
22726 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022728 for (i = 1; i <= ga_scripts.ga_len; ++i)
22729 {
22730 ht = &SCRIPT_VARS(i);
22731 if (ht->ht_mask == HT_INIT_SIZE - 1)
22732 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022733 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022734 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022735 }
22736
Bram Moolenaar071d4272004-06-13 20:20:40 +000022737 while (ga_scripts.ga_len < id)
22738 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022739 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022740 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022741 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022743 }
22744 }
22745}
22746
22747/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022748 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22749 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 */
22751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022752init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022753{
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022755 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022756 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022757 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022758 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 dict_var->di_tv.vval.v_dict = dict;
22760 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022761 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022762 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22763 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022764}
22765
22766/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022767 * Unreference a dictionary initialized by init_var_dict().
22768 */
22769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022770unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022771{
22772 /* Now the dict needs to be freed if no one else is using it, go back to
22773 * normal reference counting. */
22774 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22775 dict_unref(dict);
22776}
22777
22778/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022779 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022780 * Frees all allocated variables and the value they contain.
22781 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022782 */
22783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022784vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022785{
22786 vars_clear_ext(ht, TRUE);
22787}
22788
22789/*
22790 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22791 */
22792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022793vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022794{
Bram Moolenaara7043832005-01-21 11:56:39 +000022795 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022796 hashitem_T *hi;
22797 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798
Bram Moolenaar33570922005-01-25 22:26:29 +000022799 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022800 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022801 for (hi = ht->ht_array; todo > 0; ++hi)
22802 {
22803 if (!HASHITEM_EMPTY(hi))
22804 {
22805 --todo;
22806
Bram Moolenaar33570922005-01-25 22:26:29 +000022807 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022808 * ht_array might change then. hash_clear() takes care of it
22809 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022810 v = HI2DI(hi);
22811 if (free_val)
22812 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022813 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022814 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022815 }
22816 }
22817 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022818 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022819}
22820
Bram Moolenaara7043832005-01-21 11:56:39 +000022821/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 * Delete a variable from hashtab "ht" at item "hi".
22823 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022826delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022827{
Bram Moolenaar33570922005-01-25 22:26:29 +000022828 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022829
22830 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022831 clear_tv(&di->di_tv);
22832 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022833}
22834
22835/*
22836 * List the value of one internal variable.
22837 */
22838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022839list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022841 char_u *tofree;
22842 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022843 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022844
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022845 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022846 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022847 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022848 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022849}
22850
Bram Moolenaar071d4272004-06-13 20:20:40 +000022851 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022852list_one_var_a(
22853 char_u *prefix,
22854 char_u *name,
22855 int type,
22856 char_u *string,
22857 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022858{
Bram Moolenaar31859182007-08-14 20:41:13 +000022859 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22860 msg_start();
22861 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022862 if (name != NULL) /* "a:" vars don't have a name stored */
22863 msg_puts(name);
22864 msg_putchar(' ');
22865 msg_advance(22);
22866 if (type == VAR_NUMBER)
22867 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022868 else if (type == VAR_FUNC)
22869 msg_putchar('*');
22870 else if (type == VAR_LIST)
22871 {
22872 msg_putchar('[');
22873 if (*string == '[')
22874 ++string;
22875 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022876 else if (type == VAR_DICT)
22877 {
22878 msg_putchar('{');
22879 if (*string == '{')
22880 ++string;
22881 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022882 else
22883 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884
Bram Moolenaar071d4272004-06-13 20:20:40 +000022885 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886
22887 if (type == VAR_FUNC)
22888 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022889 if (*first)
22890 {
22891 msg_clr_eos();
22892 *first = FALSE;
22893 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022894}
22895
22896/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022897 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022898 * If the variable already exists, the value is updated.
22899 * Otherwise the variable is created.
22900 */
22901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022902set_var(
22903 char_u *name,
22904 typval_T *tv,
22905 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906{
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022909 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022910
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022911 ht = find_var_ht(name, &varname);
22912 if (ht == NULL || *varname == NUL)
22913 {
22914 EMSG2(_(e_illvar), name);
22915 return;
22916 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022917 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022918
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022919 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22920 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022921
Bram Moolenaar33570922005-01-25 22:26:29 +000022922 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022923 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022924 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022925 if (var_check_ro(v->di_flags, name, FALSE)
22926 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 return;
22928 if (v->di_tv.v_type != tv->v_type
22929 && !((v->di_tv.v_type == VAR_STRING
22930 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022931 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022932 || tv->v_type == VAR_NUMBER))
22933#ifdef FEAT_FLOAT
22934 && !((v->di_tv.v_type == VAR_NUMBER
22935 || v->di_tv.v_type == VAR_FLOAT)
22936 && (tv->v_type == VAR_NUMBER
22937 || tv->v_type == VAR_FLOAT))
22938#endif
22939 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022940 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022941 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022942 return;
22943 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022944
22945 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022946 * Handle setting internal v: variables separately where needed to
22947 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 */
22949 if (ht == &vimvarht)
22950 {
22951 if (v->di_tv.v_type == VAR_STRING)
22952 {
22953 vim_free(v->di_tv.vval.v_string);
22954 if (copy || tv->v_type != VAR_STRING)
22955 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22956 else
22957 {
22958 /* Take over the string to avoid an extra alloc/free. */
22959 v->di_tv.vval.v_string = tv->vval.v_string;
22960 tv->vval.v_string = NULL;
22961 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022962 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022963 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022964 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022965 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022967 if (STRCMP(varname, "searchforward") == 0)
22968 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022969#ifdef FEAT_SEARCH_EXTRA
22970 else if (STRCMP(varname, "hlsearch") == 0)
22971 {
22972 no_hlsearch = !v->di_tv.vval.v_number;
22973 redraw_all_later(SOME_VALID);
22974 }
22975#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022976 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022977 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022978 else if (v->di_tv.v_type != tv->v_type)
22979 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022980 }
22981
22982 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022983 }
22984 else /* add a new variable */
22985 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022986 /* Can't add "v:" variable. */
22987 if (ht == &vimvarht)
22988 {
22989 EMSG2(_(e_illvar), name);
22990 return;
22991 }
22992
Bram Moolenaar92124a32005-06-17 22:03:40 +000022993 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022994 if (!valid_varname(varname))
22995 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022996
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022997 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22998 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022999 if (v == NULL)
23000 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000023001 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000023002 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 {
Bram Moolenaara7043832005-01-21 11:56:39 +000023004 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 return;
23006 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020023007 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023008 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023009
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023010 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000023011 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023012 else
23013 {
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023015 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023016 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000023017 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023018}
23019
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023020/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023021 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000023022 * Also give an error message.
23023 */
23024 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023025var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000023026{
23027 if (flags & DI_FLAGS_RO)
23028 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023029 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023030 return TRUE;
23031 }
23032 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
23033 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023034 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000023035 return TRUE;
23036 }
23037 return FALSE;
23038}
23039
23040/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023041 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
23042 * Also give an error message.
23043 */
23044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023045var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023046{
23047 if (flags & DI_FLAGS_FIX)
23048 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020023049 EMSG2(_("E795: Cannot delete variable %s"),
23050 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000023051 return TRUE;
23052 }
23053 return FALSE;
23054}
23055
23056/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023057 * Check if a funcref is assigned to a valid variable name.
23058 * Return TRUE and give an error if not.
23059 */
23060 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023061var_check_func_name(
23062 char_u *name, /* points to start of variable name */
23063 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023064{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023065 /* Allow for w: b: s: and t:. */
23066 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023067 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23068 ? name[2] : name[0]))
23069 {
23070 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23071 name);
23072 return TRUE;
23073 }
23074 /* Don't allow hiding a function. When "v" is not NULL we might be
23075 * assigning another function to the same var, the type is checked
23076 * below. */
23077 if (new_var && function_exists(name))
23078 {
23079 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23080 name);
23081 return TRUE;
23082 }
23083 return FALSE;
23084}
23085
23086/*
23087 * Check if a variable name is valid.
23088 * Return FALSE and give an error if not.
23089 */
23090 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023091valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023092{
23093 char_u *p;
23094
23095 for (p = varname; *p != NUL; ++p)
23096 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23097 && *p != AUTOLOAD_CHAR)
23098 {
23099 EMSG2(_(e_illvar), varname);
23100 return FALSE;
23101 }
23102 return TRUE;
23103}
23104
23105/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023106 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023107 * Also give an error message, using "name" or _("name") when use_gettext is
23108 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023109 */
23110 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023111tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023112{
23113 if (lock & VAR_LOCKED)
23114 {
23115 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023116 name == NULL ? (char_u *)_("Unknown")
23117 : use_gettext ? (char_u *)_(name)
23118 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023119 return TRUE;
23120 }
23121 if (lock & VAR_FIXED)
23122 {
23123 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023124 name == NULL ? (char_u *)_("Unknown")
23125 : use_gettext ? (char_u *)_(name)
23126 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023127 return TRUE;
23128 }
23129 return FALSE;
23130}
23131
23132/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023133 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023134 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023135 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023136 * It is OK for "from" and "to" to point to the same item. This is used to
23137 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023138 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023140copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023142 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023143 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023144 switch (from->v_type)
23145 {
23146 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023147 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023148 to->vval.v_number = from->vval.v_number;
23149 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023150 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023151#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023152 to->vval.v_float = from->vval.v_float;
23153 break;
23154#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023155 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023156#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023157 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023158 if (to->vval.v_job != NULL)
23159 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023160 break;
23161#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023162 case VAR_CHANNEL:
23163#ifdef FEAT_CHANNEL
23164 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023165 if (to->vval.v_channel != NULL)
23166 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023167 break;
23168#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023169 case VAR_STRING:
23170 case VAR_FUNC:
23171 if (from->vval.v_string == NULL)
23172 to->vval.v_string = NULL;
23173 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023174 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023175 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023176 if (from->v_type == VAR_FUNC)
23177 func_ref(to->vval.v_string);
23178 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023179 break;
23180 case VAR_LIST:
23181 if (from->vval.v_list == NULL)
23182 to->vval.v_list = NULL;
23183 else
23184 {
23185 to->vval.v_list = from->vval.v_list;
23186 ++to->vval.v_list->lv_refcount;
23187 }
23188 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023189 case VAR_DICT:
23190 if (from->vval.v_dict == NULL)
23191 to->vval.v_dict = NULL;
23192 else
23193 {
23194 to->vval.v_dict = from->vval.v_dict;
23195 ++to->vval.v_dict->dv_refcount;
23196 }
23197 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023198 case VAR_UNKNOWN:
23199 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023200 break;
23201 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202}
23203
23204/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023205 * Make a copy of an item.
23206 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023207 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23208 * reference to an already copied list/dict can be used.
23209 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023210 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023212item_copy(
23213 typval_T *from,
23214 typval_T *to,
23215 int deep,
23216 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023217{
23218 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023219 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023220
Bram Moolenaar33570922005-01-25 22:26:29 +000023221 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023222 {
23223 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023224 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023225 }
23226 ++recurse;
23227
23228 switch (from->v_type)
23229 {
23230 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023231 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023232 case VAR_STRING:
23233 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023234 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023235 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023236 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023237 copy_tv(from, to);
23238 break;
23239 case VAR_LIST:
23240 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023241 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023242 if (from->vval.v_list == NULL)
23243 to->vval.v_list = NULL;
23244 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23245 {
23246 /* use the copy made earlier */
23247 to->vval.v_list = from->vval.v_list->lv_copylist;
23248 ++to->vval.v_list->lv_refcount;
23249 }
23250 else
23251 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23252 if (to->vval.v_list == NULL)
23253 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023254 break;
23255 case VAR_DICT:
23256 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023257 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023258 if (from->vval.v_dict == NULL)
23259 to->vval.v_dict = NULL;
23260 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23261 {
23262 /* use the copy made earlier */
23263 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23264 ++to->vval.v_dict->dv_refcount;
23265 }
23266 else
23267 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23268 if (to->vval.v_dict == NULL)
23269 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023270 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023271 case VAR_UNKNOWN:
23272 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023273 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023274 }
23275 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023276 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023277}
23278
23279/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 * ":echo expr1 ..." print each argument separated with a space, add a
23281 * newline at the end.
23282 * ":echon expr1 ..." print each argument plain.
23283 */
23284 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023285ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286{
23287 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023288 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023289 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 char_u *p;
23291 int needclr = TRUE;
23292 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023293 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294
23295 if (eap->skip)
23296 ++emsg_skip;
23297 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23298 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023299 /* If eval1() causes an error message the text from the command may
23300 * still need to be cleared. E.g., "echo 22,44". */
23301 need_clr_eos = needclr;
23302
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023304 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 {
23306 /*
23307 * Report the invalid expression unless the expression evaluation
23308 * has been cancelled due to an aborting error, an interrupt, or an
23309 * exception.
23310 */
23311 if (!aborting())
23312 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023313 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023314 break;
23315 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023316 need_clr_eos = FALSE;
23317
Bram Moolenaar071d4272004-06-13 20:20:40 +000023318 if (!eap->skip)
23319 {
23320 if (atstart)
23321 {
23322 atstart = FALSE;
23323 /* Call msg_start() after eval1(), evaluating the expression
23324 * may cause a message to appear. */
23325 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023326 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023327 /* Mark the saved text as finishing the line, so that what
23328 * follows is displayed on a new line when scrolling back
23329 * at the more prompt. */
23330 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023331 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333 }
23334 else if (eap->cmdidx == CMD_echo)
23335 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023336 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023337 if (p != NULL)
23338 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023339 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023340 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023341 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023342 if (*p != TAB && needclr)
23343 {
23344 /* remove any text still there from the command */
23345 msg_clr_eos();
23346 needclr = FALSE;
23347 }
23348 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023349 }
23350 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023351 {
23352#ifdef FEAT_MBYTE
23353 if (has_mbyte)
23354 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023355 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023356
23357 (void)msg_outtrans_len_attr(p, i, echo_attr);
23358 p += i - 1;
23359 }
23360 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023362 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23363 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023365 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023366 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023367 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023368 arg = skipwhite(arg);
23369 }
23370 eap->nextcmd = check_nextcmd(arg);
23371
23372 if (eap->skip)
23373 --emsg_skip;
23374 else
23375 {
23376 /* remove text that may still be there from the command */
23377 if (needclr)
23378 msg_clr_eos();
23379 if (eap->cmdidx == CMD_echo)
23380 msg_end();
23381 }
23382}
23383
23384/*
23385 * ":echohl {name}".
23386 */
23387 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023388ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023389{
23390 int id;
23391
23392 id = syn_name2id(eap->arg);
23393 if (id == 0)
23394 echo_attr = 0;
23395 else
23396 echo_attr = syn_id2attr(id);
23397}
23398
23399/*
23400 * ":execute expr1 ..." execute the result of an expression.
23401 * ":echomsg expr1 ..." Print a message
23402 * ":echoerr expr1 ..." Print an error
23403 * Each gets spaces around each argument and a newline at the end for
23404 * echo commands
23405 */
23406 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023407ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408{
23409 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023410 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 int ret = OK;
23412 char_u *p;
23413 garray_T ga;
23414 int len;
23415 int save_did_emsg;
23416
23417 ga_init2(&ga, 1, 80);
23418
23419 if (eap->skip)
23420 ++emsg_skip;
23421 while (*arg != NUL && *arg != '|' && *arg != '\n')
23422 {
23423 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023424 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023425 {
23426 /*
23427 * Report the invalid expression unless the expression evaluation
23428 * has been cancelled due to an aborting error, an interrupt, or an
23429 * exception.
23430 */
23431 if (!aborting())
23432 EMSG2(_(e_invexpr2), p);
23433 ret = FAIL;
23434 break;
23435 }
23436
23437 if (!eap->skip)
23438 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023439 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440 len = (int)STRLEN(p);
23441 if (ga_grow(&ga, len + 2) == FAIL)
23442 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023443 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 ret = FAIL;
23445 break;
23446 }
23447 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023448 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023450 ga.ga_len += len;
23451 }
23452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023453 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023454 arg = skipwhite(arg);
23455 }
23456
23457 if (ret != FAIL && ga.ga_data != NULL)
23458 {
23459 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023460 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023461 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023462 out_flush();
23463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023464 else if (eap->cmdidx == CMD_echoerr)
23465 {
23466 /* We don't want to abort following commands, restore did_emsg. */
23467 save_did_emsg = did_emsg;
23468 EMSG((char_u *)ga.ga_data);
23469 if (!force_abort)
23470 did_emsg = save_did_emsg;
23471 }
23472 else if (eap->cmdidx == CMD_execute)
23473 do_cmdline((char_u *)ga.ga_data,
23474 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23475 }
23476
23477 ga_clear(&ga);
23478
23479 if (eap->skip)
23480 --emsg_skip;
23481
23482 eap->nextcmd = check_nextcmd(arg);
23483}
23484
23485/*
23486 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23487 * "arg" points to the "&" or '+' when called, to "option" when returning.
23488 * Returns NULL when no option name found. Otherwise pointer to the char
23489 * after the option name.
23490 */
23491 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023492find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023493{
23494 char_u *p = *arg;
23495
23496 ++p;
23497 if (*p == 'g' && p[1] == ':')
23498 {
23499 *opt_flags = OPT_GLOBAL;
23500 p += 2;
23501 }
23502 else if (*p == 'l' && p[1] == ':')
23503 {
23504 *opt_flags = OPT_LOCAL;
23505 p += 2;
23506 }
23507 else
23508 *opt_flags = 0;
23509
23510 if (!ASCII_ISALPHA(*p))
23511 return NULL;
23512 *arg = p;
23513
23514 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23515 p += 4; /* termcap option */
23516 else
23517 while (ASCII_ISALPHA(*p))
23518 ++p;
23519 return p;
23520}
23521
23522/*
23523 * ":function"
23524 */
23525 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023526ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527{
23528 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023529 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 int j;
23531 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023533 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 char_u *name = NULL;
23535 char_u *p;
23536 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023537 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023538 garray_T newargs;
23539 garray_T newlines;
23540 int varargs = FALSE;
23541 int mustend = FALSE;
23542 int flags = 0;
23543 ufunc_T *fp;
23544 int indent;
23545 int nesting;
23546 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023547 dictitem_T *v;
23548 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023549 static int func_nr = 0; /* number for nameless function */
23550 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023551 hashtab_T *ht;
23552 int todo;
23553 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023554 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023555
23556 /*
23557 * ":function" without argument: list functions.
23558 */
23559 if (ends_excmd(*eap->arg))
23560 {
23561 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023562 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023563 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023564 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023565 {
23566 if (!HASHITEM_EMPTY(hi))
23567 {
23568 --todo;
23569 fp = HI2UF(hi);
23570 if (!isdigit(*fp->uf_name))
23571 list_func_head(fp, FALSE);
23572 }
23573 }
23574 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023575 eap->nextcmd = check_nextcmd(eap->arg);
23576 return;
23577 }
23578
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023580 * ":function /pat": list functions matching pattern.
23581 */
23582 if (*eap->arg == '/')
23583 {
23584 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23585 if (!eap->skip)
23586 {
23587 regmatch_T regmatch;
23588
23589 c = *p;
23590 *p = NUL;
23591 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23592 *p = c;
23593 if (regmatch.regprog != NULL)
23594 {
23595 regmatch.rm_ic = p_ic;
23596
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023597 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023598 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23599 {
23600 if (!HASHITEM_EMPTY(hi))
23601 {
23602 --todo;
23603 fp = HI2UF(hi);
23604 if (!isdigit(*fp->uf_name)
23605 && vim_regexec(&regmatch, fp->uf_name, 0))
23606 list_func_head(fp, FALSE);
23607 }
23608 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023609 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023610 }
23611 }
23612 if (*p == '/')
23613 ++p;
23614 eap->nextcmd = check_nextcmd(p);
23615 return;
23616 }
23617
23618 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023619 * Get the function name. There are these situations:
23620 * func normal function name
23621 * "name" == func, "fudi.fd_dict" == NULL
23622 * dict.func new dictionary entry
23623 * "name" == NULL, "fudi.fd_dict" set,
23624 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23625 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023626 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023627 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23628 * dict.func existing dict entry that's not a Funcref
23629 * "name" == NULL, "fudi.fd_dict" set,
23630 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023631 * s:func script-local function name
23632 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023633 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023635 name = trans_function_name(&p, eap->skip, 0, &fudi);
23636 paren = (vim_strchr(p, '(') != NULL);
23637 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023638 {
23639 /*
23640 * Return on an invalid expression in braces, unless the expression
23641 * evaluation has been cancelled due to an aborting error, an
23642 * interrupt, or an exception.
23643 */
23644 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023645 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023646 if (!eap->skip && fudi.fd_newkey != NULL)
23647 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023648 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023649 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023651 else
23652 eap->skip = TRUE;
23653 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023654
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 /* An error in a function call during evaluation of an expression in magic
23656 * braces should not cause the function not to be defined. */
23657 saved_did_emsg = did_emsg;
23658 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659
23660 /*
23661 * ":function func" with only function name: list function.
23662 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023663 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023664 {
23665 if (!ends_excmd(*skipwhite(p)))
23666 {
23667 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 }
23670 eap->nextcmd = check_nextcmd(p);
23671 if (eap->nextcmd != NULL)
23672 *p = NUL;
23673 if (!eap->skip && !got_int)
23674 {
23675 fp = find_func(name);
23676 if (fp != NULL)
23677 {
23678 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023679 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023681 if (FUNCLINE(fp, j) == NULL)
23682 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023683 msg_putchar('\n');
23684 msg_outnum((long)(j + 1));
23685 if (j < 9)
23686 msg_putchar(' ');
23687 if (j < 99)
23688 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023689 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023690 out_flush(); /* show a line at a time */
23691 ui_breakcheck();
23692 }
23693 if (!got_int)
23694 {
23695 msg_putchar('\n');
23696 msg_puts((char_u *)" endfunction");
23697 }
23698 }
23699 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023700 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023702 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023703 }
23704
23705 /*
23706 * ":function name(arg1, arg2)" Define function.
23707 */
23708 p = skipwhite(p);
23709 if (*p != '(')
23710 {
23711 if (!eap->skip)
23712 {
23713 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023714 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715 }
23716 /* attempt to continue by skipping some text */
23717 if (vim_strchr(p, '(') != NULL)
23718 p = vim_strchr(p, '(');
23719 }
23720 p = skipwhite(p + 1);
23721
23722 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23723 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23724
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023725 if (!eap->skip)
23726 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023727 /* Check the name of the function. Unless it's a dictionary function
23728 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023729 if (name != NULL)
23730 arg = name;
23731 else
23732 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023733 if (arg != NULL && (fudi.fd_di == NULL
23734 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023735 {
23736 if (*arg == K_SPECIAL)
23737 j = 3;
23738 else
23739 j = 0;
23740 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23741 : eval_isnamec(arg[j])))
23742 ++j;
23743 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023744 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023745 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023746 /* Disallow using the g: dict. */
23747 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23748 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023749 }
23750
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 /*
23752 * Isolate the arguments: "arg1, arg2, ...)"
23753 */
23754 while (*p != ')')
23755 {
23756 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23757 {
23758 varargs = TRUE;
23759 p += 3;
23760 mustend = TRUE;
23761 }
23762 else
23763 {
23764 arg = p;
23765 while (ASCII_ISALNUM(*p) || *p == '_')
23766 ++p;
23767 if (arg == p || isdigit(*arg)
23768 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23769 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23770 {
23771 if (!eap->skip)
23772 EMSG2(_("E125: Illegal argument: %s"), arg);
23773 break;
23774 }
23775 if (ga_grow(&newargs, 1) == FAIL)
23776 goto erret;
23777 c = *p;
23778 *p = NUL;
23779 arg = vim_strsave(arg);
23780 if (arg == NULL)
23781 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023782
23783 /* Check for duplicate argument name. */
23784 for (i = 0; i < newargs.ga_len; ++i)
23785 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23786 {
23787 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023788 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023789 goto erret;
23790 }
23791
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23793 *p = c;
23794 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 if (*p == ',')
23796 ++p;
23797 else
23798 mustend = TRUE;
23799 }
23800 p = skipwhite(p);
23801 if (mustend && *p != ')')
23802 {
23803 if (!eap->skip)
23804 EMSG2(_(e_invarg2), eap->arg);
23805 break;
23806 }
23807 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023808 if (*p != ')')
23809 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023810 ++p; /* skip the ')' */
23811
Bram Moolenaare9a41262005-01-15 22:18:47 +000023812 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023813 for (;;)
23814 {
23815 p = skipwhite(p);
23816 if (STRNCMP(p, "range", 5) == 0)
23817 {
23818 flags |= FC_RANGE;
23819 p += 5;
23820 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023821 else if (STRNCMP(p, "dict", 4) == 0)
23822 {
23823 flags |= FC_DICT;
23824 p += 4;
23825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023826 else if (STRNCMP(p, "abort", 5) == 0)
23827 {
23828 flags |= FC_ABORT;
23829 p += 5;
23830 }
23831 else
23832 break;
23833 }
23834
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023835 /* When there is a line break use what follows for the function body.
23836 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23837 if (*p == '\n')
23838 line_arg = p + 1;
23839 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023840 EMSG(_(e_trailing));
23841
23842 /*
23843 * Read the body of the function, until ":endfunction" is found.
23844 */
23845 if (KeyTyped)
23846 {
23847 /* Check if the function already exists, don't let the user type the
23848 * whole function before telling him it doesn't work! For a script we
23849 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023850 if (!eap->skip && !eap->forceit)
23851 {
23852 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23853 EMSG(_(e_funcdict));
23854 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023855 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023857
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023858 if (!eap->skip && did_emsg)
23859 goto erret;
23860
Bram Moolenaar071d4272004-06-13 20:20:40 +000023861 msg_putchar('\n'); /* don't overwrite the function name */
23862 cmdline_row = msg_row;
23863 }
23864
23865 indent = 2;
23866 nesting = 0;
23867 for (;;)
23868 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023869 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023870 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023871 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023872 saved_wait_return = FALSE;
23873 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023874 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023875 sourcing_lnum_off = sourcing_lnum;
23876
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023877 if (line_arg != NULL)
23878 {
23879 /* Use eap->arg, split up in parts by line breaks. */
23880 theline = line_arg;
23881 p = vim_strchr(theline, '\n');
23882 if (p == NULL)
23883 line_arg += STRLEN(line_arg);
23884 else
23885 {
23886 *p = NUL;
23887 line_arg = p + 1;
23888 }
23889 }
23890 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 theline = getcmdline(':', 0L, indent);
23892 else
23893 theline = eap->getline(':', eap->cookie, indent);
23894 if (KeyTyped)
23895 lines_left = Rows - 1;
23896 if (theline == NULL)
23897 {
23898 EMSG(_("E126: Missing :endfunction"));
23899 goto erret;
23900 }
23901
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023902 /* Detect line continuation: sourcing_lnum increased more than one. */
23903 if (sourcing_lnum > sourcing_lnum_off + 1)
23904 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23905 else
23906 sourcing_lnum_off = 0;
23907
Bram Moolenaar071d4272004-06-13 20:20:40 +000023908 if (skip_until != NULL)
23909 {
23910 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23911 * don't check for ":endfunc". */
23912 if (STRCMP(theline, skip_until) == 0)
23913 {
23914 vim_free(skip_until);
23915 skip_until = NULL;
23916 }
23917 }
23918 else
23919 {
23920 /* skip ':' and blanks*/
23921 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23922 ;
23923
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023924 /* Check for "endfunction". */
23925 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023927 if (line_arg == NULL)
23928 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 break;
23930 }
23931
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023932 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 * at "end". */
23934 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23935 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023936 else if (STRNCMP(p, "if", 2) == 0
23937 || STRNCMP(p, "wh", 2) == 0
23938 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 || STRNCMP(p, "try", 3) == 0)
23940 indent += 2;
23941
23942 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023943 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023944 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023945 if (*p == '!')
23946 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023947 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023948 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23949 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023950 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023951 ++nesting;
23952 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023953 }
23954 }
23955
23956 /* Check for ":append" or ":insert". */
23957 p = skip_range(p, NULL);
23958 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23959 || (p[0] == 'i'
23960 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23961 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23962 skip_until = vim_strsave((char_u *)".");
23963
23964 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23965 arg = skipwhite(skiptowhite(p));
23966 if (arg[0] == '<' && arg[1] =='<'
23967 && ((p[0] == 'p' && p[1] == 'y'
23968 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23969 || (p[0] == 'p' && p[1] == 'e'
23970 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23971 || (p[0] == 't' && p[1] == 'c'
23972 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023973 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23974 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023975 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23976 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023977 || (p[0] == 'm' && p[1] == 'z'
23978 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023979 ))
23980 {
23981 /* ":python <<" continues until a dot, like ":append" */
23982 p = skipwhite(arg + 2);
23983 if (*p == NUL)
23984 skip_until = vim_strsave((char_u *)".");
23985 else
23986 skip_until = vim_strsave(p);
23987 }
23988 }
23989
23990 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023991 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023992 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023993 if (line_arg == NULL)
23994 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023995 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023996 }
23997
23998 /* Copy the line to newly allocated memory. get_one_sourceline()
23999 * allocates 250 bytes per line, this saves 80% on average. The cost
24000 * is an extra alloc/free. */
24001 p = vim_strsave(theline);
24002 if (p != NULL)
24003 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024004 if (line_arg == NULL)
24005 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024006 theline = p;
24007 }
24008
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024009 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
24010
24011 /* Add NULL lines for continuation lines, so that the line count is
24012 * equal to the index in the growarray. */
24013 while (sourcing_lnum_off-- > 0)
24014 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000024015
24016 /* Check for end of eap->arg. */
24017 if (line_arg != NULL && *line_arg == NUL)
24018 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024019 }
24020
24021 /* Don't define the function when skipping commands or when an error was
24022 * detected. */
24023 if (eap->skip || did_emsg)
24024 goto erret;
24025
24026 /*
24027 * If there are no errors, add the function
24028 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024029 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024030 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024031 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000024032 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024033 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024034 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024035 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024036 goto erret;
24037 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024038
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024039 fp = find_func(name);
24040 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024042 if (!eap->forceit)
24043 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000024044 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024045 goto erret;
24046 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024047 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024048 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000024049 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024050 name);
24051 goto erret;
24052 }
24053 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024054 ga_clear_strings(&(fp->uf_args));
24055 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024056 vim_free(name);
24057 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024059 }
24060 else
24061 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 char numbuf[20];
24063
24064 fp = NULL;
24065 if (fudi.fd_newkey == NULL && !eap->forceit)
24066 {
24067 EMSG(_(e_funcdict));
24068 goto erret;
24069 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024070 if (fudi.fd_di == NULL)
24071 {
24072 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024073 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024074 goto erret;
24075 }
24076 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024077 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024078 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024079
24080 /* Give the function a sequential number. Can only be used with a
24081 * Funcref! */
24082 vim_free(name);
24083 sprintf(numbuf, "%d", ++func_nr);
24084 name = vim_strsave((char_u *)numbuf);
24085 if (name == NULL)
24086 goto erret;
24087 }
24088
24089 if (fp == NULL)
24090 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024091 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024092 {
24093 int slen, plen;
24094 char_u *scriptname;
24095
24096 /* Check that the autoload name matches the script name. */
24097 j = FAIL;
24098 if (sourcing_name != NULL)
24099 {
24100 scriptname = autoload_name(name);
24101 if (scriptname != NULL)
24102 {
24103 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024104 plen = (int)STRLEN(p);
24105 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024106 if (slen > plen && fnamecmp(p,
24107 sourcing_name + slen - plen) == 0)
24108 j = OK;
24109 vim_free(scriptname);
24110 }
24111 }
24112 if (j == FAIL)
24113 {
24114 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24115 goto erret;
24116 }
24117 }
24118
24119 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120 if (fp == NULL)
24121 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024122
24123 if (fudi.fd_dict != NULL)
24124 {
24125 if (fudi.fd_di == NULL)
24126 {
24127 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024128 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024129 if (fudi.fd_di == NULL)
24130 {
24131 vim_free(fp);
24132 goto erret;
24133 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024134 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24135 {
24136 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024137 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024138 goto erret;
24139 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024140 }
24141 else
24142 /* overwrite existing dict entry */
24143 clear_tv(&fudi.fd_di->di_tv);
24144 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024145 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024146 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024147 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024148
24149 /* behave like "dict" was used */
24150 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024151 }
24152
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024154 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024155 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24156 {
24157 vim_free(fp);
24158 goto erret;
24159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024160 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024161 fp->uf_args = newargs;
24162 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024163#ifdef FEAT_PROFILE
24164 fp->uf_tml_count = NULL;
24165 fp->uf_tml_total = NULL;
24166 fp->uf_tml_self = NULL;
24167 fp->uf_profiling = FALSE;
24168 if (prof_def_func())
24169 func_do_profile(fp);
24170#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024171 fp->uf_varargs = varargs;
24172 fp->uf_flags = flags;
24173 fp->uf_calls = 0;
24174 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024175 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176
24177erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 ga_clear_strings(&newargs);
24179 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024180ret_free:
24181 vim_free(skip_until);
24182 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024183 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024185 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024186}
24187
24188/*
24189 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024190 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024191 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024192 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024193 * TFN_INT: internal function name OK
24194 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024195 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024196 * Advances "pp" to just after the function name (if no error).
24197 */
24198 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024199trans_function_name(
24200 char_u **pp,
24201 int skip, /* only find the end, don't evaluate */
24202 int flags,
24203 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024205 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024206 char_u *start;
24207 char_u *end;
24208 int lead;
24209 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024211 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024212
24213 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024214 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024215 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024216
24217 /* Check for hard coded <SNR>: already translated function ID (from a user
24218 * command). */
24219 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24220 && (*pp)[2] == (int)KE_SNR)
24221 {
24222 *pp += 3;
24223 len = get_id_len(pp) + 3;
24224 return vim_strnsave(start, len);
24225 }
24226
24227 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24228 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024229 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024230 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024232
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024233 /* Note that TFN_ flags use the same values as GLV_ flags. */
24234 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024235 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024236 if (end == start)
24237 {
24238 if (!skip)
24239 EMSG(_("E129: Function name required"));
24240 goto theend;
24241 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024242 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024243 {
24244 /*
24245 * Report an invalid expression in braces, unless the expression
24246 * evaluation has been cancelled due to an aborting error, an
24247 * interrupt, or an exception.
24248 */
24249 if (!aborting())
24250 {
24251 if (end != NULL)
24252 EMSG2(_(e_invarg2), start);
24253 }
24254 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024255 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024256 goto theend;
24257 }
24258
24259 if (lv.ll_tv != NULL)
24260 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024261 if (fdp != NULL)
24262 {
24263 fdp->fd_dict = lv.ll_dict;
24264 fdp->fd_newkey = lv.ll_newkey;
24265 lv.ll_newkey = NULL;
24266 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024267 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024268 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24269 {
24270 name = vim_strsave(lv.ll_tv->vval.v_string);
24271 *pp = end;
24272 }
24273 else
24274 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024275 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24276 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024277 EMSG(_(e_funcref));
24278 else
24279 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024280 name = NULL;
24281 }
24282 goto theend;
24283 }
24284
24285 if (lv.ll_name == NULL)
24286 {
24287 /* Error found, but continue after the function name. */
24288 *pp = end;
24289 goto theend;
24290 }
24291
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024292 /* Check if the name is a Funcref. If so, use the value. */
24293 if (lv.ll_exp_name != NULL)
24294 {
24295 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024296 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024297 if (name == lv.ll_exp_name)
24298 name = NULL;
24299 }
24300 else
24301 {
24302 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024303 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024304 if (name == *pp)
24305 name = NULL;
24306 }
24307 if (name != NULL)
24308 {
24309 name = vim_strsave(name);
24310 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024311 if (STRNCMP(name, "<SNR>", 5) == 0)
24312 {
24313 /* Change "<SNR>" to the byte sequence. */
24314 name[0] = K_SPECIAL;
24315 name[1] = KS_EXTRA;
24316 name[2] = (int)KE_SNR;
24317 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24318 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024319 goto theend;
24320 }
24321
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024322 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024323 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024324 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024325 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24326 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24327 {
24328 /* When there was "s:" already or the name expanded to get a
24329 * leading "s:" then remove it. */
24330 lv.ll_name += 2;
24331 len -= 2;
24332 lead = 2;
24333 }
24334 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024335 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024336 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024337 /* skip over "s:" and "g:" */
24338 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024339 lv.ll_name += 2;
24340 len = (int)(end - lv.ll_name);
24341 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024342
24343 /*
24344 * Copy the function name to allocated memory.
24345 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24346 * Accept <SNR>123_name() outside a script.
24347 */
24348 if (skip)
24349 lead = 0; /* do nothing */
24350 else if (lead > 0)
24351 {
24352 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024353 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24354 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024355 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024356 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024357 if (current_SID <= 0)
24358 {
24359 EMSG(_(e_usingsid));
24360 goto theend;
24361 }
24362 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24363 lead += (int)STRLEN(sid_buf);
24364 }
24365 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024366 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024367 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024368 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024369 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024370 goto theend;
24371 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024372 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024373 {
24374 char_u *cp = vim_strchr(lv.ll_name, ':');
24375
24376 if (cp != NULL && cp < end)
24377 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024378 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024379 goto theend;
24380 }
24381 }
24382
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024383 name = alloc((unsigned)(len + lead + 1));
24384 if (name != NULL)
24385 {
24386 if (lead > 0)
24387 {
24388 name[0] = K_SPECIAL;
24389 name[1] = KS_EXTRA;
24390 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024391 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024392 STRCPY(name + 3, sid_buf);
24393 }
24394 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024395 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024396 }
24397 *pp = end;
24398
24399theend:
24400 clear_lval(&lv);
24401 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024402}
24403
24404/*
24405 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24406 * Return 2 if "p" starts with "s:".
24407 * Return 0 otherwise.
24408 */
24409 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024410eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024411{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024412 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24413 * the standard library function. */
24414 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24415 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024416 return 5;
24417 if (p[0] == 's' && p[1] == ':')
24418 return 2;
24419 return 0;
24420}
24421
24422/*
24423 * Return TRUE if "p" starts with "<SID>" or "s:".
24424 * Only works if eval_fname_script() returned non-zero for "p"!
24425 */
24426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024427eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428{
24429 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24430}
24431
24432/*
24433 * List the head of the function: "name(arg1, arg2)".
24434 */
24435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024436list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024437{
24438 int j;
24439
24440 msg_start();
24441 if (indent)
24442 MSG_PUTS(" ");
24443 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024444 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024445 {
24446 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024447 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024448 }
24449 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024450 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024451 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024452 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024453 {
24454 if (j)
24455 MSG_PUTS(", ");
24456 msg_puts(FUNCARG(fp, j));
24457 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024458 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024459 {
24460 if (j)
24461 MSG_PUTS(", ");
24462 MSG_PUTS("...");
24463 }
24464 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024465 if (fp->uf_flags & FC_ABORT)
24466 MSG_PUTS(" abort");
24467 if (fp->uf_flags & FC_RANGE)
24468 MSG_PUTS(" range");
24469 if (fp->uf_flags & FC_DICT)
24470 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024471 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024472 if (p_verbose > 0)
24473 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024474}
24475
24476/*
24477 * Find a function by name, return pointer to it in ufuncs.
24478 * Return NULL for unknown function.
24479 */
24480 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024481find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024482{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024483 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024484
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024485 hi = hash_find(&func_hashtab, name);
24486 if (!HASHITEM_EMPTY(hi))
24487 return HI2UF(hi);
24488 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024489}
24490
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024491#if defined(EXITFREE) || defined(PROTO)
24492 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024493free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024494{
24495 hashitem_T *hi;
24496
24497 /* Need to start all over every time, because func_free() may change the
24498 * hash table. */
24499 while (func_hashtab.ht_used > 0)
24500 for (hi = func_hashtab.ht_array; ; ++hi)
24501 if (!HASHITEM_EMPTY(hi))
24502 {
24503 func_free(HI2UF(hi));
24504 break;
24505 }
24506}
24507#endif
24508
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024509 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024510translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024511{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024512 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024513 return find_internal_func(name) >= 0;
24514 return find_func(name) != NULL;
24515}
24516
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024517/*
24518 * Return TRUE if a function "name" exists.
24519 */
24520 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024521function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024522{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024523 char_u *nm = name;
24524 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024525 int n = FALSE;
24526
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024527 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24528 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024529 nm = skipwhite(nm);
24530
24531 /* Only accept "funcname", "funcname ", "funcname (..." and
24532 * "funcname(...", not "funcname!...". */
24533 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024534 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024535 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024536 return n;
24537}
24538
Bram Moolenaara1544c02013-05-30 12:35:52 +020024539 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024540get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024541{
24542 char_u *nm = name;
24543 char_u *p;
24544
24545 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24546
24547 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024548 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024549 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024550
Bram Moolenaara1544c02013-05-30 12:35:52 +020024551 vim_free(p);
24552 return NULL;
24553}
24554
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024555/*
24556 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024557 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24558 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024559 */
24560 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024561builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024562{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024563 char_u *p;
24564
24565 if (!ASCII_ISLOWER(name[0]))
24566 return FALSE;
24567 p = vim_strchr(name, AUTOLOAD_CHAR);
24568 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024569}
24570
Bram Moolenaar05159a02005-02-26 23:04:13 +000024571#if defined(FEAT_PROFILE) || defined(PROTO)
24572/*
24573 * Start profiling function "fp".
24574 */
24575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024576func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024577{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024578 int len = fp->uf_lines.ga_len;
24579
24580 if (len == 0)
24581 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024582 fp->uf_tm_count = 0;
24583 profile_zero(&fp->uf_tm_self);
24584 profile_zero(&fp->uf_tm_total);
24585 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024586 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024587 if (fp->uf_tml_total == NULL)
24588 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024589 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024590 if (fp->uf_tml_self == NULL)
24591 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024592 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024593 fp->uf_tml_idx = -1;
24594 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24595 || fp->uf_tml_self == NULL)
24596 return; /* out of memory */
24597
24598 fp->uf_profiling = TRUE;
24599}
24600
24601/*
24602 * Dump the profiling results for all functions in file "fd".
24603 */
24604 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024605func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024606{
24607 hashitem_T *hi;
24608 int todo;
24609 ufunc_T *fp;
24610 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024611 ufunc_T **sorttab;
24612 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024613
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024614 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024615 if (todo == 0)
24616 return; /* nothing to dump */
24617
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024618 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024619
Bram Moolenaar05159a02005-02-26 23:04:13 +000024620 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24621 {
24622 if (!HASHITEM_EMPTY(hi))
24623 {
24624 --todo;
24625 fp = HI2UF(hi);
24626 if (fp->uf_profiling)
24627 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024628 if (sorttab != NULL)
24629 sorttab[st_len++] = fp;
24630
Bram Moolenaar05159a02005-02-26 23:04:13 +000024631 if (fp->uf_name[0] == K_SPECIAL)
24632 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24633 else
24634 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24635 if (fp->uf_tm_count == 1)
24636 fprintf(fd, "Called 1 time\n");
24637 else
24638 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24639 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24640 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24641 fprintf(fd, "\n");
24642 fprintf(fd, "count total (s) self (s)\n");
24643
24644 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24645 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024646 if (FUNCLINE(fp, i) == NULL)
24647 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024648 prof_func_line(fd, fp->uf_tml_count[i],
24649 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024650 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24651 }
24652 fprintf(fd, "\n");
24653 }
24654 }
24655 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024656
24657 if (sorttab != NULL && st_len > 0)
24658 {
24659 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24660 prof_total_cmp);
24661 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24662 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24663 prof_self_cmp);
24664 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24665 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024666
24667 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024668}
Bram Moolenaar73830342005-02-28 22:48:19 +000024669
24670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024671prof_sort_list(
24672 FILE *fd,
24673 ufunc_T **sorttab,
24674 int st_len,
24675 char *title,
24676 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024677{
24678 int i;
24679 ufunc_T *fp;
24680
24681 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24682 fprintf(fd, "count total (s) self (s) function\n");
24683 for (i = 0; i < 20 && i < st_len; ++i)
24684 {
24685 fp = sorttab[i];
24686 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24687 prefer_self);
24688 if (fp->uf_name[0] == K_SPECIAL)
24689 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24690 else
24691 fprintf(fd, " %s()\n", fp->uf_name);
24692 }
24693 fprintf(fd, "\n");
24694}
24695
24696/*
24697 * Print the count and times for one function or function line.
24698 */
24699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024700prof_func_line(
24701 FILE *fd,
24702 int count,
24703 proftime_T *total,
24704 proftime_T *self,
24705 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024706{
24707 if (count > 0)
24708 {
24709 fprintf(fd, "%5d ", count);
24710 if (prefer_self && profile_equal(total, self))
24711 fprintf(fd, " ");
24712 else
24713 fprintf(fd, "%s ", profile_msg(total));
24714 if (!prefer_self && profile_equal(total, self))
24715 fprintf(fd, " ");
24716 else
24717 fprintf(fd, "%s ", profile_msg(self));
24718 }
24719 else
24720 fprintf(fd, " ");
24721}
24722
24723/*
24724 * Compare function for total time sorting.
24725 */
24726 static int
24727#ifdef __BORLANDC__
24728_RTLENTRYF
24729#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024730prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024731{
24732 ufunc_T *p1, *p2;
24733
24734 p1 = *(ufunc_T **)s1;
24735 p2 = *(ufunc_T **)s2;
24736 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24737}
24738
24739/*
24740 * Compare function for self time sorting.
24741 */
24742 static int
24743#ifdef __BORLANDC__
24744_RTLENTRYF
24745#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024746prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024747{
24748 ufunc_T *p1, *p2;
24749
24750 p1 = *(ufunc_T **)s1;
24751 p2 = *(ufunc_T **)s2;
24752 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24753}
24754
Bram Moolenaar05159a02005-02-26 23:04:13 +000024755#endif
24756
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024757/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024758 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024759 * Return TRUE if a package was loaded.
24760 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024762script_autoload(
24763 char_u *name,
24764 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024765{
24766 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024767 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024768 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024769 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024770
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024771 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024772 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024773 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024774 return FALSE;
24775
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024776 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024777
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024778 /* Find the name in the list of previously loaded package names. Skip
24779 * "autoload/", it's always the same. */
24780 for (i = 0; i < ga_loaded.ga_len; ++i)
24781 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24782 break;
24783 if (!reload && i < ga_loaded.ga_len)
24784 ret = FALSE; /* was loaded already */
24785 else
24786 {
24787 /* Remember the name if it wasn't loaded already. */
24788 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24789 {
24790 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24791 tofree = NULL;
24792 }
24793
24794 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024795 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024796 ret = TRUE;
24797 }
24798
24799 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024800 return ret;
24801}
24802
24803/*
24804 * Return the autoload script name for a function or variable name.
24805 * Returns NULL when out of memory.
24806 */
24807 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024808autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809{
24810 char_u *p;
24811 char_u *scriptname;
24812
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024813 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024814 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24815 if (scriptname == NULL)
24816 return FALSE;
24817 STRCPY(scriptname, "autoload/");
24818 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024819 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024820 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024821 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024822 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024823 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024824}
24825
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24827
24828/*
24829 * Function given to ExpandGeneric() to obtain the list of user defined
24830 * function names.
24831 */
24832 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024833get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024835 static long_u done;
24836 static hashitem_T *hi;
24837 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838
24839 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024841 done = 0;
24842 hi = func_hashtab.ht_array;
24843 }
24844 if (done < func_hashtab.ht_used)
24845 {
24846 if (done++ > 0)
24847 ++hi;
24848 while (HASHITEM_EMPTY(hi))
24849 ++hi;
24850 fp = HI2UF(hi);
24851
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024852 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024853 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024854
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024855 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24856 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857
24858 cat_func_name(IObuff, fp);
24859 if (xp->xp_context != EXPAND_USER_FUNC)
24860 {
24861 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024862 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024863 STRCAT(IObuff, ")");
24864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024865 return IObuff;
24866 }
24867 return NULL;
24868}
24869
24870#endif /* FEAT_CMDL_COMPL */
24871
24872/*
24873 * Copy the function name of "fp" to buffer "buf".
24874 * "buf" must be able to hold the function name plus three bytes.
24875 * Takes care of script-local function names.
24876 */
24877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024878cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024879{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024880 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 {
24882 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024883 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 }
24885 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024886 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024887}
24888
24889/*
24890 * ":delfunction {name}"
24891 */
24892 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024893ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024894{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024895 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 char_u *p;
24897 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024898 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024899
24900 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024901 name = trans_function_name(&p, eap->skip, 0, &fudi);
24902 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024904 {
24905 if (fudi.fd_dict != NULL && !eap->skip)
24906 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024907 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 if (!ends_excmd(*skipwhite(p)))
24910 {
24911 vim_free(name);
24912 EMSG(_(e_trailing));
24913 return;
24914 }
24915 eap->nextcmd = check_nextcmd(p);
24916 if (eap->nextcmd != NULL)
24917 *p = NUL;
24918
24919 if (!eap->skip)
24920 fp = find_func(name);
24921 vim_free(name);
24922
24923 if (!eap->skip)
24924 {
24925 if (fp == NULL)
24926 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024927 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024928 return;
24929 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024930 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 {
24932 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24933 return;
24934 }
24935
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024936 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024938 /* Delete the dict item that refers to the function, it will
24939 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024940 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024941 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024942 else
24943 func_free(fp);
24944 }
24945}
24946
24947/*
24948 * Free a function and remove it from the list of functions.
24949 */
24950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024951func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024952{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024953 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024954
24955 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024956 ga_clear_strings(&(fp->uf_args));
24957 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024958#ifdef FEAT_PROFILE
24959 vim_free(fp->uf_tml_count);
24960 vim_free(fp->uf_tml_total);
24961 vim_free(fp->uf_tml_self);
24962#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024963
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024964 /* remove the function from the function hashtable */
24965 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24966 if (HASHITEM_EMPTY(hi))
24967 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024968 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024969 hash_remove(&func_hashtab, hi);
24970
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024971 vim_free(fp);
24972}
24973
24974/*
24975 * Unreference a Function: decrement the reference count and free it when it
24976 * becomes zero. Only for numbered functions.
24977 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024978 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024979func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024980{
24981 ufunc_T *fp;
24982
24983 if (name != NULL && isdigit(*name))
24984 {
24985 fp = find_func(name);
24986 if (fp == NULL)
24987 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024988 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024989 {
24990 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024991 * when "uf_calls" becomes zero. */
24992 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024993 func_free(fp);
24994 }
24995 }
24996}
24997
24998/*
24999 * Count a reference to a Function.
25000 */
Bram Moolenaardb913952012-06-29 12:54:53 +020025001 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025002func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000025003{
25004 ufunc_T *fp;
25005
25006 if (name != NULL && isdigit(*name))
25007 {
25008 fp = find_func(name);
25009 if (fp == NULL)
25010 EMSG2(_(e_intern2), "func_ref()");
25011 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025012 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025013 }
25014}
25015
25016/*
25017 * Call a user function.
25018 */
25019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025020call_user_func(
25021 ufunc_T *fp, /* pointer to function */
25022 int argcount, /* nr of args */
25023 typval_T *argvars, /* arguments */
25024 typval_T *rettv, /* return value */
25025 linenr_T firstline, /* first line of range */
25026 linenr_T lastline, /* last line of range */
25027 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025028{
Bram Moolenaar33570922005-01-25 22:26:29 +000025029 char_u *save_sourcing_name;
25030 linenr_T save_sourcing_lnum;
25031 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025032 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000025033 int save_did_emsg;
25034 static int depth = 0;
25035 dictitem_T *v;
25036 int fixvar_idx = 0; /* index in fixvar[] */
25037 int i;
25038 int ai;
25039 char_u numbuf[NUMBUFLEN];
25040 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025041 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025042#ifdef FEAT_PROFILE
25043 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025044 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025045#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025046
25047 /* If depth of calling is getting too high, don't execute the function */
25048 if (depth >= p_mfd)
25049 {
25050 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025051 rettv->v_type = VAR_NUMBER;
25052 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025053 return;
25054 }
25055 ++depth;
25056
25057 line_breakcheck(); /* check for CTRL-C hit */
25058
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025059 fc = (funccall_T *)alloc(sizeof(funccall_T));
25060 fc->caller = current_funccal;
25061 current_funccal = fc;
25062 fc->func = fp;
25063 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025064 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025065 fc->linenr = 0;
25066 fc->returned = FALSE;
25067 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025068 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025069 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25070 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025071
Bram Moolenaar33570922005-01-25 22:26:29 +000025072 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025073 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025074 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25075 * each argument variable and saves a lot of time.
25076 */
25077 /*
25078 * Init l: variables.
25079 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025080 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025081 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025082 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025083 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25084 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025085 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025086 name = v->di_key;
25087 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025088 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025089 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025090 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025091 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025092 v->di_tv.vval.v_dict = selfdict;
25093 ++selfdict->dv_refcount;
25094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025095
Bram Moolenaar33570922005-01-25 22:26:29 +000025096 /*
25097 * Init a: variables.
25098 * Set a:0 to "argcount".
25099 * Set a:000 to a list with room for the "..." arguments.
25100 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025101 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025102 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025103 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025104 /* Use "name" to avoid a warning from some compiler that checks the
25105 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025106 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025107 name = v->di_key;
25108 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025109 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025110 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025111 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025112 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025113 v->di_tv.vval.v_list = &fc->l_varlist;
25114 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25115 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25116 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025117
25118 /*
25119 * Set a:firstline to "firstline" and a:lastline to "lastline".
25120 * Set a:name to named arguments.
25121 * Set a:N to the "..." arguments.
25122 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025123 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025124 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025125 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025126 (varnumber_T)lastline);
25127 for (i = 0; i < argcount; ++i)
25128 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025129 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025130 if (ai < 0)
25131 /* named argument a:name */
25132 name = FUNCARG(fp, i);
25133 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025134 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025135 /* "..." argument a:1, a:2, etc. */
25136 sprintf((char *)numbuf, "%d", ai + 1);
25137 name = numbuf;
25138 }
25139 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25140 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025141 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025142 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25143 }
25144 else
25145 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025146 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25147 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025148 if (v == NULL)
25149 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025150 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025151 }
25152 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025153 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025154
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025155 /* Note: the values are copied directly to avoid alloc/free.
25156 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025157 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025158 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025159
25160 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25161 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025162 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25163 fc->l_listitems[ai].li_tv = argvars[i];
25164 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025165 }
25166 }
25167
Bram Moolenaar071d4272004-06-13 20:20:40 +000025168 /* Don't redraw while executing the function. */
25169 ++RedrawingDisabled;
25170 save_sourcing_name = sourcing_name;
25171 save_sourcing_lnum = sourcing_lnum;
25172 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025173 /* need space for function name + ("function " + 3) or "[number]" */
25174 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25175 + STRLEN(fp->uf_name) + 20;
25176 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 if (sourcing_name != NULL)
25178 {
25179 if (save_sourcing_name != NULL
25180 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025181 sprintf((char *)sourcing_name, "%s[%d]..",
25182 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 else
25184 STRCPY(sourcing_name, "function ");
25185 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25186
25187 if (p_verbose >= 12)
25188 {
25189 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025190 verbose_enter_scroll();
25191
Bram Moolenaar555b2802005-05-19 21:08:39 +000025192 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 if (p_verbose >= 14)
25194 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025196 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025197 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025198 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025199
25200 msg_puts((char_u *)"(");
25201 for (i = 0; i < argcount; ++i)
25202 {
25203 if (i > 0)
25204 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025205 if (argvars[i].v_type == VAR_NUMBER)
25206 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 else
25208 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025209 /* Do not want errors such as E724 here. */
25210 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025211 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025212 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025213 if (s != NULL)
25214 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025215 if (vim_strsize(s) > MSG_BUF_CLEN)
25216 {
25217 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25218 s = buf;
25219 }
25220 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025221 vim_free(tofree);
25222 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025223 }
25224 }
25225 msg_puts((char_u *)")");
25226 }
25227 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025228
25229 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 --no_wait_return;
25231 }
25232 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025233#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025234 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025235 {
25236 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25237 func_do_profile(fp);
25238 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025239 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025240 {
25241 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025242 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025243 profile_zero(&fp->uf_tm_children);
25244 }
25245 script_prof_save(&wait_start);
25246 }
25247#endif
25248
Bram Moolenaar071d4272004-06-13 20:20:40 +000025249 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025250 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 save_did_emsg = did_emsg;
25252 did_emsg = FALSE;
25253
25254 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025255 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25257
25258 --RedrawingDisabled;
25259
25260 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025261 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025263 clear_tv(rettv);
25264 rettv->v_type = VAR_NUMBER;
25265 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 }
25267
Bram Moolenaar05159a02005-02-26 23:04:13 +000025268#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025269 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025270 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025271 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025272 profile_end(&call_start);
25273 profile_sub_wait(&wait_start, &call_start);
25274 profile_add(&fp->uf_tm_total, &call_start);
25275 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025276 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025277 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025278 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25279 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025280 }
25281 }
25282#endif
25283
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 /* when being verbose, mention the return value */
25285 if (p_verbose >= 12)
25286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025288 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025289
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025291 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025292 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025293 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025294 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025295 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025297 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025298 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025299 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025300 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025301
Bram Moolenaar555b2802005-05-19 21:08:39 +000025302 /* The value may be very long. Skip the middle part, so that we
25303 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025304 * truncate it at the end. Don't want errors such as E724 here. */
25305 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025306 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025307 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025308 if (s != NULL)
25309 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025310 if (vim_strsize(s) > MSG_BUF_CLEN)
25311 {
25312 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25313 s = buf;
25314 }
25315 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025316 vim_free(tofree);
25317 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 }
25319 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025320
25321 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 --no_wait_return;
25323 }
25324
25325 vim_free(sourcing_name);
25326 sourcing_name = save_sourcing_name;
25327 sourcing_lnum = save_sourcing_lnum;
25328 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025329#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025330 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025331 script_prof_restore(&wait_start);
25332#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025333
25334 if (p_verbose >= 12 && sourcing_name != NULL)
25335 {
25336 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025337 verbose_enter_scroll();
25338
Bram Moolenaar555b2802005-05-19 21:08:39 +000025339 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025340 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025341
25342 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 --no_wait_return;
25344 }
25345
25346 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025347 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025349
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025350 /* If the a:000 list and the l: and a: dicts are not referenced we can
25351 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025352 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25353 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25354 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25355 {
25356 free_funccal(fc, FALSE);
25357 }
25358 else
25359 {
25360 hashitem_T *hi;
25361 listitem_T *li;
25362 int todo;
25363
25364 /* "fc" is still in use. This can happen when returning "a:000" or
25365 * assigning "l:" to a global variable.
25366 * Link "fc" in the list for garbage collection later. */
25367 fc->caller = previous_funccal;
25368 previous_funccal = fc;
25369
25370 /* Make a copy of the a: variables, since we didn't do that above. */
25371 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25372 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25373 {
25374 if (!HASHITEM_EMPTY(hi))
25375 {
25376 --todo;
25377 v = HI2DI(hi);
25378 copy_tv(&v->di_tv, &v->di_tv);
25379 }
25380 }
25381
25382 /* Make a copy of the a:000 items, since we didn't do that above. */
25383 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25384 copy_tv(&li->li_tv, &li->li_tv);
25385 }
25386}
25387
25388/*
25389 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025390 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025391 */
25392 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025393can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025394{
25395 return (fc->l_varlist.lv_copyID != copyID
25396 && fc->l_vars.dv_copyID != copyID
25397 && fc->l_avars.dv_copyID != copyID);
25398}
25399
25400/*
25401 * Free "fc" and what it contains.
25402 */
25403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025404free_funccal(
25405 funccall_T *fc,
25406 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025407{
25408 listitem_T *li;
25409
25410 /* The a: variables typevals may not have been allocated, only free the
25411 * allocated variables. */
25412 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25413
25414 /* free all l: variables */
25415 vars_clear(&fc->l_vars.dv_hashtab);
25416
25417 /* Free the a:000 variables if they were allocated. */
25418 if (free_val)
25419 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25420 clear_tv(&li->li_tv);
25421
25422 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423}
25424
25425/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025426 * Add a number variable "name" to dict "dp" with value "nr".
25427 */
25428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025429add_nr_var(
25430 dict_T *dp,
25431 dictitem_T *v,
25432 char *name,
25433 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025434{
25435 STRCPY(v->di_key, name);
25436 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25437 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25438 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025439 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025440 v->di_tv.vval.v_number = nr;
25441}
25442
25443/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 * ":return [expr]"
25445 */
25446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025447ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448{
25449 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025450 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025451 int returning = FALSE;
25452
25453 if (current_funccal == NULL)
25454 {
25455 EMSG(_("E133: :return not inside a function"));
25456 return;
25457 }
25458
25459 if (eap->skip)
25460 ++emsg_skip;
25461
25462 eap->nextcmd = NULL;
25463 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025464 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465 {
25466 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025467 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025468 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025469 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470 }
25471 /* It's safer to return also on error. */
25472 else if (!eap->skip)
25473 {
25474 /*
25475 * Return unless the expression evaluation has been cancelled due to an
25476 * aborting error, an interrupt, or an exception.
25477 */
25478 if (!aborting())
25479 returning = do_return(eap, FALSE, TRUE, NULL);
25480 }
25481
25482 /* When skipping or the return gets pending, advance to the next command
25483 * in this line (!returning). Otherwise, ignore the rest of the line.
25484 * Following lines will be ignored by get_func_line(). */
25485 if (returning)
25486 eap->nextcmd = NULL;
25487 else if (eap->nextcmd == NULL) /* no argument */
25488 eap->nextcmd = check_nextcmd(arg);
25489
25490 if (eap->skip)
25491 --emsg_skip;
25492}
25493
25494/*
25495 * Return from a function. Possibly makes the return pending. Also called
25496 * for a pending return at the ":endtry" or after returning from an extra
25497 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025498 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025499 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025500 * FALSE when the return gets pending.
25501 */
25502 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025503do_return(
25504 exarg_T *eap,
25505 int reanimate,
25506 int is_cmd,
25507 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025508{
25509 int idx;
25510 struct condstack *cstack = eap->cstack;
25511
25512 if (reanimate)
25513 /* Undo the return. */
25514 current_funccal->returned = FALSE;
25515
25516 /*
25517 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25518 * not in its finally clause (which then is to be executed next) is found.
25519 * In this case, make the ":return" pending for execution at the ":endtry".
25520 * Otherwise, return normally.
25521 */
25522 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25523 if (idx >= 0)
25524 {
25525 cstack->cs_pending[idx] = CSTP_RETURN;
25526
25527 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025528 /* A pending return again gets pending. "rettv" points to an
25529 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025531 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532 else
25533 {
25534 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025535 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025537 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025539 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 {
25541 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025542 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025543 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025544 else
25545 EMSG(_(e_outofmem));
25546 }
25547 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025548 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549
25550 if (reanimate)
25551 {
25552 /* The pending return value could be overwritten by a ":return"
25553 * without argument in a finally clause; reset the default
25554 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025555 current_funccal->rettv->v_type = VAR_NUMBER;
25556 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025557 }
25558 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025559 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025560 }
25561 else
25562 {
25563 current_funccal->returned = TRUE;
25564
25565 /* If the return is carried out now, store the return value. For
25566 * a return immediately after reanimation, the value is already
25567 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025568 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025569 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025570 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025571 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025573 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574 }
25575 }
25576
25577 return idx < 0;
25578}
25579
25580/*
25581 * Free the variable with a pending return value.
25582 */
25583 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025584discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025585{
Bram Moolenaar33570922005-01-25 22:26:29 +000025586 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587}
25588
25589/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025590 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 * is an allocated string. Used by report_pending() for verbose messages.
25592 */
25593 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025594get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025595{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025596 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025597 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025599
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025600 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025601 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025602 if (s == NULL)
25603 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025604
25605 STRCPY(IObuff, ":return ");
25606 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25607 if (STRLEN(s) + 8 >= IOSIZE)
25608 STRCPY(IObuff + IOSIZE - 4, "...");
25609 vim_free(tofree);
25610 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025611}
25612
25613/*
25614 * Get next function line.
25615 * Called by do_cmdline() to get the next line.
25616 * Returns allocated string, or NULL for end of function.
25617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025619get_func_line(
25620 int c UNUSED,
25621 void *cookie,
25622 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025623{
Bram Moolenaar33570922005-01-25 22:26:29 +000025624 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025625 ufunc_T *fp = fcp->func;
25626 char_u *retval;
25627 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628
25629 /* If breakpoints have been added/deleted need to check for it. */
25630 if (fcp->dbg_tick != debug_tick)
25631 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025632 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025633 sourcing_lnum);
25634 fcp->dbg_tick = debug_tick;
25635 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025636#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025637 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025638 func_line_end(cookie);
25639#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025640
Bram Moolenaar05159a02005-02-26 23:04:13 +000025641 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025642 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25643 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025644 retval = NULL;
25645 else
25646 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025647 /* Skip NULL lines (continuation lines). */
25648 while (fcp->linenr < gap->ga_len
25649 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25650 ++fcp->linenr;
25651 if (fcp->linenr >= gap->ga_len)
25652 retval = NULL;
25653 else
25654 {
25655 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25656 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025657#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025658 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025659 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025660#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662 }
25663
25664 /* Did we encounter a breakpoint? */
25665 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25666 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025667 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025669 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670 sourcing_lnum);
25671 fcp->dbg_tick = debug_tick;
25672 }
25673
25674 return retval;
25675}
25676
Bram Moolenaar05159a02005-02-26 23:04:13 +000025677#if defined(FEAT_PROFILE) || defined(PROTO)
25678/*
25679 * Called when starting to read a function line.
25680 * "sourcing_lnum" must be correct!
25681 * When skipping lines it may not actually be executed, but we won't find out
25682 * until later and we need to store the time now.
25683 */
25684 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025685func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025686{
25687 funccall_T *fcp = (funccall_T *)cookie;
25688 ufunc_T *fp = fcp->func;
25689
25690 if (fp->uf_profiling && sourcing_lnum >= 1
25691 && sourcing_lnum <= fp->uf_lines.ga_len)
25692 {
25693 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025694 /* Skip continuation lines. */
25695 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25696 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025697 fp->uf_tml_execed = FALSE;
25698 profile_start(&fp->uf_tml_start);
25699 profile_zero(&fp->uf_tml_children);
25700 profile_get_wait(&fp->uf_tml_wait);
25701 }
25702}
25703
25704/*
25705 * Called when actually executing a function line.
25706 */
25707 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025708func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025709{
25710 funccall_T *fcp = (funccall_T *)cookie;
25711 ufunc_T *fp = fcp->func;
25712
25713 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25714 fp->uf_tml_execed = TRUE;
25715}
25716
25717/*
25718 * Called when done with a function line.
25719 */
25720 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025721func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025722{
25723 funccall_T *fcp = (funccall_T *)cookie;
25724 ufunc_T *fp = fcp->func;
25725
25726 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25727 {
25728 if (fp->uf_tml_execed)
25729 {
25730 ++fp->uf_tml_count[fp->uf_tml_idx];
25731 profile_end(&fp->uf_tml_start);
25732 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025733 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025734 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25735 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025736 }
25737 fp->uf_tml_idx = -1;
25738 }
25739}
25740#endif
25741
Bram Moolenaar071d4272004-06-13 20:20:40 +000025742/*
25743 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025744 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025745 */
25746 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025747func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025748{
Bram Moolenaar33570922005-01-25 22:26:29 +000025749 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750
25751 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25752 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025753 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025754 || fcp->returned);
25755}
25756
25757/*
25758 * return TRUE if cookie indicates a function which "abort"s on errors.
25759 */
25760 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025761func_has_abort(
25762 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025764 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025765}
25766
25767#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25768typedef enum
25769{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025770 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25771 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25772 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773} var_flavour_T;
25774
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025775static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776
25777 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025778var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779{
25780 char_u *p = varname;
25781
25782 if (ASCII_ISUPPER(*p))
25783 {
25784 while (*(++p))
25785 if (ASCII_ISLOWER(*p))
25786 return VAR_FLAVOUR_SESSION;
25787 return VAR_FLAVOUR_VIMINFO;
25788 }
25789 else
25790 return VAR_FLAVOUR_DEFAULT;
25791}
25792#endif
25793
25794#if defined(FEAT_VIMINFO) || defined(PROTO)
25795/*
25796 * Restore global vars that start with a capital from the viminfo file
25797 */
25798 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025799read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025800{
25801 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025802 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025803 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025804 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025805
25806 if (!writing && (find_viminfo_parameter('!') != NULL))
25807 {
25808 tab = vim_strchr(virp->vir_line + 1, '\t');
25809 if (tab != NULL)
25810 {
25811 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025812 switch (*tab)
25813 {
25814 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025815#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025816 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025817#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025818 case 'D': type = VAR_DICT; break;
25819 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025820 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025822
25823 tab = vim_strchr(tab, '\t');
25824 if (tab != NULL)
25825 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025826 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025827 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025828 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025830#ifdef FEAT_FLOAT
25831 else if (type == VAR_FLOAT)
25832 (void)string2float(tab + 1, &tv.vval.v_float);
25833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025835 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025836 if (type == VAR_DICT || type == VAR_LIST)
25837 {
25838 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25839
25840 if (etv == NULL)
25841 /* Failed to parse back the dict or list, use it as a
25842 * string. */
25843 tv.v_type = VAR_STRING;
25844 else
25845 {
25846 vim_free(tv.vval.v_string);
25847 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025848 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025849 }
25850 }
25851
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025852 /* when in a function use global variables */
25853 save_funccal = current_funccal;
25854 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025855 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025856 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025857
25858 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025859 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025860 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25861 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 }
25863 }
25864 }
25865
25866 return viminfo_readline(virp);
25867}
25868
25869/*
25870 * Write global vars that start with a capital to the viminfo file
25871 */
25872 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025873write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874{
Bram Moolenaar33570922005-01-25 22:26:29 +000025875 hashitem_T *hi;
25876 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025877 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025878 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025879 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025880 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025881 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025882
25883 if (find_viminfo_parameter('!') == NULL)
25884 return;
25885
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025886 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025887
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025888 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025890 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025891 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025892 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025893 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025894 this_var = HI2DI(hi);
25895 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025896 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025897 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025898 {
25899 case VAR_STRING: s = "STR"; break;
25900 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025901 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025902 case VAR_DICT: s = "DIC"; break;
25903 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025904 case VAR_SPECIAL: s = "XPL"; break;
25905
25906 case VAR_UNKNOWN:
25907 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025908 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025909 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025910 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025911 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025912 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025913 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025914 if (p != NULL)
25915 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025916 vim_free(tofree);
25917 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025918 }
25919 }
25920}
25921#endif
25922
25923#if defined(FEAT_SESSION) || defined(PROTO)
25924 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025925store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926{
Bram Moolenaar33570922005-01-25 22:26:29 +000025927 hashitem_T *hi;
25928 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025929 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 char_u *p, *t;
25931
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025932 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025933 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025935 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025937 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025938 this_var = HI2DI(hi);
25939 if ((this_var->di_tv.v_type == VAR_NUMBER
25940 || this_var->di_tv.v_type == VAR_STRING)
25941 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025942 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025943 /* Escape special characters with a backslash. Turn a LF and
25944 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025945 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025946 (char_u *)"\\\"\n\r");
25947 if (p == NULL) /* out of memory */
25948 break;
25949 for (t = p; *t != NUL; ++t)
25950 if (*t == '\n')
25951 *t = 'n';
25952 else if (*t == '\r')
25953 *t = 'r';
25954 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025955 this_var->di_key,
25956 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25957 : ' ',
25958 p,
25959 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25960 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025961 || put_eol(fd) == FAIL)
25962 {
25963 vim_free(p);
25964 return FAIL;
25965 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025966 vim_free(p);
25967 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025968#ifdef FEAT_FLOAT
25969 else if (this_var->di_tv.v_type == VAR_FLOAT
25970 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25971 {
25972 float_T f = this_var->di_tv.vval.v_float;
25973 int sign = ' ';
25974
25975 if (f < 0)
25976 {
25977 f = -f;
25978 sign = '-';
25979 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025980 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025981 this_var->di_key, sign, f) < 0)
25982 || put_eol(fd) == FAIL)
25983 return FAIL;
25984 }
25985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 }
25987 }
25988 return OK;
25989}
25990#endif
25991
Bram Moolenaar661b1822005-07-28 22:36:45 +000025992/*
25993 * Display script name where an item was last set.
25994 * Should only be invoked when 'verbose' is non-zero.
25995 */
25996 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025997last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025998{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025999 char_u *p;
26000
Bram Moolenaar661b1822005-07-28 22:36:45 +000026001 if (scriptID != 0)
26002 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000026003 p = home_replace_save(NULL, get_scriptname(scriptID));
26004 if (p != NULL)
26005 {
26006 verbose_enter();
26007 MSG_PUTS(_("\n\tLast set from "));
26008 MSG_PUTS(p);
26009 vim_free(p);
26010 verbose_leave();
26011 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000026012 }
26013}
26014
Bram Moolenaard812df62008-11-09 12:46:09 +000026015/*
26016 * List v:oldfiles in a nice way.
26017 */
Bram Moolenaard812df62008-11-09 12:46:09 +000026018 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026019ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000026020{
26021 list_T *l = vimvars[VV_OLDFILES].vv_list;
26022 listitem_T *li;
26023 int nr = 0;
26024
26025 if (l == NULL)
26026 msg((char_u *)_("No old files"));
26027 else
26028 {
26029 msg_start();
26030 msg_scroll = TRUE;
26031 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
26032 {
26033 msg_outnum((long)++nr);
26034 MSG_PUTS(": ");
26035 msg_outtrans(get_tv_string(&li->li_tv));
26036 msg_putchar('\n');
26037 out_flush(); /* output one line at a time */
26038 ui_breakcheck();
26039 }
26040 /* Assume "got_int" was set to truncate the listing. */
26041 got_int = FALSE;
26042
26043#ifdef FEAT_BROWSE_CMD
26044 if (cmdmod.browse)
26045 {
26046 quit_more = FALSE;
26047 nr = prompt_for_number(FALSE);
26048 msg_starthere();
26049 if (nr > 0)
26050 {
26051 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
26052 (long)nr);
26053
26054 if (p != NULL)
26055 {
26056 p = expand_env_save(p);
26057 eap->arg = p;
26058 eap->cmdidx = CMD_edit;
26059 cmdmod.browse = FALSE;
26060 do_exedit(eap, NULL);
26061 vim_free(p);
26062 }
26063 }
26064 }
26065#endif
26066 }
26067}
26068
Bram Moolenaar53744302015-07-17 17:38:22 +020026069/* reset v:option_new, v:option_old and v:option_type */
26070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026071reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026072{
26073 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26074 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26075 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26076}
26077
26078
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079#endif /* FEAT_EVAL */
26080
Bram Moolenaar071d4272004-06-13 20:20:40 +000026081
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026082#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026083
26084#ifdef WIN3264
26085/*
26086 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26087 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026088static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26089static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26090static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091
26092/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026093 * Get the short path (8.3) for the filename in "fnamep".
26094 * Only works for a valid file name.
26095 * When the path gets longer "fnamep" is changed and the allocated buffer
26096 * is put in "bufp".
26097 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26098 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 */
26100 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026101get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026102{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026103 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104 char_u *newbuf;
26105
26106 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026107 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026108 if (l > len - 1)
26109 {
26110 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026111 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112 newbuf = vim_strnsave(*fnamep, l);
26113 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026114 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026115
26116 vim_free(*bufp);
26117 *fnamep = *bufp = newbuf;
26118
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026119 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026120 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121 }
26122
26123 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026124 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026125}
26126
26127/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026128 * Get the short path (8.3) for the filename in "fname". The converted
26129 * path is returned in "bufp".
26130 *
26131 * Some of the directories specified in "fname" may not exist. This function
26132 * will shorten the existing directories at the beginning of the path and then
26133 * append the remaining non-existing path.
26134 *
26135 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026136 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026137 * bufp - Pointer to an allocated buffer for the filename.
26138 * fnamelen - Length of the filename pointed to by fname
26139 *
26140 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 */
26142 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026143shortpath_for_invalid_fname(
26144 char_u **fname,
26145 char_u **bufp,
26146 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026148 char_u *short_fname, *save_fname, *pbuf_unused;
26149 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026150 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026151 int old_len, len;
26152 int new_len, sfx_len;
26153 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026154
26155 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026156 old_len = *fnamelen;
26157 save_fname = vim_strnsave(*fname, old_len);
26158 pbuf_unused = NULL;
26159 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 endp = save_fname + old_len - 1; /* Find the end of the copy */
26162 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026163
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026164 /*
26165 * Try shortening the supplied path till it succeeds by removing one
26166 * directory at a time from the tail of the path.
26167 */
26168 len = 0;
26169 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026170 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026171 /* go back one path-separator */
26172 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26173 --endp;
26174 if (endp <= save_fname)
26175 break; /* processed the complete path */
26176
26177 /*
26178 * Replace the path separator with a NUL and try to shorten the
26179 * resulting path.
26180 */
26181 ch = *endp;
26182 *endp = 0;
26183 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026184 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026185 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26186 {
26187 retval = FAIL;
26188 goto theend;
26189 }
26190 *endp = ch; /* preserve the string */
26191
26192 if (len > 0)
26193 break; /* successfully shortened the path */
26194
26195 /* failed to shorten the path. Skip the path separator */
26196 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026197 }
26198
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026199 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026200 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026201 /*
26202 * Succeeded in shortening the path. Now concatenate the shortened
26203 * path with the remaining path at the tail.
26204 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026205
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026206 /* Compute the length of the new path. */
26207 sfx_len = (int)(save_endp - endp) + 1;
26208 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026210 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026212 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026213 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026214 /* There is not enough space in the currently allocated string,
26215 * copy it to a buffer big enough. */
26216 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026217 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026218 {
26219 retval = FAIL;
26220 goto theend;
26221 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222 }
26223 else
26224 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026225 /* Transfer short_fname to the main buffer (it's big enough),
26226 * unless get_short_pathname() did its work in-place. */
26227 *fname = *bufp = save_fname;
26228 if (short_fname != save_fname)
26229 vim_strncpy(save_fname, short_fname, len);
26230 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026231 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026232
26233 /* concat the not-shortened part of the path */
26234 vim_strncpy(*fname + len, endp, sfx_len);
26235 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026236 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026237
26238theend:
26239 vim_free(pbuf_unused);
26240 vim_free(save_fname);
26241
26242 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026243}
26244
26245/*
26246 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026247 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026248 */
26249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026250shortpath_for_partial(
26251 char_u **fnamep,
26252 char_u **bufp,
26253 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026254{
26255 int sepcount, len, tflen;
26256 char_u *p;
26257 char_u *pbuf, *tfname;
26258 int hasTilde;
26259
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026260 /* Count up the path separators from the RHS.. so we know which part
26261 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026262 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026263 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026264 if (vim_ispathsep(*p))
26265 ++sepcount;
26266
26267 /* Need full path first (use expand_env() to remove a "~/") */
26268 hasTilde = (**fnamep == '~');
26269 if (hasTilde)
26270 pbuf = tfname = expand_env_save(*fnamep);
26271 else
26272 pbuf = tfname = FullName_save(*fnamep, FALSE);
26273
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026274 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026276 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26277 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026278
26279 if (len == 0)
26280 {
26281 /* Don't have a valid filename, so shorten the rest of the
26282 * path if we can. This CAN give us invalid 8.3 filenames, but
26283 * there's not a lot of point in guessing what it might be.
26284 */
26285 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026286 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26287 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026288 }
26289
26290 /* Count the paths backward to find the beginning of the desired string. */
26291 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026292 {
26293#ifdef FEAT_MBYTE
26294 if (has_mbyte)
26295 p -= mb_head_off(tfname, p);
26296#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026297 if (vim_ispathsep(*p))
26298 {
26299 if (sepcount == 0 || (hasTilde && sepcount == 1))
26300 break;
26301 else
26302 sepcount --;
26303 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026304 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026305 if (hasTilde)
26306 {
26307 --p;
26308 if (p >= tfname)
26309 *p = '~';
26310 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026311 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312 }
26313 else
26314 ++p;
26315
26316 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26317 vim_free(*bufp);
26318 *fnamelen = (int)STRLEN(p);
26319 *bufp = pbuf;
26320 *fnamep = p;
26321
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026322 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026323}
26324#endif /* WIN3264 */
26325
26326/*
26327 * Adjust a filename, according to a string of modifiers.
26328 * *fnamep must be NUL terminated when called. When returning, the length is
26329 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026330 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026331 * When there is an error, *fnamep is set to NULL.
26332 */
26333 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026334modify_fname(
26335 char_u *src, /* string with modifiers */
26336 int *usedlen, /* characters after src that are used */
26337 char_u **fnamep, /* file name so far */
26338 char_u **bufp, /* buffer for allocated file name or NULL */
26339 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340{
26341 int valid = 0;
26342 char_u *tail;
26343 char_u *s, *p, *pbuf;
26344 char_u dirname[MAXPATHL];
26345 int c;
26346 int has_fullname = 0;
26347#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026348 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026349 int has_shortname = 0;
26350#endif
26351
26352repeat:
26353 /* ":p" - full path/file_name */
26354 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26355 {
26356 has_fullname = 1;
26357
26358 valid |= VALID_PATH;
26359 *usedlen += 2;
26360
26361 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26362 if ((*fnamep)[0] == '~'
26363#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26364 && ((*fnamep)[1] == '/'
26365# ifdef BACKSLASH_IN_FILENAME
26366 || (*fnamep)[1] == '\\'
26367# endif
26368 || (*fnamep)[1] == NUL)
26369
26370#endif
26371 )
26372 {
26373 *fnamep = expand_env_save(*fnamep);
26374 vim_free(*bufp); /* free any allocated file name */
26375 *bufp = *fnamep;
26376 if (*fnamep == NULL)
26377 return -1;
26378 }
26379
26380 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026381 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026382 {
26383 if (vim_ispathsep(*p)
26384 && p[1] == '.'
26385 && (p[2] == NUL
26386 || vim_ispathsep(p[2])
26387 || (p[2] == '.'
26388 && (p[3] == NUL || vim_ispathsep(p[3])))))
26389 break;
26390 }
26391
26392 /* FullName_save() is slow, don't use it when not needed. */
26393 if (*p != NUL || !vim_isAbsName(*fnamep))
26394 {
26395 *fnamep = FullName_save(*fnamep, *p != NUL);
26396 vim_free(*bufp); /* free any allocated file name */
26397 *bufp = *fnamep;
26398 if (*fnamep == NULL)
26399 return -1;
26400 }
26401
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026402#ifdef WIN3264
26403# if _WIN32_WINNT >= 0x0500
26404 if (vim_strchr(*fnamep, '~') != NULL)
26405 {
26406 /* Expand 8.3 filename to full path. Needed to make sure the same
26407 * file does not have two different names.
26408 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26409 p = alloc(_MAX_PATH + 1);
26410 if (p != NULL)
26411 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026412 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026413 {
26414 vim_free(*bufp);
26415 *bufp = *fnamep = p;
26416 }
26417 else
26418 vim_free(p);
26419 }
26420 }
26421# endif
26422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 /* Append a path separator to a directory. */
26424 if (mch_isdir(*fnamep))
26425 {
26426 /* Make room for one or two extra characters. */
26427 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26428 vim_free(*bufp); /* free any allocated file name */
26429 *bufp = *fnamep;
26430 if (*fnamep == NULL)
26431 return -1;
26432 add_pathsep(*fnamep);
26433 }
26434 }
26435
26436 /* ":." - path relative to the current directory */
26437 /* ":~" - path relative to the home directory */
26438 /* ":8" - shortname path - postponed till after */
26439 while (src[*usedlen] == ':'
26440 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26441 {
26442 *usedlen += 2;
26443 if (c == '8')
26444 {
26445#ifdef WIN3264
26446 has_shortname = 1; /* Postpone this. */
26447#endif
26448 continue;
26449 }
26450 pbuf = NULL;
26451 /* Need full path first (use expand_env() to remove a "~/") */
26452 if (!has_fullname)
26453 {
26454 if (c == '.' && **fnamep == '~')
26455 p = pbuf = expand_env_save(*fnamep);
26456 else
26457 p = pbuf = FullName_save(*fnamep, FALSE);
26458 }
26459 else
26460 p = *fnamep;
26461
26462 has_fullname = 0;
26463
26464 if (p != NULL)
26465 {
26466 if (c == '.')
26467 {
26468 mch_dirname(dirname, MAXPATHL);
26469 s = shorten_fname(p, dirname);
26470 if (s != NULL)
26471 {
26472 *fnamep = s;
26473 if (pbuf != NULL)
26474 {
26475 vim_free(*bufp); /* free any allocated file name */
26476 *bufp = pbuf;
26477 pbuf = NULL;
26478 }
26479 }
26480 }
26481 else
26482 {
26483 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26484 /* Only replace it when it starts with '~' */
26485 if (*dirname == '~')
26486 {
26487 s = vim_strsave(dirname);
26488 if (s != NULL)
26489 {
26490 *fnamep = s;
26491 vim_free(*bufp);
26492 *bufp = s;
26493 }
26494 }
26495 }
26496 vim_free(pbuf);
26497 }
26498 }
26499
26500 tail = gettail(*fnamep);
26501 *fnamelen = (int)STRLEN(*fnamep);
26502
26503 /* ":h" - head, remove "/file_name", can be repeated */
26504 /* Don't remove the first "/" or "c:\" */
26505 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26506 {
26507 valid |= VALID_HEAD;
26508 *usedlen += 2;
26509 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026510 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026511 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026512 *fnamelen = (int)(tail - *fnamep);
26513#ifdef VMS
26514 if (*fnamelen > 0)
26515 *fnamelen += 1; /* the path separator is part of the path */
26516#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026517 if (*fnamelen == 0)
26518 {
26519 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26520 p = vim_strsave((char_u *)".");
26521 if (p == NULL)
26522 return -1;
26523 vim_free(*bufp);
26524 *bufp = *fnamep = tail = p;
26525 *fnamelen = 1;
26526 }
26527 else
26528 {
26529 while (tail > s && !after_pathsep(s, tail))
26530 mb_ptr_back(*fnamep, tail);
26531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532 }
26533
26534 /* ":8" - shortname */
26535 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26536 {
26537 *usedlen += 2;
26538#ifdef WIN3264
26539 has_shortname = 1;
26540#endif
26541 }
26542
26543#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026544 /*
26545 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 */
26547 if (has_shortname)
26548 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026549 /* Copy the string if it is shortened by :h and when it wasn't copied
26550 * yet, because we are going to change it in place. Avoids changing
26551 * the buffer name for "%:8". */
26552 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026553 {
26554 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026555 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026556 return -1;
26557 vim_free(*bufp);
26558 *bufp = *fnamep = p;
26559 }
26560
26561 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026562 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563 if (!has_fullname && !vim_isAbsName(*fnamep))
26564 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026565 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026566 return -1;
26567 }
26568 else
26569 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026570 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026571
Bram Moolenaardc935552011-08-17 15:23:23 +020026572 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026573 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026574 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026575 return -1;
26576
26577 if (l == 0)
26578 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026579 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026580 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026581 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026582 return -1;
26583 }
26584 *fnamelen = l;
26585 }
26586 }
26587#endif /* WIN3264 */
26588
26589 /* ":t" - tail, just the basename */
26590 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26591 {
26592 *usedlen += 2;
26593 *fnamelen -= (int)(tail - *fnamep);
26594 *fnamep = tail;
26595 }
26596
26597 /* ":e" - extension, can be repeated */
26598 /* ":r" - root, without extension, can be repeated */
26599 while (src[*usedlen] == ':'
26600 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26601 {
26602 /* find a '.' in the tail:
26603 * - for second :e: before the current fname
26604 * - otherwise: The last '.'
26605 */
26606 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26607 s = *fnamep - 2;
26608 else
26609 s = *fnamep + *fnamelen - 1;
26610 for ( ; s > tail; --s)
26611 if (s[0] == '.')
26612 break;
26613 if (src[*usedlen + 1] == 'e') /* :e */
26614 {
26615 if (s > tail)
26616 {
26617 *fnamelen += (int)(*fnamep - (s + 1));
26618 *fnamep = s + 1;
26619#ifdef VMS
26620 /* cut version from the extension */
26621 s = *fnamep + *fnamelen - 1;
26622 for ( ; s > *fnamep; --s)
26623 if (s[0] == ';')
26624 break;
26625 if (s > *fnamep)
26626 *fnamelen = s - *fnamep;
26627#endif
26628 }
26629 else if (*fnamep <= tail)
26630 *fnamelen = 0;
26631 }
26632 else /* :r */
26633 {
26634 if (s > tail) /* remove one extension */
26635 *fnamelen = (int)(s - *fnamep);
26636 }
26637 *usedlen += 2;
26638 }
26639
26640 /* ":s?pat?foo?" - substitute */
26641 /* ":gs?pat?foo?" - global substitute */
26642 if (src[*usedlen] == ':'
26643 && (src[*usedlen + 1] == 's'
26644 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26645 {
26646 char_u *str;
26647 char_u *pat;
26648 char_u *sub;
26649 int sep;
26650 char_u *flags;
26651 int didit = FALSE;
26652
26653 flags = (char_u *)"";
26654 s = src + *usedlen + 2;
26655 if (src[*usedlen + 1] == 'g')
26656 {
26657 flags = (char_u *)"g";
26658 ++s;
26659 }
26660
26661 sep = *s++;
26662 if (sep)
26663 {
26664 /* find end of pattern */
26665 p = vim_strchr(s, sep);
26666 if (p != NULL)
26667 {
26668 pat = vim_strnsave(s, (int)(p - s));
26669 if (pat != NULL)
26670 {
26671 s = p + 1;
26672 /* find end of substitution */
26673 p = vim_strchr(s, sep);
26674 if (p != NULL)
26675 {
26676 sub = vim_strnsave(s, (int)(p - s));
26677 str = vim_strnsave(*fnamep, *fnamelen);
26678 if (sub != NULL && str != NULL)
26679 {
26680 *usedlen = (int)(p + 1 - src);
26681 s = do_string_sub(str, pat, sub, flags);
26682 if (s != NULL)
26683 {
26684 *fnamep = s;
26685 *fnamelen = (int)STRLEN(s);
26686 vim_free(*bufp);
26687 *bufp = s;
26688 didit = TRUE;
26689 }
26690 }
26691 vim_free(sub);
26692 vim_free(str);
26693 }
26694 vim_free(pat);
26695 }
26696 }
26697 /* after using ":s", repeat all the modifiers */
26698 if (didit)
26699 goto repeat;
26700 }
26701 }
26702
Bram Moolenaar26df0922014-02-23 23:39:13 +010026703 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26704 {
26705 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26706 if (p == NULL)
26707 return -1;
26708 vim_free(*bufp);
26709 *bufp = *fnamep = p;
26710 *fnamelen = (int)STRLEN(p);
26711 *usedlen += 2;
26712 }
26713
Bram Moolenaar071d4272004-06-13 20:20:40 +000026714 return valid;
26715}
26716
26717/*
26718 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26719 * "flags" can be "g" to do a global substitute.
26720 * Returns an allocated string, NULL for error.
26721 */
26722 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026723do_string_sub(
26724 char_u *str,
26725 char_u *pat,
26726 char_u *sub,
26727 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026728{
26729 int sublen;
26730 regmatch_T regmatch;
26731 int i;
26732 int do_all;
26733 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026734 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026735 garray_T ga;
26736 char_u *ret;
26737 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026738 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026739
26740 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26741 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026742 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026743
26744 ga_init2(&ga, 1, 200);
26745
26746 do_all = (flags[0] == 'g');
26747
26748 regmatch.rm_ic = p_ic;
26749 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26750 if (regmatch.regprog != NULL)
26751 {
26752 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026753 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026754 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26755 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026756 /* Skip empty match except for first match. */
26757 if (regmatch.startp[0] == regmatch.endp[0])
26758 {
26759 if (zero_width == regmatch.startp[0])
26760 {
26761 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026762 i = MB_PTR2LEN(tail);
26763 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26764 (size_t)i);
26765 ga.ga_len += i;
26766 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026767 continue;
26768 }
26769 zero_width = regmatch.startp[0];
26770 }
26771
Bram Moolenaar071d4272004-06-13 20:20:40 +000026772 /*
26773 * Get some space for a temporary buffer to do the substitution
26774 * into. It will contain:
26775 * - The text up to where the match is.
26776 * - The substituted text.
26777 * - The text after the match.
26778 */
26779 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026780 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026781 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26782 {
26783 ga_clear(&ga);
26784 break;
26785 }
26786
26787 /* copy the text up to where the match is */
26788 i = (int)(regmatch.startp[0] - tail);
26789 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26790 /* add the substituted text */
26791 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26792 + ga.ga_len + i, TRUE, TRUE, FALSE);
26793 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026794 tail = regmatch.endp[0];
26795 if (*tail == NUL)
26796 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026797 if (!do_all)
26798 break;
26799 }
26800
26801 if (ga.ga_data != NULL)
26802 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26803
Bram Moolenaar473de612013-06-08 18:19:48 +020026804 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026805 }
26806
26807 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26808 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026809 if (p_cpo == empty_option)
26810 p_cpo = save_cpo;
26811 else
26812 /* Darn, evaluating {sub} expression changed the value. */
26813 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026814
26815 return ret;
26816}
26817
26818#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */