blob: a6486374b2590ebdf749e3e76d2b343d6653a080 [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 Moolenaar02e83b42016-02-21 20:10:26 +0100510# ifdef FEAT_JOB
511static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
512# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100513static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100514static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
515static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100516static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100517static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100518static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
519static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100520static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100521static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100522#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_changenr(typval_T *argvars, typval_T *rettv);
524static void f_char2nr(typval_T *argvars, typval_T *rettv);
525static void f_cindent(typval_T *argvars, typval_T *rettv);
526static void f_clearmatches(typval_T *argvars, typval_T *rettv);
527static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000528#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100529static void f_complete(typval_T *argvars, typval_T *rettv);
530static void f_complete_add(typval_T *argvars, typval_T *rettv);
531static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_confirm(typval_T *argvars, typval_T *rettv);
534static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000535#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100536static void f_cos(typval_T *argvars, typval_T *rettv);
537static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000538#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_count(typval_T *argvars, typval_T *rettv);
540static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
541static void f_cursor(typval_T *argsvars, typval_T *rettv);
542static void f_deepcopy(typval_T *argvars, typval_T *rettv);
543static void f_delete(typval_T *argvars, typval_T *rettv);
544static void f_did_filetype(typval_T *argvars, typval_T *rettv);
545static void f_diff_filler(typval_T *argvars, typval_T *rettv);
546static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100547static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100548static void f_empty(typval_T *argvars, typval_T *rettv);
549static void f_escape(typval_T *argvars, typval_T *rettv);
550static void f_eval(typval_T *argvars, typval_T *rettv);
551static void f_eventhandler(typval_T *argvars, typval_T *rettv);
552static void f_executable(typval_T *argvars, typval_T *rettv);
553static void f_exepath(typval_T *argvars, typval_T *rettv);
554static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200555#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100556static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200557#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_expand(typval_T *argvars, typval_T *rettv);
559static void f_extend(typval_T *argvars, typval_T *rettv);
560static void f_feedkeys(typval_T *argvars, typval_T *rettv);
561static void f_filereadable(typval_T *argvars, typval_T *rettv);
562static void f_filewritable(typval_T *argvars, typval_T *rettv);
563static void f_filter(typval_T *argvars, typval_T *rettv);
564static void f_finddir(typval_T *argvars, typval_T *rettv);
565static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000566#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100567static void f_float2nr(typval_T *argvars, typval_T *rettv);
568static void f_floor(typval_T *argvars, typval_T *rettv);
569static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000570#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100571static void f_fnameescape(typval_T *argvars, typval_T *rettv);
572static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
573static void f_foldclosed(typval_T *argvars, typval_T *rettv);
574static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
575static void f_foldlevel(typval_T *argvars, typval_T *rettv);
576static void f_foldtext(typval_T *argvars, typval_T *rettv);
577static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
578static void f_foreground(typval_T *argvars, typval_T *rettv);
579static void f_function(typval_T *argvars, typval_T *rettv);
580static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
581static void f_get(typval_T *argvars, typval_T *rettv);
582static void f_getbufline(typval_T *argvars, typval_T *rettv);
583static void f_getbufvar(typval_T *argvars, typval_T *rettv);
584static void f_getchar(typval_T *argvars, typval_T *rettv);
585static void f_getcharmod(typval_T *argvars, typval_T *rettv);
586static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
587static void f_getcmdline(typval_T *argvars, typval_T *rettv);
588static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
589static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
590static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
591static void f_getcwd(typval_T *argvars, typval_T *rettv);
592static void f_getfontname(typval_T *argvars, typval_T *rettv);
593static void f_getfperm(typval_T *argvars, typval_T *rettv);
594static void f_getfsize(typval_T *argvars, typval_T *rettv);
595static void f_getftime(typval_T *argvars, typval_T *rettv);
596static void f_getftype(typval_T *argvars, typval_T *rettv);
597static void f_getline(typval_T *argvars, typval_T *rettv);
598static void f_getmatches(typval_T *argvars, typval_T *rettv);
599static void f_getpid(typval_T *argvars, typval_T *rettv);
600static void f_getcurpos(typval_T *argvars, typval_T *rettv);
601static void f_getpos(typval_T *argvars, typval_T *rettv);
602static void f_getqflist(typval_T *argvars, typval_T *rettv);
603static void f_getreg(typval_T *argvars, typval_T *rettv);
604static void f_getregtype(typval_T *argvars, typval_T *rettv);
605static void f_gettabvar(typval_T *argvars, typval_T *rettv);
606static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
607static void f_getwinposx(typval_T *argvars, typval_T *rettv);
608static void f_getwinposy(typval_T *argvars, typval_T *rettv);
609static void f_getwinvar(typval_T *argvars, typval_T *rettv);
610static void f_glob(typval_T *argvars, typval_T *rettv);
611static void f_globpath(typval_T *argvars, typval_T *rettv);
612static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
613static void f_has(typval_T *argvars, typval_T *rettv);
614static void f_has_key(typval_T *argvars, typval_T *rettv);
615static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
616static void f_hasmapto(typval_T *argvars, typval_T *rettv);
617static void f_histadd(typval_T *argvars, typval_T *rettv);
618static void f_histdel(typval_T *argvars, typval_T *rettv);
619static void f_histget(typval_T *argvars, typval_T *rettv);
620static void f_histnr(typval_T *argvars, typval_T *rettv);
621static void f_hlID(typval_T *argvars, typval_T *rettv);
622static void f_hlexists(typval_T *argvars, typval_T *rettv);
623static void f_hostname(typval_T *argvars, typval_T *rettv);
624static void f_iconv(typval_T *argvars, typval_T *rettv);
625static void f_indent(typval_T *argvars, typval_T *rettv);
626static void f_index(typval_T *argvars, typval_T *rettv);
627static void f_input(typval_T *argvars, typval_T *rettv);
628static void f_inputdialog(typval_T *argvars, typval_T *rettv);
629static void f_inputlist(typval_T *argvars, typval_T *rettv);
630static void f_inputrestore(typval_T *argvars, typval_T *rettv);
631static void f_inputsave(typval_T *argvars, typval_T *rettv);
632static void f_inputsecret(typval_T *argvars, typval_T *rettv);
633static void f_insert(typval_T *argvars, typval_T *rettv);
634static void f_invert(typval_T *argvars, typval_T *rettv);
635static void f_isdirectory(typval_T *argvars, typval_T *rettv);
636static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100637#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
638static void f_isnan(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100641#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100642# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100643static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaarfa4bce72016-02-13 23:50:08 +0100644# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +0100645static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100646static void f_job_start(typval_T *argvars, typval_T *rettv);
647static void f_job_stop(typval_T *argvars, typval_T *rettv);
648static void f_job_status(typval_T *argvars, typval_T *rettv);
649#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100650static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100651static void f_js_decode(typval_T *argvars, typval_T *rettv);
652static void f_js_encode(typval_T *argvars, typval_T *rettv);
653static void f_json_decode(typval_T *argvars, typval_T *rettv);
654static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_keys(typval_T *argvars, typval_T *rettv);
656static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
657static void f_len(typval_T *argvars, typval_T *rettv);
658static void f_libcall(typval_T *argvars, typval_T *rettv);
659static void f_libcallnr(typval_T *argvars, typval_T *rettv);
660static void f_line(typval_T *argvars, typval_T *rettv);
661static void f_line2byte(typval_T *argvars, typval_T *rettv);
662static void f_lispindent(typval_T *argvars, typval_T *rettv);
663static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000664#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_log(typval_T *argvars, typval_T *rettv);
666static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000667#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200668#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100669static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200670#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100671static void f_map(typval_T *argvars, typval_T *rettv);
672static void f_maparg(typval_T *argvars, typval_T *rettv);
673static void f_mapcheck(typval_T *argvars, typval_T *rettv);
674static void f_match(typval_T *argvars, typval_T *rettv);
675static void f_matchadd(typval_T *argvars, typval_T *rettv);
676static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
677static void f_matcharg(typval_T *argvars, typval_T *rettv);
678static void f_matchdelete(typval_T *argvars, typval_T *rettv);
679static void f_matchend(typval_T *argvars, typval_T *rettv);
680static void f_matchlist(typval_T *argvars, typval_T *rettv);
681static void f_matchstr(typval_T *argvars, typval_T *rettv);
682static void f_max(typval_T *argvars, typval_T *rettv);
683static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000684#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000686#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100687static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100688#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
692static void f_nr2char(typval_T *argvars, typval_T *rettv);
693static void f_or(typval_T *argvars, typval_T *rettv);
694static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100695#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100697#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000698#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000700#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
702static void f_printf(typval_T *argvars, typval_T *rettv);
703static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200704#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200706#endif
707#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100708static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_range(typval_T *argvars, typval_T *rettv);
711static void f_readfile(typval_T *argvars, typval_T *rettv);
712static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100713#ifdef FEAT_FLOAT
714static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
715#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100716static void f_reltimestr(typval_T *argvars, typval_T *rettv);
717static void f_remote_expr(typval_T *argvars, typval_T *rettv);
718static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
719static void f_remote_peek(typval_T *argvars, typval_T *rettv);
720static void f_remote_read(typval_T *argvars, typval_T *rettv);
721static void f_remote_send(typval_T *argvars, typval_T *rettv);
722static void f_remove(typval_T *argvars, typval_T *rettv);
723static void f_rename(typval_T *argvars, typval_T *rettv);
724static void f_repeat(typval_T *argvars, typval_T *rettv);
725static void f_resolve(typval_T *argvars, typval_T *rettv);
726static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000727#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100728static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000729#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100730static void f_screenattr(typval_T *argvars, typval_T *rettv);
731static void f_screenchar(typval_T *argvars, typval_T *rettv);
732static void f_screencol(typval_T *argvars, typval_T *rettv);
733static void f_screenrow(typval_T *argvars, typval_T *rettv);
734static void f_search(typval_T *argvars, typval_T *rettv);
735static void f_searchdecl(typval_T *argvars, typval_T *rettv);
736static void f_searchpair(typval_T *argvars, typval_T *rettv);
737static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
738static void f_searchpos(typval_T *argvars, typval_T *rettv);
739static void f_server2client(typval_T *argvars, typval_T *rettv);
740static void f_serverlist(typval_T *argvars, typval_T *rettv);
741static void f_setbufvar(typval_T *argvars, typval_T *rettv);
742static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
743static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
744static void f_setline(typval_T *argvars, typval_T *rettv);
745static void f_setloclist(typval_T *argvars, typval_T *rettv);
746static void f_setmatches(typval_T *argvars, typval_T *rettv);
747static void f_setpos(typval_T *argvars, typval_T *rettv);
748static void f_setqflist(typval_T *argvars, typval_T *rettv);
749static void f_setreg(typval_T *argvars, typval_T *rettv);
750static void f_settabvar(typval_T *argvars, typval_T *rettv);
751static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
752static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100753#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100755#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_shellescape(typval_T *argvars, typval_T *rettv);
757static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
758static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sin(typval_T *argvars, typval_T *rettv);
761static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sort(typval_T *argvars, typval_T *rettv);
764static void f_soundfold(typval_T *argvars, typval_T *rettv);
765static void f_spellbadword(typval_T *argvars, typval_T *rettv);
766static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
767static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000768#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_sqrt(typval_T *argvars, typval_T *rettv);
770static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_str2nr(typval_T *argvars, typval_T *rettv);
773static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000774#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100775static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000776#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100777static void f_stridx(typval_T *argvars, typval_T *rettv);
778static void f_string(typval_T *argvars, typval_T *rettv);
779static void f_strlen(typval_T *argvars, typval_T *rettv);
780static void f_strpart(typval_T *argvars, typval_T *rettv);
781static void f_strridx(typval_T *argvars, typval_T *rettv);
782static void f_strtrans(typval_T *argvars, typval_T *rettv);
783static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
784static void f_strwidth(typval_T *argvars, typval_T *rettv);
785static void f_submatch(typval_T *argvars, typval_T *rettv);
786static void f_substitute(typval_T *argvars, typval_T *rettv);
787static void f_synID(typval_T *argvars, typval_T *rettv);
788static void f_synIDattr(typval_T *argvars, typval_T *rettv);
789static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
790static void f_synstack(typval_T *argvars, typval_T *rettv);
791static void f_synconcealed(typval_T *argvars, typval_T *rettv);
792static void f_system(typval_T *argvars, typval_T *rettv);
793static void f_systemlist(typval_T *argvars, typval_T *rettv);
794static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
795static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
796static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
797static void f_taglist(typval_T *argvars, typval_T *rettv);
798static void f_tagfiles(typval_T *argvars, typval_T *rettv);
799static void f_tempname(typval_T *argvars, typval_T *rettv);
800static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200801#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100802static void f_tan(typval_T *argvars, typval_T *rettv);
803static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_tolower(typval_T *argvars, typval_T *rettv);
806static void f_toupper(typval_T *argvars, typval_T *rettv);
807static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_type(typval_T *argvars, typval_T *rettv);
812static void f_undofile(typval_T *argvars, typval_T *rettv);
813static void f_undotree(typval_T *argvars, typval_T *rettv);
814static void f_uniq(typval_T *argvars, typval_T *rettv);
815static void f_values(typval_T *argvars, typval_T *rettv);
816static void f_virtcol(typval_T *argvars, typval_T *rettv);
817static void f_visualmode(typval_T *argvars, typval_T *rettv);
818static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
819static void f_winbufnr(typval_T *argvars, typval_T *rettv);
820static void f_wincol(typval_T *argvars, typval_T *rettv);
821static void f_winheight(typval_T *argvars, typval_T *rettv);
822static void f_winline(typval_T *argvars, typval_T *rettv);
823static void f_winnr(typval_T *argvars, typval_T *rettv);
824static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
825static void f_winrestview(typval_T *argvars, typval_T *rettv);
826static void f_winsaveview(typval_T *argvars, typval_T *rettv);
827static void f_winwidth(typval_T *argvars, typval_T *rettv);
828static void f_writefile(typval_T *argvars, typval_T *rettv);
829static void f_wordcount(typval_T *argvars, typval_T *rettv);
830static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
833static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
834static int get_env_len(char_u **arg);
835static int get_id_len(char_u **arg);
836static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
837static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
839#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
840 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
842static int eval_isnamec(int c);
843static int eval_isnamec1(int c);
844static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
845static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static typval_T *alloc_string_tv(char_u *string);
847static void init_tv(typval_T *varp);
848static long get_tv_number(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100849#ifdef FEAT_FLOAT
850static float_T get_tv_float(typval_T *varp);
851#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100852static linenr_T get_tv_lnum(typval_T *argvars);
853static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
854static char_u *get_tv_string(typval_T *varp);
855static char_u *get_tv_string_buf(typval_T *varp, char_u *buf);
856static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
857static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
858static hashtab_T *find_var_ht(char_u *name, char_u **varname);
859static funccall_T *get_funccal(void);
860static void vars_clear_ext(hashtab_T *ht, int free_val);
861static void delete_var(hashtab_T *ht, hashitem_T *hi);
862static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
863static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
864static void set_var(char_u *name, typval_T *varp, int copy);
865static int var_check_ro(int flags, char_u *name, int use_gettext);
866static int var_check_fixed(int flags, char_u *name, int use_gettext);
867static int var_check_func_name(char_u *name, int new_var);
868static int valid_varname(char_u *varname);
869static int tv_check_lock(int lock, char_u *name, int use_gettext);
870static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
871static char_u *find_option_end(char_u **arg, int *opt_flags);
872static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd);
873static int eval_fname_script(char_u *p);
874static int eval_fname_sid(char_u *p);
875static void list_func_head(ufunc_T *fp, int indent);
876static ufunc_T *find_func(char_u *name);
877static int function_exists(char_u *name);
878static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000879#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100880static void func_do_profile(ufunc_T *fp);
881static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
882static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000883static int
884# ifdef __BORLANDC__
885 _RTLENTRYF
886# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100887 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000888static int
889# ifdef __BORLANDC__
890 _RTLENTRYF
891# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100892 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000893#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894static int script_autoload(char_u *name, int reload);
895static char_u *autoload_name(char_u *name);
896static void cat_func_name(char_u *buf, ufunc_T *fp);
897static void func_free(ufunc_T *fp);
898static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
899static int can_free_funccal(funccall_T *fc, int copyID) ;
900static void free_funccal(funccall_T *fc, int free_val);
901static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
902static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
903static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
904static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
906static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
907static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
908static int write_list(FILE *fd, list_T *list, int binary);
909static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000910
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200911
912#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100913static int compare_func_name(const void *s1, const void *s2);
914static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200915#endif
916
Bram Moolenaar33570922005-01-25 22:26:29 +0000917/*
918 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000919 */
920 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100921eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000922{
Bram Moolenaar33570922005-01-25 22:26:29 +0000923 int i;
924 struct vimvar *p;
925
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200926 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
927 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200928 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000929 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000930 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000931
932 for (i = 0; i < VV_LEN; ++i)
933 {
934 p = &vimvars[i];
935 STRCPY(p->vv_di.di_key, p->vv_name);
936 if (p->vv_flags & VV_RO)
937 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
938 else if (p->vv_flags & VV_RO_SBX)
939 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
940 else
941 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000942
943 /* add to v: scope dict, unless the value is not always available */
944 if (p->vv_type != VAR_UNKNOWN)
945 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000947 /* add to compat scope dict */
948 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000949 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100950 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
951
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000952 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100953 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200954 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100955 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100956
957 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
958 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
959 set_vim_var_nr(VV_NONE, VVAL_NONE);
960 set_vim_var_nr(VV_NULL, VVAL_NULL);
961
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200962 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200963
964#ifdef EBCDIC
965 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100966 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200967 */
968 sortFunctions();
969#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000970}
971
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972#if defined(EXITFREE) || defined(PROTO)
973 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100974eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000975{
976 int i;
977 struct vimvar *p;
978
979 for (i = 0; i < VV_LEN; ++i)
980 {
981 p = &vimvars[i];
982 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000983 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000984 vim_free(p->vv_str);
985 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000986 }
987 else if (p->vv_di.di_tv.v_type == VAR_LIST)
988 {
989 list_unref(p->vv_list);
990 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000991 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 }
993 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000994 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000995 hash_clear(&compat_hashtab);
996
Bram Moolenaard9fba312005-06-26 22:34:35 +0000997 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100998# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200999 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001000# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001001
1002 /* global variables */
1003 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001004
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001005 /* autoloaded script names */
1006 ga_clear_strings(&ga_loaded);
1007
Bram Moolenaarcca74132013-09-25 21:00:28 +02001008 /* Script-local variables. First clear all the variables and in a second
1009 * loop free the scriptvar_T, because a variable in one script might hold
1010 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001013 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001014 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001015 ga_clear(&ga_scripts);
1016
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001017 /* unreferenced lists and dicts */
1018 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001019
1020 /* functions */
1021 free_all_functions();
1022 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001023}
1024#endif
1025
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001026/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001027 * Return the name of the executed function.
1028 */
1029 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001030func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001031{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001032 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001033}
1034
1035/*
1036 * Return the address holding the next breakpoint line for a funccall cookie.
1037 */
1038 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001039func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040{
Bram Moolenaar33570922005-01-25 22:26:29 +00001041 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001042}
1043
1044/*
1045 * Return the address holding the debug tick for a funccall cookie.
1046 */
1047 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001048func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049{
Bram Moolenaar33570922005-01-25 22:26:29 +00001050 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001051}
1052
1053/*
1054 * Return the nesting level for a funccall cookie.
1055 */
1056 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001057func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058{
Bram Moolenaar33570922005-01-25 22:26:29 +00001059 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001060}
1061
1062/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001063funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001064
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001065/* pointer to list of previously used funccal, still around because some
1066 * item in it is still being used. */
1067funccall_T *previous_funccal = NULL;
1068
Bram Moolenaar071d4272004-06-13 20:20:40 +00001069/*
1070 * Return TRUE when a function was ended by a ":return" command.
1071 */
1072 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001073current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001074{
1075 return current_funccal->returned;
1076}
1077
1078
1079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001080 * Set an internal variable to a string value. Creates the variable if it does
1081 * not already exist.
1082 */
1083 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001084set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001086 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001087 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001088
1089 val = vim_strsave(value);
1090 if (val != NULL)
1091 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001092 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001095 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001096 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001097 }
1098 }
1099}
1100
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001101static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001102static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001103static char_u *redir_endp = NULL;
1104static char_u *redir_varname = NULL;
1105
1106/*
1107 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001108 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001109 * Returns OK if successfully completed the setup. FAIL otherwise.
1110 */
1111 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001112var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001113{
1114 int save_emsg;
1115 int err;
1116 typval_T tv;
1117
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001118 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001119 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120 {
1121 EMSG(_(e_invarg));
1122 return FAIL;
1123 }
1124
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001126 redir_varname = vim_strsave(name);
1127 if (redir_varname == NULL)
1128 return FAIL;
1129
1130 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1131 if (redir_lval == NULL)
1132 {
1133 var_redir_stop();
1134 return FAIL;
1135 }
1136
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001137 /* The output is stored in growarray "redir_ga" until redirection ends. */
1138 ga_init2(&redir_ga, (int)sizeof(char), 500);
1139
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001141 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001142 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1144 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001145 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001146 if (redir_endp != NULL && *redir_endp != NUL)
1147 /* Trailing characters are present after the variable name */
1148 EMSG(_(e_trailing));
1149 else
1150 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001151 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001152 var_redir_stop();
1153 return FAIL;
1154 }
1155
1156 /* check if we can write to the variable: set it to or append an empty
1157 * string */
1158 save_emsg = did_emsg;
1159 did_emsg = FALSE;
1160 tv.v_type = VAR_STRING;
1161 tv.vval.v_string = (char_u *)"";
1162 if (append)
1163 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1164 else
1165 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001166 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001167 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001168 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 if (err)
1170 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001171 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172 var_redir_stop();
1173 return FAIL;
1174 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001175
1176 return OK;
1177}
1178
1179/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001180 * Append "value[value_len]" to the variable set by var_redir_start().
1181 * The actual appending is postponed until redirection ends, because the value
1182 * appended may in fact be the string we write to, changing it may cause freed
1183 * memory to be used:
1184 * :redir => foo
1185 * :let foo
1186 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187 */
1188 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001189var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001190{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001191 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001192
1193 if (redir_lval == NULL)
1194 return;
1195
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001196 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001197 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001198 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001199 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001200
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 {
1203 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001204 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001205 }
1206 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208}
1209
1210/*
1211 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001212 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213 */
1214 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001215var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001217 typval_T tv;
1218
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001219 if (redir_lval != NULL)
1220 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001221 /* If there was no error: assign the text to the variable. */
1222 if (redir_endp != NULL)
1223 {
1224 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1225 tv.v_type = VAR_STRING;
1226 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001227 /* Call get_lval() again, if it's inside a Dict or List it may
1228 * have changed. */
1229 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001230 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001231 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1232 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1233 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001234 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001235
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001236 /* free the collected output */
1237 vim_free(redir_ga.ga_data);
1238 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001239
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001240 vim_free(redir_lval);
1241 redir_lval = NULL;
1242 }
1243 vim_free(redir_varname);
1244 redir_varname = NULL;
1245}
1246
Bram Moolenaar071d4272004-06-13 20:20:40 +00001247# if defined(FEAT_MBYTE) || defined(PROTO)
1248 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001249eval_charconvert(
1250 char_u *enc_from,
1251 char_u *enc_to,
1252 char_u *fname_from,
1253 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254{
1255 int err = FALSE;
1256
1257 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1258 set_vim_var_string(VV_CC_TO, enc_to, -1);
1259 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1260 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1261 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1262 err = TRUE;
1263 set_vim_var_string(VV_CC_FROM, NULL, -1);
1264 set_vim_var_string(VV_CC_TO, NULL, -1);
1265 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1266 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1267
1268 if (err)
1269 return FAIL;
1270 return OK;
1271}
1272# endif
1273
1274# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1275 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001276eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277{
1278 int err = FALSE;
1279
1280 set_vim_var_string(VV_FNAME_IN, fname, -1);
1281 set_vim_var_string(VV_CMDARG, args, -1);
1282 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1283 err = TRUE;
1284 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1285 set_vim_var_string(VV_CMDARG, NULL, -1);
1286
1287 if (err)
1288 {
1289 mch_remove(fname);
1290 return FAIL;
1291 }
1292 return OK;
1293}
1294# endif
1295
1296# if defined(FEAT_DIFF) || defined(PROTO)
1297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001298eval_diff(
1299 char_u *origfile,
1300 char_u *newfile,
1301 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001302{
1303 int err = FALSE;
1304
1305 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1306 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1307 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1308 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1309 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1310 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1311 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1312}
1313
1314 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001315eval_patch(
1316 char_u *origfile,
1317 char_u *difffile,
1318 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001319{
1320 int err;
1321
1322 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1323 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1324 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1325 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1326 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1327 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1328 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1329}
1330# endif
1331
1332/*
1333 * Top level evaluation function, returning a boolean.
1334 * Sets "error" to TRUE if there was an error.
1335 * Return TRUE or FALSE.
1336 */
1337 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001338eval_to_bool(
1339 char_u *arg,
1340 int *error,
1341 char_u **nextcmd,
1342 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001343{
Bram Moolenaar33570922005-01-25 22:26:29 +00001344 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001345 int retval = FALSE;
1346
1347 if (skip)
1348 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001349 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 else
1352 {
1353 *error = FALSE;
1354 if (!skip)
1355 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001356 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001357 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 }
1359 }
1360 if (skip)
1361 --emsg_skip;
1362
1363 return retval;
1364}
1365
1366/*
1367 * Top level evaluation function, returning a string. If "skip" is TRUE,
1368 * only parsing to "nextcmd" is done, without reporting errors. Return
1369 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1370 */
1371 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001372eval_to_string_skip(
1373 char_u *arg,
1374 char_u **nextcmd,
1375 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001376{
Bram Moolenaar33570922005-01-25 22:26:29 +00001377 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001378 char_u *retval;
1379
1380 if (skip)
1381 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001382 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383 retval = NULL;
1384 else
1385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001386 retval = vim_strsave(get_tv_string(&tv));
1387 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001388 }
1389 if (skip)
1390 --emsg_skip;
1391
1392 return retval;
1393}
1394
1395/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001396 * Skip over an expression at "*pp".
1397 * Return FAIL for an error, OK otherwise.
1398 */
1399 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001400skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001401{
Bram Moolenaar33570922005-01-25 22:26:29 +00001402 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403
1404 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001405 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001406}
1407
1408/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001410 * When "convert" is TRUE convert a List into a sequence of lines and convert
1411 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001412 * Return pointer to allocated memory, or NULL for failure.
1413 */
1414 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001415eval_to_string(
1416 char_u *arg,
1417 char_u **nextcmd,
1418 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419{
Bram Moolenaar33570922005-01-25 22:26:29 +00001420 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001422 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001423#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001424 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001427 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 retval = NULL;
1429 else
1430 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001431 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001432 {
1433 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001434 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001435 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001436 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001437 if (tv.vval.v_list->lv_len > 0)
1438 ga_append(&ga, NL);
1439 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001440 ga_append(&ga, NUL);
1441 retval = (char_u *)ga.ga_data;
1442 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001443#ifdef FEAT_FLOAT
1444 else if (convert && tv.v_type == VAR_FLOAT)
1445 {
1446 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1447 retval = vim_strsave(numbuf);
1448 }
1449#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001450 else
1451 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001452 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001453 }
1454
1455 return retval;
1456}
1457
1458/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001459 * Call eval_to_string() without using current local variables and using
1460 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001461 */
1462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001463eval_to_string_safe(
1464 char_u *arg,
1465 char_u **nextcmd,
1466 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001467{
1468 char_u *retval;
1469 void *save_funccalp;
1470
1471 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001472 if (use_sandbox)
1473 ++sandbox;
1474 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001475 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001476 if (use_sandbox)
1477 --sandbox;
1478 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001479 restore_funccal(save_funccalp);
1480 return retval;
1481}
1482
Bram Moolenaar071d4272004-06-13 20:20:40 +00001483/*
1484 * Top level evaluation function, returning a number.
1485 * Evaluates "expr" silently.
1486 * Returns -1 for an error.
1487 */
1488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001489eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490{
Bram Moolenaar33570922005-01-25 22:26:29 +00001491 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001492 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001493 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001494
1495 ++emsg_off;
1496
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001497 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001498 retval = -1;
1499 else
1500 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001501 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001502 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001503 }
1504 --emsg_off;
1505
1506 return retval;
1507}
1508
Bram Moolenaara40058a2005-07-11 22:42:07 +00001509/*
1510 * Prepare v: variable "idx" to be used.
1511 * Save the current typeval in "save_tv".
1512 * When not used yet add the variable to the v: hashtable.
1513 */
1514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001515prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516{
1517 *save_tv = vimvars[idx].vv_tv;
1518 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1519 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1520}
1521
1522/*
1523 * Restore v: variable "idx" to typeval "save_tv".
1524 * When no longer defined, remove the variable from the v: hashtable.
1525 */
1526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001527restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528{
1529 hashitem_T *hi;
1530
Bram Moolenaara40058a2005-07-11 22:42:07 +00001531 vimvars[idx].vv_tv = *save_tv;
1532 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1533 {
1534 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1535 if (HASHITEM_EMPTY(hi))
1536 EMSG2(_(e_intern2), "restore_vimvar()");
1537 else
1538 hash_remove(&vimvarht, hi);
1539 }
1540}
1541
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001542#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001543/*
1544 * Evaluate an expression to a list with suggestions.
1545 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001546 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547 */
1548 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001549eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001550{
1551 typval_T save_val;
1552 typval_T rettv;
1553 list_T *list = NULL;
1554 char_u *p = skipwhite(expr);
1555
1556 /* Set "v:val" to the bad word. */
1557 prepare_vimvar(VV_VAL, &save_val);
1558 vimvars[VV_VAL].vv_type = VAR_STRING;
1559 vimvars[VV_VAL].vv_str = badword;
1560 if (p_verbose == 0)
1561 ++emsg_off;
1562
1563 if (eval1(&p, &rettv, TRUE) == OK)
1564 {
1565 if (rettv.v_type != VAR_LIST)
1566 clear_tv(&rettv);
1567 else
1568 list = rettv.vval.v_list;
1569 }
1570
1571 if (p_verbose == 0)
1572 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001573 restore_vimvar(VV_VAL, &save_val);
1574
1575 return list;
1576}
1577
1578/*
1579 * "list" is supposed to contain two items: a word and a number. Return the
1580 * word in "pp" and the number as the return value.
1581 * Return -1 if anything isn't right.
1582 * Used to get the good word and score from the eval_spell_expr() result.
1583 */
1584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001585get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001586{
1587 listitem_T *li;
1588
1589 li = list->lv_first;
1590 if (li == NULL)
1591 return -1;
1592 *pp = get_tv_string(&li->li_tv);
1593
1594 li = li->li_next;
1595 if (li == NULL)
1596 return -1;
1597 return get_tv_number(&li->li_tv);
1598}
1599#endif
1600
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001601/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001602 * Top level evaluation function.
1603 * Returns an allocated typval_T with the result.
1604 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605 */
1606 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001607eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608{
1609 typval_T *tv;
1610
1611 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 {
1614 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001615 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001616 }
1617
1618 return tv;
1619}
1620
1621
Bram Moolenaar071d4272004-06-13 20:20:40 +00001622/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001624 * Uses argv[argc] for the function arguments. Only Number and String
1625 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001626 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001627 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001628 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001629call_vim_function(
1630 char_u *func,
1631 int argc,
1632 char_u **argv,
1633 int safe, /* use the sandbox */
1634 int str_arg_only, /* all arguments are strings */
1635 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001636{
Bram Moolenaar33570922005-01-25 22:26:29 +00001637 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001638 long n;
1639 int len;
1640 int i;
1641 int doesrange;
1642 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001643 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001644
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001645 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001646 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001647 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001648
1649 for (i = 0; i < argc; i++)
1650 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001651 /* Pass a NULL or empty argument as an empty string */
1652 if (argv[i] == NULL || *argv[i] == NUL)
1653 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001654 argvars[i].v_type = VAR_STRING;
1655 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001656 continue;
1657 }
1658
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001659 if (str_arg_only)
1660 len = 0;
1661 else
1662 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001663 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001664 if (len != 0 && len == (int)STRLEN(argv[i]))
1665 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001666 argvars[i].v_type = VAR_NUMBER;
1667 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001668 }
1669 else
1670 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001671 argvars[i].v_type = VAR_STRING;
1672 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001673 }
1674 }
1675
1676 if (safe)
1677 {
1678 save_funccalp = save_funccal();
1679 ++sandbox;
1680 }
1681
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001682 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1683 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001684 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001685 &doesrange, TRUE, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001686 if (safe)
1687 {
1688 --sandbox;
1689 restore_funccal(save_funccalp);
1690 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001691 vim_free(argvars);
1692
1693 if (ret == FAIL)
1694 clear_tv(rettv);
1695
1696 return ret;
1697}
1698
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001699/*
1700 * Call vimL function "func" and return the result as a number.
1701 * Returns -1 when calling the function fails.
1702 * Uses argv[argc] for the function arguments.
1703 */
1704 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001705call_func_retnr(
1706 char_u *func,
1707 int argc,
1708 char_u **argv,
1709 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001710{
1711 typval_T rettv;
1712 long retval;
1713
1714 /* All arguments are passed as strings, no conversion to number. */
1715 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1716 return -1;
1717
1718 retval = get_tv_number_chk(&rettv, NULL);
1719 clear_tv(&rettv);
1720 return retval;
1721}
1722
1723#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1724 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1725
Bram Moolenaar4f688582007-07-24 12:34:30 +00001726# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001728 * Call vimL function "func" and return the result as a string.
1729 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001730 * Uses argv[argc] for the function arguments.
1731 */
1732 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001733call_func_retstr(
1734 char_u *func,
1735 int argc,
1736 char_u **argv,
1737 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738{
1739 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001740 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001742 /* All arguments are passed as strings, no conversion to number. */
1743 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 return NULL;
1745
1746 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001747 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001748 return retval;
1749}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001750# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001752/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001753 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001755 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001756 */
1757 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001758call_func_retlist(
1759 char_u *func,
1760 int argc,
1761 char_u **argv,
1762 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763{
1764 typval_T rettv;
1765
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001766 /* All arguments are passed as strings, no conversion to number. */
1767 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001768 return NULL;
1769
1770 if (rettv.v_type != VAR_LIST)
1771 {
1772 clear_tv(&rettv);
1773 return NULL;
1774 }
1775
1776 return rettv.vval.v_list;
1777}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001778#endif
1779
1780/*
1781 * Save the current function call pointer, and set it to NULL.
1782 * Used when executing autocommands and for ":source".
1783 */
1784 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001785save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001787 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001788
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789 current_funccal = NULL;
1790 return (void *)fc;
1791}
1792
1793 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001794restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001796 funccall_T *fc = (funccall_T *)vfc;
1797
1798 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001799}
1800
Bram Moolenaar05159a02005-02-26 23:04:13 +00001801#if defined(FEAT_PROFILE) || defined(PROTO)
1802/*
1803 * Prepare profiling for entering a child or something else that is not
1804 * counted for the script/function itself.
1805 * Should always be called in pair with prof_child_exit().
1806 */
1807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001808prof_child_enter(
1809 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001810{
1811 funccall_T *fc = current_funccal;
1812
1813 if (fc != NULL && fc->func->uf_profiling)
1814 profile_start(&fc->prof_child);
1815 script_prof_save(tm);
1816}
1817
1818/*
1819 * Take care of time spent in a child.
1820 * Should always be called after prof_child_enter().
1821 */
1822 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001823prof_child_exit(
1824 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001825{
1826 funccall_T *fc = current_funccal;
1827
1828 if (fc != NULL && fc->func->uf_profiling)
1829 {
1830 profile_end(&fc->prof_child);
1831 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1832 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1833 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1834 }
1835 script_prof_restore(tm);
1836}
1837#endif
1838
1839
Bram Moolenaar071d4272004-06-13 20:20:40 +00001840#ifdef FEAT_FOLDING
1841/*
1842 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1843 * it in "*cp". Doesn't give error messages.
1844 */
1845 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001846eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847{
Bram Moolenaar33570922005-01-25 22:26:29 +00001848 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001849 int retval;
1850 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001851 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1852 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001853
1854 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001855 if (use_sandbox)
1856 ++sandbox;
1857 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001858 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001859 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860 retval = 0;
1861 else
1862 {
1863 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001864 if (tv.v_type == VAR_NUMBER)
1865 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001866 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 retval = 0;
1868 else
1869 {
1870 /* If the result is a string, check if there is a non-digit before
1871 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001872 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001873 if (!VIM_ISDIGIT(*s) && *s != '-')
1874 *cp = *s++;
1875 retval = atol((char *)s);
1876 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001877 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001878 }
1879 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001880 if (use_sandbox)
1881 --sandbox;
1882 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001883
1884 return retval;
1885}
1886#endif
1887
Bram Moolenaar071d4272004-06-13 20:20:40 +00001888/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001889 * ":let" list all variable values
1890 * ":let var1 var2" list variable values
1891 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001892 * ":let var += expr" assignment command.
1893 * ":let var -= expr" assignment command.
1894 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001895 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896 */
1897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001898ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899{
1900 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001902 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001904 int var_count = 0;
1905 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001906 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001908 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909
Bram Moolenaardb552d602006-03-23 22:59:57 +00001910 argend = skip_var_list(arg, &var_count, &semicolon);
1911 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001912 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001913 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1914 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001915 expr = skipwhite(argend);
1916 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1917 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001919 /*
1920 * ":let" without "=": list variables
1921 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001922 if (*arg == '[')
1923 EMSG(_(e_invarg));
1924 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001925 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001926 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001927 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001928 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001929 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001930 list_glob_vars(&first);
1931 list_buf_vars(&first);
1932 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001933#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001934 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001935#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001936 list_script_vars(&first);
1937 list_func_vars(&first);
1938 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001940 eap->nextcmd = check_nextcmd(arg);
1941 }
1942 else
1943 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 op[0] = '=';
1945 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001946 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001947 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001948 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1949 op[0] = *expr; /* +=, -= or .= */
1950 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001952 else
1953 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001954
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001957 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001958 if (eap->skip)
1959 {
1960 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 --emsg_skip;
1963 }
1964 else if (i != FAIL)
1965 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001966 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001967 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 }
1970 }
1971}
1972
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973/*
1974 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1975 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001976 * When "nextchars" is not NULL it points to a string with characters that
1977 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1978 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001979 * Returns OK or FAIL;
1980 */
1981 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001982ex_let_vars(
1983 char_u *arg_start,
1984 typval_T *tv,
1985 int copy, /* copy values from "tv", don't move */
1986 int semicolon, /* from skip_var_list() */
1987 int var_count, /* from skip_var_list() */
1988 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989{
1990 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001991 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001993 listitem_T *item;
1994 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001995
1996 if (*arg != '[')
1997 {
1998 /*
1999 * ":let var = expr" or ":for var in list"
2000 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002001 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002 return FAIL;
2003 return OK;
2004 }
2005
2006 /*
2007 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2008 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002009 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002010 {
2011 EMSG(_(e_listreq));
2012 return FAIL;
2013 }
2014
2015 i = list_len(l);
2016 if (semicolon == 0 && var_count < i)
2017 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002018 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002019 return FAIL;
2020 }
2021 if (var_count - semicolon > i)
2022 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002023 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002024 return FAIL;
2025 }
2026
2027 item = l->lv_first;
2028 while (*arg != ']')
2029 {
2030 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002031 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002032 item = item->li_next;
2033 if (arg == NULL)
2034 return FAIL;
2035
2036 arg = skipwhite(arg);
2037 if (*arg == ';')
2038 {
2039 /* Put the rest of the list (may be empty) in the var after ';'.
2040 * Create a new list for this. */
2041 l = list_alloc();
2042 if (l == NULL)
2043 return FAIL;
2044 while (item != NULL)
2045 {
2046 list_append_tv(l, &item->li_tv);
2047 item = item->li_next;
2048 }
2049
2050 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002051 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002052 ltv.vval.v_list = l;
2053 l->lv_refcount = 1;
2054
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002055 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2056 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002057 clear_tv(&ltv);
2058 if (arg == NULL)
2059 return FAIL;
2060 break;
2061 }
2062 else if (*arg != ',' && *arg != ']')
2063 {
2064 EMSG2(_(e_intern2), "ex_let_vars()");
2065 return FAIL;
2066 }
2067 }
2068
2069 return OK;
2070}
2071
2072/*
2073 * Skip over assignable variable "var" or list of variables "[var, var]".
2074 * Used for ":let varvar = expr" and ":for varvar in expr".
2075 * For "[var, var]" increment "*var_count" for each variable.
2076 * for "[var, var; var]" set "semicolon".
2077 * Return NULL for an error.
2078 */
2079 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002080skip_var_list(
2081 char_u *arg,
2082 int *var_count,
2083 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002084{
2085 char_u *p, *s;
2086
2087 if (*arg == '[')
2088 {
2089 /* "[var, var]": find the matching ']'. */
2090 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002091 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002092 {
2093 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2094 s = skip_var_one(p);
2095 if (s == p)
2096 {
2097 EMSG2(_(e_invarg2), p);
2098 return NULL;
2099 }
2100 ++*var_count;
2101
2102 p = skipwhite(s);
2103 if (*p == ']')
2104 break;
2105 else if (*p == ';')
2106 {
2107 if (*semicolon == 1)
2108 {
2109 EMSG(_("Double ; in list of variables"));
2110 return NULL;
2111 }
2112 *semicolon = 1;
2113 }
2114 else if (*p != ',')
2115 {
2116 EMSG2(_(e_invarg2), p);
2117 return NULL;
2118 }
2119 }
2120 return p + 1;
2121 }
2122 else
2123 return skip_var_one(arg);
2124}
2125
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002127 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002128 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002129 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002130 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002131skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002132{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002133 if (*arg == '@' && arg[1] != NUL)
2134 return arg + 2;
2135 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2136 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137}
2138
Bram Moolenaara7043832005-01-21 11:56:39 +00002139/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002140 * List variables for hashtab "ht" with prefix "prefix".
2141 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002142 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002144list_hashtable_vars(
2145 hashtab_T *ht,
2146 char_u *prefix,
2147 int empty,
2148 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002149{
Bram Moolenaar33570922005-01-25 22:26:29 +00002150 hashitem_T *hi;
2151 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 int todo;
2153
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002154 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002155 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2156 {
2157 if (!HASHITEM_EMPTY(hi))
2158 {
2159 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002160 di = HI2DI(hi);
2161 if (empty || di->di_tv.v_type != VAR_STRING
2162 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002163 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002164 }
2165 }
2166}
2167
2168/*
2169 * List global variables.
2170 */
2171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002172list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002173{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002174 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002175}
2176
2177/*
2178 * List buffer variables.
2179 */
2180 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002181list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002182{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002183 char_u numbuf[NUMBUFLEN];
2184
Bram Moolenaar429fa852013-04-15 12:27:36 +02002185 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002187
2188 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002189 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2190 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002191}
2192
2193/*
2194 * List window variables.
2195 */
2196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002197list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002198{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002199 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002200 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002201}
2202
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002203#ifdef FEAT_WINDOWS
2204/*
2205 * List tab page variables.
2206 */
2207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002208list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002210 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002211 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002212}
2213#endif
2214
Bram Moolenaara7043832005-01-21 11:56:39 +00002215/*
2216 * List Vim variables.
2217 */
2218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002219list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002220{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002221 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002222}
2223
2224/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002225 * List script-local variables, if there is a script.
2226 */
2227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002228list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002229{
2230 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002231 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2232 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002233}
2234
2235/*
2236 * List function variables, if there is a function.
2237 */
2238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002239list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002240{
2241 if (current_funccal != NULL)
2242 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002243 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002244}
2245
2246/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002247 * List variables in "arg".
2248 */
2249 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002250list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251{
2252 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002253 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002255 char_u *name_start;
2256 char_u *arg_subsc;
2257 char_u *tofree;
2258 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002259
2260 while (!ends_excmd(*arg) && !got_int)
2261 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002263 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002264 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002265 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2266 {
2267 emsg_severe = TRUE;
2268 EMSG(_(e_trailing));
2269 break;
2270 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002271 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002273 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002274 /* get_name_len() takes care of expanding curly braces */
2275 name_start = name = arg;
2276 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2277 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 /* This is mainly to keep test 49 working: when expanding
2280 * curly braces fails overrule the exception error message. */
2281 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002282 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002283 emsg_severe = TRUE;
2284 EMSG2(_(e_invarg2), arg);
2285 break;
2286 }
2287 error = TRUE;
2288 }
2289 else
2290 {
2291 if (tofree != NULL)
2292 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002293 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002294 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 else
2296 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002297 /* handle d.key, l[idx], f(expr) */
2298 arg_subsc = arg;
2299 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002300 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002302 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002303 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002304 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002305 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002306 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002307 case 'g': list_glob_vars(first); break;
2308 case 'b': list_buf_vars(first); break;
2309 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002310#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002311 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002312#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002313 case 'v': list_vim_vars(first); break;
2314 case 's': list_script_vars(first); break;
2315 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002316 default:
2317 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002318 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
2320 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002321 {
2322 char_u numbuf[NUMBUFLEN];
2323 char_u *tf;
2324 int c;
2325 char_u *s;
2326
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002327 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 c = *arg;
2329 *arg = NUL;
2330 list_one_var_a((char_u *)"",
2331 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002332 tv.v_type,
2333 s == NULL ? (char_u *)"" : s,
2334 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 *arg = c;
2336 vim_free(tf);
2337 }
2338 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002339 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002340 }
2341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002345
2346 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348
2349 return arg;
2350}
2351
2352/*
2353 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2354 * Returns a pointer to the char just after the var name.
2355 * Returns NULL if there is an error.
2356 */
2357 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002358ex_let_one(
2359 char_u *arg, /* points to variable name */
2360 typval_T *tv, /* value to assign to variable */
2361 int copy, /* copy value from "tv" */
2362 char_u *endchars, /* valid chars after variable name or NULL */
2363 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002364{
2365 int c1;
2366 char_u *name;
2367 char_u *p;
2368 char_u *arg_end = NULL;
2369 int len;
2370 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002371 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002372
2373 /*
2374 * ":let $VAR = expr": Set environment variable.
2375 */
2376 if (*arg == '$')
2377 {
2378 /* Find the end of the name. */
2379 ++arg;
2380 name = arg;
2381 len = get_env_len(&arg);
2382 if (len == 0)
2383 EMSG2(_(e_invarg2), name - 1);
2384 else
2385 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002386 if (op != NULL && (*op == '+' || *op == '-'))
2387 EMSG2(_(e_letwrong), op);
2388 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002389 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002390 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002391 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002392 {
2393 c1 = name[len];
2394 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002395 p = get_tv_string_chk(tv);
2396 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002397 {
2398 int mustfree = FALSE;
2399 char_u *s = vim_getenv(name, &mustfree);
2400
2401 if (s != NULL)
2402 {
2403 p = tofree = concat_str(s, p);
2404 if (mustfree)
2405 vim_free(s);
2406 }
2407 }
2408 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002409 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002410 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002411 if (STRICMP(name, "HOME") == 0)
2412 init_homedir();
2413 else if (didset_vim && STRICMP(name, "VIM") == 0)
2414 didset_vim = FALSE;
2415 else if (didset_vimruntime
2416 && STRICMP(name, "VIMRUNTIME") == 0)
2417 didset_vimruntime = FALSE;
2418 arg_end = arg;
2419 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002420 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002421 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002422 }
2423 }
2424 }
2425
2426 /*
2427 * ":let &option = expr": Set option value.
2428 * ":let &l:option = expr": Set local option value.
2429 * ":let &g:option = expr": Set global option value.
2430 */
2431 else if (*arg == '&')
2432 {
2433 /* Find the end of the name. */
2434 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002435 if (p == NULL || (endchars != NULL
2436 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002437 EMSG(_(e_letunexp));
2438 else
2439 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002440 long n;
2441 int opt_type;
2442 long numval;
2443 char_u *stringval = NULL;
2444 char_u *s;
2445
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002446 c1 = *p;
2447 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002448
2449 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002450 s = get_tv_string_chk(tv); /* != NULL if number or string */
2451 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002452 {
2453 opt_type = get_option_value(arg, &numval,
2454 &stringval, opt_flags);
2455 if ((opt_type == 1 && *op == '.')
2456 || (opt_type == 0 && *op != '.'))
2457 EMSG2(_(e_letwrong), op);
2458 else
2459 {
2460 if (opt_type == 1) /* number */
2461 {
2462 if (*op == '+')
2463 n = numval + n;
2464 else
2465 n = numval - n;
2466 }
2467 else if (opt_type == 0 && stringval != NULL) /* string */
2468 {
2469 s = concat_str(stringval, s);
2470 vim_free(stringval);
2471 stringval = s;
2472 }
2473 }
2474 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002475 if (s != NULL)
2476 {
2477 set_option_value(arg, n, s, opt_flags);
2478 arg_end = p;
2479 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002480 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002481 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002482 }
2483 }
2484
2485 /*
2486 * ":let @r = expr": Set register contents.
2487 */
2488 else if (*arg == '@')
2489 {
2490 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002491 if (op != NULL && (*op == '+' || *op == '-'))
2492 EMSG2(_(e_letwrong), op);
2493 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002494 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002495 EMSG(_(e_letunexp));
2496 else
2497 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002498 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002499 char_u *s;
2500
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002501 p = get_tv_string_chk(tv);
2502 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002503 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002504 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 if (s != NULL)
2506 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002507 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002508 vim_free(s);
2509 }
2510 }
2511 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002512 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002513 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002514 arg_end = arg + 1;
2515 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002516 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002517 }
2518 }
2519
2520 /*
2521 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002522 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002523 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002524 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002525 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002526 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002528 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002531 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2532 EMSG(_(e_letunexp));
2533 else
2534 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002535 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 arg_end = p;
2537 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002538 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002539 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002540 }
2541
2542 else
2543 EMSG2(_(e_invarg2), arg);
2544
2545 return arg_end;
2546}
2547
2548/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002549 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2550 */
2551 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002552check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002553{
2554 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2555 {
2556 EMSG2(_(e_readonlyvar), arg);
2557 return TRUE;
2558 }
2559 return FALSE;
2560}
2561
2562/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002563 * Get an lval: variable, Dict item or List item that can be assigned a value
2564 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2565 * "name.key", "name.key[expr]" etc.
2566 * Indexing only works if "name" is an existing List or Dictionary.
2567 * "name" points to the start of the name.
2568 * If "rettv" is not NULL it points to the value to be assigned.
2569 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2570 * wrong; must end in space or cmd separator.
2571 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002572 * flags:
2573 * GLV_QUIET: do not give error messages
2574 * GLV_NO_AUTOLOAD: do not use script autoloading
2575 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002576 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002577 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002578 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002579 */
2580 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002581get_lval(
2582 char_u *name,
2583 typval_T *rettv,
2584 lval_T *lp,
2585 int unlet,
2586 int skip,
2587 int flags, /* GLV_ values */
2588 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002589{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002591 char_u *expr_start, *expr_end;
2592 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002593 dictitem_T *v;
2594 typval_T var1;
2595 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002596 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002598 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002601 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002602
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002605
2606 if (skip)
2607 {
2608 /* When skipping just find the end of the name. */
2609 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002610 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002611 }
2612
2613 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002614 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002615 if (expr_start != NULL)
2616 {
2617 /* Don't expand the name when we already know there is an error. */
2618 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2619 && *p != '[' && *p != '.')
2620 {
2621 EMSG(_(e_trailing));
2622 return NULL;
2623 }
2624
2625 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2626 if (lp->ll_exp_name == NULL)
2627 {
2628 /* Report an invalid expression in braces, unless the
2629 * expression evaluation has been cancelled due to an
2630 * aborting error, an interrupt, or an exception. */
2631 if (!aborting() && !quiet)
2632 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002633 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002634 EMSG2(_(e_invarg2), name);
2635 return NULL;
2636 }
2637 }
2638 lp->ll_name = lp->ll_exp_name;
2639 }
2640 else
2641 lp->ll_name = name;
2642
2643 /* Without [idx] or .key we are done. */
2644 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2645 return p;
2646
2647 cc = *p;
2648 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002649 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002650 if (v == NULL && !quiet)
2651 EMSG2(_(e_undefvar), lp->ll_name);
2652 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002653 if (v == NULL)
2654 return NULL;
2655
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002656 /*
2657 * Loop until no more [idx] or .key is following.
2658 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002659 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002660 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002661 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002662 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2663 && !(lp->ll_tv->v_type == VAR_DICT
2664 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002665 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002666 if (!quiet)
2667 EMSG(_("E689: Can only index a List or Dictionary"));
2668 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002669 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002670 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002671 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002672 if (!quiet)
2673 EMSG(_("E708: [:] must come last"));
2674 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002675 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002676
Bram Moolenaar8c711452005-01-14 21:53:12 +00002677 len = -1;
2678 if (*p == '.')
2679 {
2680 key = p + 1;
2681 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2682 ;
2683 if (len == 0)
2684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002685 if (!quiet)
2686 EMSG(_(e_emptykey));
2687 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002688 }
2689 p = key + len;
2690 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 else
2692 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002693 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 if (*p == ':')
2696 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002697 else
2698 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002699 empty1 = FALSE;
2700 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002701 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002702 if (get_tv_string_chk(&var1) == NULL)
2703 {
2704 /* not a number or string */
2705 clear_tv(&var1);
2706 return NULL;
2707 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002708 }
2709
2710 /* Optionally get the second index [ :expr]. */
2711 if (*p == ':')
2712 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002713 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002714 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002715 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002716 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 if (!empty1)
2718 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002719 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002720 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (rettv != NULL && (rettv->v_type != VAR_LIST
2722 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002724 if (!quiet)
2725 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 if (!empty1)
2727 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002729 }
2730 p = skipwhite(p + 1);
2731 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 else
2734 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2737 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 if (!empty1)
2739 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002740 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002741 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002742 if (get_tv_string_chk(&var2) == NULL)
2743 {
2744 /* not a number or string */
2745 if (!empty1)
2746 clear_tv(&var1);
2747 clear_tv(&var2);
2748 return NULL;
2749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002754 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002755
Bram Moolenaar8c711452005-01-14 21:53:12 +00002756 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002757 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 if (!quiet)
2759 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 if (!empty1)
2761 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002762 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002764 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002765 }
2766
2767 /* Skip to past ']'. */
2768 ++p;
2769 }
2770
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 {
2773 if (len == -1)
2774 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002775 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002776 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002777 if (*key == NUL)
2778 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 if (!quiet)
2780 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002781 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002783 }
2784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002785 lp->ll_list = NULL;
2786 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002787 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002788
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002789 /* When assigning to a scope dictionary check that a function and
2790 * variable name is valid (only variable name unless it is l: or
2791 * g: dictionary). Disallow overwriting a builtin function. */
2792 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002793 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002794 int prevval;
2795 int wrong;
2796
2797 if (len != -1)
2798 {
2799 prevval = key[len];
2800 key[len] = NUL;
2801 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002802 else
2803 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2805 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002806 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002807 || !valid_varname(key);
2808 if (len != -1)
2809 key[len] = prevval;
2810 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002811 return NULL;
2812 }
2813
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002814 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002815 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002816 /* Can't add "v:" variable. */
2817 if (lp->ll_dict == &vimvardict)
2818 {
2819 EMSG2(_(e_illvar), name);
2820 return NULL;
2821 }
2822
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002823 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002824 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002826 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002827 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 if (len == -1)
2829 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 }
2832 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002834 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 if (len == -1)
2837 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002838 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002839 p = NULL;
2840 break;
2841 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002842 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002843 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002844 return NULL;
2845
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 if (len == -1)
2847 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002848 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002849 }
2850 else
2851 {
2852 /*
2853 * Get the number and item for the only or first index of the List.
2854 */
2855 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002856 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 else
2858 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002859 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002860 clear_tv(&var1);
2861 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002862 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_list = lp->ll_tv->vval.v_list;
2864 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2865 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002866 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002867 if (lp->ll_n1 < 0)
2868 {
2869 lp->ll_n1 = 0;
2870 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2871 }
2872 }
2873 if (lp->ll_li == NULL)
2874 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002875 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002876 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002877 if (!quiet)
2878 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002879 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002880 }
2881
2882 /*
2883 * May need to find the item or absolute index for the second
2884 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 * When no index given: "lp->ll_empty2" is TRUE.
2886 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002888 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002889 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002890 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002891 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002893 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002894 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002895 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002896 {
2897 if (!quiet)
2898 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002900 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 }
2903
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002904 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2905 if (lp->ll_n1 < 0)
2906 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2907 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002908 {
2909 if (!quiet)
2910 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002912 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
2914
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002915 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002916 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002917 }
2918
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002919 return p;
2920}
2921
2922/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002923 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924 */
2925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002926clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002927{
2928 vim_free(lp->ll_exp_name);
2929 vim_free(lp->ll_newkey);
2930}
2931
2932/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002933 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002935 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002936 */
2937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002938set_var_lval(
2939 lval_T *lp,
2940 char_u *endp,
2941 typval_T *rettv,
2942 int copy,
2943 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002944{
2945 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002946 listitem_T *ri;
2947 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948
2949 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002950 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002952 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002953 cc = *endp;
2954 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955 if (op != NULL && *op != '=')
2956 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002957 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002958
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002959 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002960 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002961 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002962 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002963 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002964 if ((di == NULL
2965 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2966 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2967 FALSE)))
2968 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002969 set_var(lp->ll_name, &tv, FALSE);
2970 clear_tv(&tv);
2971 }
2972 }
2973 else
2974 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002975 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002976 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002977 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 else if (tv_check_lock(lp->ll_newkey == NULL
2979 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002980 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002981 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 else if (lp->ll_range)
2983 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002984 listitem_T *ll_li = lp->ll_li;
2985 int ll_n1 = lp->ll_n1;
2986
2987 /*
2988 * Check whether any of the list items is locked
2989 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002990 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002992 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002993 return;
2994 ri = ri->li_next;
2995 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2996 break;
2997 ll_li = ll_li->li_next;
2998 ++ll_n1;
2999 }
3000
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003001 /*
3002 * Assign the List values to the list items.
3003 */
3004 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003006 if (op != NULL && *op != '=')
3007 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3008 else
3009 {
3010 clear_tv(&lp->ll_li->li_tv);
3011 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3012 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003013 ri = ri->li_next;
3014 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3015 break;
3016 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003019 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003021 ri = NULL;
3022 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003023 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 lp->ll_li = lp->ll_li->li_next;
3026 ++lp->ll_n1;
3027 }
3028 if (ri != NULL)
3029 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003030 else if (lp->ll_empty2
3031 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 : lp->ll_n1 != lp->ll_n2)
3033 EMSG(_("E711: List value has not enough items"));
3034 }
3035 else
3036 {
3037 /*
3038 * Assign to a List or Dictionary item.
3039 */
3040 if (lp->ll_newkey != NULL)
3041 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003042 if (op != NULL && *op != '=')
3043 {
3044 EMSG2(_(e_letwrong), op);
3045 return;
3046 }
3047
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003048 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003050 if (di == NULL)
3051 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003052 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3053 {
3054 vim_free(di);
3055 return;
3056 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003058 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003059 else if (op != NULL && *op != '=')
3060 {
3061 tv_op(lp->ll_tv, rettv, op);
3062 return;
3063 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003064 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003065 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003066
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003067 /*
3068 * Assign the value to the variable or list item.
3069 */
3070 if (copy)
3071 copy_tv(rettv, lp->ll_tv);
3072 else
3073 {
3074 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003075 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003076 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003077 }
3078 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003079}
3080
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003081/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003082 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3083 * Returns OK or FAIL.
3084 */
3085 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003086tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003087{
3088 long n;
3089 char_u numbuf[NUMBUFLEN];
3090 char_u *s;
3091
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003092 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3093 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3094 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003095 {
3096 switch (tv1->v_type)
3097 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003098 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003099 case VAR_DICT:
3100 case VAR_FUNC:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003101 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003102 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003103 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003104 break;
3105
3106 case VAR_LIST:
3107 if (*op != '+' || tv2->v_type != VAR_LIST)
3108 break;
3109 /* List += List */
3110 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3111 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3112 return OK;
3113
3114 case VAR_NUMBER:
3115 case VAR_STRING:
3116 if (tv2->v_type == VAR_LIST)
3117 break;
3118 if (*op == '+' || *op == '-')
3119 {
3120 /* nr += nr or nr -= nr*/
3121 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003122#ifdef FEAT_FLOAT
3123 if (tv2->v_type == VAR_FLOAT)
3124 {
3125 float_T f = n;
3126
3127 if (*op == '+')
3128 f += tv2->vval.v_float;
3129 else
3130 f -= tv2->vval.v_float;
3131 clear_tv(tv1);
3132 tv1->v_type = VAR_FLOAT;
3133 tv1->vval.v_float = f;
3134 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003135 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003136#endif
3137 {
3138 if (*op == '+')
3139 n += get_tv_number(tv2);
3140 else
3141 n -= get_tv_number(tv2);
3142 clear_tv(tv1);
3143 tv1->v_type = VAR_NUMBER;
3144 tv1->vval.v_number = n;
3145 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003146 }
3147 else
3148 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003149 if (tv2->v_type == VAR_FLOAT)
3150 break;
3151
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003152 /* str .= str */
3153 s = get_tv_string(tv1);
3154 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3155 clear_tv(tv1);
3156 tv1->v_type = VAR_STRING;
3157 tv1->vval.v_string = s;
3158 }
3159 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003160
3161#ifdef FEAT_FLOAT
3162 case VAR_FLOAT:
3163 {
3164 float_T f;
3165
3166 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3167 && tv2->v_type != VAR_NUMBER
3168 && tv2->v_type != VAR_STRING))
3169 break;
3170 if (tv2->v_type == VAR_FLOAT)
3171 f = tv2->vval.v_float;
3172 else
3173 f = get_tv_number(tv2);
3174 if (*op == '+')
3175 tv1->vval.v_float += f;
3176 else
3177 tv1->vval.v_float -= f;
3178 }
3179 return OK;
3180#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003181 }
3182 }
3183
3184 EMSG2(_(e_letwrong), op);
3185 return FAIL;
3186}
3187
3188/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003189 * Add a watcher to a list.
3190 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003191 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003192list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003193{
3194 lw->lw_next = l->lv_watch;
3195 l->lv_watch = lw;
3196}
3197
3198/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003199 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003200 * No warning when it isn't found...
3201 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003202 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003203list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204{
Bram Moolenaar33570922005-01-25 22:26:29 +00003205 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206
3207 lwp = &l->lv_watch;
3208 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3209 {
3210 if (lw == lwrem)
3211 {
3212 *lwp = lw->lw_next;
3213 break;
3214 }
3215 lwp = &lw->lw_next;
3216 }
3217}
3218
3219/*
3220 * Just before removing an item from a list: advance watchers to the next
3221 * item.
3222 */
3223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003224list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225{
Bram Moolenaar33570922005-01-25 22:26:29 +00003226 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227
3228 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3229 if (lw->lw_item == item)
3230 lw->lw_item = item->li_next;
3231}
3232
3233/*
3234 * Evaluate the expression used in a ":for var in expr" command.
3235 * "arg" points to "var".
3236 * Set "*errp" to TRUE for an error, FALSE otherwise;
3237 * Return a pointer that holds the info. Null when there is an error.
3238 */
3239 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003240eval_for_line(
3241 char_u *arg,
3242 int *errp,
3243 char_u **nextcmdp,
3244 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245{
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 typval_T tv;
3249 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003250
3251 *errp = TRUE; /* default: there is an error */
3252
Bram Moolenaar33570922005-01-25 22:26:29 +00003253 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003254 if (fi == NULL)
3255 return NULL;
3256
3257 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3258 if (expr == NULL)
3259 return fi;
3260
3261 expr = skipwhite(expr);
3262 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3263 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003264 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003265 return fi;
3266 }
3267
3268 if (skip)
3269 ++emsg_skip;
3270 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3271 {
3272 *errp = FALSE;
3273 if (!skip)
3274 {
3275 l = tv.vval.v_list;
3276 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003278 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 clear_tv(&tv);
3280 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003281 else
3282 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003283 /* No need to increment the refcount, it's already set for the
3284 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003285 fi->fi_list = l;
3286 list_add_watch(l, &fi->fi_lw);
3287 fi->fi_lw.lw_item = l->lv_first;
3288 }
3289 }
3290 }
3291 if (skip)
3292 --emsg_skip;
3293
3294 return fi;
3295}
3296
3297/*
3298 * Use the first item in a ":for" list. Advance to the next.
3299 * Assign the values to the variable (list). "arg" points to the first one.
3300 * Return TRUE when a valid item was found, FALSE when at end of list or
3301 * something wrong.
3302 */
3303 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003304next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003306 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003308 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309
3310 item = fi->fi_lw.lw_item;
3311 if (item == NULL)
3312 result = FALSE;
3313 else
3314 {
3315 fi->fi_lw.lw_item = item->li_next;
3316 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3317 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3318 }
3319 return result;
3320}
3321
3322/*
3323 * Free the structure used to store info used by ":for".
3324 */
3325 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003326free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327{
Bram Moolenaar33570922005-01-25 22:26:29 +00003328 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003330 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003332 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 list_unref(fi->fi_list);
3334 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335 vim_free(fi);
3336}
3337
Bram Moolenaar071d4272004-06-13 20:20:40 +00003338#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3339
3340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003341set_context_for_expression(
3342 expand_T *xp,
3343 char_u *arg,
3344 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003345{
3346 int got_eq = FALSE;
3347 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003349
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 if (cmdidx == CMD_let)
3351 {
3352 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003353 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003354 {
3355 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003356 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003357 {
3358 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003359 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003360 if (vim_iswhite(*p))
3361 break;
3362 }
3363 return;
3364 }
3365 }
3366 else
3367 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3368 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003369 while ((xp->xp_pattern = vim_strpbrk(arg,
3370 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3371 {
3372 c = *xp->xp_pattern;
3373 if (c == '&')
3374 {
3375 c = xp->xp_pattern[1];
3376 if (c == '&')
3377 {
3378 ++xp->xp_pattern;
3379 xp->xp_context = cmdidx != CMD_let || got_eq
3380 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3381 }
3382 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003384 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3386 xp->xp_pattern += 2;
3387
3388 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003389 }
3390 else if (c == '$')
3391 {
3392 /* environment variable */
3393 xp->xp_context = EXPAND_ENV_VARS;
3394 }
3395 else if (c == '=')
3396 {
3397 got_eq = TRUE;
3398 xp->xp_context = EXPAND_EXPRESSION;
3399 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003400 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 && xp->xp_context == EXPAND_FUNCTIONS
3402 && vim_strchr(xp->xp_pattern, '(') == NULL)
3403 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003404 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003405 break;
3406 }
3407 else if (cmdidx != CMD_let || got_eq)
3408 {
3409 if (c == '"') /* string */
3410 {
3411 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3412 if (c == '\\' && xp->xp_pattern[1] != NUL)
3413 ++xp->xp_pattern;
3414 xp->xp_context = EXPAND_NOTHING;
3415 }
3416 else if (c == '\'') /* literal string */
3417 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003418 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3420 /* skip */ ;
3421 xp->xp_context = EXPAND_NOTHING;
3422 }
3423 else if (c == '|')
3424 {
3425 if (xp->xp_pattern[1] == '|')
3426 {
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_EXPRESSION;
3429 }
3430 else
3431 xp->xp_context = EXPAND_COMMANDS;
3432 }
3433 else
3434 xp->xp_context = EXPAND_EXPRESSION;
3435 }
3436 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003437 /* Doesn't look like something valid, expand as an expression
3438 * anyway. */
3439 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003440 arg = xp->xp_pattern;
3441 if (*arg != NUL)
3442 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3443 /* skip */ ;
3444 }
3445 xp->xp_pattern = arg;
3446}
3447
3448#endif /* FEAT_CMDL_COMPL */
3449
3450/*
3451 * ":1,25call func(arg1, arg2)" function call.
3452 */
3453 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003454ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003455{
3456 char_u *arg = eap->arg;
3457 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003459 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003461 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003462 linenr_T lnum;
3463 int doesrange;
3464 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003465 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003467 if (eap->skip)
3468 {
3469 /* trans_function_name() doesn't work well when skipping, use eval0()
3470 * instead to skip to any following command, e.g. for:
3471 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003472 ++emsg_skip;
3473 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3474 clear_tv(&rettv);
3475 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003476 return;
3477 }
3478
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003479 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003480 if (fudi.fd_newkey != NULL)
3481 {
3482 /* Still need to give an error message for missing key. */
3483 EMSG2(_(e_dictkey), fudi.fd_newkey);
3484 vim_free(fudi.fd_newkey);
3485 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003486 if (tofree == NULL)
3487 return;
3488
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003489 /* Increase refcount on dictionary, it could get deleted when evaluating
3490 * the arguments. */
3491 if (fudi.fd_dict != NULL)
3492 ++fudi.fd_dict->dv_refcount;
3493
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003494 /* If it is the name of a variable of type VAR_FUNC use its contents. */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003495 len = (int)STRLEN(tofree);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01003496 name = deref_func_name(tofree, &len, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
Bram Moolenaar532c7802005-01-27 14:44:31 +00003498 /* Skip white space to allow ":call func ()". Not good, but required for
3499 * backward compatibility. */
3500 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003501 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003502
3503 if (*startarg != '(')
3504 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003505 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003506 goto end;
3507 }
3508
3509 /*
3510 * When skipping, evaluate the function once, to find the end of the
3511 * arguments.
3512 * When the function takes a range, this is discovered after the first
3513 * call, and the loop is broken.
3514 */
3515 if (eap->skip)
3516 {
3517 ++emsg_skip;
3518 lnum = eap->line2; /* do it once, also with an invalid range */
3519 }
3520 else
3521 lnum = eap->line1;
3522 for ( ; lnum <= eap->line2; ++lnum)
3523 {
3524 if (!eap->skip && eap->addr_count > 0)
3525 {
3526 curwin->w_cursor.lnum = lnum;
3527 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003528#ifdef FEAT_VIRTUALEDIT
3529 curwin->w_cursor.coladd = 0;
3530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003531 }
3532 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003533 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003534 eap->line1, eap->line2, &doesrange,
3535 !eap->skip, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 {
3537 failed = TRUE;
3538 break;
3539 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003540
3541 /* Handle a function returning a Funcref, Dictionary or List. */
3542 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3543 {
3544 failed = TRUE;
3545 break;
3546 }
3547
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003548 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 if (doesrange || eap->skip)
3550 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003551
Bram Moolenaar071d4272004-06-13 20:20:40 +00003552 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003553 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003554 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003555 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003556 if (aborting())
3557 break;
3558 }
3559 if (eap->skip)
3560 --emsg_skip;
3561
3562 if (!failed)
3563 {
3564 /* Check for trailing illegal characters and a following command. */
3565 if (!ends_excmd(*arg))
3566 {
3567 emsg_severe = TRUE;
3568 EMSG(_(e_trailing));
3569 }
3570 else
3571 eap->nextcmd = check_nextcmd(arg);
3572 }
3573
3574end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003575 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003576 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003577}
3578
3579/*
3580 * ":unlet[!] var1 ... " command.
3581 */
3582 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003583ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003584{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003585 ex_unletlock(eap, eap->arg, 0);
3586}
3587
3588/*
3589 * ":lockvar" and ":unlockvar" commands
3590 */
3591 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003592ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003594 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003595 int deep = 2;
3596
3597 if (eap->forceit)
3598 deep = -1;
3599 else if (vim_isdigit(*arg))
3600 {
3601 deep = getdigits(&arg);
3602 arg = skipwhite(arg);
3603 }
3604
3605 ex_unletlock(eap, arg, deep);
3606}
3607
3608/*
3609 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3610 */
3611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003612ex_unletlock(
3613 exarg_T *eap,
3614 char_u *argstart,
3615 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003616{
3617 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003618 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003620 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003621
3622 do
3623 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003624 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003625 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003626 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003627 if (lv.ll_name == NULL)
3628 error = TRUE; /* error but continue parsing */
3629 if (name_end == NULL || (!vim_iswhite(*name_end)
3630 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (name_end != NULL)
3633 {
3634 emsg_severe = TRUE;
3635 EMSG(_(e_trailing));
3636 }
3637 if (!(eap->skip || error))
3638 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 break;
3640 }
3641
3642 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003643 {
3644 if (eap->cmdidx == CMD_unlet)
3645 {
3646 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3647 error = TRUE;
3648 }
3649 else
3650 {
3651 if (do_lock_var(&lv, name_end, deep,
3652 eap->cmdidx == CMD_lockvar) == FAIL)
3653 error = TRUE;
3654 }
3655 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003657 if (!eap->skip)
3658 clear_lval(&lv);
3659
Bram Moolenaar071d4272004-06-13 20:20:40 +00003660 arg = skipwhite(name_end);
3661 } while (!ends_excmd(*arg));
3662
3663 eap->nextcmd = check_nextcmd(arg);
3664}
3665
Bram Moolenaar8c711452005-01-14 21:53:12 +00003666 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003667do_unlet_var(
3668 lval_T *lp,
3669 char_u *name_end,
3670 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003672 int ret = OK;
3673 int cc;
3674
3675 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 cc = *name_end;
3678 *name_end = NUL;
3679
3680 /* Normal name or expanded name. */
3681 if (check_changedtick(lp->ll_name))
3682 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003683 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003686 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003687 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003688 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003689 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003690 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 else if (lp->ll_range)
3693 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003694 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003695 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003696 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003697
3698 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3699 {
3700 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003701 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702 return FAIL;
3703 ll_li = li;
3704 ++ll_n1;
3705 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003706
3707 /* Delete a range of List items. */
3708 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3709 {
3710 li = lp->ll_li->li_next;
3711 listitem_remove(lp->ll_list, lp->ll_li);
3712 lp->ll_li = li;
3713 ++lp->ll_n1;
3714 }
3715 }
3716 else
3717 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003719 /* unlet a List item. */
3720 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003721 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003722 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003723 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 }
3725
3726 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003727}
3728
Bram Moolenaar071d4272004-06-13 20:20:40 +00003729/*
3730 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003731 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003732 */
3733 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003734do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003735{
Bram Moolenaar33570922005-01-25 22:26:29 +00003736 hashtab_T *ht;
3737 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003738 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003739 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003740 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741
Bram Moolenaar33570922005-01-25 22:26:29 +00003742 ht = find_var_ht(name, &varname);
3743 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003745 if (ht == &globvarht)
3746 d = &globvardict;
3747 else if (current_funccal != NULL
3748 && ht == &current_funccal->l_vars.dv_hashtab)
3749 d = &current_funccal->l_vars;
3750 else if (ht == &compat_hashtab)
3751 d = &vimvardict;
3752 else
3753 {
3754 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3755 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3756 }
3757 if (d == NULL)
3758 {
3759 EMSG2(_(e_intern2), "do_unlet()");
3760 return FAIL;
3761 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003762 hi = hash_find(ht, varname);
3763 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003764 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003765 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003766 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003767 || var_check_ro(di->di_flags, name, FALSE)
3768 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003769 return FAIL;
3770
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003771 delete_var(ht, hi);
3772 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003774 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003775 if (forceit)
3776 return OK;
3777 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003778 return FAIL;
3779}
3780
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003781/*
3782 * Lock or unlock variable indicated by "lp".
3783 * "deep" is the levels to go (-1 for unlimited);
3784 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3785 */
3786 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003787do_lock_var(
3788 lval_T *lp,
3789 char_u *name_end,
3790 int deep,
3791 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003792{
3793 int ret = OK;
3794 int cc;
3795 dictitem_T *di;
3796
3797 if (deep == 0) /* nothing to do */
3798 return OK;
3799
3800 if (lp->ll_tv == NULL)
3801 {
3802 cc = *name_end;
3803 *name_end = NUL;
3804
3805 /* Normal name or expanded name. */
3806 if (check_changedtick(lp->ll_name))
3807 ret = FAIL;
3808 else
3809 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003810 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003811 if (di == NULL)
3812 ret = FAIL;
3813 else
3814 {
3815 if (lock)
3816 di->di_flags |= DI_FLAGS_LOCK;
3817 else
3818 di->di_flags &= ~DI_FLAGS_LOCK;
3819 item_lock(&di->di_tv, deep, lock);
3820 }
3821 }
3822 *name_end = cc;
3823 }
3824 else if (lp->ll_range)
3825 {
3826 listitem_T *li = lp->ll_li;
3827
3828 /* (un)lock a range of List items. */
3829 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3830 {
3831 item_lock(&li->li_tv, deep, lock);
3832 li = li->li_next;
3833 ++lp->ll_n1;
3834 }
3835 }
3836 else if (lp->ll_list != NULL)
3837 /* (un)lock a List item. */
3838 item_lock(&lp->ll_li->li_tv, deep, lock);
3839 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003840 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003841 item_lock(&lp->ll_di->di_tv, deep, lock);
3842
3843 return ret;
3844}
3845
3846/*
3847 * Lock or unlock an item. "deep" is nr of levels to go.
3848 */
3849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003850item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003851{
3852 static int recurse = 0;
3853 list_T *l;
3854 listitem_T *li;
3855 dict_T *d;
3856 hashitem_T *hi;
3857 int todo;
3858
3859 if (recurse >= DICT_MAXNEST)
3860 {
3861 EMSG(_("E743: variable nested too deep for (un)lock"));
3862 return;
3863 }
3864 if (deep == 0)
3865 return;
3866 ++recurse;
3867
3868 /* lock/unlock the item itself */
3869 if (lock)
3870 tv->v_lock |= VAR_LOCKED;
3871 else
3872 tv->v_lock &= ~VAR_LOCKED;
3873
3874 switch (tv->v_type)
3875 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003876 case VAR_UNKNOWN:
3877 case VAR_NUMBER:
3878 case VAR_STRING:
3879 case VAR_FUNC:
3880 case VAR_FLOAT:
3881 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003882 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003883 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003884 break;
3885
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003886 case VAR_LIST:
3887 if ((l = tv->vval.v_list) != NULL)
3888 {
3889 if (lock)
3890 l->lv_lock |= VAR_LOCKED;
3891 else
3892 l->lv_lock &= ~VAR_LOCKED;
3893 if (deep < 0 || deep > 1)
3894 /* recursive: lock/unlock the items the List contains */
3895 for (li = l->lv_first; li != NULL; li = li->li_next)
3896 item_lock(&li->li_tv, deep - 1, lock);
3897 }
3898 break;
3899 case VAR_DICT:
3900 if ((d = tv->vval.v_dict) != NULL)
3901 {
3902 if (lock)
3903 d->dv_lock |= VAR_LOCKED;
3904 else
3905 d->dv_lock &= ~VAR_LOCKED;
3906 if (deep < 0 || deep > 1)
3907 {
3908 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003909 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003910 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3911 {
3912 if (!HASHITEM_EMPTY(hi))
3913 {
3914 --todo;
3915 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3916 }
3917 }
3918 }
3919 }
3920 }
3921 --recurse;
3922}
3923
Bram Moolenaara40058a2005-07-11 22:42:07 +00003924/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003925 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3926 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003927 */
3928 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003929tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930{
3931 return (tv->v_lock & VAR_LOCKED)
3932 || (tv->v_type == VAR_LIST
3933 && tv->vval.v_list != NULL
3934 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3935 || (tv->v_type == VAR_DICT
3936 && tv->vval.v_dict != NULL
3937 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3938}
3939
Bram Moolenaar071d4272004-06-13 20:20:40 +00003940#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3941/*
3942 * Delete all "menutrans_" variables.
3943 */
3944 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003945del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946{
Bram Moolenaar33570922005-01-25 22:26:29 +00003947 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003948 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949
Bram Moolenaar33570922005-01-25 22:26:29 +00003950 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003951 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003952 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003953 {
3954 if (!HASHITEM_EMPTY(hi))
3955 {
3956 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003957 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3958 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 }
3960 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962}
3963#endif
3964
3965#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3966
3967/*
3968 * Local string buffer for the next two functions to store a variable name
3969 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3970 * get_user_var_name().
3971 */
3972
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003973static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975static char_u *varnamebuf = NULL;
3976static int varnamebuflen = 0;
3977
3978/*
3979 * Function to concatenate a prefix and a variable name.
3980 */
3981 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003982cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983{
3984 int len;
3985
3986 len = (int)STRLEN(name) + 3;
3987 if (len > varnamebuflen)
3988 {
3989 vim_free(varnamebuf);
3990 len += 10; /* some additional space */
3991 varnamebuf = alloc(len);
3992 if (varnamebuf == NULL)
3993 {
3994 varnamebuflen = 0;
3995 return NULL;
3996 }
3997 varnamebuflen = len;
3998 }
3999 *varnamebuf = prefix;
4000 varnamebuf[1] = ':';
4001 STRCPY(varnamebuf + 2, name);
4002 return varnamebuf;
4003}
4004
4005/*
4006 * Function given to ExpandGeneric() to obtain the list of user defined
4007 * (global/buffer/window/built-in) variable names.
4008 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004009 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004010get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004011{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004012 static long_u gdone;
4013 static long_u bdone;
4014 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004015#ifdef FEAT_WINDOWS
4016 static long_u tdone;
4017#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 static int vidx;
4019 static hashitem_T *hi;
4020 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004021
4022 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004023 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004024 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004025#ifdef FEAT_WINDOWS
4026 tdone = 0;
4027#endif
4028 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004029
4030 /* Global variables */
4031 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004034 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004035 else
4036 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 while (HASHITEM_EMPTY(hi))
4038 ++hi;
4039 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4040 return cat_prefix_varname('g', hi->hi_key);
4041 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004042 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004043
4044 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004045 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004046 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004047 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004048 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004049 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004050 else
4051 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 while (HASHITEM_EMPTY(hi))
4053 ++hi;
4054 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004057 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004059 return (char_u *)"b:changedtick";
4060 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004061
4062 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004063 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004064 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004066 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004067 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004068 else
4069 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004070 while (HASHITEM_EMPTY(hi))
4071 ++hi;
4072 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004075#ifdef FEAT_WINDOWS
4076 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004077 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004078 if (tdone < ht->ht_used)
4079 {
4080 if (tdone++ == 0)
4081 hi = ht->ht_array;
4082 else
4083 ++hi;
4084 while (HASHITEM_EMPTY(hi))
4085 ++hi;
4086 return cat_prefix_varname('t', hi->hi_key);
4087 }
4088#endif
4089
Bram Moolenaar33570922005-01-25 22:26:29 +00004090 /* v: variables */
4091 if (vidx < VV_LEN)
4092 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004093
4094 vim_free(varnamebuf);
4095 varnamebuf = NULL;
4096 varnamebuflen = 0;
4097 return NULL;
4098}
4099
4100#endif /* FEAT_CMDL_COMPL */
4101
4102/*
4103 * types for expressions.
4104 */
4105typedef enum
4106{
4107 TYPE_UNKNOWN = 0
4108 , TYPE_EQUAL /* == */
4109 , TYPE_NEQUAL /* != */
4110 , TYPE_GREATER /* > */
4111 , TYPE_GEQUAL /* >= */
4112 , TYPE_SMALLER /* < */
4113 , TYPE_SEQUAL /* <= */
4114 , TYPE_MATCH /* =~ */
4115 , TYPE_NOMATCH /* !~ */
4116} exptype_T;
4117
4118/*
4119 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004120 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004121 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4122 */
4123
4124/*
4125 * Handle zero level expression.
4126 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004127 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004128 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004129 * Return OK or FAIL.
4130 */
4131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004132eval0(
4133 char_u *arg,
4134 typval_T *rettv,
4135 char_u **nextcmd,
4136 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004137{
4138 int ret;
4139 char_u *p;
4140
4141 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004142 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143 if (ret == FAIL || !ends_excmd(*p))
4144 {
4145 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004146 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 /*
4148 * Report the invalid expression unless the expression evaluation has
4149 * been cancelled due to an aborting error, an interrupt, or an
4150 * exception.
4151 */
4152 if (!aborting())
4153 EMSG2(_(e_invexpr2), arg);
4154 ret = FAIL;
4155 }
4156 if (nextcmd != NULL)
4157 *nextcmd = check_nextcmd(p);
4158
4159 return ret;
4160}
4161
4162/*
4163 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004164 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 *
4166 * "arg" must point to the first non-white of the expression.
4167 * "arg" is advanced to the next non-white after the recognized expression.
4168 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004169 * Note: "rettv.v_lock" is not set.
4170 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 * Return OK or FAIL.
4172 */
4173 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004174eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004177 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004178
4179 /*
4180 * Get the first variable.
4181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004182 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004183 return FAIL;
4184
4185 if ((*arg)[0] == '?')
4186 {
4187 result = FALSE;
4188 if (evaluate)
4189 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004190 int error = FALSE;
4191
4192 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004193 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004194 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004195 if (error)
4196 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004197 }
4198
4199 /*
4200 * Get the second variable.
4201 */
4202 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004204 return FAIL;
4205
4206 /*
4207 * Check for the ":".
4208 */
4209 if ((*arg)[0] != ':')
4210 {
4211 EMSG(_("E109: Missing ':' after '?'"));
4212 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004213 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004214 return FAIL;
4215 }
4216
4217 /*
4218 * Get the third variable.
4219 */
4220 *arg = skipwhite(*arg + 1);
4221 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4222 {
4223 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004224 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004225 return FAIL;
4226 }
4227 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004228 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004229 }
4230
4231 return OK;
4232}
4233
4234/*
4235 * Handle first level expression:
4236 * expr2 || expr2 || expr2 logical OR
4237 *
4238 * "arg" must point to the first non-white of the expression.
4239 * "arg" is advanced to the next non-white after the recognized expression.
4240 *
4241 * Return OK or FAIL.
4242 */
4243 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004244eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004245{
Bram Moolenaar33570922005-01-25 22:26:29 +00004246 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004247 long result;
4248 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004249 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004250
4251 /*
4252 * Get the first variable.
4253 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004254 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004255 return FAIL;
4256
4257 /*
4258 * Repeat until there is no following "||".
4259 */
4260 first = TRUE;
4261 result = FALSE;
4262 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4263 {
4264 if (evaluate && first)
4265 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004266 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004269 if (error)
4270 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004271 first = FALSE;
4272 }
4273
4274 /*
4275 * Get the second variable.
4276 */
4277 *arg = skipwhite(*arg + 2);
4278 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4279 return FAIL;
4280
4281 /*
4282 * Compute the result.
4283 */
4284 if (evaluate && !result)
4285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004286 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004287 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004288 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004289 if (error)
4290 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 }
4292 if (evaluate)
4293 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 rettv->v_type = VAR_NUMBER;
4295 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 }
4297 }
4298
4299 return OK;
4300}
4301
4302/*
4303 * Handle second level expression:
4304 * expr3 && expr3 && expr3 logical AND
4305 *
4306 * "arg" must point to the first non-white of the expression.
4307 * "arg" is advanced to the next non-white after the recognized expression.
4308 *
4309 * Return OK or FAIL.
4310 */
4311 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004312eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004313{
Bram Moolenaar33570922005-01-25 22:26:29 +00004314 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 long result;
4316 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004317 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004318
4319 /*
4320 * Get the first variable.
4321 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004322 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004323 return FAIL;
4324
4325 /*
4326 * Repeat until there is no following "&&".
4327 */
4328 first = TRUE;
4329 result = TRUE;
4330 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4331 {
4332 if (evaluate && first)
4333 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004334 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004336 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004337 if (error)
4338 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004339 first = FALSE;
4340 }
4341
4342 /*
4343 * Get the second variable.
4344 */
4345 *arg = skipwhite(*arg + 2);
4346 if (eval4(arg, &var2, evaluate && result) == FAIL)
4347 return FAIL;
4348
4349 /*
4350 * Compute the result.
4351 */
4352 if (evaluate && result)
4353 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004354 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004355 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004356 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004357 if (error)
4358 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 }
4360 if (evaluate)
4361 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 rettv->v_type = VAR_NUMBER;
4363 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 }
4365 }
4366
4367 return OK;
4368}
4369
4370/*
4371 * Handle third level expression:
4372 * var1 == var2
4373 * var1 =~ var2
4374 * var1 != var2
4375 * var1 !~ var2
4376 * var1 > var2
4377 * var1 >= var2
4378 * var1 < var2
4379 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004380 * var1 is var2
4381 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004382 *
4383 * "arg" must point to the first non-white of the expression.
4384 * "arg" is advanced to the next non-white after the recognized expression.
4385 *
4386 * Return OK or FAIL.
4387 */
4388 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004389eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004390{
Bram Moolenaar33570922005-01-25 22:26:29 +00004391 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004392 char_u *p;
4393 int i;
4394 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004395 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396 int len = 2;
4397 long n1, n2;
4398 char_u *s1, *s2;
4399 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4400 regmatch_T regmatch;
4401 int ic;
4402 char_u *save_cpo;
4403
4404 /*
4405 * Get the first variable.
4406 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004407 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 return FAIL;
4409
4410 p = *arg;
4411 switch (p[0])
4412 {
4413 case '=': if (p[1] == '=')
4414 type = TYPE_EQUAL;
4415 else if (p[1] == '~')
4416 type = TYPE_MATCH;
4417 break;
4418 case '!': if (p[1] == '=')
4419 type = TYPE_NEQUAL;
4420 else if (p[1] == '~')
4421 type = TYPE_NOMATCH;
4422 break;
4423 case '>': if (p[1] != '=')
4424 {
4425 type = TYPE_GREATER;
4426 len = 1;
4427 }
4428 else
4429 type = TYPE_GEQUAL;
4430 break;
4431 case '<': if (p[1] != '=')
4432 {
4433 type = TYPE_SMALLER;
4434 len = 1;
4435 }
4436 else
4437 type = TYPE_SEQUAL;
4438 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 case 'i': if (p[1] == 's')
4440 {
4441 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4442 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004443 i = p[len];
4444 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 {
4446 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4447 type_is = TRUE;
4448 }
4449 }
4450 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004451 }
4452
4453 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004454 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455 */
4456 if (type != TYPE_UNKNOWN)
4457 {
4458 /* extra question mark appended: ignore case */
4459 if (p[len] == '?')
4460 {
4461 ic = TRUE;
4462 ++len;
4463 }
4464 /* extra '#' appended: match case */
4465 else if (p[len] == '#')
4466 {
4467 ic = FALSE;
4468 ++len;
4469 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004470 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 else
4472 ic = p_ic;
4473
4474 /*
4475 * Get the second variable.
4476 */
4477 *arg = skipwhite(p + len);
4478 if (eval5(arg, &var2, evaluate) == FAIL)
4479 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004480 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 return FAIL;
4482 }
4483
4484 if (evaluate)
4485 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004486 if (type_is && rettv->v_type != var2.v_type)
4487 {
4488 /* For "is" a different type always means FALSE, for "notis"
4489 * it means TRUE. */
4490 n1 = (type == TYPE_NEQUAL);
4491 }
4492 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4493 {
4494 if (type_is)
4495 {
4496 n1 = (rettv->v_type == var2.v_type
4497 && rettv->vval.v_list == var2.vval.v_list);
4498 if (type == TYPE_NEQUAL)
4499 n1 = !n1;
4500 }
4501 else if (rettv->v_type != var2.v_type
4502 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4503 {
4504 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004505 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004506 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004507 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004508 clear_tv(rettv);
4509 clear_tv(&var2);
4510 return FAIL;
4511 }
4512 else
4513 {
4514 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004515 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4516 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 if (type == TYPE_NEQUAL)
4518 n1 = !n1;
4519 }
4520 }
4521
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004522 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4523 {
4524 if (type_is)
4525 {
4526 n1 = (rettv->v_type == var2.v_type
4527 && rettv->vval.v_dict == var2.vval.v_dict);
4528 if (type == TYPE_NEQUAL)
4529 n1 = !n1;
4530 }
4531 else if (rettv->v_type != var2.v_type
4532 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4533 {
4534 if (rettv->v_type != var2.v_type)
4535 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4536 else
4537 EMSG(_("E736: Invalid operation for Dictionary"));
4538 clear_tv(rettv);
4539 clear_tv(&var2);
4540 return FAIL;
4541 }
4542 else
4543 {
4544 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004545 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4546 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004547 if (type == TYPE_NEQUAL)
4548 n1 = !n1;
4549 }
4550 }
4551
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004552 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC)
4553 {
4554 if (rettv->v_type != var2.v_type
4555 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4556 {
4557 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004558 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004560 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004561 clear_tv(rettv);
4562 clear_tv(&var2);
4563 return FAIL;
4564 }
4565 else
4566 {
4567 /* Compare two Funcrefs for being equal or unequal. */
4568 if (rettv->vval.v_string == NULL
4569 || var2.vval.v_string == NULL)
4570 n1 = FALSE;
4571 else
4572 n1 = STRCMP(rettv->vval.v_string,
4573 var2.vval.v_string) == 0;
4574 if (type == TYPE_NEQUAL)
4575 n1 = !n1;
4576 }
4577 }
4578
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004579#ifdef FEAT_FLOAT
4580 /*
4581 * If one of the two variables is a float, compare as a float.
4582 * When using "=~" or "!~", always compare as string.
4583 */
4584 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4585 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4586 {
4587 float_T f1, f2;
4588
4589 if (rettv->v_type == VAR_FLOAT)
4590 f1 = rettv->vval.v_float;
4591 else
4592 f1 = get_tv_number(rettv);
4593 if (var2.v_type == VAR_FLOAT)
4594 f2 = var2.vval.v_float;
4595 else
4596 f2 = get_tv_number(&var2);
4597 n1 = FALSE;
4598 switch (type)
4599 {
4600 case TYPE_EQUAL: n1 = (f1 == f2); break;
4601 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4602 case TYPE_GREATER: n1 = (f1 > f2); break;
4603 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4604 case TYPE_SMALLER: n1 = (f1 < f2); break;
4605 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4606 case TYPE_UNKNOWN:
4607 case TYPE_MATCH:
4608 case TYPE_NOMATCH: break; /* avoid gcc warning */
4609 }
4610 }
4611#endif
4612
Bram Moolenaar071d4272004-06-13 20:20:40 +00004613 /*
4614 * If one of the two variables is a number, compare as a number.
4615 * When using "=~" or "!~", always compare as string.
4616 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004617 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004618 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4619 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004620 n1 = get_tv_number(rettv);
4621 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (n1 == n2); break;
4625 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4626 case TYPE_GREATER: n1 = (n1 > n2); break;
4627 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4628 case TYPE_SMALLER: n1 = (n1 < n2); break;
4629 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4630 case TYPE_UNKNOWN:
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH: break; /* avoid gcc warning */
4633 }
4634 }
4635 else
4636 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004637 s1 = get_tv_string_buf(rettv, buf1);
4638 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004639 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4640 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4641 else
4642 i = 0;
4643 n1 = FALSE;
4644 switch (type)
4645 {
4646 case TYPE_EQUAL: n1 = (i == 0); break;
4647 case TYPE_NEQUAL: n1 = (i != 0); break;
4648 case TYPE_GREATER: n1 = (i > 0); break;
4649 case TYPE_GEQUAL: n1 = (i >= 0); break;
4650 case TYPE_SMALLER: n1 = (i < 0); break;
4651 case TYPE_SEQUAL: n1 = (i <= 0); break;
4652
4653 case TYPE_MATCH:
4654 case TYPE_NOMATCH:
4655 /* avoid 'l' flag in 'cpoptions' */
4656 save_cpo = p_cpo;
4657 p_cpo = (char_u *)"";
4658 regmatch.regprog = vim_regcomp(s2,
4659 RE_MAGIC + RE_STRING);
4660 regmatch.rm_ic = ic;
4661 if (regmatch.regprog != NULL)
4662 {
4663 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004664 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004665 if (type == TYPE_NOMATCH)
4666 n1 = !n1;
4667 }
4668 p_cpo = save_cpo;
4669 break;
4670
4671 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4672 }
4673 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004674 clear_tv(rettv);
4675 clear_tv(&var2);
4676 rettv->v_type = VAR_NUMBER;
4677 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004678 }
4679 }
4680
4681 return OK;
4682}
4683
4684/*
4685 * Handle fourth level expression:
4686 * + number addition
4687 * - number subtraction
4688 * . string concatenation
4689 *
4690 * "arg" must point to the first non-white of the expression.
4691 * "arg" is advanced to the next non-white after the recognized expression.
4692 *
4693 * Return OK or FAIL.
4694 */
4695 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004696eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697{
Bram Moolenaar33570922005-01-25 22:26:29 +00004698 typval_T var2;
4699 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004700 int op;
4701 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004702#ifdef FEAT_FLOAT
4703 float_T f1 = 0, f2 = 0;
4704#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004705 char_u *s1, *s2;
4706 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4707 char_u *p;
4708
4709 /*
4710 * Get the first variable.
4711 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004712 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 return FAIL;
4714
4715 /*
4716 * Repeat computing, until no '+', '-' or '.' is following.
4717 */
4718 for (;;)
4719 {
4720 op = **arg;
4721 if (op != '+' && op != '-' && op != '.')
4722 break;
4723
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004724 if ((op != '+' || rettv->v_type != VAR_LIST)
4725#ifdef FEAT_FLOAT
4726 && (op == '.' || rettv->v_type != VAR_FLOAT)
4727#endif
4728 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004729 {
4730 /* For "list + ...", an illegal use of the first operand as
4731 * a number cannot be determined before evaluating the 2nd
4732 * operand: if this is also a list, all is ok.
4733 * For "something . ...", "something - ..." or "non-list + ...",
4734 * we know that the first operand needs to be a string or number
4735 * without evaluating the 2nd operand. So check before to avoid
4736 * side effects after an error. */
4737 if (evaluate && get_tv_string_chk(rettv) == NULL)
4738 {
4739 clear_tv(rettv);
4740 return FAIL;
4741 }
4742 }
4743
Bram Moolenaar071d4272004-06-13 20:20:40 +00004744 /*
4745 * Get the second variable.
4746 */
4747 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004748 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004749 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004750 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751 return FAIL;
4752 }
4753
4754 if (evaluate)
4755 {
4756 /*
4757 * Compute the result.
4758 */
4759 if (op == '.')
4760 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004761 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4762 s2 = get_tv_string_buf_chk(&var2, buf2);
4763 if (s2 == NULL) /* type error ? */
4764 {
4765 clear_tv(rettv);
4766 clear_tv(&var2);
4767 return FAIL;
4768 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004769 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004770 clear_tv(rettv);
4771 rettv->v_type = VAR_STRING;
4772 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004773 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004774 else if (op == '+' && rettv->v_type == VAR_LIST
4775 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004776 {
4777 /* concatenate Lists */
4778 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4779 &var3) == FAIL)
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
4785 clear_tv(rettv);
4786 *rettv = var3;
4787 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004788 else
4789 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004790 int error = FALSE;
4791
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792#ifdef FEAT_FLOAT
4793 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 f1 = rettv->vval.v_float;
4796 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004797 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004798 else
4799#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004800 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004801 n1 = get_tv_number_chk(rettv, &error);
4802 if (error)
4803 {
4804 /* This can only happen for "list + non-list". For
4805 * "non-list + ..." or "something - ...", we returned
4806 * before evaluating the 2nd operand. */
4807 clear_tv(rettv);
4808 return FAIL;
4809 }
4810#ifdef FEAT_FLOAT
4811 if (var2.v_type == VAR_FLOAT)
4812 f1 = n1;
4813#endif
4814 }
4815#ifdef FEAT_FLOAT
4816 if (var2.v_type == VAR_FLOAT)
4817 {
4818 f2 = var2.vval.v_float;
4819 n2 = 0;
4820 }
4821 else
4822#endif
4823 {
4824 n2 = get_tv_number_chk(&var2, &error);
4825 if (error)
4826 {
4827 clear_tv(rettv);
4828 clear_tv(&var2);
4829 return FAIL;
4830 }
4831#ifdef FEAT_FLOAT
4832 if (rettv->v_type == VAR_FLOAT)
4833 f2 = n2;
4834#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004835 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004836 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004837
4838#ifdef FEAT_FLOAT
4839 /* If there is a float on either side the result is a float. */
4840 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4841 {
4842 if (op == '+')
4843 f1 = f1 + f2;
4844 else
4845 f1 = f1 - f2;
4846 rettv->v_type = VAR_FLOAT;
4847 rettv->vval.v_float = f1;
4848 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004849 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850#endif
4851 {
4852 if (op == '+')
4853 n1 = n1 + n2;
4854 else
4855 n1 = n1 - n2;
4856 rettv->v_type = VAR_NUMBER;
4857 rettv->vval.v_number = n1;
4858 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004859 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004860 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004861 }
4862 }
4863 return OK;
4864}
4865
4866/*
4867 * Handle fifth level expression:
4868 * * number multiplication
4869 * / number division
4870 * % number modulo
4871 *
4872 * "arg" must point to the first non-white of the expression.
4873 * "arg" is advanced to the next non-white after the recognized expression.
4874 *
4875 * Return OK or FAIL.
4876 */
4877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004878eval6(
4879 char_u **arg,
4880 typval_T *rettv,
4881 int evaluate,
4882 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004883{
Bram Moolenaar33570922005-01-25 22:26:29 +00004884 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004885 int op;
4886 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004887#ifdef FEAT_FLOAT
4888 int use_float = FALSE;
4889 float_T f1 = 0, f2;
4890#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004891 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004892
4893 /*
4894 * Get the first variable.
4895 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004896 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 return FAIL;
4898
4899 /*
4900 * Repeat computing, until no '*', '/' or '%' is following.
4901 */
4902 for (;;)
4903 {
4904 op = **arg;
4905 if (op != '*' && op != '/' && op != '%')
4906 break;
4907
4908 if (evaluate)
4909 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004910#ifdef FEAT_FLOAT
4911 if (rettv->v_type == VAR_FLOAT)
4912 {
4913 f1 = rettv->vval.v_float;
4914 use_float = TRUE;
4915 n1 = 0;
4916 }
4917 else
4918#endif
4919 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004920 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004921 if (error)
4922 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004923 }
4924 else
4925 n1 = 0;
4926
4927 /*
4928 * Get the second variable.
4929 */
4930 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004931 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004932 return FAIL;
4933
4934 if (evaluate)
4935 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004936#ifdef FEAT_FLOAT
4937 if (var2.v_type == VAR_FLOAT)
4938 {
4939 if (!use_float)
4940 {
4941 f1 = n1;
4942 use_float = TRUE;
4943 }
4944 f2 = var2.vval.v_float;
4945 n2 = 0;
4946 }
4947 else
4948#endif
4949 {
4950 n2 = get_tv_number_chk(&var2, &error);
4951 clear_tv(&var2);
4952 if (error)
4953 return FAIL;
4954#ifdef FEAT_FLOAT
4955 if (use_float)
4956 f2 = n2;
4957#endif
4958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004959
4960 /*
4961 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004962 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004963 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004964#ifdef FEAT_FLOAT
4965 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004966 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004967 if (op == '*')
4968 f1 = f1 * f2;
4969 else if (op == '/')
4970 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971# ifdef VMS
4972 /* VMS crashes on divide by zero, work around it */
4973 if (f2 == 0.0)
4974 {
4975 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004976 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004977 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004978 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004979 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004980 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004981 }
4982 else
4983 f1 = f1 / f2;
4984# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 /* We rely on the floating point library to handle divide
4986 * by zero to result in "inf" and not a crash. */
4987 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004988# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004989 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004990 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004991 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004992 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993 return FAIL;
4994 }
4995 rettv->v_type = VAR_FLOAT;
4996 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004999#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005000 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 if (op == '*')
5002 n1 = n1 * n2;
5003 else if (op == '/')
5004 {
5005 if (n2 == 0) /* give an error message? */
5006 {
5007 if (n1 == 0)
5008 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5009 else if (n1 < 0)
5010 n1 = -0x7fffffffL;
5011 else
5012 n1 = 0x7fffffffL;
5013 }
5014 else
5015 n1 = n1 / n2;
5016 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018 {
5019 if (n2 == 0) /* give an error message? */
5020 n1 = 0;
5021 else
5022 n1 = n1 % n2;
5023 }
5024 rettv->v_type = VAR_NUMBER;
5025 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005026 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005027 }
5028 }
5029
5030 return OK;
5031}
5032
5033/*
5034 * Handle sixth level expression:
5035 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005036 * "string" string constant
5037 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005038 * &option-name option value
5039 * @r register contents
5040 * identifier variable value
5041 * function() function call
5042 * $VAR environment variable
5043 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005044 * [expr, expr] List
5045 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 *
5047 * Also handle:
5048 * ! in front logical NOT
5049 * - in front unary minus
5050 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005051 * trailing [] subscript in String or List
5052 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005053 *
5054 * "arg" must point to the first non-white of the expression.
5055 * "arg" is advanced to the next non-white after the recognized expression.
5056 *
5057 * Return OK or FAIL.
5058 */
5059 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005060eval7(
5061 char_u **arg,
5062 typval_T *rettv,
5063 int evaluate,
5064 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 long n;
5067 int len;
5068 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 char_u *start_leader, *end_leader;
5070 int ret = OK;
5071 char_u *alias;
5072
5073 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005074 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005075 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005076 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005077 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078
5079 /*
5080 * Skip '!' and '-' characters. They are handled later.
5081 */
5082 start_leader = *arg;
5083 while (**arg == '!' || **arg == '-' || **arg == '+')
5084 *arg = skipwhite(*arg + 1);
5085 end_leader = *arg;
5086
5087 switch (**arg)
5088 {
5089 /*
5090 * Number constant.
5091 */
5092 case '0':
5093 case '1':
5094 case '2':
5095 case '3':
5096 case '4':
5097 case '5':
5098 case '6':
5099 case '7':
5100 case '8':
5101 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005102 {
5103#ifdef FEAT_FLOAT
5104 char_u *p = skipdigits(*arg + 1);
5105 int get_float = FALSE;
5106
5107 /* We accept a float when the format matches
5108 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005109 * strict to avoid backwards compatibility problems.
5110 * Don't look for a float after the "." operator, so that
5111 * ":let vers = 1.2.3" doesn't fail. */
5112 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005113 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005114 get_float = TRUE;
5115 p = skipdigits(p + 2);
5116 if (*p == 'e' || *p == 'E')
5117 {
5118 ++p;
5119 if (*p == '-' || *p == '+')
5120 ++p;
5121 if (!vim_isdigit(*p))
5122 get_float = FALSE;
5123 else
5124 p = skipdigits(p + 1);
5125 }
5126 if (ASCII_ISALPHA(*p) || *p == '.')
5127 get_float = FALSE;
5128 }
5129 if (get_float)
5130 {
5131 float_T f;
5132
5133 *arg += string2float(*arg, &f);
5134 if (evaluate)
5135 {
5136 rettv->v_type = VAR_FLOAT;
5137 rettv->vval.v_float = f;
5138 }
5139 }
5140 else
5141#endif
5142 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005143 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005144 *arg += len;
5145 if (evaluate)
5146 {
5147 rettv->v_type = VAR_NUMBER;
5148 rettv->vval.v_number = n;
5149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005150 }
5151 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005152 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005153
5154 /*
5155 * String constant: "string".
5156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005158 break;
5159
5160 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005161 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005164 break;
5165
5166 /*
5167 * List: [expr, expr]
5168 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005169 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005170 break;
5171
5172 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005173 * Dictionary: {key: val, key: val}
5174 */
5175 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5176 break;
5177
5178 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005179 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005180 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005181 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Environment variable: $VAR.
5186 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005187 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 break;
5189
5190 /*
5191 * Register contents: @r.
5192 */
5193 case '@': ++*arg;
5194 if (evaluate)
5195 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005196 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005197 rettv->vval.v_string = get_reg_contents(**arg,
5198 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 }
5200 if (**arg != NUL)
5201 ++*arg;
5202 break;
5203
5204 /*
5205 * nested expression: (expression).
5206 */
5207 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 if (**arg == ')')
5210 ++*arg;
5211 else if (ret == OK)
5212 {
5213 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005214 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 ret = FAIL;
5216 }
5217 break;
5218
Bram Moolenaar8c711452005-01-14 21:53:12 +00005219 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005220 break;
5221 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005222
5223 if (ret == NOTDONE)
5224 {
5225 /*
5226 * Must be a variable or function name.
5227 * Can also be a curly-braces kind of name: {expr}.
5228 */
5229 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005230 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005231 if (alias != NULL)
5232 s = alias;
5233
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005234 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 ret = FAIL;
5236 else
5237 {
5238 if (**arg == '(') /* recursive! */
5239 {
5240 /* If "s" is the name of a variable of type VAR_FUNC
5241 * use its contents. */
Bram Moolenaarf31ecce2014-02-05 22:13:05 +01005242 s = deref_func_name(s, &len, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005243
5244 /* Invoke the function. */
5245 ret = get_func_tv(s, len, rettv, arg,
5246 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaare9a41262005-01-15 22:18:47 +00005247 &len, evaluate, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005248
5249 /* If evaluate is FALSE rettv->v_type was not set in
5250 * get_func_tv, but it's needed in handle_subscript() to parse
5251 * what follows. So set it here. */
5252 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5253 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005254 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005255 rettv->v_type = VAR_FUNC;
5256 }
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /* Stop the expression evaluation when immediately
5259 * aborting on error, or when an interrupt occurred or
5260 * an exception was thrown but not caught. */
5261 if (aborting())
5262 {
5263 if (ret == OK)
5264 clear_tv(rettv);
5265 ret = FAIL;
5266 }
5267 }
5268 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005269 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005270 else
5271 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005272 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005273 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005274 }
5275
Bram Moolenaar071d4272004-06-13 20:20:40 +00005276 *arg = skipwhite(*arg);
5277
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005278 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5279 * expr(expr). */
5280 if (ret == OK)
5281 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005282
5283 /*
5284 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5285 */
5286 if (ret == OK && evaluate && end_leader > start_leader)
5287 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 int val = 0;
5290#ifdef FEAT_FLOAT
5291 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005292
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005293 if (rettv->v_type == VAR_FLOAT)
5294 f = rettv->vval.v_float;
5295 else
5296#endif
5297 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005298 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005299 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005300 clear_tv(rettv);
5301 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005302 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 else
5304 {
5305 while (end_leader > start_leader)
5306 {
5307 --end_leader;
5308 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005309 {
5310#ifdef FEAT_FLOAT
5311 if (rettv->v_type == VAR_FLOAT)
5312 f = !f;
5313 else
5314#endif
5315 val = !val;
5316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005317 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005318 {
5319#ifdef FEAT_FLOAT
5320 if (rettv->v_type == VAR_FLOAT)
5321 f = -f;
5322 else
5323#endif
5324 val = -val;
5325 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005326 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327#ifdef FEAT_FLOAT
5328 if (rettv->v_type == VAR_FLOAT)
5329 {
5330 clear_tv(rettv);
5331 rettv->vval.v_float = f;
5332 }
5333 else
5334#endif
5335 {
5336 clear_tv(rettv);
5337 rettv->v_type = VAR_NUMBER;
5338 rettv->vval.v_number = val;
5339 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005341 }
5342
5343 return ret;
5344}
5345
5346/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005347 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5348 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005349 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5350 */
5351 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005352eval_index(
5353 char_u **arg,
5354 typval_T *rettv,
5355 int evaluate,
5356 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005357{
5358 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005359 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005360 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005361 long len = -1;
5362 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005364 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005365
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005368 case VAR_FUNC:
5369 if (verbose)
5370 EMSG(_("E695: Cannot index a Funcref"));
5371 return FAIL;
5372 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005373#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005374 if (verbose)
5375 EMSG(_(e_float_as_string));
5376 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005377#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005379 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005380 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005381 if (verbose)
5382 EMSG(_("E909: Cannot index a special variable"));
5383 return FAIL;
5384 case VAR_UNKNOWN:
5385 if (evaluate)
5386 return FAIL;
5387 /* FALLTHROUGH */
5388
5389 case VAR_STRING:
5390 case VAR_NUMBER:
5391 case VAR_LIST:
5392 case VAR_DICT:
5393 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005394 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005395
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005396 init_tv(&var1);
5397 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005398 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005399 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005400 /*
5401 * dict.name
5402 */
5403 key = *arg + 1;
5404 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5405 ;
5406 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005407 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005408 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005409 }
5410 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005412 /*
5413 * something[idx]
5414 *
5415 * Get the (first) variable from inside the [].
5416 */
5417 *arg = skipwhite(*arg + 1);
5418 if (**arg == ':')
5419 empty1 = TRUE;
5420 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5421 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005422 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5423 {
5424 /* not a number or string */
5425 clear_tv(&var1);
5426 return FAIL;
5427 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428
5429 /*
5430 * Get the second variable from inside the [:].
5431 */
5432 if (**arg == ':')
5433 {
5434 range = TRUE;
5435 *arg = skipwhite(*arg + 1);
5436 if (**arg == ']')
5437 empty2 = TRUE;
5438 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5439 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005440 if (!empty1)
5441 clear_tv(&var1);
5442 return FAIL;
5443 }
5444 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5445 {
5446 /* not a number or string */
5447 if (!empty1)
5448 clear_tv(&var1);
5449 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450 return FAIL;
5451 }
5452 }
5453
5454 /* Check for the ']'. */
5455 if (**arg != ']')
5456 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005457 if (verbose)
5458 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005459 clear_tv(&var1);
5460 if (range)
5461 clear_tv(&var2);
5462 return FAIL;
5463 }
5464 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005465 }
5466
5467 if (evaluate)
5468 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 n1 = 0;
5470 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005472 n1 = get_tv_number(&var1);
5473 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005474 }
5475 if (range)
5476 {
5477 if (empty2)
5478 n2 = -1;
5479 else
5480 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005481 n2 = get_tv_number(&var2);
5482 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005483 }
5484 }
5485
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005486 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005488 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005489 case VAR_FUNC:
5490 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005491 case VAR_SPECIAL:
5492 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005493 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005494 break; /* not evaluating, skipping over subscript */
5495
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 case VAR_NUMBER:
5497 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005498 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 len = (long)STRLEN(s);
5500 if (range)
5501 {
5502 /* The resulting variable is a substring. If the indexes
5503 * are out of range the result is empty. */
5504 if (n1 < 0)
5505 {
5506 n1 = len + n1;
5507 if (n1 < 0)
5508 n1 = 0;
5509 }
5510 if (n2 < 0)
5511 n2 = len + n2;
5512 else if (n2 >= len)
5513 n2 = len;
5514 if (n1 >= len || n2 < 0 || n1 > n2)
5515 s = NULL;
5516 else
5517 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5518 }
5519 else
5520 {
5521 /* The resulting variable is a string of a single
5522 * character. If the index is too big or negative the
5523 * result is empty. */
5524 if (n1 >= len || n1 < 0)
5525 s = NULL;
5526 else
5527 s = vim_strnsave(s + n1, 1);
5528 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005529 clear_tv(rettv);
5530 rettv->v_type = VAR_STRING;
5531 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005532 break;
5533
5534 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005535 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005536 if (n1 < 0)
5537 n1 = len + n1;
5538 if (!empty1 && (n1 < 0 || n1 >= len))
5539 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005540 /* For a range we allow invalid values and return an empty
5541 * list. A list index out of range is an error. */
5542 if (!range)
5543 {
5544 if (verbose)
5545 EMSGN(_(e_listidx), n1);
5546 return FAIL;
5547 }
5548 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 }
5550 if (range)
5551 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005552 list_T *l;
5553 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005554
5555 if (n2 < 0)
5556 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005557 else if (n2 >= len)
5558 n2 = len - 1;
5559 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005560 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005561 l = list_alloc();
5562 if (l == NULL)
5563 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005564 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005565 n1 <= n2; ++n1)
5566 {
5567 if (list_append_tv(l, &item->li_tv) == FAIL)
5568 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005569 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005570 return FAIL;
5571 }
5572 item = item->li_next;
5573 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005574 clear_tv(rettv);
5575 rettv->v_type = VAR_LIST;
5576 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005577 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 }
5579 else
5580 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005581 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005582 clear_tv(rettv);
5583 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 }
5585 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005586
5587 case VAR_DICT:
5588 if (range)
5589 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005590 if (verbose)
5591 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005592 if (len == -1)
5593 clear_tv(&var1);
5594 return FAIL;
5595 }
5596 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005597 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005598
5599 if (len == -1)
5600 {
5601 key = get_tv_string(&var1);
5602 if (*key == NUL)
5603 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005604 if (verbose)
5605 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606 clear_tv(&var1);
5607 return FAIL;
5608 }
5609 }
5610
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005611 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005613 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005614 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 if (len == -1)
5616 clear_tv(&var1);
5617 if (item == NULL)
5618 return FAIL;
5619
5620 copy_tv(&item->di_tv, &var1);
5621 clear_tv(rettv);
5622 *rettv = var1;
5623 }
5624 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005625 }
5626 }
5627
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005628 return OK;
5629}
5630
5631/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005632 * Get an option value.
5633 * "arg" points to the '&' or '+' before the option name.
5634 * "arg" is advanced to character after the option name.
5635 * Return OK or FAIL.
5636 */
5637 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005638get_option_tv(
5639 char_u **arg,
5640 typval_T *rettv, /* when NULL, only check if option exists */
5641 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005642{
5643 char_u *option_end;
5644 long numval;
5645 char_u *stringval;
5646 int opt_type;
5647 int c;
5648 int working = (**arg == '+'); /* has("+option") */
5649 int ret = OK;
5650 int opt_flags;
5651
5652 /*
5653 * Isolate the option name and find its value.
5654 */
5655 option_end = find_option_end(arg, &opt_flags);
5656 if (option_end == NULL)
5657 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005658 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659 EMSG2(_("E112: Option name missing: %s"), *arg);
5660 return FAIL;
5661 }
5662
5663 if (!evaluate)
5664 {
5665 *arg = option_end;
5666 return OK;
5667 }
5668
5669 c = *option_end;
5670 *option_end = NUL;
5671 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005672 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005673
5674 if (opt_type == -3) /* invalid name */
5675 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005676 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005677 EMSG2(_("E113: Unknown option: %s"), *arg);
5678 ret = FAIL;
5679 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005680 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005681 {
5682 if (opt_type == -2) /* hidden string option */
5683 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005684 rettv->v_type = VAR_STRING;
5685 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005686 }
5687 else if (opt_type == -1) /* hidden number option */
5688 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv->v_type = VAR_NUMBER;
5690 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005691 }
5692 else if (opt_type == 1) /* number option */
5693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005694 rettv->v_type = VAR_NUMBER;
5695 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696 }
5697 else /* string option */
5698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005699 rettv->v_type = VAR_STRING;
5700 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 }
5702 }
5703 else if (working && (opt_type == -2 || opt_type == -1))
5704 ret = FAIL;
5705
5706 *option_end = c; /* put back for error messages */
5707 *arg = option_end;
5708
5709 return ret;
5710}
5711
5712/*
5713 * Allocate a variable for a string constant.
5714 * Return OK or FAIL.
5715 */
5716 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005717get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718{
5719 char_u *p;
5720 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 int extra = 0;
5722
5723 /*
5724 * Find the end of the string, skipping backslashed characters.
5725 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005726 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005727 {
5728 if (*p == '\\' && p[1] != NUL)
5729 {
5730 ++p;
5731 /* A "\<x>" form occupies at least 4 characters, and produces up
5732 * to 6 characters: reserve space for 2 extra */
5733 if (*p == '<')
5734 extra += 2;
5735 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005736 }
5737
5738 if (*p != '"')
5739 {
5740 EMSG2(_("E114: Missing quote: %s"), *arg);
5741 return FAIL;
5742 }
5743
5744 /* If only parsing, set *arg and return here */
5745 if (!evaluate)
5746 {
5747 *arg = p + 1;
5748 return OK;
5749 }
5750
5751 /*
5752 * Copy the string into allocated memory, handling backslashed
5753 * characters.
5754 */
5755 name = alloc((unsigned)(p - *arg + extra));
5756 if (name == NULL)
5757 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005758 rettv->v_type = VAR_STRING;
5759 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760
Bram Moolenaar8c711452005-01-14 21:53:12 +00005761 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005762 {
5763 if (*p == '\\')
5764 {
5765 switch (*++p)
5766 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005767 case 'b': *name++ = BS; ++p; break;
5768 case 'e': *name++ = ESC; ++p; break;
5769 case 'f': *name++ = FF; ++p; break;
5770 case 'n': *name++ = NL; ++p; break;
5771 case 'r': *name++ = CAR; ++p; break;
5772 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005773
5774 case 'X': /* hex: "\x1", "\x12" */
5775 case 'x':
5776 case 'u': /* Unicode: "\u0023" */
5777 case 'U':
5778 if (vim_isxdigit(p[1]))
5779 {
5780 int n, nr;
5781 int c = toupper(*p);
5782
5783 if (c == 'X')
5784 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005785 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005786 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005787 else
5788 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005789 nr = 0;
5790 while (--n >= 0 && vim_isxdigit(p[1]))
5791 {
5792 ++p;
5793 nr = (nr << 4) + hex2nr(*p);
5794 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005795 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796#ifdef FEAT_MBYTE
5797 /* For "\u" store the number according to
5798 * 'encoding'. */
5799 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005800 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005801 else
5802#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005803 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005805 break;
5806
5807 /* octal: "\1", "\12", "\123" */
5808 case '0':
5809 case '1':
5810 case '2':
5811 case '3':
5812 case '4':
5813 case '5':
5814 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 case '7': *name = *p++ - '0';
5816 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005817 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 *name = (*name << 3) + *p++ - '0';
5819 if (*p >= '0' && *p <= '7')
5820 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005822 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005823 break;
5824
5825 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 if (extra != 0)
5828 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005829 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005830 break;
5831 }
5832 /* FALLTHROUGH */
5833
Bram Moolenaar8c711452005-01-14 21:53:12 +00005834 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005835 break;
5836 }
5837 }
5838 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 *arg = p + 1;
5844
Bram Moolenaar071d4272004-06-13 20:20:40 +00005845 return OK;
5846}
5847
5848/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 * Return OK or FAIL.
5851 */
5852 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005853get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005854{
5855 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005856 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005857 int reduce = 0;
5858
5859 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005860 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005861 */
5862 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005867 break;
5868 ++reduce;
5869 ++p;
5870 }
5871 }
5872
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005875 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 return FAIL;
5877 }
5878
Bram Moolenaar8c711452005-01-14 21:53:12 +00005879 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 if (!evaluate)
5881 {
5882 *arg = p + 1;
5883 return OK;
5884 }
5885
5886 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 */
5889 str = alloc((unsigned)((p - *arg) - reduce));
5890 if (str == NULL)
5891 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 rettv->v_type = VAR_STRING;
5893 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 break;
5901 ++p;
5902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005905 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 *arg = p + 1;
5907
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 return OK;
5909}
5910
5911/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005912 * Allocate a variable for a List and fill it from "*arg".
5913 * Return OK or FAIL.
5914 */
5915 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005916get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005917{
Bram Moolenaar33570922005-01-25 22:26:29 +00005918 list_T *l = NULL;
5919 typval_T tv;
5920 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005921
5922 if (evaluate)
5923 {
5924 l = list_alloc();
5925 if (l == NULL)
5926 return FAIL;
5927 }
5928
5929 *arg = skipwhite(*arg + 1);
5930 while (**arg != ']' && **arg != NUL)
5931 {
5932 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5933 goto failret;
5934 if (evaluate)
5935 {
5936 item = listitem_alloc();
5937 if (item != NULL)
5938 {
5939 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005940 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941 list_append(l, item);
5942 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005943 else
5944 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005945 }
5946
5947 if (**arg == ']')
5948 break;
5949 if (**arg != ',')
5950 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005951 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005952 goto failret;
5953 }
5954 *arg = skipwhite(*arg + 1);
5955 }
5956
5957 if (**arg != ']')
5958 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005959 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005960failret:
5961 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005962 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005963 return FAIL;
5964 }
5965
5966 *arg = skipwhite(*arg + 1);
5967 if (evaluate)
5968 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005969 rettv->v_type = VAR_LIST;
5970 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005971 ++l->lv_refcount;
5972 }
5973
5974 return OK;
5975}
5976
5977/*
5978 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005979 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005981 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005982list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005984 list_T *l;
5985
5986 l = (list_T *)alloc_clear(sizeof(list_T));
5987 if (l != NULL)
5988 {
5989 /* Prepend the list to the list of lists for garbage collection. */
5990 if (first_list != NULL)
5991 first_list->lv_used_prev = l;
5992 l->lv_used_prev = NULL;
5993 l->lv_used_next = first_list;
5994 first_list = l;
5995 }
5996 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997}
5998
5999/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006000 * Allocate an empty list for a return value.
6001 * Returns OK or FAIL.
6002 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006003 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006004rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006005{
6006 list_T *l = list_alloc();
6007
6008 if (l == NULL)
6009 return FAIL;
6010
6011 rettv->vval.v_list = l;
6012 rettv->v_type = VAR_LIST;
6013 ++l->lv_refcount;
6014 return OK;
6015}
6016
6017/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006018 * Unreference a list: decrement the reference count and free it when it
6019 * becomes zero.
6020 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006021 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006022list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006023{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006024 if (l != NULL && --l->lv_refcount <= 0)
6025 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006026}
6027
6028/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006029 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006030 * Ignores the reference count.
6031 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006032 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006033list_free(
6034 list_T *l,
6035 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036{
Bram Moolenaar33570922005-01-25 22:26:29 +00006037 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006039 /* Remove the list from the list of lists for garbage collection. */
6040 if (l->lv_used_prev == NULL)
6041 first_list = l->lv_used_next;
6042 else
6043 l->lv_used_prev->lv_used_next = l->lv_used_next;
6044 if (l->lv_used_next != NULL)
6045 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6046
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006049 /* Remove the item before deleting it. */
6050 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006051 if (recurse || (item->li_tv.v_type != VAR_LIST
6052 && item->li_tv.v_type != VAR_DICT))
6053 clear_tv(&item->li_tv);
6054 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055 }
6056 vim_free(l);
6057}
6058
6059/*
6060 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006061 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006063 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006064listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065{
Bram Moolenaar33570922005-01-25 22:26:29 +00006066 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006067}
6068
6069/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006070 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006071 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006072 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006073listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006075 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006076 vim_free(item);
6077}
6078
6079/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006080 * Remove a list item from a List and free it. Also clears the value.
6081 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006082 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006083listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006084{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006085 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006086 listitem_free(item);
6087}
6088
6089/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006090 * Get the number of items in a list.
6091 */
6092 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 if (l == NULL)
6096 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006097 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098}
6099
6100/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006101 * Return TRUE when two lists have exactly the same values.
6102 */
6103 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006104list_equal(
6105 list_T *l1,
6106 list_T *l2,
6107 int ic, /* ignore case for strings */
6108 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109{
Bram Moolenaar33570922005-01-25 22:26:29 +00006110 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006111
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006112 if (l1 == NULL || l2 == NULL)
6113 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006114 if (l1 == l2)
6115 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006116 if (list_len(l1) != list_len(l2))
6117 return FALSE;
6118
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 for (item1 = l1->lv_first, item2 = l2->lv_first;
6120 item1 != NULL && item2 != NULL;
6121 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006122 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006123 return FALSE;
6124 return item1 == NULL && item2 == NULL;
6125}
6126
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006127/*
6128 * Return the dictitem that an entry in a hashtable points to.
6129 */
6130 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006131dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006132{
6133 return HI2DI(hi);
6134}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006135
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006137 * Return TRUE when two dictionaries have exactly the same key/values.
6138 */
6139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006140dict_equal(
6141 dict_T *d1,
6142 dict_T *d2,
6143 int ic, /* ignore case for strings */
6144 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006145{
Bram Moolenaar33570922005-01-25 22:26:29 +00006146 hashitem_T *hi;
6147 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006148 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006149
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006150 if (d1 == NULL || d2 == NULL)
6151 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006152 if (d1 == d2)
6153 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 if (dict_len(d1) != dict_len(d2))
6155 return FALSE;
6156
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006157 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006158 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006159 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006160 if (!HASHITEM_EMPTY(hi))
6161 {
6162 item2 = dict_find(d2, hi->hi_key, -1);
6163 if (item2 == NULL)
6164 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006165 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 return FALSE;
6167 --todo;
6168 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169 }
6170 return TRUE;
6171}
6172
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006173static int tv_equal_recurse_limit;
6174
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006175/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006176 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006177 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006178 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006179 */
6180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006181tv_equal(
6182 typval_T *tv1,
6183 typval_T *tv2,
6184 int ic, /* ignore case */
6185 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006186{
6187 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006188 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006190 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006191
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006192 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006193 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006194
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006195 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006196 * recursiveness to a limit. We guess they are equal then.
6197 * A fixed limit has the problem of still taking an awful long time.
6198 * Reduce the limit every time running into it. That should work fine for
6199 * deeply linked structures that are not recursively linked and catch
6200 * recursiveness quickly. */
6201 if (!recursive)
6202 tv_equal_recurse_limit = 1000;
6203 if (recursive_cnt >= tv_equal_recurse_limit)
6204 {
6205 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006208
6209 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006211 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006212 ++recursive_cnt;
6213 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6214 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006215 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006216
6217 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 ++recursive_cnt;
6219 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6220 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006221 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222
6223 case VAR_FUNC:
6224 return (tv1->vval.v_string != NULL
6225 && tv2->vval.v_string != NULL
6226 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6227
6228 case VAR_NUMBER:
6229 return tv1->vval.v_number == tv2->vval.v_number;
6230
6231 case VAR_STRING:
6232 s1 = get_tv_string_buf(tv1, buf1);
6233 s2 = get_tv_string_buf(tv2, buf2);
6234 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006235
6236 case VAR_SPECIAL:
6237 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006238
6239 case VAR_FLOAT:
6240#ifdef FEAT_FLOAT
6241 return tv1->vval.v_float == tv2->vval.v_float;
6242#endif
6243 case VAR_JOB:
6244#ifdef FEAT_JOB
6245 return tv1->vval.v_job == tv2->vval.v_job;
6246#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006247 case VAR_CHANNEL:
6248#ifdef FEAT_CHANNEL
6249 return tv1->vval.v_channel == tv2->vval.v_channel;
6250#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251 case VAR_UNKNOWN:
6252 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006253 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006254
Bram Moolenaara03f2332016-02-06 18:09:59 +01006255 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6256 * does not equal anything, not even itself. */
6257 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006258}
6259
6260/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006261 * Locate item with index "n" in list "l" and return it.
6262 * A negative index is counted from the end; -1 is the last item.
6263 * Returns NULL when "n" is out of range.
6264 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006265 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006266list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006267{
Bram Moolenaar33570922005-01-25 22:26:29 +00006268 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 long idx;
6270
6271 if (l == NULL)
6272 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006273
6274 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006276 n = l->lv_len + n;
6277
6278 /* Check for index out of range. */
6279 if (n < 0 || n >= l->lv_len)
6280 return NULL;
6281
6282 /* When there is a cached index may start search from there. */
6283 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006284 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006285 if (n < l->lv_idx / 2)
6286 {
6287 /* closest to the start of the list */
6288 item = l->lv_first;
6289 idx = 0;
6290 }
6291 else if (n > (l->lv_idx + l->lv_len) / 2)
6292 {
6293 /* closest to the end of the list */
6294 item = l->lv_last;
6295 idx = l->lv_len - 1;
6296 }
6297 else
6298 {
6299 /* closest to the cached index */
6300 item = l->lv_idx_item;
6301 idx = l->lv_idx;
6302 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 }
6304 else
6305 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006306 if (n < l->lv_len / 2)
6307 {
6308 /* closest to the start of the list */
6309 item = l->lv_first;
6310 idx = 0;
6311 }
6312 else
6313 {
6314 /* closest to the end of the list */
6315 item = l->lv_last;
6316 idx = l->lv_len - 1;
6317 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006319
6320 while (n > idx)
6321 {
6322 /* search forward */
6323 item = item->li_next;
6324 ++idx;
6325 }
6326 while (n < idx)
6327 {
6328 /* search backward */
6329 item = item->li_prev;
6330 --idx;
6331 }
6332
6333 /* cache the used index */
6334 l->lv_idx = idx;
6335 l->lv_idx_item = item;
6336
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 return item;
6338}
6339
6340/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006341 * Get list item "l[idx]" as a number.
6342 */
6343 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006344list_find_nr(
6345 list_T *l,
6346 long idx,
6347 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006348{
6349 listitem_T *li;
6350
6351 li = list_find(l, idx);
6352 if (li == NULL)
6353 {
6354 if (errorp != NULL)
6355 *errorp = TRUE;
6356 return -1L;
6357 }
6358 return get_tv_number_chk(&li->li_tv, errorp);
6359}
6360
6361/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006362 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6363 */
6364 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006365list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006366{
6367 listitem_T *li;
6368
6369 li = list_find(l, idx - 1);
6370 if (li == NULL)
6371 {
6372 EMSGN(_(e_listidx), idx);
6373 return NULL;
6374 }
6375 return get_tv_string(&li->li_tv);
6376}
6377
6378/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006379 * Locate "item" list "l" and return its index.
6380 * Returns -1 when "item" is not in the list.
6381 */
6382 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006383list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006384{
6385 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006386 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387
6388 if (l == NULL)
6389 return -1;
6390 idx = 0;
6391 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6392 ++idx;
6393 if (li == NULL)
6394 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006395 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006396}
6397
6398/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006399 * Append item "item" to the end of list "l".
6400 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006401 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006402list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006403{
6404 if (l->lv_last == NULL)
6405 {
6406 /* empty list */
6407 l->lv_first = item;
6408 l->lv_last = item;
6409 item->li_prev = NULL;
6410 }
6411 else
6412 {
6413 l->lv_last->li_next = item;
6414 item->li_prev = l->lv_last;
6415 l->lv_last = item;
6416 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006417 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006418 item->li_next = NULL;
6419}
6420
6421/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006422 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006423 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006424 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006425 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006428 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006429
Bram Moolenaar05159a02005-02-26 23:04:13 +00006430 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006432 copy_tv(tv, &li->li_tv);
6433 list_append(l, li);
6434 return OK;
6435}
6436
6437/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006438 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006439 * Return FAIL when out of memory.
6440 */
6441 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006442list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006443{
6444 listitem_T *li = listitem_alloc();
6445
6446 if (li == NULL)
6447 return FAIL;
6448 li->li_tv.v_type = VAR_DICT;
6449 li->li_tv.v_lock = 0;
6450 li->li_tv.vval.v_dict = dict;
6451 list_append(list, li);
6452 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453 return OK;
6454}
6455
6456/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006457 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006458 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006459 * Returns FAIL when out of memory.
6460 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006461 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006462list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006463{
6464 listitem_T *li = listitem_alloc();
6465
6466 if (li == NULL)
6467 return FAIL;
6468 list_append(l, li);
6469 li->li_tv.v_type = VAR_STRING;
6470 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006471 if (str == NULL)
6472 li->li_tv.vval.v_string = NULL;
6473 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006474 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006475 return FAIL;
6476 return OK;
6477}
6478
6479/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006480 * Append "n" to list "l".
6481 * Returns FAIL when out of memory.
6482 */
6483 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006484list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006485{
6486 listitem_T *li;
6487
6488 li = listitem_alloc();
6489 if (li == NULL)
6490 return FAIL;
6491 li->li_tv.v_type = VAR_NUMBER;
6492 li->li_tv.v_lock = 0;
6493 li->li_tv.vval.v_number = n;
6494 list_append(l, li);
6495 return OK;
6496}
6497
6498/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006499 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006500 * If "item" is NULL append at the end.
6501 * Return FAIL when out of memory.
6502 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006503 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006504list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006505{
Bram Moolenaar33570922005-01-25 22:26:29 +00006506 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006507
6508 if (ni == NULL)
6509 return FAIL;
6510 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006511 list_insert(l, ni, item);
6512 return OK;
6513}
6514
6515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006516list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006517{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006518 if (item == NULL)
6519 /* Append new item at end of list. */
6520 list_append(l, ni);
6521 else
6522 {
6523 /* Insert new item before existing item. */
6524 ni->li_prev = item->li_prev;
6525 ni->li_next = item;
6526 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006529 ++l->lv_idx;
6530 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006532 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006534 l->lv_idx_item = NULL;
6535 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539}
6540
6541/*
6542 * Extend "l1" with "l2".
6543 * If "bef" is NULL append at the end, otherwise insert before this item.
6544 * Returns FAIL when out of memory.
6545 */
6546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006547list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006548{
Bram Moolenaar33570922005-01-25 22:26:29 +00006549 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006550 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006551
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006552 /* We also quit the loop when we have inserted the original item count of
6553 * the list, avoid a hang when we extend a list with itself. */
6554 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6556 return FAIL;
6557 return OK;
6558}
6559
6560/*
6561 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6562 * Return FAIL when out of memory.
6563 */
6564 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006565list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566{
Bram Moolenaar33570922005-01-25 22:26:29 +00006567 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006568
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006569 if (l1 == NULL || l2 == NULL)
6570 return FAIL;
6571
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006573 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574 if (l == NULL)
6575 return FAIL;
6576 tv->v_type = VAR_LIST;
6577 tv->vval.v_list = l;
6578
6579 /* append all items from the second list */
6580 return list_extend(l, l2, NULL);
6581}
6582
6583/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006584 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006585 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006586 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006587 * Returns NULL when out of memory.
6588 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006589 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006590list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006591{
Bram Moolenaar33570922005-01-25 22:26:29 +00006592 list_T *copy;
6593 listitem_T *item;
6594 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595
6596 if (orig == NULL)
6597 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006598
6599 copy = list_alloc();
6600 if (copy != NULL)
6601 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006602 if (copyID != 0)
6603 {
6604 /* Do this before adding the items, because one of the items may
6605 * refer back to this list. */
6606 orig->lv_copyID = copyID;
6607 orig->lv_copylist = copy;
6608 }
6609 for (item = orig->lv_first; item != NULL && !got_int;
6610 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 {
6612 ni = listitem_alloc();
6613 if (ni == NULL)
6614 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006615 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006616 {
6617 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6618 {
6619 vim_free(ni);
6620 break;
6621 }
6622 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006624 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625 list_append(copy, ni);
6626 }
6627 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006628 if (item != NULL)
6629 {
6630 list_unref(copy);
6631 copy = NULL;
6632 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 }
6634
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 return copy;
6636}
6637
6638/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006639 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006640 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006641 * This used to be called list_remove, but that conflicts with a Sun header
6642 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006645vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006646{
Bram Moolenaar33570922005-01-25 22:26:29 +00006647 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006648
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006649 /* notify watchers */
6650 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006652 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006653 list_fix_watch(l, ip);
6654 if (ip == item2)
6655 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657
6658 if (item2->li_next == NULL)
6659 l->lv_last = item->li_prev;
6660 else
6661 item2->li_next->li_prev = item->li_prev;
6662 if (item->li_prev == NULL)
6663 l->lv_first = item2->li_next;
6664 else
6665 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006666 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667}
6668
6669/*
6670 * Return an allocated string with the string representation of a list.
6671 * May return NULL.
6672 */
6673 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006674list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675{
6676 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677
6678 if (tv->vval.v_list == NULL)
6679 return NULL;
6680 ga_init2(&ga, (int)sizeof(char), 80);
6681 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006682 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006683 {
6684 vim_free(ga.ga_data);
6685 return NULL;
6686 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006687 ga_append(&ga, ']');
6688 ga_append(&ga, NUL);
6689 return (char_u *)ga.ga_data;
6690}
6691
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006692typedef struct join_S {
6693 char_u *s;
6694 char_u *tofree;
6695} join_T;
6696
6697 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list_join_inner(
6699 garray_T *gap, /* to store the result in */
6700 list_T *l,
6701 char_u *sep,
6702 int echo_style,
6703 int copyID,
6704 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006705{
6706 int i;
6707 join_T *p;
6708 int len;
6709 int sumlen = 0;
6710 int first = TRUE;
6711 char_u *tofree;
6712 char_u numbuf[NUMBUFLEN];
6713 listitem_T *item;
6714 char_u *s;
6715
6716 /* Stringify each item in the list. */
6717 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6718 {
6719 if (echo_style)
6720 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6721 else
6722 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6723 if (s == NULL)
6724 return FAIL;
6725
6726 len = (int)STRLEN(s);
6727 sumlen += len;
6728
Bram Moolenaarcde88542015-08-11 19:14:00 +02006729 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006730 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6731 if (tofree != NULL || s != numbuf)
6732 {
6733 p->s = s;
6734 p->tofree = tofree;
6735 }
6736 else
6737 {
6738 p->s = vim_strnsave(s, len);
6739 p->tofree = p->s;
6740 }
6741
6742 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006743 if (did_echo_string_emsg) /* recursion error, bail out */
6744 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006745 }
6746
6747 /* Allocate result buffer with its total size, avoid re-allocation and
6748 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6749 if (join_gap->ga_len >= 2)
6750 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6751 if (ga_grow(gap, sumlen + 2) == FAIL)
6752 return FAIL;
6753
6754 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6755 {
6756 if (first)
6757 first = FALSE;
6758 else
6759 ga_concat(gap, sep);
6760 p = ((join_T *)join_gap->ga_data) + i;
6761
6762 if (p->s != NULL)
6763 ga_concat(gap, p->s);
6764 line_breakcheck();
6765 }
6766
6767 return OK;
6768}
6769
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006770/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006771 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006772 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006773 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006774 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006775 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006776list_join(
6777 garray_T *gap,
6778 list_T *l,
6779 char_u *sep,
6780 int echo_style,
6781 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006783 garray_T join_ga;
6784 int retval;
6785 join_T *p;
6786 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006787
Bram Moolenaard39a7512015-04-16 22:51:22 +02006788 if (l->lv_len < 1)
6789 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006790 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6791 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6792
6793 /* Dispose each item in join_ga. */
6794 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006796 p = (join_T *)join_ga.ga_data;
6797 for (i = 0; i < join_ga.ga_len; ++i)
6798 {
6799 vim_free(p->tofree);
6800 ++p;
6801 }
6802 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804
6805 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806}
6807
6808/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006809 * Return the next (unique) copy ID.
6810 * Used for serializing nested structures.
6811 */
6812 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006813get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006814{
6815 current_copyID += COPYID_INC;
6816 return current_copyID;
6817}
6818
6819/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006820 * Garbage collection for lists and dictionaries.
6821 *
6822 * We use reference counts to be able to free most items right away when they
6823 * are no longer used. But for composite items it's possible that it becomes
6824 * unused while the reference count is > 0: When there is a recursive
6825 * reference. Example:
6826 * :let l = [1, 2, 3]
6827 * :let d = {9: l}
6828 * :let l[1] = d
6829 *
6830 * Since this is quite unusual we handle this with garbage collection: every
6831 * once in a while find out which lists and dicts are not referenced from any
6832 * variable.
6833 *
6834 * Here is a good reference text about garbage collection (refers to Python
6835 * but it applies to all reference-counting mechanisms):
6836 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006837 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006838
6839/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006840 * Do garbage collection for lists and dicts.
6841 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006842 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006843 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006844garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006846 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006847 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 buf_T *buf;
6849 win_T *wp;
6850 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006851 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006852 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006853 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006854#ifdef FEAT_WINDOWS
6855 tabpage_T *tp;
6856#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006857
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006858 /* Only do this once. */
6859 want_garbage_collect = FALSE;
6860 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006861 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006862
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006863 /* We advance by two because we add one for items referenced through
6864 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006865 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006866
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 /*
6868 * 1. Go through all accessible variables and mark all lists and dicts
6869 * with copyID.
6870 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871
6872 /* Don't free variables in the previous_funccal list unless they are only
6873 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006874 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006875 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6876 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006877 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6878 NULL);
6879 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6880 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 }
6882
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006883 /* script-local variables */
6884 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006886
6887 /* buffer-local variables */
6888 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006889 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6890 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
6892 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006893 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006894 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6895 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006896#ifdef FEAT_AUTOCMD
6897 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006898 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6899 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006900#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006902#ifdef FEAT_WINDOWS
6903 /* tabpage-local variables */
6904 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6906 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006907#endif
6908
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006910 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911
6912 /* function-local variables */
6913 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6914 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006915 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6916 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 }
6918
Bram Moolenaard812df62008-11-09 12:46:09 +00006919 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006920 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006921
Bram Moolenaar1dced572012-04-05 16:54:08 +02006922#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006924#endif
6925
Bram Moolenaardb913952012-06-29 12:54:53 +02006926#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006927 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006928#endif
6929
6930#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006932#endif
6933
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006934#ifdef FEAT_CHANNEL
6935 abort = abort || set_ref_in_channel(copyID);
6936#endif
6937
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006939 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006940 /*
6941 * 2. Free lists and dictionaries that are not referenced.
6942 */
6943 did_free = free_unref_items(copyID);
6944
6945 /*
6946 * 3. Check if any funccal can be freed now.
6947 */
6948 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006949 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 if (can_free_funccal(*pfc, copyID))
6951 {
6952 fc = *pfc;
6953 *pfc = fc->caller;
6954 free_funccal(fc, TRUE);
6955 did_free = TRUE;
6956 did_free_funccal = TRUE;
6957 }
6958 else
6959 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006960 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 if (did_free_funccal)
6962 /* When a funccal was freed some more items might be garbage
6963 * collected, so run again. */
6964 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006965 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 else if (p_verbose > 0)
6967 {
6968 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6969 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006970
6971 return did_free;
6972}
6973
6974/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006975 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 */
6977 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006978free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006979{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006980 dict_T *dd, *dd_next;
6981 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006982 int did_free = FALSE;
6983
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006984 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006985 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006986 */
6987 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 {
6989 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006991 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00006992 /* Free the Dictionary and ordinary items it contains, but don't
6993 * recurse into Lists and Dictionaries, they will be in the list
6994 * of dicts or list of lists. */
6995 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006996 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006997 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01006998 dd = dd_next;
6999 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007000
7001 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007002 * Go through the list of lists and free items without the copyID.
7003 * But don't free a list that has a watcher (used in a for loop), these
7004 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 */
7006 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 {
7008 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7010 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007012 /* Free the List and ordinary items it contains, but don't recurse
7013 * into Lists and Dictionaries, they will be in the list of dicts
7014 * or list of lists. */
7015 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007016 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007017 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007018 ll = ll_next;
7019 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007020
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 return did_free;
7022}
7023
7024/*
7025 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007026 * "list_stack" is used to add lists to be marked. Can be NULL.
7027 *
7028 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007030 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007031set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032{
7033 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007036 hashtab_T *cur_ht;
7037 ht_stack_T *ht_stack = NULL;
7038 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007040 cur_ht = ht;
7041 for (;;)
7042 {
7043 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007045 /* Mark each item in the hashtab. If the item contains a hashtab
7046 * it is added to ht_stack, if it contains a list it is added to
7047 * list_stack. */
7048 todo = (int)cur_ht->ht_used;
7049 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7050 if (!HASHITEM_EMPTY(hi))
7051 {
7052 --todo;
7053 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7054 &ht_stack, list_stack);
7055 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057
7058 if (ht_stack == NULL)
7059 break;
7060
7061 /* take an item from the stack */
7062 cur_ht = ht_stack->ht;
7063 tempitem = ht_stack;
7064 ht_stack = ht_stack->prev;
7065 free(tempitem);
7066 }
7067
7068 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069}
7070
7071/*
7072 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7074 *
7075 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007076 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007077 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007078set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007079{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007080 listitem_T *li;
7081 int abort = FALSE;
7082 list_T *cur_l;
7083 list_stack_T *list_stack = NULL;
7084 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007085
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007086 cur_l = l;
7087 for (;;)
7088 {
7089 if (!abort)
7090 /* Mark each item in the list. If the item contains a hashtab
7091 * it is added to ht_stack, if it contains a list it is added to
7092 * list_stack. */
7093 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7094 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7095 ht_stack, &list_stack);
7096 if (list_stack == NULL)
7097 break;
7098
7099 /* take an item from the stack */
7100 cur_l = list_stack->list;
7101 tempitem = list_stack;
7102 list_stack = list_stack->prev;
7103 free(tempitem);
7104 }
7105
7106 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107}
7108
7109/*
7110 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 * "list_stack" is used to add lists to be marked. Can be NULL.
7112 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7113 *
7114 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007116 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007117set_ref_in_item(
7118 typval_T *tv,
7119 int copyID,
7120 ht_stack_T **ht_stack,
7121 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007122{
7123 dict_T *dd;
7124 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007125 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007126
Bram Moolenaara03f2332016-02-06 18:09:59 +01007127 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007128 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007129 dd = tv->vval.v_dict;
7130 if (dd != NULL && dd->dv_copyID != copyID)
7131 {
7132 /* Didn't see this dict yet. */
7133 dd->dv_copyID = copyID;
7134 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007135 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007136 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7137 }
7138 else
7139 {
7140 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7141 if (newitem == NULL)
7142 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 else
7144 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007145 newitem->ht = &dd->dv_hashtab;
7146 newitem->prev = *ht_stack;
7147 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007148 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007149 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007150 }
7151 }
7152 else if (tv->v_type == VAR_LIST)
7153 {
7154 ll = tv->vval.v_list;
7155 if (ll != NULL && ll->lv_copyID != copyID)
7156 {
7157 /* Didn't see this list yet. */
7158 ll->lv_copyID = copyID;
7159 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007161 abort = set_ref_in_list(ll, copyID, ht_stack);
7162 }
7163 else
7164 {
7165 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007166 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007167 if (newitem == NULL)
7168 abort = TRUE;
7169 else
7170 {
7171 newitem->list = ll;
7172 newitem->prev = *list_stack;
7173 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007174 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007175 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007176 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007177 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007178 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007179}
7180
7181/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007182 * Allocate an empty header for a dictionary.
7183 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007184 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007185dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007186{
Bram Moolenaar33570922005-01-25 22:26:29 +00007187 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007188
Bram Moolenaar33570922005-01-25 22:26:29 +00007189 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007190 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007191 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007192 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007193 if (first_dict != NULL)
7194 first_dict->dv_used_prev = d;
7195 d->dv_used_next = first_dict;
7196 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007197 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007198
Bram Moolenaar33570922005-01-25 22:26:29 +00007199 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007200 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007201 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007202 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007203 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007204 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007205 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206}
7207
7208/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007209 * Allocate an empty dict for a return value.
7210 * Returns OK or FAIL.
7211 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007212 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007213rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007214{
7215 dict_T *d = dict_alloc();
7216
7217 if (d == NULL)
7218 return FAIL;
7219
7220 rettv->vval.v_dict = d;
7221 rettv->v_type = VAR_DICT;
7222 ++d->dv_refcount;
7223 return OK;
7224}
7225
7226
7227/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007228 * Unreference a Dictionary: decrement the reference count and free it when it
7229 * becomes zero.
7230 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007232dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007233{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007234 if (d != NULL && --d->dv_refcount <= 0)
7235 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007236}
7237
7238/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007239 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 * Ignores the reference count.
7241 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007242 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243dict_free(
7244 dict_T *d,
7245 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007246{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007249 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007250
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007251 /* Remove the dict from the list of dicts for garbage collection. */
7252 if (d->dv_used_prev == NULL)
7253 first_dict = d->dv_used_next;
7254 else
7255 d->dv_used_prev->dv_used_next = d->dv_used_next;
7256 if (d->dv_used_next != NULL)
7257 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7258
7259 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007260 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007261 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007262 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007263 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 if (!HASHITEM_EMPTY(hi))
7265 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007266 /* Remove the item before deleting it, just in case there is
7267 * something recursive causing trouble. */
7268 di = HI2DI(hi);
7269 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007270 if (recurse || (di->di_tv.v_type != VAR_LIST
7271 && di->di_tv.v_type != VAR_DICT))
7272 clear_tv(&di->di_tv);
7273 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007274 --todo;
7275 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007276 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007277 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007278 vim_free(d);
7279}
7280
7281/*
7282 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007283 * The "key" is copied to the new item.
7284 * Note that the value of the item "di_tv" still needs to be initialized!
7285 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007287 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007288dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289{
Bram Moolenaar33570922005-01-25 22:26:29 +00007290 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007291
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007292 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007293 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007294 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007295 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007296 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007297 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299}
7300
7301/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007302 * Make a copy of a Dictionary item.
7303 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007304 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007305dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007306{
Bram Moolenaar33570922005-01-25 22:26:29 +00007307 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007308
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007309 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7310 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007311 if (di != NULL)
7312 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007314 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007315 copy_tv(&org->di_tv, &di->di_tv);
7316 }
7317 return di;
7318}
7319
7320/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007321 * Remove item "item" from Dictionary "dict" and free it.
7322 */
7323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007324dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007325{
Bram Moolenaar33570922005-01-25 22:26:29 +00007326 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007327
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007329 if (HASHITEM_EMPTY(hi))
7330 EMSG2(_(e_intern2), "dictitem_remove()");
7331 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007332 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 dictitem_free(item);
7334}
7335
7336/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007337 * Free a dict item. Also clears the value.
7338 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007339 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007340dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007341{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007342 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007343 if (item->di_flags & DI_FLAGS_ALLOC)
7344 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345}
7346
7347/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007348 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7349 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007350 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007351 * Returns NULL when out of memory.
7352 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007353 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007354dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007355{
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 dict_T *copy;
7357 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007358 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007360
7361 if (orig == NULL)
7362 return NULL;
7363
7364 copy = dict_alloc();
7365 if (copy != NULL)
7366 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007367 if (copyID != 0)
7368 {
7369 orig->dv_copyID = copyID;
7370 orig->dv_copydict = copy;
7371 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007372 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007373 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007374 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007376 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377 --todo;
7378
7379 di = dictitem_alloc(hi->hi_key);
7380 if (di == NULL)
7381 break;
7382 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007383 {
7384 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7385 copyID) == FAIL)
7386 {
7387 vim_free(di);
7388 break;
7389 }
7390 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007391 else
7392 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7393 if (dict_add(copy, di) == FAIL)
7394 {
7395 dictitem_free(di);
7396 break;
7397 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007399 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007402 if (todo > 0)
7403 {
7404 dict_unref(copy);
7405 copy = NULL;
7406 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007407 }
7408
7409 return copy;
7410}
7411
7412/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007414 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007416 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418{
Bram Moolenaar33570922005-01-25 22:26:29 +00007419 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007420}
7421
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007423 * Add a number or string entry to dictionary "d".
7424 * When "str" is NULL use number "nr", otherwise use "str".
7425 * Returns FAIL when out of memory and when key already exists.
7426 */
7427 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007428dict_add_nr_str(
7429 dict_T *d,
7430 char *key,
7431 long nr,
7432 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007433{
7434 dictitem_T *item;
7435
7436 item = dictitem_alloc((char_u *)key);
7437 if (item == NULL)
7438 return FAIL;
7439 item->di_tv.v_lock = 0;
7440 if (str == NULL)
7441 {
7442 item->di_tv.v_type = VAR_NUMBER;
7443 item->di_tv.vval.v_number = nr;
7444 }
7445 else
7446 {
7447 item->di_tv.v_type = VAR_STRING;
7448 item->di_tv.vval.v_string = vim_strsave(str);
7449 }
7450 if (dict_add(d, item) == FAIL)
7451 {
7452 dictitem_free(item);
7453 return FAIL;
7454 }
7455 return OK;
7456}
7457
7458/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007459 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007460 * Returns FAIL when out of memory and when key already exists.
7461 */
7462 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007463dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007464{
7465 dictitem_T *item;
7466
7467 item = dictitem_alloc((char_u *)key);
7468 if (item == NULL)
7469 return FAIL;
7470 item->di_tv.v_lock = 0;
7471 item->di_tv.v_type = VAR_LIST;
7472 item->di_tv.vval.v_list = list;
7473 if (dict_add(d, item) == FAIL)
7474 {
7475 dictitem_free(item);
7476 return FAIL;
7477 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007478 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007479 return OK;
7480}
7481
7482/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007483 * Get the number of items in a Dictionary.
7484 */
7485 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007487{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007488 if (d == NULL)
7489 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007490 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007491}
7492
7493/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007494 * Find item "key[len]" in Dictionary "d".
7495 * If "len" is negative use strlen(key).
7496 * Returns NULL when not found.
7497 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007498 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007499dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007500{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007501#define AKEYLEN 200
7502 char_u buf[AKEYLEN];
7503 char_u *akey;
7504 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007505 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007506
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007507 if (len < 0)
7508 akey = key;
7509 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007510 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007511 tofree = akey = vim_strnsave(key, len);
7512 if (akey == NULL)
7513 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007514 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007515 else
7516 {
7517 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007518 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007519 akey = buf;
7520 }
7521
Bram Moolenaar33570922005-01-25 22:26:29 +00007522 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007523 vim_free(tofree);
7524 if (HASHITEM_EMPTY(hi))
7525 return NULL;
7526 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007527}
7528
7529/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007530 * Get a string item from a dictionary.
7531 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007532 * Returns NULL if the entry doesn't exist or out of memory.
7533 */
7534 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007535get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007536{
7537 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007538 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007539
7540 di = dict_find(d, key, -1);
7541 if (di == NULL)
7542 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007543 s = get_tv_string(&di->di_tv);
7544 if (save && s != NULL)
7545 s = vim_strsave(s);
7546 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007547}
7548
7549/*
7550 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007551 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007552 */
7553 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007554get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007555{
7556 dictitem_T *di;
7557
7558 di = dict_find(d, key, -1);
7559 if (di == NULL)
7560 return 0;
7561 return get_tv_number(&di->di_tv);
7562}
7563
7564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 * Return an allocated string with the string representation of a Dictionary.
7566 * May return NULL.
7567 */
7568 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007569dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007570{
7571 garray_T ga;
7572 int first = TRUE;
7573 char_u *tofree;
7574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007575 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007576 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007577 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007579
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007581 return NULL;
7582 ga_init2(&ga, (int)sizeof(char), 80);
7583 ga_append(&ga, '{');
7584
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007585 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007586 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 --todo;
7591
7592 if (first)
7593 first = FALSE;
7594 else
7595 ga_concat(&ga, (char_u *)", ");
7596
7597 tofree = string_quote(hi->hi_key, FALSE);
7598 if (tofree != NULL)
7599 {
7600 ga_concat(&ga, tofree);
7601 vim_free(tofree);
7602 }
7603 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007604 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007605 if (s != NULL)
7606 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007607 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007608 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007609 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007610 line_breakcheck();
7611
Bram Moolenaar8c711452005-01-14 21:53:12 +00007612 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007614 if (todo > 0)
7615 {
7616 vim_free(ga.ga_data);
7617 return NULL;
7618 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007619
7620 ga_append(&ga, '}');
7621 ga_append(&ga, NUL);
7622 return (char_u *)ga.ga_data;
7623}
7624
7625/*
7626 * Allocate a variable for a Dictionary and fill it from "*arg".
7627 * Return OK or FAIL. Returns NOTDONE for {expr}.
7628 */
7629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007630get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631{
Bram Moolenaar33570922005-01-25 22:26:29 +00007632 dict_T *d = NULL;
7633 typval_T tvkey;
7634 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007635 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007636 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639
7640 /*
7641 * First check if it's not a curly-braces thing: {expr}.
7642 * Must do this without evaluating, otherwise a function may be called
7643 * twice. Unfortunately this means we need to call eval1() twice for the
7644 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007645 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007646 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007647 if (*start != '}')
7648 {
7649 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7650 return FAIL;
7651 if (*start == '}')
7652 return NOTDONE;
7653 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007654
7655 if (evaluate)
7656 {
7657 d = dict_alloc();
7658 if (d == NULL)
7659 return FAIL;
7660 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 tvkey.v_type = VAR_UNKNOWN;
7662 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 *arg = skipwhite(*arg + 1);
7665 while (**arg != '}' && **arg != NUL)
7666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007668 goto failret;
7669 if (**arg != ':')
7670 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007671 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007672 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007673 goto failret;
7674 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007675 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007677 key = get_tv_string_buf_chk(&tvkey, buf);
7678 if (key == NULL || *key == NUL)
7679 {
7680 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7681 if (key != NULL)
7682 EMSG(_(e_emptykey));
7683 clear_tv(&tvkey);
7684 goto failret;
7685 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7690 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007691 if (evaluate)
7692 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007693 goto failret;
7694 }
7695 if (evaluate)
7696 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007697 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007698 if (item != NULL)
7699 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007700 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007701 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702 clear_tv(&tv);
7703 goto failret;
7704 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007705 item = dictitem_alloc(key);
7706 clear_tv(&tvkey);
7707 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007709 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007710 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 if (dict_add(d, item) == FAIL)
7712 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713 }
7714 }
7715
7716 if (**arg == '}')
7717 break;
7718 if (**arg != ',')
7719 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007720 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007721 goto failret;
7722 }
7723 *arg = skipwhite(*arg + 1);
7724 }
7725
7726 if (**arg != '}')
7727 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007728 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007729failret:
7730 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007731 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 return FAIL;
7733 }
7734
7735 *arg = skipwhite(*arg + 1);
7736 if (evaluate)
7737 {
7738 rettv->v_type = VAR_DICT;
7739 rettv->vval.v_dict = d;
7740 ++d->dv_refcount;
7741 }
7742
7743 return OK;
7744}
7745
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007746#if defined(FEAT_CHANNEL) || defined(PROTO)
7747/*
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007748 * Decrement the reference count on "channel" and maybe free it when it goes
7749 * down to zero. Don't free it if there is a pending action.
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007750 * Returns TRUE when the channel was freed.
7751 */
7752 int
Bram Moolenaar77073442016-02-13 23:23:53 +01007753channel_unref(channel_T *channel)
7754{
7755 if (channel != NULL && --channel->ch_refcount <= 0)
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007756 {
Bram Moolenaarc8dcbb12016-02-25 23:10:17 +01007757 channel_may_free(channel);
Bram Moolenaar3bece9f2016-02-15 20:39:46 +01007758 return TRUE;
7759 }
7760 return FALSE;
Bram Moolenaar77073442016-02-13 23:23:53 +01007761}
7762#endif
7763
Bram Moolenaar65edff82016-02-21 16:40:11 +01007764#if defined(FEAT_JOB) || defined(PROTO)
7765static job_T *first_job = NULL;
7766
Bram Moolenaar835dc632016-02-07 14:27:38 +01007767 static void
7768job_free(job_T *job)
7769{
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007770# ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01007771 if (job->jv_channel != NULL)
7772 {
Bram Moolenaar46c85432016-02-26 11:17:46 +01007773 /* The link from the channel to the job doesn't count as a reference,
7774 * thus don't decrement the refcount of the job. The reference from
7775 * the job to the channel does count the refrence, decrement it and
7776 * NULL the reference. We don't set ch_job_killed, unreferencing the
7777 * job doesn't mean it stops running. */
Bram Moolenaar77073442016-02-13 23:23:53 +01007778 job->jv_channel->ch_job = NULL;
7779 channel_unref(job->jv_channel);
7780 }
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01007781# endif
Bram Moolenaar76467df2016-02-12 19:30:26 +01007782 mch_clear_job(job);
Bram Moolenaar65edff82016-02-21 16:40:11 +01007783
7784 if (job->jv_next != NULL)
7785 job->jv_next->jv_prev = job->jv_prev;
7786 if (job->jv_prev == NULL)
7787 first_job = job->jv_next;
7788 else
7789 job->jv_prev->jv_next = job->jv_next;
7790
7791 vim_free(job->jv_stoponexit);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007792 vim_free(job->jv_exit_cb);
Bram Moolenaar835dc632016-02-07 14:27:38 +01007793 vim_free(job);
7794}
7795
7796 static void
7797job_unref(job_T *job)
7798{
7799 if (job != NULL && --job->jv_refcount <= 0)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007800 {
7801 /* Do not free the job when it has not ended yet and there is a
7802 * "stoponexit" flag or an exit callback. */
7803 if (job->jv_status != JOB_STARTED
7804 || (job->jv_stoponexit == NULL && job->jv_exit_cb == NULL))
7805 job_free(job);
7806 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007807}
7808
7809/*
Bram Moolenaar65edff82016-02-21 16:40:11 +01007810 * Allocate a job. Sets the refcount to one and sets options default.
Bram Moolenaar835dc632016-02-07 14:27:38 +01007811 */
7812 static job_T *
7813job_alloc(void)
7814{
7815 job_T *job;
7816
7817 job = (job_T *)alloc_clear(sizeof(job_T));
7818 if (job != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007819 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01007820 job->jv_refcount = 1;
Bram Moolenaar65edff82016-02-21 16:40:11 +01007821 job->jv_stoponexit = vim_strsave((char_u *)"term");
7822
7823 if (first_job != NULL)
7824 {
7825 first_job->jv_prev = job;
7826 job->jv_next = first_job;
7827 }
7828 first_job = job;
7829 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007830 return job;
7831}
7832
Bram Moolenaar65edff82016-02-21 16:40:11 +01007833 static void
7834job_set_options(job_T *job, jobopt_T *opt)
7835{
7836 if (opt->jo_set & JO_STOPONEXIT)
7837 {
7838 vim_free(job->jv_stoponexit);
7839 if (opt->jo_stoponexit == NULL || *opt->jo_stoponexit == NUL)
7840 job->jv_stoponexit = NULL;
7841 else
7842 job->jv_stoponexit = vim_strsave(opt->jo_stoponexit);
7843 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007844 if (opt->jo_set & JO_EXIT_CB)
7845 {
7846 vim_free(job->jv_exit_cb);
7847 if (opt->jo_exit_cb == NULL || *opt->jo_exit_cb == NUL)
7848 job->jv_exit_cb = NULL;
7849 else
7850 job->jv_exit_cb = vim_strsave(opt->jo_exit_cb);
7851 }
Bram Moolenaar65edff82016-02-21 16:40:11 +01007852}
7853
7854/*
7855 * Called when Vim is exiting: kill all jobs that have the "stoponexit" flag.
7856 */
7857 void
7858job_stop_on_exit()
7859{
7860 job_T *job;
7861
7862 for (job = first_job; job != NULL; job = job->jv_next)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +01007863 if (job->jv_status == JOB_STARTED && job->jv_stoponexit != NULL)
Bram Moolenaar65edff82016-02-21 16:40:11 +01007864 mch_stop_job(job, job->jv_stoponexit);
7865}
Bram Moolenaar835dc632016-02-07 14:27:38 +01007866#endif
7867
Bram Moolenaar17a13432016-01-24 14:22:10 +01007868 static char *
7869get_var_special_name(int nr)
7870{
7871 switch (nr)
7872 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007873 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007874 case VVAL_TRUE: return "v:true";
7875 case VVAL_NONE: return "v:none";
7876 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007877 }
7878 EMSG2(_(e_intern2), "get_var_special_name()");
7879 return "42";
7880}
7881
Bram Moolenaar8c711452005-01-14 21:53:12 +00007882/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007883 * Return a string with the string representation of a variable.
7884 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007885 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007886 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007887 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007888 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007889 */
7890 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007891echo_string(
7892 typval_T *tv,
7893 char_u **tofree,
7894 char_u *numbuf,
7895 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007896{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007897 static int recurse = 0;
7898 char_u *r = NULL;
7899
Bram Moolenaar33570922005-01-25 22:26:29 +00007900 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007901 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007902 if (!did_echo_string_emsg)
7903 {
7904 /* Only give this message once for a recursive call to avoid
7905 * flooding the user with errors. And stop iterating over lists
7906 * and dicts. */
7907 did_echo_string_emsg = TRUE;
7908 EMSG(_("E724: variable nested too deep for displaying"));
7909 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007910 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007911 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007912 }
7913 ++recurse;
7914
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007915 switch (tv->v_type)
7916 {
7917 case VAR_FUNC:
7918 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007919 r = tv->vval.v_string;
7920 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007921
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007922 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007923 if (tv->vval.v_list == NULL)
7924 {
7925 *tofree = NULL;
7926 r = NULL;
7927 }
7928 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7929 {
7930 *tofree = NULL;
7931 r = (char_u *)"[...]";
7932 }
7933 else
7934 {
7935 tv->vval.v_list->lv_copyID = copyID;
7936 *tofree = list2string(tv, copyID);
7937 r = *tofree;
7938 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007939 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007940
Bram Moolenaar8c711452005-01-14 21:53:12 +00007941 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007942 if (tv->vval.v_dict == NULL)
7943 {
7944 *tofree = NULL;
7945 r = NULL;
7946 }
7947 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7948 {
7949 *tofree = NULL;
7950 r = (char_u *)"{...}";
7951 }
7952 else
7953 {
7954 tv->vval.v_dict->dv_copyID = copyID;
7955 *tofree = dict2string(tv, copyID);
7956 r = *tofree;
7957 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007958 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007959
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007960 case VAR_STRING:
7961 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007962 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007963 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007964 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007965 *tofree = NULL;
7966 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007967 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007968
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007970#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007971 *tofree = NULL;
7972 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7973 r = numbuf;
7974 break;
7975#endif
7976
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007977 case VAR_SPECIAL:
7978 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007979 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007980 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007981 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007982
Bram Moolenaar8502c702014-06-17 12:51:16 +02007983 if (--recurse == 0)
7984 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007985 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007986}
7987
7988/*
7989 * Return a string with the string representation of a variable.
7990 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7991 * "numbuf" is used for a number.
7992 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007993 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007994 */
7995 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007996tv2string(
7997 typval_T *tv,
7998 char_u **tofree,
7999 char_u *numbuf,
8000 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008001{
8002 switch (tv->v_type)
8003 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008004 case VAR_FUNC:
8005 *tofree = string_quote(tv->vval.v_string, TRUE);
8006 return *tofree;
8007 case VAR_STRING:
8008 *tofree = string_quote(tv->vval.v_string, FALSE);
8009 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008010#ifdef FEAT_FLOAT
8011 case VAR_FLOAT:
8012 *tofree = NULL;
8013 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8014 return numbuf;
8015#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008016 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008017 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008018 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008019 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008020 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008021 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008022 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008023 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008024 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008025 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008026}
8027
8028/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008029 * Return string "str" in ' quotes, doubling ' characters.
8030 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008031 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008032 */
8033 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008034string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008035{
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 char_u *p, *r, *s;
8038
Bram Moolenaar33570922005-01-25 22:26:29 +00008039 len = (function ? 13 : 3);
8040 if (str != NULL)
8041 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008042 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008043 for (p = str; *p != NUL; mb_ptr_adv(p))
8044 if (*p == '\'')
8045 ++len;
8046 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008047 s = r = alloc(len);
8048 if (r != NULL)
8049 {
8050 if (function)
8051 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008052 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008053 r += 10;
8054 }
8055 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008056 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008057 if (str != NULL)
8058 for (p = str; *p != NUL; )
8059 {
8060 if (*p == '\'')
8061 *r++ = '\'';
8062 MB_COPY_CHAR(p, r);
8063 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008064 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008065 if (function)
8066 *r++ = ')';
8067 *r++ = NUL;
8068 }
8069 return s;
8070}
8071
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008072#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008073/*
8074 * Convert the string "text" to a floating point number.
8075 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8076 * this always uses a decimal point.
8077 * Returns the length of the text that was consumed.
8078 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008080string2float(
8081 char_u *text,
8082 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008083{
8084 char *s = (char *)text;
8085 float_T f;
8086
8087 f = strtod(s, &s);
8088 *value = f;
8089 return (int)((char_u *)s - text);
8090}
8091#endif
8092
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008093/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008094 * Get the value of an environment variable.
8095 * "arg" is pointing to the '$'. It is advanced to after the name.
8096 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008097 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098 */
8099 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008100get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101{
8102 char_u *string = NULL;
8103 int len;
8104 int cc;
8105 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008106 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008107
8108 ++*arg;
8109 name = *arg;
8110 len = get_env_len(arg);
8111 if (evaluate)
8112 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008113 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008114 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008115
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008116 cc = name[len];
8117 name[len] = NUL;
8118 /* first try vim_getenv(), fast for normal environment vars */
8119 string = vim_getenv(name, &mustfree);
8120 if (string != NULL && *string != NUL)
8121 {
8122 if (!mustfree)
8123 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008124 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008125 else
8126 {
8127 if (mustfree)
8128 vim_free(string);
8129
8130 /* next try expanding things like $VIM and ${HOME} */
8131 string = expand_env_save(name - 1);
8132 if (string != NULL && *string == '$')
8133 {
8134 vim_free(string);
8135 string = NULL;
8136 }
8137 }
8138 name[len] = cc;
8139
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008140 rettv->v_type = VAR_STRING;
8141 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008142 }
8143
8144 return OK;
8145}
8146
8147/*
8148 * Array with names and number of arguments of all internal functions
8149 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8150 */
8151static struct fst
8152{
8153 char *f_name; /* function name */
8154 char f_min_argc; /* minimal number of arguments */
8155 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008156 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008157 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008158} functions[] =
8159{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#ifdef FEAT_FLOAT
8161 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008162 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008163#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008164 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008165 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008166 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"append", 2, 2, f_append},
8168 {"argc", 0, 0, f_argc},
8169 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008170 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008171 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008172#ifdef FEAT_FLOAT
8173 {"asin", 1, 1, f_asin}, /* WJMc */
8174#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008175 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008176 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008177 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008178 {"assert_false", 1, 2, f_assert_false},
8179 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008180#ifdef FEAT_FLOAT
8181 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008182 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008185 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"bufexists", 1, 1, f_bufexists},
8187 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8188 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8189 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8190 {"buflisted", 1, 1, f_buflisted},
8191 {"bufloaded", 1, 1, f_bufloaded},
8192 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008193 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"bufwinnr", 1, 1, f_bufwinnr},
8195 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008196 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008197 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008198 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008199#ifdef FEAT_FLOAT
8200 {"ceil", 1, 1, f_ceil},
8201#endif
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008202#ifdef FEAT_CHANNEL
8203 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008204# ifdef FEAT_JOB
8205 {"ch_getjob", 1, 1, f_ch_getjob},
8206# endif
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008207 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008208 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008209 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008210 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008211 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008212 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8213 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008214 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008215 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008216#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008217 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008218 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008220 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008222#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008223 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008224 {"complete_add", 1, 1, f_complete_add},
8225 {"complete_check", 0, 0, f_complete_check},
8226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#ifdef FEAT_FLOAT
8230 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008231 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008233 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008235 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008236 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008237 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008239 {"diff_filler", 1, 1, f_diff_filler},
8240 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008241 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008242 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008244 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"eventhandler", 0, 0, f_eventhandler},
8246 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008247 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008249#ifdef FEAT_FLOAT
8250 {"exp", 1, 1, f_exp},
8251#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008252 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008253 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008254 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8256 {"filereadable", 1, 1, f_filereadable},
8257 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008258 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008259 {"finddir", 1, 3, f_finddir},
8260 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"float2nr", 1, 1, f_float2nr},
8263 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008264 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008266 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"fnamemodify", 2, 2, f_fnamemodify},
8268 {"foldclosed", 1, 1, f_foldclosed},
8269 {"foldclosedend", 1, 1, f_foldclosedend},
8270 {"foldlevel", 1, 1, f_foldlevel},
8271 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008272 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"foreground", 0, 0, f_foreground},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008274 {"function", 1, 1, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008275 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008276 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008277 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008278 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"getchar", 0, 1, f_getchar},
8280 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008281 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"getcmdline", 0, 0, f_getcmdline},
8283 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008284 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008285 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008286 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008287 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008288 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008289 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getfsize", 1, 1, f_getfsize},
8291 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008292 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008293 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008294 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008295 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008296 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008297 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008298 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008299 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008301 {"gettabvar", 2, 3, f_gettabvar},
8302 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"getwinposx", 0, 0, f_getwinposx},
8304 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008305 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008306 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008307 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008308 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008310 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008311 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008312 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8314 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8315 {"histadd", 2, 2, f_histadd},
8316 {"histdel", 1, 2, f_histdel},
8317 {"histget", 1, 2, f_histget},
8318 {"histnr", 1, 1, f_histnr},
8319 {"hlID", 1, 1, f_hlID},
8320 {"hlexists", 1, 1, f_hlexists},
8321 {"hostname", 0, 0, f_hostname},
8322 {"iconv", 3, 3, f_iconv},
8323 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008324 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008325 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008327 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"inputrestore", 0, 0, f_inputrestore},
8329 {"inputsave", 0, 0, f_inputsave},
8330 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008332 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008334 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008335#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8336 {"isnan", 1, 1, f_isnan},
8337#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 {"items", 1, 1, f_items},
Bram Moolenaarcb4b0122016-02-07 14:53:21 +01008339#ifdef FEAT_JOB
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008340# ifdef FEAT_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008341 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaarfa4bce72016-02-13 23:50:08 +01008342# endif
Bram Moolenaar65edff82016-02-21 16:40:11 +01008343 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008344 {"job_start", 1, 2, f_job_start},
8345 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008346 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008347#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008348 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008349 {"js_decode", 1, 1, f_js_decode},
8350 {"js_encode", 1, 1, f_js_encode},
8351 {"json_decode", 1, 1, f_json_decode},
8352 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008353 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008354 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008355 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"libcall", 3, 3, f_libcall},
8357 {"libcallnr", 3, 3, f_libcallnr},
8358 {"line", 1, 1, f_line},
8359 {"line2byte", 1, 1, f_line2byte},
8360 {"lispindent", 1, 1, f_lispindent},
8361 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008362#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008363 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008364 {"log10", 1, 1, f_log10},
8365#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008366#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008367 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008368#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008369 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008370 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008371 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008372 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008373 {"matchadd", 2, 5, f_matchadd},
8374 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008375 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008376 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008377 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008378 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008379 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008380 {"max", 1, 1, f_max},
8381 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008382#ifdef vim_mkdir
8383 {"mkdir", 1, 3, f_mkdir},
8384#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008385 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008386#ifdef FEAT_MZSCHEME
8387 {"mzeval", 1, 1, f_mzeval},
8388#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008390 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008391 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008392 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008393#ifdef FEAT_PERL
8394 {"perleval", 1, 1, f_perleval},
8395#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008396#ifdef FEAT_FLOAT
8397 {"pow", 2, 2, f_pow},
8398#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008399 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008400 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008401 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008402#ifdef FEAT_PYTHON3
8403 {"py3eval", 1, 1, f_py3eval},
8404#endif
8405#ifdef FEAT_PYTHON
8406 {"pyeval", 1, 1, f_pyeval},
8407#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008408 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008409 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008410 {"reltime", 0, 2, f_reltime},
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008411 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008412 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008413 {"remote_expr", 2, 3, f_remote_expr},
8414 {"remote_foreground", 1, 1, f_remote_foreground},
8415 {"remote_peek", 1, 2, f_remote_peek},
8416 {"remote_read", 1, 1, f_remote_read},
8417 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008418 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008420 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008421 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008422 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008423#ifdef FEAT_FLOAT
8424 {"round", 1, 1, f_round},
8425#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008426 {"screenattr", 2, 2, f_screenattr},
8427 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008428 {"screencol", 0, 0, f_screencol},
8429 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008430 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008431 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008432 {"searchpair", 3, 7, f_searchpair},
8433 {"searchpairpos", 3, 7, f_searchpairpos},
8434 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008435 {"server2client", 2, 2, f_server2client},
8436 {"serverlist", 0, 0, f_serverlist},
8437 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008438 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"setcmdpos", 1, 1, f_setcmdpos},
8440 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008441 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008442 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008443 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008444 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008446 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008447 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008448 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008449#ifdef FEAT_CRYPT
8450 {"sha256", 1, 1, f_sha256},
8451#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008452 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008453 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008455#ifdef FEAT_FLOAT
8456 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008457 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008458#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008459 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008460 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008461 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008462 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008463 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008464#ifdef FEAT_FLOAT
8465 {"sqrt", 1, 1, f_sqrt},
8466 {"str2float", 1, 1, f_str2float},
8467#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008468 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008469 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008470 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008471#ifdef HAVE_STRFTIME
8472 {"strftime", 1, 2, f_strftime},
8473#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008474 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008475 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008476 {"strlen", 1, 1, f_strlen},
8477 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008478 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008479 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008480 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008481 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482 {"substitute", 4, 4, f_substitute},
8483 {"synID", 3, 3, f_synID},
8484 {"synIDattr", 2, 3, f_synIDattr},
8485 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008486 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008487 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008488 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008489 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008490 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008491 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008492 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008493 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008494 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008495#ifdef FEAT_FLOAT
8496 {"tan", 1, 1, f_tan},
8497 {"tanh", 1, 1, f_tanh},
8498#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008499 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008500 {"test", 1, 1, f_test},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"tolower", 1, 1, f_tolower},
8502 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008503 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008504#ifdef FEAT_FLOAT
8505 {"trunc", 1, 1, f_trunc},
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008508 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008509 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008510 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008511 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008512 {"virtcol", 1, 1, f_virtcol},
8513 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008514 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"winbufnr", 1, 1, f_winbufnr},
8516 {"wincol", 0, 0, f_wincol},
8517 {"winheight", 1, 1, f_winheight},
8518 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008519 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008521 {"winrestview", 1, 1, f_winrestview},
8522 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008523 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008524 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008525 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008526 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527};
8528
8529#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8530
8531/*
8532 * Function given to ExpandGeneric() to obtain the list of internal
8533 * or user defined function names.
8534 */
8535 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008536get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008537{
8538 static int intidx = -1;
8539 char_u *name;
8540
8541 if (idx == 0)
8542 intidx = -1;
8543 if (intidx < 0)
8544 {
8545 name = get_user_func_name(xp, idx);
8546 if (name != NULL)
8547 return name;
8548 }
8549 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8550 {
8551 STRCPY(IObuff, functions[intidx].f_name);
8552 STRCAT(IObuff, "(");
8553 if (functions[intidx].f_max_argc == 0)
8554 STRCAT(IObuff, ")");
8555 return IObuff;
8556 }
8557
8558 return NULL;
8559}
8560
8561/*
8562 * Function given to ExpandGeneric() to obtain the list of internal or
8563 * user defined variable or function names.
8564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008566get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008567{
8568 static int intidx = -1;
8569 char_u *name;
8570
8571 if (idx == 0)
8572 intidx = -1;
8573 if (intidx < 0)
8574 {
8575 name = get_function_name(xp, idx);
8576 if (name != NULL)
8577 return name;
8578 }
8579 return get_user_var_name(xp, ++intidx);
8580}
8581
8582#endif /* FEAT_CMDL_COMPL */
8583
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008584#if defined(EBCDIC) || defined(PROTO)
8585/*
8586 * Compare struct fst by function name.
8587 */
8588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008589compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008590{
8591 struct fst *p1 = (struct fst *)s1;
8592 struct fst *p2 = (struct fst *)s2;
8593
8594 return STRCMP(p1->f_name, p2->f_name);
8595}
8596
8597/*
8598 * Sort the function table by function name.
8599 * The sorting of the table above is ASCII dependant.
8600 * On machines using EBCDIC we have to sort it.
8601 */
8602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008603sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008604{
8605 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8606
8607 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8608}
8609#endif
8610
8611
Bram Moolenaar071d4272004-06-13 20:20:40 +00008612/*
8613 * Find internal function in table above.
8614 * Return index, or -1 if not found
8615 */
8616 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008617find_internal_func(
8618 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008619{
8620 int first = 0;
8621 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8622 int cmp;
8623 int x;
8624
8625 /*
8626 * Find the function name in the table. Binary search.
8627 */
8628 while (first <= last)
8629 {
8630 x = first + ((unsigned)(last - first) >> 1);
8631 cmp = STRCMP(name, functions[x].f_name);
8632 if (cmp < 0)
8633 last = x - 1;
8634 else if (cmp > 0)
8635 first = x + 1;
8636 else
8637 return x;
8638 }
8639 return -1;
8640}
8641
8642/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008643 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8644 * name it contains, otherwise return "name".
8645 */
8646 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008647deref_func_name(char_u *name, int *lenp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008648{
Bram Moolenaar33570922005-01-25 22:26:29 +00008649 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008650 int cc;
8651
8652 cc = name[*lenp];
8653 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008654 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008655 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008656 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008657 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008658 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008659 {
8660 *lenp = 0;
8661 return (char_u *)""; /* just in case */
8662 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008663 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008665 }
8666
8667 return name;
8668}
8669
8670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008671 * Allocate a variable for the result of a function.
8672 * Return OK or FAIL.
8673 */
8674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008675get_func_tv(
8676 char_u *name, /* name of the function */
8677 int len, /* length of "name" */
8678 typval_T *rettv,
8679 char_u **arg, /* argument, pointing to the '(' */
8680 linenr_T firstline, /* first line of range */
8681 linenr_T lastline, /* last line of range */
8682 int *doesrange, /* return: function handled range */
8683 int evaluate,
8684 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685{
8686 char_u *argp;
8687 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008688 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008689 int argcount = 0; /* number of arguments found */
8690
8691 /*
8692 * Get the arguments.
8693 */
8694 argp = *arg;
8695 while (argcount < MAX_FUNC_ARGS)
8696 {
8697 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8698 if (*argp == ')' || *argp == ',' || *argp == NUL)
8699 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8701 {
8702 ret = FAIL;
8703 break;
8704 }
8705 ++argcount;
8706 if (*argp != ',')
8707 break;
8708 }
8709 if (*argp == ')')
8710 ++argp;
8711 else
8712 ret = FAIL;
8713
8714 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008715 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008716 firstline, lastline, doesrange, evaluate, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008718 {
8719 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008720 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008721 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008722 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008723 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008724
8725 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008726 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008727
8728 *arg = skipwhite(argp);
8729 return ret;
8730}
8731
8732
8733/*
8734 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008735 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008736 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008739call_func(
8740 char_u *funcname, /* name of the function */
8741 int len, /* length of "name" */
8742 typval_T *rettv, /* return value goes here */
8743 int argcount, /* number of "argvars" */
8744 typval_T *argvars, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008745 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008746 linenr_T firstline, /* first line of range */
8747 linenr_T lastline, /* last line of range */
8748 int *doesrange, /* return: function handled range */
8749 int evaluate,
8750 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751{
8752 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008753#define ERROR_UNKNOWN 0
8754#define ERROR_TOOMANY 1
8755#define ERROR_TOOFEW 2
8756#define ERROR_SCRIPT 3
Bram Moolenaare9a41262005-01-15 22:18:47 +00008757#define ERROR_DICT 4
8758#define ERROR_NONE 5
8759#define ERROR_OTHER 6
Bram Moolenaar071d4272004-06-13 20:20:40 +00008760 int error = ERROR_NONE;
8761 int i;
8762 int llen;
8763 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008764#define FLEN_FIXED 40
8765 char_u fname_buf[FLEN_FIXED + 1];
8766 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008767 char_u *name;
8768
8769 /* Make a copy of the name, if it comes from a funcref variable it could
8770 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008771 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008772 if (name == NULL)
8773 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008774
8775 /*
8776 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8777 * Change <SNR>123_name() to K_SNR 123_name().
8778 * Use fname_buf[] when it fits, otherwise allocate memory (slow).
8779 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008780 llen = eval_fname_script(name);
8781 if (llen > 0)
8782 {
8783 fname_buf[0] = K_SPECIAL;
8784 fname_buf[1] = KS_EXTRA;
8785 fname_buf[2] = (int)KE_SNR;
8786 i = 3;
8787 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8788 {
8789 if (current_SID <= 0)
8790 error = ERROR_SCRIPT;
8791 else
8792 {
8793 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8794 i = (int)STRLEN(fname_buf);
8795 }
8796 }
8797 if (i + STRLEN(name + llen) < FLEN_FIXED)
8798 {
8799 STRCPY(fname_buf + i, name + llen);
8800 fname = fname_buf;
8801 }
8802 else
8803 {
8804 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8805 if (fname == NULL)
8806 error = ERROR_OTHER;
8807 else
8808 {
8809 mch_memmove(fname, fname_buf, (size_t)i);
8810 STRCPY(fname + i, name + llen);
8811 }
8812 }
8813 }
8814 else
8815 fname = name;
8816
8817 *doesrange = FALSE;
8818
8819
8820 /* execute the function if no errors detected and executing */
8821 if (evaluate && error == ERROR_NONE)
8822 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008823 char_u *rfname = fname;
8824
8825 /* Ignore "g:" before a function name. */
8826 if (fname[0] == 'g' && fname[1] == ':')
8827 rfname = fname + 2;
8828
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008829 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8830 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008831 error = ERROR_UNKNOWN;
8832
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008833 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
8835 /*
8836 * User defined function.
8837 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008838 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008839
Bram Moolenaar071d4272004-06-13 20:20:40 +00008840#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 /* Trigger FuncUndefined event, may load the function. */
8842 if (fp == NULL
8843 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008844 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008845 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008847 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008848 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 }
8850#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008851 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008852 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008853 {
8854 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008855 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008856 }
8857
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 if (fp != NULL)
8859 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008860 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008862 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008863 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008864 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008866 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008867 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008868 else
8869 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008870 int did_save_redo = FALSE;
8871
Bram Moolenaar071d4272004-06-13 20:20:40 +00008872 /*
8873 * Call the user function.
8874 * Save and restore search patterns, script variables and
8875 * redo buffer.
8876 */
8877 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008878#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008879 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008880#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008881 {
8882 saveRedobuff();
8883 did_save_redo = TRUE;
8884 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008885 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008886 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008887 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008888 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8889 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8890 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008891 /* Function was unreferenced while being used, free it
8892 * now. */
8893 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008894 if (did_save_redo)
8895 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008896 restore_search_patterns();
8897 error = ERROR_NONE;
8898 }
8899 }
8900 }
8901 else
8902 {
8903 /*
8904 * Find the function name in the table, call its implementation.
8905 */
8906 i = find_internal_func(fname);
8907 if (i >= 0)
8908 {
8909 if (argcount < functions[i].f_min_argc)
8910 error = ERROR_TOOFEW;
8911 else if (argcount > functions[i].f_max_argc)
8912 error = ERROR_TOOMANY;
8913 else
8914 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008915 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008916 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008917 error = ERROR_NONE;
8918 }
8919 }
8920 }
8921 /*
8922 * The function call (or "FuncUndefined" autocommand sequence) might
8923 * have been aborted by an error, an interrupt, or an explicitly thrown
8924 * exception that has not been caught so far. This situation can be
8925 * tested for by calling aborting(). For an error in an internal
8926 * function or for the "E132" error in call_user_func(), however, the
8927 * throw point at which the "force_abort" flag (temporarily reset by
8928 * emsg()) is normally updated has not been reached yet. We need to
8929 * update that flag first to make aborting() reliable.
8930 */
8931 update_force_abort();
8932 }
8933 if (error == ERROR_NONE)
8934 ret = OK;
8935
8936 /*
8937 * Report an error unless the argument evaluation or function call has been
8938 * cancelled due to an aborting error, an interrupt, or an exception.
8939 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 if (!aborting())
8941 {
8942 switch (error)
8943 {
8944 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008945 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008946 break;
8947 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008948 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008949 break;
8950 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008951 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008952 name);
8953 break;
8954 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008955 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008956 name);
8957 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008958 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008959 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008960 name);
8961 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008962 }
8963 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008964
Bram Moolenaar071d4272004-06-13 20:20:40 +00008965 if (fname != name && fname != fname_buf)
8966 vim_free(fname);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008967 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968
8969 return ret;
8970}
8971
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008972/*
8973 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008974 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975 */
8976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008977emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008978{
8979 char_u *p;
8980
8981 if (*name == K_SPECIAL)
8982 p = concat_str((char_u *)"<SNR>", name + 3);
8983 else
8984 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008985 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008986 if (p != name)
8987 vim_free(p);
8988}
8989
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008990/*
8991 * Return TRUE for a non-zero Number and a non-empty String.
8992 */
8993 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008994non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008995{
8996 return ((argvars[0].v_type == VAR_NUMBER
8997 && argvars[0].vval.v_number != 0)
8998 || (argvars[0].v_type == VAR_STRING
8999 && argvars[0].vval.v_string != NULL
9000 && *argvars[0].vval.v_string != NUL));
9001}
9002
Bram Moolenaar071d4272004-06-13 20:20:40 +00009003/*********************************************
9004 * Implementation of the built-in functions
9005 */
9006
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009007#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009008static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009009
9010/*
9011 * Get the float value of "argvars[0]" into "f".
9012 * Returns FAIL when the argument is not a Number or Float.
9013 */
9014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009015get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009016{
9017 if (argvars[0].v_type == VAR_FLOAT)
9018 {
9019 *f = argvars[0].vval.v_float;
9020 return OK;
9021 }
9022 if (argvars[0].v_type == VAR_NUMBER)
9023 {
9024 *f = (float_T)argvars[0].vval.v_number;
9025 return OK;
9026 }
9027 EMSG(_("E808: Number or Float required"));
9028 return FAIL;
9029}
9030
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009031/*
9032 * "abs(expr)" function
9033 */
9034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009035f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009036{
9037 if (argvars[0].v_type == VAR_FLOAT)
9038 {
9039 rettv->v_type = VAR_FLOAT;
9040 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9041 }
9042 else
9043 {
9044 varnumber_T n;
9045 int error = FALSE;
9046
9047 n = get_tv_number_chk(&argvars[0], &error);
9048 if (error)
9049 rettv->vval.v_number = -1;
9050 else if (n > 0)
9051 rettv->vval.v_number = n;
9052 else
9053 rettv->vval.v_number = -n;
9054 }
9055}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009056
9057/*
9058 * "acos()" function
9059 */
9060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009061f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009062{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009063 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009064
9065 rettv->v_type = VAR_FLOAT;
9066 if (get_float_arg(argvars, &f) == OK)
9067 rettv->vval.v_float = acos(f);
9068 else
9069 rettv->vval.v_float = 0.0;
9070}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009071#endif
9072
Bram Moolenaar071d4272004-06-13 20:20:40 +00009073/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009074 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009075 */
9076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009077f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078{
Bram Moolenaar33570922005-01-25 22:26:29 +00009079 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009080
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009081 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009082 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009083 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009084 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009085 && !tv_check_lock(l->lv_lock,
9086 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009087 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009088 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009089 }
9090 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009091 EMSG(_(e_listreq));
9092}
9093
9094/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009095 * "alloc_fail(id, countdown, repeat)" function
9096 */
9097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009098f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009099{
9100 if (argvars[0].v_type != VAR_NUMBER
9101 || argvars[0].vval.v_number <= 0
9102 || argvars[1].v_type != VAR_NUMBER
9103 || argvars[1].vval.v_number < 0
9104 || argvars[2].v_type != VAR_NUMBER)
9105 EMSG(_(e_invarg));
9106 else
9107 {
9108 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009109 if (alloc_fail_id >= aid_last)
9110 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009111 alloc_fail_countdown = argvars[1].vval.v_number;
9112 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009113 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009114 }
9115}
9116
9117/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009118 * "and(expr, expr)" function
9119 */
9120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009121f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009122{
9123 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9124 & get_tv_number_chk(&argvars[1], NULL);
9125}
9126
9127/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009128 * "append(lnum, string/list)" function
9129 */
9130 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009131f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009132{
9133 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009134 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 list_T *l = NULL;
9136 listitem_T *li = NULL;
9137 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009138 long added = 0;
9139
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009140 /* When coming here from Insert mode, sync undo, so that this can be
9141 * undone separately from what was previously inserted. */
9142 if (u_sync_once == 2)
9143 {
9144 u_sync_once = 1; /* notify that u_sync() was called */
9145 u_sync(TRUE);
9146 }
9147
Bram Moolenaar0d660222005-01-07 21:51:51 +00009148 lnum = get_tv_lnum(argvars);
9149 if (lnum >= 0
9150 && lnum <= curbuf->b_ml.ml_line_count
9151 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009152 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009153 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009154 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009155 l = argvars[1].vval.v_list;
9156 if (l == NULL)
9157 return;
9158 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009159 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009160 for (;;)
9161 {
9162 if (l == NULL)
9163 tv = &argvars[1]; /* append a string */
9164 else if (li == NULL)
9165 break; /* end of list */
9166 else
9167 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009168 line = get_tv_string_chk(tv);
9169 if (line == NULL) /* type error */
9170 {
9171 rettv->vval.v_number = 1; /* Failed */
9172 break;
9173 }
9174 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009175 ++added;
9176 if (l == NULL)
9177 break;
9178 li = li->li_next;
9179 }
9180
9181 appended_lines_mark(lnum, added);
9182 if (curwin->w_cursor.lnum > lnum)
9183 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009184 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009185 else
9186 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009187}
9188
9189/*
9190 * "argc()" function
9191 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009193f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009195 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009196}
9197
9198/*
9199 * "argidx()" function
9200 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009202f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009203{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009204 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009205}
9206
9207/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009208 * "arglistid()" function
9209 */
9210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009211f_arglistid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009212{
9213 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009214
9215 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009216 wp = find_tabwin(&argvars[0], &argvars[1]);
9217 if (wp != NULL)
9218 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009219}
9220
9221/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009222 * "argv(nr)" function
9223 */
9224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009225f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009226{
9227 int idx;
9228
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009229 if (argvars[0].v_type != VAR_UNKNOWN)
9230 {
9231 idx = get_tv_number_chk(&argvars[0], NULL);
9232 if (idx >= 0 && idx < ARGCOUNT)
9233 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9234 else
9235 rettv->vval.v_string = NULL;
9236 rettv->v_type = VAR_STRING;
9237 }
9238 else if (rettv_list_alloc(rettv) == OK)
9239 for (idx = 0; idx < ARGCOUNT; ++idx)
9240 list_append_string(rettv->vval.v_list,
9241 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009242}
9243
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009244static void prepare_assert_error(garray_T*gap);
9245static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9246static void assert_error(garray_T *gap);
9247static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009248
9249/*
9250 * Prepare "gap" for an assert error and add the sourcing position.
9251 */
9252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009253prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009254{
9255 char buf[NUMBUFLEN];
9256
9257 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009258 if (sourcing_name != NULL)
9259 {
9260 ga_concat(gap, sourcing_name);
9261 if (sourcing_lnum > 0)
9262 ga_concat(gap, (char_u *)" ");
9263 }
9264 if (sourcing_lnum > 0)
9265 {
9266 sprintf(buf, "line %ld", (long)sourcing_lnum);
9267 ga_concat(gap, (char_u *)buf);
9268 }
9269 if (sourcing_name != NULL || sourcing_lnum > 0)
9270 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009271}
9272
9273/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009274 * Append "str" to "gap", escaping unprintable characters.
9275 * Changes NL to \n, CR to \r, etc.
9276 */
9277 static void
9278ga_concat_esc(garray_T *gap, char_u *str)
9279{
9280 char_u *p;
9281 char_u buf[NUMBUFLEN];
9282
9283 for (p = str; *p != NUL; ++p)
9284 switch (*p)
9285 {
9286 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9287 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9288 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9289 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9290 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9291 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9292 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9293 default:
9294 if (*p < ' ')
9295 {
9296 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9297 ga_concat(gap, buf);
9298 }
9299 else
9300 ga_append(gap, *p);
9301 break;
9302 }
9303}
9304
9305/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009306 * Fill "gap" with information about an assert error.
9307 */
9308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009309fill_assert_error(
9310 garray_T *gap,
9311 typval_T *opt_msg_tv,
9312 char_u *exp_str,
9313 typval_T *exp_tv,
9314 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009315{
9316 char_u numbuf[NUMBUFLEN];
9317 char_u *tofree;
9318
9319 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9320 {
9321 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9322 vim_free(tofree);
9323 }
9324 else
9325 {
9326 ga_concat(gap, (char_u *)"Expected ");
9327 if (exp_str == NULL)
9328 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009329 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009330 vim_free(tofree);
9331 }
9332 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009333 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009334 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009335 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009336 vim_free(tofree);
9337 }
9338}
Bram Moolenaar43345542015-11-29 17:35:35 +01009339
9340/*
9341 * Add an assert error to v:errors.
9342 */
9343 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009344assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009345{
9346 struct vimvar *vp = &vimvars[VV_ERRORS];
9347
9348 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9349 /* Make sure v:errors is a list. */
9350 set_vim_var_list(VV_ERRORS, list_alloc());
9351 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9352}
9353
Bram Moolenaar43345542015-11-29 17:35:35 +01009354/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009355 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009356 */
9357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009358f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009359{
9360 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009361
9362 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9363 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009364 prepare_assert_error(&ga);
9365 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9366 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009367 ga_clear(&ga);
9368 }
9369}
9370
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009371/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009372 * "assert_exception(string[, msg])" function
9373 */
9374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009375f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009376{
9377 garray_T ga;
9378 char *error;
9379
9380 error = (char *)get_tv_string_chk(&argvars[0]);
9381 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9382 {
9383 prepare_assert_error(&ga);
9384 ga_concat(&ga, (char_u *)"v:exception is not set");
9385 assert_error(&ga);
9386 ga_clear(&ga);
9387 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009388 else if (error != NULL
9389 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009390 {
9391 prepare_assert_error(&ga);
9392 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9393 &vimvars[VV_EXCEPTION].vv_tv);
9394 assert_error(&ga);
9395 ga_clear(&ga);
9396 }
9397}
9398
9399/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009400 * "assert_fails(cmd [, error])" function
9401 */
9402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009403f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009404{
9405 char_u *cmd = get_tv_string_chk(&argvars[0]);
9406 garray_T ga;
9407
9408 called_emsg = FALSE;
9409 suppress_errthrow = TRUE;
9410 emsg_silent = TRUE;
9411 do_cmdline_cmd(cmd);
9412 if (!called_emsg)
9413 {
9414 prepare_assert_error(&ga);
9415 ga_concat(&ga, (char_u *)"command did not fail: ");
9416 ga_concat(&ga, cmd);
9417 assert_error(&ga);
9418 ga_clear(&ga);
9419 }
9420 else if (argvars[1].v_type != VAR_UNKNOWN)
9421 {
9422 char_u buf[NUMBUFLEN];
9423 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9424
9425 if (strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
9426 {
9427 prepare_assert_error(&ga);
9428 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9429 &vimvars[VV_ERRMSG].vv_tv);
9430 assert_error(&ga);
9431 ga_clear(&ga);
9432 }
9433 }
9434
9435 called_emsg = FALSE;
9436 suppress_errthrow = FALSE;
9437 emsg_silent = FALSE;
9438 emsg_on_display = FALSE;
9439 set_vim_var_string(VV_ERRMSG, NULL, 0);
9440}
9441
9442/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009443 * Common for assert_true() and assert_false().
9444 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009446assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009447{
9448 int error = FALSE;
9449 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009450
Bram Moolenaar37127922016-02-06 20:29:28 +01009451 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009452 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009453 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 if (argvars[0].v_type != VAR_NUMBER
9455 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9456 || error)
9457 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458 prepare_assert_error(&ga);
9459 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009460 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009461 NULL, &argvars[0]);
9462 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 ga_clear(&ga);
9464 }
9465}
9466
9467/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009468 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009469 */
9470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009472{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009473 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009474}
9475
9476/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009477 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009478 */
9479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009480f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009481{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009482 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009483}
9484
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009485#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009486/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009487 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009488 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009491{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009492 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009493
9494 rettv->v_type = VAR_FLOAT;
9495 if (get_float_arg(argvars, &f) == OK)
9496 rettv->vval.v_float = asin(f);
9497 else
9498 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009499}
9500
9501/*
9502 * "atan()" function
9503 */
9504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009505f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009506{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009507 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009508
9509 rettv->v_type = VAR_FLOAT;
9510 if (get_float_arg(argvars, &f) == OK)
9511 rettv->vval.v_float = atan(f);
9512 else
9513 rettv->vval.v_float = 0.0;
9514}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009515
9516/*
9517 * "atan2()" function
9518 */
9519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009520f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009521{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009522 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009523
9524 rettv->v_type = VAR_FLOAT;
9525 if (get_float_arg(argvars, &fx) == OK
9526 && get_float_arg(&argvars[1], &fy) == OK)
9527 rettv->vval.v_float = atan2(fx, fy);
9528 else
9529 rettv->vval.v_float = 0.0;
9530}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009531#endif
9532
Bram Moolenaar071d4272004-06-13 20:20:40 +00009533/*
9534 * "browse(save, title, initdir, default)" function
9535 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009537f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009538{
9539#ifdef FEAT_BROWSE
9540 int save;
9541 char_u *title;
9542 char_u *initdir;
9543 char_u *defname;
9544 char_u buf[NUMBUFLEN];
9545 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009546 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009547
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009548 save = get_tv_number_chk(&argvars[0], &error);
9549 title = get_tv_string_chk(&argvars[1]);
9550 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9551 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009552
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009553 if (error || title == NULL || initdir == NULL || defname == NULL)
9554 rettv->vval.v_string = NULL;
9555 else
9556 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557 do_browse(save ? BROWSE_SAVE : 0,
9558 title, defname, NULL, initdir, NULL, curbuf);
9559#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009560 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009561#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009562 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009563}
9564
9565/*
9566 * "browsedir(title, initdir)" function
9567 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009569f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009570{
9571#ifdef FEAT_BROWSE
9572 char_u *title;
9573 char_u *initdir;
9574 char_u buf[NUMBUFLEN];
9575
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009576 title = get_tv_string_chk(&argvars[0]);
9577 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009578
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009579 if (title == NULL || initdir == NULL)
9580 rettv->vval.v_string = NULL;
9581 else
9582 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009583 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009585 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009586#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009587 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588}
9589
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009590static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009591
Bram Moolenaar071d4272004-06-13 20:20:40 +00009592/*
9593 * Find a buffer by number or exact name.
9594 */
9595 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009596find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009597{
9598 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009600 if (avar->v_type == VAR_NUMBER)
9601 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009602 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009603 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009604 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009605 if (buf == NULL)
9606 {
9607 /* No full path name match, try a match with a URL or a "nofile"
9608 * buffer, these don't use the full path. */
9609 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9610 if (buf->b_fname != NULL
9611 && (path_with_url(buf->b_fname)
9612#ifdef FEAT_QUICKFIX
9613 || bt_nofile(buf)
9614#endif
9615 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009616 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009617 break;
9618 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009619 }
9620 return buf;
9621}
9622
9623/*
9624 * "bufexists(expr)" function
9625 */
9626 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009627f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009628{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009629 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009630}
9631
9632/*
9633 * "buflisted(expr)" function
9634 */
9635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009636f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009637{
9638 buf_T *buf;
9639
9640 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009641 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009642}
9643
9644/*
9645 * "bufloaded(expr)" function
9646 */
9647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009648f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649{
9650 buf_T *buf;
9651
9652 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009653 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654}
9655
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009656static buf_T *get_buf_tv(typval_T *tv, int curtab_only);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009657
Bram Moolenaar071d4272004-06-13 20:20:40 +00009658/*
9659 * Get buffer by number or pattern.
9660 */
9661 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009662get_buf_tv(typval_T *tv, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009663{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009664 char_u *name = tv->vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009665 int save_magic;
9666 char_u *save_cpo;
9667 buf_T *buf;
9668
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009669 if (tv->v_type == VAR_NUMBER)
9670 return buflist_findnr((int)tv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009671 if (tv->v_type != VAR_STRING)
9672 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673 if (name == NULL || *name == NUL)
9674 return curbuf;
9675 if (name[0] == '$' && name[1] == NUL)
9676 return lastbuf;
9677
9678 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9679 save_magic = p_magic;
9680 p_magic = TRUE;
9681 save_cpo = p_cpo;
9682 p_cpo = (char_u *)"";
9683
9684 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009685 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009686
9687 p_magic = save_magic;
9688 p_cpo = save_cpo;
9689
9690 /* If not found, try expanding the name, like done for bufexists(). */
9691 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693
9694 return buf;
9695}
9696
9697/*
9698 * "bufname(expr)" function
9699 */
9700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009701f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702{
9703 buf_T *buf;
9704
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009705 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009706 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009707 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009710 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009712 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 --emsg_off;
9714}
9715
9716/*
9717 * "bufnr(expr)" function
9718 */
9719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009720f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
9722 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009723 int error = FALSE;
9724 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009726 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009727 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009728 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009729 --emsg_off;
9730
9731 /* If the buffer isn't found and the second argument is not zero create a
9732 * new buffer. */
9733 if (buf == NULL
9734 && argvars[1].v_type != VAR_UNKNOWN
9735 && get_tv_number_chk(&argvars[1], &error) != 0
9736 && !error
9737 && (name = get_tv_string_chk(&argvars[0])) != NULL
9738 && !error)
9739 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9740
Bram Moolenaar071d4272004-06-13 20:20:40 +00009741 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009742 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009745}
9746
9747/*
9748 * "bufwinnr(nr)" function
9749 */
9750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009751f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752{
9753#ifdef FEAT_WINDOWS
9754 win_T *wp;
9755 int winnr = 0;
9756#endif
9757 buf_T *buf;
9758
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009759 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009760 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009761 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009762#ifdef FEAT_WINDOWS
9763 for (wp = firstwin; wp; wp = wp->w_next)
9764 {
9765 ++winnr;
9766 if (wp->w_buffer == buf)
9767 break;
9768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009769 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009771 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772#endif
9773 --emsg_off;
9774}
9775
9776/*
9777 * "byte2line(byte)" function
9778 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009780f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781{
9782#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009783 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784#else
9785 long boff = 0;
9786
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009787 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009788 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009791 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792 (linenr_T)0, &boff);
9793#endif
9794}
9795
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009796 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009797byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009798{
9799#ifdef FEAT_MBYTE
9800 char_u *t;
9801#endif
9802 char_u *str;
9803 long idx;
9804
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009805 str = get_tv_string_chk(&argvars[0]);
9806 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009807 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009809 return;
9810
9811#ifdef FEAT_MBYTE
9812 t = str;
9813 for ( ; idx > 0; idx--)
9814 {
9815 if (*t == NUL) /* EOL reached */
9816 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009817 if (enc_utf8 && comp)
9818 t += utf_ptr2len(t);
9819 else
9820 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009821 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009822 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009823#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009824 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009826#endif
9827}
9828
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009829/*
9830 * "byteidx()" function
9831 */
9832 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009833f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009834{
9835 byteidx(argvars, rettv, FALSE);
9836}
9837
9838/*
9839 * "byteidxcomp()" function
9840 */
9841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009842f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009843{
9844 byteidx(argvars, rettv, TRUE);
9845}
9846
Bram Moolenaardb913952012-06-29 12:54:53 +02009847 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009848func_call(
9849 char_u *name,
9850 typval_T *args,
9851 dict_T *selfdict,
9852 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009853{
9854 listitem_T *item;
9855 typval_T argv[MAX_FUNC_ARGS + 1];
9856 int argc = 0;
9857 int dummy;
9858 int r = 0;
9859
9860 for (item = args->vval.v_list->lv_first; item != NULL;
9861 item = item->li_next)
9862 {
9863 if (argc == MAX_FUNC_ARGS)
9864 {
9865 EMSG(_("E699: Too many arguments"));
9866 break;
9867 }
9868 /* Make a copy of each argument. This is needed to be able to set
9869 * v_lock to VAR_FIXED in the copy without changing the original list.
9870 */
9871 copy_tv(&item->li_tv, &argv[argc++]);
9872 }
9873
9874 if (item == NULL)
9875 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9876 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
9877 &dummy, TRUE, selfdict);
9878
9879 /* Free the arguments. */
9880 while (argc > 0)
9881 clear_tv(&argv[--argc]);
9882
9883 return r;
9884}
9885
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009886/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009887 * "call(func, arglist)" function
9888 */
9889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009890f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009891{
9892 char_u *func;
Bram Moolenaar33570922005-01-25 22:26:29 +00009893 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009894
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895 if (argvars[1].v_type != VAR_LIST)
9896 {
9897 EMSG(_(e_listreq));
9898 return;
9899 }
9900 if (argvars[1].vval.v_list == NULL)
9901 return;
9902
9903 if (argvars[0].v_type == VAR_FUNC)
9904 func = argvars[0].vval.v_string;
9905 else
9906 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009907 if (*func == NUL)
9908 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009909
Bram Moolenaare9a41262005-01-15 22:18:47 +00009910 if (argvars[2].v_type != VAR_UNKNOWN)
9911 {
9912 if (argvars[2].v_type != VAR_DICT)
9913 {
9914 EMSG(_(e_dictreq));
9915 return;
9916 }
9917 selfdict = argvars[2].vval.v_dict;
9918 }
9919
Bram Moolenaardb913952012-06-29 12:54:53 +02009920 (void)func_call(func, &argvars[1], selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009921}
9922
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009923#ifdef FEAT_FLOAT
9924/*
9925 * "ceil({float})" function
9926 */
9927 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009928f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009929{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009930 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009931
9932 rettv->v_type = VAR_FLOAT;
9933 if (get_float_arg(argvars, &f) == OK)
9934 rettv->vval.v_float = ceil(f);
9935 else
9936 rettv->vval.v_float = 0.0;
9937}
9938#endif
9939
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009940#if defined(FEAT_CHANNEL) || defined(FEAT_JOB)
Bram Moolenaarf57969a2016-02-02 20:47:49 +01009941/*
9942 * Get a callback from "arg". It can be a Funcref or a function name.
9943 * When "arg" is zero return an empty string.
9944 * Return NULL for an invalid argument.
9945 */
9946 static char_u *
9947get_callback(typval_T *arg)
9948{
9949 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
9950 return arg->vval.v_string;
9951 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
9952 return (char_u *)"";
9953 EMSG(_("E999: Invalid callback argument"));
9954 return NULL;
9955}
9956
Bram Moolenaarb6b52522016-02-20 23:30:07 +01009957 static int
9958handle_mode(typval_T *item, jobopt_T *opt, ch_mode_T *modep, int jo)
9959{
9960 char_u *val = get_tv_string(item);
9961
9962 opt->jo_set |= jo;
9963 if (STRCMP(val, "nl") == 0)
9964 *modep = MODE_NL;
9965 else if (STRCMP(val, "raw") == 0)
9966 *modep = MODE_RAW;
9967 else if (STRCMP(val, "js") == 0)
9968 *modep = MODE_JS;
9969 else if (STRCMP(val, "json") == 0)
9970 *modep = MODE_JSON;
9971 else
9972 {
9973 EMSG2(_(e_invarg2), val);
9974 return FAIL;
9975 }
9976 return OK;
9977}
9978
Bram Moolenaar187db502016-02-27 14:44:26 +01009979 static int
9980handle_io(typval_T *item, int part, jobopt_T *opt)
9981{
9982 char_u *val = get_tv_string(item);
9983
9984 opt->jo_set |= JO_OUT_IO << (part - PART_OUT);
9985 if (STRCMP(val, "null") == 0)
9986 opt->jo_io[part] = JIO_NULL;
9987 else if (STRCMP(val, "pipe") == 0)
9988 opt->jo_io[part] = JIO_PIPE;
9989 else if (STRCMP(val, "file") == 0)
9990 opt->jo_io[part] = JIO_FILE;
9991 else if (STRCMP(val, "buffer") == 0)
9992 opt->jo_io[part] = JIO_BUFFER;
9993 else if (STRCMP(val, "out") == 0 && part == PART_ERR)
9994 opt->jo_io[part] = JIO_OUT;
9995 else
9996 {
9997 EMSG2(_(e_invarg2), val);
9998 return FAIL;
9999 }
10000 return OK;
10001}
10002
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010003 static void
10004clear_job_options(jobopt_T *opt)
10005{
10006 vim_memset(opt, 0, sizeof(jobopt_T));
10007}
10008
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010009/*
Bram Moolenaar187db502016-02-27 14:44:26 +010010010 * Get the PART_ number from the first character of an option name.
10011 */
10012 static int
10013part_from_char(int c)
10014{
10015 return c == 'i' ? PART_IN : c == 'o' ? PART_OUT: PART_ERR;
10016}
10017
10018/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010019 * Get the option entries from the dict in "tv", parse them and put the result
10020 * in "opt".
10021 * Only accept options in "supported".
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010022 * If an option value is invalid return FAIL.
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010023 */
10024 static int
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010025get_job_options(typval_T *tv, jobopt_T *opt, int supported)
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010026{
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010027 typval_T *item;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010028 char_u *val;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010029 dict_T *dict;
10030 int todo;
10031 hashitem_T *hi;
Bram Moolenaar187db502016-02-27 14:44:26 +010010032 int part;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010033
Bram Moolenaar8600ace2016-02-19 23:31:40 +010010034 opt->jo_set = 0;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010035 if (tv->v_type == VAR_UNKNOWN)
10036 return OK;
10037 if (tv->v_type != VAR_DICT)
10038 {
10039 EMSG(_(e_invarg));
10040 return FAIL;
10041 }
10042 dict = tv->vval.v_dict;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010043 if (dict == NULL)
10044 return OK;
10045
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010046 todo = (int)dict->dv_hashtab.ht_used;
10047 for (hi = dict->dv_hashtab.ht_array; todo > 0; ++hi)
10048 if (!HASHITEM_EMPTY(hi))
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010049 {
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010050 item = &HI2DI(hi)->di_tv;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010051
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010052 if (STRCMP(hi->hi_key, "mode") == 0)
10053 {
10054 if (!(supported & JO_MODE))
10055 break;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010056 if (handle_mode(item, opt, &opt->jo_mode, JO_MODE) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010057 return FAIL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010058 }
10059 else if (STRCMP(hi->hi_key, "in-mode") == 0)
10060 {
10061 if (!(supported & JO_IN_MODE))
10062 break;
10063 if (handle_mode(item, opt, &opt->jo_in_mode, JO_IN_MODE)
10064 == FAIL)
10065 return FAIL;
10066 }
10067 else if (STRCMP(hi->hi_key, "out-mode") == 0)
10068 {
10069 if (!(supported & JO_OUT_MODE))
10070 break;
10071 if (handle_mode(item, opt, &opt->jo_out_mode, JO_OUT_MODE)
10072 == FAIL)
10073 return FAIL;
10074 }
10075 else if (STRCMP(hi->hi_key, "err-mode") == 0)
10076 {
10077 if (!(supported & JO_ERR_MODE))
10078 break;
10079 if (handle_mode(item, opt, &opt->jo_err_mode, JO_ERR_MODE)
10080 == FAIL)
10081 return FAIL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010082 }
Bram Moolenaar187db502016-02-27 14:44:26 +010010083 else if (STRCMP(hi->hi_key, "in-io") == 0
10084 || STRCMP(hi->hi_key, "out-io") == 0
10085 || STRCMP(hi->hi_key, "err-io") == 0)
10086 {
10087 if (!(supported & JO_OUT_IO))
10088 break;
10089 if (handle_io(item, part_from_char(*hi->hi_key), opt) == FAIL)
10090 return FAIL;
10091 }
10092 else if (STRCMP(hi->hi_key, "in-name") == 0
10093 || STRCMP(hi->hi_key, "out-name") == 0
10094 || STRCMP(hi->hi_key, "err-name") == 0)
10095 {
10096 part = part_from_char(*hi->hi_key);
10097
10098 if (!(supported & JO_OUT_IO))
10099 break;
10100 opt->jo_set |= JO_OUT_NAME << (part - PART_OUT);
10101 opt->jo_io_name[part] =
10102 get_tv_string_buf_chk(item, opt->jo_io_name_buf[part]);
10103 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010104 else if (STRCMP(hi->hi_key, "callback") == 0)
10105 {
10106 if (!(supported & JO_CALLBACK))
10107 break;
10108 opt->jo_set |= JO_CALLBACK;
10109 opt->jo_callback = get_callback(item);
10110 if (opt->jo_callback == NULL)
10111 {
10112 EMSG2(_(e_invarg2), "callback");
10113 return FAIL;
10114 }
10115 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010116 else if (STRCMP(hi->hi_key, "out-cb") == 0)
10117 {
10118 if (!(supported & JO_OUT_CALLBACK))
10119 break;
10120 opt->jo_set |= JO_OUT_CALLBACK;
10121 opt->jo_out_cb = get_callback(item);
10122 if (opt->jo_out_cb == NULL)
10123 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010124 EMSG2(_(e_invarg2), "out-cb");
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010125 return FAIL;
10126 }
10127 }
10128 else if (STRCMP(hi->hi_key, "err-cb") == 0)
10129 {
10130 if (!(supported & JO_ERR_CALLBACK))
10131 break;
10132 opt->jo_set |= JO_ERR_CALLBACK;
10133 opt->jo_err_cb = get_callback(item);
10134 if (opt->jo_err_cb == NULL)
10135 {
10136 EMSG2(_(e_invarg2), "err-cb");
10137 return FAIL;
10138 }
10139 }
Bram Moolenaar4e221c92016-02-23 13:20:22 +010010140 else if (STRCMP(hi->hi_key, "close-cb") == 0)
10141 {
10142 if (!(supported & JO_CLOSE_CALLBACK))
10143 break;
10144 opt->jo_set |= JO_CLOSE_CALLBACK;
10145 opt->jo_close_cb = get_callback(item);
10146 if (opt->jo_close_cb == NULL)
10147 {
10148 EMSG2(_(e_invarg2), "close-cb");
10149 return FAIL;
10150 }
10151 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010152 else if (STRCMP(hi->hi_key, "waittime") == 0)
10153 {
10154 if (!(supported & JO_WAITTIME))
10155 break;
10156 opt->jo_set |= JO_WAITTIME;
10157 opt->jo_waittime = get_tv_number(item);
10158 }
10159 else if (STRCMP(hi->hi_key, "timeout") == 0)
10160 {
10161 if (!(supported & JO_TIMEOUT))
10162 break;
10163 opt->jo_set |= JO_TIMEOUT;
10164 opt->jo_timeout = get_tv_number(item);
10165 }
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010166 else if (STRCMP(hi->hi_key, "out-timeout") == 0)
10167 {
10168 if (!(supported & JO_OUT_TIMEOUT))
10169 break;
10170 opt->jo_set |= JO_OUT_TIMEOUT;
10171 opt->jo_out_timeout = get_tv_number(item);
10172 }
10173 else if (STRCMP(hi->hi_key, "err-timeout") == 0)
10174 {
10175 if (!(supported & JO_ERR_TIMEOUT))
10176 break;
10177 opt->jo_set |= JO_ERR_TIMEOUT;
10178 opt->jo_err_timeout = get_tv_number(item);
10179 }
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010180 else if (STRCMP(hi->hi_key, "part") == 0)
10181 {
10182 if (!(supported & JO_PART))
10183 break;
10184 opt->jo_set |= JO_PART;
10185 val = get_tv_string(item);
10186 if (STRCMP(val, "err") == 0)
10187 opt->jo_part = PART_ERR;
10188 else
10189 {
10190 EMSG2(_(e_invarg2), val);
10191 return FAIL;
10192 }
10193 }
10194 else if (STRCMP(hi->hi_key, "id") == 0)
10195 {
10196 if (!(supported & JO_ID))
10197 break;
10198 opt->jo_set |= JO_ID;
10199 opt->jo_id = get_tv_number(item);
10200 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010010201 else if (STRCMP(hi->hi_key, "stoponexit") == 0)
10202 {
10203 if (!(supported & JO_STOPONEXIT))
10204 break;
10205 opt->jo_set |= JO_STOPONEXIT;
10206 opt->jo_stoponexit = get_tv_string_buf_chk(item,
10207 opt->jo_soe_buf);
10208 if (opt->jo_stoponexit == NULL)
10209 {
10210 EMSG2(_(e_invarg2), "stoponexit");
10211 return FAIL;
10212 }
10213 }
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010214 else if (STRCMP(hi->hi_key, "exit-cb") == 0)
10215 {
10216 if (!(supported & JO_EXIT_CB))
10217 break;
10218 opt->jo_set |= JO_EXIT_CB;
10219 opt->jo_exit_cb = get_tv_string_buf_chk(item, opt->jo_ecb_buf);
Bram Moolenaar5e838402016-02-21 23:12:41 +010010220 if (opt->jo_exit_cb == NULL)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010010221 {
10222 EMSG2(_(e_invarg2), "exit-cb");
10223 return FAIL;
10224 }
10225 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010226 else
10227 break;
10228 --todo;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010229 }
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010230 if (todo > 0)
10231 {
10232 EMSG2(_(e_invarg2), hi->hi_key);
10233 return FAIL;
Bram Moolenaar910b8aa2016-02-16 21:03:07 +010010234 }
10235
Bram Moolenaar187db502016-02-27 14:44:26 +010010236 for (part = PART_OUT; part <= PART_IN; ++part)
10237 if (opt->jo_io[part] == JIO_BUFFER && opt->jo_io_name[part] == NULL)
10238 {
10239 EMSG(_("E915: Missing name for buffer"));
10240 return FAIL;
10241 }
10242
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010243 return OK;
10244}
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010245#endif
10246
10247#ifdef FEAT_CHANNEL
10248/*
10249 * Get the channel from the argument.
10250 * Returns NULL if the handle is invalid.
10251 */
10252 static channel_T *
10253get_channel_arg(typval_T *tv)
10254{
10255 channel_T *channel;
10256
10257 if (tv->v_type != VAR_CHANNEL)
10258 {
10259 EMSG2(_(e_invarg2), get_tv_string(tv));
10260 return NULL;
10261 }
10262 channel = tv->vval.v_channel;
10263
10264 if (channel == NULL || !channel_is_open(channel))
10265 {
10266 EMSG(_("E906: not an open channel"));
10267 return NULL;
10268 }
10269 return channel;
10270}
10271
10272/*
10273 * "ch_close()" function
10274 */
10275 static void
10276f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10277{
10278 channel_T *channel = get_channel_arg(&argvars[0]);
10279
10280 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010281 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010282 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010283 channel_clear(channel);
10284 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010285}
10286
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010287# ifdef FEAT_JOB
10288/*
10289 * "ch_getjob()" function
10290 */
10291 static void
10292f_ch_getjob(typval_T *argvars, typval_T *rettv)
10293{
10294 channel_T *channel = get_channel_arg(&argvars[0]);
10295
10296 if (channel != NULL)
10297 {
10298 rettv->v_type = VAR_JOB;
10299 rettv->vval.v_job = channel->ch_job;
10300 if (channel->ch_job != NULL)
10301 ++channel->ch_job->jv_refcount;
10302 }
10303}
10304# endif
10305
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010306/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010307 * "ch_log()" function
10308 */
10309 static void
10310f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10311{
10312 char_u *msg = get_tv_string(&argvars[0]);
10313 channel_T *channel = NULL;
10314
10315 if (argvars[1].v_type != VAR_UNKNOWN)
10316 channel = get_channel_arg(&argvars[1]);
10317
10318 ch_log(channel, (char *)msg);
10319}
10320
10321/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010322 * "ch_logfile()" function
10323 */
10324 static void
10325f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10326{
10327 char_u *fname;
10328 char_u *opt = (char_u *)"";
10329 char_u buf[NUMBUFLEN];
10330 FILE *file = NULL;
10331
10332 fname = get_tv_string(&argvars[0]);
10333 if (argvars[1].v_type == VAR_STRING)
10334 opt = get_tv_string_buf(&argvars[1], buf);
10335 if (*fname != NUL)
10336 {
10337 file = fopen((char *)fname, *opt == 'w' ? "w" : "a");
10338 if (file == NULL)
10339 {
10340 EMSG2(_(e_notopen), fname);
10341 return;
10342 }
10343 }
10344 ch_logfile(file);
10345}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010346
10347/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010348 * "ch_open()" function
10349 */
10350 static void
10351f_ch_open(typval_T *argvars, typval_T *rettv)
10352{
10353 char_u *address;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010354 char_u *p;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010355 char *rest;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010356 int port;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010357 jobopt_T opt;
Bram Moolenaar77073442016-02-13 23:23:53 +010010358 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010359
10360 /* default: fail */
Bram Moolenaar77073442016-02-13 23:23:53 +010010361 rettv->v_type = VAR_CHANNEL;
10362 rettv->vval.v_channel = NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010363
10364 address = get_tv_string(&argvars[0]);
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010365 if (argvars[1].v_type != VAR_UNKNOWN
10366 && (argvars[1].v_type != VAR_DICT || argvars[1].vval.v_dict == NULL))
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010367 {
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010368 EMSG(_(e_invarg));
10369 return;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010370 }
10371
10372 /* parse address */
10373 p = vim_strchr(address, ':');
10374 if (p == NULL)
10375 {
10376 EMSG2(_(e_invarg2), address);
10377 return;
10378 }
10379 *p++ = NUL;
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010380 port = strtol((char *)p, &rest, 10);
10381 if (*address == NUL || port <= 0 || *rest != NUL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010382 {
10383 p[-1] = ':';
10384 EMSG2(_(e_invarg2), address);
10385 return;
10386 }
10387
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010388 /* parse options */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010389 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010390 opt.jo_mode = MODE_JSON;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010391 opt.jo_timeout = 2000;
10392 if (get_job_options(&argvars[1], &opt,
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010393 JO_MODE_ALL + JO_CB_ALL + JO_WAITTIME + JO_TIMEOUT_ALL) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010394 return;
10395 if (opt.jo_timeout < 0)
Bram Moolenaar4d919d72016-02-05 22:36:41 +010010396 {
10397 EMSG(_(e_invarg));
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010398 return;
10399 }
10400
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010401 channel = channel_open((char *)address, port, opt.jo_waittime, NULL);
Bram Moolenaar77073442016-02-13 23:23:53 +010010402 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010403 {
Bram Moolenaar7b3ca762016-02-14 19:13:43 +010010404 rettv->vval.v_channel = channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010405 opt.jo_set = JO_ALL;
10406 channel_set_options(channel, &opt);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010407 }
10408}
10409
10410/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010411 * Common for ch_read() and ch_readraw().
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010412 */
10413 static void
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010414common_channel_read(typval_T *argvars, typval_T *rettv, int raw)
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010415{
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010416 channel_T *channel;
10417 int part;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010418 jobopt_T opt;
10419 int mode;
10420 int timeout;
10421 int id = -1;
10422 typval_T *listtv = NULL;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010010423
10424 /* return an empty string by default */
10425 rettv->v_type = VAR_STRING;
10426 rettv->vval.v_string = NULL;
10427
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010428 clear_job_options(&opt);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010429 if (get_job_options(&argvars[1], &opt, JO_TIMEOUT + JO_PART + JO_ID)
10430 == FAIL)
10431 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010432
Bram Moolenaar77073442016-02-13 23:23:53 +010010433 channel = get_channel_arg(&argvars[0]);
10434 if (channel != NULL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010435 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010436 if (opt.jo_set & JO_PART)
10437 part = opt.jo_part;
10438 else
10439 part = channel_part_read(channel);
10440 mode = channel_get_mode(channel, part);
10441 timeout = channel_get_timeout(channel, part);
10442 if (opt.jo_set & JO_TIMEOUT)
10443 timeout = opt.jo_timeout;
10444
10445 if (raw || mode == MODE_RAW || mode == MODE_NL)
10446 rettv->vval.v_string = channel_read_block(channel, part, timeout);
10447 else
10448 {
10449 if (opt.jo_set & JO_ID)
10450 id = opt.jo_id;
10451 channel_read_json_block(channel, part, timeout, id, &listtv);
10452 if (listtv != NULL)
10453 *rettv = *listtv;
10454 else
10455 {
10456 rettv->v_type = VAR_SPECIAL;
10457 rettv->vval.v_number = VVAL_NONE;
10458 }
10459 }
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010460 }
Bram Moolenaar77073442016-02-13 23:23:53 +010010461}
10462
10463/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010464 * "ch_read()" function
10465 */
10466 static void
10467f_ch_read(typval_T *argvars, typval_T *rettv)
10468{
10469 common_channel_read(argvars, rettv, FALSE);
10470}
10471
10472/*
10473 * "ch_readraw()" function
10474 */
10475 static void
10476f_ch_readraw(typval_T *argvars, typval_T *rettv)
10477{
10478 common_channel_read(argvars, rettv, TRUE);
10479}
10480
10481/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010482 * common for "sendexpr()" and "sendraw()"
Bram Moolenaar77073442016-02-13 23:23:53 +010010483 * Returns the channel if the caller should read the response.
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010484 * Sets "part_read" to the the read fd.
Bram Moolenaar77073442016-02-13 23:23:53 +010010485 * Otherwise returns NULL.
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010486 */
Bram Moolenaar77073442016-02-13 23:23:53 +010010487 static channel_T *
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010488send_common(typval_T *argvars, char_u *text, int id, char *fun, int *part_read)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010489{
Bram Moolenaar77073442016-02-13 23:23:53 +010010490 channel_T *channel;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010491 jobopt_T opt;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010492 int part_send;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010493
Bram Moolenaar77073442016-02-13 23:23:53 +010010494 channel = get_channel_arg(&argvars[0]);
10495 if (channel == NULL)
10496 return NULL;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010497 part_send = channel_part_send(channel);
10498 *part_read = channel_part_read(channel);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010499
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010500 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010501 if (get_job_options(&argvars[2], &opt, JO_CALLBACK) == FAIL)
10502 return NULL;
10503
Bram Moolenaara07fec92016-02-05 21:04:08 +010010504 /* Set the callback. An empty callback means no callback and not reading
10505 * the response. */
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010506 if (opt.jo_callback != NULL && *opt.jo_callback != NUL)
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010507 channel_set_req_callback(channel, part_send, opt.jo_callback, id);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010508
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010509 if (channel_send(channel, part_send, text, fun) == OK
10510 && opt.jo_callback == NULL)
Bram Moolenaar77073442016-02-13 23:23:53 +010010511 return channel;
10512 return NULL;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010513}
10514
10515/*
10516 * "ch_sendexpr()" function
10517 */
10518 static void
10519f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10520{
10521 char_u *text;
10522 typval_T *listtv;
Bram Moolenaar77073442016-02-13 23:23:53 +010010523 channel_T *channel;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010524 int id;
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010525 ch_mode_T ch_mode;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010526 int part_send;
10527 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010528 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010529
10530 /* return an empty string by default */
10531 rettv->v_type = VAR_STRING;
10532 rettv->vval.v_string = NULL;
10533
Bram Moolenaar77073442016-02-13 23:23:53 +010010534 channel = get_channel_arg(&argvars[0]);
10535 if (channel == NULL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010536 return;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010537 part_send = channel_part_send(channel);
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010538
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010539 ch_mode = channel_get_mode(channel, part_send);
10540 if (ch_mode == MODE_RAW || ch_mode == MODE_NL)
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010541 {
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010542 EMSG(_("E912: cannot use ch_sendexpr() with a raw or nl channel"));
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010543 return;
10544 }
10545
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010546 id = channel_get_id();
Bram Moolenaarae8eb3c2016-02-07 21:59:26 +010010547 text = json_encode_nr_expr(id, &argvars[1],
10548 ch_mode == MODE_JS ? JSON_JS : 0);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010549 if (text == NULL)
10550 return;
10551
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010552 channel = send_common(argvars, text, id, "sendexpr", &part_read);
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +010010553 vim_free(text);
Bram Moolenaar77073442016-02-13 23:23:53 +010010554 if (channel != NULL)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010555 {
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010556 /* TODO: timeout from options */
10557 timeout = channel_get_timeout(channel, part_read);
10558 if (channel_read_json_block(channel, part_read, timeout, id, &listtv)
10559 == OK)
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010560 {
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010561 list_T *list = listtv->vval.v_list;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010562
Bram Moolenaar6076fe12016-02-05 22:49:56 +010010563 /* Move the item from the list and then change the type to
10564 * avoid the value being freed. */
10565 *rettv = list->lv_last->li_tv;
10566 list->lv_last->li_tv.v_type = VAR_NUMBER;
Bram Moolenaar77073442016-02-13 23:23:53 +010010567 free_tv(listtv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010568 }
10569 }
10570}
10571
10572/*
10573 * "ch_sendraw()" function
10574 */
10575 static void
10576f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10577{
10578 char_u buf[NUMBUFLEN];
10579 char_u *text;
Bram Moolenaar77073442016-02-13 23:23:53 +010010580 channel_T *channel;
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010581 int part_read;
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010582 int timeout;
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010583
10584 /* return an empty string by default */
10585 rettv->v_type = VAR_STRING;
10586 rettv->vval.v_string = NULL;
10587
10588 text = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar42d38a22016-02-20 18:18:59 +010010589 channel = send_common(argvars, text, 0, "sendraw", &part_read);
Bram Moolenaar77073442016-02-13 23:23:53 +010010590 if (channel != NULL)
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010591 {
10592 /* TODO: timeout from options */
10593 timeout = channel_get_timeout(channel, part_read);
10594 rettv->vval.v_string = channel_read_block(channel, part_read, timeout);
10595 }
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010596}
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010597
10598/*
10599 * "ch_setoptions()" function
10600 */
10601 static void
10602f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10603{
10604 channel_T *channel;
10605 jobopt_T opt;
10606
10607 channel = get_channel_arg(&argvars[0]);
10608 if (channel == NULL)
10609 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010610 clear_job_options(&opt);
10611 if (get_job_options(&argvars[1], &opt,
10612 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010613 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010614 channel_set_options(channel, &opt);
10615}
10616
10617/*
10618 * "ch_status()" function
10619 */
10620 static void
10621f_ch_status(typval_T *argvars, typval_T *rettv)
10622{
10623 /* return an empty string by default */
10624 rettv->v_type = VAR_STRING;
10625
10626 if (argvars[0].v_type != VAR_CHANNEL)
10627 {
10628 EMSG2(_(e_invarg2), get_tv_string(&argvars[0]));
10629 rettv->vval.v_string = NULL;
10630 }
10631 else
10632 rettv->vval.v_string = vim_strsave(
10633 (char_u *)channel_status(argvars[0].vval.v_channel));
10634}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010635#endif
10636
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010637/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010638 * "changenr()" function
10639 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010641f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010642{
10643 rettv->vval.v_number = curbuf->b_u_seq_cur;
10644}
10645
10646/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 * "char2nr(string)" function
10648 */
10649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010650f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651{
10652#ifdef FEAT_MBYTE
10653 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010654 {
10655 int utf8 = 0;
10656
10657 if (argvars[1].v_type != VAR_UNKNOWN)
10658 utf8 = get_tv_number_chk(&argvars[1], NULL);
10659
10660 if (utf8)
10661 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10662 else
10663 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10664 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010665 else
10666#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010667 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668}
10669
10670/*
10671 * "cindent(lnum)" function
10672 */
10673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010674f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010675{
10676#ifdef FEAT_CINDENT
10677 pos_T pos;
10678 linenr_T lnum;
10679
10680 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010681 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010682 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10683 {
10684 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010685 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010686 curwin->w_cursor = pos;
10687 }
10688 else
10689#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010690 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010691}
10692
10693/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010694 * "clearmatches()" function
10695 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010697f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010698{
10699#ifdef FEAT_SEARCH_EXTRA
10700 clear_matches(curwin);
10701#endif
10702}
10703
10704/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010705 * "col(string)" function
10706 */
10707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010708f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010709{
10710 colnr_T col = 0;
10711 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010712 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010714 fp = var2fpos(&argvars[0], FALSE, &fnum);
10715 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010716 {
10717 if (fp->col == MAXCOL)
10718 {
10719 /* '> can be MAXCOL, get the length of the line then */
10720 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010721 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722 else
10723 col = MAXCOL;
10724 }
10725 else
10726 {
10727 col = fp->col + 1;
10728#ifdef FEAT_VIRTUALEDIT
10729 /* col(".") when the cursor is on the NUL at the end of the line
10730 * because of "coladd" can be seen as an extra column. */
10731 if (virtual_active() && fp == &curwin->w_cursor)
10732 {
10733 char_u *p = ml_get_cursor();
10734
10735 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10736 curwin->w_virtcol - curwin->w_cursor.coladd))
10737 {
10738# ifdef FEAT_MBYTE
10739 int l;
10740
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010741 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010742 col += l;
10743# else
10744 if (*p != NUL && p[1] == NUL)
10745 ++col;
10746# endif
10747 }
10748 }
10749#endif
10750 }
10751 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010752 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010753}
10754
Bram Moolenaar572cb562005-08-05 21:35:02 +000010755#if defined(FEAT_INS_EXPAND)
10756/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010757 * "complete()" function
10758 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010760f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010761{
10762 int startcol;
10763
10764 if ((State & INSERT) == 0)
10765 {
10766 EMSG(_("E785: complete() can only be used in Insert mode"));
10767 return;
10768 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010769
10770 /* Check for undo allowed here, because if something was already inserted
10771 * the line was already saved for undo and this check isn't done. */
10772 if (!undo_allowed())
10773 return;
10774
Bram Moolenaarade00832006-03-10 21:46:58 +000010775 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10776 {
10777 EMSG(_(e_invarg));
10778 return;
10779 }
10780
10781 startcol = get_tv_number_chk(&argvars[0], NULL);
10782 if (startcol <= 0)
10783 return;
10784
10785 set_completion(startcol - 1, argvars[1].vval.v_list);
10786}
10787
10788/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010789 * "complete_add()" function
10790 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010791 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010792f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010793{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010794 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010795}
10796
10797/*
10798 * "complete_check()" function
10799 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010801f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010802{
10803 int saved = RedrawingDisabled;
10804
10805 RedrawingDisabled = 0;
10806 ins_compl_check_keys(0);
10807 rettv->vval.v_number = compl_interrupted;
10808 RedrawingDisabled = saved;
10809}
10810#endif
10811
Bram Moolenaar071d4272004-06-13 20:20:40 +000010812/*
10813 * "confirm(message, buttons[, default [, type]])" function
10814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010816f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010817{
10818#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10819 char_u *message;
10820 char_u *buttons = NULL;
10821 char_u buf[NUMBUFLEN];
10822 char_u buf2[NUMBUFLEN];
10823 int def = 1;
10824 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010825 char_u *typestr;
10826 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010827
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 message = get_tv_string_chk(&argvars[0]);
10829 if (message == NULL)
10830 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010831 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010832 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010833 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10834 if (buttons == NULL)
10835 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010836 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010837 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010838 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010839 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010841 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10842 if (typestr == NULL)
10843 error = TRUE;
10844 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010845 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010846 switch (TOUPPER_ASC(*typestr))
10847 {
10848 case 'E': type = VIM_ERROR; break;
10849 case 'Q': type = VIM_QUESTION; break;
10850 case 'I': type = VIM_INFO; break;
10851 case 'W': type = VIM_WARNING; break;
10852 case 'G': type = VIM_GENERIC; break;
10853 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010854 }
10855 }
10856 }
10857 }
10858
10859 if (buttons == NULL || *buttons == NUL)
10860 buttons = (char_u *)_("&Ok");
10861
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010862 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010863 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010864 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010865#endif
10866}
10867
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010868/*
10869 * "copy()" function
10870 */
10871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010872f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010873{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010874 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010875}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010876
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010877#ifdef FEAT_FLOAT
10878/*
10879 * "cos()" function
10880 */
10881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010882f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010883{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010884 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010885
10886 rettv->v_type = VAR_FLOAT;
10887 if (get_float_arg(argvars, &f) == OK)
10888 rettv->vval.v_float = cos(f);
10889 else
10890 rettv->vval.v_float = 0.0;
10891}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010892
10893/*
10894 * "cosh()" function
10895 */
10896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010897f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010898{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010899 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010900
10901 rettv->v_type = VAR_FLOAT;
10902 if (get_float_arg(argvars, &f) == OK)
10903 rettv->vval.v_float = cosh(f);
10904 else
10905 rettv->vval.v_float = 0.0;
10906}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010907#endif
10908
Bram Moolenaar071d4272004-06-13 20:20:40 +000010909/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010910 * "count()" function
10911 */
10912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010913f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010914{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010915 long n = 0;
10916 int ic = FALSE;
10917
Bram Moolenaare9a41262005-01-15 22:18:47 +000010918 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010919 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010920 listitem_T *li;
10921 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010922 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010923
Bram Moolenaare9a41262005-01-15 22:18:47 +000010924 if ((l = argvars[0].vval.v_list) != NULL)
10925 {
10926 li = l->lv_first;
10927 if (argvars[2].v_type != VAR_UNKNOWN)
10928 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010929 int error = FALSE;
10930
10931 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010932 if (argvars[3].v_type != VAR_UNKNOWN)
10933 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010934 idx = get_tv_number_chk(&argvars[3], &error);
10935 if (!error)
10936 {
10937 li = list_find(l, idx);
10938 if (li == NULL)
10939 EMSGN(_(e_listidx), idx);
10940 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010941 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 if (error)
10943 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010944 }
10945
10946 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010947 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010948 ++n;
10949 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010950 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010951 else if (argvars[0].v_type == VAR_DICT)
10952 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010953 int todo;
10954 dict_T *d;
10955 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010956
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010957 if ((d = argvars[0].vval.v_dict) != NULL)
10958 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010959 int error = FALSE;
10960
Bram Moolenaare9a41262005-01-15 22:18:47 +000010961 if (argvars[2].v_type != VAR_UNKNOWN)
10962 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010964 if (argvars[3].v_type != VAR_UNKNOWN)
10965 EMSG(_(e_invarg));
10966 }
10967
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010968 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010969 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010970 {
10971 if (!HASHITEM_EMPTY(hi))
10972 {
10973 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010974 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010975 ++n;
10976 }
10977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010978 }
10979 }
10980 else
10981 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010982 rettv->vval.v_number = n;
10983}
10984
10985/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010986 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10987 *
10988 * Checks the existence of a cscope connection.
10989 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010991f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992{
10993#ifdef FEAT_CSCOPE
10994 int num = 0;
10995 char_u *dbpath = NULL;
10996 char_u *prepend = NULL;
10997 char_u buf[NUMBUFLEN];
10998
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010999 if (argvars[0].v_type != VAR_UNKNOWN
11000 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011002 num = (int)get_tv_number(&argvars[0]);
11003 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011004 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 }
11007
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011008 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011009#endif
11010}
11011
11012/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011013 * "cursor(lnum, col)" function, or
11014 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011016 * Moves the cursor to the specified line and column.
11017 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000011018 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011020f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021{
11022 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011023#ifdef FEAT_VIRTUALEDIT
11024 long coladd = 0;
11025#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011026 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011027
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011028 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011029 if (argvars[1].v_type == VAR_UNKNOWN)
11030 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011031 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020011032 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000011033
Bram Moolenaar493c1782014-05-28 14:34:46 +020011034 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011035 {
11036 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000011037 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010011038 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011039 line = pos.lnum;
11040 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000011041#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000011042 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000011043#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020011044 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011045 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020011046 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011047 set_curswant = FALSE;
11048 }
Bram Moolenaara5525202006-03-02 22:52:09 +000011049 }
11050 else
11051 {
11052 line = get_tv_lnum(argvars);
11053 col = get_tv_number_chk(&argvars[1], NULL);
11054#ifdef FEAT_VIRTUALEDIT
11055 if (argvars[2].v_type != VAR_UNKNOWN)
11056 coladd = get_tv_number_chk(&argvars[2], NULL);
11057#endif
11058 }
11059 if (line < 0 || col < 0
11060#ifdef FEAT_VIRTUALEDIT
11061 || coladd < 0
11062#endif
11063 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011064 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011065 if (line > 0)
11066 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011067 if (col > 0)
11068 curwin->w_cursor.col = col - 1;
11069#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000011070 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071#endif
11072
11073 /* Make sure the cursor is in a valid position. */
11074 check_cursor();
11075#ifdef FEAT_MBYTE
11076 /* Correct cursor for multi-byte character. */
11077 if (has_mbyte)
11078 mb_adjust_cursor();
11079#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000011080
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010011081 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000011082 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011083}
11084
11085/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011086 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000011087 */
11088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011089f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011091 int noref = 0;
11092
11093 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011094 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000011095 if (noref < 0 || noref > 1)
11096 EMSG(_(e_invarg));
11097 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000011098 {
11099 current_copyID += COPYID_INC;
11100 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
11101 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102}
11103
11104/*
11105 * "delete()" function
11106 */
11107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011108f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011109{
Bram Moolenaarda440d22016-01-16 21:27:23 +010011110 char_u nbuf[NUMBUFLEN];
11111 char_u *name;
11112 char_u *flags;
11113
11114 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010011116 return;
11117
11118 name = get_tv_string(&argvars[0]);
11119 if (name == NULL || *name == NUL)
11120 {
11121 EMSG(_(e_invarg));
11122 return;
11123 }
11124
11125 if (argvars[1].v_type != VAR_UNKNOWN)
11126 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011127 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010011128 flags = (char_u *)"";
11129
11130 if (*flags == NUL)
11131 /* delete a file */
11132 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
11133 else if (STRCMP(flags, "d") == 0)
11134 /* delete an empty directory */
11135 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
11136 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010011137 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010011138 rettv->vval.v_number = delete_recursive(name);
11139 else
11140 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011141}
11142
11143/*
11144 * "did_filetype()" function
11145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011147f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011148{
11149#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011150 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011151#endif
11152}
11153
11154/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000011155 * "diff_filler()" function
11156 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011158f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011159{
11160#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011161 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000011162#endif
11163}
11164
11165/*
11166 * "diff_hlID()" function
11167 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011169f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000011170{
11171#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011172 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000011173 static linenr_T prev_lnum = 0;
11174 static int changedtick = 0;
11175 static int fnum = 0;
11176 static int change_start = 0;
11177 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000011178 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011179 int filler_lines;
11180 int col;
11181
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011182 if (lnum < 0) /* ignore type error in {lnum} arg */
11183 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011184 if (lnum != prev_lnum
11185 || changedtick != curbuf->b_changedtick
11186 || fnum != curbuf->b_fnum)
11187 {
11188 /* New line, buffer, change: need to get the values. */
11189 filler_lines = diff_check(curwin, lnum);
11190 if (filler_lines < 0)
11191 {
11192 if (filler_lines == -1)
11193 {
11194 change_start = MAXCOL;
11195 change_end = -1;
11196 if (diff_find_change(curwin, lnum, &change_start, &change_end))
11197 hlID = HLF_ADD; /* added line */
11198 else
11199 hlID = HLF_CHD; /* changed line */
11200 }
11201 else
11202 hlID = HLF_ADD; /* added line */
11203 }
11204 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011205 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011206 prev_lnum = lnum;
11207 changedtick = curbuf->b_changedtick;
11208 fnum = curbuf->b_fnum;
11209 }
11210
11211 if (hlID == HLF_CHD || hlID == HLF_TXD)
11212 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011213 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000011214 if (col >= change_start && col <= change_end)
11215 hlID = HLF_TXD; /* changed text */
11216 else
11217 hlID = HLF_CHD; /* changed line */
11218 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000011219 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000011220#endif
11221}
11222
11223/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010011224 * "disable_char_avail_for_testing({expr})" function
11225 */
11226 static void
11227f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
11228{
11229 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
11230}
11231
11232/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011233 * "empty({expr})" function
11234 */
11235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011236f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011237{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010011238 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011239
11240 switch (argvars[0].v_type)
11241 {
11242 case VAR_STRING:
11243 case VAR_FUNC:
11244 n = argvars[0].vval.v_string == NULL
11245 || *argvars[0].vval.v_string == NUL;
11246 break;
11247 case VAR_NUMBER:
11248 n = argvars[0].vval.v_number == 0;
11249 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011250 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010011251#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011252 n = argvars[0].vval.v_float == 0.0;
11253 break;
11254#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011255 case VAR_LIST:
11256 n = argvars[0].vval.v_list == NULL
11257 || argvars[0].vval.v_list->lv_first == NULL;
11258 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011259 case VAR_DICT:
11260 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000011261 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011262 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010011263 case VAR_SPECIAL:
11264 n = argvars[0].vval.v_number != VVAL_TRUE;
11265 break;
11266
Bram Moolenaar835dc632016-02-07 14:27:38 +010011267 case VAR_JOB:
11268#ifdef FEAT_JOB
Bram Moolenaar77073442016-02-13 23:23:53 +010011269 n = argvars[0].vval.v_job == NULL
11270 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
11271 break;
11272#endif
11273 case VAR_CHANNEL:
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010011274#ifdef FEAT_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010011275 n = argvars[0].vval.v_channel == NULL
11276 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010011277 break;
11278#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010011279 case VAR_UNKNOWN:
11280 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
11281 n = TRUE;
11282 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011283 }
11284
11285 rettv->vval.v_number = n;
11286}
11287
11288/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011289 * "escape({string}, {chars})" function
11290 */
11291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011292f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011293{
11294 char_u buf[NUMBUFLEN];
11295
Bram Moolenaar758711c2005-02-02 23:11:38 +000011296 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
11297 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011299}
11300
11301/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011302 * "eval()" function
11303 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011305f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011306{
Bram Moolenaar615b9972015-01-14 17:15:05 +010011307 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011308
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011309 s = get_tv_string_chk(&argvars[0]);
11310 if (s != NULL)
11311 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011312
Bram Moolenaar615b9972015-01-14 17:15:05 +010011313 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011314 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
11315 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010011316 if (p != NULL && !aborting())
11317 EMSG2(_(e_invexpr2), p);
11318 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011319 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011320 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011321 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011322 else if (*s != NUL)
11323 EMSG(_(e_trailing));
11324}
11325
11326/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 * "eventhandler()" function
11328 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011330f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011331{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011332 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011333}
11334
11335/*
11336 * "executable()" function
11337 */
11338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011339f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011340{
Bram Moolenaarb5971142015-03-21 17:32:19 +010011341 char_u *name = get_tv_string(&argvars[0]);
11342
11343 /* Check in $PATH and also check directly if there is a directory name. */
11344 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
11345 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011346}
11347
11348/*
11349 * "exepath()" function
11350 */
11351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011352f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011353{
11354 char_u *p = NULL;
11355
Bram Moolenaarb5971142015-03-21 17:32:19 +010011356 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011357 rettv->v_type = VAR_STRING;
11358 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011359}
11360
11361/*
11362 * "exists()" function
11363 */
11364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011365f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011366{
11367 char_u *p;
11368 char_u *name;
11369 int n = FALSE;
11370 int len = 0;
11371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011372 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011373 if (*p == '$') /* environment variable */
11374 {
11375 /* first try "normal" environment variables (fast) */
11376 if (mch_getenv(p + 1) != NULL)
11377 n = TRUE;
11378 else
11379 {
11380 /* try expanding things like $VIM and ${HOME} */
11381 p = expand_env_save(p);
11382 if (p != NULL && *p != '$')
11383 n = TRUE;
11384 vim_free(p);
11385 }
11386 }
11387 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011388 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011389 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011390 if (*skipwhite(p) != NUL)
11391 n = FALSE; /* trailing garbage */
11392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393 else if (*p == '*') /* internal or user defined function */
11394 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011395 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011396 }
11397 else if (*p == ':')
11398 {
11399 n = cmd_exists(p + 1);
11400 }
11401 else if (*p == '#')
11402 {
11403#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011404 if (p[1] == '#')
11405 n = autocmd_supported(p + 2);
11406 else
11407 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011408#endif
11409 }
11410 else /* internal variable */
11411 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011412 char_u *tofree;
11413 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011414
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011415 /* get_name_len() takes care of expanding curly braces */
11416 name = p;
11417 len = get_name_len(&p, &tofree, TRUE, FALSE);
11418 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011419 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011420 if (tofree != NULL)
11421 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011422 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011423 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011424 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011425 /* handle d.key, l[idx], f(expr) */
11426 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11427 if (n)
11428 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011429 }
11430 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011431 if (*p != NUL)
11432 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011433
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011434 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011435 }
11436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011437 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011438}
11439
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011440#ifdef FEAT_FLOAT
11441/*
11442 * "exp()" function
11443 */
11444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011445f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011446{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011447 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011448
11449 rettv->v_type = VAR_FLOAT;
11450 if (get_float_arg(argvars, &f) == OK)
11451 rettv->vval.v_float = exp(f);
11452 else
11453 rettv->vval.v_float = 0.0;
11454}
11455#endif
11456
Bram Moolenaar071d4272004-06-13 20:20:40 +000011457/*
11458 * "expand()" function
11459 */
11460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011461f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011462{
11463 char_u *s;
11464 int len;
11465 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011466 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011467 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011469 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011470
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011471 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011472 if (argvars[1].v_type != VAR_UNKNOWN
11473 && argvars[2].v_type != VAR_UNKNOWN
11474 && get_tv_number_chk(&argvars[2], &error)
11475 && !error)
11476 {
11477 rettv->v_type = VAR_LIST;
11478 rettv->vval.v_list = NULL;
11479 }
11480
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011481 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011482 if (*s == '%' || *s == '#' || *s == '<')
11483 {
11484 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011485 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011486 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011487 if (rettv->v_type == VAR_LIST)
11488 {
11489 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11490 list_append_string(rettv->vval.v_list, result, -1);
11491 else
11492 vim_free(result);
11493 }
11494 else
11495 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011496 }
11497 else
11498 {
11499 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011500 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 if (argvars[1].v_type != VAR_UNKNOWN
11502 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011503 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011504 if (!error)
11505 {
11506 ExpandInit(&xpc);
11507 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011508 if (p_wic)
11509 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011510 if (rettv->v_type == VAR_STRING)
11511 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11512 options, WILD_ALL);
11513 else if (rettv_list_alloc(rettv) != FAIL)
11514 {
11515 int i;
11516
11517 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11518 for (i = 0; i < xpc.xp_numfiles; i++)
11519 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11520 ExpandCleanup(&xpc);
11521 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 }
11523 else
11524 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011525 }
11526}
11527
11528/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011529 * Go over all entries in "d2" and add them to "d1".
11530 * When "action" is "error" then a duplicate key is an error.
11531 * When "action" is "force" then a duplicate key is overwritten.
11532 * Otherwise duplicate keys are ignored ("action" is "keep").
11533 */
11534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011535dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011536{
11537 dictitem_T *di1;
11538 hashitem_T *hi2;
11539 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011540 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011541
11542 todo = (int)d2->dv_hashtab.ht_used;
11543 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11544 {
11545 if (!HASHITEM_EMPTY(hi2))
11546 {
11547 --todo;
11548 di1 = dict_find(d1, hi2->hi_key, -1);
11549 if (d1->dv_scope != 0)
11550 {
11551 /* Disallow replacing a builtin function in l: and g:.
11552 * Check the key to be valid when adding to any
11553 * scope. */
11554 if (d1->dv_scope == VAR_DEF_SCOPE
11555 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11556 && var_check_func_name(hi2->hi_key,
11557 di1 == NULL))
11558 break;
11559 if (!valid_varname(hi2->hi_key))
11560 break;
11561 }
11562 if (di1 == NULL)
11563 {
11564 di1 = dictitem_copy(HI2DI(hi2));
11565 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11566 dictitem_free(di1);
11567 }
11568 else if (*action == 'e')
11569 {
11570 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11571 break;
11572 }
11573 else if (*action == 'f' && HI2DI(hi2) != di1)
11574 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011575 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11576 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011577 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011578 clear_tv(&di1->di_tv);
11579 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11580 }
11581 }
11582 }
11583}
11584
11585/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011586 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011587 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011588 */
11589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011590f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011591{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011592 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011593
Bram Moolenaare9a41262005-01-15 22:18:47 +000011594 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011595 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 list_T *l1, *l2;
11597 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011598 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011599 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011600
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 l1 = argvars[0].vval.v_list;
11602 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011603 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011604 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011605 {
11606 if (argvars[2].v_type != VAR_UNKNOWN)
11607 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011608 before = get_tv_number_chk(&argvars[2], &error);
11609 if (error)
11610 return; /* type error; errmsg already given */
11611
Bram Moolenaar758711c2005-02-02 23:11:38 +000011612 if (before == l1->lv_len)
11613 item = NULL;
11614 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011616 item = list_find(l1, before);
11617 if (item == NULL)
11618 {
11619 EMSGN(_(e_listidx), before);
11620 return;
11621 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011622 }
11623 }
11624 else
11625 item = NULL;
11626 list_extend(l1, l2, item);
11627
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 copy_tv(&argvars[0], rettv);
11629 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011630 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011631 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11632 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011633 dict_T *d1, *d2;
11634 char_u *action;
11635 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011636
11637 d1 = argvars[0].vval.v_dict;
11638 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011639 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011640 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011641 {
11642 /* Check the third argument. */
11643 if (argvars[2].v_type != VAR_UNKNOWN)
11644 {
11645 static char *(av[]) = {"keep", "force", "error"};
11646
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011647 action = get_tv_string_chk(&argvars[2]);
11648 if (action == NULL)
11649 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011650 for (i = 0; i < 3; ++i)
11651 if (STRCMP(action, av[i]) == 0)
11652 break;
11653 if (i == 3)
11654 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011655 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011656 return;
11657 }
11658 }
11659 else
11660 action = (char_u *)"force";
11661
Bram Moolenaara9922d62013-05-30 13:01:18 +020011662 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011663
Bram Moolenaare9a41262005-01-15 22:18:47 +000011664 copy_tv(&argvars[0], rettv);
11665 }
11666 }
11667 else
11668 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011669}
11670
11671/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011672 * "feedkeys()" function
11673 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011675f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011676{
11677 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011678 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011679 char_u *keys, *flags;
11680 char_u nbuf[NUMBUFLEN];
11681 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011682 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011683 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011684
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011685 /* This is not allowed in the sandbox. If the commands would still be
11686 * executed in the sandbox it would be OK, but it probably happens later,
11687 * when "sandbox" is no longer set. */
11688 if (check_secure())
11689 return;
11690
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011691 keys = get_tv_string(&argvars[0]);
11692 if (*keys != NUL)
11693 {
11694 if (argvars[1].v_type != VAR_UNKNOWN)
11695 {
11696 flags = get_tv_string_buf(&argvars[1], nbuf);
11697 for ( ; *flags != NUL; ++flags)
11698 {
11699 switch (*flags)
11700 {
11701 case 'n': remap = FALSE; break;
11702 case 'm': remap = TRUE; break;
11703 case 't': typed = TRUE; break;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011704 case 'i': insert = TRUE; break;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011705 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011706 }
11707 }
11708 }
11709
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011710 /* Need to escape K_SPECIAL and CSI before putting the string in the
11711 * typeahead buffer. */
11712 keys_esc = vim_strsave_escape_csi(keys);
11713 if (keys_esc != NULL)
11714 {
11715 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011716 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011717 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011718 if (vgetc_busy)
11719 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011720 if (execute)
11721 exec_normal(TRUE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011722 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011723 }
11724}
11725
11726/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011727 * "filereadable()" function
11728 */
11729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011730f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011732 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 char_u *p;
11734 int n;
11735
Bram Moolenaarc236c162008-07-13 17:41:49 +000011736#ifndef O_NONBLOCK
11737# define O_NONBLOCK 0
11738#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011739 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011740 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11741 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 {
11743 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011744 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011745 }
11746 else
11747 n = FALSE;
11748
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011749 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011750}
11751
11752/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011753 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 * rights to write into.
11755 */
11756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011757f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011759 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760}
11761
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011762 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011763findfilendir(
11764 typval_T *argvars UNUSED,
11765 typval_T *rettv,
11766 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011767{
11768#ifdef FEAT_SEARCHPATH
11769 char_u *fname;
11770 char_u *fresult = NULL;
11771 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11772 char_u *p;
11773 char_u pathbuf[NUMBUFLEN];
11774 int count = 1;
11775 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011776 int error = FALSE;
11777#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011778
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011779 rettv->vval.v_string = NULL;
11780 rettv->v_type = VAR_STRING;
11781
11782#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011783 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011785 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011786 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011787 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11788 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011789 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011790 else
11791 {
11792 if (*p != NUL)
11793 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011794
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011795 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011796 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011797 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011798 }
11799
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011800 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11801 error = TRUE;
11802
11803 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011805 do
11806 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011807 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011808 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011809 fresult = find_file_in_path_option(first ? fname : NULL,
11810 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011811 0, first, path,
11812 find_what,
11813 curbuf->b_ffname,
11814 find_what == FINDFILE_DIR
11815 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011816 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011817
11818 if (fresult != NULL && rettv->v_type == VAR_LIST)
11819 list_append_string(rettv->vval.v_list, fresult, -1);
11820
11821 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011822 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011823
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011824 if (rettv->v_type == VAR_STRING)
11825 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011826#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011827}
11828
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011829static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11830static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011831
11832/*
11833 * Implementation of map() and filter().
11834 */
11835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011836filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011837{
11838 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011839 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011840 listitem_T *li, *nli;
11841 list_T *l = NULL;
11842 dictitem_T *di;
11843 hashtab_T *ht;
11844 hashitem_T *hi;
11845 dict_T *d = NULL;
11846 typval_T save_val;
11847 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011849 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011850 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011851 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011852 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011853 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011854 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011855
Bram Moolenaare9a41262005-01-15 22:18:47 +000011856 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011857 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011858 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011859 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860 return;
11861 }
11862 else if (argvars[0].v_type == VAR_DICT)
11863 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011864 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011865 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011866 return;
11867 }
11868 else
11869 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011870 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011871 return;
11872 }
11873
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011874 expr = get_tv_string_buf_chk(&argvars[1], buf);
11875 /* On type errors, the preceding call has already displayed an error
11876 * message. Avoid a misleading error message for an empty string that
11877 * was not passed as argument. */
11878 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011879 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011880 prepare_vimvar(VV_VAL, &save_val);
11881 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011882
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011883 /* We reset "did_emsg" to be able to detect whether an error
11884 * occurred during evaluation of the expression. */
11885 save_did_emsg = did_emsg;
11886 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011887
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011888 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011889 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011891 vimvars[VV_KEY].vv_type = VAR_STRING;
11892
11893 ht = &d->dv_hashtab;
11894 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011895 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011896 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011897 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011898 if (!HASHITEM_EMPTY(hi))
11899 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011900 int r;
11901
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011902 --todo;
11903 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011904 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011905 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11906 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011907 break;
11908 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011909 r = filter_map_one(&di->di_tv, expr, map, &rem);
11910 clear_tv(&vimvars[VV_KEY].vv_tv);
11911 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011912 break;
11913 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011914 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011915 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11916 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011917 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011918 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011919 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011920 }
11921 }
11922 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011923 }
11924 else
11925 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011926 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11927
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011928 for (li = l->lv_first; li != NULL; li = nli)
11929 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011930 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011931 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011932 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011933 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011934 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011935 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011936 break;
11937 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011938 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011939 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011940 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011941 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011942
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011943 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011944 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011945
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011946 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011947 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011948
11949 copy_tv(&argvars[0], rettv);
11950}
11951
11952 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011953filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011954{
Bram Moolenaar33570922005-01-25 22:26:29 +000011955 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011956 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011957 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011958
Bram Moolenaar33570922005-01-25 22:26:29 +000011959 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011960 s = expr;
11961 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011962 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011963 if (*s != NUL) /* check for trailing chars after expr */
11964 {
11965 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011966 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011967 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011968 }
11969 if (map)
11970 {
11971 /* map(): replace the list item value */
11972 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011973 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011974 *tv = rettv;
11975 }
11976 else
11977 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011978 int error = FALSE;
11979
Bram Moolenaare9a41262005-01-15 22:18:47 +000011980 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011981 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011982 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011983 /* On type error, nothing has been removed; return FAIL to stop the
11984 * loop. The error message was given by get_tv_number_chk(). */
11985 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011986 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011987 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011988 retval = OK;
11989theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011990 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011991 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011992}
11993
11994/*
11995 * "filter()" function
11996 */
11997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011998f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011999{
12000 filter_map(argvars, rettv, FALSE);
12001}
12002
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000012003/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012004 * "finddir({fname}[, {path}[, {count}]])" function
12005 */
12006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012007f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012008{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012009 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012010}
12011
12012/*
12013 * "findfile({fname}[, {path}[, {count}]])" function
12014 */
12015 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012016f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012017{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000012018 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012019}
12020
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012021#ifdef FEAT_FLOAT
12022/*
12023 * "float2nr({float})" function
12024 */
12025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012026f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012027{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012028 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012029
12030 if (get_float_arg(argvars, &f) == OK)
12031 {
12032 if (f < -0x7fffffff)
12033 rettv->vval.v_number = -0x7fffffff;
12034 else if (f > 0x7fffffff)
12035 rettv->vval.v_number = 0x7fffffff;
12036 else
12037 rettv->vval.v_number = (varnumber_T)f;
12038 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012039}
12040
12041/*
12042 * "floor({float})" function
12043 */
12044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012045f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012046{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012047 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012048
12049 rettv->v_type = VAR_FLOAT;
12050 if (get_float_arg(argvars, &f) == OK)
12051 rettv->vval.v_float = floor(f);
12052 else
12053 rettv->vval.v_float = 0.0;
12054}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012055
12056/*
12057 * "fmod()" function
12058 */
12059 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012060f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012061{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010012062 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020012063
12064 rettv->v_type = VAR_FLOAT;
12065 if (get_float_arg(argvars, &fx) == OK
12066 && get_float_arg(&argvars[1], &fy) == OK)
12067 rettv->vval.v_float = fmod(fx, fy);
12068 else
12069 rettv->vval.v_float = 0.0;
12070}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000012071#endif
12072
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012074 * "fnameescape({string})" function
12075 */
12076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012077f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000012078{
12079 rettv->vval.v_string = vim_strsave_fnameescape(
12080 get_tv_string(&argvars[0]), FALSE);
12081 rettv->v_type = VAR_STRING;
12082}
12083
12084/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012085 * "fnamemodify({fname}, {mods})" function
12086 */
12087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012088f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012089{
12090 char_u *fname;
12091 char_u *mods;
12092 int usedlen = 0;
12093 int len;
12094 char_u *fbuf = NULL;
12095 char_u buf[NUMBUFLEN];
12096
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012097 fname = get_tv_string_chk(&argvars[0]);
12098 mods = get_tv_string_buf_chk(&argvars[1], buf);
12099 if (fname == NULL || mods == NULL)
12100 fname = NULL;
12101 else
12102 {
12103 len = (int)STRLEN(fname);
12104 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
12105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012107 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012108 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012109 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012110 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012111 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012112 vim_free(fbuf);
12113}
12114
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012115static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012116
12117/*
12118 * "foldclosed()" function
12119 */
12120 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012121foldclosed_both(
12122 typval_T *argvars UNUSED,
12123 typval_T *rettv,
12124 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012125{
12126#ifdef FEAT_FOLDING
12127 linenr_T lnum;
12128 linenr_T first, last;
12129
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012130 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012131 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
12132 {
12133 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
12134 {
12135 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012136 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012137 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012138 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012139 return;
12140 }
12141 }
12142#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012143 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012144}
12145
12146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 * "foldclosed()" function
12148 */
12149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012150f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151{
12152 foldclosed_both(argvars, rettv, FALSE);
12153}
12154
12155/*
12156 * "foldclosedend()" function
12157 */
12158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012159f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012160{
12161 foldclosed_both(argvars, rettv, TRUE);
12162}
12163
12164/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012165 * "foldlevel()" function
12166 */
12167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012168f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012169{
12170#ifdef FEAT_FOLDING
12171 linenr_T lnum;
12172
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012173 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012174 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012175 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012176#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012177}
12178
12179/*
12180 * "foldtext()" function
12181 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012183f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012184{
12185#ifdef FEAT_FOLDING
12186 linenr_T lnum;
12187 char_u *s;
12188 char_u *r;
12189 int len;
12190 char *txt;
12191#endif
12192
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012193 rettv->v_type = VAR_STRING;
12194 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000012196 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
12197 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
12198 <= curbuf->b_ml.ml_line_count
12199 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012200 {
12201 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012202 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
12203 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012204 {
12205 if (!linewhite(lnum))
12206 break;
12207 ++lnum;
12208 }
12209
12210 /* Find interesting text in this line. */
12211 s = skipwhite(ml_get(lnum));
12212 /* skip C comment-start */
12213 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012214 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000012215 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012216 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000012217 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000012218 {
12219 s = skipwhite(ml_get(lnum + 1));
12220 if (*s == '*')
12221 s = skipwhite(s + 1);
12222 }
12223 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012224 txt = _("+-%s%3ld lines: ");
12225 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012226 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 + 20 /* for %3ld */
12228 + STRLEN(s))); /* concatenated */
12229 if (r != NULL)
12230 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012231 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
12232 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
12233 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000012234 len = (int)STRLEN(r);
12235 STRCAT(r, s);
12236 /* remove 'foldmarker' and 'commentstring' */
12237 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012238 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012239 }
12240 }
12241#endif
12242}
12243
12244/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012245 * "foldtextresult(lnum)" function
12246 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012248f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012249{
12250#ifdef FEAT_FOLDING
12251 linenr_T lnum;
12252 char_u *text;
12253 char_u buf[51];
12254 foldinfo_T foldinfo;
12255 int fold_count;
12256#endif
12257
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012258 rettv->v_type = VAR_STRING;
12259 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012260#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012261 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012262 /* treat illegal types and illegal string values for {lnum} the same */
12263 if (lnum < 0)
12264 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012265 fold_count = foldedCount(curwin, lnum, &foldinfo);
12266 if (fold_count > 0)
12267 {
12268 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
12269 &foldinfo, buf);
12270 if (text == buf)
12271 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012272 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000012273 }
12274#endif
12275}
12276
12277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012278 * "foreground()" function
12279 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012281f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012282{
Bram Moolenaar071d4272004-06-13 20:20:40 +000012283#ifdef FEAT_GUI
12284 if (gui.in_use)
12285 gui_mch_set_foreground();
12286#else
12287# ifdef WIN32
12288 win32_set_foreground();
12289# endif
12290#endif
12291}
12292
12293/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012294 * "function()" function
12295 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012296 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012297f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012298{
12299 char_u *s;
12300
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012301 s = get_tv_string(&argvars[0]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000012302 if (s == NULL || *s == NUL || VIM_ISDIGIT(*s))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012303 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012304 /* Don't check an autoload name for existence here. */
12305 else if (vim_strchr(s, AUTOLOAD_CHAR) == NULL && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000012306 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012307 else
12308 {
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012309 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012310 {
12311 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012312 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012313
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012314 /* Expand s: and <SID> into <SNR>nr_, so that the function can
12315 * also be called from another script. Using trans_function_name()
12316 * would also work, but some plugins depend on the name being
12317 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012318 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaaredb07a22013-06-12 18:13:38 +020012319 rettv->vval.v_string =
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012320 alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012321 if (rettv->vval.v_string != NULL)
12322 {
12323 STRCPY(rettv->vval.v_string, sid_buf);
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020012324 STRCAT(rettv->vval.v_string, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012325 }
12326 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012327 else
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012328 rettv->vval.v_string = vim_strsave(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012329 rettv->v_type = VAR_FUNC;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012330 }
12331}
12332
12333/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012334 * "garbagecollect()" function
12335 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012337f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012338{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012339 /* This is postponed until we are back at the toplevel, because we may be
12340 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12341 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012342
12343 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12344 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012345}
12346
12347/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012348 * "get()" function
12349 */
12350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012351f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012352{
Bram Moolenaar33570922005-01-25 22:26:29 +000012353 listitem_T *li;
12354 list_T *l;
12355 dictitem_T *di;
12356 dict_T *d;
12357 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012358
Bram Moolenaare9a41262005-01-15 22:18:47 +000012359 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012360 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012361 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012363 int error = FALSE;
12364
12365 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12366 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012367 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012368 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012369 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012370 else if (argvars[0].v_type == VAR_DICT)
12371 {
12372 if ((d = argvars[0].vval.v_dict) != NULL)
12373 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012374 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012375 if (di != NULL)
12376 tv = &di->di_tv;
12377 }
12378 }
12379 else
12380 EMSG2(_(e_listdictarg), "get()");
12381
12382 if (tv == NULL)
12383 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012384 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012385 copy_tv(&argvars[2], rettv);
12386 }
12387 else
12388 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012389}
12390
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012391static 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 +000012392
12393/*
12394 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012395 * Return a range (from start to end) of lines in rettv from the specified
12396 * buffer.
12397 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012398 */
12399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012400get_buffer_lines(
12401 buf_T *buf,
12402 linenr_T start,
12403 linenr_T end,
12404 int retlist,
12405 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012406{
12407 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012408
Bram Moolenaar959a1432013-12-14 12:17:38 +010012409 rettv->v_type = VAR_STRING;
12410 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012411 if (retlist && rettv_list_alloc(rettv) == FAIL)
12412 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012413
12414 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12415 return;
12416
12417 if (!retlist)
12418 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012419 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12420 p = ml_get_buf(buf, start, FALSE);
12421 else
12422 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012423 rettv->vval.v_string = vim_strsave(p);
12424 }
12425 else
12426 {
12427 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012428 return;
12429
12430 if (start < 1)
12431 start = 1;
12432 if (end > buf->b_ml.ml_line_count)
12433 end = buf->b_ml.ml_line_count;
12434 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012435 if (list_append_string(rettv->vval.v_list,
12436 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012437 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012438 }
12439}
12440
12441/*
12442 * "getbufline()" function
12443 */
12444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012445f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012446{
12447 linenr_T lnum;
12448 linenr_T end;
12449 buf_T *buf;
12450
12451 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12452 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012453 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012454 --emsg_off;
12455
Bram Moolenaar661b1822005-07-28 22:36:45 +000012456 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012457 if (argvars[2].v_type == VAR_UNKNOWN)
12458 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012459 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012460 end = get_tv_lnum_buf(&argvars[2], buf);
12461
Bram Moolenaar342337a2005-07-21 21:11:17 +000012462 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012463}
12464
Bram Moolenaar0d660222005-01-07 21:51:51 +000012465/*
12466 * "getbufvar()" function
12467 */
12468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012469f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012470{
12471 buf_T *buf;
12472 buf_T *save_curbuf;
12473 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012474 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012475 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012476
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012477 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12478 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012479 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012480 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012481
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012482 rettv->v_type = VAR_STRING;
12483 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012484
12485 if (buf != NULL && varname != NULL)
12486 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012487 /* set curbuf to be our buf, temporarily */
12488 save_curbuf = curbuf;
12489 curbuf = buf;
12490
Bram Moolenaar0d660222005-01-07 21:51:51 +000012491 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012492 {
12493 if (get_option_tv(&varname, rettv, TRUE) == OK)
12494 done = TRUE;
12495 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012496 else if (STRCMP(varname, "changedtick") == 0)
12497 {
12498 rettv->v_type = VAR_NUMBER;
12499 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012500 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012501 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012502 else
12503 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012504 /* Look up the variable. */
12505 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12506 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12507 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012508 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012509 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012510 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012511 done = TRUE;
12512 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012513 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012514
12515 /* restore previous notion of curbuf */
12516 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012517 }
12518
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012519 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12520 /* use the default value */
12521 copy_tv(&argvars[2], rettv);
12522
Bram Moolenaar0d660222005-01-07 21:51:51 +000012523 --emsg_off;
12524}
12525
12526/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012527 * "getchar()" function
12528 */
12529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012530f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012531{
12532 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012533 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012534
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012535 /* Position the cursor. Needed after a message that ends in a space. */
12536 windgoto(msg_row, msg_col);
12537
Bram Moolenaar071d4272004-06-13 20:20:40 +000012538 ++no_mapping;
12539 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012540 for (;;)
12541 {
12542 if (argvars[0].v_type == VAR_UNKNOWN)
12543 /* getchar(): blocking wait. */
12544 n = safe_vgetc();
12545 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12546 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012547 n = vpeekc_any();
12548 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012549 /* illegal argument or getchar(0) and no char avail: return zero */
12550 n = 0;
12551 else
12552 /* getchar(0) and char avail: return char */
12553 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012554
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012555 if (n == K_IGNORE)
12556 continue;
12557 break;
12558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559 --no_mapping;
12560 --allow_keys;
12561
Bram Moolenaar219b8702006-11-01 14:32:36 +000012562 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12563 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12564 vimvars[VV_MOUSE_COL].vv_nr = 0;
12565
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012566 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012567 if (IS_SPECIAL(n) || mod_mask != 0)
12568 {
12569 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12570 int i = 0;
12571
12572 /* Turn a special key into three bytes, plus modifier. */
12573 if (mod_mask != 0)
12574 {
12575 temp[i++] = K_SPECIAL;
12576 temp[i++] = KS_MODIFIER;
12577 temp[i++] = mod_mask;
12578 }
12579 if (IS_SPECIAL(n))
12580 {
12581 temp[i++] = K_SPECIAL;
12582 temp[i++] = K_SECOND(n);
12583 temp[i++] = K_THIRD(n);
12584 }
12585#ifdef FEAT_MBYTE
12586 else if (has_mbyte)
12587 i += (*mb_char2bytes)(n, temp + i);
12588#endif
12589 else
12590 temp[i++] = n;
12591 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012592 rettv->v_type = VAR_STRING;
12593 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012594
12595#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012596 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012597 {
12598 int row = mouse_row;
12599 int col = mouse_col;
12600 win_T *win;
12601 linenr_T lnum;
12602# ifdef FEAT_WINDOWS
12603 win_T *wp;
12604# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012605 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012606
12607 if (row >= 0 && col >= 0)
12608 {
12609 /* Find the window at the mouse coordinates and compute the
12610 * text position. */
12611 win = mouse_find_win(&row, &col);
12612 (void)mouse_comp_pos(win, &row, &col, &lnum);
12613# ifdef FEAT_WINDOWS
12614 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012615 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012616# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012617 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012618 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12619 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12620 }
12621 }
12622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 }
12624}
12625
12626/*
12627 * "getcharmod()" function
12628 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012630f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012631{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012632 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012633}
12634
12635/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012636 * "getcharsearch()" function
12637 */
12638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012639f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012640{
12641 if (rettv_dict_alloc(rettv) != FAIL)
12642 {
12643 dict_T *dict = rettv->vval.v_dict;
12644
12645 dict_add_nr_str(dict, "char", 0L, last_csearch());
12646 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12647 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12648 }
12649}
12650
12651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012652 * "getcmdline()" function
12653 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012655f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012656{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012657 rettv->v_type = VAR_STRING;
12658 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012659}
12660
12661/*
12662 * "getcmdpos()" function
12663 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012665f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012666{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012667 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012668}
12669
12670/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012671 * "getcmdtype()" function
12672 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012674f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012675{
12676 rettv->v_type = VAR_STRING;
12677 rettv->vval.v_string = alloc(2);
12678 if (rettv->vval.v_string != NULL)
12679 {
12680 rettv->vval.v_string[0] = get_cmdline_type();
12681 rettv->vval.v_string[1] = NUL;
12682 }
12683}
12684
12685/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012686 * "getcmdwintype()" function
12687 */
12688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012689f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012690{
12691 rettv->v_type = VAR_STRING;
12692 rettv->vval.v_string = NULL;
12693#ifdef FEAT_CMDWIN
12694 rettv->vval.v_string = alloc(2);
12695 if (rettv->vval.v_string != NULL)
12696 {
12697 rettv->vval.v_string[0] = cmdwin_type;
12698 rettv->vval.v_string[1] = NUL;
12699 }
12700#endif
12701}
12702
12703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012704 * "getcwd()" function
12705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012707f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012708{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012709 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012710 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012711
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012712 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012713 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012714
12715 wp = find_tabwin(&argvars[0], &argvars[1]);
12716 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012717 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012718 if (wp->w_localdir != NULL)
12719 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12720 else if(globaldir != NULL)
12721 rettv->vval.v_string = vim_strsave(globaldir);
12722 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012723 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012724 cwd = alloc(MAXPATHL);
12725 if (cwd != NULL)
12726 {
12727 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12728 rettv->vval.v_string = vim_strsave(cwd);
12729 vim_free(cwd);
12730 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012731 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012732#ifdef BACKSLASH_IN_FILENAME
12733 if (rettv->vval.v_string != NULL)
12734 slash_adjust(rettv->vval.v_string);
12735#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012736 }
12737}
12738
12739/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012740 * "getfontname()" function
12741 */
12742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012744{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012745 rettv->v_type = VAR_STRING;
12746 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012747#ifdef FEAT_GUI
12748 if (gui.in_use)
12749 {
12750 GuiFont font;
12751 char_u *name = NULL;
12752
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012753 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012754 {
12755 /* Get the "Normal" font. Either the name saved by
12756 * hl_set_font_name() or from the font ID. */
12757 font = gui.norm_font;
12758 name = hl_get_font_name();
12759 }
12760 else
12761 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012762 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012763 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12764 return;
12765 font = gui_mch_get_font(name, FALSE);
12766 if (font == NOFONT)
12767 return; /* Invalid font name, return empty string. */
12768 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012769 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012770 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012771 gui_mch_free_font(font);
12772 }
12773#endif
12774}
12775
12776/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012777 * "getfperm({fname})" function
12778 */
12779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012780f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012781{
12782 char_u *fname;
12783 struct stat st;
12784 char_u *perm = NULL;
12785 char_u flags[] = "rwx";
12786 int i;
12787
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012788 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012789
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012790 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012791 if (mch_stat((char *)fname, &st) >= 0)
12792 {
12793 perm = vim_strsave((char_u *)"---------");
12794 if (perm != NULL)
12795 {
12796 for (i = 0; i < 9; i++)
12797 {
12798 if (st.st_mode & (1 << (8 - i)))
12799 perm[i] = flags[i % 3];
12800 }
12801 }
12802 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012803 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012804}
12805
12806/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012807 * "getfsize({fname})" function
12808 */
12809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012810f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012811{
12812 char_u *fname;
12813 struct stat st;
12814
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012815 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012817 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012818
12819 if (mch_stat((char *)fname, &st) >= 0)
12820 {
12821 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012822 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012823 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012824 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012825 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012826
12827 /* non-perfect check for overflow */
12828 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12829 rettv->vval.v_number = -2;
12830 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012831 }
12832 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012833 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
12836/*
12837 * "getftime({fname})" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842 char_u *fname;
12843 struct stat st;
12844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012845 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012846
12847 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012848 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012850 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012851}
12852
12853/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012854 * "getftype({fname})" function
12855 */
12856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012857f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012858{
12859 char_u *fname;
12860 struct stat st;
12861 char_u *type = NULL;
12862 char *t;
12863
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012864 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012865
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012866 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012867 if (mch_lstat((char *)fname, &st) >= 0)
12868 {
12869#ifdef S_ISREG
12870 if (S_ISREG(st.st_mode))
12871 t = "file";
12872 else if (S_ISDIR(st.st_mode))
12873 t = "dir";
12874# ifdef S_ISLNK
12875 else if (S_ISLNK(st.st_mode))
12876 t = "link";
12877# endif
12878# ifdef S_ISBLK
12879 else if (S_ISBLK(st.st_mode))
12880 t = "bdev";
12881# endif
12882# ifdef S_ISCHR
12883 else if (S_ISCHR(st.st_mode))
12884 t = "cdev";
12885# endif
12886# ifdef S_ISFIFO
12887 else if (S_ISFIFO(st.st_mode))
12888 t = "fifo";
12889# endif
12890# ifdef S_ISSOCK
12891 else if (S_ISSOCK(st.st_mode))
12892 t = "fifo";
12893# endif
12894 else
12895 t = "other";
12896#else
12897# ifdef S_IFMT
12898 switch (st.st_mode & S_IFMT)
12899 {
12900 case S_IFREG: t = "file"; break;
12901 case S_IFDIR: t = "dir"; break;
12902# ifdef S_IFLNK
12903 case S_IFLNK: t = "link"; break;
12904# endif
12905# ifdef S_IFBLK
12906 case S_IFBLK: t = "bdev"; break;
12907# endif
12908# ifdef S_IFCHR
12909 case S_IFCHR: t = "cdev"; break;
12910# endif
12911# ifdef S_IFIFO
12912 case S_IFIFO: t = "fifo"; break;
12913# endif
12914# ifdef S_IFSOCK
12915 case S_IFSOCK: t = "socket"; break;
12916# endif
12917 default: t = "other";
12918 }
12919# else
12920 if (mch_isdir(fname))
12921 t = "dir";
12922 else
12923 t = "file";
12924# endif
12925#endif
12926 type = vim_strsave((char_u *)t);
12927 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012928 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012929}
12930
12931/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012932 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012933 */
12934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012935f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012936{
12937 linenr_T lnum;
12938 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012939 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012940
12941 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012942 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012943 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012944 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012945 retlist = FALSE;
12946 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012947 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012948 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012949 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012950 retlist = TRUE;
12951 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012952
Bram Moolenaar342337a2005-07-21 21:11:17 +000012953 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012954}
12955
12956/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012957 * "getmatches()" function
12958 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012959 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012960f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012961{
12962#ifdef FEAT_SEARCH_EXTRA
12963 dict_T *dict;
12964 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012965 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012966
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012967 if (rettv_list_alloc(rettv) == OK)
12968 {
12969 while (cur != NULL)
12970 {
12971 dict = dict_alloc();
12972 if (dict == NULL)
12973 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012974 if (cur->match.regprog == NULL)
12975 {
12976 /* match added with matchaddpos() */
12977 for (i = 0; i < MAXPOSMATCH; ++i)
12978 {
12979 llpos_T *llpos;
12980 char buf[6];
12981 list_T *l;
12982
12983 llpos = &cur->pos.pos[i];
12984 if (llpos->lnum == 0)
12985 break;
12986 l = list_alloc();
12987 if (l == NULL)
12988 break;
12989 list_append_number(l, (varnumber_T)llpos->lnum);
12990 if (llpos->col > 0)
12991 {
12992 list_append_number(l, (varnumber_T)llpos->col);
12993 list_append_number(l, (varnumber_T)llpos->len);
12994 }
12995 sprintf(buf, "pos%d", i + 1);
12996 dict_add_list(dict, buf, l);
12997 }
12998 }
12999 else
13000 {
13001 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
13002 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013003 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013004 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
13005 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020013006# ifdef FEAT_CONCEAL
13007 if (cur->conceal_char)
13008 {
13009 char_u buf[MB_MAXBYTES + 1];
13010
13011 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
13012 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
13013 }
13014# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000013015 list_append_dict(rettv->vval.v_list, dict);
13016 cur = cur->next;
13017 }
13018 }
13019#endif
13020}
13021
13022/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000013023 * "getpid()" function
13024 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000013025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013026f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000013027{
13028 rettv->vval.v_number = mch_get_pid();
13029}
13030
Bram Moolenaar48e697e2016-01-23 22:17:30 +010013031static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013032
13033/*
13034 * "getcurpos()" function
13035 */
13036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013037f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013038{
13039 getpos_both(argvars, rettv, TRUE);
13040}
13041
Bram Moolenaar18081e32008-02-20 19:11:07 +000013042/*
Bram Moolenaara5525202006-03-02 22:52:09 +000013043 * "getpos(string)" function
13044 */
13045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013046f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000013047{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013048 getpos_both(argvars, rettv, FALSE);
13049}
13050
13051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013052getpos_both(
13053 typval_T *argvars,
13054 typval_T *rettv,
13055 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013056{
Bram Moolenaara5525202006-03-02 22:52:09 +000013057 pos_T *fp;
13058 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013059 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000013060
13061 if (rettv_list_alloc(rettv) == OK)
13062 {
13063 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013064 if (getcurpos)
13065 fp = &curwin->w_cursor;
13066 else
13067 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000013068 if (fnum != -1)
13069 list_append_number(l, (varnumber_T)fnum);
13070 else
13071 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000013072 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
13073 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000013074 list_append_number(l, (fp != NULL)
13075 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000013076 : (varnumber_T)0);
13077 list_append_number(l,
13078#ifdef FEAT_VIRTUALEDIT
13079 (fp != NULL) ? (varnumber_T)fp->coladd :
13080#endif
13081 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020013082 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013083 {
13084 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010013085 list_append_number(l, curwin->w_curswant == MAXCOL ?
13086 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010013087 }
Bram Moolenaara5525202006-03-02 22:52:09 +000013088 }
13089 else
13090 rettv->vval.v_number = FALSE;
13091}
13092
13093/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000013094 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000013095 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000013096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013097f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013098{
13099#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000013100 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000013101#endif
13102
Bram Moolenaar2641f772005-03-25 21:58:17 +000013103#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013104 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000013105 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000013106 wp = NULL;
13107 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
13108 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013109 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000013110 if (wp == NULL)
13111 return;
13112 }
13113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000013114 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000013115 }
13116#endif
13117}
13118
13119/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013120 * "getreg()" function
13121 */
13122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013123f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013124{
13125 char_u *strregname;
13126 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013127 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013128 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013129 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013130
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013131 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013132 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013133 strregname = get_tv_string_chk(&argvars[0]);
13134 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013135 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013137 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013138 if (!error && argvars[2].v_type != VAR_UNKNOWN)
13139 return_list = get_tv_number_chk(&argvars[2], &error);
13140 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000013141 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000013143 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013144
13145 if (error)
13146 return;
13147
Bram Moolenaar071d4272004-06-13 20:20:40 +000013148 regname = (strregname == NULL ? '"' : *strregname);
13149 if (regname == 0)
13150 regname = '"';
13151
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013152 if (return_list)
13153 {
13154 rettv->v_type = VAR_LIST;
13155 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
13156 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010013157 if (rettv->vval.v_list != NULL)
13158 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020013159 }
13160 else
13161 {
13162 rettv->v_type = VAR_STRING;
13163 rettv->vval.v_string = get_reg_contents(regname,
13164 arg2 ? GREG_EXPR_SRC : 0);
13165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013166}
13167
13168/*
13169 * "getregtype()" function
13170 */
13171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013172f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173{
13174 char_u *strregname;
13175 int regname;
13176 char_u buf[NUMBUFLEN + 2];
13177 long reglen = 0;
13178
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013179 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013180 {
13181 strregname = get_tv_string_chk(&argvars[0]);
13182 if (strregname == NULL) /* type error; errmsg already given */
13183 {
13184 rettv->v_type = VAR_STRING;
13185 rettv->vval.v_string = NULL;
13186 return;
13187 }
13188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189 else
13190 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000013191 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013192
13193 regname = (strregname == NULL ? '"' : *strregname);
13194 if (regname == 0)
13195 regname = '"';
13196
13197 buf[0] = NUL;
13198 buf[1] = NUL;
13199 switch (get_reg_type(regname, &reglen))
13200 {
13201 case MLINE: buf[0] = 'V'; break;
13202 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 case MBLOCK:
13204 buf[0] = Ctrl_V;
13205 sprintf((char *)buf + 1, "%ld", reglen + 1);
13206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013208 rettv->v_type = VAR_STRING;
13209 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013210}
13211
13212/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013213 * "gettabvar()" function
13214 */
13215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013216f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013217{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013218 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013219 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013220 dictitem_T *v;
13221 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013222 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013223
13224 rettv->v_type = VAR_STRING;
13225 rettv->vval.v_string = NULL;
13226
13227 varname = get_tv_string_chk(&argvars[1]);
13228 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13229 if (tp != NULL && varname != NULL)
13230 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013231 /* Set tp to be our tabpage, temporarily. Also set the window to the
13232 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013233 if (switch_win(&oldcurwin, &oldtabpage,
13234 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013235 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013236 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013237 /* look up the variable */
13238 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13239 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13240 if (v != NULL)
13241 {
13242 copy_tv(&v->di_tv, rettv);
13243 done = TRUE;
13244 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013245 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013246
13247 /* restore previous notion of curwin */
13248 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013249 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013250
13251 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013252 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013253}
13254
13255/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013256 * "gettabwinvar()" function
13257 */
13258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013259f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013260{
13261 getwinvar(argvars, rettv, 1);
13262}
13263
13264/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 * "getwinposx()" function
13266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013268f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013270 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013271#ifdef FEAT_GUI
13272 if (gui.in_use)
13273 {
13274 int x, y;
13275
13276 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013277 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 }
13279#endif
13280}
13281
13282/*
13283 * "getwinposy()" function
13284 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013286f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013287{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013288 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289#ifdef FEAT_GUI
13290 if (gui.in_use)
13291 {
13292 int x, y;
13293
13294 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013295 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013296 }
13297#endif
13298}
13299
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013300/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013301 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013302 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013303 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013304find_win_by_nr(
13305 typval_T *vp,
13306 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013307{
13308#ifdef FEAT_WINDOWS
13309 win_T *wp;
13310#endif
13311 int nr;
13312
13313 nr = get_tv_number_chk(vp, NULL);
13314
13315#ifdef FEAT_WINDOWS
13316 if (nr < 0)
13317 return NULL;
13318 if (nr == 0)
13319 return curwin;
13320
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013321 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13322 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013323 if (--nr <= 0)
13324 break;
13325 return wp;
13326#else
13327 if (nr == 0 || nr == 1)
13328 return curwin;
13329 return NULL;
13330#endif
13331}
13332
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013334 * Find window specified by "wvp" in tabpage "tvp".
13335 */
13336 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013337find_tabwin(
13338 typval_T *wvp, /* VAR_UNKNOWN for current window */
13339 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013340{
13341 win_T *wp = NULL;
13342 tabpage_T *tp = NULL;
13343 long n;
13344
13345 if (wvp->v_type != VAR_UNKNOWN)
13346 {
13347 if (tvp->v_type != VAR_UNKNOWN)
13348 {
13349 n = get_tv_number(tvp);
13350 if (n >= 0)
13351 tp = find_tabpage(n);
13352 }
13353 else
13354 tp = curtab;
13355
13356 if (tp != NULL)
13357 wp = find_win_by_nr(wvp, tp);
13358 }
13359 else
13360 wp = curwin;
13361
13362 return wp;
13363}
13364
13365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366 * "getwinvar()" function
13367 */
13368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013369f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013371 getwinvar(argvars, rettv, 0);
13372}
13373
13374/*
13375 * getwinvar() and gettabwinvar()
13376 */
13377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013378getwinvar(
13379 typval_T *argvars,
13380 typval_T *rettv,
13381 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013382{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013383 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013384 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013385 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013386 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013387 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013388#ifdef FEAT_WINDOWS
13389 win_T *oldcurwin;
13390 tabpage_T *oldtabpage;
13391 int need_switch_win;
13392#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013393
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013394#ifdef FEAT_WINDOWS
13395 if (off == 1)
13396 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13397 else
13398 tp = curtab;
13399#endif
13400 win = find_win_by_nr(&argvars[off], tp);
13401 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013402 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013403
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013404 rettv->v_type = VAR_STRING;
13405 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406
13407 if (win != NULL && varname != NULL)
13408 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013409#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013410 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013411 * otherwise the window is not valid. Only do this when needed,
13412 * autocommands get blocked. */
13413 need_switch_win = !(tp == curtab && win == curwin);
13414 if (!need_switch_win
13415 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13416#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013417 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013418 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013419 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013420 if (get_option_tv(&varname, rettv, 1) == OK)
13421 done = TRUE;
13422 }
13423 else
13424 {
13425 /* Look up the variable. */
13426 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13427 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13428 varname, FALSE);
13429 if (v != NULL)
13430 {
13431 copy_tv(&v->di_tv, rettv);
13432 done = TRUE;
13433 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013434 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013435 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013436
Bram Moolenaarba117c22015-09-29 16:53:22 +020013437#ifdef FEAT_WINDOWS
13438 if (need_switch_win)
13439 /* restore previous notion of curwin */
13440 restore_win(oldcurwin, oldtabpage, TRUE);
13441#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013442 }
13443
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013444 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13445 /* use the default return value */
13446 copy_tv(&argvars[off + 2], rettv);
13447
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448 --emsg_off;
13449}
13450
13451/*
13452 * "glob()" function
13453 */
13454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013455f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013456{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013457 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013459 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013460
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013461 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013462 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013463 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013464 if (argvars[1].v_type != VAR_UNKNOWN)
13465 {
13466 if (get_tv_number_chk(&argvars[1], &error))
13467 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013469 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013470 if (get_tv_number_chk(&argvars[2], &error))
13471 {
13472 rettv->v_type = VAR_LIST;
13473 rettv->vval.v_list = NULL;
13474 }
13475 if (argvars[3].v_type != VAR_UNKNOWN
13476 && get_tv_number_chk(&argvars[3], &error))
13477 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013478 }
13479 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013480 if (!error)
13481 {
13482 ExpandInit(&xpc);
13483 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013484 if (p_wic)
13485 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013486 if (rettv->v_type == VAR_STRING)
13487 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013488 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013489 else if (rettv_list_alloc(rettv) != FAIL)
13490 {
13491 int i;
13492
13493 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13494 NULL, options, WILD_ALL_KEEP);
13495 for (i = 0; i < xpc.xp_numfiles; i++)
13496 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13497
13498 ExpandCleanup(&xpc);
13499 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013500 }
13501 else
13502 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013503}
13504
13505/*
13506 * "globpath()" function
13507 */
13508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013509f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013510{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013511 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013512 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013513 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013514 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013515 garray_T ga;
13516 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013517
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013518 /* When the optional second argument is non-zero, don't remove matches
13519 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013520 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013521 if (argvars[2].v_type != VAR_UNKNOWN)
13522 {
13523 if (get_tv_number_chk(&argvars[2], &error))
13524 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013525 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013526 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013527 if (get_tv_number_chk(&argvars[3], &error))
13528 {
13529 rettv->v_type = VAR_LIST;
13530 rettv->vval.v_list = NULL;
13531 }
13532 if (argvars[4].v_type != VAR_UNKNOWN
13533 && get_tv_number_chk(&argvars[4], &error))
13534 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013535 }
13536 }
13537 if (file != NULL && !error)
13538 {
13539 ga_init2(&ga, (int)sizeof(char_u *), 10);
13540 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13541 if (rettv->v_type == VAR_STRING)
13542 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13543 else if (rettv_list_alloc(rettv) != FAIL)
13544 for (i = 0; i < ga.ga_len; ++i)
13545 list_append_string(rettv->vval.v_list,
13546 ((char_u **)(ga.ga_data))[i], -1);
13547 ga_clear_strings(&ga);
13548 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013549 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013550 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551}
13552
13553/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013554 * "glob2regpat()" function
13555 */
13556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013557f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013558{
13559 char_u *pat = get_tv_string_chk(&argvars[0]);
13560
13561 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013562 rettv->vval.v_string = (pat == NULL)
13563 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013564}
13565
13566/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013567 * "has()" function
13568 */
13569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013570f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571{
13572 int i;
13573 char_u *name;
13574 int n = FALSE;
13575 static char *(has_list[]) =
13576 {
13577#ifdef AMIGA
13578 "amiga",
13579# ifdef FEAT_ARP
13580 "arp",
13581# endif
13582#endif
13583#ifdef __BEOS__
13584 "beos",
13585#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013586#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 "mac",
13588#endif
13589#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013590 "macunix", /* built with 'darwin' enabled */
13591#endif
13592#if defined(__APPLE__) && __APPLE__ == 1
13593 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013594#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013595#ifdef __QNX__
13596 "qnx",
13597#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013598#ifdef UNIX
13599 "unix",
13600#endif
13601#ifdef VMS
13602 "vms",
13603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#ifdef WIN32
13605 "win32",
13606#endif
13607#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13608 "win32unix",
13609#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013610#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013611 "win64",
13612#endif
13613#ifdef EBCDIC
13614 "ebcdic",
13615#endif
13616#ifndef CASE_INSENSITIVE_FILENAME
13617 "fname_case",
13618#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013619#ifdef HAVE_ACL
13620 "acl",
13621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013622#ifdef FEAT_ARABIC
13623 "arabic",
13624#endif
13625#ifdef FEAT_AUTOCMD
13626 "autocmd",
13627#endif
13628#ifdef FEAT_BEVAL
13629 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013630# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13631 "balloon_multiline",
13632# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013633#endif
13634#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13635 "builtin_terms",
13636# ifdef ALL_BUILTIN_TCAPS
13637 "all_builtin_terms",
13638# endif
13639#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013640#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13641 || defined(FEAT_GUI_W32) \
13642 || defined(FEAT_GUI_MOTIF))
13643 "browsefilter",
13644#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013645#ifdef FEAT_BYTEOFF
13646 "byte_offset",
13647#endif
Bram Moolenaare0874f82016-01-24 20:36:41 +010013648#ifdef FEAT_CHANNEL
13649 "channel",
13650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651#ifdef FEAT_CINDENT
13652 "cindent",
13653#endif
13654#ifdef FEAT_CLIENTSERVER
13655 "clientserver",
13656#endif
13657#ifdef FEAT_CLIPBOARD
13658 "clipboard",
13659#endif
13660#ifdef FEAT_CMDL_COMPL
13661 "cmdline_compl",
13662#endif
13663#ifdef FEAT_CMDHIST
13664 "cmdline_hist",
13665#endif
13666#ifdef FEAT_COMMENTS
13667 "comments",
13668#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013669#ifdef FEAT_CONCEAL
13670 "conceal",
13671#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013672#ifdef FEAT_CRYPT
13673 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013674 "crypt-blowfish",
13675 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013676#endif
13677#ifdef FEAT_CSCOPE
13678 "cscope",
13679#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013680#ifdef FEAT_CURSORBIND
13681 "cursorbind",
13682#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013683#ifdef CURSOR_SHAPE
13684 "cursorshape",
13685#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686#ifdef DEBUG
13687 "debug",
13688#endif
13689#ifdef FEAT_CON_DIALOG
13690 "dialog_con",
13691#endif
13692#ifdef FEAT_GUI_DIALOG
13693 "dialog_gui",
13694#endif
13695#ifdef FEAT_DIFF
13696 "diff",
13697#endif
13698#ifdef FEAT_DIGRAPHS
13699 "digraphs",
13700#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013701#ifdef FEAT_DIRECTX
13702 "directx",
13703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_DND
13705 "dnd",
13706#endif
13707#ifdef FEAT_EMACS_TAGS
13708 "emacs_tags",
13709#endif
13710 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013711 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013712#ifdef FEAT_SEARCH_EXTRA
13713 "extra_search",
13714#endif
13715#ifdef FEAT_FKMAP
13716 "farsi",
13717#endif
13718#ifdef FEAT_SEARCHPATH
13719 "file_in_path",
13720#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013721#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013722 "filterpipe",
13723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013724#ifdef FEAT_FIND_ID
13725 "find_in_path",
13726#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013727#ifdef FEAT_FLOAT
13728 "float",
13729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730#ifdef FEAT_FOLDING
13731 "folding",
13732#endif
13733#ifdef FEAT_FOOTER
13734 "footer",
13735#endif
13736#if !defined(USE_SYSTEM) && defined(UNIX)
13737 "fork",
13738#endif
13739#ifdef FEAT_GETTEXT
13740 "gettext",
13741#endif
13742#ifdef FEAT_GUI
13743 "gui",
13744#endif
13745#ifdef FEAT_GUI_ATHENA
13746# ifdef FEAT_GUI_NEXTAW
13747 "gui_neXtaw",
13748# else
13749 "gui_athena",
13750# endif
13751#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013752#ifdef FEAT_GUI_GTK
13753 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013754# ifdef USE_GTK3
13755 "gui_gtk3",
13756# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013758# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013759#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013760#ifdef FEAT_GUI_GNOME
13761 "gui_gnome",
13762#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013763#ifdef FEAT_GUI_MAC
13764 "gui_mac",
13765#endif
13766#ifdef FEAT_GUI_MOTIF
13767 "gui_motif",
13768#endif
13769#ifdef FEAT_GUI_PHOTON
13770 "gui_photon",
13771#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772#ifdef FEAT_GUI_W32
13773 "gui_win32",
13774#endif
13775#ifdef FEAT_HANGULIN
13776 "hangul_input",
13777#endif
13778#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13779 "iconv",
13780#endif
13781#ifdef FEAT_INS_EXPAND
13782 "insert_expand",
13783#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010013784#ifdef FEAT_JOB
13785 "job",
13786#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013787#ifdef FEAT_JUMPLIST
13788 "jumplist",
13789#endif
13790#ifdef FEAT_KEYMAP
13791 "keymap",
13792#endif
13793#ifdef FEAT_LANGMAP
13794 "langmap",
13795#endif
13796#ifdef FEAT_LIBCALL
13797 "libcall",
13798#endif
13799#ifdef FEAT_LINEBREAK
13800 "linebreak",
13801#endif
13802#ifdef FEAT_LISP
13803 "lispindent",
13804#endif
13805#ifdef FEAT_LISTCMDS
13806 "listcmds",
13807#endif
13808#ifdef FEAT_LOCALMAP
13809 "localmap",
13810#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013811#ifdef FEAT_LUA
13812# ifndef DYNAMIC_LUA
13813 "lua",
13814# endif
13815#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013816#ifdef FEAT_MENU
13817 "menu",
13818#endif
13819#ifdef FEAT_SESSION
13820 "mksession",
13821#endif
13822#ifdef FEAT_MODIFY_FNAME
13823 "modify_fname",
13824#endif
13825#ifdef FEAT_MOUSE
13826 "mouse",
13827#endif
13828#ifdef FEAT_MOUSESHAPE
13829 "mouseshape",
13830#endif
13831#if defined(UNIX) || defined(VMS)
13832# ifdef FEAT_MOUSE_DEC
13833 "mouse_dec",
13834# endif
13835# ifdef FEAT_MOUSE_GPM
13836 "mouse_gpm",
13837# endif
13838# ifdef FEAT_MOUSE_JSB
13839 "mouse_jsbterm",
13840# endif
13841# ifdef FEAT_MOUSE_NET
13842 "mouse_netterm",
13843# endif
13844# ifdef FEAT_MOUSE_PTERM
13845 "mouse_pterm",
13846# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013847# ifdef FEAT_MOUSE_SGR
13848 "mouse_sgr",
13849# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013850# ifdef FEAT_SYSMOUSE
13851 "mouse_sysmouse",
13852# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013853# ifdef FEAT_MOUSE_URXVT
13854 "mouse_urxvt",
13855# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013856# ifdef FEAT_MOUSE_XTERM
13857 "mouse_xterm",
13858# endif
13859#endif
13860#ifdef FEAT_MBYTE
13861 "multi_byte",
13862#endif
13863#ifdef FEAT_MBYTE_IME
13864 "multi_byte_ime",
13865#endif
13866#ifdef FEAT_MULTI_LANG
13867 "multi_lang",
13868#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013869#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013870#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013871 "mzscheme",
13872#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013873#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013874#ifdef FEAT_OLE
13875 "ole",
13876#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013877 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878#ifdef FEAT_PATH_EXTRA
13879 "path_extra",
13880#endif
13881#ifdef FEAT_PERL
13882#ifndef DYNAMIC_PERL
13883 "perl",
13884#endif
13885#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013886#ifdef FEAT_PERSISTENT_UNDO
13887 "persistent_undo",
13888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013889#ifdef FEAT_PYTHON
13890#ifndef DYNAMIC_PYTHON
13891 "python",
13892#endif
13893#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013894#ifdef FEAT_PYTHON3
13895#ifndef DYNAMIC_PYTHON3
13896 "python3",
13897#endif
13898#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013899#ifdef FEAT_POSTSCRIPT
13900 "postscript",
13901#endif
13902#ifdef FEAT_PRINTER
13903 "printer",
13904#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013905#ifdef FEAT_PROFILE
13906 "profile",
13907#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013908#ifdef FEAT_RELTIME
13909 "reltime",
13910#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911#ifdef FEAT_QUICKFIX
13912 "quickfix",
13913#endif
13914#ifdef FEAT_RIGHTLEFT
13915 "rightleft",
13916#endif
13917#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13918 "ruby",
13919#endif
13920#ifdef FEAT_SCROLLBIND
13921 "scrollbind",
13922#endif
13923#ifdef FEAT_CMDL_INFO
13924 "showcmd",
13925 "cmdline_info",
13926#endif
13927#ifdef FEAT_SIGNS
13928 "signs",
13929#endif
13930#ifdef FEAT_SMARTINDENT
13931 "smartindent",
13932#endif
13933#ifdef FEAT_SNIFF
13934 "sniff",
13935#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013936#ifdef STARTUPTIME
13937 "startuptime",
13938#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013939#ifdef FEAT_STL_OPT
13940 "statusline",
13941#endif
13942#ifdef FEAT_SUN_WORKSHOP
13943 "sun_workshop",
13944#endif
13945#ifdef FEAT_NETBEANS_INTG
13946 "netbeans_intg",
13947#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013948#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013949 "spell",
13950#endif
13951#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013952 "syntax",
13953#endif
13954#if defined(USE_SYSTEM) || !defined(UNIX)
13955 "system",
13956#endif
13957#ifdef FEAT_TAG_BINS
13958 "tag_binary",
13959#endif
13960#ifdef FEAT_TAG_OLDSTATIC
13961 "tag_old_static",
13962#endif
13963#ifdef FEAT_TAG_ANYWHITE
13964 "tag_any_white",
13965#endif
13966#ifdef FEAT_TCL
13967# ifndef DYNAMIC_TCL
13968 "tcl",
13969# endif
13970#endif
13971#ifdef TERMINFO
13972 "terminfo",
13973#endif
13974#ifdef FEAT_TERMRESPONSE
13975 "termresponse",
13976#endif
13977#ifdef FEAT_TEXTOBJ
13978 "textobjects",
13979#endif
13980#ifdef HAVE_TGETENT
13981 "tgetent",
13982#endif
13983#ifdef FEAT_TITLE
13984 "title",
13985#endif
13986#ifdef FEAT_TOOLBAR
13987 "toolbar",
13988#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013989#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13990 "unnamedplus",
13991#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992#ifdef FEAT_USR_CMDS
13993 "user-commands", /* was accidentally included in 5.4 */
13994 "user_commands",
13995#endif
13996#ifdef FEAT_VIMINFO
13997 "viminfo",
13998#endif
13999#ifdef FEAT_VERTSPLIT
14000 "vertsplit",
14001#endif
14002#ifdef FEAT_VIRTUALEDIT
14003 "virtualedit",
14004#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014005 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014006#ifdef FEAT_VISUALEXTRA
14007 "visualextra",
14008#endif
14009#ifdef FEAT_VREPLACE
14010 "vreplace",
14011#endif
14012#ifdef FEAT_WILDIGN
14013 "wildignore",
14014#endif
14015#ifdef FEAT_WILDMENU
14016 "wildmenu",
14017#endif
14018#ifdef FEAT_WINDOWS
14019 "windows",
14020#endif
14021#ifdef FEAT_WAK
14022 "winaltkeys",
14023#endif
14024#ifdef FEAT_WRITEBACKUP
14025 "writebackup",
14026#endif
14027#ifdef FEAT_XIM
14028 "xim",
14029#endif
14030#ifdef FEAT_XFONTSET
14031 "xfontset",
14032#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014033#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014034 "xpm",
14035 "xpm_w32", /* for backward compatibility */
14036#else
14037# if defined(HAVE_XPM)
14038 "xpm",
14039# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014040#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041#ifdef USE_XSMP
14042 "xsmp",
14043#endif
14044#ifdef USE_XSMP_INTERACT
14045 "xsmp_interact",
14046#endif
14047#ifdef FEAT_XCLIPBOARD
14048 "xterm_clipboard",
14049#endif
14050#ifdef FEAT_XTERM_SAVE
14051 "xterm_save",
14052#endif
14053#if defined(UNIX) && defined(FEAT_X11)
14054 "X11",
14055#endif
14056 NULL
14057 };
14058
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014059 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060 for (i = 0; has_list[i] != NULL; ++i)
14061 if (STRICMP(name, has_list[i]) == 0)
14062 {
14063 n = TRUE;
14064 break;
14065 }
14066
14067 if (n == FALSE)
14068 {
14069 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014070 {
14071 if (name[5] == '-'
14072 && STRLEN(name) > 11
14073 && vim_isdigit(name[6])
14074 && vim_isdigit(name[8])
14075 && vim_isdigit(name[10]))
14076 {
14077 int major = atoi((char *)name + 6);
14078 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014079
14080 /* Expect "patch-9.9.01234". */
14081 n = (major < VIM_VERSION_MAJOR
14082 || (major == VIM_VERSION_MAJOR
14083 && (minor < VIM_VERSION_MINOR
14084 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014085 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014086 }
14087 else
14088 n = has_patch(atoi((char *)name + 5));
14089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 else if (STRICMP(name, "vim_starting") == 0)
14091 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014092#ifdef FEAT_MBYTE
14093 else if (STRICMP(name, "multi_byte_encoding") == 0)
14094 n = has_mbyte;
14095#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014096#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14097 else if (STRICMP(name, "balloon_multiline") == 0)
14098 n = multiline_balloon_available();
14099#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014100#ifdef DYNAMIC_TCL
14101 else if (STRICMP(name, "tcl") == 0)
14102 n = tcl_enabled(FALSE);
14103#endif
14104#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14105 else if (STRICMP(name, "iconv") == 0)
14106 n = iconv_enabled(FALSE);
14107#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014108#ifdef DYNAMIC_LUA
14109 else if (STRICMP(name, "lua") == 0)
14110 n = lua_enabled(FALSE);
14111#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014112#ifdef DYNAMIC_MZSCHEME
14113 else if (STRICMP(name, "mzscheme") == 0)
14114 n = mzscheme_enabled(FALSE);
14115#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116#ifdef DYNAMIC_RUBY
14117 else if (STRICMP(name, "ruby") == 0)
14118 n = ruby_enabled(FALSE);
14119#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014120#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014121#ifdef DYNAMIC_PYTHON
14122 else if (STRICMP(name, "python") == 0)
14123 n = python_enabled(FALSE);
14124#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014125#endif
14126#ifdef FEAT_PYTHON3
14127#ifdef DYNAMIC_PYTHON3
14128 else if (STRICMP(name, "python3") == 0)
14129 n = python3_enabled(FALSE);
14130#endif
14131#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132#ifdef DYNAMIC_PERL
14133 else if (STRICMP(name, "perl") == 0)
14134 n = perl_enabled(FALSE);
14135#endif
14136#ifdef FEAT_GUI
14137 else if (STRICMP(name, "gui_running") == 0)
14138 n = (gui.in_use || gui.starting);
14139# ifdef FEAT_GUI_W32
14140 else if (STRICMP(name, "gui_win32s") == 0)
14141 n = gui_is_win32s();
14142# endif
14143# ifdef FEAT_BROWSE
14144 else if (STRICMP(name, "browse") == 0)
14145 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14146# endif
14147#endif
14148#ifdef FEAT_SYN_HL
14149 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014150 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151#endif
14152#if defined(WIN3264)
14153 else if (STRICMP(name, "win95") == 0)
14154 n = mch_windows95();
14155#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014156#ifdef FEAT_NETBEANS_INTG
14157 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014158 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160 }
14161
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014162 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014163}
14164
14165/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014166 * "has_key()" function
14167 */
14168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014169f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014170{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014171 if (argvars[0].v_type != VAR_DICT)
14172 {
14173 EMSG(_(e_dictreq));
14174 return;
14175 }
14176 if (argvars[0].vval.v_dict == NULL)
14177 return;
14178
14179 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014180 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014181}
14182
14183/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014184 * "haslocaldir()" function
14185 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014187f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014188{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014189 win_T *wp = NULL;
14190
14191 wp = find_tabwin(&argvars[0], &argvars[1]);
14192 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014193}
14194
14195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014196 * "hasmapto()" function
14197 */
14198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014199f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200{
14201 char_u *name;
14202 char_u *mode;
14203 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014204 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014206 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014207 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014208 mode = (char_u *)"nvo";
14209 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014210 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014212 if (argvars[2].v_type != VAR_UNKNOWN)
14213 abbr = get_tv_number(&argvars[2]);
14214 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215
Bram Moolenaar2c932302006-03-18 21:42:09 +000014216 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014217 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014218 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220}
14221
14222/*
14223 * "histadd()" function
14224 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014226f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227{
14228#ifdef FEAT_CMDHIST
14229 int histype;
14230 char_u *str;
14231 char_u buf[NUMBUFLEN];
14232#endif
14233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014234 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014235 if (check_restricted() || check_secure())
14236 return;
14237#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014238 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14239 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 if (histype >= 0)
14241 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014242 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 if (*str != NUL)
14244 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014245 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014246 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014247 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014248 return;
14249 }
14250 }
14251#endif
14252}
14253
14254/*
14255 * "histdel()" function
14256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014258f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014259{
14260#ifdef FEAT_CMDHIST
14261 int n;
14262 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014264
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14266 if (str == NULL)
14267 n = 0;
14268 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014271 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014274 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014275 else
14276 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014277 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014278 get_tv_string_buf(&argvars[1], buf));
14279 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014280#endif
14281}
14282
14283/*
14284 * "histget()" function
14285 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014287f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288{
14289#ifdef FEAT_CMDHIST
14290 int type;
14291 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014292 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014293
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014294 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14295 if (str == NULL)
14296 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 {
14299 type = get_histtype(str);
14300 if (argvars[1].v_type == VAR_UNKNOWN)
14301 idx = get_history_idx(type);
14302 else
14303 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14304 /* -1 on type error */
14305 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014307#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014308 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014310 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311}
14312
14313/*
14314 * "histnr()" function
14315 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014316 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014317f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014318{
14319 int i;
14320
14321#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014322 char_u *history = get_tv_string_chk(&argvars[0]);
14323
14324 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 if (i >= HIST_CMD && i < HIST_COUNT)
14326 i = get_history_idx(i);
14327 else
14328#endif
14329 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014330 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331}
14332
14333/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 * "highlightID(name)" function
14335 */
14336 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014337f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014339 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340}
14341
14342/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014343 * "highlight_exists()" function
14344 */
14345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014346f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014347{
14348 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14349}
14350
14351/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352 * "hostname()" function
14353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014355f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014356{
14357 char_u hostname[256];
14358
14359 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014360 rettv->v_type = VAR_STRING;
14361 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014362}
14363
14364/*
14365 * iconv() function
14366 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014367 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014368f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014369{
14370#ifdef FEAT_MBYTE
14371 char_u buf1[NUMBUFLEN];
14372 char_u buf2[NUMBUFLEN];
14373 char_u *from, *to, *str;
14374 vimconv_T vimconv;
14375#endif
14376
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014377 rettv->v_type = VAR_STRING;
14378 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014379
14380#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 str = get_tv_string(&argvars[0]);
14382 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14383 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 vimconv.vc_type = CONV_NONE;
14385 convert_setup(&vimconv, from, to);
14386
14387 /* If the encodings are equal, no conversion needed. */
14388 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014389 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014390 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392
14393 convert_setup(&vimconv, NULL, NULL);
14394 vim_free(from);
14395 vim_free(to);
14396#endif
14397}
14398
14399/*
14400 * "indent()" function
14401 */
14402 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014403f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014404{
14405 linenr_T lnum;
14406
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014407 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014409 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014411 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412}
14413
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014414/*
14415 * "index()" function
14416 */
14417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014418f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014419{
Bram Moolenaar33570922005-01-25 22:26:29 +000014420 list_T *l;
14421 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014422 long idx = 0;
14423 int ic = FALSE;
14424
14425 rettv->vval.v_number = -1;
14426 if (argvars[0].v_type != VAR_LIST)
14427 {
14428 EMSG(_(e_listreq));
14429 return;
14430 }
14431 l = argvars[0].vval.v_list;
14432 if (l != NULL)
14433 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014434 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014435 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014437 int error = FALSE;
14438
Bram Moolenaar758711c2005-02-02 23:11:38 +000014439 /* Start at specified item. Use the cached index that list_find()
14440 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014442 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014443 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014444 ic = get_tv_number_chk(&argvars[3], &error);
14445 if (error)
14446 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014447 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014448
Bram Moolenaar758711c2005-02-02 23:11:38 +000014449 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014450 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014451 {
14452 rettv->vval.v_number = idx;
14453 break;
14454 }
14455 }
14456}
14457
Bram Moolenaar071d4272004-06-13 20:20:40 +000014458static int inputsecret_flag = 0;
14459
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014460static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014461
Bram Moolenaar071d4272004-06-13 20:20:40 +000014462/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014463 * This function is used by f_input() and f_inputdialog() functions. The third
14464 * argument to f_input() specifies the type of completion to use at the
14465 * prompt. The third argument to f_inputdialog() specifies the value to return
14466 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014467 */
14468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014469get_user_input(
14470 typval_T *argvars,
14471 typval_T *rettv,
14472 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014473{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014474 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475 char_u *p = NULL;
14476 int c;
14477 char_u buf[NUMBUFLEN];
14478 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014479 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014480 int xp_type = EXPAND_NOTHING;
14481 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014483 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014484 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014485
14486#ifdef NO_CONSOLE_INPUT
14487 /* While starting up, there is no place to enter text. */
14488 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014489 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490#endif
14491
14492 cmd_silent = FALSE; /* Want to see the prompt. */
14493 if (prompt != NULL)
14494 {
14495 /* Only the part of the message after the last NL is considered as
14496 * prompt for the command line */
14497 p = vim_strrchr(prompt, '\n');
14498 if (p == NULL)
14499 p = prompt;
14500 else
14501 {
14502 ++p;
14503 c = *p;
14504 *p = NUL;
14505 msg_start();
14506 msg_clr_eos();
14507 msg_puts_attr(prompt, echo_attr);
14508 msg_didout = FALSE;
14509 msg_starthere();
14510 *p = c;
14511 }
14512 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014514 if (argvars[1].v_type != VAR_UNKNOWN)
14515 {
14516 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14517 if (defstr != NULL)
14518 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014519
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014520 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014521 {
14522 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014523 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014524 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014525
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014526 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014527 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014528
Bram Moolenaar4463f292005-09-25 22:20:24 +000014529 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14530 if (xp_name == NULL)
14531 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014532
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014533 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014534
Bram Moolenaar4463f292005-09-25 22:20:24 +000014535 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14536 &xp_arg) == FAIL)
14537 return;
14538 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014539 }
14540
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014542 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014543 int save_ex_normal_busy = ex_normal_busy;
14544 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014545 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014546 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14547 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014548 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014549 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014550 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014551 && argvars[1].v_type != VAR_UNKNOWN
14552 && argvars[2].v_type != VAR_UNKNOWN)
14553 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14554 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014555
14556 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014557
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 /* since the user typed this, no need to wait for return */
14559 need_wait_return = FALSE;
14560 msg_didout = FALSE;
14561 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014562 cmd_silent = cmd_silent_save;
14563}
14564
14565/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014566 * "input()" function
14567 * Also handles inputsecret() when inputsecret is set.
14568 */
14569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014570f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014571{
14572 get_user_input(argvars, rettv, FALSE);
14573}
14574
14575/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014576 * "inputdialog()" function
14577 */
14578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014579f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580{
14581#if defined(FEAT_GUI_TEXTDIALOG)
14582 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14583 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14584 {
14585 char_u *message;
14586 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014587 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014589 message = get_tv_string_chk(&argvars[0]);
14590 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014591 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014592 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014593 else
14594 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014595 if (message != NULL && defstr != NULL
14596 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014597 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014598 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014599 else
14600 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014601 if (message != NULL && defstr != NULL
14602 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014603 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->vval.v_string = vim_strsave(
14605 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014607 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014608 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014609 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014610 }
14611 else
14612#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014613 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014614}
14615
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014616/*
14617 * "inputlist()" function
14618 */
14619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014620f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014621{
14622 listitem_T *li;
14623 int selected;
14624 int mouse_used;
14625
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014626#ifdef NO_CONSOLE_INPUT
14627 /* While starting up, there is no place to enter text. */
14628 if (no_console_input())
14629 return;
14630#endif
14631 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14632 {
14633 EMSG2(_(e_listarg), "inputlist()");
14634 return;
14635 }
14636
14637 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014638 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014639 lines_left = Rows; /* avoid more prompt */
14640 msg_scroll = TRUE;
14641 msg_clr_eos();
14642
14643 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14644 {
14645 msg_puts(get_tv_string(&li->li_tv));
14646 msg_putchar('\n');
14647 }
14648
14649 /* Ask for choice. */
14650 selected = prompt_for_number(&mouse_used);
14651 if (mouse_used)
14652 selected -= lines_left;
14653
14654 rettv->vval.v_number = selected;
14655}
14656
14657
Bram Moolenaar071d4272004-06-13 20:20:40 +000014658static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14659
14660/*
14661 * "inputrestore()" function
14662 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014664f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014665{
14666 if (ga_userinput.ga_len > 0)
14667 {
14668 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14670 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014671 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014672 }
14673 else if (p_verbose > 1)
14674 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014675 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014676 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014677 }
14678}
14679
14680/*
14681 * "inputsave()" function
14682 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014684f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014685{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014686 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014687 if (ga_grow(&ga_userinput, 1) == OK)
14688 {
14689 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14690 + ga_userinput.ga_len);
14691 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014692 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693 }
14694 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014695 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014696}
14697
14698/*
14699 * "inputsecret()" function
14700 */
14701 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014702f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014703{
14704 ++cmdline_star;
14705 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014706 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014707 --cmdline_star;
14708 --inputsecret_flag;
14709}
14710
14711/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014712 * "insert()" function
14713 */
14714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014715f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014716{
14717 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014718 listitem_T *item;
14719 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014720 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014721
14722 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014723 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014724 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014725 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014726 {
14727 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014728 before = get_tv_number_chk(&argvars[2], &error);
14729 if (error)
14730 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014731
Bram Moolenaar758711c2005-02-02 23:11:38 +000014732 if (before == l->lv_len)
14733 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014734 else
14735 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014736 item = list_find(l, before);
14737 if (item == NULL)
14738 {
14739 EMSGN(_(e_listidx), before);
14740 l = NULL;
14741 }
14742 }
14743 if (l != NULL)
14744 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014745 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014746 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014747 }
14748 }
14749}
14750
14751/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014752 * "invert(expr)" function
14753 */
14754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014755f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014756{
14757 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14758}
14759
14760/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014761 * "isdirectory()" function
14762 */
14763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014764f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014765{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014766 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014767}
14768
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014769/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014770 * "islocked()" function
14771 */
14772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014773f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014774{
14775 lval_T lv;
14776 char_u *end;
14777 dictitem_T *di;
14778
14779 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014780 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14781 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014782 if (end != NULL && lv.ll_name != NULL)
14783 {
14784 if (*end != NUL)
14785 EMSG(_(e_trailing));
14786 else
14787 {
14788 if (lv.ll_tv == NULL)
14789 {
14790 if (check_changedtick(lv.ll_name))
14791 rettv->vval.v_number = 1; /* always locked */
14792 else
14793 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014794 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014795 if (di != NULL)
14796 {
14797 /* Consider a variable locked when:
14798 * 1. the variable itself is locked
14799 * 2. the value of the variable is locked.
14800 * 3. the List or Dict value is locked.
14801 */
14802 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14803 || tv_islocked(&di->di_tv));
14804 }
14805 }
14806 }
14807 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014808 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014809 else if (lv.ll_newkey != NULL)
14810 EMSG2(_(e_dictkey), lv.ll_newkey);
14811 else if (lv.ll_list != NULL)
14812 /* List item. */
14813 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14814 else
14815 /* Dictionary item. */
14816 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14817 }
14818 }
14819
14820 clear_lval(&lv);
14821}
14822
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014823#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14824/*
14825 * "isnan()" function
14826 */
14827 static void
14828f_isnan(typval_T *argvars, typval_T *rettv)
14829{
14830 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14831 && isnan(argvars[0].vval.v_float);
14832}
14833#endif
14834
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014835static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014836
14837/*
14838 * Turn a dict into a list:
14839 * "what" == 0: list of keys
14840 * "what" == 1: list of values
14841 * "what" == 2: list of items
14842 */
14843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014844dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014845{
Bram Moolenaar33570922005-01-25 22:26:29 +000014846 list_T *l2;
14847 dictitem_T *di;
14848 hashitem_T *hi;
14849 listitem_T *li;
14850 listitem_T *li2;
14851 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014852 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014853
Bram Moolenaar8c711452005-01-14 21:53:12 +000014854 if (argvars[0].v_type != VAR_DICT)
14855 {
14856 EMSG(_(e_dictreq));
14857 return;
14858 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014859 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014860 return;
14861
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014862 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014863 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014864
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014865 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014866 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014867 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014868 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014869 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014870 --todo;
14871 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014872
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014873 li = listitem_alloc();
14874 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014875 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014876 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014877
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014878 if (what == 0)
14879 {
14880 /* keys() */
14881 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014882 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014883 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14884 }
14885 else if (what == 1)
14886 {
14887 /* values() */
14888 copy_tv(&di->di_tv, &li->li_tv);
14889 }
14890 else
14891 {
14892 /* items() */
14893 l2 = list_alloc();
14894 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014895 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014896 li->li_tv.vval.v_list = l2;
14897 if (l2 == NULL)
14898 break;
14899 ++l2->lv_refcount;
14900
14901 li2 = listitem_alloc();
14902 if (li2 == NULL)
14903 break;
14904 list_append(l2, li2);
14905 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014906 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014907 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14908
14909 li2 = listitem_alloc();
14910 if (li2 == NULL)
14911 break;
14912 list_append(l2, li2);
14913 copy_tv(&di->di_tv, &li2->li_tv);
14914 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014915 }
14916 }
14917}
14918
14919/*
14920 * "items(dict)" function
14921 */
14922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014923f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014924{
14925 dict_list(argvars, rettv, 2);
14926}
14927
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014928#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014929/*
14930 * Get the job from the argument.
14931 * Returns NULL if the job is invalid.
14932 */
14933 static job_T *
14934get_job_arg(typval_T *tv)
14935{
14936 job_T *job;
14937
14938 if (tv->v_type != VAR_JOB)
14939 {
14940 EMSG2(_(e_invarg2), get_tv_string(tv));
14941 return NULL;
14942 }
14943 job = tv->vval.v_job;
14944
14945 if (job == NULL)
14946 EMSG(_("E916: not a valid job"));
14947 return job;
14948}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014949
14950# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014951/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014952 * "job_getchannel()" function
14953 */
14954 static void
14955f_job_getchannel(typval_T *argvars, typval_T *rettv)
14956{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014957 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014958
Bram Moolenaar65edff82016-02-21 16:40:11 +010014959 if (job != NULL)
14960 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014961 rettv->v_type = VAR_CHANNEL;
14962 rettv->vval.v_channel = job->jv_channel;
14963 if (job->jv_channel != NULL)
14964 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014965 }
14966}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014967# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014968
14969/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014970 * "job_setoptions()" function
14971 */
14972 static void
14973f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14974{
14975 job_T *job = get_job_arg(&argvars[0]);
14976 jobopt_T opt;
14977
14978 if (job == NULL)
14979 return;
14980 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014981 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014982 return;
14983 job_set_options(job, &opt);
14984}
14985
14986/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014987 * "job_start()" function
14988 */
14989 static void
14990f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14991{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014992 job_T *job;
14993 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014994#if defined(UNIX)
14995# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014996 char **argv = NULL;
14997 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014998#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014999 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015000#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015001 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015002
15003 rettv->v_type = VAR_JOB;
15004 job = job_alloc();
15005 rettv->vval.v_job = job;
15006 if (job == NULL)
15007 return;
15008
15009 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015010
15011 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015012 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015013 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015014 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015015 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015016 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015017 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015018 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015019
Bram Moolenaar835dc632016-02-07 14:27:38 +010015020#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015021 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015022#endif
15023
15024 if (argvars[0].v_type == VAR_STRING)
15025 {
15026 /* Command is a string. */
15027 cmd = argvars[0].vval.v_string;
15028#ifdef USE_ARGV
15029 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15030 return;
15031 argv[argc] = NULL;
15032#endif
15033 }
15034 else if (argvars[0].v_type != VAR_LIST
15035 || argvars[0].vval.v_list == NULL
15036 || argvars[0].vval.v_list->lv_len < 1)
15037 {
15038 EMSG(_(e_invarg));
15039 return;
15040 }
15041 else
15042 {
15043 list_T *l = argvars[0].vval.v_list;
15044 listitem_T *li;
15045 char_u *s;
15046
15047#ifdef USE_ARGV
15048 /* Pass argv[] to mch_call_shell(). */
15049 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15050 if (argv == NULL)
15051 return;
15052#endif
15053 for (li = l->lv_first; li != NULL; li = li->li_next)
15054 {
15055 s = get_tv_string_chk(&li->li_tv);
15056 if (s == NULL)
15057 goto theend;
15058#ifdef USE_ARGV
15059 argv[argc++] = (char *)s;
15060#else
15061 if (li != l->lv_first)
15062 {
15063 s = vim_strsave_shellescape(s, FALSE, TRUE);
15064 if (s == NULL)
15065 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015066 ga_concat(&ga, s);
15067 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015068 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015069 else
15070 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015071 if (li->li_next != NULL)
15072 ga_append(&ga, ' ');
15073#endif
15074 }
15075#ifdef USE_ARGV
15076 argv[argc] = NULL;
15077#else
15078 cmd = ga.ga_data;
15079#endif
15080 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015081
Bram Moolenaar835dc632016-02-07 14:27:38 +010015082#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015083# ifdef FEAT_CHANNEL
15084 if (ch_log_active())
15085 {
15086 garray_T ga;
15087 int i;
15088
15089 ga_init2(&ga, (int)sizeof(char), 200);
15090 for (i = 0; i < argc; ++i)
15091 {
15092 if (i > 0)
15093 ga_concat(&ga, (char_u *)" ");
15094 ga_concat(&ga, (char_u *)argv[i]);
15095 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015096 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015097 ga_clear(&ga);
15098 }
15099# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015100 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015101#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015102# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015103 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015104# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015105 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015106#endif
15107
15108theend:
15109#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015110 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015111#else
15112 vim_free(ga.ga_data);
15113#endif
15114}
15115
15116/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015117 * Get the status of "job" and invoke the exit callback when needed.
15118 * The returned string is not allocated.
15119 */
15120 static char *
15121job_status(job_T *job)
15122{
15123 char *result;
15124
15125 if (job->jv_status == JOB_ENDED)
15126 /* No need to check, dead is dead. */
15127 result = "dead";
15128 else if (job->jv_status == JOB_FAILED)
15129 result = "fail";
15130 else
15131 {
15132 result = mch_job_status(job);
15133# ifdef FEAT_CHANNEL
15134 if (job->jv_status == JOB_ENDED)
15135 ch_log(job->jv_channel, "Job ended");
15136# endif
15137 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15138 {
15139 typval_T argv[3];
15140 typval_T rettv;
15141 int dummy;
15142
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015143 /* invoke the exit callback; make sure the refcount is > 0 */
15144 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015145 argv[0].v_type = VAR_JOB;
15146 argv[0].vval.v_job = job;
15147 argv[1].v_type = VAR_NUMBER;
15148 argv[1].vval.v_number = job->jv_exitval;
15149 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15150 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15151 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015152 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015153 }
15154 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15155 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015156 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015157 * freed. Careful: caller must not use "job" after this! */
15158 job_free(job);
15159 }
15160 }
15161 return result;
15162}
15163
15164/*
15165 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15166 */
15167 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015168job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015169{
15170 static time_t last_check = 0;
15171 time_t now;
15172 job_T *job;
15173 job_T *next;
15174
15175 /* Only do this once in 10 seconds. */
15176 now = time(NULL);
15177 if (last_check + 10 < now)
15178 {
15179 last_check = now;
15180 for (job = first_job; job != NULL; job = next)
15181 {
15182 next = job->jv_next;
15183 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15184 job_status(job); /* may free "job" */
15185 }
15186 }
15187}
15188
15189/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015190 * "job_status()" function
15191 */
15192 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015193f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015194{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015195 job_T *job = get_job_arg(&argvars[0]);
15196 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015197
Bram Moolenaar65edff82016-02-21 16:40:11 +010015198 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015199 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015200 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015201 rettv->v_type = VAR_STRING;
15202 rettv->vval.v_string = vim_strsave((char_u *)result);
15203 }
15204}
15205
15206/*
15207 * "job_stop()" function
15208 */
15209 static void
15210f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15211{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015212 job_T *job = get_job_arg(&argvars[0]);
15213
15214 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015215 {
15216 char_u *arg;
15217
15218 if (argvars[1].v_type == VAR_UNKNOWN)
15219 arg = (char_u *)"";
15220 else
15221 {
15222 arg = get_tv_string_chk(&argvars[1]);
15223 if (arg == NULL)
15224 {
15225 EMSG(_(e_invarg));
15226 return;
15227 }
15228 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015229 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015230 rettv->vval.v_number = 0;
15231 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015232 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015233 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015234 /* Assume that "hup" does not kill the job. */
15235 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15236 job->jv_channel->ch_job_killed = TRUE;
15237 }
15238 /* We don't try freeing the job, obviously the caller still has a
15239 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015240 }
15241}
15242#endif
15243
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015245 * "join()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015249{
15250 garray_T ga;
15251 char_u *sep;
15252
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015253 if (argvars[0].v_type != VAR_LIST)
15254 {
15255 EMSG(_(e_listreq));
15256 return;
15257 }
15258 if (argvars[0].vval.v_list == NULL)
15259 return;
15260 if (argvars[1].v_type == VAR_UNKNOWN)
15261 sep = (char_u *)" ";
15262 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015264
15265 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015266
15267 if (sep != NULL)
15268 {
15269 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015270 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015271 ga_append(&ga, NUL);
15272 rettv->vval.v_string = (char_u *)ga.ga_data;
15273 }
15274 else
15275 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276}
15277
15278/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015279 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015280 */
15281 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015282f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015283{
15284 js_read_T reader;
15285
15286 reader.js_buf = get_tv_string(&argvars[0]);
15287 reader.js_fill = NULL;
15288 reader.js_used = 0;
15289 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15290 EMSG(_(e_invarg));
15291}
15292
15293/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015294 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015295 */
15296 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015297f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015298{
15299 rettv->v_type = VAR_STRING;
15300 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15301}
15302
15303/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015304 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015305 */
15306 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015307f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015308{
15309 js_read_T reader;
15310
15311 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015312 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015313 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015314 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015315 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015316}
15317
15318/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015319 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015320 */
15321 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015322f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015323{
15324 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015325 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015326}
15327
15328/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015329 * "keys()" function
15330 */
15331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015332f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015333{
15334 dict_list(argvars, rettv, 0);
15335}
15336
15337/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015338 * "last_buffer_nr()" function.
15339 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015340 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015341f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015342{
15343 int n = 0;
15344 buf_T *buf;
15345
15346 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15347 if (n < buf->b_fnum)
15348 n = buf->b_fnum;
15349
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015350 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015351}
15352
15353/*
15354 * "len()" function
15355 */
15356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015357f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015358{
15359 switch (argvars[0].v_type)
15360 {
15361 case VAR_STRING:
15362 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015363 rettv->vval.v_number = (varnumber_T)STRLEN(
15364 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015365 break;
15366 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015367 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015368 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015369 case VAR_DICT:
15370 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15371 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015372 case VAR_UNKNOWN:
15373 case VAR_SPECIAL:
15374 case VAR_FLOAT:
15375 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015376 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015377 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015378 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015379 break;
15380 }
15381}
15382
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015383static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015384
15385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015386libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015387{
15388#ifdef FEAT_LIBCALL
15389 char_u *string_in;
15390 char_u **string_result;
15391 int nr_result;
15392#endif
15393
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015394 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015395 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015396 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015397
15398 if (check_restricted() || check_secure())
15399 return;
15400
15401#ifdef FEAT_LIBCALL
15402 /* The first two args must be strings, otherwise its meaningless */
15403 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15404 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015405 string_in = NULL;
15406 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015407 string_in = argvars[2].vval.v_string;
15408 if (type == VAR_NUMBER)
15409 string_result = NULL;
15410 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015411 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015412 if (mch_libcall(argvars[0].vval.v_string,
15413 argvars[1].vval.v_string,
15414 string_in,
15415 argvars[2].vval.v_number,
15416 string_result,
15417 &nr_result) == OK
15418 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015419 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015420 }
15421#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015422}
15423
15424/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015425 * "libcall()" function
15426 */
15427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015428f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015429{
15430 libcall_common(argvars, rettv, VAR_STRING);
15431}
15432
15433/*
15434 * "libcallnr()" function
15435 */
15436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015437f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015438{
15439 libcall_common(argvars, rettv, VAR_NUMBER);
15440}
15441
15442/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 * "line(string)" function
15444 */
15445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015446f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447{
15448 linenr_T lnum = 0;
15449 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015450 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015452 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453 if (fp != NULL)
15454 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015455 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456}
15457
15458/*
15459 * "line2byte(lnum)" function
15460 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015462f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463{
15464#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015465 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015466#else
15467 linenr_T lnum;
15468
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015469 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015470 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015471 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015472 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015473 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15474 if (rettv->vval.v_number >= 0)
15475 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015476#endif
15477}
15478
15479/*
15480 * "lispindent(lnum)" function
15481 */
15482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015483f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015484{
15485#ifdef FEAT_LISP
15486 pos_T pos;
15487 linenr_T lnum;
15488
15489 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15492 {
15493 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015494 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015495 curwin->w_cursor = pos;
15496 }
15497 else
15498#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015499 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015500}
15501
15502/*
15503 * "localtime()" function
15504 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015505 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015506f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015507{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015508 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509}
15510
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015511static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512
15513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015514get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015515{
15516 char_u *keys;
15517 char_u *which;
15518 char_u buf[NUMBUFLEN];
15519 char_u *keys_buf = NULL;
15520 char_u *rhs;
15521 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015522 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015523 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015524 mapblock_T *mp;
15525 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015526
15527 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 rettv->v_type = VAR_STRING;
15529 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015530
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015531 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015532 if (*keys == NUL)
15533 return;
15534
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015535 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015536 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015537 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015538 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015539 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015540 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015541 if (argvars[3].v_type != VAR_UNKNOWN)
15542 get_dict = get_tv_number(&argvars[3]);
15543 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015544 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015545 else
15546 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015547 if (which == NULL)
15548 return;
15549
Bram Moolenaar071d4272004-06-13 20:20:40 +000015550 mode = get_map_mode(&which, 0);
15551
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015552 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015553 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015555
15556 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015557 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015558 /* Return a string. */
15559 if (rhs != NULL)
15560 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015561
Bram Moolenaarbd743252010-10-20 21:23:33 +020015562 }
15563 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15564 {
15565 /* Return a dictionary. */
15566 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15567 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15568 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015569
Bram Moolenaarbd743252010-10-20 21:23:33 +020015570 dict_add_nr_str(dict, "lhs", 0L, lhs);
15571 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15572 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15573 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15574 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15575 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15576 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015577 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015578 dict_add_nr_str(dict, "mode", 0L, mapmode);
15579
15580 vim_free(lhs);
15581 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015582 }
15583}
15584
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015585#ifdef FEAT_FLOAT
15586/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015587 * "log()" function
15588 */
15589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015590f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015591{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015592 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015593
15594 rettv->v_type = VAR_FLOAT;
15595 if (get_float_arg(argvars, &f) == OK)
15596 rettv->vval.v_float = log(f);
15597 else
15598 rettv->vval.v_float = 0.0;
15599}
15600
15601/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015602 * "log10()" function
15603 */
15604 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015605f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015606{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015607 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015608
15609 rettv->v_type = VAR_FLOAT;
15610 if (get_float_arg(argvars, &f) == OK)
15611 rettv->vval.v_float = log10(f);
15612 else
15613 rettv->vval.v_float = 0.0;
15614}
15615#endif
15616
Bram Moolenaar1dced572012-04-05 16:54:08 +020015617#ifdef FEAT_LUA
15618/*
15619 * "luaeval()" function
15620 */
15621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015622f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015623{
15624 char_u *str;
15625 char_u buf[NUMBUFLEN];
15626
15627 str = get_tv_string_buf(&argvars[0], buf);
15628 do_luaeval(str, argvars + 1, rettv);
15629}
15630#endif
15631
Bram Moolenaar071d4272004-06-13 20:20:40 +000015632/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015633 * "map()" function
15634 */
15635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015636f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015637{
15638 filter_map(argvars, rettv, TRUE);
15639}
15640
15641/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015642 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643 */
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015646{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015647 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015648}
15649
15650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015655{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015656 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657}
15658
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015659static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660
15661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015662find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015663{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015664 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015665 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015666 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015667 char_u *pat;
15668 regmatch_T regmatch;
15669 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015670 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015671 char_u *save_cpo;
15672 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015673 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015674 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015675 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015676 list_T *l = NULL;
15677 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015678 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015679 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015680
15681 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15682 save_cpo = p_cpo;
15683 p_cpo = (char_u *)"";
15684
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015685 rettv->vval.v_number = -1;
15686 if (type == 3)
15687 {
15688 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015689 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015690 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015691 }
15692 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015694 rettv->v_type = VAR_STRING;
15695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015697
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015698 if (argvars[0].v_type == VAR_LIST)
15699 {
15700 if ((l = argvars[0].vval.v_list) == NULL)
15701 goto theend;
15702 li = l->lv_first;
15703 }
15704 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015705 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015706 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015707 len = (long)STRLEN(str);
15708 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015709
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15711 if (pat == NULL)
15712 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015713
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015714 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015716 int error = FALSE;
15717
15718 start = get_tv_number_chk(&argvars[2], &error);
15719 if (error)
15720 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015721 if (l != NULL)
15722 {
15723 li = list_find(l, start);
15724 if (li == NULL)
15725 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015726 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015727 }
15728 else
15729 {
15730 if (start < 0)
15731 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015732 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015733 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015734 /* When "count" argument is there ignore matches before "start",
15735 * otherwise skip part of the string. Differs when pattern is "^"
15736 * or "\<". */
15737 if (argvars[3].v_type != VAR_UNKNOWN)
15738 startcol = start;
15739 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015740 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015741 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015742 len -= start;
15743 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015744 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015745
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015746 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015747 nth = get_tv_number_chk(&argvars[3], &error);
15748 if (error)
15749 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015750 }
15751
15752 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15753 if (regmatch.regprog != NULL)
15754 {
15755 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015756
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015757 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015758 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015759 if (l != NULL)
15760 {
15761 if (li == NULL)
15762 {
15763 match = FALSE;
15764 break;
15765 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015766 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015767 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015768 if (str == NULL)
15769 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770 }
15771
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015772 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015773
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015774 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015775 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015776 if (l == NULL && !match)
15777 break;
15778
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015779 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015780 if (l != NULL)
15781 {
15782 li = li->li_next;
15783 ++idx;
15784 }
15785 else
15786 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015787#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015788 startcol = (colnr_T)(regmatch.startp[0]
15789 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015790#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015791 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015792#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015793 if (startcol > (colnr_T)len
15794 || str + startcol <= regmatch.startp[0])
15795 {
15796 match = FALSE;
15797 break;
15798 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015799 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015800 }
15801
15802 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015803 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015804 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015805 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015806 int i;
15807
15808 /* return list with matched string and submatches */
15809 for (i = 0; i < NSUBEXP; ++i)
15810 {
15811 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015812 {
15813 if (list_append_string(rettv->vval.v_list,
15814 (char_u *)"", 0) == FAIL)
15815 break;
15816 }
15817 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015818 regmatch.startp[i],
15819 (int)(regmatch.endp[i] - regmatch.startp[i]))
15820 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015821 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015822 }
15823 }
15824 else if (type == 2)
15825 {
15826 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015827 if (l != NULL)
15828 copy_tv(&li->li_tv, rettv);
15829 else
15830 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015832 }
15833 else if (l != NULL)
15834 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015835 else
15836 {
15837 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015838 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 (varnumber_T)(regmatch.startp[0] - str);
15840 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015841 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015842 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015843 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 }
15845 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015846 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015847 }
15848
15849theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015850 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 p_cpo = save_cpo;
15852}
15853
15854/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015855 * "match()" function
15856 */
15857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015858f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015859{
15860 find_some_match(argvars, rettv, 1);
15861}
15862
15863/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015864 * "matchadd()" function
15865 */
15866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015867f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015868{
15869#ifdef FEAT_SEARCH_EXTRA
15870 char_u buf[NUMBUFLEN];
15871 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15872 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15873 int prio = 10; /* default priority */
15874 int id = -1;
15875 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015876 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015877
15878 rettv->vval.v_number = -1;
15879
15880 if (grp == NULL || pat == NULL)
15881 return;
15882 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015883 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015884 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015885 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015886 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015887 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015888 if (argvars[4].v_type != VAR_UNKNOWN)
15889 {
15890 if (argvars[4].v_type != VAR_DICT)
15891 {
15892 EMSG(_(e_dictreq));
15893 return;
15894 }
15895 if (dict_find(argvars[4].vval.v_dict,
15896 (char_u *)"conceal", -1) != NULL)
15897 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15898 (char_u *)"conceal", FALSE);
15899 }
15900 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015901 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015902 if (error == TRUE)
15903 return;
15904 if (id >= 1 && id <= 3)
15905 {
15906 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15907 return;
15908 }
15909
Bram Moolenaar6561d522015-07-21 15:48:27 +020015910 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15911 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015912#endif
15913}
15914
15915/*
15916 * "matchaddpos()" function
15917 */
15918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015919f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015920{
15921#ifdef FEAT_SEARCH_EXTRA
15922 char_u buf[NUMBUFLEN];
15923 char_u *group;
15924 int prio = 10;
15925 int id = -1;
15926 int error = FALSE;
15927 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015928 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015929
15930 rettv->vval.v_number = -1;
15931
15932 group = get_tv_string_buf_chk(&argvars[0], buf);
15933 if (group == NULL)
15934 return;
15935
15936 if (argvars[1].v_type != VAR_LIST)
15937 {
15938 EMSG2(_(e_listarg), "matchaddpos()");
15939 return;
15940 }
15941 l = argvars[1].vval.v_list;
15942 if (l == NULL)
15943 return;
15944
15945 if (argvars[2].v_type != VAR_UNKNOWN)
15946 {
15947 prio = get_tv_number_chk(&argvars[2], &error);
15948 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015949 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015950 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015951 if (argvars[4].v_type != VAR_UNKNOWN)
15952 {
15953 if (argvars[4].v_type != VAR_DICT)
15954 {
15955 EMSG(_(e_dictreq));
15956 return;
15957 }
15958 if (dict_find(argvars[4].vval.v_dict,
15959 (char_u *)"conceal", -1) != NULL)
15960 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15961 (char_u *)"conceal", FALSE);
15962 }
15963 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015964 }
15965 if (error == TRUE)
15966 return;
15967
15968 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15969 if (id == 1 || id == 2)
15970 {
15971 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15972 return;
15973 }
15974
Bram Moolenaar6561d522015-07-21 15:48:27 +020015975 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15976 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015977#endif
15978}
15979
15980/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015981 * "matcharg()" function
15982 */
15983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015984f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015985{
15986 if (rettv_list_alloc(rettv) == OK)
15987 {
15988#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015989 int id = get_tv_number(&argvars[0]);
15990 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015991
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015992 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015993 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015994 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15995 {
15996 list_append_string(rettv->vval.v_list,
15997 syn_id2name(m->hlg_id), -1);
15998 list_append_string(rettv->vval.v_list, m->pattern, -1);
15999 }
16000 else
16001 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010016002 list_append_string(rettv->vval.v_list, NULL, -1);
16003 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016004 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016005 }
16006#endif
16007 }
16008}
16009
16010/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016011 * "matchdelete()" function
16012 */
16013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016014f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016015{
16016#ifdef FEAT_SEARCH_EXTRA
16017 rettv->vval.v_number = match_delete(curwin,
16018 (int)get_tv_number(&argvars[0]), TRUE);
16019#endif
16020}
16021
16022/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016023 * "matchend()" function
16024 */
16025 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016026f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027{
16028 find_some_match(argvars, rettv, 0);
16029}
16030
16031/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016032 * "matchlist()" function
16033 */
16034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016035f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016036{
16037 find_some_match(argvars, rettv, 3);
16038}
16039
16040/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016041 * "matchstr()" function
16042 */
16043 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016044f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016045{
16046 find_some_match(argvars, rettv, 2);
16047}
16048
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016049static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016050
16051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016052max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016053{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016054 long n = 0;
16055 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016056 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016057
16058 if (argvars[0].v_type == VAR_LIST)
16059 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016060 list_T *l;
16061 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016062
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016063 l = argvars[0].vval.v_list;
16064 if (l != NULL)
16065 {
16066 li = l->lv_first;
16067 if (li != NULL)
16068 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016069 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016070 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016071 {
16072 li = li->li_next;
16073 if (li == NULL)
16074 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016075 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016076 if (domax ? i > n : i < n)
16077 n = i;
16078 }
16079 }
16080 }
16081 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016082 else if (argvars[0].v_type == VAR_DICT)
16083 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016084 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016085 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016086 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016087 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016088
16089 d = argvars[0].vval.v_dict;
16090 if (d != NULL)
16091 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016092 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016093 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016094 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016095 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016096 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016097 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016099 if (first)
16100 {
16101 n = i;
16102 first = FALSE;
16103 }
16104 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016105 n = i;
16106 }
16107 }
16108 }
16109 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016110 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016111 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016112 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016113}
16114
16115/*
16116 * "max()" function
16117 */
16118 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016119f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016120{
16121 max_min(argvars, rettv, TRUE);
16122}
16123
16124/*
16125 * "min()" function
16126 */
16127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016128f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016129{
16130 max_min(argvars, rettv, FALSE);
16131}
16132
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016133static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016134
16135/*
16136 * Create the directory in which "dir" is located, and higher levels when
16137 * needed.
16138 */
16139 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016140mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016141{
16142 char_u *p;
16143 char_u *updir;
16144 int r = FAIL;
16145
16146 /* Get end of directory name in "dir".
16147 * We're done when it's "/" or "c:/". */
16148 p = gettail_sep(dir);
16149 if (p <= get_past_head(dir))
16150 return OK;
16151
16152 /* If the directory exists we're done. Otherwise: create it.*/
16153 updir = vim_strnsave(dir, (int)(p - dir));
16154 if (updir == NULL)
16155 return FAIL;
16156 if (mch_isdir(updir))
16157 r = OK;
16158 else if (mkdir_recurse(updir, prot) == OK)
16159 r = vim_mkdir_emsg(updir, prot);
16160 vim_free(updir);
16161 return r;
16162}
16163
16164#ifdef vim_mkdir
16165/*
16166 * "mkdir()" function
16167 */
16168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016169f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016170{
16171 char_u *dir;
16172 char_u buf[NUMBUFLEN];
16173 int prot = 0755;
16174
16175 rettv->vval.v_number = FAIL;
16176 if (check_restricted() || check_secure())
16177 return;
16178
16179 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016180 if (*dir == NUL)
16181 rettv->vval.v_number = FAIL;
16182 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016183 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016184 if (*gettail(dir) == NUL)
16185 /* remove trailing slashes */
16186 *gettail_sep(dir) = NUL;
16187
16188 if (argvars[1].v_type != VAR_UNKNOWN)
16189 {
16190 if (argvars[2].v_type != VAR_UNKNOWN)
16191 prot = get_tv_number_chk(&argvars[2], NULL);
16192 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16193 mkdir_recurse(dir, prot);
16194 }
16195 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016196 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016197}
16198#endif
16199
Bram Moolenaar0d660222005-01-07 21:51:51 +000016200/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016201 * "mode()" function
16202 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016204f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016205{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016206 char_u buf[3];
16207
16208 buf[1] = NUL;
16209 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016210
Bram Moolenaar071d4272004-06-13 20:20:40 +000016211 if (VIsual_active)
16212 {
16213 if (VIsual_select)
16214 buf[0] = VIsual_mode + 's' - 'v';
16215 else
16216 buf[0] = VIsual_mode;
16217 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016218 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016219 || State == CONFIRM)
16220 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016221 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016222 if (State == ASKMORE)
16223 buf[1] = 'm';
16224 else if (State == CONFIRM)
16225 buf[1] = '?';
16226 }
16227 else if (State == EXTERNCMD)
16228 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016229 else if (State & INSERT)
16230 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016231#ifdef FEAT_VREPLACE
16232 if (State & VREPLACE_FLAG)
16233 {
16234 buf[0] = 'R';
16235 buf[1] = 'v';
16236 }
16237 else
16238#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016239 if (State & REPLACE_FLAG)
16240 buf[0] = 'R';
16241 else
16242 buf[0] = 'i';
16243 }
16244 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016245 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016246 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016247 if (exmode_active)
16248 buf[1] = 'v';
16249 }
16250 else if (exmode_active)
16251 {
16252 buf[0] = 'c';
16253 buf[1] = 'e';
16254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016255 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016256 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016257 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016258 if (finish_op)
16259 buf[1] = 'o';
16260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016261
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016262 /* Clear out the minor mode when the argument is not a non-zero number or
16263 * non-empty string. */
16264 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016265 buf[1] = NUL;
16266
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016267 rettv->vval.v_string = vim_strsave(buf);
16268 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016269}
16270
Bram Moolenaar429fa852013-04-15 12:27:36 +020016271#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016272/*
16273 * "mzeval()" function
16274 */
16275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016276f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016277{
16278 char_u *str;
16279 char_u buf[NUMBUFLEN];
16280
16281 str = get_tv_string_buf(&argvars[0], buf);
16282 do_mzeval(str, rettv);
16283}
Bram Moolenaar75676462013-01-30 14:55:42 +010016284
16285 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016286mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016287{
16288 typval_T argvars[3];
16289
16290 argvars[0].v_type = VAR_STRING;
16291 argvars[0].vval.v_string = name;
16292 copy_tv(args, &argvars[1]);
16293 argvars[2].v_type = VAR_UNKNOWN;
16294 f_call(argvars, rettv);
16295 clear_tv(&argvars[1]);
16296}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016297#endif
16298
Bram Moolenaar071d4272004-06-13 20:20:40 +000016299/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016300 * "nextnonblank()" function
16301 */
16302 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016303f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016304{
16305 linenr_T lnum;
16306
16307 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016309 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016310 {
16311 lnum = 0;
16312 break;
16313 }
16314 if (*skipwhite(ml_get(lnum)) != NUL)
16315 break;
16316 }
16317 rettv->vval.v_number = lnum;
16318}
16319
16320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016321 * "nr2char()" function
16322 */
16323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016324f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016325{
16326 char_u buf[NUMBUFLEN];
16327
16328#ifdef FEAT_MBYTE
16329 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016330 {
16331 int utf8 = 0;
16332
16333 if (argvars[1].v_type != VAR_UNKNOWN)
16334 utf8 = get_tv_number_chk(&argvars[1], NULL);
16335 if (utf8)
16336 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16337 else
16338 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16339 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016340 else
16341#endif
16342 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016344 buf[1] = NUL;
16345 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016346 rettv->v_type = VAR_STRING;
16347 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016348}
16349
16350/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016351 * "or(expr, expr)" function
16352 */
16353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016354f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016355{
16356 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16357 | get_tv_number_chk(&argvars[1], NULL);
16358}
16359
16360/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016361 * "pathshorten()" function
16362 */
16363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016364f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016365{
16366 char_u *p;
16367
16368 rettv->v_type = VAR_STRING;
16369 p = get_tv_string_chk(&argvars[0]);
16370 if (p == NULL)
16371 rettv->vval.v_string = NULL;
16372 else
16373 {
16374 p = vim_strsave(p);
16375 rettv->vval.v_string = p;
16376 if (p != NULL)
16377 shorten_dir(p);
16378 }
16379}
16380
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016381#ifdef FEAT_PERL
16382/*
16383 * "perleval()" function
16384 */
16385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016386f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016387{
16388 char_u *str;
16389 char_u buf[NUMBUFLEN];
16390
16391 str = get_tv_string_buf(&argvars[0], buf);
16392 do_perleval(str, rettv);
16393}
16394#endif
16395
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016396#ifdef FEAT_FLOAT
16397/*
16398 * "pow()" function
16399 */
16400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016401f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016402{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016403 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016404
16405 rettv->v_type = VAR_FLOAT;
16406 if (get_float_arg(argvars, &fx) == OK
16407 && get_float_arg(&argvars[1], &fy) == OK)
16408 rettv->vval.v_float = pow(fx, fy);
16409 else
16410 rettv->vval.v_float = 0.0;
16411}
16412#endif
16413
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016414/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016415 * "prevnonblank()" function
16416 */
16417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016418f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016419{
16420 linenr_T lnum;
16421
16422 lnum = get_tv_lnum(argvars);
16423 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16424 lnum = 0;
16425 else
16426 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16427 --lnum;
16428 rettv->vval.v_number = lnum;
16429}
16430
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016431/* This dummy va_list is here because:
16432 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16433 * - locally in the function results in a "used before set" warning
16434 * - using va_start() to initialize it gives "function with fixed args" error */
16435static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016436
Bram Moolenaar8c711452005-01-14 21:53:12 +000016437/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016438 * "printf()" function
16439 */
16440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016441f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016442{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016443 char_u buf[NUMBUFLEN];
16444 int len;
16445 char_u *s;
16446 int saved_did_emsg = did_emsg;
16447 char *fmt;
16448
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016449 rettv->v_type = VAR_STRING;
16450 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016451
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016452 /* Get the required length, allocate the buffer and do it for real. */
16453 did_emsg = FALSE;
16454 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16455 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16456 if (!did_emsg)
16457 {
16458 s = alloc(len + 1);
16459 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016460 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016461 rettv->vval.v_string = s;
16462 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016463 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016464 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016465 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016466}
16467
16468/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016469 * "pumvisible()" function
16470 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016472f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016473{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016474#ifdef FEAT_INS_EXPAND
16475 if (pum_visible())
16476 rettv->vval.v_number = 1;
16477#endif
16478}
16479
Bram Moolenaardb913952012-06-29 12:54:53 +020016480#ifdef FEAT_PYTHON3
16481/*
16482 * "py3eval()" function
16483 */
16484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016485f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016486{
16487 char_u *str;
16488 char_u buf[NUMBUFLEN];
16489
16490 str = get_tv_string_buf(&argvars[0], buf);
16491 do_py3eval(str, rettv);
16492}
16493#endif
16494
16495#ifdef FEAT_PYTHON
16496/*
16497 * "pyeval()" function
16498 */
16499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016500f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016501{
16502 char_u *str;
16503 char_u buf[NUMBUFLEN];
16504
16505 str = get_tv_string_buf(&argvars[0], buf);
16506 do_pyeval(str, rettv);
16507}
16508#endif
16509
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016510/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016511 * "range()" function
16512 */
16513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016514f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016515{
16516 long start;
16517 long end;
16518 long stride = 1;
16519 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016522 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016523 if (argvars[1].v_type == VAR_UNKNOWN)
16524 {
16525 end = start - 1;
16526 start = 0;
16527 }
16528 else
16529 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016530 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016531 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016532 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016533 }
16534
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016535 if (error)
16536 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016537 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016538 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016539 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016540 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016541 else
16542 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016543 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016544 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016545 if (list_append_number(rettv->vval.v_list,
16546 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016547 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016548 }
16549}
16550
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016551/*
16552 * "readfile()" function
16553 */
16554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016555f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556{
16557 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016558 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016559 char_u *fname;
16560 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016561 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16562 int io_size = sizeof(buf);
16563 int readlen; /* size of last fread() */
16564 char_u *prev = NULL; /* previously read bytes, if any */
16565 long prevlen = 0; /* length of data in prev */
16566 long prevsize = 0; /* size of prev buffer */
16567 long maxline = MAXLNUM;
16568 long cnt = 0;
16569 char_u *p; /* position in buf */
16570 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016571
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016572 if (argvars[1].v_type != VAR_UNKNOWN)
16573 {
16574 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16575 binary = TRUE;
16576 if (argvars[2].v_type != VAR_UNKNOWN)
16577 maxline = get_tv_number(&argvars[2]);
16578 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016579
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016580 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016581 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016582
16583 /* Always open the file in binary mode, library functions have a mind of
16584 * their own about CR-LF conversion. */
16585 fname = get_tv_string(&argvars[0]);
16586 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16587 {
16588 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16589 return;
16590 }
16591
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016592 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016593 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016594 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016595
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016596 /* This for loop processes what was read, but is also entered at end
16597 * of file so that either:
16598 * - an incomplete line gets written
16599 * - a "binary" file gets an empty line at the end if it ends in a
16600 * newline. */
16601 for (p = buf, start = buf;
16602 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16603 ++p)
16604 {
16605 if (*p == '\n' || readlen <= 0)
16606 {
16607 listitem_T *li;
16608 char_u *s = NULL;
16609 long_u len = p - start;
16610
16611 /* Finished a line. Remove CRs before NL. */
16612 if (readlen > 0 && !binary)
16613 {
16614 while (len > 0 && start[len - 1] == '\r')
16615 --len;
16616 /* removal may cross back to the "prev" string */
16617 if (len == 0)
16618 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16619 --prevlen;
16620 }
16621 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016622 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016623 else
16624 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016625 /* Change "prev" buffer to be the right size. This way
16626 * the bytes are only copied once, and very long lines are
16627 * allocated only once. */
16628 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016629 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016630 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016632 prev = NULL; /* the list will own the string */
16633 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016634 }
16635 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016636 if (s == NULL)
16637 {
16638 do_outofmem_msg((long_u) prevlen + len + 1);
16639 failed = TRUE;
16640 break;
16641 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016642
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644 {
16645 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016646 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016647 break;
16648 }
16649 li->li_tv.v_type = VAR_STRING;
16650 li->li_tv.v_lock = 0;
16651 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016652 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016653
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016654 start = p + 1; /* step over newline */
16655 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016656 break;
16657 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 else if (*p == NUL)
16659 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016660#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016661 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16662 * when finding the BF and check the previous two bytes. */
16663 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016664 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016665 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16666 * + 1, these may be in the "prev" string. */
16667 char_u back1 = p >= buf + 1 ? p[-1]
16668 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16669 char_u back2 = p >= buf + 2 ? p[-2]
16670 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16671 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016672
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016673 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016674 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016675 char_u *dest = p - 2;
16676
16677 /* Usually a BOM is at the beginning of a file, and so at
16678 * the beginning of a line; then we can just step over it.
16679 */
16680 if (start == dest)
16681 start = p + 1;
16682 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016683 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016684 /* have to shuffle buf to close gap */
16685 int adjust_prevlen = 0;
16686
16687 if (dest < buf)
16688 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016689 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016690 dest = buf;
16691 }
16692 if (readlen > p - buf + 1)
16693 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16694 readlen -= 3 - adjust_prevlen;
16695 prevlen -= adjust_prevlen;
16696 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016697 }
16698 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016699 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016700#endif
16701 } /* for */
16702
16703 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16704 break;
16705 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016706 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016707 /* There's part of a line in buf, store it in "prev". */
16708 if (p - start + prevlen >= prevsize)
16709 {
16710 /* need bigger "prev" buffer */
16711 char_u *newprev;
16712
16713 /* A common use case is ordinary text files and "prev" gets a
16714 * fragment of a line, so the first allocation is made
16715 * small, to avoid repeatedly 'allocing' large and
16716 * 'reallocing' small. */
16717 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016718 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016719 else
16720 {
16721 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016722 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016723 prevsize = grow50pc > growmin ? grow50pc : growmin;
16724 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016725 newprev = prev == NULL ? alloc(prevsize)
16726 : vim_realloc(prev, prevsize);
16727 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016728 {
16729 do_outofmem_msg((long_u)prevsize);
16730 failed = TRUE;
16731 break;
16732 }
16733 prev = newprev;
16734 }
16735 /* Add the line part to end of "prev". */
16736 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016737 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016738 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016739 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016740
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016741 /*
16742 * For a negative line count use only the lines at the end of the file,
16743 * free the rest.
16744 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016745 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016746 while (cnt > -maxline)
16747 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016748 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016749 --cnt;
16750 }
16751
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016752 if (failed)
16753 {
16754 list_free(rettv->vval.v_list, TRUE);
16755 /* readfile doc says an empty list is returned on error */
16756 rettv->vval.v_list = list_alloc();
16757 }
16758
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016759 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016760 fclose(fd);
16761}
16762
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016763#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016764static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016765
16766/*
16767 * Convert a List to proftime_T.
16768 * Return FAIL when there is something wrong.
16769 */
16770 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016771list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016772{
16773 long n1, n2;
16774 int error = FALSE;
16775
16776 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16777 || arg->vval.v_list->lv_len != 2)
16778 return FAIL;
16779 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16780 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16781# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016782 tm->HighPart = n1;
16783 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016784# else
16785 tm->tv_sec = n1;
16786 tm->tv_usec = n2;
16787# endif
16788 return error ? FAIL : OK;
16789}
16790#endif /* FEAT_RELTIME */
16791
16792/*
16793 * "reltime()" function
16794 */
16795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016796f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016797{
16798#ifdef FEAT_RELTIME
16799 proftime_T res;
16800 proftime_T start;
16801
16802 if (argvars[0].v_type == VAR_UNKNOWN)
16803 {
16804 /* No arguments: get current time. */
16805 profile_start(&res);
16806 }
16807 else if (argvars[1].v_type == VAR_UNKNOWN)
16808 {
16809 if (list2proftime(&argvars[0], &res) == FAIL)
16810 return;
16811 profile_end(&res);
16812 }
16813 else
16814 {
16815 /* Two arguments: compute the difference. */
16816 if (list2proftime(&argvars[0], &start) == FAIL
16817 || list2proftime(&argvars[1], &res) == FAIL)
16818 return;
16819 profile_sub(&res, &start);
16820 }
16821
16822 if (rettv_list_alloc(rettv) == OK)
16823 {
16824 long n1, n2;
16825
16826# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016827 n1 = res.HighPart;
16828 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016829# else
16830 n1 = res.tv_sec;
16831 n2 = res.tv_usec;
16832# endif
16833 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16834 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16835 }
16836#endif
16837}
16838
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016839#ifdef FEAT_FLOAT
16840/*
16841 * "reltimefloat()" function
16842 */
16843 static void
16844f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16845{
16846# ifdef FEAT_RELTIME
16847 proftime_T tm;
16848# endif
16849
16850 rettv->v_type = VAR_FLOAT;
16851 rettv->vval.v_float = 0;
16852# ifdef FEAT_RELTIME
16853 if (list2proftime(&argvars[0], &tm) == OK)
16854 rettv->vval.v_float = profile_float(&tm);
16855# endif
16856}
16857#endif
16858
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016859/*
16860 * "reltimestr()" function
16861 */
16862 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016863f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016864{
16865#ifdef FEAT_RELTIME
16866 proftime_T tm;
16867#endif
16868
16869 rettv->v_type = VAR_STRING;
16870 rettv->vval.v_string = NULL;
16871#ifdef FEAT_RELTIME
16872 if (list2proftime(&argvars[0], &tm) == OK)
16873 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16874#endif
16875}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016876
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016878static void make_connection(void);
16879static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880
16881 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016882make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016883{
16884 if (X_DISPLAY == NULL
16885# ifdef FEAT_GUI
16886 && !gui.in_use
16887# endif
16888 )
16889 {
16890 x_force_connect = TRUE;
16891 setup_term_clip();
16892 x_force_connect = FALSE;
16893 }
16894}
16895
16896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016897check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016898{
16899 make_connection();
16900 if (X_DISPLAY == NULL)
16901 {
16902 EMSG(_("E240: No connection to Vim server"));
16903 return FAIL;
16904 }
16905 return OK;
16906}
16907#endif
16908
16909#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016910static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911
16912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016913remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016914{
16915 char_u *server_name;
16916 char_u *keys;
16917 char_u *r = NULL;
16918 char_u buf[NUMBUFLEN];
16919# ifdef WIN32
16920 HWND w;
16921# else
16922 Window w;
16923# endif
16924
16925 if (check_restricted() || check_secure())
16926 return;
16927
16928# ifdef FEAT_X11
16929 if (check_connection() == FAIL)
16930 return;
16931# endif
16932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016933 server_name = get_tv_string_chk(&argvars[0]);
16934 if (server_name == NULL)
16935 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016936 keys = get_tv_string_buf(&argvars[1], buf);
16937# ifdef WIN32
16938 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16939# else
16940 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16941 < 0)
16942# endif
16943 {
16944 if (r != NULL)
16945 EMSG(r); /* sending worked but evaluation failed */
16946 else
16947 EMSG2(_("E241: Unable to send to %s"), server_name);
16948 return;
16949 }
16950
16951 rettv->vval.v_string = r;
16952
16953 if (argvars[2].v_type != VAR_UNKNOWN)
16954 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016955 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016956 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016957 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016958
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016959 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016960 v.di_tv.v_type = VAR_STRING;
16961 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016962 idvar = get_tv_string_chk(&argvars[2]);
16963 if (idvar != NULL)
16964 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016965 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016966 }
16967}
16968#endif
16969
16970/*
16971 * "remote_expr()" function
16972 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016974f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016975{
16976 rettv->v_type = VAR_STRING;
16977 rettv->vval.v_string = NULL;
16978#ifdef FEAT_CLIENTSERVER
16979 remote_common(argvars, rettv, TRUE);
16980#endif
16981}
16982
16983/*
16984 * "remote_foreground()" function
16985 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016987f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016988{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016989#ifdef FEAT_CLIENTSERVER
16990# ifdef WIN32
16991 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016992 {
16993 char_u *server_name = get_tv_string_chk(&argvars[0]);
16994
16995 if (server_name != NULL)
16996 serverForeground(server_name);
16997 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016998# else
16999 /* Send a foreground() expression to the server. */
17000 argvars[1].v_type = VAR_STRING;
17001 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
17002 argvars[2].v_type = VAR_UNKNOWN;
17003 remote_common(argvars, rettv, TRUE);
17004 vim_free(argvars[1].vval.v_string);
17005# endif
17006#endif
17007}
17008
Bram Moolenaar0d660222005-01-07 21:51:51 +000017009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017010f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011{
17012#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017013 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014 char_u *s = NULL;
17015# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017016 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017017# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017018 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019
17020 if (check_restricted() || check_secure())
17021 {
17022 rettv->vval.v_number = -1;
17023 return;
17024 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017025 serverid = get_tv_string_chk(&argvars[0]);
17026 if (serverid == NULL)
17027 {
17028 rettv->vval.v_number = -1;
17029 return; /* type error; errmsg already given */
17030 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017031# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017032 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017033 if (n == 0)
17034 rettv->vval.v_number = -1;
17035 else
17036 {
17037 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17038 rettv->vval.v_number = (s != NULL);
17039 }
17040# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017041 if (check_connection() == FAIL)
17042 return;
17043
17044 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017045 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017046# endif
17047
17048 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17049 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017050 char_u *retvar;
17051
Bram Moolenaar33570922005-01-25 22:26:29 +000017052 v.di_tv.v_type = VAR_STRING;
17053 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017054 retvar = get_tv_string_chk(&argvars[1]);
17055 if (retvar != NULL)
17056 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017057 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017058 }
17059#else
17060 rettv->vval.v_number = -1;
17061#endif
17062}
17063
Bram Moolenaar0d660222005-01-07 21:51:51 +000017064 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017065f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017066{
17067 char_u *r = NULL;
17068
17069#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017070 char_u *serverid = get_tv_string_chk(&argvars[0]);
17071
17072 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017073 {
17074# ifdef WIN32
17075 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017076 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017077
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017078 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017079 if (n != 0)
17080 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17081 if (r == NULL)
17082# else
17083 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017084 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085# endif
17086 EMSG(_("E277: Unable to read a server reply"));
17087 }
17088#endif
17089 rettv->v_type = VAR_STRING;
17090 rettv->vval.v_string = r;
17091}
17092
17093/*
17094 * "remote_send()" function
17095 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017097f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098{
17099 rettv->v_type = VAR_STRING;
17100 rettv->vval.v_string = NULL;
17101#ifdef FEAT_CLIENTSERVER
17102 remote_common(argvars, rettv, FALSE);
17103#endif
17104}
17105
17106/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017107 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108 */
17109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017110f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017111{
Bram Moolenaar33570922005-01-25 22:26:29 +000017112 list_T *l;
17113 listitem_T *item, *item2;
17114 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017115 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017116 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017117 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017118 dict_T *d;
17119 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017120 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017121
Bram Moolenaar8c711452005-01-14 21:53:12 +000017122 if (argvars[0].v_type == VAR_DICT)
17123 {
17124 if (argvars[2].v_type != VAR_UNKNOWN)
17125 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017126 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017127 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017129 key = get_tv_string_chk(&argvars[1]);
17130 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017131 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017132 di = dict_find(d, key, -1);
17133 if (di == NULL)
17134 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017135 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17136 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017137 {
17138 *rettv = di->di_tv;
17139 init_tv(&di->di_tv);
17140 dictitem_remove(d, di);
17141 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017142 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017143 }
17144 }
17145 else if (argvars[0].v_type != VAR_LIST)
17146 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017147 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017148 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017149 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017150 int error = FALSE;
17151
17152 idx = get_tv_number_chk(&argvars[1], &error);
17153 if (error)
17154 ; /* type error: do nothing, errmsg already given */
17155 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017156 EMSGN(_(e_listidx), idx);
17157 else
17158 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017159 if (argvars[2].v_type == VAR_UNKNOWN)
17160 {
17161 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017162 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017163 *rettv = item->li_tv;
17164 vim_free(item);
17165 }
17166 else
17167 {
17168 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017169 end = get_tv_number_chk(&argvars[2], &error);
17170 if (error)
17171 ; /* type error: do nothing */
17172 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017173 EMSGN(_(e_listidx), end);
17174 else
17175 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017176 int cnt = 0;
17177
17178 for (li = item; li != NULL; li = li->li_next)
17179 {
17180 ++cnt;
17181 if (li == item2)
17182 break;
17183 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017184 if (li == NULL) /* didn't find "item2" after "item" */
17185 EMSG(_(e_invrange));
17186 else
17187 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017188 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017189 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017190 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017191 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017192 l->lv_first = item;
17193 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017194 item->li_prev = NULL;
17195 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017196 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017197 }
17198 }
17199 }
17200 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017201 }
17202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017203}
17204
17205/*
17206 * "rename({from}, {to})" function
17207 */
17208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017209f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017210{
17211 char_u buf[NUMBUFLEN];
17212
17213 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017214 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017216 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17217 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017218}
17219
17220/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017221 * "repeat()" function
17222 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017224f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017225{
17226 char_u *p;
17227 int n;
17228 int slen;
17229 int len;
17230 char_u *r;
17231 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017232
17233 n = get_tv_number(&argvars[1]);
17234 if (argvars[0].v_type == VAR_LIST)
17235 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017236 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017237 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017238 if (list_extend(rettv->vval.v_list,
17239 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017240 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017241 }
17242 else
17243 {
17244 p = get_tv_string(&argvars[0]);
17245 rettv->v_type = VAR_STRING;
17246 rettv->vval.v_string = NULL;
17247
17248 slen = (int)STRLEN(p);
17249 len = slen * n;
17250 if (len <= 0)
17251 return;
17252
17253 r = alloc(len + 1);
17254 if (r != NULL)
17255 {
17256 for (i = 0; i < n; i++)
17257 mch_memmove(r + i * slen, p, (size_t)slen);
17258 r[len] = NUL;
17259 }
17260
17261 rettv->vval.v_string = r;
17262 }
17263}
17264
17265/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017266 * "resolve()" function
17267 */
17268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017269f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017270{
17271 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017272#ifdef HAVE_READLINK
17273 char_u *buf = NULL;
17274#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017276 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017277#ifdef FEAT_SHORTCUT
17278 {
17279 char_u *v = NULL;
17280
17281 v = mch_resolve_shortcut(p);
17282 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017283 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017284 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017285 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017286 }
17287#else
17288# ifdef HAVE_READLINK
17289 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017290 char_u *cpy;
17291 int len;
17292 char_u *remain = NULL;
17293 char_u *q;
17294 int is_relative_to_current = FALSE;
17295 int has_trailing_pathsep = FALSE;
17296 int limit = 100;
17297
17298 p = vim_strsave(p);
17299
17300 if (p[0] == '.' && (vim_ispathsep(p[1])
17301 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17302 is_relative_to_current = TRUE;
17303
17304 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017305 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017306 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017308 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17309 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017310
17311 q = getnextcomp(p);
17312 if (*q != NUL)
17313 {
17314 /* Separate the first path component in "p", and keep the
17315 * remainder (beginning with the path separator). */
17316 remain = vim_strsave(q - 1);
17317 q[-1] = NUL;
17318 }
17319
Bram Moolenaard9462e32011-04-11 21:35:11 +020017320 buf = alloc(MAXPATHL + 1);
17321 if (buf == NULL)
17322 goto fail;
17323
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324 for (;;)
17325 {
17326 for (;;)
17327 {
17328 len = readlink((char *)p, (char *)buf, MAXPATHL);
17329 if (len <= 0)
17330 break;
17331 buf[len] = NUL;
17332
17333 if (limit-- == 0)
17334 {
17335 vim_free(p);
17336 vim_free(remain);
17337 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017338 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339 goto fail;
17340 }
17341
17342 /* Ensure that the result will have a trailing path separator
17343 * if the argument has one. */
17344 if (remain == NULL && has_trailing_pathsep)
17345 add_pathsep(buf);
17346
17347 /* Separate the first path component in the link value and
17348 * concatenate the remainders. */
17349 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17350 if (*q != NUL)
17351 {
17352 if (remain == NULL)
17353 remain = vim_strsave(q - 1);
17354 else
17355 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017356 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017357 if (cpy != NULL)
17358 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017359 vim_free(remain);
17360 remain = cpy;
17361 }
17362 }
17363 q[-1] = NUL;
17364 }
17365
17366 q = gettail(p);
17367 if (q > p && *q == NUL)
17368 {
17369 /* Ignore trailing path separator. */
17370 q[-1] = NUL;
17371 q = gettail(p);
17372 }
17373 if (q > p && !mch_isFullName(buf))
17374 {
17375 /* symlink is relative to directory of argument */
17376 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17377 if (cpy != NULL)
17378 {
17379 STRCPY(cpy, p);
17380 STRCPY(gettail(cpy), buf);
17381 vim_free(p);
17382 p = cpy;
17383 }
17384 }
17385 else
17386 {
17387 vim_free(p);
17388 p = vim_strsave(buf);
17389 }
17390 }
17391
17392 if (remain == NULL)
17393 break;
17394
17395 /* Append the first path component of "remain" to "p". */
17396 q = getnextcomp(remain + 1);
17397 len = q - remain - (*q != NUL);
17398 cpy = vim_strnsave(p, STRLEN(p) + len);
17399 if (cpy != NULL)
17400 {
17401 STRNCAT(cpy, remain, len);
17402 vim_free(p);
17403 p = cpy;
17404 }
17405 /* Shorten "remain". */
17406 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017407 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017408 else
17409 {
17410 vim_free(remain);
17411 remain = NULL;
17412 }
17413 }
17414
17415 /* If the result is a relative path name, make it explicitly relative to
17416 * the current directory if and only if the argument had this form. */
17417 if (!vim_ispathsep(*p))
17418 {
17419 if (is_relative_to_current
17420 && *p != NUL
17421 && !(p[0] == '.'
17422 && (p[1] == NUL
17423 || vim_ispathsep(p[1])
17424 || (p[1] == '.'
17425 && (p[2] == NUL
17426 || vim_ispathsep(p[2]))))))
17427 {
17428 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017429 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430 if (cpy != NULL)
17431 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432 vim_free(p);
17433 p = cpy;
17434 }
17435 }
17436 else if (!is_relative_to_current)
17437 {
17438 /* Strip leading "./". */
17439 q = p;
17440 while (q[0] == '.' && vim_ispathsep(q[1]))
17441 q += 2;
17442 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017443 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017444 }
17445 }
17446
17447 /* Ensure that the result will have no trailing path separator
17448 * if the argument had none. But keep "/" or "//". */
17449 if (!has_trailing_pathsep)
17450 {
17451 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017452 if (after_pathsep(p, q))
17453 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 }
17455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457 }
17458# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017459 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460# endif
17461#endif
17462
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017463 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017464
17465#ifdef HAVE_READLINK
17466fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017467 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017468#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017469 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017470}
17471
17472/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017473 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474 */
17475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017476f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477{
Bram Moolenaar33570922005-01-25 22:26:29 +000017478 list_T *l;
17479 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017480
Bram Moolenaar0d660222005-01-07 21:51:51 +000017481 if (argvars[0].v_type != VAR_LIST)
17482 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017483 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017484 && !tv_check_lock(l->lv_lock,
17485 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017486 {
17487 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017488 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017489 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017490 while (li != NULL)
17491 {
17492 ni = li->li_prev;
17493 list_append(l, li);
17494 li = ni;
17495 }
17496 rettv->vval.v_list = l;
17497 rettv->v_type = VAR_LIST;
17498 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017499 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017501}
17502
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017503#define SP_NOMOVE 0x01 /* don't move cursor */
17504#define SP_REPEAT 0x02 /* repeat to find outer pair */
17505#define SP_RETCOUNT 0x04 /* return matchcount */
17506#define SP_SETPCMARK 0x08 /* set previous context mark */
17507#define SP_START 0x10 /* accept match at start position */
17508#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17509#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017510#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017511
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017512static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017513
17514/*
17515 * Get flags for a search function.
17516 * Possibly sets "p_ws".
17517 * Returns BACKWARD, FORWARD or zero (for an error).
17518 */
17519 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017520get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017521{
17522 int dir = FORWARD;
17523 char_u *flags;
17524 char_u nbuf[NUMBUFLEN];
17525 int mask;
17526
17527 if (varp->v_type != VAR_UNKNOWN)
17528 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017529 flags = get_tv_string_buf_chk(varp, nbuf);
17530 if (flags == NULL)
17531 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017532 while (*flags != NUL)
17533 {
17534 switch (*flags)
17535 {
17536 case 'b': dir = BACKWARD; break;
17537 case 'w': p_ws = TRUE; break;
17538 case 'W': p_ws = FALSE; break;
17539 default: mask = 0;
17540 if (flagsp != NULL)
17541 switch (*flags)
17542 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017543 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017544 case 'e': mask = SP_END; break;
17545 case 'm': mask = SP_RETCOUNT; break;
17546 case 'n': mask = SP_NOMOVE; break;
17547 case 'p': mask = SP_SUBPAT; break;
17548 case 'r': mask = SP_REPEAT; break;
17549 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017550 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017551 }
17552 if (mask == 0)
17553 {
17554 EMSG2(_(e_invarg2), flags);
17555 dir = 0;
17556 }
17557 else
17558 *flagsp |= mask;
17559 }
17560 if (dir == 0)
17561 break;
17562 ++flags;
17563 }
17564 }
17565 return dir;
17566}
17567
Bram Moolenaar071d4272004-06-13 20:20:40 +000017568/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017569 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017571 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017572search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017574 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 char_u *pat;
17576 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017577 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017578 int save_p_ws = p_ws;
17579 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017580 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017581 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017582 proftime_T tm;
17583#ifdef FEAT_RELTIME
17584 long time_limit = 0;
17585#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017586 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017587 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017589 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017590 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017591 if (dir == 0)
17592 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017593 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017594 if (flags & SP_START)
17595 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017596 if (flags & SP_END)
17597 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017598 if (flags & SP_COLUMN)
17599 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017600
Bram Moolenaar76929292008-01-06 19:07:36 +000017601 /* Optional arguments: line number to stop searching and timeout. */
17602 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017603 {
17604 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17605 if (lnum_stop < 0)
17606 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017607#ifdef FEAT_RELTIME
17608 if (argvars[3].v_type != VAR_UNKNOWN)
17609 {
17610 time_limit = get_tv_number_chk(&argvars[3], NULL);
17611 if (time_limit < 0)
17612 goto theend;
17613 }
17614#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017615 }
17616
Bram Moolenaar76929292008-01-06 19:07:36 +000017617#ifdef FEAT_RELTIME
17618 /* Set the time limit, if there is one. */
17619 profile_setlimit(time_limit, &tm);
17620#endif
17621
Bram Moolenaar231334e2005-07-25 20:46:57 +000017622 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017623 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017624 * Check to make sure only those flags are set.
17625 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17626 * flags cannot be set. Check for that condition also.
17627 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017628 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017629 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017631 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017632 goto theend;
17633 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017634
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017635 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017636 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017637 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017638 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017639 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017640 if (flags & SP_SUBPAT)
17641 retval = subpatnum;
17642 else
17643 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017644 if (flags & SP_SETPCMARK)
17645 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017646 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017647 if (match_pos != NULL)
17648 {
17649 /* Store the match cursor position */
17650 match_pos->lnum = pos.lnum;
17651 match_pos->col = pos.col + 1;
17652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 /* "/$" will put the cursor after the end of the line, may need to
17654 * correct that here */
17655 check_cursor();
17656 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017657
17658 /* If 'n' flag is used: restore cursor position. */
17659 if (flags & SP_NOMOVE)
17660 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017661 else
17662 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017663theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017664 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017665
17666 return retval;
17667}
17668
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017669#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017670
17671/*
17672 * round() is not in C90, use ceil() or floor() instead.
17673 */
17674 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017675vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017676{
17677 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17678}
17679
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017680/*
17681 * "round({float})" function
17682 */
17683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017684f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017685{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017686 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017687
17688 rettv->v_type = VAR_FLOAT;
17689 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017690 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017691 else
17692 rettv->vval.v_float = 0.0;
17693}
17694#endif
17695
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017696/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017697 * "screenattr()" function
17698 */
17699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017700f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017701{
17702 int row;
17703 int col;
17704 int c;
17705
17706 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17707 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17708 if (row < 0 || row >= screen_Rows
17709 || col < 0 || col >= screen_Columns)
17710 c = -1;
17711 else
17712 c = ScreenAttrs[LineOffset[row] + col];
17713 rettv->vval.v_number = c;
17714}
17715
17716/*
17717 * "screenchar()" function
17718 */
17719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017720f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017721{
17722 int row;
17723 int col;
17724 int off;
17725 int c;
17726
17727 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17728 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17729 if (row < 0 || row >= screen_Rows
17730 || col < 0 || col >= screen_Columns)
17731 c = -1;
17732 else
17733 {
17734 off = LineOffset[row] + col;
17735#ifdef FEAT_MBYTE
17736 if (enc_utf8 && ScreenLinesUC[off] != 0)
17737 c = ScreenLinesUC[off];
17738 else
17739#endif
17740 c = ScreenLines[off];
17741 }
17742 rettv->vval.v_number = c;
17743}
17744
17745/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017746 * "screencol()" function
17747 *
17748 * First column is 1 to be consistent with virtcol().
17749 */
17750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017751f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017752{
17753 rettv->vval.v_number = screen_screencol() + 1;
17754}
17755
17756/*
17757 * "screenrow()" function
17758 */
17759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017760f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017761{
17762 rettv->vval.v_number = screen_screenrow() + 1;
17763}
17764
17765/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017766 * "search()" function
17767 */
17768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017769f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017770{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017771 int flags = 0;
17772
17773 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017774}
17775
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017777 * "searchdecl()" function
17778 */
17779 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017780f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017781{
17782 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017783 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017784 int error = FALSE;
17785 char_u *name;
17786
17787 rettv->vval.v_number = 1; /* default: FAIL */
17788
17789 name = get_tv_string_chk(&argvars[0]);
17790 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017791 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017792 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017793 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17794 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17795 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017796 if (!error && name != NULL)
17797 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017798 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017799}
17800
17801/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017802 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017805searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806{
17807 char_u *spat, *mpat, *epat;
17808 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017810 int dir;
17811 int flags = 0;
17812 char_u nbuf1[NUMBUFLEN];
17813 char_u nbuf2[NUMBUFLEN];
17814 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017815 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017816 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017817 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818
Bram Moolenaar071d4272004-06-13 20:20:40 +000017819 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017820 spat = get_tv_string_chk(&argvars[0]);
17821 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17822 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17823 if (spat == NULL || mpat == NULL || epat == NULL)
17824 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017825
Bram Moolenaar071d4272004-06-13 20:20:40 +000017826 /* Handle the optional fourth argument: flags */
17827 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017828 if (dir == 0)
17829 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017830
17831 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017832 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17833 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017834 if ((flags & (SP_END | SP_SUBPAT)) != 0
17835 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017836 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017837 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017838 goto theend;
17839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017841 /* Using 'r' implies 'W', otherwise it doesn't work. */
17842 if (flags & SP_REPEAT)
17843 p_ws = FALSE;
17844
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017845 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017846 if (argvars[3].v_type == VAR_UNKNOWN
17847 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017848 skip = (char_u *)"";
17849 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017850 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017851 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017852 if (argvars[5].v_type != VAR_UNKNOWN)
17853 {
17854 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17855 if (lnum_stop < 0)
17856 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017857#ifdef FEAT_RELTIME
17858 if (argvars[6].v_type != VAR_UNKNOWN)
17859 {
17860 time_limit = get_tv_number_chk(&argvars[6], NULL);
17861 if (time_limit < 0)
17862 goto theend;
17863 }
17864#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017865 }
17866 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017867 if (skip == NULL)
17868 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017870 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017871 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017872
17873theend:
17874 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017875
17876 return retval;
17877}
17878
17879/*
17880 * "searchpair()" function
17881 */
17882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017883f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017884{
17885 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17886}
17887
17888/*
17889 * "searchpairpos()" function
17890 */
17891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017892f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017893{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017894 pos_T match_pos;
17895 int lnum = 0;
17896 int col = 0;
17897
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017898 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017899 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017900
17901 if (searchpair_cmn(argvars, &match_pos) > 0)
17902 {
17903 lnum = match_pos.lnum;
17904 col = match_pos.col;
17905 }
17906
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017907 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17908 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017909}
17910
17911/*
17912 * Search for a start/middle/end thing.
17913 * Used by searchpair(), see its documentation for the details.
17914 * Returns 0 or -1 for no match,
17915 */
17916 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017917do_searchpair(
17918 char_u *spat, /* start pattern */
17919 char_u *mpat, /* middle pattern */
17920 char_u *epat, /* end pattern */
17921 int dir, /* BACKWARD or FORWARD */
17922 char_u *skip, /* skip expression */
17923 int flags, /* SP_SETPCMARK and other SP_ values */
17924 pos_T *match_pos,
17925 linenr_T lnum_stop, /* stop at this line if not zero */
17926 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017927{
17928 char_u *save_cpo;
17929 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17930 long retval = 0;
17931 pos_T pos;
17932 pos_T firstpos;
17933 pos_T foundpos;
17934 pos_T save_cursor;
17935 pos_T save_pos;
17936 int n;
17937 int r;
17938 int nest = 1;
17939 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017940 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017941 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017942
17943 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17944 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017945 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017946
Bram Moolenaar76929292008-01-06 19:07:36 +000017947#ifdef FEAT_RELTIME
17948 /* Set the time limit, if there is one. */
17949 profile_setlimit(time_limit, &tm);
17950#endif
17951
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017952 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17953 * start/middle/end (pat3, for the top pair). */
17954 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17955 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17956 if (pat2 == NULL || pat3 == NULL)
17957 goto theend;
17958 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17959 if (*mpat == NUL)
17960 STRCPY(pat3, pat2);
17961 else
17962 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17963 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017964 if (flags & SP_START)
17965 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017966
Bram Moolenaar071d4272004-06-13 20:20:40 +000017967 save_cursor = curwin->w_cursor;
17968 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017969 clearpos(&firstpos);
17970 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017971 pat = pat3;
17972 for (;;)
17973 {
17974 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017975 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17977 /* didn't find it or found the first match again: FAIL */
17978 break;
17979
17980 if (firstpos.lnum == 0)
17981 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017982 if (equalpos(pos, foundpos))
17983 {
17984 /* Found the same position again. Can happen with a pattern that
17985 * has "\zs" at the end and searching backwards. Advance one
17986 * character and try again. */
17987 if (dir == BACKWARD)
17988 decl(&pos);
17989 else
17990 incl(&pos);
17991 }
17992 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017993
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017994 /* clear the start flag to avoid getting stuck here */
17995 options &= ~SEARCH_START;
17996
Bram Moolenaar071d4272004-06-13 20:20:40 +000017997 /* If the skip pattern matches, ignore this match. */
17998 if (*skip != NUL)
17999 {
18000 save_pos = curwin->w_cursor;
18001 curwin->w_cursor = pos;
18002 r = eval_to_bool(skip, &err, NULL, FALSE);
18003 curwin->w_cursor = save_pos;
18004 if (err)
18005 {
18006 /* Evaluating {skip} caused an error, break here. */
18007 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018008 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018009 break;
18010 }
18011 if (r)
18012 continue;
18013 }
18014
18015 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18016 {
18017 /* Found end when searching backwards or start when searching
18018 * forward: nested pair. */
18019 ++nest;
18020 pat = pat2; /* nested, don't search for middle */
18021 }
18022 else
18023 {
18024 /* Found end when searching forward or start when searching
18025 * backward: end of (nested) pair; or found middle in outer pair. */
18026 if (--nest == 1)
18027 pat = pat3; /* outer level, search for middle */
18028 }
18029
18030 if (nest == 0)
18031 {
18032 /* Found the match: return matchcount or line number. */
18033 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018034 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018035 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018036 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018037 if (flags & SP_SETPCMARK)
18038 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018039 curwin->w_cursor = pos;
18040 if (!(flags & SP_REPEAT))
18041 break;
18042 nest = 1; /* search for next unmatched */
18043 }
18044 }
18045
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018046 if (match_pos != NULL)
18047 {
18048 /* Store the match cursor position */
18049 match_pos->lnum = curwin->w_cursor.lnum;
18050 match_pos->col = curwin->w_cursor.col + 1;
18051 }
18052
Bram Moolenaar071d4272004-06-13 20:20:40 +000018053 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018054 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018055 curwin->w_cursor = save_cursor;
18056
18057theend:
18058 vim_free(pat2);
18059 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018060 if (p_cpo == empty_option)
18061 p_cpo = save_cpo;
18062 else
18063 /* Darn, evaluating the {skip} expression changed the value. */
18064 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018065
18066 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018067}
18068
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018069/*
18070 * "searchpos()" function
18071 */
18072 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018073f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018074{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018075 pos_T match_pos;
18076 int lnum = 0;
18077 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018078 int n;
18079 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018080
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018081 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018082 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018083
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018084 n = search_cmn(argvars, &match_pos, &flags);
18085 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018086 {
18087 lnum = match_pos.lnum;
18088 col = match_pos.col;
18089 }
18090
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018091 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18092 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018093 if (flags & SP_SUBPAT)
18094 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018095}
18096
Bram Moolenaar0d660222005-01-07 21:51:51 +000018097 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018098f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018099{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018100#ifdef FEAT_CLIENTSERVER
18101 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018102 char_u *server = get_tv_string_chk(&argvars[0]);
18103 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018104
Bram Moolenaar0d660222005-01-07 21:51:51 +000018105 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018106 if (server == NULL || reply == NULL)
18107 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018108 if (check_restricted() || check_secure())
18109 return;
18110# ifdef FEAT_X11
18111 if (check_connection() == FAIL)
18112 return;
18113# endif
18114
18115 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 EMSG(_("E258: Unable to send to client"));
18118 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018120 rettv->vval.v_number = 0;
18121#else
18122 rettv->vval.v_number = -1;
18123#endif
18124}
18125
Bram Moolenaar0d660222005-01-07 21:51:51 +000018126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018127f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018128{
18129 char_u *r = NULL;
18130
18131#ifdef FEAT_CLIENTSERVER
18132# ifdef WIN32
18133 r = serverGetVimNames();
18134# else
18135 make_connection();
18136 if (X_DISPLAY != NULL)
18137 r = serverGetVimNames(X_DISPLAY);
18138# endif
18139#endif
18140 rettv->v_type = VAR_STRING;
18141 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018142}
18143
18144/*
18145 * "setbufvar()" function
18146 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018147 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018148f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149{
18150 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018152 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018153 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018154 char_u nbuf[NUMBUFLEN];
18155
18156 if (check_restricted() || check_secure())
18157 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018158 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18159 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018160 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018161 varp = &argvars[2];
18162
18163 if (buf != NULL && varname != NULL && varp != NULL)
18164 {
18165 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018166 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018167
18168 if (*varname == '&')
18169 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018170 long numval;
18171 char_u *strval;
18172 int error = FALSE;
18173
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018175 numval = get_tv_number_chk(varp, &error);
18176 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018177 if (!error && strval != NULL)
18178 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018179 }
18180 else
18181 {
18182 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18183 if (bufvarname != NULL)
18184 {
18185 STRCPY(bufvarname, "b:");
18186 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018187 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018188 vim_free(bufvarname);
18189 }
18190 }
18191
18192 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018193 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018195}
18196
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018198f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018199{
18200 dict_T *d;
18201 dictitem_T *di;
18202 char_u *csearch;
18203
18204 if (argvars[0].v_type != VAR_DICT)
18205 {
18206 EMSG(_(e_dictreq));
18207 return;
18208 }
18209
18210 if ((d = argvars[0].vval.v_dict) != NULL)
18211 {
18212 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18213 if (csearch != NULL)
18214 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018215#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018216 if (enc_utf8)
18217 {
18218 int pcc[MAX_MCO];
18219 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018220
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018221 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18222 }
18223 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018224#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018225 set_last_csearch(PTR2CHAR(csearch),
18226 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018227 }
18228
18229 di = dict_find(d, (char_u *)"forward", -1);
18230 if (di != NULL)
18231 set_csearch_direction(get_tv_number(&di->di_tv)
18232 ? FORWARD : BACKWARD);
18233
18234 di = dict_find(d, (char_u *)"until", -1);
18235 if (di != NULL)
18236 set_csearch_until(!!get_tv_number(&di->di_tv));
18237 }
18238}
18239
Bram Moolenaar071d4272004-06-13 20:20:40 +000018240/*
18241 * "setcmdpos()" function
18242 */
18243 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018244f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018245{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018246 int pos = (int)get_tv_number(&argvars[0]) - 1;
18247
18248 if (pos >= 0)
18249 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250}
18251
18252/*
18253 * "setline()" function
18254 */
18255 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018256f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018257{
18258 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018259 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018260 list_T *l = NULL;
18261 listitem_T *li = NULL;
18262 long added = 0;
18263 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018265 lnum = get_tv_lnum(&argvars[0]);
18266 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268 l = argvars[1].vval.v_list;
18269 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018270 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018271 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018273
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018274 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018275 for (;;)
18276 {
18277 if (l != NULL)
18278 {
18279 /* list argument, get next string */
18280 if (li == NULL)
18281 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018282 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018283 li = li->li_next;
18284 }
18285
18286 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018287 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018288 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018289
18290 /* When coming here from Insert mode, sync undo, so that this can be
18291 * undone separately from what was previously inserted. */
18292 if (u_sync_once == 2)
18293 {
18294 u_sync_once = 1; /* notify that u_sync() was called */
18295 u_sync(TRUE);
18296 }
18297
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018298 if (lnum <= curbuf->b_ml.ml_line_count)
18299 {
18300 /* existing line, replace it */
18301 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18302 {
18303 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018304 if (lnum == curwin->w_cursor.lnum)
18305 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018306 rettv->vval.v_number = 0; /* OK */
18307 }
18308 }
18309 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18310 {
18311 /* lnum is one past the last line, append the line */
18312 ++added;
18313 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18314 rettv->vval.v_number = 0; /* OK */
18315 }
18316
18317 if (l == NULL) /* only one string argument */
18318 break;
18319 ++lnum;
18320 }
18321
18322 if (added > 0)
18323 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324}
18325
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018326static 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 +000018327
Bram Moolenaar071d4272004-06-13 20:20:40 +000018328/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018329 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018330 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018332set_qf_ll_list(
18333 win_T *wp UNUSED,
18334 typval_T *list_arg UNUSED,
18335 typval_T *action_arg UNUSED,
18336 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018337{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018338#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018339 char_u *act;
18340 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018341#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018342
Bram Moolenaar2641f772005-03-25 21:58:17 +000018343 rettv->vval.v_number = -1;
18344
18345#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018346 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018347 EMSG(_(e_listreq));
18348 else
18349 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018350 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018351
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018352 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018353 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018354 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018355 if (act == NULL)
18356 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018357 if (*act == 'a' || *act == 'r')
18358 action = *act;
18359 }
18360
Bram Moolenaar81484f42012-12-05 15:16:47 +010018361 if (l != NULL && set_errorlist(wp, l, action,
18362 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018363 rettv->vval.v_number = 0;
18364 }
18365#endif
18366}
18367
18368/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018369 * "setloclist()" function
18370 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018372f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018373{
18374 win_T *win;
18375
18376 rettv->vval.v_number = -1;
18377
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018378 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018379 if (win != NULL)
18380 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18381}
18382
18383/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018384 * "setmatches()" function
18385 */
18386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018387f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018388{
18389#ifdef FEAT_SEARCH_EXTRA
18390 list_T *l;
18391 listitem_T *li;
18392 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018393 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018394
18395 rettv->vval.v_number = -1;
18396 if (argvars[0].v_type != VAR_LIST)
18397 {
18398 EMSG(_(e_listreq));
18399 return;
18400 }
18401 if ((l = argvars[0].vval.v_list) != NULL)
18402 {
18403
18404 /* To some extent make sure that we are dealing with a list from
18405 * "getmatches()". */
18406 li = l->lv_first;
18407 while (li != NULL)
18408 {
18409 if (li->li_tv.v_type != VAR_DICT
18410 || (d = li->li_tv.vval.v_dict) == NULL)
18411 {
18412 EMSG(_(e_invarg));
18413 return;
18414 }
18415 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018416 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18417 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018418 && dict_find(d, (char_u *)"priority", -1) != NULL
18419 && dict_find(d, (char_u *)"id", -1) != NULL))
18420 {
18421 EMSG(_(e_invarg));
18422 return;
18423 }
18424 li = li->li_next;
18425 }
18426
18427 clear_matches(curwin);
18428 li = l->lv_first;
18429 while (li != NULL)
18430 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018431 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018432 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018433 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018434 char_u *group;
18435 int priority;
18436 int id;
18437 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018438
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018439 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018440 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18441 {
18442 if (s == NULL)
18443 {
18444 s = list_alloc();
18445 if (s == NULL)
18446 return;
18447 }
18448
18449 /* match from matchaddpos() */
18450 for (i = 1; i < 9; i++)
18451 {
18452 sprintf((char *)buf, (char *)"pos%d", i);
18453 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18454 {
18455 if (di->di_tv.v_type != VAR_LIST)
18456 return;
18457
18458 list_append_tv(s, &di->di_tv);
18459 s->lv_refcount++;
18460 }
18461 else
18462 break;
18463 }
18464 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018465
18466 group = get_dict_string(d, (char_u *)"group", FALSE);
18467 priority = (int)get_dict_number(d, (char_u *)"priority");
18468 id = (int)get_dict_number(d, (char_u *)"id");
18469 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18470 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18471 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018472 if (i == 0)
18473 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018474 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018475 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018476 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018477 }
18478 else
18479 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018480 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018481 list_unref(s);
18482 s = NULL;
18483 }
18484
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018485 li = li->li_next;
18486 }
18487 rettv->vval.v_number = 0;
18488 }
18489#endif
18490}
18491
18492/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018493 * "setpos()" function
18494 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018496f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018497{
18498 pos_T pos;
18499 int fnum;
18500 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018501 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018502
Bram Moolenaar08250432008-02-13 11:42:46 +000018503 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018504 name = get_tv_string_chk(argvars);
18505 if (name != NULL)
18506 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018507 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018508 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018509 if (--pos.col < 0)
18510 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018511 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018512 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018513 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018514 if (fnum == curbuf->b_fnum)
18515 {
18516 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018517 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018518 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018519 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018520 curwin->w_set_curswant = FALSE;
18521 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018522 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018523 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018524 }
18525 else
18526 EMSG(_(e_invarg));
18527 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018528 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18529 {
18530 /* set mark */
18531 if (setmark_pos(name[1], &pos, fnum) == OK)
18532 rettv->vval.v_number = 0;
18533 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018534 else
18535 EMSG(_(e_invarg));
18536 }
18537 }
18538}
18539
18540/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018541 * "setqflist()" function
18542 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018543 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018544f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018545{
18546 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18547}
18548
18549/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550 * "setreg()" function
18551 */
18552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018553f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018554{
18555 int regname;
18556 char_u *strregname;
18557 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018558 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018559 int append;
18560 char_u yank_type;
18561 long block_len;
18562
18563 block_len = -1;
18564 yank_type = MAUTO;
18565 append = FALSE;
18566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018567 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018568 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018569
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018570 if (strregname == NULL)
18571 return; /* type error; errmsg already given */
18572 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018573 if (regname == 0 || regname == '@')
18574 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018575
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018576 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018577 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018578 stropt = get_tv_string_chk(&argvars[2]);
18579 if (stropt == NULL)
18580 return; /* type error */
18581 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582 switch (*stropt)
18583 {
18584 case 'a': case 'A': /* append */
18585 append = TRUE;
18586 break;
18587 case 'v': case 'c': /* character-wise selection */
18588 yank_type = MCHAR;
18589 break;
18590 case 'V': case 'l': /* line-wise selection */
18591 yank_type = MLINE;
18592 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018593 case 'b': case Ctrl_V: /* block-wise selection */
18594 yank_type = MBLOCK;
18595 if (VIM_ISDIGIT(stropt[1]))
18596 {
18597 ++stropt;
18598 block_len = getdigits(&stropt) - 1;
18599 --stropt;
18600 }
18601 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018602 }
18603 }
18604
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018605 if (argvars[1].v_type == VAR_LIST)
18606 {
18607 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018608 char_u **allocval;
18609 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018610 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018611 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018612 int len = argvars[1].vval.v_list->lv_len;
18613 listitem_T *li;
18614
Bram Moolenaar7d647822014-04-05 21:28:56 +020018615 /* First half: use for pointers to result lines; second half: use for
18616 * pointers to allocated copies. */
18617 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018618 if (lstval == NULL)
18619 return;
18620 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018621 allocval = lstval + len + 2;
18622 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018623
18624 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18625 li = li->li_next)
18626 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018627 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018628 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018629 goto free_lstval;
18630 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018631 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018632 /* Need to make a copy, next get_tv_string_buf_chk() will
18633 * overwrite the string. */
18634 strval = vim_strsave(buf);
18635 if (strval == NULL)
18636 goto free_lstval;
18637 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018638 }
18639 *curval++ = strval;
18640 }
18641 *curval++ = NULL;
18642
18643 write_reg_contents_lst(regname, lstval, -1,
18644 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018645free_lstval:
18646 while (curallocval > allocval)
18647 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018648 vim_free(lstval);
18649 }
18650 else
18651 {
18652 strval = get_tv_string_chk(&argvars[1]);
18653 if (strval == NULL)
18654 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018655 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018657 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018658 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659}
18660
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018661/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018662 * "settabvar()" function
18663 */
18664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018665f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018666{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018667#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018668 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018669 tabpage_T *tp;
18670#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018671 char_u *varname, *tabvarname;
18672 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018673
18674 rettv->vval.v_number = 0;
18675
18676 if (check_restricted() || check_secure())
18677 return;
18678
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018679#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018680 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018681#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018682 varname = get_tv_string_chk(&argvars[1]);
18683 varp = &argvars[2];
18684
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018685 if (varname != NULL && varp != NULL
18686#ifdef FEAT_WINDOWS
18687 && tp != NULL
18688#endif
18689 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018690 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018691#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018692 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018693 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018694#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018695
18696 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18697 if (tabvarname != NULL)
18698 {
18699 STRCPY(tabvarname, "t:");
18700 STRCPY(tabvarname + 2, varname);
18701 set_var(tabvarname, varp, TRUE);
18702 vim_free(tabvarname);
18703 }
18704
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018705#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018706 /* Restore current tabpage */
18707 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018708 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018709#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018710 }
18711}
18712
18713/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018714 * "settabwinvar()" function
18715 */
18716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018717f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018718{
18719 setwinvar(argvars, rettv, 1);
18720}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721
18722/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018723 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018726f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018727{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018728 setwinvar(argvars, rettv, 0);
18729}
18730
18731/*
18732 * "setwinvar()" and "settabwinvar()" functions
18733 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018734
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018736setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018737{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018738 win_T *win;
18739#ifdef FEAT_WINDOWS
18740 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018741 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018742 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743#endif
18744 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018745 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018746 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018747 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018748
18749 if (check_restricted() || check_secure())
18750 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018751
18752#ifdef FEAT_WINDOWS
18753 if (off == 1)
18754 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18755 else
18756 tp = curtab;
18757#endif
18758 win = find_win_by_nr(&argvars[off], tp);
18759 varname = get_tv_string_chk(&argvars[off + 1]);
18760 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018761
18762 if (win != NULL && varname != NULL && varp != NULL)
18763 {
18764#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018765 need_switch_win = !(tp == curtab && win == curwin);
18766 if (!need_switch_win
18767 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018770 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018772 long numval;
18773 char_u *strval;
18774 int error = FALSE;
18775
18776 ++varname;
18777 numval = get_tv_number_chk(varp, &error);
18778 strval = get_tv_string_buf_chk(varp, nbuf);
18779 if (!error && strval != NULL)
18780 set_option_value(varname, numval, strval, OPT_LOCAL);
18781 }
18782 else
18783 {
18784 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18785 if (winvarname != NULL)
18786 {
18787 STRCPY(winvarname, "w:");
18788 STRCPY(winvarname + 2, varname);
18789 set_var(winvarname, varp, TRUE);
18790 vim_free(winvarname);
18791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018792 }
18793 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018795 if (need_switch_win)
18796 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797#endif
18798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018799}
18800
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018801#ifdef FEAT_CRYPT
18802/*
18803 * "sha256({string})" function
18804 */
18805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018806f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018807{
18808 char_u *p;
18809
18810 p = get_tv_string(&argvars[0]);
18811 rettv->vval.v_string = vim_strsave(
18812 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18813 rettv->v_type = VAR_STRING;
18814}
18815#endif /* FEAT_CRYPT */
18816
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018818 * "shellescape({string})" function
18819 */
18820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018821f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018822{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018823 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018824 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018825 rettv->v_type = VAR_STRING;
18826}
18827
18828/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018829 * shiftwidth() function
18830 */
18831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018832f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018833{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018834 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018835}
18836
18837/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018838 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839 */
18840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018841f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018842{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018843 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018844
Bram Moolenaar0d660222005-01-07 21:51:51 +000018845 p = get_tv_string(&argvars[0]);
18846 rettv->vval.v_string = vim_strsave(p);
18847 simplify_filename(rettv->vval.v_string); /* simplify in place */
18848 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018849}
18850
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018851#ifdef FEAT_FLOAT
18852/*
18853 * "sin()" function
18854 */
18855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018856f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018857{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018858 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018859
18860 rettv->v_type = VAR_FLOAT;
18861 if (get_float_arg(argvars, &f) == OK)
18862 rettv->vval.v_float = sin(f);
18863 else
18864 rettv->vval.v_float = 0.0;
18865}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018866
18867/*
18868 * "sinh()" function
18869 */
18870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018871f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018872{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018873 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018874
18875 rettv->v_type = VAR_FLOAT;
18876 if (get_float_arg(argvars, &f) == OK)
18877 rettv->vval.v_float = sinh(f);
18878 else
18879 rettv->vval.v_float = 0.0;
18880}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018881#endif
18882
Bram Moolenaar0d660222005-01-07 21:51:51 +000018883static int
18884#ifdef __BORLANDC__
18885 _RTLENTRYF
18886#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018887 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018888static int
18889#ifdef __BORLANDC__
18890 _RTLENTRYF
18891#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018892 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018893
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018894/* struct used in the array that's given to qsort() */
18895typedef struct
18896{
18897 listitem_T *item;
18898 int idx;
18899} sortItem_T;
18900
Bram Moolenaar0b962472016-02-22 22:51:33 +010018901/* struct storing information about current sort */
18902typedef struct
18903{
18904 int item_compare_ic;
18905 int item_compare_numeric;
18906 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018907#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018908 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018909#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 char_u *item_compare_func;
18911 dict_T *item_compare_selfdict;
18912 int item_compare_func_err;
18913 int item_compare_keep_zero;
18914} sortinfo_T;
18915static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018916static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018917#define ITEM_COMPARE_FAIL 999
18918
Bram Moolenaar071d4272004-06-13 20:20:40 +000018919/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018920 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018921 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018922 static int
18923#ifdef __BORLANDC__
18924_RTLENTRYF
18925#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018926item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018927{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018928 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018929 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018930 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018931 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018932 int res;
18933 char_u numbuf1[NUMBUFLEN];
18934 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018935
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018936 si1 = (sortItem_T *)s1;
18937 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018938 tv1 = &si1->item->li_tv;
18939 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018940
Bram Moolenaar0b962472016-02-22 22:51:33 +010018941 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018942 {
18943 long v1 = get_tv_number(tv1);
18944 long v2 = get_tv_number(tv2);
18945
18946 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18947 }
18948
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018949#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018950 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018951 {
18952 float_T v1 = get_tv_float(tv1);
18953 float_T v2 = get_tv_float(tv2);
18954
18955 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18956 }
18957#endif
18958
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018959 /* tv2string() puts quotes around a string and allocates memory. Don't do
18960 * that for string variables. Use a single quote when comparing with a
18961 * non-string to do what the docs promise. */
18962 if (tv1->v_type == VAR_STRING)
18963 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018964 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018965 p1 = (char_u *)"'";
18966 else
18967 p1 = tv1->vval.v_string;
18968 }
18969 else
18970 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18971 if (tv2->v_type == VAR_STRING)
18972 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018973 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018974 p2 = (char_u *)"'";
18975 else
18976 p2 = tv2->vval.v_string;
18977 }
18978 else
18979 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018980 if (p1 == NULL)
18981 p1 = (char_u *)"";
18982 if (p2 == NULL)
18983 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018984 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018985 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018986 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018987 res = STRICMP(p1, p2);
18988 else
18989 res = STRCMP(p1, p2);
18990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018992 {
18993 double n1, n2;
18994 n1 = strtod((char *)p1, (char **)&p1);
18995 n2 = strtod((char *)p2, (char **)&p2);
18996 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18997 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018998
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018999 /* When the result would be zero, compare the item indexes. Makes the
19000 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019001 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019002 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019003
Bram Moolenaar0d660222005-01-07 21:51:51 +000019004 vim_free(tofree1);
19005 vim_free(tofree2);
19006 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019007}
19008
19009 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019010#ifdef __BORLANDC__
19011_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019012#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019013item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019014{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019015 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019017 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019018 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019019 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019020
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019021 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019022 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019023 return 0;
19024
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019025 si1 = (sortItem_T *)s1;
19026 si2 = (sortItem_T *)s2;
19027
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019028 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019029 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019030 copy_tv(&si1->item->li_tv, &argv[0]);
19031 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019032
19033 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019034 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019035 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019036 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019037 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019038 clear_tv(&argv[0]);
19039 clear_tv(&argv[1]);
19040
19041 if (res == FAIL)
19042 res = ITEM_COMPARE_FAIL;
19043 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019044 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19045 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019046 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019047 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019048
19049 /* When the result would be zero, compare the pointers themselves. Makes
19050 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019051 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019052 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019053
Bram Moolenaar0d660222005-01-07 21:51:51 +000019054 return res;
19055}
19056
19057/*
19058 * "sort({list})" function
19059 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019061do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019062{
Bram Moolenaar33570922005-01-25 22:26:29 +000019063 list_T *l;
19064 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019065 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019066 sortinfo_T *old_sortinfo;
19067 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 long len;
19069 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019070
Bram Moolenaar0b962472016-02-22 22:51:33 +010019071 /* Pointer to current info struct used in compare function. Save and
19072 * restore the current one for nested calls. */
19073 old_sortinfo = sortinfo;
19074 sortinfo = &info;
19075
Bram Moolenaar0d660222005-01-07 21:51:51 +000019076 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019077 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019078 else
19079 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019080 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019081 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019082 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19083 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019084 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019085 rettv->vval.v_list = l;
19086 rettv->v_type = VAR_LIST;
19087 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089 len = list_len(l);
19090 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019091 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092
Bram Moolenaar0b962472016-02-22 22:51:33 +010019093 info.item_compare_ic = FALSE;
19094 info.item_compare_numeric = FALSE;
19095 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019096#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019097 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019098#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019099 info.item_compare_func = NULL;
19100 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 if (argvars[1].v_type != VAR_UNKNOWN)
19102 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019103 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019104 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019105 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019106 else
19107 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019108 int error = FALSE;
19109
19110 i = get_tv_number_chk(&argvars[1], &error);
19111 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019112 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019114 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 info.item_compare_func = get_tv_string(&argvars[1]);
19117 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019118 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019119 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019120 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019121 info.item_compare_func = NULL;
19122 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019123 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019124 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019125 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019126 info.item_compare_func = NULL;
19127 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019128 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019129#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019130 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019131 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019132 info.item_compare_func = NULL;
19133 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019134 }
19135#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019136 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019137 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019138 info.item_compare_func = NULL;
19139 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019140 }
19141 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019142 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019143
19144 if (argvars[2].v_type != VAR_UNKNOWN)
19145 {
19146 /* optional third argument: {dict} */
19147 if (argvars[2].v_type != VAR_DICT)
19148 {
19149 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019150 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019151 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019152 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019153 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019155
Bram Moolenaar0d660222005-01-07 21:51:51 +000019156 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019157 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019158 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019159 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019160
Bram Moolenaar327aa022014-03-25 18:24:23 +010019161 i = 0;
19162 if (sort)
19163 {
19164 /* sort(): ptrs will be the list to sort */
19165 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019166 {
19167 ptrs[i].item = li;
19168 ptrs[i].idx = i;
19169 ++i;
19170 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 info.item_compare_func_err = FALSE;
19173 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019174 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019175 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019176 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019177 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019178 EMSG(_("E702: Sort compare function failed"));
19179 else
19180 {
19181 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019182 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019183 info.item_compare_func == NULL
19184 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019185
Bram Moolenaar0b962472016-02-22 22:51:33 +010019186 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019187 {
19188 /* Clear the List and append the items in sorted order. */
19189 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19190 l->lv_len = 0;
19191 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019192 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019193 }
19194 }
19195 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019196 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019197 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019198 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019199
19200 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019201 info.item_compare_func_err = FALSE;
19202 info.item_compare_keep_zero = TRUE;
19203 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019204 ? item_compare2 : item_compare;
19205
19206 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19207 li = li->li_next)
19208 {
19209 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19210 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019211 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019212 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019213 {
19214 EMSG(_("E882: Uniq compare function failed"));
19215 break;
19216 }
19217 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019218
Bram Moolenaar0b962472016-02-22 22:51:33 +010019219 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019220 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019221 while (--i >= 0)
19222 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019223 li = ptrs[i].item->li_next;
19224 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019225 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019226 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019227 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019228 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019229 list_fix_watch(l, li);
19230 listitem_free(li);
19231 l->lv_len--;
19232 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019233 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019234 }
19235
19236 vim_free(ptrs);
19237 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019238theend:
19239 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019240}
19241
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019242/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019243 * "sort({list})" function
19244 */
19245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019246f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019247{
19248 do_sort_uniq(argvars, rettv, TRUE);
19249}
19250
19251/*
19252 * "uniq({list})" function
19253 */
19254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019255f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019256{
19257 do_sort_uniq(argvars, rettv, FALSE);
19258}
19259
19260/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019261 * "soundfold({word})" function
19262 */
19263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019264f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019265{
19266 char_u *s;
19267
19268 rettv->v_type = VAR_STRING;
19269 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019270#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019271 rettv->vval.v_string = eval_soundfold(s);
19272#else
19273 rettv->vval.v_string = vim_strsave(s);
19274#endif
19275}
19276
19277/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019278 * "spellbadword()" function
19279 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019281f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019282{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019283 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019284 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019285 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019286
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019287 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019288 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019289
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019290#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019291 if (argvars[0].v_type == VAR_UNKNOWN)
19292 {
19293 /* Find the start and length of the badly spelled word. */
19294 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19295 if (len != 0)
19296 word = ml_get_cursor();
19297 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019298 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019299 {
19300 char_u *str = get_tv_string_chk(&argvars[0]);
19301 int capcol = -1;
19302
19303 if (str != NULL)
19304 {
19305 /* Check the argument for spelling. */
19306 while (*str != NUL)
19307 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019308 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019309 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019310 {
19311 word = str;
19312 break;
19313 }
19314 str += len;
19315 }
19316 }
19317 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019318#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019319
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019320 list_append_string(rettv->vval.v_list, word, len);
19321 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019322 attr == HLF_SPB ? "bad" :
19323 attr == HLF_SPR ? "rare" :
19324 attr == HLF_SPL ? "local" :
19325 attr == HLF_SPC ? "caps" :
19326 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019327}
19328
19329/*
19330 * "spellsuggest()" function
19331 */
19332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019333f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019334{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019335#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019336 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019337 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019338 int maxcount;
19339 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019340 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019341 listitem_T *li;
19342 int need_capital = FALSE;
19343#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019345 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019346 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019347
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019348#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019349 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019350 {
19351 str = get_tv_string(&argvars[0]);
19352 if (argvars[1].v_type != VAR_UNKNOWN)
19353 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019354 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019355 if (maxcount <= 0)
19356 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019357 if (argvars[2].v_type != VAR_UNKNOWN)
19358 {
19359 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19360 if (typeerr)
19361 return;
19362 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019363 }
19364 else
19365 maxcount = 25;
19366
Bram Moolenaar4770d092006-01-12 23:22:24 +000019367 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019368
19369 for (i = 0; i < ga.ga_len; ++i)
19370 {
19371 str = ((char_u **)ga.ga_data)[i];
19372
19373 li = listitem_alloc();
19374 if (li == NULL)
19375 vim_free(str);
19376 else
19377 {
19378 li->li_tv.v_type = VAR_STRING;
19379 li->li_tv.v_lock = 0;
19380 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019381 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019382 }
19383 }
19384 ga_clear(&ga);
19385 }
19386#endif
19387}
19388
Bram Moolenaar0d660222005-01-07 21:51:51 +000019389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019390f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019391{
19392 char_u *str;
19393 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019394 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019395 regmatch_T regmatch;
19396 char_u patbuf[NUMBUFLEN];
19397 char_u *save_cpo;
19398 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019400 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019401 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019402
19403 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19404 save_cpo = p_cpo;
19405 p_cpo = (char_u *)"";
19406
19407 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019408 if (argvars[1].v_type != VAR_UNKNOWN)
19409 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019410 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19411 if (pat == NULL)
19412 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019413 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019414 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019415 }
19416 if (pat == NULL || *pat == NUL)
19417 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019419 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019421 if (typeerr)
19422 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19425 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019426 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019428 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019429 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019430 if (*str == NUL)
19431 match = FALSE; /* empty item at the end */
19432 else
19433 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 if (match)
19435 end = regmatch.startp[0];
19436 else
19437 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019438 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19439 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019441 if (list_append_string(rettv->vval.v_list, str,
19442 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019443 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019444 }
19445 if (!match)
19446 break;
19447 /* Advance to just after the match. */
19448 if (regmatch.endp[0] > str)
19449 col = 0;
19450 else
19451 {
19452 /* Don't get stuck at the same match. */
19453#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019454 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019455#else
19456 col = 1;
19457#endif
19458 }
19459 str = regmatch.endp[0];
19460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
Bram Moolenaar473de612013-06-08 18:19:48 +020019462 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464
Bram Moolenaar0d660222005-01-07 21:51:51 +000019465 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466}
19467
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019468#ifdef FEAT_FLOAT
19469/*
19470 * "sqrt()" function
19471 */
19472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019474{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019475 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019476
19477 rettv->v_type = VAR_FLOAT;
19478 if (get_float_arg(argvars, &f) == OK)
19479 rettv->vval.v_float = sqrt(f);
19480 else
19481 rettv->vval.v_float = 0.0;
19482}
19483
19484/*
19485 * "str2float()" function
19486 */
19487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019488f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019489{
19490 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19491
19492 if (*p == '+')
19493 p = skipwhite(p + 1);
19494 (void)string2float(p, &rettv->vval.v_float);
19495 rettv->v_type = VAR_FLOAT;
19496}
19497#endif
19498
Bram Moolenaar2c932302006-03-18 21:42:09 +000019499/*
19500 * "str2nr()" function
19501 */
19502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019503f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019504{
19505 int base = 10;
19506 char_u *p;
19507 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019508 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019509
19510 if (argvars[1].v_type != VAR_UNKNOWN)
19511 {
19512 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019513 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019514 {
19515 EMSG(_(e_invarg));
19516 return;
19517 }
19518 }
19519
19520 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019521 if (*p == '+')
19522 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019523 switch (base)
19524 {
19525 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19526 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19527 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19528 default: what = 0;
19529 }
19530 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019531 rettv->vval.v_number = n;
19532}
19533
Bram Moolenaar071d4272004-06-13 20:20:40 +000019534#ifdef HAVE_STRFTIME
19535/*
19536 * "strftime({format}[, {time}])" function
19537 */
19538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019539f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019540{
19541 char_u result_buf[256];
19542 struct tm *curtime;
19543 time_t seconds;
19544 char_u *p;
19545
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019546 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019549 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 seconds = time(NULL);
19551 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019552 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 curtime = localtime(&seconds);
19554 /* MSVC returns NULL for an invalid value of seconds. */
19555 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019556 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019557 else
19558 {
19559# ifdef FEAT_MBYTE
19560 vimconv_T conv;
19561 char_u *enc;
19562
19563 conv.vc_type = CONV_NONE;
19564 enc = enc_locale();
19565 convert_setup(&conv, p_enc, enc);
19566 if (conv.vc_type != CONV_NONE)
19567 p = string_convert(&conv, p, NULL);
19568# endif
19569 if (p != NULL)
19570 (void)strftime((char *)result_buf, sizeof(result_buf),
19571 (char *)p, curtime);
19572 else
19573 result_buf[0] = NUL;
19574
19575# ifdef FEAT_MBYTE
19576 if (conv.vc_type != CONV_NONE)
19577 vim_free(p);
19578 convert_setup(&conv, enc, p_enc);
19579 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581 else
19582# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019583 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019584
19585# ifdef FEAT_MBYTE
19586 /* Release conversion descriptors */
19587 convert_setup(&conv, NULL, NULL);
19588 vim_free(enc);
19589# endif
19590 }
19591}
19592#endif
19593
19594/*
19595 * "stridx()" function
19596 */
19597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019598f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019599{
19600 char_u buf[NUMBUFLEN];
19601 char_u *needle;
19602 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019603 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019604 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019605 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019606
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019607 needle = get_tv_string_chk(&argvars[1]);
19608 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019609 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019610 if (needle == NULL || haystack == NULL)
19611 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019612
Bram Moolenaar33570922005-01-25 22:26:29 +000019613 if (argvars[2].v_type != VAR_UNKNOWN)
19614 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019615 int error = FALSE;
19616
19617 start_idx = get_tv_number_chk(&argvars[2], &error);
19618 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019620 if (start_idx >= 0)
19621 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019622 }
19623
19624 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19625 if (pos != NULL)
19626 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019627}
19628
19629/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019630 * "string()" function
19631 */
19632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019633f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019634{
19635 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019636 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019638 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019639 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019640 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019641 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019642 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019643}
19644
19645/*
19646 * "strlen()" function
19647 */
19648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019649f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019651 rettv->vval.v_number = (varnumber_T)(STRLEN(
19652 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653}
19654
19655/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019656 * "strchars()" function
19657 */
19658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019659f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019660{
19661 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019662 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019663#ifdef FEAT_MBYTE
19664 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019665 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019666#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019667
19668 if (argvars[1].v_type != VAR_UNKNOWN)
19669 skipcc = get_tv_number_chk(&argvars[1], NULL);
19670 if (skipcc < 0 || skipcc > 1)
19671 EMSG(_(e_invarg));
19672 else
19673 {
19674#ifdef FEAT_MBYTE
19675 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19676 while (*s != NUL)
19677 {
19678 func_mb_ptr2char_adv(&s);
19679 ++len;
19680 }
19681 rettv->vval.v_number = len;
19682#else
19683 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19684#endif
19685 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019686}
19687
19688/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019689 * "strdisplaywidth()" function
19690 */
19691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019692f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019693{
19694 char_u *s = get_tv_string(&argvars[0]);
19695 int col = 0;
19696
19697 if (argvars[1].v_type != VAR_UNKNOWN)
19698 col = get_tv_number(&argvars[1]);
19699
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019700 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019701}
19702
19703/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019704 * "strwidth()" function
19705 */
19706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019707f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019708{
19709 char_u *s = get_tv_string(&argvars[0]);
19710
19711 rettv->vval.v_number = (varnumber_T)(
19712#ifdef FEAT_MBYTE
19713 mb_string2cells(s, -1)
19714#else
19715 STRLEN(s)
19716#endif
19717 );
19718}
19719
19720/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019721 * "strpart()" function
19722 */
19723 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019724f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019725{
19726 char_u *p;
19727 int n;
19728 int len;
19729 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019730 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019731
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019732 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019733 slen = (int)STRLEN(p);
19734
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019735 n = get_tv_number_chk(&argvars[1], &error);
19736 if (error)
19737 len = 0;
19738 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019739 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019740 else
19741 len = slen - n; /* default len: all bytes that are available. */
19742
19743 /*
19744 * Only return the overlap between the specified part and the actual
19745 * string.
19746 */
19747 if (n < 0)
19748 {
19749 len += n;
19750 n = 0;
19751 }
19752 else if (n > slen)
19753 n = slen;
19754 if (len < 0)
19755 len = 0;
19756 else if (n + len > slen)
19757 len = slen - n;
19758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019759 rettv->v_type = VAR_STRING;
19760 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019761}
19762
19763/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019764 * "strridx()" function
19765 */
19766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019767f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019768{
19769 char_u buf[NUMBUFLEN];
19770 char_u *needle;
19771 char_u *haystack;
19772 char_u *rest;
19773 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019774 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019775
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019776 needle = get_tv_string_chk(&argvars[1]);
19777 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019778
19779 rettv->vval.v_number = -1;
19780 if (needle == NULL || haystack == NULL)
19781 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019782
19783 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019784 if (argvars[2].v_type != VAR_UNKNOWN)
19785 {
19786 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019787 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019788 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019789 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019790 }
19791 else
19792 end_idx = haystack_len;
19793
Bram Moolenaar0d660222005-01-07 21:51:51 +000019794 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019795 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019796 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019797 lastmatch = haystack + end_idx;
19798 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019799 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019800 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019801 for (rest = haystack; *rest != '\0'; ++rest)
19802 {
19803 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019804 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019805 break;
19806 lastmatch = rest;
19807 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019808 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019809
19810 if (lastmatch == NULL)
19811 rettv->vval.v_number = -1;
19812 else
19813 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19814}
19815
19816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019817 * "strtrans()" function
19818 */
19819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019820f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019822 rettv->v_type = VAR_STRING;
19823 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019824}
19825
19826/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019827 * "submatch()" function
19828 */
19829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019830f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019831{
Bram Moolenaar41571762014-04-02 19:00:58 +020019832 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019833 int no;
19834 int retList = 0;
19835
19836 no = (int)get_tv_number_chk(&argvars[0], &error);
19837 if (error)
19838 return;
19839 error = FALSE;
19840 if (argvars[1].v_type != VAR_UNKNOWN)
19841 retList = get_tv_number_chk(&argvars[1], &error);
19842 if (error)
19843 return;
19844
19845 if (retList == 0)
19846 {
19847 rettv->v_type = VAR_STRING;
19848 rettv->vval.v_string = reg_submatch(no);
19849 }
19850 else
19851 {
19852 rettv->v_type = VAR_LIST;
19853 rettv->vval.v_list = reg_submatch_list(no);
19854 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019855}
19856
19857/*
19858 * "substitute()" function
19859 */
19860 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019861f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019862{
19863 char_u patbuf[NUMBUFLEN];
19864 char_u subbuf[NUMBUFLEN];
19865 char_u flagsbuf[NUMBUFLEN];
19866
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019867 char_u *str = get_tv_string_chk(&argvars[0]);
19868 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19869 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19870 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19871
Bram Moolenaar0d660222005-01-07 21:51:51 +000019872 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019873 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19874 rettv->vval.v_string = NULL;
19875 else
19876 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019877}
19878
19879/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019880 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019883f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019884{
19885 int id = 0;
19886#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019887 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888 long col;
19889 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019890 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019891
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019892 lnum = get_tv_lnum(argvars); /* -1 on type error */
19893 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19894 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019896 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019897 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019898 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899#endif
19900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019901 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019902}
19903
19904/*
19905 * "synIDattr(id, what [, mode])" function
19906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019908f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019909{
19910 char_u *p = NULL;
19911#ifdef FEAT_SYN_HL
19912 int id;
19913 char_u *what;
19914 char_u *mode;
19915 char_u modebuf[NUMBUFLEN];
19916 int modec;
19917
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019918 id = get_tv_number(&argvars[0]);
19919 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019920 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019922 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019923 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019924 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 modec = 0; /* replace invalid with current */
19926 }
19927 else
19928 {
19929#ifdef FEAT_GUI
19930 if (gui.in_use)
19931 modec = 'g';
19932 else
19933#endif
19934 if (t_colors > 1)
19935 modec = 'c';
19936 else
19937 modec = 't';
19938 }
19939
19940
19941 switch (TOLOWER_ASC(what[0]))
19942 {
19943 case 'b':
19944 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19945 p = highlight_color(id, what, modec);
19946 else /* bold */
19947 p = highlight_has_attr(id, HL_BOLD, modec);
19948 break;
19949
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019950 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019951 p = highlight_color(id, what, modec);
19952 break;
19953
19954 case 'i':
19955 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19956 p = highlight_has_attr(id, HL_INVERSE, modec);
19957 else /* italic */
19958 p = highlight_has_attr(id, HL_ITALIC, modec);
19959 break;
19960
19961 case 'n': /* name */
19962 p = get_highlight_name(NULL, id - 1);
19963 break;
19964
19965 case 'r': /* reverse */
19966 p = highlight_has_attr(id, HL_INVERSE, modec);
19967 break;
19968
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019969 case 's':
19970 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19971 p = highlight_color(id, what, modec);
19972 else /* standout */
19973 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019974 break;
19975
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019976 case 'u':
19977 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19978 /* underline */
19979 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19980 else
19981 /* undercurl */
19982 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019983 break;
19984 }
19985
19986 if (p != NULL)
19987 p = vim_strsave(p);
19988#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019989 rettv->v_type = VAR_STRING;
19990 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019991}
19992
19993/*
19994 * "synIDtrans(id)" function
19995 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019996 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019997f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019998{
19999 int id;
20000
20001#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020002 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020003
20004 if (id > 0)
20005 id = syn_get_final_id(id);
20006 else
20007#endif
20008 id = 0;
20009
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020010 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020011}
20012
20013/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020014 * "synconcealed(lnum, col)" function
20015 */
20016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020017f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020018{
20019#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20020 long lnum;
20021 long col;
20022 int syntax_flags = 0;
20023 int cchar;
20024 int matchid = 0;
20025 char_u str[NUMBUFLEN];
20026#endif
20027
20028 rettv->v_type = VAR_LIST;
20029 rettv->vval.v_list = NULL;
20030
20031#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20032 lnum = get_tv_lnum(argvars); /* -1 on type error */
20033 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20034
20035 vim_memset(str, NUL, sizeof(str));
20036
20037 if (rettv_list_alloc(rettv) != FAIL)
20038 {
20039 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20040 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20041 && curwin->w_p_cole > 0)
20042 {
20043 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20044 syntax_flags = get_syntax_info(&matchid);
20045
20046 /* get the conceal character */
20047 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20048 {
20049 cchar = syn_get_sub_char();
20050 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20051 cchar = lcs_conceal;
20052 if (cchar != NUL)
20053 {
20054# ifdef FEAT_MBYTE
20055 if (has_mbyte)
20056 (*mb_char2bytes)(cchar, str);
20057 else
20058# endif
20059 str[0] = cchar;
20060 }
20061 }
20062 }
20063
20064 list_append_number(rettv->vval.v_list,
20065 (syntax_flags & HL_CONCEAL) != 0);
20066 /* -1 to auto-determine strlen */
20067 list_append_string(rettv->vval.v_list, str, -1);
20068 list_append_number(rettv->vval.v_list, matchid);
20069 }
20070#endif
20071}
20072
20073/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020074 * "synstack(lnum, col)" function
20075 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020077f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020078{
20079#ifdef FEAT_SYN_HL
20080 long lnum;
20081 long col;
20082 int i;
20083 int id;
20084#endif
20085
20086 rettv->v_type = VAR_LIST;
20087 rettv->vval.v_list = NULL;
20088
20089#ifdef FEAT_SYN_HL
20090 lnum = get_tv_lnum(argvars); /* -1 on type error */
20091 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20092
20093 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020094 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020095 && rettv_list_alloc(rettv) != FAIL)
20096 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020097 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020098 for (i = 0; ; ++i)
20099 {
20100 id = syn_get_stack_item(i);
20101 if (id < 0)
20102 break;
20103 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20104 break;
20105 }
20106 }
20107#endif
20108}
20109
Bram Moolenaar071d4272004-06-13 20:20:40 +000020110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020111get_cmd_output_as_rettv(
20112 typval_T *argvars,
20113 typval_T *rettv,
20114 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020115{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020116 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020117 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020118 char_u *infile = NULL;
20119 char_u buf[NUMBUFLEN];
20120 int err = FALSE;
20121 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020122 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020123 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020124
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020125 rettv->v_type = VAR_STRING;
20126 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020127 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020128 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020129
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020130 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020131 {
20132 /*
20133 * Write the string to a temp file, to be used for input of the shell
20134 * command.
20135 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020136 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020137 {
20138 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020139 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020140 }
20141
20142 fd = mch_fopen((char *)infile, WRITEBIN);
20143 if (fd == NULL)
20144 {
20145 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020146 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020147 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020148 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020149 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020150 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20151 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020152 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020153 else
20154 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020155 size_t len;
20156
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020157 p = get_tv_string_buf_chk(&argvars[1], buf);
20158 if (p == NULL)
20159 {
20160 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020161 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020162 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020163 len = STRLEN(p);
20164 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020165 err = TRUE;
20166 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020167 if (fclose(fd) != 0)
20168 err = TRUE;
20169 if (err)
20170 {
20171 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020172 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020173 }
20174 }
20175
Bram Moolenaar52a72462014-08-29 15:53:52 +020020176 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20177 * echoes typeahead, that messes up the display. */
20178 if (!msg_silent)
20179 flags += SHELL_COOKED;
20180
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020181 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020182 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020183 int len;
20184 listitem_T *li;
20185 char_u *s = NULL;
20186 char_u *start;
20187 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020188 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020189
Bram Moolenaar52a72462014-08-29 15:53:52 +020020190 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020191 if (res == NULL)
20192 goto errret;
20193
20194 list = list_alloc();
20195 if (list == NULL)
20196 goto errret;
20197
20198 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020200 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020201 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020202 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020203 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020204
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020205 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020206 if (s == NULL)
20207 goto errret;
20208
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020209 for (p = s; start < end; ++p, ++start)
20210 *p = *start == NUL ? NL : *start;
20211 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020212
20213 li = listitem_alloc();
20214 if (li == NULL)
20215 {
20216 vim_free(s);
20217 goto errret;
20218 }
20219 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020220 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020221 li->li_tv.vval.v_string = s;
20222 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020223 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020224
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020225 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020226 rettv->v_type = VAR_LIST;
20227 rettv->vval.v_list = list;
20228 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020229 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020230 else
20231 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020232 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020233#ifdef USE_CR
20234 /* translate <CR> into <NL> */
20235 if (res != NULL)
20236 {
20237 char_u *s;
20238
20239 for (s = res; *s; ++s)
20240 {
20241 if (*s == CAR)
20242 *s = NL;
20243 }
20244 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020245#else
20246# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020247 /* translate <CR><NL> into <NL> */
20248 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020250 char_u *s, *d;
20251
20252 d = res;
20253 for (s = res; *s; ++s)
20254 {
20255 if (s[0] == CAR && s[1] == NL)
20256 ++s;
20257 *d++ = *s;
20258 }
20259 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020260 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261# endif
20262#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020263 rettv->vval.v_string = res;
20264 res = NULL;
20265 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020266
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020267errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020268 if (infile != NULL)
20269 {
20270 mch_remove(infile);
20271 vim_free(infile);
20272 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020273 if (res != NULL)
20274 vim_free(res);
20275 if (list != NULL)
20276 list_free(list, TRUE);
20277}
20278
20279/*
20280 * "system()" function
20281 */
20282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020283f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020284{
20285 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20286}
20287
20288/*
20289 * "systemlist()" function
20290 */
20291 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020292f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020293{
20294 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020295}
20296
20297/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020298 * "tabpagebuflist()" function
20299 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020300 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020301f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020302{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020303#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020304 tabpage_T *tp;
20305 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020306
20307 if (argvars[0].v_type == VAR_UNKNOWN)
20308 wp = firstwin;
20309 else
20310 {
20311 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20312 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020313 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020314 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020315 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020316 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020317 for (; wp != NULL; wp = wp->w_next)
20318 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020319 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020320 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020321 }
20322#endif
20323}
20324
20325
20326/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020327 * "tabpagenr()" function
20328 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020330f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020331{
20332 int nr = 1;
20333#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020334 char_u *arg;
20335
20336 if (argvars[0].v_type != VAR_UNKNOWN)
20337 {
20338 arg = get_tv_string_chk(&argvars[0]);
20339 nr = 0;
20340 if (arg != NULL)
20341 {
20342 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020343 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020344 else
20345 EMSG2(_(e_invexpr2), arg);
20346 }
20347 }
20348 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020349 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020350#endif
20351 rettv->vval.v_number = nr;
20352}
20353
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020354
20355#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020356static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020357
20358/*
20359 * Common code for tabpagewinnr() and winnr().
20360 */
20361 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020362get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020363{
20364 win_T *twin;
20365 int nr = 1;
20366 win_T *wp;
20367 char_u *arg;
20368
20369 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20370 if (argvar->v_type != VAR_UNKNOWN)
20371 {
20372 arg = get_tv_string_chk(argvar);
20373 if (arg == NULL)
20374 nr = 0; /* type error; errmsg already given */
20375 else if (STRCMP(arg, "$") == 0)
20376 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20377 else if (STRCMP(arg, "#") == 0)
20378 {
20379 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20380 if (twin == NULL)
20381 nr = 0;
20382 }
20383 else
20384 {
20385 EMSG2(_(e_invexpr2), arg);
20386 nr = 0;
20387 }
20388 }
20389
20390 if (nr > 0)
20391 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20392 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020393 {
20394 if (wp == NULL)
20395 {
20396 /* didn't find it in this tabpage */
20397 nr = 0;
20398 break;
20399 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020400 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020401 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020402 return nr;
20403}
20404#endif
20405
20406/*
20407 * "tabpagewinnr()" function
20408 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020410f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020411{
20412 int nr = 1;
20413#ifdef FEAT_WINDOWS
20414 tabpage_T *tp;
20415
20416 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20417 if (tp == NULL)
20418 nr = 0;
20419 else
20420 nr = get_winnr(tp, &argvars[1]);
20421#endif
20422 rettv->vval.v_number = nr;
20423}
20424
20425
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020426/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020427 * "tagfiles()" function
20428 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020430f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020431{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020432 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020433 tagname_T tn;
20434 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020435
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020436 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020437 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020438 fname = alloc(MAXPATHL);
20439 if (fname == NULL)
20440 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020441
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020442 for (first = TRUE; ; first = FALSE)
20443 if (get_tagfname(&tn, first, fname) == FAIL
20444 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020445 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020446 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020447 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020448}
20449
20450/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020451 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020452 */
20453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020454f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020455{
20456 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020457
20458 tag_pattern = get_tv_string(&argvars[0]);
20459
20460 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020461 if (*tag_pattern == NUL)
20462 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020463
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020464 if (rettv_list_alloc(rettv) == OK)
20465 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020466}
20467
20468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020469 * "tempname()" function
20470 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020472f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020473{
20474 static int x = 'A';
20475
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020476 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020477 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020478
20479 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20480 * names. Skip 'I' and 'O', they are used for shell redirection. */
20481 do
20482 {
20483 if (x == 'Z')
20484 x = '0';
20485 else if (x == '9')
20486 x = 'A';
20487 else
20488 {
20489#ifdef EBCDIC
20490 if (x == 'I')
20491 x = 'J';
20492 else if (x == 'R')
20493 x = 'S';
20494 else
20495#endif
20496 ++x;
20497 }
20498 } while (x == 'I' || x == 'O');
20499}
20500
20501/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020502 * "test(list)" function: Just checking the walls...
20503 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020505f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020506{
20507 /* Used for unit testing. Change the code below to your liking. */
20508#if 0
20509 listitem_T *li;
20510 list_T *l;
20511 char_u *bad, *good;
20512
20513 if (argvars[0].v_type != VAR_LIST)
20514 return;
20515 l = argvars[0].vval.v_list;
20516 if (l == NULL)
20517 return;
20518 li = l->lv_first;
20519 if (li == NULL)
20520 return;
20521 bad = get_tv_string(&li->li_tv);
20522 li = li->li_next;
20523 if (li == NULL)
20524 return;
20525 good = get_tv_string(&li->li_tv);
20526 rettv->vval.v_number = test_edit_score(bad, good);
20527#endif
20528}
20529
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020530#ifdef FEAT_FLOAT
20531/*
20532 * "tan()" function
20533 */
20534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020535f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020536{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020537 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020538
20539 rettv->v_type = VAR_FLOAT;
20540 if (get_float_arg(argvars, &f) == OK)
20541 rettv->vval.v_float = tan(f);
20542 else
20543 rettv->vval.v_float = 0.0;
20544}
20545
20546/*
20547 * "tanh()" function
20548 */
20549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020550f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020551{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020552 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020553
20554 rettv->v_type = VAR_FLOAT;
20555 if (get_float_arg(argvars, &f) == OK)
20556 rettv->vval.v_float = tanh(f);
20557 else
20558 rettv->vval.v_float = 0.0;
20559}
20560#endif
20561
Bram Moolenaard52d9742005-08-21 22:20:28 +000020562/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 * "tolower(string)" function
20564 */
20565 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020566f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020567{
20568 char_u *p;
20569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020570 p = vim_strsave(get_tv_string(&argvars[0]));
20571 rettv->v_type = VAR_STRING;
20572 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020573
20574 if (p != NULL)
20575 while (*p != NUL)
20576 {
20577#ifdef FEAT_MBYTE
20578 int l;
20579
20580 if (enc_utf8)
20581 {
20582 int c, lc;
20583
20584 c = utf_ptr2char(p);
20585 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020586 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 /* TODO: reallocate string when byte count changes. */
20588 if (utf_char2len(lc) == l)
20589 utf_char2bytes(lc, p);
20590 p += l;
20591 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020592 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593 p += l; /* skip multi-byte character */
20594 else
20595#endif
20596 {
20597 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20598 ++p;
20599 }
20600 }
20601}
20602
20603/*
20604 * "toupper(string)" function
20605 */
20606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020607f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020609 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020610 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611}
20612
20613/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020614 * "tr(string, fromstr, tostr)" function
20615 */
20616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020617f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020618{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020619 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020620 char_u *fromstr;
20621 char_u *tostr;
20622 char_u *p;
20623#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020624 int inlen;
20625 int fromlen;
20626 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020627 int idx;
20628 char_u *cpstr;
20629 int cplen;
20630 int first = TRUE;
20631#endif
20632 char_u buf[NUMBUFLEN];
20633 char_u buf2[NUMBUFLEN];
20634 garray_T ga;
20635
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020636 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020637 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20638 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020639
20640 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020641 rettv->v_type = VAR_STRING;
20642 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020643 if (fromstr == NULL || tostr == NULL)
20644 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020645 ga_init2(&ga, (int)sizeof(char), 80);
20646
20647#ifdef FEAT_MBYTE
20648 if (!has_mbyte)
20649#endif
20650 /* not multi-byte: fromstr and tostr must be the same length */
20651 if (STRLEN(fromstr) != STRLEN(tostr))
20652 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020653#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020654error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020655#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020656 EMSG2(_(e_invarg2), fromstr);
20657 ga_clear(&ga);
20658 return;
20659 }
20660
20661 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020662 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020663 {
20664#ifdef FEAT_MBYTE
20665 if (has_mbyte)
20666 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020667 inlen = (*mb_ptr2len)(in_str);
20668 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020669 cplen = inlen;
20670 idx = 0;
20671 for (p = fromstr; *p != NUL; p += fromlen)
20672 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020673 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020674 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020675 {
20676 for (p = tostr; *p != NUL; p += tolen)
20677 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020678 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020679 if (idx-- == 0)
20680 {
20681 cplen = tolen;
20682 cpstr = p;
20683 break;
20684 }
20685 }
20686 if (*p == NUL) /* tostr is shorter than fromstr */
20687 goto error;
20688 break;
20689 }
20690 ++idx;
20691 }
20692
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020693 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020694 {
20695 /* Check that fromstr and tostr have the same number of
20696 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020697 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020698 first = FALSE;
20699 for (p = tostr; *p != NUL; p += tolen)
20700 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020701 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020702 --idx;
20703 }
20704 if (idx != 0)
20705 goto error;
20706 }
20707
Bram Moolenaarcde88542015-08-11 19:14:00 +020020708 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020709 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020710 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020711
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020712 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020713 }
20714 else
20715#endif
20716 {
20717 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020718 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020719 if (p != NULL)
20720 ga_append(&ga, tostr[p - fromstr]);
20721 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020722 ga_append(&ga, *in_str);
20723 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020724 }
20725 }
20726
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020727 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020728 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020729 ga_append(&ga, NUL);
20730
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020731 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020732}
20733
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020734#ifdef FEAT_FLOAT
20735/*
20736 * "trunc({float})" function
20737 */
20738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020739f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020740{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020741 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020742
20743 rettv->v_type = VAR_FLOAT;
20744 if (get_float_arg(argvars, &f) == OK)
20745 /* trunc() is not in C90, use floor() or ceil() instead. */
20746 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20747 else
20748 rettv->vval.v_float = 0.0;
20749}
20750#endif
20751
Bram Moolenaar8299df92004-07-10 09:47:34 +000020752/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020753 * "type(expr)" function
20754 */
20755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020756f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020757{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020758 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020759
20760 switch (argvars[0].v_type)
20761 {
20762 case VAR_NUMBER: n = 0; break;
20763 case VAR_STRING: n = 1; break;
20764 case VAR_FUNC: n = 2; break;
20765 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020766 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020767 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020768 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020769 if (argvars[0].vval.v_number == VVAL_FALSE
20770 || argvars[0].vval.v_number == VVAL_TRUE)
20771 n = 6;
20772 else
20773 n = 7;
20774 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020775 case VAR_JOB: n = 8; break;
20776 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020777 case VAR_UNKNOWN:
20778 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20779 n = -1;
20780 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020781 }
20782 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020783}
20784
20785/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020786 * "undofile(name)" function
20787 */
20788 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020789f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020790{
20791 rettv->v_type = VAR_STRING;
20792#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020793 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020794 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020795
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020796 if (*fname == NUL)
20797 {
20798 /* If there is no file name there will be no undo file. */
20799 rettv->vval.v_string = NULL;
20800 }
20801 else
20802 {
20803 char_u *ffname = FullName_save(fname, FALSE);
20804
20805 if (ffname != NULL)
20806 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20807 vim_free(ffname);
20808 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020809 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020810#else
20811 rettv->vval.v_string = NULL;
20812#endif
20813}
20814
20815/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020816 * "undotree()" function
20817 */
20818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020819f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020820{
20821 if (rettv_dict_alloc(rettv) == OK)
20822 {
20823 dict_T *dict = rettv->vval.v_dict;
20824 list_T *list;
20825
Bram Moolenaar730cde92010-06-27 05:18:54 +020020826 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020827 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020828 dict_add_nr_str(dict, "save_last",
20829 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020830 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20831 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020832 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020833
20834 list = list_alloc();
20835 if (list != NULL)
20836 {
20837 u_eval_tree(curbuf->b_u_oldhead, list);
20838 dict_add_list(dict, "entries", list);
20839 }
20840 }
20841}
20842
20843/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020844 * "values(dict)" function
20845 */
20846 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020847f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020848{
20849 dict_list(argvars, rettv, 1);
20850}
20851
20852/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020853 * "virtcol(string)" function
20854 */
20855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020856f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020857{
20858 colnr_T vcol = 0;
20859 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020860 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020861
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020862 fp = var2fpos(&argvars[0], FALSE, &fnum);
20863 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20864 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020865 {
20866 getvvcol(curwin, fp, NULL, NULL, &vcol);
20867 ++vcol;
20868 }
20869
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020870 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020871}
20872
20873/*
20874 * "visualmode()" function
20875 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020877f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020878{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 char_u str[2];
20880
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882 str[0] = curbuf->b_visual_mode_eval;
20883 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020884 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885
20886 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020887 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020888 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889}
20890
20891/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020892 * "wildmenumode()" function
20893 */
20894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020895f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020896{
20897#ifdef FEAT_WILDMENU
20898 if (wild_menu_showing)
20899 rettv->vval.v_number = 1;
20900#endif
20901}
20902
20903/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020904 * "winbufnr(nr)" function
20905 */
20906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020907f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020908{
20909 win_T *wp;
20910
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020911 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020912 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020913 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020914 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020915 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020916}
20917
20918/*
20919 * "wincol()" function
20920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020922f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923{
20924 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020925 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926}
20927
20928/*
20929 * "winheight(nr)" function
20930 */
20931 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020932f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020933{
20934 win_T *wp;
20935
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020936 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020937 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020938 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020939 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020940 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020941}
20942
20943/*
20944 * "winline()" function
20945 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020947f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948{
20949 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020950 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020951}
20952
20953/*
20954 * "winnr()" function
20955 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020957f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958{
20959 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020960
Bram Moolenaar071d4272004-06-13 20:20:40 +000020961#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020962 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020963#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020964 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020965}
20966
20967/*
20968 * "winrestcmd()" function
20969 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020971f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972{
20973#ifdef FEAT_WINDOWS
20974 win_T *wp;
20975 int winnr = 1;
20976 garray_T ga;
20977 char_u buf[50];
20978
20979 ga_init2(&ga, (int)sizeof(char), 70);
20980 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20981 {
20982 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20983 ga_concat(&ga, buf);
20984# ifdef FEAT_VERTSPLIT
20985 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20986 ga_concat(&ga, buf);
20987# endif
20988 ++winnr;
20989 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020990 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020992 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020993#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020994 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020995#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020996 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020997}
20998
20999/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021000 * "winrestview()" function
21001 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021002 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021003f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021004{
21005 dict_T *dict;
21006
21007 if (argvars[0].v_type != VAR_DICT
21008 || (dict = argvars[0].vval.v_dict) == NULL)
21009 EMSG(_(e_invarg));
21010 else
21011 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021012 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21013 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21014 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21015 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021016#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021017 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21018 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021019#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021020 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21021 {
21022 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21023 curwin->w_set_curswant = FALSE;
21024 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021025
Bram Moolenaar82c25852014-05-28 16:47:16 +020021026 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21027 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021028#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021029 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21030 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021031#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021032 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21033 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21034 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21035 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021036
21037 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021038 win_new_height(curwin, curwin->w_height);
21039# ifdef FEAT_VERTSPLIT
21040 win_new_width(curwin, W_WIDTH(curwin));
21041# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021042 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021043
Bram Moolenaarb851a962014-10-31 15:45:52 +010021044 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021045 curwin->w_topline = 1;
21046 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21047 curwin->w_topline = curbuf->b_ml.ml_line_count;
21048#ifdef FEAT_DIFF
21049 check_topfill(curwin, TRUE);
21050#endif
21051 }
21052}
21053
21054/*
21055 * "winsaveview()" function
21056 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021059{
21060 dict_T *dict;
21061
Bram Moolenaara800b422010-06-27 01:15:55 +020021062 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021063 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021064 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021065
21066 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21067 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21068#ifdef FEAT_VIRTUALEDIT
21069 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21070#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021071 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021072 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21073
21074 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21075#ifdef FEAT_DIFF
21076 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21077#endif
21078 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21079 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21080}
21081
21082/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021083 * "winwidth(nr)" function
21084 */
21085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021086f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087{
21088 win_T *wp;
21089
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021090 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093 else
21094#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021095 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021096#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021097 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098#endif
21099}
21100
Bram Moolenaar071d4272004-06-13 20:20:40 +000021101/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021102 * "wordcount()" function
21103 */
21104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021105f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021106{
21107 if (rettv_dict_alloc(rettv) == FAIL)
21108 return;
21109 cursor_pos_info(rettv->vval.v_dict);
21110}
21111
21112/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021113 * Write list of strings to file
21114 */
21115 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021116write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021117{
21118 listitem_T *li;
21119 int c;
21120 int ret = OK;
21121 char_u *s;
21122
21123 for (li = list->lv_first; li != NULL; li = li->li_next)
21124 {
21125 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21126 {
21127 if (*s == '\n')
21128 c = putc(NUL, fd);
21129 else
21130 c = putc(*s, fd);
21131 if (c == EOF)
21132 {
21133 ret = FAIL;
21134 break;
21135 }
21136 }
21137 if (!binary || li->li_next != NULL)
21138 if (putc('\n', fd) == EOF)
21139 {
21140 ret = FAIL;
21141 break;
21142 }
21143 if (ret == FAIL)
21144 {
21145 EMSG(_(e_write));
21146 break;
21147 }
21148 }
21149 return ret;
21150}
21151
21152/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021153 * "writefile()" function
21154 */
21155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021156f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021157{
21158 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021159 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021160 char_u *fname;
21161 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021162 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021163
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021164 if (check_restricted() || check_secure())
21165 return;
21166
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021167 if (argvars[0].v_type != VAR_LIST)
21168 {
21169 EMSG2(_(e_listarg), "writefile()");
21170 return;
21171 }
21172 if (argvars[0].vval.v_list == NULL)
21173 return;
21174
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021175 if (argvars[2].v_type != VAR_UNKNOWN)
21176 {
21177 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21178 binary = TRUE;
21179 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21180 append = TRUE;
21181 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021182
21183 /* Always open the file in binary mode, library functions have a mind of
21184 * their own about CR-LF conversion. */
21185 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021186 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21187 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021188 {
21189 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21190 ret = -1;
21191 }
21192 else
21193 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021194 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21195 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021196 fclose(fd);
21197 }
21198
21199 rettv->vval.v_number = ret;
21200}
21201
21202/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021203 * "xor(expr, expr)" function
21204 */
21205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021206f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021207{
21208 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21209 ^ get_tv_number_chk(&argvars[1], NULL);
21210}
21211
21212
21213/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021214 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021215 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021216 */
21217 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021218var2fpos(
21219 typval_T *varp,
21220 int dollar_lnum, /* TRUE when $ is last line */
21221 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021223 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021224 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021225 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226
Bram Moolenaara5525202006-03-02 22:52:09 +000021227 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021228 if (varp->v_type == VAR_LIST)
21229 {
21230 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021231 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021232 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021233 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021234
21235 l = varp->vval.v_list;
21236 if (l == NULL)
21237 return NULL;
21238
21239 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021240 pos.lnum = list_find_nr(l, 0L, &error);
21241 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021242 return NULL; /* invalid line number */
21243
21244 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021245 pos.col = list_find_nr(l, 1L, &error);
21246 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021247 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021248 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021249
21250 /* We accept "$" for the column number: last column. */
21251 li = list_find(l, 1L);
21252 if (li != NULL && li->li_tv.v_type == VAR_STRING
21253 && li->li_tv.vval.v_string != NULL
21254 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21255 pos.col = len + 1;
21256
Bram Moolenaara5525202006-03-02 22:52:09 +000021257 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021258 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021259 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021260 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021261
Bram Moolenaara5525202006-03-02 22:52:09 +000021262#ifdef FEAT_VIRTUALEDIT
21263 /* Get the virtual offset. Defaults to zero. */
21264 pos.coladd = list_find_nr(l, 2L, &error);
21265 if (error)
21266 pos.coladd = 0;
21267#endif
21268
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021269 return &pos;
21270 }
21271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021272 name = get_tv_string_chk(varp);
21273 if (name == NULL)
21274 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021275 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021276 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021277 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21278 {
21279 if (VIsual_active)
21280 return &VIsual;
21281 return &curwin->w_cursor;
21282 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021283 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021285 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021286 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21287 return NULL;
21288 return pp;
21289 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021290
21291#ifdef FEAT_VIRTUALEDIT
21292 pos.coladd = 0;
21293#endif
21294
Bram Moolenaar477933c2007-07-17 14:32:23 +000021295 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021296 {
21297 pos.col = 0;
21298 if (name[1] == '0') /* "w0": first visible line */
21299 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021300 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021301 pos.lnum = curwin->w_topline;
21302 return &pos;
21303 }
21304 else if (name[1] == '$') /* "w$": last visible line */
21305 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021306 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021307 pos.lnum = curwin->w_botline - 1;
21308 return &pos;
21309 }
21310 }
21311 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021313 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021314 {
21315 pos.lnum = curbuf->b_ml.ml_line_count;
21316 pos.col = 0;
21317 }
21318 else
21319 {
21320 pos.lnum = curwin->w_cursor.lnum;
21321 pos.col = (colnr_T)STRLEN(ml_get_curline());
21322 }
21323 return &pos;
21324 }
21325 return NULL;
21326}
21327
21328/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021329 * Convert list in "arg" into a position and optional file number.
21330 * When "fnump" is NULL there is no file number, only 3 items.
21331 * Note that the column is passed on as-is, the caller may want to decrement
21332 * it to use 1 for the first column.
21333 * Return FAIL when conversion is not possible, doesn't check the position for
21334 * validity.
21335 */
21336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021337list2fpos(
21338 typval_T *arg,
21339 pos_T *posp,
21340 int *fnump,
21341 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021342{
21343 list_T *l = arg->vval.v_list;
21344 long i = 0;
21345 long n;
21346
Bram Moolenaar493c1782014-05-28 14:34:46 +020021347 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21348 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021349 if (arg->v_type != VAR_LIST
21350 || l == NULL
21351 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021352 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021353 return FAIL;
21354
21355 if (fnump != NULL)
21356 {
21357 n = list_find_nr(l, i++, NULL); /* fnum */
21358 if (n < 0)
21359 return FAIL;
21360 if (n == 0)
21361 n = curbuf->b_fnum; /* current buffer */
21362 *fnump = n;
21363 }
21364
21365 n = list_find_nr(l, i++, NULL); /* lnum */
21366 if (n < 0)
21367 return FAIL;
21368 posp->lnum = n;
21369
21370 n = list_find_nr(l, i++, NULL); /* col */
21371 if (n < 0)
21372 return FAIL;
21373 posp->col = n;
21374
21375#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021376 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021377 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021378 posp->coladd = 0;
21379 else
21380 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021381#endif
21382
Bram Moolenaar493c1782014-05-28 14:34:46 +020021383 if (curswantp != NULL)
21384 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21385
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021386 return OK;
21387}
21388
21389/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 * Get the length of an environment variable name.
21391 * Advance "arg" to the first character after the name.
21392 * Return 0 for error.
21393 */
21394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021395get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021396{
21397 char_u *p;
21398 int len;
21399
21400 for (p = *arg; vim_isIDc(*p); ++p)
21401 ;
21402 if (p == *arg) /* no name found */
21403 return 0;
21404
21405 len = (int)(p - *arg);
21406 *arg = p;
21407 return len;
21408}
21409
21410/*
21411 * Get the length of the name of a function or internal variable.
21412 * "arg" is advanced to the first non-white character after the name.
21413 * Return 0 if something is wrong.
21414 */
21415 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021416get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417{
21418 char_u *p;
21419 int len;
21420
21421 /* Find the end of the name. */
21422 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021423 {
21424 if (*p == ':')
21425 {
21426 /* "s:" is start of "s:var", but "n:" is not and can be used in
21427 * slice "[n:]". Also "xx:" is not a namespace. */
21428 len = (int)(p - *arg);
21429 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21430 || len > 1)
21431 break;
21432 }
21433 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021434 if (p == *arg) /* no name found */
21435 return 0;
21436
21437 len = (int)(p - *arg);
21438 *arg = skipwhite(p);
21439
21440 return len;
21441}
21442
21443/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021444 * Get the length of the name of a variable or function.
21445 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021447 * Return -1 if curly braces expansion failed.
21448 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021449 * If the name contains 'magic' {}'s, expand them and return the
21450 * expanded name in an allocated string via 'alias' - caller must free.
21451 */
21452 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021453get_name_len(
21454 char_u **arg,
21455 char_u **alias,
21456 int evaluate,
21457 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021458{
21459 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460 char_u *p;
21461 char_u *expr_start;
21462 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021463
21464 *alias = NULL; /* default to no alias */
21465
21466 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21467 && (*arg)[2] == (int)KE_SNR)
21468 {
21469 /* hard coded <SNR>, already translated */
21470 *arg += 3;
21471 return get_id_len(arg) + 3;
21472 }
21473 len = eval_fname_script(*arg);
21474 if (len > 0)
21475 {
21476 /* literal "<SID>", "s:" or "<SNR>" */
21477 *arg += len;
21478 }
21479
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021481 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021483 p = find_name_end(*arg, &expr_start, &expr_end,
21484 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021485 if (expr_start != NULL)
21486 {
21487 char_u *temp_string;
21488
21489 if (!evaluate)
21490 {
21491 len += (int)(p - *arg);
21492 *arg = skipwhite(p);
21493 return len;
21494 }
21495
21496 /*
21497 * Include any <SID> etc in the expanded string:
21498 * Thus the -len here.
21499 */
21500 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21501 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021502 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021503 *alias = temp_string;
21504 *arg = skipwhite(p);
21505 return (int)STRLEN(temp_string);
21506 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507
21508 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021509 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510 EMSG2(_(e_invexpr2), *arg);
21511
21512 return len;
21513}
21514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021515/*
21516 * Find the end of a variable or function name, taking care of magic braces.
21517 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21518 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021519 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021520 * Return a pointer to just after the name. Equal to "arg" if there is no
21521 * valid name.
21522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021524find_name_end(
21525 char_u *arg,
21526 char_u **expr_start,
21527 char_u **expr_end,
21528 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021530 int mb_nest = 0;
21531 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021532 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021533 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021534
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021535 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021536 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021537 *expr_start = NULL;
21538 *expr_end = NULL;
21539 }
21540
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021541 /* Quick check for valid starting character. */
21542 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21543 return arg;
21544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021545 for (p = arg; *p != NUL
21546 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021547 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021548 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021549 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021550 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021551 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021552 if (*p == '\'')
21553 {
21554 /* skip over 'string' to avoid counting [ and ] inside it. */
21555 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21556 ;
21557 if (*p == NUL)
21558 break;
21559 }
21560 else if (*p == '"')
21561 {
21562 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21563 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21564 if (*p == '\\' && p[1] != NUL)
21565 ++p;
21566 if (*p == NUL)
21567 break;
21568 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021569 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21570 {
21571 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021572 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021573 len = (int)(p - arg);
21574 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021575 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021576 break;
21577 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021578
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021579 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021580 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021581 if (*p == '[')
21582 ++br_nest;
21583 else if (*p == ']')
21584 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021587 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021589 if (*p == '{')
21590 {
21591 mb_nest++;
21592 if (expr_start != NULL && *expr_start == NULL)
21593 *expr_start = p;
21594 }
21595 else if (*p == '}')
21596 {
21597 mb_nest--;
21598 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21599 *expr_end = p;
21600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 }
21603
21604 return p;
21605}
21606
21607/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021608 * Expands out the 'magic' {}'s in a variable/function name.
21609 * Note that this can call itself recursively, to deal with
21610 * constructs like foo{bar}{baz}{bam}
21611 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21612 * "in_start" ^
21613 * "expr_start" ^
21614 * "expr_end" ^
21615 * "in_end" ^
21616 *
21617 * Returns a new allocated string, which the caller must free.
21618 * Returns NULL for failure.
21619 */
21620 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021621make_expanded_name(
21622 char_u *in_start,
21623 char_u *expr_start,
21624 char_u *expr_end,
21625 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021626{
21627 char_u c1;
21628 char_u *retval = NULL;
21629 char_u *temp_result;
21630 char_u *nextcmd = NULL;
21631
21632 if (expr_end == NULL || in_end == NULL)
21633 return NULL;
21634 *expr_start = NUL;
21635 *expr_end = NUL;
21636 c1 = *in_end;
21637 *in_end = NUL;
21638
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021639 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021640 if (temp_result != NULL && nextcmd == NULL)
21641 {
21642 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21643 + (in_end - expr_end) + 1));
21644 if (retval != NULL)
21645 {
21646 STRCPY(retval, in_start);
21647 STRCAT(retval, temp_result);
21648 STRCAT(retval, expr_end + 1);
21649 }
21650 }
21651 vim_free(temp_result);
21652
21653 *in_end = c1; /* put char back for error messages */
21654 *expr_start = '{';
21655 *expr_end = '}';
21656
21657 if (retval != NULL)
21658 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021659 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021660 if (expr_start != NULL)
21661 {
21662 /* Further expansion! */
21663 temp_result = make_expanded_name(retval, expr_start,
21664 expr_end, temp_result);
21665 vim_free(retval);
21666 retval = temp_result;
21667 }
21668 }
21669
21670 return retval;
21671}
21672
21673/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021675 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676 */
21677 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021678eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021679{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021680 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21681}
21682
21683/*
21684 * Return TRUE if character "c" can be used as the first character in a
21685 * variable or function name (excluding '{' and '}').
21686 */
21687 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021688eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021689{
21690 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691}
21692
21693/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 * Set number v: variable to "val".
21695 */
21696 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021697set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021698{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021699 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021700}
21701
21702/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021703 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704 */
21705 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021706get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021708 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709}
21710
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021711/*
21712 * Get string v: variable value. Uses a static buffer, can only be used once.
21713 */
21714 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021715get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021716{
21717 return get_tv_string(&vimvars[idx].vv_tv);
21718}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021719
Bram Moolenaar071d4272004-06-13 20:20:40 +000021720/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021721 * Get List v: variable value. Caller must take care of reference count when
21722 * needed.
21723 */
21724 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021725get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021726{
21727 return vimvars[idx].vv_list;
21728}
21729
21730/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021731 * Set v:char to character "c".
21732 */
21733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021734set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021735{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021736 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021737
21738#ifdef FEAT_MBYTE
21739 if (has_mbyte)
21740 buf[(*mb_char2bytes)(c, buf)] = NUL;
21741 else
21742#endif
21743 {
21744 buf[0] = c;
21745 buf[1] = NUL;
21746 }
21747 set_vim_var_string(VV_CHAR, buf, -1);
21748}
21749
21750/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021751 * Set v:count to "count" and v:count1 to "count1".
21752 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021753 */
21754 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021755set_vcount(
21756 long count,
21757 long count1,
21758 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021759{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021760 if (set_prevcount)
21761 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021762 vimvars[VV_COUNT].vv_nr = count;
21763 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021764}
21765
21766/*
21767 * Set string v: variable to a copy of "val".
21768 */
21769 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021770set_vim_var_string(
21771 int idx,
21772 char_u *val,
21773 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774{
Bram Moolenaara542c682016-01-31 16:28:04 +010021775 clear_tv(&vimvars[idx].vv_di.di_tv);
21776 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021777 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021778 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021779 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021780 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021781 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021782 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021783}
21784
21785/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021786 * Set List v: variable to "val".
21787 */
21788 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021789set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021790{
Bram Moolenaara542c682016-01-31 16:28:04 +010021791 clear_tv(&vimvars[idx].vv_di.di_tv);
21792 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021793 vimvars[idx].vv_list = val;
21794 if (val != NULL)
21795 ++val->lv_refcount;
21796}
21797
21798/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021799 * Set Dictionary v: variable to "val".
21800 */
21801 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021802set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021803{
21804 int todo;
21805 hashitem_T *hi;
21806
Bram Moolenaara542c682016-01-31 16:28:04 +010021807 clear_tv(&vimvars[idx].vv_di.di_tv);
21808 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021809 vimvars[idx].vv_dict = val;
21810 if (val != NULL)
21811 {
21812 ++val->dv_refcount;
21813
21814 /* Set readonly */
21815 todo = (int)val->dv_hashtab.ht_used;
21816 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21817 {
21818 if (HASHITEM_EMPTY(hi))
21819 continue;
21820 --todo;
21821 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21822 }
21823 }
21824}
21825
21826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021827 * Set v:register if needed.
21828 */
21829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021830set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021831{
21832 char_u regname;
21833
21834 if (c == 0 || c == ' ')
21835 regname = '"';
21836 else
21837 regname = c;
21838 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021839 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021840 set_vim_var_string(VV_REG, &regname, 1);
21841}
21842
21843/*
21844 * Get or set v:exception. If "oldval" == NULL, return the current value.
21845 * Otherwise, restore the value to "oldval" and return NULL.
21846 * Must always be called in pairs to save and restore v:exception! Does not
21847 * take care of memory allocations.
21848 */
21849 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021850v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851{
21852 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021853 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854
Bram Moolenaare9a41262005-01-15 22:18:47 +000021855 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021856 return NULL;
21857}
21858
21859/*
21860 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21861 * Otherwise, restore the value to "oldval" and return NULL.
21862 * Must always be called in pairs to save and restore v:throwpoint! Does not
21863 * take care of memory allocations.
21864 */
21865 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021866v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867{
21868 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021869 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021870
Bram Moolenaare9a41262005-01-15 22:18:47 +000021871 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 return NULL;
21873}
21874
21875#if defined(FEAT_AUTOCMD) || defined(PROTO)
21876/*
21877 * Set v:cmdarg.
21878 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21879 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21880 * Must always be called in pairs!
21881 */
21882 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021883set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021884{
21885 char_u *oldval;
21886 char_u *newval;
21887 unsigned len;
21888
Bram Moolenaare9a41262005-01-15 22:18:47 +000021889 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021890 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021891 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021892 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021893 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021894 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021895 }
21896
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021897 if (eap->force_bin == FORCE_BIN)
21898 len = 6;
21899 else if (eap->force_bin == FORCE_NOBIN)
21900 len = 8;
21901 else
21902 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021903
21904 if (eap->read_edit)
21905 len += 7;
21906
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021907 if (eap->force_ff != 0)
21908 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21909# ifdef FEAT_MBYTE
21910 if (eap->force_enc != 0)
21911 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021912 if (eap->bad_char != 0)
21913 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021914# endif
21915
21916 newval = alloc(len + 1);
21917 if (newval == NULL)
21918 return NULL;
21919
21920 if (eap->force_bin == FORCE_BIN)
21921 sprintf((char *)newval, " ++bin");
21922 else if (eap->force_bin == FORCE_NOBIN)
21923 sprintf((char *)newval, " ++nobin");
21924 else
21925 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021926
21927 if (eap->read_edit)
21928 STRCAT(newval, " ++edit");
21929
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021930 if (eap->force_ff != 0)
21931 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21932 eap->cmd + eap->force_ff);
21933# ifdef FEAT_MBYTE
21934 if (eap->force_enc != 0)
21935 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21936 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021937 if (eap->bad_char == BAD_KEEP)
21938 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21939 else if (eap->bad_char == BAD_DROP)
21940 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21941 else if (eap->bad_char != 0)
21942 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021943# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021944 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021945 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021946}
21947#endif
21948
21949/*
21950 * Get the value of internal variable "name".
21951 * Return OK or FAIL.
21952 */
21953 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021954get_var_tv(
21955 char_u *name,
21956 int len, /* length of "name" */
21957 typval_T *rettv, /* NULL when only checking existence */
21958 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21959 int verbose, /* may give error message */
21960 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021961{
21962 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021963 typval_T *tv = NULL;
21964 typval_T atv;
21965 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021966 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021967
21968 /* truncate the name, so that we can use strcmp() */
21969 cc = name[len];
21970 name[len] = NUL;
21971
21972 /*
21973 * Check for "b:changedtick".
21974 */
21975 if (STRCMP(name, "b:changedtick") == 0)
21976 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021977 atv.v_type = VAR_NUMBER;
21978 atv.vval.v_number = curbuf->b_changedtick;
21979 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 }
21981
21982 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 * Check for user-defined variables.
21984 */
21985 else
21986 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021987 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021988 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021989 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021990 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021991 if (dip != NULL)
21992 *dip = v;
21993 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 }
21995
Bram Moolenaare9a41262005-01-15 22:18:47 +000021996 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021998 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022000 ret = FAIL;
22001 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022002 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022003 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022004
22005 name[len] = cc;
22006
22007 return ret;
22008}
22009
22010/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022011 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22012 * Also handle function call with Funcref variable: func(expr)
22013 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22014 */
22015 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022016handle_subscript(
22017 char_u **arg,
22018 typval_T *rettv,
22019 int evaluate, /* do more than finding the end */
22020 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022021{
22022 int ret = OK;
22023 dict_T *selfdict = NULL;
22024 char_u *s;
22025 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022026 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022027
22028 while (ret == OK
22029 && (**arg == '['
22030 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022031 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022032 && !vim_iswhite(*(*arg - 1)))
22033 {
22034 if (**arg == '(')
22035 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022036 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022037 if (evaluate)
22038 {
22039 functv = *rettv;
22040 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022041
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022042 /* Invoke the function. Recursive! */
22043 s = functv.vval.v_string;
22044 }
22045 else
22046 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022047 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022048 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22049 &len, evaluate, selfdict);
22050
22051 /* Clear the funcref afterwards, so that deleting it while
22052 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022053 if (evaluate)
22054 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022055
22056 /* Stop the expression evaluation when immediately aborting on
22057 * error, or when an interrupt occurred or an exception was thrown
22058 * but not caught. */
22059 if (aborting())
22060 {
22061 if (ret == OK)
22062 clear_tv(rettv);
22063 ret = FAIL;
22064 }
22065 dict_unref(selfdict);
22066 selfdict = NULL;
22067 }
22068 else /* **arg == '[' || **arg == '.' */
22069 {
22070 dict_unref(selfdict);
22071 if (rettv->v_type == VAR_DICT)
22072 {
22073 selfdict = rettv->vval.v_dict;
22074 if (selfdict != NULL)
22075 ++selfdict->dv_refcount;
22076 }
22077 else
22078 selfdict = NULL;
22079 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22080 {
22081 clear_tv(rettv);
22082 ret = FAIL;
22083 }
22084 }
22085 }
22086 dict_unref(selfdict);
22087 return ret;
22088}
22089
22090/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022091 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022092 * value).
22093 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022094 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022095alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022096{
Bram Moolenaar33570922005-01-25 22:26:29 +000022097 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022098}
22099
22100/*
22101 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022102 * The string "s" must have been allocated, it is consumed.
22103 * Return NULL for out of memory, the variable otherwise.
22104 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022106alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107{
Bram Moolenaar33570922005-01-25 22:26:29 +000022108 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022110 rettv = alloc_tv();
22111 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022113 rettv->v_type = VAR_STRING;
22114 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115 }
22116 else
22117 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022118 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119}
22120
22121/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022122 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022124 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022125free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022126{
22127 if (varp != NULL)
22128 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022129 switch (varp->v_type)
22130 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022131 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022132 func_unref(varp->vval.v_string);
22133 /*FALLTHROUGH*/
22134 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022135 vim_free(varp->vval.v_string);
22136 break;
22137 case VAR_LIST:
22138 list_unref(varp->vval.v_list);
22139 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022140 case VAR_DICT:
22141 dict_unref(varp->vval.v_dict);
22142 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022143 case VAR_JOB:
22144#ifdef FEAT_JOB
22145 job_unref(varp->vval.v_job);
22146 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022147#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022148 case VAR_CHANNEL:
22149#ifdef FEAT_CHANNEL
22150 channel_unref(varp->vval.v_channel);
22151 break;
22152#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022153 case VAR_NUMBER:
22154 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022155 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022156 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022157 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022159 vim_free(varp);
22160 }
22161}
22162
22163/*
22164 * Free the memory for a variable value and set the value to NULL or 0.
22165 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022166 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022167clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168{
22169 if (varp != NULL)
22170 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022171 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022172 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022173 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022174 func_unref(varp->vval.v_string);
22175 /*FALLTHROUGH*/
22176 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022177 vim_free(varp->vval.v_string);
22178 varp->vval.v_string = NULL;
22179 break;
22180 case VAR_LIST:
22181 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022182 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022183 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022184 case VAR_DICT:
22185 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022186 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022187 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022188 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022189 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022190 varp->vval.v_number = 0;
22191 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022192 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022193#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022194 varp->vval.v_float = 0.0;
22195 break;
22196#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022197 case VAR_JOB:
22198#ifdef FEAT_JOB
22199 job_unref(varp->vval.v_job);
22200 varp->vval.v_job = NULL;
22201#endif
22202 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022203 case VAR_CHANNEL:
22204#ifdef FEAT_CHANNEL
22205 channel_unref(varp->vval.v_channel);
22206 varp->vval.v_channel = NULL;
22207#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022208 case VAR_UNKNOWN:
22209 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022210 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022211 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022212 }
22213}
22214
22215/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022216 * Set the value of a variable to NULL without freeing items.
22217 */
22218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022219init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022220{
22221 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022222 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022223}
22224
22225/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022226 * Get the number value of a variable.
22227 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022228 * For incompatible types, return 0.
22229 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22230 * caller of incompatible types: it sets *denote to TRUE if "denote"
22231 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 */
22233 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022235{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022236 int error = FALSE;
22237
22238 return get_tv_number_chk(varp, &error); /* return 0L on error */
22239}
22240
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022241 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022242get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022243{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022244 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022247 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022248 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022249 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022250 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022251#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022252 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022253 break;
22254#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022255 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022256 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022257 break;
22258 case VAR_STRING:
22259 if (varp->vval.v_string != NULL)
22260 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022261 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022262 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022263 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022264 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022265 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022266 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022267 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022268 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022269 case VAR_SPECIAL:
22270 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22271 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022272 case VAR_JOB:
22273#ifdef FEAT_JOB
22274 EMSG(_("E910: Using a Job as a Number"));
22275 break;
22276#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022277 case VAR_CHANNEL:
22278#ifdef FEAT_CHANNEL
22279 EMSG(_("E913: Using a Channel as a Number"));
22280 break;
22281#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022282 case VAR_UNKNOWN:
22283 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022284 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022286 if (denote == NULL) /* useful for values that must be unsigned */
22287 n = -1;
22288 else
22289 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022290 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022291}
22292
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022293#ifdef FEAT_FLOAT
22294 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022295get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022296{
22297 switch (varp->v_type)
22298 {
22299 case VAR_NUMBER:
22300 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022301 case VAR_FLOAT:
22302 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022303 case VAR_FUNC:
22304 EMSG(_("E891: Using a Funcref as a Float"));
22305 break;
22306 case VAR_STRING:
22307 EMSG(_("E892: Using a String as a Float"));
22308 break;
22309 case VAR_LIST:
22310 EMSG(_("E893: Using a List as a Float"));
22311 break;
22312 case VAR_DICT:
22313 EMSG(_("E894: Using a Dictionary as a Float"));
22314 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022315 case VAR_SPECIAL:
22316 EMSG(_("E907: Using a special value as a Float"));
22317 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022318 case VAR_JOB:
22319# ifdef FEAT_JOB
22320 EMSG(_("E911: Using a Job as a Float"));
22321 break;
22322# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022323 case VAR_CHANNEL:
22324# ifdef FEAT_CHANNEL
22325 EMSG(_("E914: Using a Channel as a Float"));
22326 break;
22327# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022328 case VAR_UNKNOWN:
22329 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022330 break;
22331 }
22332 return 0;
22333}
22334#endif
22335
Bram Moolenaar071d4272004-06-13 20:20:40 +000022336/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022337 * Get the lnum from the first argument.
22338 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022339 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340 */
22341 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022342get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343{
Bram Moolenaar33570922005-01-25 22:26:29 +000022344 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 linenr_T lnum;
22346
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022347 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022348 if (lnum == 0) /* no valid number, try using line() */
22349 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022350 rettv.v_type = VAR_NUMBER;
22351 f_line(argvars, &rettv);
22352 lnum = rettv.vval.v_number;
22353 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354 }
22355 return lnum;
22356}
22357
22358/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022359 * Get the lnum from the first argument.
22360 * Also accepts "$", then "buf" is used.
22361 * Returns 0 on error.
22362 */
22363 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022364get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022365{
22366 if (argvars[0].v_type == VAR_STRING
22367 && argvars[0].vval.v_string != NULL
22368 && argvars[0].vval.v_string[0] == '$'
22369 && buf != NULL)
22370 return buf->b_ml.ml_line_count;
22371 return get_tv_number_chk(&argvars[0], NULL);
22372}
22373
22374/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022375 * Get the string value of a variable.
22376 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022377 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22378 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022379 * If the String variable has never been set, return an empty string.
22380 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022381 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22382 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 */
22384 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022385get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386{
22387 static char_u mybuf[NUMBUFLEN];
22388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022389 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022390}
22391
22392 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022393get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022395 char_u *res = get_tv_string_buf_chk(varp, buf);
22396
22397 return res != NULL ? res : (char_u *)"";
22398}
22399
Bram Moolenaar7d647822014-04-05 21:28:56 +020022400/*
22401 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22402 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022403 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022404get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022405{
22406 static char_u mybuf[NUMBUFLEN];
22407
22408 return get_tv_string_buf_chk(varp, mybuf);
22409}
22410
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022412get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022413{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022414 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022415 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022416 case VAR_NUMBER:
22417 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22418 return buf;
22419 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022420 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022421 break;
22422 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022424 break;
22425 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022426 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022427 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022428 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022429#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022430 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022431 break;
22432#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022433 case VAR_STRING:
22434 if (varp->vval.v_string != NULL)
22435 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022436 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022437 case VAR_SPECIAL:
22438 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22439 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022440 case VAR_JOB:
22441#ifdef FEAT_JOB
22442 {
22443 job_T *job = varp->vval.v_job;
22444 char *status = job->jv_status == JOB_FAILED ? "fail"
22445 : job->jv_status == JOB_ENDED ? "dead"
22446 : "run";
22447# ifdef UNIX
22448 vim_snprintf((char *)buf, NUMBUFLEN,
22449 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022450# elif defined(WIN32)
22451 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022452 "process %ld %s",
22453 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022454 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022455# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022456 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022457 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22458# endif
22459 return buf;
22460 }
22461#endif
22462 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022463 case VAR_CHANNEL:
22464#ifdef FEAT_CHANNEL
22465 {
22466 channel_T *channel = varp->vval.v_channel;
22467 char *status = channel_status(channel);
22468
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022469 if (channel == NULL)
22470 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22471 else
22472 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022473 "channel %d %s", channel->ch_id, status);
22474 return buf;
22475 }
22476#endif
22477 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022478 case VAR_UNKNOWN:
22479 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022480 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022482 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483}
22484
22485/*
22486 * Find variable "name" in the list of variables.
22487 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022489 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022492 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022493find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022496 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022497
Bram Moolenaara7043832005-01-21 11:56:39 +000022498 ht = find_var_ht(name, &varname);
22499 if (htp != NULL)
22500 *htp = ht;
22501 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022503 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022504}
22505
22506/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022507 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022508 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022509 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022510 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022511find_var_in_ht(
22512 hashtab_T *ht,
22513 int htname,
22514 char_u *varname,
22515 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022516{
Bram Moolenaar33570922005-01-25 22:26:29 +000022517 hashitem_T *hi;
22518
22519 if (*varname == NUL)
22520 {
22521 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022522 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022523 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022524 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022525 case 'g': return &globvars_var;
22526 case 'v': return &vimvars_var;
22527 case 'b': return &curbuf->b_bufvar;
22528 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022529#ifdef FEAT_WINDOWS
22530 case 't': return &curtab->tp_winvar;
22531#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022532 case 'l': return current_funccal == NULL
22533 ? NULL : &current_funccal->l_vars_var;
22534 case 'a': return current_funccal == NULL
22535 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 }
22537 return NULL;
22538 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022539
22540 hi = hash_find(ht, varname);
22541 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022542 {
22543 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022544 * worked find the variable again. Don't auto-load a script if it was
22545 * loaded already, otherwise it would be loaded every time when
22546 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022547 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022548 {
22549 /* Note: script_autoload() may make "hi" invalid. It must either
22550 * be obtained again or not used. */
22551 if (!script_autoload(varname, FALSE) || aborting())
22552 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022553 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022554 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022555 if (HASHITEM_EMPTY(hi))
22556 return NULL;
22557 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022558 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022559}
22560
22561/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022563 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022564 * Set "varname" to the start of name without ':'.
22565 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022566 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022567find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022569 hashitem_T *hi;
22570
Bram Moolenaar73627d02015-08-11 15:46:09 +020022571 if (name[0] == NUL)
22572 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 if (name[1] != ':')
22574 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022575 /* The name must not start with a colon or #. */
22576 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022577 return NULL;
22578 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022579
22580 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022581 hi = hash_find(&compat_hashtab, name);
22582 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022583 return &compat_hashtab;
22584
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022586 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022587 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022588 }
22589 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022590 if (*name == 'g') /* global variable */
22591 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022592 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22593 */
22594 if (vim_strchr(name + 2, ':') != NULL
22595 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022596 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022597 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022598 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022600 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022601#ifdef FEAT_WINDOWS
22602 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022603 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022604#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022605 if (*name == 'v') /* v: variable */
22606 return &vimvarht;
22607 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022608 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022609 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022610 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022611 if (*name == 's' /* script variable */
22612 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22613 return &SCRIPT_VARS(current_SID);
22614 return NULL;
22615}
22616
22617/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022618 * Get function call environment based on bactrace debug level
22619 */
22620 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022621get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022622{
22623 int i;
22624 funccall_T *funccal;
22625 funccall_T *temp_funccal;
22626
22627 funccal = current_funccal;
22628 if (debug_backtrace_level > 0)
22629 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022630 for (i = 0; i < debug_backtrace_level; i++)
22631 {
22632 temp_funccal = funccal->caller;
22633 if (temp_funccal)
22634 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022635 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022636 /* backtrace level overflow. reset to max */
22637 debug_backtrace_level = i;
22638 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022639 }
22640 return funccal;
22641}
22642
22643/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022645 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022646 * Returns NULL when it doesn't exist.
22647 */
22648 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022649get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022650{
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022652
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022653 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654 if (v == NULL)
22655 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022657}
22658
22659/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022660 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 * sourcing this script and when executing functions defined in the script.
22662 */
22663 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022664new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665{
Bram Moolenaara7043832005-01-21 11:56:39 +000022666 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 hashtab_T *ht;
22668 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022669
Bram Moolenaar071d4272004-06-13 20:20:40 +000022670 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022672 /* Re-allocating ga_data means that an ht_array pointing to
22673 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022674 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022675 for (i = 1; i <= ga_scripts.ga_len; ++i)
22676 {
22677 ht = &SCRIPT_VARS(i);
22678 if (ht->ht_mask == HT_INIT_SIZE - 1)
22679 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022680 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022681 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022682 }
22683
Bram Moolenaar071d4272004-06-13 20:20:40 +000022684 while (ga_scripts.ga_len < id)
22685 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022686 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022687 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022688 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022689 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 }
22691 }
22692}
22693
22694/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022695 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22696 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697 */
22698 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022699init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022700{
Bram Moolenaar33570922005-01-25 22:26:29 +000022701 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022702 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022703 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022704 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022705 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 dict_var->di_tv.vval.v_dict = dict;
22707 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022708 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022709 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22710 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022711}
22712
22713/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022714 * Unreference a dictionary initialized by init_var_dict().
22715 */
22716 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022717unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022718{
22719 /* Now the dict needs to be freed if no one else is using it, go back to
22720 * normal reference counting. */
22721 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22722 dict_unref(dict);
22723}
22724
22725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 * Frees all allocated variables and the value they contain.
22728 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022729 */
22730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022731vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022732{
22733 vars_clear_ext(ht, TRUE);
22734}
22735
22736/*
22737 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22738 */
22739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022740vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022741{
Bram Moolenaara7043832005-01-21 11:56:39 +000022742 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 hashitem_T *hi;
22744 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022745
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022747 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022748 for (hi = ht->ht_array; todo > 0; ++hi)
22749 {
22750 if (!HASHITEM_EMPTY(hi))
22751 {
22752 --todo;
22753
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022755 * ht_array might change then. hash_clear() takes care of it
22756 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022757 v = HI2DI(hi);
22758 if (free_val)
22759 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022760 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022762 }
22763 }
22764 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022765 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022766}
22767
Bram Moolenaara7043832005-01-21 11:56:39 +000022768/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 * Delete a variable from hashtab "ht" at item "hi".
22770 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022771 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022773delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022774{
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022776
22777 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 clear_tv(&di->di_tv);
22779 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022780}
22781
22782/*
22783 * List the value of one internal variable.
22784 */
22785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022786list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022787{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788 char_u *tofree;
22789 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022790 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022791
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022792 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022793 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022794 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022795 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022796}
22797
Bram Moolenaar071d4272004-06-13 20:20:40 +000022798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022799list_one_var_a(
22800 char_u *prefix,
22801 char_u *name,
22802 int type,
22803 char_u *string,
22804 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022805{
Bram Moolenaar31859182007-08-14 20:41:13 +000022806 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22807 msg_start();
22808 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022809 if (name != NULL) /* "a:" vars don't have a name stored */
22810 msg_puts(name);
22811 msg_putchar(' ');
22812 msg_advance(22);
22813 if (type == VAR_NUMBER)
22814 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022815 else if (type == VAR_FUNC)
22816 msg_putchar('*');
22817 else if (type == VAR_LIST)
22818 {
22819 msg_putchar('[');
22820 if (*string == '[')
22821 ++string;
22822 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022823 else if (type == VAR_DICT)
22824 {
22825 msg_putchar('{');
22826 if (*string == '{')
22827 ++string;
22828 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 else
22830 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022831
Bram Moolenaar071d4272004-06-13 20:20:40 +000022832 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022833
22834 if (type == VAR_FUNC)
22835 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022836 if (*first)
22837 {
22838 msg_clr_eos();
22839 *first = FALSE;
22840 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022841}
22842
22843/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022844 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022845 * If the variable already exists, the value is updated.
22846 * Otherwise the variable is created.
22847 */
22848 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022849set_var(
22850 char_u *name,
22851 typval_T *tv,
22852 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022853{
Bram Moolenaar33570922005-01-25 22:26:29 +000022854 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022855 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022856 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022858 ht = find_var_ht(name, &varname);
22859 if (ht == NULL || *varname == NUL)
22860 {
22861 EMSG2(_(e_illvar), name);
22862 return;
22863 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022864 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022865
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022866 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22867 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022868
Bram Moolenaar33570922005-01-25 22:26:29 +000022869 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022871 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022872 if (var_check_ro(v->di_flags, name, FALSE)
22873 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022874 return;
22875 if (v->di_tv.v_type != tv->v_type
22876 && !((v->di_tv.v_type == VAR_STRING
22877 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022878 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022879 || tv->v_type == VAR_NUMBER))
22880#ifdef FEAT_FLOAT
22881 && !((v->di_tv.v_type == VAR_NUMBER
22882 || v->di_tv.v_type == VAR_FLOAT)
22883 && (tv->v_type == VAR_NUMBER
22884 || tv->v_type == VAR_FLOAT))
22885#endif
22886 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022887 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022888 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022889 return;
22890 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022891
22892 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022893 * Handle setting internal v: variables separately where needed to
22894 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022895 */
22896 if (ht == &vimvarht)
22897 {
22898 if (v->di_tv.v_type == VAR_STRING)
22899 {
22900 vim_free(v->di_tv.vval.v_string);
22901 if (copy || tv->v_type != VAR_STRING)
22902 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22903 else
22904 {
22905 /* Take over the string to avoid an extra alloc/free. */
22906 v->di_tv.vval.v_string = tv->vval.v_string;
22907 tv->vval.v_string = NULL;
22908 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022909 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022911 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022912 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022913 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022914 if (STRCMP(varname, "searchforward") == 0)
22915 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022916#ifdef FEAT_SEARCH_EXTRA
22917 else if (STRCMP(varname, "hlsearch") == 0)
22918 {
22919 no_hlsearch = !v->di_tv.vval.v_number;
22920 redraw_all_later(SOME_VALID);
22921 }
22922#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022923 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022924 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022925 else if (v->di_tv.v_type != tv->v_type)
22926 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 }
22928
22929 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022930 }
22931 else /* add a new variable */
22932 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022933 /* Can't add "v:" variable. */
22934 if (ht == &vimvarht)
22935 {
22936 EMSG2(_(e_illvar), name);
22937 return;
22938 }
22939
Bram Moolenaar92124a32005-06-17 22:03:40 +000022940 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022941 if (!valid_varname(varname))
22942 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022943
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022944 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22945 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022946 if (v == NULL)
22947 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022948 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022949 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022950 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022951 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 return;
22953 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022954 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022955 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022956
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022957 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022959 else
22960 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022961 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022962 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022963 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022965}
22966
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022967/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022968 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022969 * Also give an error message.
22970 */
22971 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022972var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022973{
22974 if (flags & DI_FLAGS_RO)
22975 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022976 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022977 return TRUE;
22978 }
22979 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22980 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022981 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022982 return TRUE;
22983 }
22984 return FALSE;
22985}
22986
22987/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022988 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22989 * Also give an error message.
22990 */
22991 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022992var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022993{
22994 if (flags & DI_FLAGS_FIX)
22995 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022996 EMSG2(_("E795: Cannot delete variable %s"),
22997 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022998 return TRUE;
22999 }
23000 return FALSE;
23001}
23002
23003/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023004 * Check if a funcref is assigned to a valid variable name.
23005 * Return TRUE and give an error if not.
23006 */
23007 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023008var_check_func_name(
23009 char_u *name, /* points to start of variable name */
23010 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023011{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023012 /* Allow for w: b: s: and t:. */
23013 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023014 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23015 ? name[2] : name[0]))
23016 {
23017 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23018 name);
23019 return TRUE;
23020 }
23021 /* Don't allow hiding a function. When "v" is not NULL we might be
23022 * assigning another function to the same var, the type is checked
23023 * below. */
23024 if (new_var && function_exists(name))
23025 {
23026 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23027 name);
23028 return TRUE;
23029 }
23030 return FALSE;
23031}
23032
23033/*
23034 * Check if a variable name is valid.
23035 * Return FALSE and give an error if not.
23036 */
23037 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023038valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023039{
23040 char_u *p;
23041
23042 for (p = varname; *p != NUL; ++p)
23043 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23044 && *p != AUTOLOAD_CHAR)
23045 {
23046 EMSG2(_(e_illvar), varname);
23047 return FALSE;
23048 }
23049 return TRUE;
23050}
23051
23052/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023053 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023054 * Also give an error message, using "name" or _("name") when use_gettext is
23055 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023056 */
23057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023058tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023059{
23060 if (lock & VAR_LOCKED)
23061 {
23062 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023063 name == NULL ? (char_u *)_("Unknown")
23064 : use_gettext ? (char_u *)_(name)
23065 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023066 return TRUE;
23067 }
23068 if (lock & VAR_FIXED)
23069 {
23070 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023071 name == NULL ? (char_u *)_("Unknown")
23072 : use_gettext ? (char_u *)_(name)
23073 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023074 return TRUE;
23075 }
23076 return FALSE;
23077}
23078
23079/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023080 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023081 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023082 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023083 * It is OK for "from" and "to" to point to the same item. This is used to
23084 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023085 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023086 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023087copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023089 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023090 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023091 switch (from->v_type)
23092 {
23093 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023094 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023095 to->vval.v_number = from->vval.v_number;
23096 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023097 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023098#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023099 to->vval.v_float = from->vval.v_float;
23100 break;
23101#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023102 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023103#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023104 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023105 if (to->vval.v_job != NULL)
23106 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023107 break;
23108#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023109 case VAR_CHANNEL:
23110#ifdef FEAT_CHANNEL
23111 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023112 if (to->vval.v_channel != NULL)
23113 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023114 break;
23115#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023116 case VAR_STRING:
23117 case VAR_FUNC:
23118 if (from->vval.v_string == NULL)
23119 to->vval.v_string = NULL;
23120 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023121 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023122 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023123 if (from->v_type == VAR_FUNC)
23124 func_ref(to->vval.v_string);
23125 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023126 break;
23127 case VAR_LIST:
23128 if (from->vval.v_list == NULL)
23129 to->vval.v_list = NULL;
23130 else
23131 {
23132 to->vval.v_list = from->vval.v_list;
23133 ++to->vval.v_list->lv_refcount;
23134 }
23135 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023136 case VAR_DICT:
23137 if (from->vval.v_dict == NULL)
23138 to->vval.v_dict = NULL;
23139 else
23140 {
23141 to->vval.v_dict = from->vval.v_dict;
23142 ++to->vval.v_dict->dv_refcount;
23143 }
23144 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023145 case VAR_UNKNOWN:
23146 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023147 break;
23148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149}
23150
23151/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023152 * Make a copy of an item.
23153 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023154 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23155 * reference to an already copied list/dict can be used.
23156 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023157 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023158 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023159item_copy(
23160 typval_T *from,
23161 typval_T *to,
23162 int deep,
23163 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023164{
23165 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023166 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023167
Bram Moolenaar33570922005-01-25 22:26:29 +000023168 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023169 {
23170 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023171 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023172 }
23173 ++recurse;
23174
23175 switch (from->v_type)
23176 {
23177 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023178 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023179 case VAR_STRING:
23180 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023181 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023182 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023183 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023184 copy_tv(from, to);
23185 break;
23186 case VAR_LIST:
23187 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023188 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023189 if (from->vval.v_list == NULL)
23190 to->vval.v_list = NULL;
23191 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23192 {
23193 /* use the copy made earlier */
23194 to->vval.v_list = from->vval.v_list->lv_copylist;
23195 ++to->vval.v_list->lv_refcount;
23196 }
23197 else
23198 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23199 if (to->vval.v_list == NULL)
23200 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023201 break;
23202 case VAR_DICT:
23203 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023204 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023205 if (from->vval.v_dict == NULL)
23206 to->vval.v_dict = NULL;
23207 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23208 {
23209 /* use the copy made earlier */
23210 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23211 ++to->vval.v_dict->dv_refcount;
23212 }
23213 else
23214 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23215 if (to->vval.v_dict == NULL)
23216 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023217 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023218 case VAR_UNKNOWN:
23219 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023220 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023221 }
23222 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023223 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023224}
23225
23226/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 * ":echo expr1 ..." print each argument separated with a space, add a
23228 * newline at the end.
23229 * ":echon expr1 ..." print each argument plain.
23230 */
23231 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023232ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023233{
23234 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023235 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023236 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023237 char_u *p;
23238 int needclr = TRUE;
23239 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023240 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241
23242 if (eap->skip)
23243 ++emsg_skip;
23244 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23245 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023246 /* If eval1() causes an error message the text from the command may
23247 * still need to be cleared. E.g., "echo 22,44". */
23248 need_clr_eos = needclr;
23249
Bram Moolenaar071d4272004-06-13 20:20:40 +000023250 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023251 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023252 {
23253 /*
23254 * Report the invalid expression unless the expression evaluation
23255 * has been cancelled due to an aborting error, an interrupt, or an
23256 * exception.
23257 */
23258 if (!aborting())
23259 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023260 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023261 break;
23262 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023263 need_clr_eos = FALSE;
23264
Bram Moolenaar071d4272004-06-13 20:20:40 +000023265 if (!eap->skip)
23266 {
23267 if (atstart)
23268 {
23269 atstart = FALSE;
23270 /* Call msg_start() after eval1(), evaluating the expression
23271 * may cause a message to appear. */
23272 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023273 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023274 /* Mark the saved text as finishing the line, so that what
23275 * follows is displayed on a new line when scrolling back
23276 * at the more prompt. */
23277 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023278 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023279 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 }
23281 else if (eap->cmdidx == CMD_echo)
23282 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023283 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023284 if (p != NULL)
23285 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023286 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023287 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023289 if (*p != TAB && needclr)
23290 {
23291 /* remove any text still there from the command */
23292 msg_clr_eos();
23293 needclr = FALSE;
23294 }
23295 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023296 }
23297 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023298 {
23299#ifdef FEAT_MBYTE
23300 if (has_mbyte)
23301 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023302 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023303
23304 (void)msg_outtrans_len_attr(p, i, echo_attr);
23305 p += i - 1;
23306 }
23307 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023309 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23310 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023312 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023314 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 arg = skipwhite(arg);
23316 }
23317 eap->nextcmd = check_nextcmd(arg);
23318
23319 if (eap->skip)
23320 --emsg_skip;
23321 else
23322 {
23323 /* remove text that may still be there from the command */
23324 if (needclr)
23325 msg_clr_eos();
23326 if (eap->cmdidx == CMD_echo)
23327 msg_end();
23328 }
23329}
23330
23331/*
23332 * ":echohl {name}".
23333 */
23334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023335ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023336{
23337 int id;
23338
23339 id = syn_name2id(eap->arg);
23340 if (id == 0)
23341 echo_attr = 0;
23342 else
23343 echo_attr = syn_id2attr(id);
23344}
23345
23346/*
23347 * ":execute expr1 ..." execute the result of an expression.
23348 * ":echomsg expr1 ..." Print a message
23349 * ":echoerr expr1 ..." Print an error
23350 * Each gets spaces around each argument and a newline at the end for
23351 * echo commands
23352 */
23353 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023354ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355{
23356 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023357 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358 int ret = OK;
23359 char_u *p;
23360 garray_T ga;
23361 int len;
23362 int save_did_emsg;
23363
23364 ga_init2(&ga, 1, 80);
23365
23366 if (eap->skip)
23367 ++emsg_skip;
23368 while (*arg != NUL && *arg != '|' && *arg != '\n')
23369 {
23370 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023371 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023372 {
23373 /*
23374 * Report the invalid expression unless the expression evaluation
23375 * has been cancelled due to an aborting error, an interrupt, or an
23376 * exception.
23377 */
23378 if (!aborting())
23379 EMSG2(_(e_invexpr2), p);
23380 ret = FAIL;
23381 break;
23382 }
23383
23384 if (!eap->skip)
23385 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023386 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023387 len = (int)STRLEN(p);
23388 if (ga_grow(&ga, len + 2) == FAIL)
23389 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023390 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023391 ret = FAIL;
23392 break;
23393 }
23394 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023396 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023397 ga.ga_len += len;
23398 }
23399
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023400 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023401 arg = skipwhite(arg);
23402 }
23403
23404 if (ret != FAIL && ga.ga_data != NULL)
23405 {
23406 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023407 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023409 out_flush();
23410 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 else if (eap->cmdidx == CMD_echoerr)
23412 {
23413 /* We don't want to abort following commands, restore did_emsg. */
23414 save_did_emsg = did_emsg;
23415 EMSG((char_u *)ga.ga_data);
23416 if (!force_abort)
23417 did_emsg = save_did_emsg;
23418 }
23419 else if (eap->cmdidx == CMD_execute)
23420 do_cmdline((char_u *)ga.ga_data,
23421 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23422 }
23423
23424 ga_clear(&ga);
23425
23426 if (eap->skip)
23427 --emsg_skip;
23428
23429 eap->nextcmd = check_nextcmd(arg);
23430}
23431
23432/*
23433 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23434 * "arg" points to the "&" or '+' when called, to "option" when returning.
23435 * Returns NULL when no option name found. Otherwise pointer to the char
23436 * after the option name.
23437 */
23438 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023439find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023440{
23441 char_u *p = *arg;
23442
23443 ++p;
23444 if (*p == 'g' && p[1] == ':')
23445 {
23446 *opt_flags = OPT_GLOBAL;
23447 p += 2;
23448 }
23449 else if (*p == 'l' && p[1] == ':')
23450 {
23451 *opt_flags = OPT_LOCAL;
23452 p += 2;
23453 }
23454 else
23455 *opt_flags = 0;
23456
23457 if (!ASCII_ISALPHA(*p))
23458 return NULL;
23459 *arg = p;
23460
23461 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23462 p += 4; /* termcap option */
23463 else
23464 while (ASCII_ISALPHA(*p))
23465 ++p;
23466 return p;
23467}
23468
23469/*
23470 * ":function"
23471 */
23472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023473ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474{
23475 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023476 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 int j;
23478 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023480 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023481 char_u *name = NULL;
23482 char_u *p;
23483 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023484 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023485 garray_T newargs;
23486 garray_T newlines;
23487 int varargs = FALSE;
23488 int mustend = FALSE;
23489 int flags = 0;
23490 ufunc_T *fp;
23491 int indent;
23492 int nesting;
23493 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023494 dictitem_T *v;
23495 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023496 static int func_nr = 0; /* number for nameless function */
23497 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023498 hashtab_T *ht;
23499 int todo;
23500 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023501 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023502
23503 /*
23504 * ":function" without argument: list functions.
23505 */
23506 if (ends_excmd(*eap->arg))
23507 {
23508 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023509 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023510 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023511 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023512 {
23513 if (!HASHITEM_EMPTY(hi))
23514 {
23515 --todo;
23516 fp = HI2UF(hi);
23517 if (!isdigit(*fp->uf_name))
23518 list_func_head(fp, FALSE);
23519 }
23520 }
23521 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023522 eap->nextcmd = check_nextcmd(eap->arg);
23523 return;
23524 }
23525
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023526 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023527 * ":function /pat": list functions matching pattern.
23528 */
23529 if (*eap->arg == '/')
23530 {
23531 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23532 if (!eap->skip)
23533 {
23534 regmatch_T regmatch;
23535
23536 c = *p;
23537 *p = NUL;
23538 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23539 *p = c;
23540 if (regmatch.regprog != NULL)
23541 {
23542 regmatch.rm_ic = p_ic;
23543
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023544 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023545 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23546 {
23547 if (!HASHITEM_EMPTY(hi))
23548 {
23549 --todo;
23550 fp = HI2UF(hi);
23551 if (!isdigit(*fp->uf_name)
23552 && vim_regexec(&regmatch, fp->uf_name, 0))
23553 list_func_head(fp, FALSE);
23554 }
23555 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023556 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023557 }
23558 }
23559 if (*p == '/')
23560 ++p;
23561 eap->nextcmd = check_nextcmd(p);
23562 return;
23563 }
23564
23565 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023566 * Get the function name. There are these situations:
23567 * func normal function name
23568 * "name" == func, "fudi.fd_dict" == NULL
23569 * dict.func new dictionary entry
23570 * "name" == NULL, "fudi.fd_dict" set,
23571 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23572 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023573 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023574 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23575 * dict.func existing dict entry that's not a Funcref
23576 * "name" == NULL, "fudi.fd_dict" set,
23577 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023578 * s:func script-local function name
23579 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023580 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023581 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023582 name = trans_function_name(&p, eap->skip, 0, &fudi);
23583 paren = (vim_strchr(p, '(') != NULL);
23584 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023585 {
23586 /*
23587 * Return on an invalid expression in braces, unless the expression
23588 * evaluation has been cancelled due to an aborting error, an
23589 * interrupt, or an exception.
23590 */
23591 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023592 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023593 if (!eap->skip && fudi.fd_newkey != NULL)
23594 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023595 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023596 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023598 else
23599 eap->skip = TRUE;
23600 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023601
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 /* An error in a function call during evaluation of an expression in magic
23603 * braces should not cause the function not to be defined. */
23604 saved_did_emsg = did_emsg;
23605 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023606
23607 /*
23608 * ":function func" with only function name: list function.
23609 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023610 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023611 {
23612 if (!ends_excmd(*skipwhite(p)))
23613 {
23614 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023615 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 }
23617 eap->nextcmd = check_nextcmd(p);
23618 if (eap->nextcmd != NULL)
23619 *p = NUL;
23620 if (!eap->skip && !got_int)
23621 {
23622 fp = find_func(name);
23623 if (fp != NULL)
23624 {
23625 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023626 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023628 if (FUNCLINE(fp, j) == NULL)
23629 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023630 msg_putchar('\n');
23631 msg_outnum((long)(j + 1));
23632 if (j < 9)
23633 msg_putchar(' ');
23634 if (j < 99)
23635 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023636 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 out_flush(); /* show a line at a time */
23638 ui_breakcheck();
23639 }
23640 if (!got_int)
23641 {
23642 msg_putchar('\n');
23643 msg_puts((char_u *)" endfunction");
23644 }
23645 }
23646 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023647 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023648 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023649 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 }
23651
23652 /*
23653 * ":function name(arg1, arg2)" Define function.
23654 */
23655 p = skipwhite(p);
23656 if (*p != '(')
23657 {
23658 if (!eap->skip)
23659 {
23660 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023661 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023662 }
23663 /* attempt to continue by skipping some text */
23664 if (vim_strchr(p, '(') != NULL)
23665 p = vim_strchr(p, '(');
23666 }
23667 p = skipwhite(p + 1);
23668
23669 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23670 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23671
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023672 if (!eap->skip)
23673 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023674 /* Check the name of the function. Unless it's a dictionary function
23675 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023676 if (name != NULL)
23677 arg = name;
23678 else
23679 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023680 if (arg != NULL && (fudi.fd_di == NULL
23681 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023682 {
23683 if (*arg == K_SPECIAL)
23684 j = 3;
23685 else
23686 j = 0;
23687 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23688 : eval_isnamec(arg[j])))
23689 ++j;
23690 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023691 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023692 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023693 /* Disallow using the g: dict. */
23694 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23695 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023696 }
23697
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 /*
23699 * Isolate the arguments: "arg1, arg2, ...)"
23700 */
23701 while (*p != ')')
23702 {
23703 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23704 {
23705 varargs = TRUE;
23706 p += 3;
23707 mustend = TRUE;
23708 }
23709 else
23710 {
23711 arg = p;
23712 while (ASCII_ISALNUM(*p) || *p == '_')
23713 ++p;
23714 if (arg == p || isdigit(*arg)
23715 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23716 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23717 {
23718 if (!eap->skip)
23719 EMSG2(_("E125: Illegal argument: %s"), arg);
23720 break;
23721 }
23722 if (ga_grow(&newargs, 1) == FAIL)
23723 goto erret;
23724 c = *p;
23725 *p = NUL;
23726 arg = vim_strsave(arg);
23727 if (arg == NULL)
23728 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023729
23730 /* Check for duplicate argument name. */
23731 for (i = 0; i < newargs.ga_len; ++i)
23732 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23733 {
23734 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023735 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023736 goto erret;
23737 }
23738
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23740 *p = c;
23741 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023742 if (*p == ',')
23743 ++p;
23744 else
23745 mustend = TRUE;
23746 }
23747 p = skipwhite(p);
23748 if (mustend && *p != ')')
23749 {
23750 if (!eap->skip)
23751 EMSG2(_(e_invarg2), eap->arg);
23752 break;
23753 }
23754 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023755 if (*p != ')')
23756 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 ++p; /* skip the ')' */
23758
Bram Moolenaare9a41262005-01-15 22:18:47 +000023759 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023760 for (;;)
23761 {
23762 p = skipwhite(p);
23763 if (STRNCMP(p, "range", 5) == 0)
23764 {
23765 flags |= FC_RANGE;
23766 p += 5;
23767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023768 else if (STRNCMP(p, "dict", 4) == 0)
23769 {
23770 flags |= FC_DICT;
23771 p += 4;
23772 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 else if (STRNCMP(p, "abort", 5) == 0)
23774 {
23775 flags |= FC_ABORT;
23776 p += 5;
23777 }
23778 else
23779 break;
23780 }
23781
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023782 /* When there is a line break use what follows for the function body.
23783 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23784 if (*p == '\n')
23785 line_arg = p + 1;
23786 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023787 EMSG(_(e_trailing));
23788
23789 /*
23790 * Read the body of the function, until ":endfunction" is found.
23791 */
23792 if (KeyTyped)
23793 {
23794 /* Check if the function already exists, don't let the user type the
23795 * whole function before telling him it doesn't work! For a script we
23796 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023797 if (!eap->skip && !eap->forceit)
23798 {
23799 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23800 EMSG(_(e_funcdict));
23801 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023802 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023804
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023805 if (!eap->skip && did_emsg)
23806 goto erret;
23807
Bram Moolenaar071d4272004-06-13 20:20:40 +000023808 msg_putchar('\n'); /* don't overwrite the function name */
23809 cmdline_row = msg_row;
23810 }
23811
23812 indent = 2;
23813 nesting = 0;
23814 for (;;)
23815 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023816 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023817 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023818 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023819 saved_wait_return = FALSE;
23820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023821 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023822 sourcing_lnum_off = sourcing_lnum;
23823
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023824 if (line_arg != NULL)
23825 {
23826 /* Use eap->arg, split up in parts by line breaks. */
23827 theline = line_arg;
23828 p = vim_strchr(theline, '\n');
23829 if (p == NULL)
23830 line_arg += STRLEN(line_arg);
23831 else
23832 {
23833 *p = NUL;
23834 line_arg = p + 1;
23835 }
23836 }
23837 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023838 theline = getcmdline(':', 0L, indent);
23839 else
23840 theline = eap->getline(':', eap->cookie, indent);
23841 if (KeyTyped)
23842 lines_left = Rows - 1;
23843 if (theline == NULL)
23844 {
23845 EMSG(_("E126: Missing :endfunction"));
23846 goto erret;
23847 }
23848
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023849 /* Detect line continuation: sourcing_lnum increased more than one. */
23850 if (sourcing_lnum > sourcing_lnum_off + 1)
23851 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23852 else
23853 sourcing_lnum_off = 0;
23854
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 if (skip_until != NULL)
23856 {
23857 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23858 * don't check for ":endfunc". */
23859 if (STRCMP(theline, skip_until) == 0)
23860 {
23861 vim_free(skip_until);
23862 skip_until = NULL;
23863 }
23864 }
23865 else
23866 {
23867 /* skip ':' and blanks*/
23868 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23869 ;
23870
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023871 /* Check for "endfunction". */
23872 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023874 if (line_arg == NULL)
23875 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023876 break;
23877 }
23878
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023879 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023880 * at "end". */
23881 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23882 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023883 else if (STRNCMP(p, "if", 2) == 0
23884 || STRNCMP(p, "wh", 2) == 0
23885 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 || STRNCMP(p, "try", 3) == 0)
23887 indent += 2;
23888
23889 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023890 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023892 if (*p == '!')
23893 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023895 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23896 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023898 ++nesting;
23899 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023900 }
23901 }
23902
23903 /* Check for ":append" or ":insert". */
23904 p = skip_range(p, NULL);
23905 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23906 || (p[0] == 'i'
23907 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23908 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23909 skip_until = vim_strsave((char_u *)".");
23910
23911 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23912 arg = skipwhite(skiptowhite(p));
23913 if (arg[0] == '<' && arg[1] =='<'
23914 && ((p[0] == 'p' && p[1] == 'y'
23915 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23916 || (p[0] == 'p' && p[1] == 'e'
23917 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23918 || (p[0] == 't' && p[1] == 'c'
23919 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023920 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23921 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23923 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023924 || (p[0] == 'm' && p[1] == 'z'
23925 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023926 ))
23927 {
23928 /* ":python <<" continues until a dot, like ":append" */
23929 p = skipwhite(arg + 2);
23930 if (*p == NUL)
23931 skip_until = vim_strsave((char_u *)".");
23932 else
23933 skip_until = vim_strsave(p);
23934 }
23935 }
23936
23937 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023938 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023939 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023940 if (line_arg == NULL)
23941 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023943 }
23944
23945 /* Copy the line to newly allocated memory. get_one_sourceline()
23946 * allocates 250 bytes per line, this saves 80% on average. The cost
23947 * is an extra alloc/free. */
23948 p = vim_strsave(theline);
23949 if (p != NULL)
23950 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023951 if (line_arg == NULL)
23952 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023953 theline = p;
23954 }
23955
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023956 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23957
23958 /* Add NULL lines for continuation lines, so that the line count is
23959 * equal to the index in the growarray. */
23960 while (sourcing_lnum_off-- > 0)
23961 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023962
23963 /* Check for end of eap->arg. */
23964 if (line_arg != NULL && *line_arg == NUL)
23965 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023966 }
23967
23968 /* Don't define the function when skipping commands or when an error was
23969 * detected. */
23970 if (eap->skip || did_emsg)
23971 goto erret;
23972
23973 /*
23974 * If there are no errors, add the function
23975 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023976 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023977 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023978 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023979 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023980 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023981 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023982 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023983 goto erret;
23984 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023985
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023986 fp = find_func(name);
23987 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023988 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989 if (!eap->forceit)
23990 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023991 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023992 goto erret;
23993 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023994 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023995 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023996 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023997 name);
23998 goto erret;
23999 }
24000 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024001 ga_clear_strings(&(fp->uf_args));
24002 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024003 vim_free(name);
24004 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 }
24007 else
24008 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024009 char numbuf[20];
24010
24011 fp = NULL;
24012 if (fudi.fd_newkey == NULL && !eap->forceit)
24013 {
24014 EMSG(_(e_funcdict));
24015 goto erret;
24016 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024017 if (fudi.fd_di == NULL)
24018 {
24019 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024020 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024021 goto erret;
24022 }
24023 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024024 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024025 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024026
24027 /* Give the function a sequential number. Can only be used with a
24028 * Funcref! */
24029 vim_free(name);
24030 sprintf(numbuf, "%d", ++func_nr);
24031 name = vim_strsave((char_u *)numbuf);
24032 if (name == NULL)
24033 goto erret;
24034 }
24035
24036 if (fp == NULL)
24037 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024038 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024039 {
24040 int slen, plen;
24041 char_u *scriptname;
24042
24043 /* Check that the autoload name matches the script name. */
24044 j = FAIL;
24045 if (sourcing_name != NULL)
24046 {
24047 scriptname = autoload_name(name);
24048 if (scriptname != NULL)
24049 {
24050 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024051 plen = (int)STRLEN(p);
24052 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024053 if (slen > plen && fnamecmp(p,
24054 sourcing_name + slen - plen) == 0)
24055 j = OK;
24056 vim_free(scriptname);
24057 }
24058 }
24059 if (j == FAIL)
24060 {
24061 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24062 goto erret;
24063 }
24064 }
24065
24066 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024067 if (fp == NULL)
24068 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024069
24070 if (fudi.fd_dict != NULL)
24071 {
24072 if (fudi.fd_di == NULL)
24073 {
24074 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024075 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024076 if (fudi.fd_di == NULL)
24077 {
24078 vim_free(fp);
24079 goto erret;
24080 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024081 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24082 {
24083 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024084 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024085 goto erret;
24086 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024087 }
24088 else
24089 /* overwrite existing dict entry */
24090 clear_tv(&fudi.fd_di->di_tv);
24091 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024092 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024093 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024094 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024095
24096 /* behave like "dict" was used */
24097 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024098 }
24099
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024101 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024102 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24103 {
24104 vim_free(fp);
24105 goto erret;
24106 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024107 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024108 fp->uf_args = newargs;
24109 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024110#ifdef FEAT_PROFILE
24111 fp->uf_tml_count = NULL;
24112 fp->uf_tml_total = NULL;
24113 fp->uf_tml_self = NULL;
24114 fp->uf_profiling = FALSE;
24115 if (prof_def_func())
24116 func_do_profile(fp);
24117#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024118 fp->uf_varargs = varargs;
24119 fp->uf_flags = flags;
24120 fp->uf_calls = 0;
24121 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024122 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024123
24124erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024125 ga_clear_strings(&newargs);
24126 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024127ret_free:
24128 vim_free(skip_until);
24129 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024132 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024133}
24134
24135/*
24136 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024137 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024139 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024140 * TFN_INT: internal function name OK
24141 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024142 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024143 * Advances "pp" to just after the function name (if no error).
24144 */
24145 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146trans_function_name(
24147 char_u **pp,
24148 int skip, /* only find the end, don't evaluate */
24149 int flags,
24150 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024151{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024152 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024153 char_u *start;
24154 char_u *end;
24155 int lead;
24156 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024157 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024158 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024159
24160 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024161 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024162 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024163
24164 /* Check for hard coded <SNR>: already translated function ID (from a user
24165 * command). */
24166 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24167 && (*pp)[2] == (int)KE_SNR)
24168 {
24169 *pp += 3;
24170 len = get_id_len(pp) + 3;
24171 return vim_strnsave(start, len);
24172 }
24173
24174 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24175 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024176 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024177 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024178 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024179
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024180 /* Note that TFN_ flags use the same values as GLV_ flags. */
24181 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024182 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024183 if (end == start)
24184 {
24185 if (!skip)
24186 EMSG(_("E129: Function name required"));
24187 goto theend;
24188 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024189 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024190 {
24191 /*
24192 * Report an invalid expression in braces, unless the expression
24193 * evaluation has been cancelled due to an aborting error, an
24194 * interrupt, or an exception.
24195 */
24196 if (!aborting())
24197 {
24198 if (end != NULL)
24199 EMSG2(_(e_invarg2), start);
24200 }
24201 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024202 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024203 goto theend;
24204 }
24205
24206 if (lv.ll_tv != NULL)
24207 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024208 if (fdp != NULL)
24209 {
24210 fdp->fd_dict = lv.ll_dict;
24211 fdp->fd_newkey = lv.ll_newkey;
24212 lv.ll_newkey = NULL;
24213 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024214 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024215 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24216 {
24217 name = vim_strsave(lv.ll_tv->vval.v_string);
24218 *pp = end;
24219 }
24220 else
24221 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024222 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24223 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024224 EMSG(_(e_funcref));
24225 else
24226 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024227 name = NULL;
24228 }
24229 goto theend;
24230 }
24231
24232 if (lv.ll_name == NULL)
24233 {
24234 /* Error found, but continue after the function name. */
24235 *pp = end;
24236 goto theend;
24237 }
24238
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024239 /* Check if the name is a Funcref. If so, use the value. */
24240 if (lv.ll_exp_name != NULL)
24241 {
24242 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024243 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024244 if (name == lv.ll_exp_name)
24245 name = NULL;
24246 }
24247 else
24248 {
24249 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024250 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024251 if (name == *pp)
24252 name = NULL;
24253 }
24254 if (name != NULL)
24255 {
24256 name = vim_strsave(name);
24257 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024258 if (STRNCMP(name, "<SNR>", 5) == 0)
24259 {
24260 /* Change "<SNR>" to the byte sequence. */
24261 name[0] = K_SPECIAL;
24262 name[1] = KS_EXTRA;
24263 name[2] = (int)KE_SNR;
24264 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24265 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024266 goto theend;
24267 }
24268
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024269 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024270 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024271 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024272 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24273 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24274 {
24275 /* When there was "s:" already or the name expanded to get a
24276 * leading "s:" then remove it. */
24277 lv.ll_name += 2;
24278 len -= 2;
24279 lead = 2;
24280 }
24281 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024282 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024283 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024284 /* skip over "s:" and "g:" */
24285 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024286 lv.ll_name += 2;
24287 len = (int)(end - lv.ll_name);
24288 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024289
24290 /*
24291 * Copy the function name to allocated memory.
24292 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24293 * Accept <SNR>123_name() outside a script.
24294 */
24295 if (skip)
24296 lead = 0; /* do nothing */
24297 else if (lead > 0)
24298 {
24299 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024300 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24301 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024302 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024303 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024304 if (current_SID <= 0)
24305 {
24306 EMSG(_(e_usingsid));
24307 goto theend;
24308 }
24309 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24310 lead += (int)STRLEN(sid_buf);
24311 }
24312 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024313 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024314 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024315 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024316 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024317 goto theend;
24318 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024319 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024320 {
24321 char_u *cp = vim_strchr(lv.ll_name, ':');
24322
24323 if (cp != NULL && cp < end)
24324 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024325 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024326 goto theend;
24327 }
24328 }
24329
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024330 name = alloc((unsigned)(len + lead + 1));
24331 if (name != NULL)
24332 {
24333 if (lead > 0)
24334 {
24335 name[0] = K_SPECIAL;
24336 name[1] = KS_EXTRA;
24337 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024338 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024339 STRCPY(name + 3, sid_buf);
24340 }
24341 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024342 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024343 }
24344 *pp = end;
24345
24346theend:
24347 clear_lval(&lv);
24348 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024349}
24350
24351/*
24352 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24353 * Return 2 if "p" starts with "s:".
24354 * Return 0 otherwise.
24355 */
24356 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024357eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024358{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024359 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24360 * the standard library function. */
24361 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24362 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024363 return 5;
24364 if (p[0] == 's' && p[1] == ':')
24365 return 2;
24366 return 0;
24367}
24368
24369/*
24370 * Return TRUE if "p" starts with "<SID>" or "s:".
24371 * Only works if eval_fname_script() returned non-zero for "p"!
24372 */
24373 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024374eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024375{
24376 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24377}
24378
24379/*
24380 * List the head of the function: "name(arg1, arg2)".
24381 */
24382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024383list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024384{
24385 int j;
24386
24387 msg_start();
24388 if (indent)
24389 MSG_PUTS(" ");
24390 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024391 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 {
24393 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024394 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 }
24396 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024397 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024398 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024399 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024400 {
24401 if (j)
24402 MSG_PUTS(", ");
24403 msg_puts(FUNCARG(fp, j));
24404 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024405 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024406 {
24407 if (j)
24408 MSG_PUTS(", ");
24409 MSG_PUTS("...");
24410 }
24411 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024412 if (fp->uf_flags & FC_ABORT)
24413 MSG_PUTS(" abort");
24414 if (fp->uf_flags & FC_RANGE)
24415 MSG_PUTS(" range");
24416 if (fp->uf_flags & FC_DICT)
24417 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024418 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024419 if (p_verbose > 0)
24420 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024421}
24422
24423/*
24424 * Find a function by name, return pointer to it in ufuncs.
24425 * Return NULL for unknown function.
24426 */
24427 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024428find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024429{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024430 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024431
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024432 hi = hash_find(&func_hashtab, name);
24433 if (!HASHITEM_EMPTY(hi))
24434 return HI2UF(hi);
24435 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024436}
24437
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024438#if defined(EXITFREE) || defined(PROTO)
24439 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024440free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024441{
24442 hashitem_T *hi;
24443
24444 /* Need to start all over every time, because func_free() may change the
24445 * hash table. */
24446 while (func_hashtab.ht_used > 0)
24447 for (hi = func_hashtab.ht_array; ; ++hi)
24448 if (!HASHITEM_EMPTY(hi))
24449 {
24450 func_free(HI2UF(hi));
24451 break;
24452 }
24453}
24454#endif
24455
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024456 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024457translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024458{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024459 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024460 return find_internal_func(name) >= 0;
24461 return find_func(name) != NULL;
24462}
24463
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024464/*
24465 * Return TRUE if a function "name" exists.
24466 */
24467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024468function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024469{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024470 char_u *nm = name;
24471 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024472 int n = FALSE;
24473
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024474 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24475 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024476 nm = skipwhite(nm);
24477
24478 /* Only accept "funcname", "funcname ", "funcname (..." and
24479 * "funcname(...", not "funcname!...". */
24480 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024481 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024482 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024483 return n;
24484}
24485
Bram Moolenaara1544c02013-05-30 12:35:52 +020024486 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024487get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024488{
24489 char_u *nm = name;
24490 char_u *p;
24491
24492 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24493
24494 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024495 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024496 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024497
Bram Moolenaara1544c02013-05-30 12:35:52 +020024498 vim_free(p);
24499 return NULL;
24500}
24501
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024502/*
24503 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024504 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24505 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024506 */
24507 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024508builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024509{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024510 char_u *p;
24511
24512 if (!ASCII_ISLOWER(name[0]))
24513 return FALSE;
24514 p = vim_strchr(name, AUTOLOAD_CHAR);
24515 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024516}
24517
Bram Moolenaar05159a02005-02-26 23:04:13 +000024518#if defined(FEAT_PROFILE) || defined(PROTO)
24519/*
24520 * Start profiling function "fp".
24521 */
24522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024523func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024524{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024525 int len = fp->uf_lines.ga_len;
24526
24527 if (len == 0)
24528 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024529 fp->uf_tm_count = 0;
24530 profile_zero(&fp->uf_tm_self);
24531 profile_zero(&fp->uf_tm_total);
24532 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024533 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024534 if (fp->uf_tml_total == NULL)
24535 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024536 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024537 if (fp->uf_tml_self == NULL)
24538 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024539 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024540 fp->uf_tml_idx = -1;
24541 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24542 || fp->uf_tml_self == NULL)
24543 return; /* out of memory */
24544
24545 fp->uf_profiling = TRUE;
24546}
24547
24548/*
24549 * Dump the profiling results for all functions in file "fd".
24550 */
24551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024552func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024553{
24554 hashitem_T *hi;
24555 int todo;
24556 ufunc_T *fp;
24557 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024558 ufunc_T **sorttab;
24559 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024560
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024561 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024562 if (todo == 0)
24563 return; /* nothing to dump */
24564
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024565 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024566
Bram Moolenaar05159a02005-02-26 23:04:13 +000024567 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24568 {
24569 if (!HASHITEM_EMPTY(hi))
24570 {
24571 --todo;
24572 fp = HI2UF(hi);
24573 if (fp->uf_profiling)
24574 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024575 if (sorttab != NULL)
24576 sorttab[st_len++] = fp;
24577
Bram Moolenaar05159a02005-02-26 23:04:13 +000024578 if (fp->uf_name[0] == K_SPECIAL)
24579 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24580 else
24581 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24582 if (fp->uf_tm_count == 1)
24583 fprintf(fd, "Called 1 time\n");
24584 else
24585 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24586 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24587 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24588 fprintf(fd, "\n");
24589 fprintf(fd, "count total (s) self (s)\n");
24590
24591 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24592 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024593 if (FUNCLINE(fp, i) == NULL)
24594 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024595 prof_func_line(fd, fp->uf_tml_count[i],
24596 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024597 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24598 }
24599 fprintf(fd, "\n");
24600 }
24601 }
24602 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024603
24604 if (sorttab != NULL && st_len > 0)
24605 {
24606 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24607 prof_total_cmp);
24608 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24609 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24610 prof_self_cmp);
24611 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24612 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024613
24614 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024615}
Bram Moolenaar73830342005-02-28 22:48:19 +000024616
24617 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024618prof_sort_list(
24619 FILE *fd,
24620 ufunc_T **sorttab,
24621 int st_len,
24622 char *title,
24623 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024624{
24625 int i;
24626 ufunc_T *fp;
24627
24628 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24629 fprintf(fd, "count total (s) self (s) function\n");
24630 for (i = 0; i < 20 && i < st_len; ++i)
24631 {
24632 fp = sorttab[i];
24633 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24634 prefer_self);
24635 if (fp->uf_name[0] == K_SPECIAL)
24636 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24637 else
24638 fprintf(fd, " %s()\n", fp->uf_name);
24639 }
24640 fprintf(fd, "\n");
24641}
24642
24643/*
24644 * Print the count and times for one function or function line.
24645 */
24646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024647prof_func_line(
24648 FILE *fd,
24649 int count,
24650 proftime_T *total,
24651 proftime_T *self,
24652 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024653{
24654 if (count > 0)
24655 {
24656 fprintf(fd, "%5d ", count);
24657 if (prefer_self && profile_equal(total, self))
24658 fprintf(fd, " ");
24659 else
24660 fprintf(fd, "%s ", profile_msg(total));
24661 if (!prefer_self && profile_equal(total, self))
24662 fprintf(fd, " ");
24663 else
24664 fprintf(fd, "%s ", profile_msg(self));
24665 }
24666 else
24667 fprintf(fd, " ");
24668}
24669
24670/*
24671 * Compare function for total time sorting.
24672 */
24673 static int
24674#ifdef __BORLANDC__
24675_RTLENTRYF
24676#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024677prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024678{
24679 ufunc_T *p1, *p2;
24680
24681 p1 = *(ufunc_T **)s1;
24682 p2 = *(ufunc_T **)s2;
24683 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24684}
24685
24686/*
24687 * Compare function for self time sorting.
24688 */
24689 static int
24690#ifdef __BORLANDC__
24691_RTLENTRYF
24692#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024693prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024694{
24695 ufunc_T *p1, *p2;
24696
24697 p1 = *(ufunc_T **)s1;
24698 p2 = *(ufunc_T **)s2;
24699 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24700}
24701
Bram Moolenaar05159a02005-02-26 23:04:13 +000024702#endif
24703
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024704/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024706 * Return TRUE if a package was loaded.
24707 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024709script_autoload(
24710 char_u *name,
24711 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024712{
24713 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024714 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024715 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024716 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024717
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024718 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024719 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024720 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024721 return FALSE;
24722
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024723 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024724
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024725 /* Find the name in the list of previously loaded package names. Skip
24726 * "autoload/", it's always the same. */
24727 for (i = 0; i < ga_loaded.ga_len; ++i)
24728 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24729 break;
24730 if (!reload && i < ga_loaded.ga_len)
24731 ret = FALSE; /* was loaded already */
24732 else
24733 {
24734 /* Remember the name if it wasn't loaded already. */
24735 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24736 {
24737 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24738 tofree = NULL;
24739 }
24740
24741 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024742 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024743 ret = TRUE;
24744 }
24745
24746 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024747 return ret;
24748}
24749
24750/*
24751 * Return the autoload script name for a function or variable name.
24752 * Returns NULL when out of memory.
24753 */
24754 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024755autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024756{
24757 char_u *p;
24758 char_u *scriptname;
24759
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024760 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024761 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24762 if (scriptname == NULL)
24763 return FALSE;
24764 STRCPY(scriptname, "autoload/");
24765 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024766 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024767 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024768 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024769 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024770 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024771}
24772
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24774
24775/*
24776 * Function given to ExpandGeneric() to obtain the list of user defined
24777 * function names.
24778 */
24779 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024780get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024781{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024782 static long_u done;
24783 static hashitem_T *hi;
24784 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024785
24786 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024787 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024788 done = 0;
24789 hi = func_hashtab.ht_array;
24790 }
24791 if (done < func_hashtab.ht_used)
24792 {
24793 if (done++ > 0)
24794 ++hi;
24795 while (HASHITEM_EMPTY(hi))
24796 ++hi;
24797 fp = HI2UF(hi);
24798
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024799 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024800 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024801
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024802 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24803 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804
24805 cat_func_name(IObuff, fp);
24806 if (xp->xp_context != EXPAND_USER_FUNC)
24807 {
24808 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024809 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024810 STRCAT(IObuff, ")");
24811 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024812 return IObuff;
24813 }
24814 return NULL;
24815}
24816
24817#endif /* FEAT_CMDL_COMPL */
24818
24819/*
24820 * Copy the function name of "fp" to buffer "buf".
24821 * "buf" must be able to hold the function name plus three bytes.
24822 * Takes care of script-local function names.
24823 */
24824 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024825cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024826{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024827 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 {
24829 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831 }
24832 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024833 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834}
24835
24836/*
24837 * ":delfunction {name}"
24838 */
24839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024840ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024842 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843 char_u *p;
24844 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846
24847 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024848 name = trans_function_name(&p, eap->skip, 0, &fudi);
24849 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024850 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024851 {
24852 if (fudi.fd_dict != NULL && !eap->skip)
24853 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024855 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 if (!ends_excmd(*skipwhite(p)))
24857 {
24858 vim_free(name);
24859 EMSG(_(e_trailing));
24860 return;
24861 }
24862 eap->nextcmd = check_nextcmd(p);
24863 if (eap->nextcmd != NULL)
24864 *p = NUL;
24865
24866 if (!eap->skip)
24867 fp = find_func(name);
24868 vim_free(name);
24869
24870 if (!eap->skip)
24871 {
24872 if (fp == NULL)
24873 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024874 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 return;
24876 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024877 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024878 {
24879 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24880 return;
24881 }
24882
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024883 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024884 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024885 /* Delete the dict item that refers to the function, it will
24886 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024887 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024889 else
24890 func_free(fp);
24891 }
24892}
24893
24894/*
24895 * Free a function and remove it from the list of functions.
24896 */
24897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024898func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024899{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024900 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024901
24902 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024903 ga_clear_strings(&(fp->uf_args));
24904 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024905#ifdef FEAT_PROFILE
24906 vim_free(fp->uf_tml_count);
24907 vim_free(fp->uf_tml_total);
24908 vim_free(fp->uf_tml_self);
24909#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024910
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024911 /* remove the function from the function hashtable */
24912 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24913 if (HASHITEM_EMPTY(hi))
24914 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024915 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024916 hash_remove(&func_hashtab, hi);
24917
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024918 vim_free(fp);
24919}
24920
24921/*
24922 * Unreference a Function: decrement the reference count and free it when it
24923 * becomes zero. Only for numbered functions.
24924 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024925 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024926func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024927{
24928 ufunc_T *fp;
24929
24930 if (name != NULL && isdigit(*name))
24931 {
24932 fp = find_func(name);
24933 if (fp == NULL)
24934 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024935 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024936 {
24937 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024938 * when "uf_calls" becomes zero. */
24939 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024940 func_free(fp);
24941 }
24942 }
24943}
24944
24945/*
24946 * Count a reference to a Function.
24947 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024948 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024949func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024950{
24951 ufunc_T *fp;
24952
24953 if (name != NULL && isdigit(*name))
24954 {
24955 fp = find_func(name);
24956 if (fp == NULL)
24957 EMSG2(_(e_intern2), "func_ref()");
24958 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024959 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024960 }
24961}
24962
24963/*
24964 * Call a user function.
24965 */
24966 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024967call_user_func(
24968 ufunc_T *fp, /* pointer to function */
24969 int argcount, /* nr of args */
24970 typval_T *argvars, /* arguments */
24971 typval_T *rettv, /* return value */
24972 linenr_T firstline, /* first line of range */
24973 linenr_T lastline, /* last line of range */
24974 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024975{
Bram Moolenaar33570922005-01-25 22:26:29 +000024976 char_u *save_sourcing_name;
24977 linenr_T save_sourcing_lnum;
24978 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024979 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024980 int save_did_emsg;
24981 static int depth = 0;
24982 dictitem_T *v;
24983 int fixvar_idx = 0; /* index in fixvar[] */
24984 int i;
24985 int ai;
24986 char_u numbuf[NUMBUFLEN];
24987 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024988 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024989#ifdef FEAT_PROFILE
24990 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024991 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024992#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993
24994 /* If depth of calling is getting too high, don't execute the function */
24995 if (depth >= p_mfd)
24996 {
24997 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024998 rettv->v_type = VAR_NUMBER;
24999 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025000 return;
25001 }
25002 ++depth;
25003
25004 line_breakcheck(); /* check for CTRL-C hit */
25005
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025006 fc = (funccall_T *)alloc(sizeof(funccall_T));
25007 fc->caller = current_funccal;
25008 current_funccal = fc;
25009 fc->func = fp;
25010 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025011 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025012 fc->linenr = 0;
25013 fc->returned = FALSE;
25014 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025016 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25017 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018
Bram Moolenaar33570922005-01-25 22:26:29 +000025019 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025020 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025021 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25022 * each argument variable and saves a lot of time.
25023 */
25024 /*
25025 * Init l: variables.
25026 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025027 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025028 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025029 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025030 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25031 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025032 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025033 name = v->di_key;
25034 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025035 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025036 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025037 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025038 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025039 v->di_tv.vval.v_dict = selfdict;
25040 ++selfdict->dv_refcount;
25041 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025042
Bram Moolenaar33570922005-01-25 22:26:29 +000025043 /*
25044 * Init a: variables.
25045 * Set a:0 to "argcount".
25046 * Set a:000 to a list with room for the "..." arguments.
25047 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025048 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025049 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025050 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025051 /* Use "name" to avoid a warning from some compiler that checks the
25052 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025053 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025054 name = v->di_key;
25055 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025056 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025057 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025058 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025059 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025060 v->di_tv.vval.v_list = &fc->l_varlist;
25061 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25062 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25063 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025064
25065 /*
25066 * Set a:firstline to "firstline" and a:lastline to "lastline".
25067 * Set a:name to named arguments.
25068 * Set a:N to the "..." arguments.
25069 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025070 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025071 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025072 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025073 (varnumber_T)lastline);
25074 for (i = 0; i < argcount; ++i)
25075 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025076 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025077 if (ai < 0)
25078 /* named argument a:name */
25079 name = FUNCARG(fp, i);
25080 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025081 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025082 /* "..." argument a:1, a:2, etc. */
25083 sprintf((char *)numbuf, "%d", ai + 1);
25084 name = numbuf;
25085 }
25086 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25087 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025088 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025089 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25090 }
25091 else
25092 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025093 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25094 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025095 if (v == NULL)
25096 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025097 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025098 }
25099 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025100 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025101
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025102 /* Note: the values are copied directly to avoid alloc/free.
25103 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025104 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025105 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025106
25107 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25108 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025109 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25110 fc->l_listitems[ai].li_tv = argvars[i];
25111 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025112 }
25113 }
25114
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 /* Don't redraw while executing the function. */
25116 ++RedrawingDisabled;
25117 save_sourcing_name = sourcing_name;
25118 save_sourcing_lnum = sourcing_lnum;
25119 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025120 /* need space for function name + ("function " + 3) or "[number]" */
25121 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25122 + STRLEN(fp->uf_name) + 20;
25123 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025124 if (sourcing_name != NULL)
25125 {
25126 if (save_sourcing_name != NULL
25127 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025128 sprintf((char *)sourcing_name, "%s[%d]..",
25129 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025130 else
25131 STRCPY(sourcing_name, "function ");
25132 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25133
25134 if (p_verbose >= 12)
25135 {
25136 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025137 verbose_enter_scroll();
25138
Bram Moolenaar555b2802005-05-19 21:08:39 +000025139 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025140 if (p_verbose >= 14)
25141 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025142 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025143 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025144 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025145 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025146
25147 msg_puts((char_u *)"(");
25148 for (i = 0; i < argcount; ++i)
25149 {
25150 if (i > 0)
25151 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025152 if (argvars[i].v_type == VAR_NUMBER)
25153 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025154 else
25155 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025156 /* Do not want errors such as E724 here. */
25157 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025158 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025159 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025160 if (s != NULL)
25161 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025162 if (vim_strsize(s) > MSG_BUF_CLEN)
25163 {
25164 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25165 s = buf;
25166 }
25167 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025168 vim_free(tofree);
25169 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025170 }
25171 }
25172 msg_puts((char_u *)")");
25173 }
25174 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025175
25176 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 --no_wait_return;
25178 }
25179 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025180#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025181 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025182 {
25183 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25184 func_do_profile(fp);
25185 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025186 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025187 {
25188 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025189 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025190 profile_zero(&fp->uf_tm_children);
25191 }
25192 script_prof_save(&wait_start);
25193 }
25194#endif
25195
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025197 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025198 save_did_emsg = did_emsg;
25199 did_emsg = FALSE;
25200
25201 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025202 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025203 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25204
25205 --RedrawingDisabled;
25206
25207 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025208 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025210 clear_tv(rettv);
25211 rettv->v_type = VAR_NUMBER;
25212 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025213 }
25214
Bram Moolenaar05159a02005-02-26 23:04:13 +000025215#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025216 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025217 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025218 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025219 profile_end(&call_start);
25220 profile_sub_wait(&wait_start, &call_start);
25221 profile_add(&fp->uf_tm_total, &call_start);
25222 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025223 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025224 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025225 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25226 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025227 }
25228 }
25229#endif
25230
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 /* when being verbose, mention the return value */
25232 if (p_verbose >= 12)
25233 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025235 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025238 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025239 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025240 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025241 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025242 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025243 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025244 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025245 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025246 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025247 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025248
Bram Moolenaar555b2802005-05-19 21:08:39 +000025249 /* The value may be very long. Skip the middle part, so that we
25250 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025251 * truncate it at the end. Don't want errors such as E724 here. */
25252 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025253 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025254 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025255 if (s != NULL)
25256 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025257 if (vim_strsize(s) > MSG_BUF_CLEN)
25258 {
25259 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25260 s = buf;
25261 }
25262 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025263 vim_free(tofree);
25264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 }
25266 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025267
25268 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025269 --no_wait_return;
25270 }
25271
25272 vim_free(sourcing_name);
25273 sourcing_name = save_sourcing_name;
25274 sourcing_lnum = save_sourcing_lnum;
25275 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025276#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025277 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025278 script_prof_restore(&wait_start);
25279#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280
25281 if (p_verbose >= 12 && sourcing_name != NULL)
25282 {
25283 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025284 verbose_enter_scroll();
25285
Bram Moolenaar555b2802005-05-19 21:08:39 +000025286 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025288
25289 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290 --no_wait_return;
25291 }
25292
25293 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025294 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025295 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025296
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025297 /* If the a:000 list and the l: and a: dicts are not referenced we can
25298 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025299 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25300 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25301 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25302 {
25303 free_funccal(fc, FALSE);
25304 }
25305 else
25306 {
25307 hashitem_T *hi;
25308 listitem_T *li;
25309 int todo;
25310
25311 /* "fc" is still in use. This can happen when returning "a:000" or
25312 * assigning "l:" to a global variable.
25313 * Link "fc" in the list for garbage collection later. */
25314 fc->caller = previous_funccal;
25315 previous_funccal = fc;
25316
25317 /* Make a copy of the a: variables, since we didn't do that above. */
25318 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25319 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25320 {
25321 if (!HASHITEM_EMPTY(hi))
25322 {
25323 --todo;
25324 v = HI2DI(hi);
25325 copy_tv(&v->di_tv, &v->di_tv);
25326 }
25327 }
25328
25329 /* Make a copy of the a:000 items, since we didn't do that above. */
25330 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25331 copy_tv(&li->li_tv, &li->li_tv);
25332 }
25333}
25334
25335/*
25336 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025337 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025338 */
25339 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025340can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025341{
25342 return (fc->l_varlist.lv_copyID != copyID
25343 && fc->l_vars.dv_copyID != copyID
25344 && fc->l_avars.dv_copyID != copyID);
25345}
25346
25347/*
25348 * Free "fc" and what it contains.
25349 */
25350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025351free_funccal(
25352 funccall_T *fc,
25353 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025354{
25355 listitem_T *li;
25356
25357 /* The a: variables typevals may not have been allocated, only free the
25358 * allocated variables. */
25359 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25360
25361 /* free all l: variables */
25362 vars_clear(&fc->l_vars.dv_hashtab);
25363
25364 /* Free the a:000 variables if they were allocated. */
25365 if (free_val)
25366 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25367 clear_tv(&li->li_tv);
25368
25369 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025370}
25371
25372/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025373 * Add a number variable "name" to dict "dp" with value "nr".
25374 */
25375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025376add_nr_var(
25377 dict_T *dp,
25378 dictitem_T *v,
25379 char *name,
25380 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025381{
25382 STRCPY(v->di_key, name);
25383 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25384 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25385 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025386 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025387 v->di_tv.vval.v_number = nr;
25388}
25389
25390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391 * ":return [expr]"
25392 */
25393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025394ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395{
25396 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025397 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 int returning = FALSE;
25399
25400 if (current_funccal == NULL)
25401 {
25402 EMSG(_("E133: :return not inside a function"));
25403 return;
25404 }
25405
25406 if (eap->skip)
25407 ++emsg_skip;
25408
25409 eap->nextcmd = NULL;
25410 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025411 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 {
25413 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025414 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025415 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025416 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025417 }
25418 /* It's safer to return also on error. */
25419 else if (!eap->skip)
25420 {
25421 /*
25422 * Return unless the expression evaluation has been cancelled due to an
25423 * aborting error, an interrupt, or an exception.
25424 */
25425 if (!aborting())
25426 returning = do_return(eap, FALSE, TRUE, NULL);
25427 }
25428
25429 /* When skipping or the return gets pending, advance to the next command
25430 * in this line (!returning). Otherwise, ignore the rest of the line.
25431 * Following lines will be ignored by get_func_line(). */
25432 if (returning)
25433 eap->nextcmd = NULL;
25434 else if (eap->nextcmd == NULL) /* no argument */
25435 eap->nextcmd = check_nextcmd(arg);
25436
25437 if (eap->skip)
25438 --emsg_skip;
25439}
25440
25441/*
25442 * Return from a function. Possibly makes the return pending. Also called
25443 * for a pending return at the ":endtry" or after returning from an extra
25444 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025445 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025446 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447 * FALSE when the return gets pending.
25448 */
25449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025450do_return(
25451 exarg_T *eap,
25452 int reanimate,
25453 int is_cmd,
25454 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025455{
25456 int idx;
25457 struct condstack *cstack = eap->cstack;
25458
25459 if (reanimate)
25460 /* Undo the return. */
25461 current_funccal->returned = FALSE;
25462
25463 /*
25464 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25465 * not in its finally clause (which then is to be executed next) is found.
25466 * In this case, make the ":return" pending for execution at the ":endtry".
25467 * Otherwise, return normally.
25468 */
25469 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25470 if (idx >= 0)
25471 {
25472 cstack->cs_pending[idx] = CSTP_RETURN;
25473
25474 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025475 /* A pending return again gets pending. "rettv" points to an
25476 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025477 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025478 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025479 else
25480 {
25481 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025482 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025483 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025484 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025486 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025487 {
25488 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025489 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025490 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025491 else
25492 EMSG(_(e_outofmem));
25493 }
25494 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025495 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025496
25497 if (reanimate)
25498 {
25499 /* The pending return value could be overwritten by a ":return"
25500 * without argument in a finally clause; reset the default
25501 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025502 current_funccal->rettv->v_type = VAR_NUMBER;
25503 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 }
25505 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025506 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025507 }
25508 else
25509 {
25510 current_funccal->returned = TRUE;
25511
25512 /* If the return is carried out now, store the return value. For
25513 * a return immediately after reanimation, the value is already
25514 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025515 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025517 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025518 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025519 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025520 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521 }
25522 }
25523
25524 return idx < 0;
25525}
25526
25527/*
25528 * Free the variable with a pending return value.
25529 */
25530 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025531discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025532{
Bram Moolenaar33570922005-01-25 22:26:29 +000025533 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534}
25535
25536/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025537 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538 * is an allocated string. Used by report_pending() for verbose messages.
25539 */
25540 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025541get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025543 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025544 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025545 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025546
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025547 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025548 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025549 if (s == NULL)
25550 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025551
25552 STRCPY(IObuff, ":return ");
25553 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25554 if (STRLEN(s) + 8 >= IOSIZE)
25555 STRCPY(IObuff + IOSIZE - 4, "...");
25556 vim_free(tofree);
25557 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558}
25559
25560/*
25561 * Get next function line.
25562 * Called by do_cmdline() to get the next line.
25563 * Returns allocated string, or NULL for end of function.
25564 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025566get_func_line(
25567 int c UNUSED,
25568 void *cookie,
25569 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570{
Bram Moolenaar33570922005-01-25 22:26:29 +000025571 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025572 ufunc_T *fp = fcp->func;
25573 char_u *retval;
25574 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025575
25576 /* If breakpoints have been added/deleted need to check for it. */
25577 if (fcp->dbg_tick != debug_tick)
25578 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025579 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025580 sourcing_lnum);
25581 fcp->dbg_tick = debug_tick;
25582 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025583#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025584 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025585 func_line_end(cookie);
25586#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587
Bram Moolenaar05159a02005-02-26 23:04:13 +000025588 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025589 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25590 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591 retval = NULL;
25592 else
25593 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025594 /* Skip NULL lines (continuation lines). */
25595 while (fcp->linenr < gap->ga_len
25596 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25597 ++fcp->linenr;
25598 if (fcp->linenr >= gap->ga_len)
25599 retval = NULL;
25600 else
25601 {
25602 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25603 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025604#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025605 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025606 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025607#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025608 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025609 }
25610
25611 /* Did we encounter a breakpoint? */
25612 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25613 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025614 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025616 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 sourcing_lnum);
25618 fcp->dbg_tick = debug_tick;
25619 }
25620
25621 return retval;
25622}
25623
Bram Moolenaar05159a02005-02-26 23:04:13 +000025624#if defined(FEAT_PROFILE) || defined(PROTO)
25625/*
25626 * Called when starting to read a function line.
25627 * "sourcing_lnum" must be correct!
25628 * When skipping lines it may not actually be executed, but we won't find out
25629 * until later and we need to store the time now.
25630 */
25631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025632func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025633{
25634 funccall_T *fcp = (funccall_T *)cookie;
25635 ufunc_T *fp = fcp->func;
25636
25637 if (fp->uf_profiling && sourcing_lnum >= 1
25638 && sourcing_lnum <= fp->uf_lines.ga_len)
25639 {
25640 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025641 /* Skip continuation lines. */
25642 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25643 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025644 fp->uf_tml_execed = FALSE;
25645 profile_start(&fp->uf_tml_start);
25646 profile_zero(&fp->uf_tml_children);
25647 profile_get_wait(&fp->uf_tml_wait);
25648 }
25649}
25650
25651/*
25652 * Called when actually executing a function line.
25653 */
25654 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025655func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025656{
25657 funccall_T *fcp = (funccall_T *)cookie;
25658 ufunc_T *fp = fcp->func;
25659
25660 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25661 fp->uf_tml_execed = TRUE;
25662}
25663
25664/*
25665 * Called when done with a function line.
25666 */
25667 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025668func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025669{
25670 funccall_T *fcp = (funccall_T *)cookie;
25671 ufunc_T *fp = fcp->func;
25672
25673 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25674 {
25675 if (fp->uf_tml_execed)
25676 {
25677 ++fp->uf_tml_count[fp->uf_tml_idx];
25678 profile_end(&fp->uf_tml_start);
25679 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025680 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025681 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25682 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025683 }
25684 fp->uf_tml_idx = -1;
25685 }
25686}
25687#endif
25688
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689/*
25690 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025691 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692 */
25693 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025694func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025695{
Bram Moolenaar33570922005-01-25 22:26:29 +000025696 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025697
25698 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25699 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025700 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025701 || fcp->returned);
25702}
25703
25704/*
25705 * return TRUE if cookie indicates a function which "abort"s on errors.
25706 */
25707 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025708func_has_abort(
25709 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025710{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025711 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025712}
25713
25714#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25715typedef enum
25716{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025717 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25718 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25719 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720} var_flavour_T;
25721
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025722static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723
25724 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025725var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025726{
25727 char_u *p = varname;
25728
25729 if (ASCII_ISUPPER(*p))
25730 {
25731 while (*(++p))
25732 if (ASCII_ISLOWER(*p))
25733 return VAR_FLAVOUR_SESSION;
25734 return VAR_FLAVOUR_VIMINFO;
25735 }
25736 else
25737 return VAR_FLAVOUR_DEFAULT;
25738}
25739#endif
25740
25741#if defined(FEAT_VIMINFO) || defined(PROTO)
25742/*
25743 * Restore global vars that start with a capital from the viminfo file
25744 */
25745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025746read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747{
25748 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025749 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025750 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025751 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752
25753 if (!writing && (find_viminfo_parameter('!') != NULL))
25754 {
25755 tab = vim_strchr(virp->vir_line + 1, '\t');
25756 if (tab != NULL)
25757 {
25758 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025759 switch (*tab)
25760 {
25761 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025762#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025763 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025764#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025765 case 'D': type = VAR_DICT; break;
25766 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025767 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025768 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769
25770 tab = vim_strchr(tab, '\t');
25771 if (tab != NULL)
25772 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025773 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025774 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025775 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025776 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025777#ifdef FEAT_FLOAT
25778 else if (type == VAR_FLOAT)
25779 (void)string2float(tab + 1, &tv.vval.v_float);
25780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025781 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025782 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025783 if (type == VAR_DICT || type == VAR_LIST)
25784 {
25785 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25786
25787 if (etv == NULL)
25788 /* Failed to parse back the dict or list, use it as a
25789 * string. */
25790 tv.v_type = VAR_STRING;
25791 else
25792 {
25793 vim_free(tv.vval.v_string);
25794 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025795 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025796 }
25797 }
25798
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025799 /* when in a function use global variables */
25800 save_funccal = current_funccal;
25801 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025802 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025803 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025804
25805 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025806 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025807 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25808 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 }
25810 }
25811 }
25812
25813 return viminfo_readline(virp);
25814}
25815
25816/*
25817 * Write global vars that start with a capital to the viminfo file
25818 */
25819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025820write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025821{
Bram Moolenaar33570922005-01-25 22:26:29 +000025822 hashitem_T *hi;
25823 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025824 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025825 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025826 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025827 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025828 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829
25830 if (find_viminfo_parameter('!') == NULL)
25831 return;
25832
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025833 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025834
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025835 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025836 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025837 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025838 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025840 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025841 this_var = HI2DI(hi);
25842 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025843 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025844 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025845 {
25846 case VAR_STRING: s = "STR"; break;
25847 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025848 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025849 case VAR_DICT: s = "DIC"; break;
25850 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025851 case VAR_SPECIAL: s = "XPL"; break;
25852
25853 case VAR_UNKNOWN:
25854 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025855 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025856 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025857 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025858 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025859 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025860 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025861 if (p != NULL)
25862 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025863 vim_free(tofree);
25864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 }
25866 }
25867}
25868#endif
25869
25870#if defined(FEAT_SESSION) || defined(PROTO)
25871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025872store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025873{
Bram Moolenaar33570922005-01-25 22:26:29 +000025874 hashitem_T *hi;
25875 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025876 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025877 char_u *p, *t;
25878
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025879 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025880 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025881 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025882 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025883 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025884 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025885 this_var = HI2DI(hi);
25886 if ((this_var->di_tv.v_type == VAR_NUMBER
25887 || this_var->di_tv.v_type == VAR_STRING)
25888 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025889 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025890 /* Escape special characters with a backslash. Turn a LF and
25891 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025892 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025893 (char_u *)"\\\"\n\r");
25894 if (p == NULL) /* out of memory */
25895 break;
25896 for (t = p; *t != NUL; ++t)
25897 if (*t == '\n')
25898 *t = 'n';
25899 else if (*t == '\r')
25900 *t = 'r';
25901 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025902 this_var->di_key,
25903 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25904 : ' ',
25905 p,
25906 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25907 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025908 || put_eol(fd) == FAIL)
25909 {
25910 vim_free(p);
25911 return FAIL;
25912 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025913 vim_free(p);
25914 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025915#ifdef FEAT_FLOAT
25916 else if (this_var->di_tv.v_type == VAR_FLOAT
25917 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25918 {
25919 float_T f = this_var->di_tv.vval.v_float;
25920 int sign = ' ';
25921
25922 if (f < 0)
25923 {
25924 f = -f;
25925 sign = '-';
25926 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025927 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025928 this_var->di_key, sign, f) < 0)
25929 || put_eol(fd) == FAIL)
25930 return FAIL;
25931 }
25932#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025933 }
25934 }
25935 return OK;
25936}
25937#endif
25938
Bram Moolenaar661b1822005-07-28 22:36:45 +000025939/*
25940 * Display script name where an item was last set.
25941 * Should only be invoked when 'verbose' is non-zero.
25942 */
25943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025944last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025945{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025946 char_u *p;
25947
Bram Moolenaar661b1822005-07-28 22:36:45 +000025948 if (scriptID != 0)
25949 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025950 p = home_replace_save(NULL, get_scriptname(scriptID));
25951 if (p != NULL)
25952 {
25953 verbose_enter();
25954 MSG_PUTS(_("\n\tLast set from "));
25955 MSG_PUTS(p);
25956 vim_free(p);
25957 verbose_leave();
25958 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025959 }
25960}
25961
Bram Moolenaard812df62008-11-09 12:46:09 +000025962/*
25963 * List v:oldfiles in a nice way.
25964 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025965 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025966ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025967{
25968 list_T *l = vimvars[VV_OLDFILES].vv_list;
25969 listitem_T *li;
25970 int nr = 0;
25971
25972 if (l == NULL)
25973 msg((char_u *)_("No old files"));
25974 else
25975 {
25976 msg_start();
25977 msg_scroll = TRUE;
25978 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25979 {
25980 msg_outnum((long)++nr);
25981 MSG_PUTS(": ");
25982 msg_outtrans(get_tv_string(&li->li_tv));
25983 msg_putchar('\n');
25984 out_flush(); /* output one line at a time */
25985 ui_breakcheck();
25986 }
25987 /* Assume "got_int" was set to truncate the listing. */
25988 got_int = FALSE;
25989
25990#ifdef FEAT_BROWSE_CMD
25991 if (cmdmod.browse)
25992 {
25993 quit_more = FALSE;
25994 nr = prompt_for_number(FALSE);
25995 msg_starthere();
25996 if (nr > 0)
25997 {
25998 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25999 (long)nr);
26000
26001 if (p != NULL)
26002 {
26003 p = expand_env_save(p);
26004 eap->arg = p;
26005 eap->cmdidx = CMD_edit;
26006 cmdmod.browse = FALSE;
26007 do_exedit(eap, NULL);
26008 vim_free(p);
26009 }
26010 }
26011 }
26012#endif
26013 }
26014}
26015
Bram Moolenaar53744302015-07-17 17:38:22 +020026016/* reset v:option_new, v:option_old and v:option_type */
26017 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026018reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026019{
26020 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26021 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26022 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26023}
26024
26025
Bram Moolenaar071d4272004-06-13 20:20:40 +000026026#endif /* FEAT_EVAL */
26027
Bram Moolenaar071d4272004-06-13 20:20:40 +000026028
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026029#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030
26031#ifdef WIN3264
26032/*
26033 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26034 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026035static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26036static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26037static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026038
26039/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026040 * Get the short path (8.3) for the filename in "fnamep".
26041 * Only works for a valid file name.
26042 * When the path gets longer "fnamep" is changed and the allocated buffer
26043 * is put in "bufp".
26044 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26045 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046 */
26047 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026048get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026050 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 char_u *newbuf;
26052
26053 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026054 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026055 if (l > len - 1)
26056 {
26057 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026058 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059 newbuf = vim_strnsave(*fnamep, l);
26060 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026061 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026062
26063 vim_free(*bufp);
26064 *fnamep = *bufp = newbuf;
26065
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026066 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026067 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 }
26069
26070 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026071 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026072}
26073
26074/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026075 * Get the short path (8.3) for the filename in "fname". The converted
26076 * path is returned in "bufp".
26077 *
26078 * Some of the directories specified in "fname" may not exist. This function
26079 * will shorten the existing directories at the beginning of the path and then
26080 * append the remaining non-existing path.
26081 *
26082 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026083 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026084 * bufp - Pointer to an allocated buffer for the filename.
26085 * fnamelen - Length of the filename pointed to by fname
26086 *
26087 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026088 */
26089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026090shortpath_for_invalid_fname(
26091 char_u **fname,
26092 char_u **bufp,
26093 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026095 char_u *short_fname, *save_fname, *pbuf_unused;
26096 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026098 int old_len, len;
26099 int new_len, sfx_len;
26100 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101
26102 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026103 old_len = *fnamelen;
26104 save_fname = vim_strnsave(*fname, old_len);
26105 pbuf_unused = NULL;
26106 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026108 endp = save_fname + old_len - 1; /* Find the end of the copy */
26109 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026111 /*
26112 * Try shortening the supplied path till it succeeds by removing one
26113 * directory at a time from the tail of the path.
26114 */
26115 len = 0;
26116 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026117 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026118 /* go back one path-separator */
26119 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26120 --endp;
26121 if (endp <= save_fname)
26122 break; /* processed the complete path */
26123
26124 /*
26125 * Replace the path separator with a NUL and try to shorten the
26126 * resulting path.
26127 */
26128 ch = *endp;
26129 *endp = 0;
26130 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026131 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026132 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26133 {
26134 retval = FAIL;
26135 goto theend;
26136 }
26137 *endp = ch; /* preserve the string */
26138
26139 if (len > 0)
26140 break; /* successfully shortened the path */
26141
26142 /* failed to shorten the path. Skip the path separator */
26143 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144 }
26145
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026146 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026147 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026148 /*
26149 * Succeeded in shortening the path. Now concatenate the shortened
26150 * path with the remaining path at the tail.
26151 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026152
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026153 /* Compute the length of the new path. */
26154 sfx_len = (int)(save_endp - endp) + 1;
26155 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026156
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026157 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026158 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026159 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026161 /* There is not enough space in the currently allocated string,
26162 * copy it to a buffer big enough. */
26163 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026164 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026165 {
26166 retval = FAIL;
26167 goto theend;
26168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 }
26170 else
26171 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026172 /* Transfer short_fname to the main buffer (it's big enough),
26173 * unless get_short_pathname() did its work in-place. */
26174 *fname = *bufp = save_fname;
26175 if (short_fname != save_fname)
26176 vim_strncpy(save_fname, short_fname, len);
26177 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026178 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026179
26180 /* concat the not-shortened part of the path */
26181 vim_strncpy(*fname + len, endp, sfx_len);
26182 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026184
26185theend:
26186 vim_free(pbuf_unused);
26187 vim_free(save_fname);
26188
26189 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026190}
26191
26192/*
26193 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026194 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026195 */
26196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026197shortpath_for_partial(
26198 char_u **fnamep,
26199 char_u **bufp,
26200 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201{
26202 int sepcount, len, tflen;
26203 char_u *p;
26204 char_u *pbuf, *tfname;
26205 int hasTilde;
26206
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026207 /* Count up the path separators from the RHS.. so we know which part
26208 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026209 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026210 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211 if (vim_ispathsep(*p))
26212 ++sepcount;
26213
26214 /* Need full path first (use expand_env() to remove a "~/") */
26215 hasTilde = (**fnamep == '~');
26216 if (hasTilde)
26217 pbuf = tfname = expand_env_save(*fnamep);
26218 else
26219 pbuf = tfname = FullName_save(*fnamep, FALSE);
26220
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026221 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026223 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26224 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225
26226 if (len == 0)
26227 {
26228 /* Don't have a valid filename, so shorten the rest of the
26229 * path if we can. This CAN give us invalid 8.3 filenames, but
26230 * there's not a lot of point in guessing what it might be.
26231 */
26232 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026233 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26234 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026235 }
26236
26237 /* Count the paths backward to find the beginning of the desired string. */
26238 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026239 {
26240#ifdef FEAT_MBYTE
26241 if (has_mbyte)
26242 p -= mb_head_off(tfname, p);
26243#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026244 if (vim_ispathsep(*p))
26245 {
26246 if (sepcount == 0 || (hasTilde && sepcount == 1))
26247 break;
26248 else
26249 sepcount --;
26250 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026252 if (hasTilde)
26253 {
26254 --p;
26255 if (p >= tfname)
26256 *p = '~';
26257 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026258 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026259 }
26260 else
26261 ++p;
26262
26263 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26264 vim_free(*bufp);
26265 *fnamelen = (int)STRLEN(p);
26266 *bufp = pbuf;
26267 *fnamep = p;
26268
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026269 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026270}
26271#endif /* WIN3264 */
26272
26273/*
26274 * Adjust a filename, according to a string of modifiers.
26275 * *fnamep must be NUL terminated when called. When returning, the length is
26276 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026277 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026278 * When there is an error, *fnamep is set to NULL.
26279 */
26280 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026281modify_fname(
26282 char_u *src, /* string with modifiers */
26283 int *usedlen, /* characters after src that are used */
26284 char_u **fnamep, /* file name so far */
26285 char_u **bufp, /* buffer for allocated file name or NULL */
26286 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026287{
26288 int valid = 0;
26289 char_u *tail;
26290 char_u *s, *p, *pbuf;
26291 char_u dirname[MAXPATHL];
26292 int c;
26293 int has_fullname = 0;
26294#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026295 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026296 int has_shortname = 0;
26297#endif
26298
26299repeat:
26300 /* ":p" - full path/file_name */
26301 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26302 {
26303 has_fullname = 1;
26304
26305 valid |= VALID_PATH;
26306 *usedlen += 2;
26307
26308 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26309 if ((*fnamep)[0] == '~'
26310#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26311 && ((*fnamep)[1] == '/'
26312# ifdef BACKSLASH_IN_FILENAME
26313 || (*fnamep)[1] == '\\'
26314# endif
26315 || (*fnamep)[1] == NUL)
26316
26317#endif
26318 )
26319 {
26320 *fnamep = expand_env_save(*fnamep);
26321 vim_free(*bufp); /* free any allocated file name */
26322 *bufp = *fnamep;
26323 if (*fnamep == NULL)
26324 return -1;
26325 }
26326
26327 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026328 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026329 {
26330 if (vim_ispathsep(*p)
26331 && p[1] == '.'
26332 && (p[2] == NUL
26333 || vim_ispathsep(p[2])
26334 || (p[2] == '.'
26335 && (p[3] == NUL || vim_ispathsep(p[3])))))
26336 break;
26337 }
26338
26339 /* FullName_save() is slow, don't use it when not needed. */
26340 if (*p != NUL || !vim_isAbsName(*fnamep))
26341 {
26342 *fnamep = FullName_save(*fnamep, *p != NUL);
26343 vim_free(*bufp); /* free any allocated file name */
26344 *bufp = *fnamep;
26345 if (*fnamep == NULL)
26346 return -1;
26347 }
26348
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026349#ifdef WIN3264
26350# if _WIN32_WINNT >= 0x0500
26351 if (vim_strchr(*fnamep, '~') != NULL)
26352 {
26353 /* Expand 8.3 filename to full path. Needed to make sure the same
26354 * file does not have two different names.
26355 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26356 p = alloc(_MAX_PATH + 1);
26357 if (p != NULL)
26358 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026359 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026360 {
26361 vim_free(*bufp);
26362 *bufp = *fnamep = p;
26363 }
26364 else
26365 vim_free(p);
26366 }
26367 }
26368# endif
26369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026370 /* Append a path separator to a directory. */
26371 if (mch_isdir(*fnamep))
26372 {
26373 /* Make room for one or two extra characters. */
26374 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26375 vim_free(*bufp); /* free any allocated file name */
26376 *bufp = *fnamep;
26377 if (*fnamep == NULL)
26378 return -1;
26379 add_pathsep(*fnamep);
26380 }
26381 }
26382
26383 /* ":." - path relative to the current directory */
26384 /* ":~" - path relative to the home directory */
26385 /* ":8" - shortname path - postponed till after */
26386 while (src[*usedlen] == ':'
26387 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26388 {
26389 *usedlen += 2;
26390 if (c == '8')
26391 {
26392#ifdef WIN3264
26393 has_shortname = 1; /* Postpone this. */
26394#endif
26395 continue;
26396 }
26397 pbuf = NULL;
26398 /* Need full path first (use expand_env() to remove a "~/") */
26399 if (!has_fullname)
26400 {
26401 if (c == '.' && **fnamep == '~')
26402 p = pbuf = expand_env_save(*fnamep);
26403 else
26404 p = pbuf = FullName_save(*fnamep, FALSE);
26405 }
26406 else
26407 p = *fnamep;
26408
26409 has_fullname = 0;
26410
26411 if (p != NULL)
26412 {
26413 if (c == '.')
26414 {
26415 mch_dirname(dirname, MAXPATHL);
26416 s = shorten_fname(p, dirname);
26417 if (s != NULL)
26418 {
26419 *fnamep = s;
26420 if (pbuf != NULL)
26421 {
26422 vim_free(*bufp); /* free any allocated file name */
26423 *bufp = pbuf;
26424 pbuf = NULL;
26425 }
26426 }
26427 }
26428 else
26429 {
26430 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26431 /* Only replace it when it starts with '~' */
26432 if (*dirname == '~')
26433 {
26434 s = vim_strsave(dirname);
26435 if (s != NULL)
26436 {
26437 *fnamep = s;
26438 vim_free(*bufp);
26439 *bufp = s;
26440 }
26441 }
26442 }
26443 vim_free(pbuf);
26444 }
26445 }
26446
26447 tail = gettail(*fnamep);
26448 *fnamelen = (int)STRLEN(*fnamep);
26449
26450 /* ":h" - head, remove "/file_name", can be repeated */
26451 /* Don't remove the first "/" or "c:\" */
26452 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26453 {
26454 valid |= VALID_HEAD;
26455 *usedlen += 2;
26456 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026457 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026458 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026459 *fnamelen = (int)(tail - *fnamep);
26460#ifdef VMS
26461 if (*fnamelen > 0)
26462 *fnamelen += 1; /* the path separator is part of the path */
26463#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026464 if (*fnamelen == 0)
26465 {
26466 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26467 p = vim_strsave((char_u *)".");
26468 if (p == NULL)
26469 return -1;
26470 vim_free(*bufp);
26471 *bufp = *fnamep = tail = p;
26472 *fnamelen = 1;
26473 }
26474 else
26475 {
26476 while (tail > s && !after_pathsep(s, tail))
26477 mb_ptr_back(*fnamep, tail);
26478 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026479 }
26480
26481 /* ":8" - shortname */
26482 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26483 {
26484 *usedlen += 2;
26485#ifdef WIN3264
26486 has_shortname = 1;
26487#endif
26488 }
26489
26490#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026491 /*
26492 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026493 */
26494 if (has_shortname)
26495 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026496 /* Copy the string if it is shortened by :h and when it wasn't copied
26497 * yet, because we are going to change it in place. Avoids changing
26498 * the buffer name for "%:8". */
26499 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026500 {
26501 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026502 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026503 return -1;
26504 vim_free(*bufp);
26505 *bufp = *fnamep = p;
26506 }
26507
26508 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026509 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510 if (!has_fullname && !vim_isAbsName(*fnamep))
26511 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026512 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513 return -1;
26514 }
26515 else
26516 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026517 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026518
Bram Moolenaardc935552011-08-17 15:23:23 +020026519 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026520 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026521 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 return -1;
26523
26524 if (l == 0)
26525 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026526 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026527 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026528 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026529 return -1;
26530 }
26531 *fnamelen = l;
26532 }
26533 }
26534#endif /* WIN3264 */
26535
26536 /* ":t" - tail, just the basename */
26537 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26538 {
26539 *usedlen += 2;
26540 *fnamelen -= (int)(tail - *fnamep);
26541 *fnamep = tail;
26542 }
26543
26544 /* ":e" - extension, can be repeated */
26545 /* ":r" - root, without extension, can be repeated */
26546 while (src[*usedlen] == ':'
26547 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26548 {
26549 /* find a '.' in the tail:
26550 * - for second :e: before the current fname
26551 * - otherwise: The last '.'
26552 */
26553 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26554 s = *fnamep - 2;
26555 else
26556 s = *fnamep + *fnamelen - 1;
26557 for ( ; s > tail; --s)
26558 if (s[0] == '.')
26559 break;
26560 if (src[*usedlen + 1] == 'e') /* :e */
26561 {
26562 if (s > tail)
26563 {
26564 *fnamelen += (int)(*fnamep - (s + 1));
26565 *fnamep = s + 1;
26566#ifdef VMS
26567 /* cut version from the extension */
26568 s = *fnamep + *fnamelen - 1;
26569 for ( ; s > *fnamep; --s)
26570 if (s[0] == ';')
26571 break;
26572 if (s > *fnamep)
26573 *fnamelen = s - *fnamep;
26574#endif
26575 }
26576 else if (*fnamep <= tail)
26577 *fnamelen = 0;
26578 }
26579 else /* :r */
26580 {
26581 if (s > tail) /* remove one extension */
26582 *fnamelen = (int)(s - *fnamep);
26583 }
26584 *usedlen += 2;
26585 }
26586
26587 /* ":s?pat?foo?" - substitute */
26588 /* ":gs?pat?foo?" - global substitute */
26589 if (src[*usedlen] == ':'
26590 && (src[*usedlen + 1] == 's'
26591 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26592 {
26593 char_u *str;
26594 char_u *pat;
26595 char_u *sub;
26596 int sep;
26597 char_u *flags;
26598 int didit = FALSE;
26599
26600 flags = (char_u *)"";
26601 s = src + *usedlen + 2;
26602 if (src[*usedlen + 1] == 'g')
26603 {
26604 flags = (char_u *)"g";
26605 ++s;
26606 }
26607
26608 sep = *s++;
26609 if (sep)
26610 {
26611 /* find end of pattern */
26612 p = vim_strchr(s, sep);
26613 if (p != NULL)
26614 {
26615 pat = vim_strnsave(s, (int)(p - s));
26616 if (pat != NULL)
26617 {
26618 s = p + 1;
26619 /* find end of substitution */
26620 p = vim_strchr(s, sep);
26621 if (p != NULL)
26622 {
26623 sub = vim_strnsave(s, (int)(p - s));
26624 str = vim_strnsave(*fnamep, *fnamelen);
26625 if (sub != NULL && str != NULL)
26626 {
26627 *usedlen = (int)(p + 1 - src);
26628 s = do_string_sub(str, pat, sub, flags);
26629 if (s != NULL)
26630 {
26631 *fnamep = s;
26632 *fnamelen = (int)STRLEN(s);
26633 vim_free(*bufp);
26634 *bufp = s;
26635 didit = TRUE;
26636 }
26637 }
26638 vim_free(sub);
26639 vim_free(str);
26640 }
26641 vim_free(pat);
26642 }
26643 }
26644 /* after using ":s", repeat all the modifiers */
26645 if (didit)
26646 goto repeat;
26647 }
26648 }
26649
Bram Moolenaar26df0922014-02-23 23:39:13 +010026650 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26651 {
26652 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26653 if (p == NULL)
26654 return -1;
26655 vim_free(*bufp);
26656 *bufp = *fnamep = p;
26657 *fnamelen = (int)STRLEN(p);
26658 *usedlen += 2;
26659 }
26660
Bram Moolenaar071d4272004-06-13 20:20:40 +000026661 return valid;
26662}
26663
26664/*
26665 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26666 * "flags" can be "g" to do a global substitute.
26667 * Returns an allocated string, NULL for error.
26668 */
26669 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026670do_string_sub(
26671 char_u *str,
26672 char_u *pat,
26673 char_u *sub,
26674 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026675{
26676 int sublen;
26677 regmatch_T regmatch;
26678 int i;
26679 int do_all;
26680 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026681 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026682 garray_T ga;
26683 char_u *ret;
26684 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026685 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026686
26687 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26688 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026689 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026690
26691 ga_init2(&ga, 1, 200);
26692
26693 do_all = (flags[0] == 'g');
26694
26695 regmatch.rm_ic = p_ic;
26696 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26697 if (regmatch.regprog != NULL)
26698 {
26699 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026700 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026701 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26702 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026703 /* Skip empty match except for first match. */
26704 if (regmatch.startp[0] == regmatch.endp[0])
26705 {
26706 if (zero_width == regmatch.startp[0])
26707 {
26708 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026709 i = MB_PTR2LEN(tail);
26710 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26711 (size_t)i);
26712 ga.ga_len += i;
26713 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026714 continue;
26715 }
26716 zero_width = regmatch.startp[0];
26717 }
26718
Bram Moolenaar071d4272004-06-13 20:20:40 +000026719 /*
26720 * Get some space for a temporary buffer to do the substitution
26721 * into. It will contain:
26722 * - The text up to where the match is.
26723 * - The substituted text.
26724 * - The text after the match.
26725 */
26726 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026727 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026728 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26729 {
26730 ga_clear(&ga);
26731 break;
26732 }
26733
26734 /* copy the text up to where the match is */
26735 i = (int)(regmatch.startp[0] - tail);
26736 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26737 /* add the substituted text */
26738 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26739 + ga.ga_len + i, TRUE, TRUE, FALSE);
26740 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026741 tail = regmatch.endp[0];
26742 if (*tail == NUL)
26743 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026744 if (!do_all)
26745 break;
26746 }
26747
26748 if (ga.ga_data != NULL)
26749 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26750
Bram Moolenaar473de612013-06-08 18:19:48 +020026751 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026752 }
26753
26754 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26755 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026756 if (p_cpo == empty_option)
26757 p_cpo = save_cpo;
26758 else
26759 /* Darn, evaluating {sub} expression changed the value. */
26760 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026761
26762 return ret;
26763}
26764
26765#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */