blob: f69e81431037d183ec5e2e0d68d7debc74e35f7d [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
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013933#ifdef STARTUPTIME
13934 "startuptime",
13935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013936#ifdef FEAT_STL_OPT
13937 "statusline",
13938#endif
13939#ifdef FEAT_SUN_WORKSHOP
13940 "sun_workshop",
13941#endif
13942#ifdef FEAT_NETBEANS_INTG
13943 "netbeans_intg",
13944#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013945#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013946 "spell",
13947#endif
13948#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013949 "syntax",
13950#endif
13951#if defined(USE_SYSTEM) || !defined(UNIX)
13952 "system",
13953#endif
13954#ifdef FEAT_TAG_BINS
13955 "tag_binary",
13956#endif
13957#ifdef FEAT_TAG_OLDSTATIC
13958 "tag_old_static",
13959#endif
13960#ifdef FEAT_TAG_ANYWHITE
13961 "tag_any_white",
13962#endif
13963#ifdef FEAT_TCL
13964# ifndef DYNAMIC_TCL
13965 "tcl",
13966# endif
13967#endif
13968#ifdef TERMINFO
13969 "terminfo",
13970#endif
13971#ifdef FEAT_TERMRESPONSE
13972 "termresponse",
13973#endif
13974#ifdef FEAT_TEXTOBJ
13975 "textobjects",
13976#endif
13977#ifdef HAVE_TGETENT
13978 "tgetent",
13979#endif
13980#ifdef FEAT_TITLE
13981 "title",
13982#endif
13983#ifdef FEAT_TOOLBAR
13984 "toolbar",
13985#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013986#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13987 "unnamedplus",
13988#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013989#ifdef FEAT_USR_CMDS
13990 "user-commands", /* was accidentally included in 5.4 */
13991 "user_commands",
13992#endif
13993#ifdef FEAT_VIMINFO
13994 "viminfo",
13995#endif
13996#ifdef FEAT_VERTSPLIT
13997 "vertsplit",
13998#endif
13999#ifdef FEAT_VIRTUALEDIT
14000 "virtualedit",
14001#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014002 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003#ifdef FEAT_VISUALEXTRA
14004 "visualextra",
14005#endif
14006#ifdef FEAT_VREPLACE
14007 "vreplace",
14008#endif
14009#ifdef FEAT_WILDIGN
14010 "wildignore",
14011#endif
14012#ifdef FEAT_WILDMENU
14013 "wildmenu",
14014#endif
14015#ifdef FEAT_WINDOWS
14016 "windows",
14017#endif
14018#ifdef FEAT_WAK
14019 "winaltkeys",
14020#endif
14021#ifdef FEAT_WRITEBACKUP
14022 "writebackup",
14023#endif
14024#ifdef FEAT_XIM
14025 "xim",
14026#endif
14027#ifdef FEAT_XFONTSET
14028 "xfontset",
14029#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014030#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020014031 "xpm",
14032 "xpm_w32", /* for backward compatibility */
14033#else
14034# if defined(HAVE_XPM)
14035 "xpm",
14036# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010014037#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038#ifdef USE_XSMP
14039 "xsmp",
14040#endif
14041#ifdef USE_XSMP_INTERACT
14042 "xsmp_interact",
14043#endif
14044#ifdef FEAT_XCLIPBOARD
14045 "xterm_clipboard",
14046#endif
14047#ifdef FEAT_XTERM_SAVE
14048 "xterm_save",
14049#endif
14050#if defined(UNIX) && defined(FEAT_X11)
14051 "X11",
14052#endif
14053 NULL
14054 };
14055
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014056 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057 for (i = 0; has_list[i] != NULL; ++i)
14058 if (STRICMP(name, has_list[i]) == 0)
14059 {
14060 n = TRUE;
14061 break;
14062 }
14063
14064 if (n == FALSE)
14065 {
14066 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014067 {
14068 if (name[5] == '-'
14069 && STRLEN(name) > 11
14070 && vim_isdigit(name[6])
14071 && vim_isdigit(name[8])
14072 && vim_isdigit(name[10]))
14073 {
14074 int major = atoi((char *)name + 6);
14075 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014076
14077 /* Expect "patch-9.9.01234". */
14078 n = (major < VIM_VERSION_MAJOR
14079 || (major == VIM_VERSION_MAJOR
14080 && (minor < VIM_VERSION_MINOR
14081 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020014082 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020014083 }
14084 else
14085 n = has_patch(atoi((char *)name + 5));
14086 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 else if (STRICMP(name, "vim_starting") == 0)
14088 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000014089#ifdef FEAT_MBYTE
14090 else if (STRICMP(name, "multi_byte_encoding") == 0)
14091 n = has_mbyte;
14092#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000014093#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
14094 else if (STRICMP(name, "balloon_multiline") == 0)
14095 n = multiline_balloon_available();
14096#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097#ifdef DYNAMIC_TCL
14098 else if (STRICMP(name, "tcl") == 0)
14099 n = tcl_enabled(FALSE);
14100#endif
14101#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
14102 else if (STRICMP(name, "iconv") == 0)
14103 n = iconv_enabled(FALSE);
14104#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020014105#ifdef DYNAMIC_LUA
14106 else if (STRICMP(name, "lua") == 0)
14107 n = lua_enabled(FALSE);
14108#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000014109#ifdef DYNAMIC_MZSCHEME
14110 else if (STRICMP(name, "mzscheme") == 0)
14111 n = mzscheme_enabled(FALSE);
14112#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014113#ifdef DYNAMIC_RUBY
14114 else if (STRICMP(name, "ruby") == 0)
14115 n = ruby_enabled(FALSE);
14116#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014117#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118#ifdef DYNAMIC_PYTHON
14119 else if (STRICMP(name, "python") == 0)
14120 n = python_enabled(FALSE);
14121#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020014122#endif
14123#ifdef FEAT_PYTHON3
14124#ifdef DYNAMIC_PYTHON3
14125 else if (STRICMP(name, "python3") == 0)
14126 n = python3_enabled(FALSE);
14127#endif
14128#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014129#ifdef DYNAMIC_PERL
14130 else if (STRICMP(name, "perl") == 0)
14131 n = perl_enabled(FALSE);
14132#endif
14133#ifdef FEAT_GUI
14134 else if (STRICMP(name, "gui_running") == 0)
14135 n = (gui.in_use || gui.starting);
14136# ifdef FEAT_GUI_W32
14137 else if (STRICMP(name, "gui_win32s") == 0)
14138 n = gui_is_win32s();
14139# endif
14140# ifdef FEAT_BROWSE
14141 else if (STRICMP(name, "browse") == 0)
14142 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
14143# endif
14144#endif
14145#ifdef FEAT_SYN_HL
14146 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020014147 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148#endif
14149#if defined(WIN3264)
14150 else if (STRICMP(name, "win95") == 0)
14151 n = mch_windows95();
14152#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014153#ifdef FEAT_NETBEANS_INTG
14154 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020014155 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000014156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157 }
14158
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014159 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014160}
14161
14162/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000014163 * "has_key()" function
14164 */
14165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014166f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014167{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014168 if (argvars[0].v_type != VAR_DICT)
14169 {
14170 EMSG(_(e_dictreq));
14171 return;
14172 }
14173 if (argvars[0].vval.v_dict == NULL)
14174 return;
14175
14176 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014177 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014178}
14179
14180/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014181 * "haslocaldir()" function
14182 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014184f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014185{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014186 win_T *wp = NULL;
14187
14188 wp = find_tabwin(&argvars[0], &argvars[1]);
14189 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014190}
14191
14192/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014193 * "hasmapto()" function
14194 */
14195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014196f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197{
14198 char_u *name;
14199 char_u *mode;
14200 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014201 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014203 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014204 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 mode = (char_u *)"nvo";
14206 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014207 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014209 if (argvars[2].v_type != VAR_UNKNOWN)
14210 abbr = get_tv_number(&argvars[2]);
14211 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014212
Bram Moolenaar2c932302006-03-18 21:42:09 +000014213 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014214 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014215 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014216 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014217}
14218
14219/*
14220 * "histadd()" function
14221 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014223f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014224{
14225#ifdef FEAT_CMDHIST
14226 int histype;
14227 char_u *str;
14228 char_u buf[NUMBUFLEN];
14229#endif
14230
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014231 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014232 if (check_restricted() || check_secure())
14233 return;
14234#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014235 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14236 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014237 if (histype >= 0)
14238 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 if (*str != NUL)
14241 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014242 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014243 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014244 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245 return;
14246 }
14247 }
14248#endif
14249}
14250
14251/*
14252 * "histdel()" function
14253 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014255f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014256{
14257#ifdef FEAT_CMDHIST
14258 int n;
14259 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14263 if (str == NULL)
14264 n = 0;
14265 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014266 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014268 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014269 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014270 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014271 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272 else
14273 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014275 get_tv_string_buf(&argvars[1], buf));
14276 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277#endif
14278}
14279
14280/*
14281 * "histget()" function
14282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014284f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285{
14286#ifdef FEAT_CMDHIST
14287 int type;
14288 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014289 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014291 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14292 if (str == NULL)
14293 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 {
14296 type = get_histtype(str);
14297 if (argvars[1].v_type == VAR_UNKNOWN)
14298 idx = get_history_idx(type);
14299 else
14300 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14301 /* -1 on type error */
14302 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14303 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014307 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308}
14309
14310/*
14311 * "histnr()" function
14312 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014314f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315{
14316 int i;
14317
14318#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014319 char_u *history = get_tv_string_chk(&argvars[0]);
14320
14321 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014322 if (i >= HIST_CMD && i < HIST_COUNT)
14323 i = get_history_idx(i);
14324 else
14325#endif
14326 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014327 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014328}
14329
14330/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014331 * "highlightID(name)" function
14332 */
14333 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014334f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014335{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014336 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014337}
14338
14339/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014340 * "highlight_exists()" function
14341 */
14342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014343f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014344{
14345 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14346}
14347
14348/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349 * "hostname()" function
14350 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014351 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014352f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014353{
14354 char_u hostname[256];
14355
14356 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014357 rettv->v_type = VAR_STRING;
14358 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014359}
14360
14361/*
14362 * iconv() function
14363 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014365f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014366{
14367#ifdef FEAT_MBYTE
14368 char_u buf1[NUMBUFLEN];
14369 char_u buf2[NUMBUFLEN];
14370 char_u *from, *to, *str;
14371 vimconv_T vimconv;
14372#endif
14373
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014374 rettv->v_type = VAR_STRING;
14375 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376
14377#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014378 str = get_tv_string(&argvars[0]);
14379 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14380 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 vimconv.vc_type = CONV_NONE;
14382 convert_setup(&vimconv, from, to);
14383
14384 /* If the encodings are equal, no conversion needed. */
14385 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014386 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014388 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014389
14390 convert_setup(&vimconv, NULL, NULL);
14391 vim_free(from);
14392 vim_free(to);
14393#endif
14394}
14395
14396/*
14397 * "indent()" function
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402 linenr_T lnum;
14403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014404 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014405 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014406 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014408 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409}
14410
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014411/*
14412 * "index()" function
14413 */
14414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014415f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014416{
Bram Moolenaar33570922005-01-25 22:26:29 +000014417 list_T *l;
14418 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014419 long idx = 0;
14420 int ic = FALSE;
14421
14422 rettv->vval.v_number = -1;
14423 if (argvars[0].v_type != VAR_LIST)
14424 {
14425 EMSG(_(e_listreq));
14426 return;
14427 }
14428 l = argvars[0].vval.v_list;
14429 if (l != NULL)
14430 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014431 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014432 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014433 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014434 int error = FALSE;
14435
Bram Moolenaar758711c2005-02-02 23:11:38 +000014436 /* Start at specified item. Use the cached index that list_find()
14437 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014438 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014439 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014440 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014441 ic = get_tv_number_chk(&argvars[3], &error);
14442 if (error)
14443 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014444 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014445
Bram Moolenaar758711c2005-02-02 23:11:38 +000014446 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014447 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014448 {
14449 rettv->vval.v_number = idx;
14450 break;
14451 }
14452 }
14453}
14454
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455static int inputsecret_flag = 0;
14456
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014457static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014458
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014460 * This function is used by f_input() and f_inputdialog() functions. The third
14461 * argument to f_input() specifies the type of completion to use at the
14462 * prompt. The third argument to f_inputdialog() specifies the value to return
14463 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014464 */
14465 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014466get_user_input(
14467 typval_T *argvars,
14468 typval_T *rettv,
14469 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014470{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014471 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014472 char_u *p = NULL;
14473 int c;
14474 char_u buf[NUMBUFLEN];
14475 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014476 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014477 int xp_type = EXPAND_NOTHING;
14478 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014480 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014481 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482
14483#ifdef NO_CONSOLE_INPUT
14484 /* While starting up, there is no place to enter text. */
14485 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014487#endif
14488
14489 cmd_silent = FALSE; /* Want to see the prompt. */
14490 if (prompt != NULL)
14491 {
14492 /* Only the part of the message after the last NL is considered as
14493 * prompt for the command line */
14494 p = vim_strrchr(prompt, '\n');
14495 if (p == NULL)
14496 p = prompt;
14497 else
14498 {
14499 ++p;
14500 c = *p;
14501 *p = NUL;
14502 msg_start();
14503 msg_clr_eos();
14504 msg_puts_attr(prompt, echo_attr);
14505 msg_didout = FALSE;
14506 msg_starthere();
14507 *p = c;
14508 }
14509 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014510
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014511 if (argvars[1].v_type != VAR_UNKNOWN)
14512 {
14513 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14514 if (defstr != NULL)
14515 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014516
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014517 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014518 {
14519 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014520 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014521 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014522
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014523 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014524 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014525
Bram Moolenaar4463f292005-09-25 22:20:24 +000014526 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14527 if (xp_name == NULL)
14528 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014529
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014530 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014531
Bram Moolenaar4463f292005-09-25 22:20:24 +000014532 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14533 &xp_arg) == FAIL)
14534 return;
14535 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014536 }
14537
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014538 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014539 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014540 int save_ex_normal_busy = ex_normal_busy;
14541 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014542 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014543 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14544 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014545 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014546 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014547 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014548 && argvars[1].v_type != VAR_UNKNOWN
14549 && argvars[2].v_type != VAR_UNKNOWN)
14550 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14551 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014552
14553 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014554
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014555 /* since the user typed this, no need to wait for return */
14556 need_wait_return = FALSE;
14557 msg_didout = FALSE;
14558 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014559 cmd_silent = cmd_silent_save;
14560}
14561
14562/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014563 * "input()" function
14564 * Also handles inputsecret() when inputsecret is set.
14565 */
14566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014567f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014568{
14569 get_user_input(argvars, rettv, FALSE);
14570}
14571
14572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014573 * "inputdialog()" function
14574 */
14575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014576f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014577{
14578#if defined(FEAT_GUI_TEXTDIALOG)
14579 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14580 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14581 {
14582 char_u *message;
14583 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014584 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014585
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014586 message = get_tv_string_chk(&argvars[0]);
14587 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014588 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014589 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014590 else
14591 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014592 if (message != NULL && defstr != NULL
14593 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014594 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014595 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014596 else
14597 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014598 if (message != NULL && defstr != NULL
14599 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014600 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014601 rettv->vval.v_string = vim_strsave(
14602 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014603 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014604 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014605 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014606 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014607 }
14608 else
14609#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014610 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014611}
14612
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014613/*
14614 * "inputlist()" function
14615 */
14616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014617f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014618{
14619 listitem_T *li;
14620 int selected;
14621 int mouse_used;
14622
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014623#ifdef NO_CONSOLE_INPUT
14624 /* While starting up, there is no place to enter text. */
14625 if (no_console_input())
14626 return;
14627#endif
14628 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14629 {
14630 EMSG2(_(e_listarg), "inputlist()");
14631 return;
14632 }
14633
14634 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014635 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014636 lines_left = Rows; /* avoid more prompt */
14637 msg_scroll = TRUE;
14638 msg_clr_eos();
14639
14640 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14641 {
14642 msg_puts(get_tv_string(&li->li_tv));
14643 msg_putchar('\n');
14644 }
14645
14646 /* Ask for choice. */
14647 selected = prompt_for_number(&mouse_used);
14648 if (mouse_used)
14649 selected -= lines_left;
14650
14651 rettv->vval.v_number = selected;
14652}
14653
14654
Bram Moolenaar071d4272004-06-13 20:20:40 +000014655static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14656
14657/*
14658 * "inputrestore()" function
14659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014661f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014662{
14663 if (ga_userinput.ga_len > 0)
14664 {
14665 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014666 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14667 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014668 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014669 }
14670 else if (p_verbose > 1)
14671 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014672 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014673 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014674 }
14675}
14676
14677/*
14678 * "inputsave()" function
14679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014681f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014682{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014683 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014684 if (ga_grow(&ga_userinput, 1) == OK)
14685 {
14686 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14687 + ga_userinput.ga_len);
14688 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014689 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014690 }
14691 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014692 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014693}
14694
14695/*
14696 * "inputsecret()" function
14697 */
14698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014699f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014700{
14701 ++cmdline_star;
14702 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014703 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014704 --cmdline_star;
14705 --inputsecret_flag;
14706}
14707
14708/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014709 * "insert()" function
14710 */
14711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014712f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014713{
14714 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014715 listitem_T *item;
14716 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014717 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014718
14719 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014720 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014721 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014722 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014723 {
14724 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014725 before = get_tv_number_chk(&argvars[2], &error);
14726 if (error)
14727 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014728
Bram Moolenaar758711c2005-02-02 23:11:38 +000014729 if (before == l->lv_len)
14730 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014731 else
14732 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014733 item = list_find(l, before);
14734 if (item == NULL)
14735 {
14736 EMSGN(_(e_listidx), before);
14737 l = NULL;
14738 }
14739 }
14740 if (l != NULL)
14741 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014742 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014743 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014744 }
14745 }
14746}
14747
14748/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014749 * "invert(expr)" function
14750 */
14751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014752f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014753{
14754 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14755}
14756
14757/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014758 * "isdirectory()" function
14759 */
14760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014761f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014762{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014763 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014764}
14765
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014766/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014767 * "islocked()" function
14768 */
14769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014770f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014771{
14772 lval_T lv;
14773 char_u *end;
14774 dictitem_T *di;
14775
14776 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014777 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14778 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014779 if (end != NULL && lv.ll_name != NULL)
14780 {
14781 if (*end != NUL)
14782 EMSG(_(e_trailing));
14783 else
14784 {
14785 if (lv.ll_tv == NULL)
14786 {
14787 if (check_changedtick(lv.ll_name))
14788 rettv->vval.v_number = 1; /* always locked */
14789 else
14790 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014791 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014792 if (di != NULL)
14793 {
14794 /* Consider a variable locked when:
14795 * 1. the variable itself is locked
14796 * 2. the value of the variable is locked.
14797 * 3. the List or Dict value is locked.
14798 */
14799 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14800 || tv_islocked(&di->di_tv));
14801 }
14802 }
14803 }
14804 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014805 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014806 else if (lv.ll_newkey != NULL)
14807 EMSG2(_(e_dictkey), lv.ll_newkey);
14808 else if (lv.ll_list != NULL)
14809 /* List item. */
14810 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14811 else
14812 /* Dictionary item. */
14813 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14814 }
14815 }
14816
14817 clear_lval(&lv);
14818}
14819
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014820#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14821/*
14822 * "isnan()" function
14823 */
14824 static void
14825f_isnan(typval_T *argvars, typval_T *rettv)
14826{
14827 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14828 && isnan(argvars[0].vval.v_float);
14829}
14830#endif
14831
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014832static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014833
14834/*
14835 * Turn a dict into a list:
14836 * "what" == 0: list of keys
14837 * "what" == 1: list of values
14838 * "what" == 2: list of items
14839 */
14840 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014841dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014842{
Bram Moolenaar33570922005-01-25 22:26:29 +000014843 list_T *l2;
14844 dictitem_T *di;
14845 hashitem_T *hi;
14846 listitem_T *li;
14847 listitem_T *li2;
14848 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014849 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014850
Bram Moolenaar8c711452005-01-14 21:53:12 +000014851 if (argvars[0].v_type != VAR_DICT)
14852 {
14853 EMSG(_(e_dictreq));
14854 return;
14855 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014856 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014857 return;
14858
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014859 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014860 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014861
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014862 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014863 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014864 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014865 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014866 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014867 --todo;
14868 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014869
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014870 li = listitem_alloc();
14871 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014872 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014873 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014874
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014875 if (what == 0)
14876 {
14877 /* keys() */
14878 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014879 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014880 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14881 }
14882 else if (what == 1)
14883 {
14884 /* values() */
14885 copy_tv(&di->di_tv, &li->li_tv);
14886 }
14887 else
14888 {
14889 /* items() */
14890 l2 = list_alloc();
14891 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014892 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014893 li->li_tv.vval.v_list = l2;
14894 if (l2 == NULL)
14895 break;
14896 ++l2->lv_refcount;
14897
14898 li2 = listitem_alloc();
14899 if (li2 == NULL)
14900 break;
14901 list_append(l2, li2);
14902 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014903 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014904 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14905
14906 li2 = listitem_alloc();
14907 if (li2 == NULL)
14908 break;
14909 list_append(l2, li2);
14910 copy_tv(&di->di_tv, &li2->li_tv);
14911 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014912 }
14913 }
14914}
14915
14916/*
14917 * "items(dict)" function
14918 */
14919 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014920f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014921{
14922 dict_list(argvars, rettv, 2);
14923}
14924
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014925#if defined(FEAT_JOB) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014926/*
14927 * Get the job from the argument.
14928 * Returns NULL if the job is invalid.
14929 */
14930 static job_T *
14931get_job_arg(typval_T *tv)
14932{
14933 job_T *job;
14934
14935 if (tv->v_type != VAR_JOB)
14936 {
14937 EMSG2(_(e_invarg2), get_tv_string(tv));
14938 return NULL;
14939 }
14940 job = tv->vval.v_job;
14941
14942 if (job == NULL)
14943 EMSG(_("E916: not a valid job"));
14944 return job;
14945}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014946
14947# ifdef FEAT_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010014948/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014949 * "job_getchannel()" function
14950 */
14951 static void
14952f_job_getchannel(typval_T *argvars, typval_T *rettv)
14953{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014954 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014955
Bram Moolenaar65edff82016-02-21 16:40:11 +010014956 if (job != NULL)
14957 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014958 rettv->v_type = VAR_CHANNEL;
14959 rettv->vval.v_channel = job->jv_channel;
14960 if (job->jv_channel != NULL)
14961 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014962 }
14963}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014964# endif
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014965
14966/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014967 * "job_setoptions()" function
14968 */
14969 static void
14970f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14971{
14972 job_T *job = get_job_arg(&argvars[0]);
14973 jobopt_T opt;
14974
14975 if (job == NULL)
14976 return;
14977 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014978 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014979 return;
14980 job_set_options(job, &opt);
14981}
14982
14983/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014984 * "job_start()" function
14985 */
14986 static void
14987f_job_start(typval_T *argvars UNUSED, typval_T *rettv)
14988{
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014989 job_T *job;
14990 char_u *cmd = NULL;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014991#if defined(UNIX)
14992# define USE_ARGV
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014993 char **argv = NULL;
14994 int argc = 0;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014995#else
Bram Moolenaarba093bc2016-02-16 19:37:29 +010014996 garray_T ga;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014997#endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010014998 jobopt_T opt;
Bram Moolenaar835dc632016-02-07 14:27:38 +010014999
15000 rettv->v_type = VAR_JOB;
15001 job = job_alloc();
15002 rettv->vval.v_job = job;
15003 if (job == NULL)
15004 return;
15005
15006 rettv->vval.v_job->jv_status = JOB_FAILED;
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015007
15008 /* Default mode is NL. */
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015009 clear_job_options(&opt);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015010 opt.jo_mode = MODE_NL;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010015011 if (get_job_options(&argvars[1], &opt,
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015012 JO_MODE_ALL + JO_CB_ALL + JO_TIMEOUT_ALL
Bram Moolenaar187db502016-02-27 14:44:26 +010015013 + JO_STOPONEXIT + JO_EXIT_CB + JO_OUT_IO) == FAIL)
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015014 return;
Bram Moolenaar65edff82016-02-21 16:40:11 +010015015 job_set_options(job, &opt);
Bram Moolenaarba093bc2016-02-16 19:37:29 +010015016
Bram Moolenaar835dc632016-02-07 14:27:38 +010015017#ifndef USE_ARGV
Bram Moolenaar942d6b22016-02-07 19:57:16 +010015018 ga_init2(&ga, (int)sizeof(char*), 20);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015019#endif
15020
15021 if (argvars[0].v_type == VAR_STRING)
15022 {
15023 /* Command is a string. */
15024 cmd = argvars[0].vval.v_string;
15025#ifdef USE_ARGV
15026 if (mch_parse_cmd(cmd, FALSE, &argv, &argc) == FAIL)
15027 return;
15028 argv[argc] = NULL;
15029#endif
15030 }
15031 else if (argvars[0].v_type != VAR_LIST
15032 || argvars[0].vval.v_list == NULL
15033 || argvars[0].vval.v_list->lv_len < 1)
15034 {
15035 EMSG(_(e_invarg));
15036 return;
15037 }
15038 else
15039 {
15040 list_T *l = argvars[0].vval.v_list;
15041 listitem_T *li;
15042 char_u *s;
15043
15044#ifdef USE_ARGV
15045 /* Pass argv[] to mch_call_shell(). */
15046 argv = (char **)alloc(sizeof(char *) * (l->lv_len + 1));
15047 if (argv == NULL)
15048 return;
15049#endif
15050 for (li = l->lv_first; li != NULL; li = li->li_next)
15051 {
15052 s = get_tv_string_chk(&li->li_tv);
15053 if (s == NULL)
15054 goto theend;
15055#ifdef USE_ARGV
15056 argv[argc++] = (char *)s;
15057#else
15058 if (li != l->lv_first)
15059 {
15060 s = vim_strsave_shellescape(s, FALSE, TRUE);
15061 if (s == NULL)
15062 goto theend;
Bram Moolenaar76467df2016-02-12 19:30:26 +010015063 ga_concat(&ga, s);
15064 vim_free(s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015065 }
Bram Moolenaar76467df2016-02-12 19:30:26 +010015066 else
15067 ga_concat(&ga, s);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015068 if (li->li_next != NULL)
15069 ga_append(&ga, ' ');
15070#endif
15071 }
15072#ifdef USE_ARGV
15073 argv[argc] = NULL;
15074#else
15075 cmd = ga.ga_data;
15076#endif
15077 }
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015078
Bram Moolenaar835dc632016-02-07 14:27:38 +010015079#ifdef USE_ARGV
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015080# ifdef FEAT_CHANNEL
15081 if (ch_log_active())
15082 {
15083 garray_T ga;
15084 int i;
15085
15086 ga_init2(&ga, (int)sizeof(char), 200);
15087 for (i = 0; i < argc; ++i)
15088 {
15089 if (i > 0)
15090 ga_concat(&ga, (char_u *)" ");
15091 ga_concat(&ga, (char_u *)argv[i]);
15092 }
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015093 ch_logs(NULL, "Starting job: %s", (char *)ga.ga_data);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015094 ga_clear(&ga);
15095 }
15096# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015097 mch_start_job(argv, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015098#else
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015099# ifdef FEAT_CHANNEL
Bram Moolenaared5a78e2016-02-19 21:05:03 +010015100 ch_logs(NULL, "Starting job: %s", (char *)cmd);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010015101# endif
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010015102 mch_start_job((char *)cmd, job, &opt);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015103#endif
15104
15105theend:
15106#ifdef USE_ARGV
Bram Moolenaaree5aeae2016-02-07 22:30:47 +010015107 vim_free(argv);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015108#else
15109 vim_free(ga.ga_data);
15110#endif
15111}
15112
15113/*
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015114 * Get the status of "job" and invoke the exit callback when needed.
15115 * The returned string is not allocated.
15116 */
15117 static char *
15118job_status(job_T *job)
15119{
15120 char *result;
15121
15122 if (job->jv_status == JOB_ENDED)
15123 /* No need to check, dead is dead. */
15124 result = "dead";
15125 else if (job->jv_status == JOB_FAILED)
15126 result = "fail";
15127 else
15128 {
15129 result = mch_job_status(job);
15130# ifdef FEAT_CHANNEL
15131 if (job->jv_status == JOB_ENDED)
15132 ch_log(job->jv_channel, "Job ended");
15133# endif
15134 if (job->jv_status == JOB_ENDED && job->jv_exit_cb != NULL)
15135 {
15136 typval_T argv[3];
15137 typval_T rettv;
15138 int dummy;
15139
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015140 /* invoke the exit callback; make sure the refcount is > 0 */
15141 ++job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015142 argv[0].v_type = VAR_JOB;
15143 argv[0].vval.v_job = job;
15144 argv[1].v_type = VAR_NUMBER;
15145 argv[1].vval.v_number = job->jv_exitval;
15146 call_func(job->jv_exit_cb, (int)STRLEN(job->jv_exit_cb),
15147 &rettv, 2, argv, 0L, 0L, &dummy, TRUE, NULL);
15148 clear_tv(&rettv);
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015149 --job->jv_refcount;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015150 }
15151 if (job->jv_status == JOB_ENDED && job->jv_refcount == 0)
15152 {
Bram Moolenaar23c463a2016-02-22 11:39:27 +010015153 /* The job was already unreferenced, now that it ended it can be
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015154 * freed. Careful: caller must not use "job" after this! */
15155 job_free(job);
15156 }
15157 }
15158 return result;
15159}
15160
15161/*
15162 * Called once in a while: check if any jobs with an "exit-cb" have ended.
15163 */
15164 void
Bram Moolenaarb2bd6a02016-02-22 20:20:25 +010015165job_check_ended(void)
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015166{
15167 static time_t last_check = 0;
15168 time_t now;
15169 job_T *job;
15170 job_T *next;
15171
15172 /* Only do this once in 10 seconds. */
15173 now = time(NULL);
15174 if (last_check + 10 < now)
15175 {
15176 last_check = now;
15177 for (job = first_job; job != NULL; job = next)
15178 {
15179 next = job->jv_next;
15180 if (job->jv_status == JOB_STARTED && job->jv_exit_cb != NULL)
15181 job_status(job); /* may free "job" */
15182 }
15183 }
15184}
15185
15186/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010015187 * "job_status()" function
15188 */
15189 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010015190f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015191{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015192 job_T *job = get_job_arg(&argvars[0]);
15193 char *result;
Bram Moolenaar835dc632016-02-07 14:27:38 +010015194
Bram Moolenaar65edff82016-02-21 16:40:11 +010015195 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015196 {
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010015197 result = job_status(job);
Bram Moolenaar835dc632016-02-07 14:27:38 +010015198 rettv->v_type = VAR_STRING;
15199 rettv->vval.v_string = vim_strsave((char_u *)result);
15200 }
15201}
15202
15203/*
15204 * "job_stop()" function
15205 */
15206 static void
15207f_job_stop(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
15208{
Bram Moolenaar65edff82016-02-21 16:40:11 +010015209 job_T *job = get_job_arg(&argvars[0]);
15210
15211 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015212 {
15213 char_u *arg;
15214
15215 if (argvars[1].v_type == VAR_UNKNOWN)
15216 arg = (char_u *)"";
15217 else
15218 {
15219 arg = get_tv_string_chk(&argvars[1]);
15220 if (arg == NULL)
15221 {
15222 EMSG(_(e_invarg));
15223 return;
15224 }
15225 }
Bram Moolenaar65edff82016-02-21 16:40:11 +010015226 if (mch_stop_job(job, arg) == FAIL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010015227 rettv->vval.v_number = 0;
15228 else
Bram Moolenaar46c85432016-02-26 11:17:46 +010015229 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010015230 rettv->vval.v_number = 1;
Bram Moolenaar46c85432016-02-26 11:17:46 +010015231 /* Assume that "hup" does not kill the job. */
15232 if (job->jv_channel != NULL && STRCMP(arg, "hup") != 0)
15233 job->jv_channel->ch_job_killed = TRUE;
15234 }
15235 /* We don't try freeing the job, obviously the caller still has a
15236 * reference to it. */
Bram Moolenaar835dc632016-02-07 14:27:38 +010015237 }
15238}
15239#endif
15240
Bram Moolenaar071d4272004-06-13 20:20:40 +000015241/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015242 * "join()" function
15243 */
15244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015245f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015246{
15247 garray_T ga;
15248 char_u *sep;
15249
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 if (argvars[0].v_type != VAR_LIST)
15251 {
15252 EMSG(_(e_listreq));
15253 return;
15254 }
15255 if (argvars[0].vval.v_list == NULL)
15256 return;
15257 if (argvars[1].v_type == VAR_UNKNOWN)
15258 sep = (char_u *)" ";
15259 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015260 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015261
15262 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015263
15264 if (sep != NULL)
15265 {
15266 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000015267 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015268 ga_append(&ga, NUL);
15269 rettv->vval.v_string = (char_u *)ga.ga_data;
15270 }
15271 else
15272 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015273}
15274
15275/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015276 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015277 */
15278 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015279f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015280{
15281 js_read_T reader;
15282
15283 reader.js_buf = get_tv_string(&argvars[0]);
15284 reader.js_fill = NULL;
15285 reader.js_used = 0;
15286 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
15287 EMSG(_(e_invarg));
15288}
15289
15290/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015291 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015292 */
15293 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015294f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015295{
15296 rettv->v_type = VAR_STRING;
15297 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
15298}
15299
15300/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015301 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015302 */
15303 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015304f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015305{
15306 js_read_T reader;
15307
15308 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010015309 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015310 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015311 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010015312 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015313}
15314
15315/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015316 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015317 */
15318 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010015319f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015320{
15321 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010015322 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010015323}
15324
15325/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000015326 * "keys()" function
15327 */
15328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015329f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000015330{
15331 dict_list(argvars, rettv, 0);
15332}
15333
15334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015335 * "last_buffer_nr()" function.
15336 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015338f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015339{
15340 int n = 0;
15341 buf_T *buf;
15342
15343 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
15344 if (n < buf->b_fnum)
15345 n = buf->b_fnum;
15346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015348}
15349
15350/*
15351 * "len()" function
15352 */
15353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015354f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015355{
15356 switch (argvars[0].v_type)
15357 {
15358 case VAR_STRING:
15359 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015360 rettv->vval.v_number = (varnumber_T)STRLEN(
15361 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015362 break;
15363 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015364 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015365 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015366 case VAR_DICT:
15367 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
15368 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010015369 case VAR_UNKNOWN:
15370 case VAR_SPECIAL:
15371 case VAR_FLOAT:
15372 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010015373 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010015374 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000015375 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015376 break;
15377 }
15378}
15379
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015380static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015381
15382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015383libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015384{
15385#ifdef FEAT_LIBCALL
15386 char_u *string_in;
15387 char_u **string_result;
15388 int nr_result;
15389#endif
15390
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015391 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015392 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015393 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015394
15395 if (check_restricted() || check_secure())
15396 return;
15397
15398#ifdef FEAT_LIBCALL
15399 /* The first two args must be strings, otherwise its meaningless */
15400 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15401 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015402 string_in = NULL;
15403 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015404 string_in = argvars[2].vval.v_string;
15405 if (type == VAR_NUMBER)
15406 string_result = NULL;
15407 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015408 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015409 if (mch_libcall(argvars[0].vval.v_string,
15410 argvars[1].vval.v_string,
15411 string_in,
15412 argvars[2].vval.v_number,
15413 string_result,
15414 &nr_result) == OK
15415 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015416 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015417 }
15418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015419}
15420
15421/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422 * "libcall()" function
15423 */
15424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015426{
15427 libcall_common(argvars, rettv, VAR_STRING);
15428}
15429
15430/*
15431 * "libcallnr()" function
15432 */
15433 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015434f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015435{
15436 libcall_common(argvars, rettv, VAR_NUMBER);
15437}
15438
15439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015440 * "line(string)" function
15441 */
15442 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015443f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015444{
15445 linenr_T lnum = 0;
15446 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015447 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015448
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015449 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015450 if (fp != NULL)
15451 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015452 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015453}
15454
15455/*
15456 * "line2byte(lnum)" function
15457 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015459f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015460{
15461#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015462 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463#else
15464 linenr_T lnum;
15465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015466 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015467 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015468 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015469 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015470 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15471 if (rettv->vval.v_number >= 0)
15472 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015473#endif
15474}
15475
15476/*
15477 * "lispindent(lnum)" function
15478 */
15479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015480f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015481{
15482#ifdef FEAT_LISP
15483 pos_T pos;
15484 linenr_T lnum;
15485
15486 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015487 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015488 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15489 {
15490 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015491 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015492 curwin->w_cursor = pos;
15493 }
15494 else
15495#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015496 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015497}
15498
15499/*
15500 * "localtime()" function
15501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015503f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015504{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015505 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506}
15507
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015508static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015509
15510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015511get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015512{
15513 char_u *keys;
15514 char_u *which;
15515 char_u buf[NUMBUFLEN];
15516 char_u *keys_buf = NULL;
15517 char_u *rhs;
15518 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015519 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015520 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015521 mapblock_T *mp;
15522 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015523
15524 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015525 rettv->v_type = VAR_STRING;
15526 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015527
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015528 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015529 if (*keys == NUL)
15530 return;
15531
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015532 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015534 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015535 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015536 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015537 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015538 if (argvars[3].v_type != VAR_UNKNOWN)
15539 get_dict = get_tv_number(&argvars[3]);
15540 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015541 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015542 else
15543 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015544 if (which == NULL)
15545 return;
15546
Bram Moolenaar071d4272004-06-13 20:20:40 +000015547 mode = get_map_mode(&which, 0);
15548
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015549 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015550 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015551 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015552
15553 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015554 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015555 /* Return a string. */
15556 if (rhs != NULL)
15557 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015558
Bram Moolenaarbd743252010-10-20 21:23:33 +020015559 }
15560 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15561 {
15562 /* Return a dictionary. */
15563 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15564 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15565 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015566
Bram Moolenaarbd743252010-10-20 21:23:33 +020015567 dict_add_nr_str(dict, "lhs", 0L, lhs);
15568 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15569 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15570 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15571 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15572 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15573 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015574 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015575 dict_add_nr_str(dict, "mode", 0L, mapmode);
15576
15577 vim_free(lhs);
15578 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015579 }
15580}
15581
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015582#ifdef FEAT_FLOAT
15583/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015584 * "log()" function
15585 */
15586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015587f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015588{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015589 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015590
15591 rettv->v_type = VAR_FLOAT;
15592 if (get_float_arg(argvars, &f) == OK)
15593 rettv->vval.v_float = log(f);
15594 else
15595 rettv->vval.v_float = 0.0;
15596}
15597
15598/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015599 * "log10()" function
15600 */
15601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015602f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015603{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015604 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015605
15606 rettv->v_type = VAR_FLOAT;
15607 if (get_float_arg(argvars, &f) == OK)
15608 rettv->vval.v_float = log10(f);
15609 else
15610 rettv->vval.v_float = 0.0;
15611}
15612#endif
15613
Bram Moolenaar1dced572012-04-05 16:54:08 +020015614#ifdef FEAT_LUA
15615/*
15616 * "luaeval()" function
15617 */
15618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015619f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015620{
15621 char_u *str;
15622 char_u buf[NUMBUFLEN];
15623
15624 str = get_tv_string_buf(&argvars[0], buf);
15625 do_luaeval(str, argvars + 1, rettv);
15626}
15627#endif
15628
Bram Moolenaar071d4272004-06-13 20:20:40 +000015629/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015630 * "map()" function
15631 */
15632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015633f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015634{
15635 filter_map(argvars, rettv, TRUE);
15636}
15637
15638/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015639 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015640 */
15641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015642f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015643{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015644 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015645}
15646
15647/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015648 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015649 */
15650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015651f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015652{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015654}
15655
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015656static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015657
15658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015659find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015660{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015661 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015662 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015663 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015664 char_u *pat;
15665 regmatch_T regmatch;
15666 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015667 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015668 char_u *save_cpo;
15669 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015670 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015671 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015672 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015673 list_T *l = NULL;
15674 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015675 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015676 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015677
15678 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15679 save_cpo = p_cpo;
15680 p_cpo = (char_u *)"";
15681
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015682 rettv->vval.v_number = -1;
15683 if (type == 3)
15684 {
15685 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015686 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015687 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015688 }
15689 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015690 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015691 rettv->v_type = VAR_STRING;
15692 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015694
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015695 if (argvars[0].v_type == VAR_LIST)
15696 {
15697 if ((l = argvars[0].vval.v_list) == NULL)
15698 goto theend;
15699 li = l->lv_first;
15700 }
15701 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015702 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015703 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015704 len = (long)STRLEN(str);
15705 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015706
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015707 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15708 if (pat == NULL)
15709 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015710
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015711 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015713 int error = FALSE;
15714
15715 start = get_tv_number_chk(&argvars[2], &error);
15716 if (error)
15717 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015718 if (l != NULL)
15719 {
15720 li = list_find(l, start);
15721 if (li == NULL)
15722 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015723 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015724 }
15725 else
15726 {
15727 if (start < 0)
15728 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015729 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015730 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015731 /* When "count" argument is there ignore matches before "start",
15732 * otherwise skip part of the string. Differs when pattern is "^"
15733 * or "\<". */
15734 if (argvars[3].v_type != VAR_UNKNOWN)
15735 startcol = start;
15736 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015737 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015738 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015739 len -= start;
15740 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015741 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015742
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015743 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015744 nth = get_tv_number_chk(&argvars[3], &error);
15745 if (error)
15746 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015747 }
15748
15749 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15750 if (regmatch.regprog != NULL)
15751 {
15752 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015753
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015754 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015755 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015756 if (l != NULL)
15757 {
15758 if (li == NULL)
15759 {
15760 match = FALSE;
15761 break;
15762 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015763 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015764 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015765 if (str == NULL)
15766 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015767 }
15768
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015769 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015770
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015771 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015772 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015773 if (l == NULL && !match)
15774 break;
15775
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015776 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015777 if (l != NULL)
15778 {
15779 li = li->li_next;
15780 ++idx;
15781 }
15782 else
15783 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015784#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015785 startcol = (colnr_T)(regmatch.startp[0]
15786 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015787#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015788 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015789#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015790 if (startcol > (colnr_T)len
15791 || str + startcol <= regmatch.startp[0])
15792 {
15793 match = FALSE;
15794 break;
15795 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015796 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015797 }
15798
15799 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015800 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015801 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015802 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015803 int i;
15804
15805 /* return list with matched string and submatches */
15806 for (i = 0; i < NSUBEXP; ++i)
15807 {
15808 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015809 {
15810 if (list_append_string(rettv->vval.v_list,
15811 (char_u *)"", 0) == FAIL)
15812 break;
15813 }
15814 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015815 regmatch.startp[i],
15816 (int)(regmatch.endp[i] - regmatch.startp[i]))
15817 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015818 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015819 }
15820 }
15821 else if (type == 2)
15822 {
15823 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015824 if (l != NULL)
15825 copy_tv(&li->li_tv, rettv);
15826 else
15827 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015828 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015829 }
15830 else if (l != NULL)
15831 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832 else
15833 {
15834 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015835 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015836 (varnumber_T)(regmatch.startp[0] - str);
15837 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015838 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015840 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 }
15842 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015843 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015844 }
15845
15846theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015847 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015848 p_cpo = save_cpo;
15849}
15850
15851/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015852 * "match()" function
15853 */
15854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015855f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015856{
15857 find_some_match(argvars, rettv, 1);
15858}
15859
15860/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015861 * "matchadd()" function
15862 */
15863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015864f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015865{
15866#ifdef FEAT_SEARCH_EXTRA
15867 char_u buf[NUMBUFLEN];
15868 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15869 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15870 int prio = 10; /* default priority */
15871 int id = -1;
15872 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015873 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015874
15875 rettv->vval.v_number = -1;
15876
15877 if (grp == NULL || pat == NULL)
15878 return;
15879 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015880 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015881 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015882 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015883 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015884 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015885 if (argvars[4].v_type != VAR_UNKNOWN)
15886 {
15887 if (argvars[4].v_type != VAR_DICT)
15888 {
15889 EMSG(_(e_dictreq));
15890 return;
15891 }
15892 if (dict_find(argvars[4].vval.v_dict,
15893 (char_u *)"conceal", -1) != NULL)
15894 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15895 (char_u *)"conceal", FALSE);
15896 }
15897 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015898 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015899 if (error == TRUE)
15900 return;
15901 if (id >= 1 && id <= 3)
15902 {
15903 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15904 return;
15905 }
15906
Bram Moolenaar6561d522015-07-21 15:48:27 +020015907 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15908 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015909#endif
15910}
15911
15912/*
15913 * "matchaddpos()" function
15914 */
15915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015916f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015917{
15918#ifdef FEAT_SEARCH_EXTRA
15919 char_u buf[NUMBUFLEN];
15920 char_u *group;
15921 int prio = 10;
15922 int id = -1;
15923 int error = FALSE;
15924 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015925 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015926
15927 rettv->vval.v_number = -1;
15928
15929 group = get_tv_string_buf_chk(&argvars[0], buf);
15930 if (group == NULL)
15931 return;
15932
15933 if (argvars[1].v_type != VAR_LIST)
15934 {
15935 EMSG2(_(e_listarg), "matchaddpos()");
15936 return;
15937 }
15938 l = argvars[1].vval.v_list;
15939 if (l == NULL)
15940 return;
15941
15942 if (argvars[2].v_type != VAR_UNKNOWN)
15943 {
15944 prio = get_tv_number_chk(&argvars[2], &error);
15945 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015946 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015947 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015948 if (argvars[4].v_type != VAR_UNKNOWN)
15949 {
15950 if (argvars[4].v_type != VAR_DICT)
15951 {
15952 EMSG(_(e_dictreq));
15953 return;
15954 }
15955 if (dict_find(argvars[4].vval.v_dict,
15956 (char_u *)"conceal", -1) != NULL)
15957 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15958 (char_u *)"conceal", FALSE);
15959 }
15960 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015961 }
15962 if (error == TRUE)
15963 return;
15964
15965 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15966 if (id == 1 || id == 2)
15967 {
15968 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15969 return;
15970 }
15971
Bram Moolenaar6561d522015-07-21 15:48:27 +020015972 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15973 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015974#endif
15975}
15976
15977/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015978 * "matcharg()" function
15979 */
15980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015981f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015982{
15983 if (rettv_list_alloc(rettv) == OK)
15984 {
15985#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015986 int id = get_tv_number(&argvars[0]);
15987 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015988
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015989 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015990 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015991 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15992 {
15993 list_append_string(rettv->vval.v_list,
15994 syn_id2name(m->hlg_id), -1);
15995 list_append_string(rettv->vval.v_list, m->pattern, -1);
15996 }
15997 else
15998 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015999 list_append_string(rettv->vval.v_list, NULL, -1);
16000 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016001 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016002 }
16003#endif
16004 }
16005}
16006
16007/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016008 * "matchdelete()" function
16009 */
16010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016011f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000016012{
16013#ifdef FEAT_SEARCH_EXTRA
16014 rettv->vval.v_number = match_delete(curwin,
16015 (int)get_tv_number(&argvars[0]), TRUE);
16016#endif
16017}
16018
16019/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016020 * "matchend()" function
16021 */
16022 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016023f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016024{
16025 find_some_match(argvars, rettv, 0);
16026}
16027
16028/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016029 * "matchlist()" function
16030 */
16031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016032f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016033{
16034 find_some_match(argvars, rettv, 3);
16035}
16036
16037/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016038 * "matchstr()" function
16039 */
16040 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016041f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016042{
16043 find_some_match(argvars, rettv, 2);
16044}
16045
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016046static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016047
16048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016049max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016050{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016051 long n = 0;
16052 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016053 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016054
16055 if (argvars[0].v_type == VAR_LIST)
16056 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016057 list_T *l;
16058 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016059
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016060 l = argvars[0].vval.v_list;
16061 if (l != NULL)
16062 {
16063 li = l->lv_first;
16064 if (li != NULL)
16065 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016066 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000016067 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016068 {
16069 li = li->li_next;
16070 if (li == NULL)
16071 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016072 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016073 if (domax ? i > n : i < n)
16074 n = i;
16075 }
16076 }
16077 }
16078 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000016079 else if (argvars[0].v_type == VAR_DICT)
16080 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016081 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016082 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000016083 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016084 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000016085
16086 d = argvars[0].vval.v_dict;
16087 if (d != NULL)
16088 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000016089 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000016090 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016091 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016092 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000016093 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016094 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016095 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016096 if (first)
16097 {
16098 n = i;
16099 first = FALSE;
16100 }
16101 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000016102 n = i;
16103 }
16104 }
16105 }
16106 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016107 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000016108 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016109 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016110}
16111
16112/*
16113 * "max()" function
16114 */
16115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016116f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016117{
16118 max_min(argvars, rettv, TRUE);
16119}
16120
16121/*
16122 * "min()" function
16123 */
16124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016125f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000016126{
16127 max_min(argvars, rettv, FALSE);
16128}
16129
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016130static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016131
16132/*
16133 * Create the directory in which "dir" is located, and higher levels when
16134 * needed.
16135 */
16136 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016137mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016138{
16139 char_u *p;
16140 char_u *updir;
16141 int r = FAIL;
16142
16143 /* Get end of directory name in "dir".
16144 * We're done when it's "/" or "c:/". */
16145 p = gettail_sep(dir);
16146 if (p <= get_past_head(dir))
16147 return OK;
16148
16149 /* If the directory exists we're done. Otherwise: create it.*/
16150 updir = vim_strnsave(dir, (int)(p - dir));
16151 if (updir == NULL)
16152 return FAIL;
16153 if (mch_isdir(updir))
16154 r = OK;
16155 else if (mkdir_recurse(updir, prot) == OK)
16156 r = vim_mkdir_emsg(updir, prot);
16157 vim_free(updir);
16158 return r;
16159}
16160
16161#ifdef vim_mkdir
16162/*
16163 * "mkdir()" function
16164 */
16165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016166f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016167{
16168 char_u *dir;
16169 char_u buf[NUMBUFLEN];
16170 int prot = 0755;
16171
16172 rettv->vval.v_number = FAIL;
16173 if (check_restricted() || check_secure())
16174 return;
16175
16176 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016177 if (*dir == NUL)
16178 rettv->vval.v_number = FAIL;
16179 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016180 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020016181 if (*gettail(dir) == NUL)
16182 /* remove trailing slashes */
16183 *gettail_sep(dir) = NUL;
16184
16185 if (argvars[1].v_type != VAR_UNKNOWN)
16186 {
16187 if (argvars[2].v_type != VAR_UNKNOWN)
16188 prot = get_tv_number_chk(&argvars[2], NULL);
16189 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
16190 mkdir_recurse(dir, prot);
16191 }
16192 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016193 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016194}
16195#endif
16196
Bram Moolenaar0d660222005-01-07 21:51:51 +000016197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016198 * "mode()" function
16199 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000016200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016201f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016202{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016203 char_u buf[3];
16204
16205 buf[1] = NUL;
16206 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016207
Bram Moolenaar071d4272004-06-13 20:20:40 +000016208 if (VIsual_active)
16209 {
16210 if (VIsual_select)
16211 buf[0] = VIsual_mode + 's' - 'v';
16212 else
16213 buf[0] = VIsual_mode;
16214 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010016215 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016216 || State == CONFIRM)
16217 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016218 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016219 if (State == ASKMORE)
16220 buf[1] = 'm';
16221 else if (State == CONFIRM)
16222 buf[1] = '?';
16223 }
16224 else if (State == EXTERNCMD)
16225 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000016226 else if (State & INSERT)
16227 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016228#ifdef FEAT_VREPLACE
16229 if (State & VREPLACE_FLAG)
16230 {
16231 buf[0] = 'R';
16232 buf[1] = 'v';
16233 }
16234 else
16235#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016236 if (State & REPLACE_FLAG)
16237 buf[0] = 'R';
16238 else
16239 buf[0] = 'i';
16240 }
16241 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016242 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016243 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016244 if (exmode_active)
16245 buf[1] = 'v';
16246 }
16247 else if (exmode_active)
16248 {
16249 buf[0] = 'c';
16250 buf[1] = 'e';
16251 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016252 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016253 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016254 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016255 if (finish_op)
16256 buf[1] = 'o';
16257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016258
Bram Moolenaar05bb9532008-07-04 09:44:11 +000016259 /* Clear out the minor mode when the argument is not a non-zero number or
16260 * non-empty string. */
16261 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016262 buf[1] = NUL;
16263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016264 rettv->vval.v_string = vim_strsave(buf);
16265 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016266}
16267
Bram Moolenaar429fa852013-04-15 12:27:36 +020016268#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016269/*
16270 * "mzeval()" function
16271 */
16272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016273f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016274{
16275 char_u *str;
16276 char_u buf[NUMBUFLEN];
16277
16278 str = get_tv_string_buf(&argvars[0], buf);
16279 do_mzeval(str, rettv);
16280}
Bram Moolenaar75676462013-01-30 14:55:42 +010016281
16282 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016283mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010016284{
16285 typval_T argvars[3];
16286
16287 argvars[0].v_type = VAR_STRING;
16288 argvars[0].vval.v_string = name;
16289 copy_tv(args, &argvars[1]);
16290 argvars[2].v_type = VAR_UNKNOWN;
16291 f_call(argvars, rettv);
16292 clear_tv(&argvars[1]);
16293}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010016294#endif
16295
Bram Moolenaar071d4272004-06-13 20:20:40 +000016296/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016297 * "nextnonblank()" function
16298 */
16299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016300f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016301{
16302 linenr_T lnum;
16303
16304 for (lnum = get_tv_lnum(argvars); ; ++lnum)
16305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016306 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016307 {
16308 lnum = 0;
16309 break;
16310 }
16311 if (*skipwhite(ml_get(lnum)) != NUL)
16312 break;
16313 }
16314 rettv->vval.v_number = lnum;
16315}
16316
16317/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016318 * "nr2char()" function
16319 */
16320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016321f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016322{
16323 char_u buf[NUMBUFLEN];
16324
16325#ifdef FEAT_MBYTE
16326 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010016327 {
16328 int utf8 = 0;
16329
16330 if (argvars[1].v_type != VAR_UNKNOWN)
16331 utf8 = get_tv_number_chk(&argvars[1], NULL);
16332 if (utf8)
16333 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16334 else
16335 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016337 else
16338#endif
16339 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016340 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016341 buf[1] = NUL;
16342 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016343 rettv->v_type = VAR_STRING;
16344 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016345}
16346
16347/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016348 * "or(expr, expr)" function
16349 */
16350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016351f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016352{
16353 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16354 | get_tv_number_chk(&argvars[1], NULL);
16355}
16356
16357/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016358 * "pathshorten()" function
16359 */
16360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016361f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016362{
16363 char_u *p;
16364
16365 rettv->v_type = VAR_STRING;
16366 p = get_tv_string_chk(&argvars[0]);
16367 if (p == NULL)
16368 rettv->vval.v_string = NULL;
16369 else
16370 {
16371 p = vim_strsave(p);
16372 rettv->vval.v_string = p;
16373 if (p != NULL)
16374 shorten_dir(p);
16375 }
16376}
16377
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016378#ifdef FEAT_PERL
16379/*
16380 * "perleval()" function
16381 */
16382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016383f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016384{
16385 char_u *str;
16386 char_u buf[NUMBUFLEN];
16387
16388 str = get_tv_string_buf(&argvars[0], buf);
16389 do_perleval(str, rettv);
16390}
16391#endif
16392
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016393#ifdef FEAT_FLOAT
16394/*
16395 * "pow()" function
16396 */
16397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016398f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016399{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016400 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016401
16402 rettv->v_type = VAR_FLOAT;
16403 if (get_float_arg(argvars, &fx) == OK
16404 && get_float_arg(&argvars[1], &fy) == OK)
16405 rettv->vval.v_float = pow(fx, fy);
16406 else
16407 rettv->vval.v_float = 0.0;
16408}
16409#endif
16410
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016411/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016412 * "prevnonblank()" function
16413 */
16414 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016415f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016416{
16417 linenr_T lnum;
16418
16419 lnum = get_tv_lnum(argvars);
16420 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16421 lnum = 0;
16422 else
16423 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16424 --lnum;
16425 rettv->vval.v_number = lnum;
16426}
16427
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016428/* This dummy va_list is here because:
16429 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16430 * - locally in the function results in a "used before set" warning
16431 * - using va_start() to initialize it gives "function with fixed args" error */
16432static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016433
Bram Moolenaar8c711452005-01-14 21:53:12 +000016434/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016435 * "printf()" function
16436 */
16437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016438f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016439{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016440 char_u buf[NUMBUFLEN];
16441 int len;
16442 char_u *s;
16443 int saved_did_emsg = did_emsg;
16444 char *fmt;
16445
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016446 rettv->v_type = VAR_STRING;
16447 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016448
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016449 /* Get the required length, allocate the buffer and do it for real. */
16450 did_emsg = FALSE;
16451 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16452 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16453 if (!did_emsg)
16454 {
16455 s = alloc(len + 1);
16456 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016457 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016458 rettv->vval.v_string = s;
16459 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016460 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016461 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016462 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016463}
16464
16465/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016466 * "pumvisible()" function
16467 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016469f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016470{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016471#ifdef FEAT_INS_EXPAND
16472 if (pum_visible())
16473 rettv->vval.v_number = 1;
16474#endif
16475}
16476
Bram Moolenaardb913952012-06-29 12:54:53 +020016477#ifdef FEAT_PYTHON3
16478/*
16479 * "py3eval()" function
16480 */
16481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016482f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016483{
16484 char_u *str;
16485 char_u buf[NUMBUFLEN];
16486
16487 str = get_tv_string_buf(&argvars[0], buf);
16488 do_py3eval(str, rettv);
16489}
16490#endif
16491
16492#ifdef FEAT_PYTHON
16493/*
16494 * "pyeval()" function
16495 */
16496 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016497f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016498{
16499 char_u *str;
16500 char_u buf[NUMBUFLEN];
16501
16502 str = get_tv_string_buf(&argvars[0], buf);
16503 do_pyeval(str, rettv);
16504}
16505#endif
16506
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016507/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016508 * "range()" function
16509 */
16510 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016511f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016512{
16513 long start;
16514 long end;
16515 long stride = 1;
16516 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016517 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016518
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016519 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016520 if (argvars[1].v_type == VAR_UNKNOWN)
16521 {
16522 end = start - 1;
16523 start = 0;
16524 }
16525 else
16526 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016527 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016528 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016529 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016530 }
16531
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016532 if (error)
16533 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016534 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016535 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016536 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016537 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016538 else
16539 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016540 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016541 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016542 if (list_append_number(rettv->vval.v_list,
16543 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016544 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016545 }
16546}
16547
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016548/*
16549 * "readfile()" function
16550 */
16551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016552f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016553{
16554 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016555 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016556 char_u *fname;
16557 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016558 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16559 int io_size = sizeof(buf);
16560 int readlen; /* size of last fread() */
16561 char_u *prev = NULL; /* previously read bytes, if any */
16562 long prevlen = 0; /* length of data in prev */
16563 long prevsize = 0; /* size of prev buffer */
16564 long maxline = MAXLNUM;
16565 long cnt = 0;
16566 char_u *p; /* position in buf */
16567 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016568
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016569 if (argvars[1].v_type != VAR_UNKNOWN)
16570 {
16571 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16572 binary = TRUE;
16573 if (argvars[2].v_type != VAR_UNKNOWN)
16574 maxline = get_tv_number(&argvars[2]);
16575 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016578 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016579
16580 /* Always open the file in binary mode, library functions have a mind of
16581 * their own about CR-LF conversion. */
16582 fname = get_tv_string(&argvars[0]);
16583 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16584 {
16585 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16586 return;
16587 }
16588
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016589 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016590 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016591 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016592
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016593 /* This for loop processes what was read, but is also entered at end
16594 * of file so that either:
16595 * - an incomplete line gets written
16596 * - a "binary" file gets an empty line at the end if it ends in a
16597 * newline. */
16598 for (p = buf, start = buf;
16599 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16600 ++p)
16601 {
16602 if (*p == '\n' || readlen <= 0)
16603 {
16604 listitem_T *li;
16605 char_u *s = NULL;
16606 long_u len = p - start;
16607
16608 /* Finished a line. Remove CRs before NL. */
16609 if (readlen > 0 && !binary)
16610 {
16611 while (len > 0 && start[len - 1] == '\r')
16612 --len;
16613 /* removal may cross back to the "prev" string */
16614 if (len == 0)
16615 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16616 --prevlen;
16617 }
16618 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016619 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016620 else
16621 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016622 /* Change "prev" buffer to be the right size. This way
16623 * the bytes are only copied once, and very long lines are
16624 * allocated only once. */
16625 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016626 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016627 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016628 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016629 prev = NULL; /* the list will own the string */
16630 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016631 }
16632 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016633 if (s == NULL)
16634 {
16635 do_outofmem_msg((long_u) prevlen + len + 1);
16636 failed = TRUE;
16637 break;
16638 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016639
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016640 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016641 {
16642 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016643 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016644 break;
16645 }
16646 li->li_tv.v_type = VAR_STRING;
16647 li->li_tv.v_lock = 0;
16648 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016649 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016650
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016651 start = p + 1; /* step over newline */
16652 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016653 break;
16654 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016655 else if (*p == NUL)
16656 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016657#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016658 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16659 * when finding the BF and check the previous two bytes. */
16660 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016661 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016662 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16663 * + 1, these may be in the "prev" string. */
16664 char_u back1 = p >= buf + 1 ? p[-1]
16665 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16666 char_u back2 = p >= buf + 2 ? p[-2]
16667 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16668 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016669
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016670 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016671 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016672 char_u *dest = p - 2;
16673
16674 /* Usually a BOM is at the beginning of a file, and so at
16675 * the beginning of a line; then we can just step over it.
16676 */
16677 if (start == dest)
16678 start = p + 1;
16679 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016680 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016681 /* have to shuffle buf to close gap */
16682 int adjust_prevlen = 0;
16683
16684 if (dest < buf)
16685 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016686 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016687 dest = buf;
16688 }
16689 if (readlen > p - buf + 1)
16690 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16691 readlen -= 3 - adjust_prevlen;
16692 prevlen -= adjust_prevlen;
16693 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016694 }
16695 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016696 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016697#endif
16698 } /* for */
16699
16700 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16701 break;
16702 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016703 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016704 /* There's part of a line in buf, store it in "prev". */
16705 if (p - start + prevlen >= prevsize)
16706 {
16707 /* need bigger "prev" buffer */
16708 char_u *newprev;
16709
16710 /* A common use case is ordinary text files and "prev" gets a
16711 * fragment of a line, so the first allocation is made
16712 * small, to avoid repeatedly 'allocing' large and
16713 * 'reallocing' small. */
16714 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016715 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016716 else
16717 {
16718 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016719 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016720 prevsize = grow50pc > growmin ? grow50pc : growmin;
16721 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016722 newprev = prev == NULL ? alloc(prevsize)
16723 : vim_realloc(prev, prevsize);
16724 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016725 {
16726 do_outofmem_msg((long_u)prevsize);
16727 failed = TRUE;
16728 break;
16729 }
16730 prev = newprev;
16731 }
16732 /* Add the line part to end of "prev". */
16733 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016734 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016735 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016736 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016737
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016738 /*
16739 * For a negative line count use only the lines at the end of the file,
16740 * free the rest.
16741 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016742 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016743 while (cnt > -maxline)
16744 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016745 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016746 --cnt;
16747 }
16748
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016749 if (failed)
16750 {
16751 list_free(rettv->vval.v_list, TRUE);
16752 /* readfile doc says an empty list is returned on error */
16753 rettv->vval.v_list = list_alloc();
16754 }
16755
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016756 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016757 fclose(fd);
16758}
16759
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016760#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016761static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016762
16763/*
16764 * Convert a List to proftime_T.
16765 * Return FAIL when there is something wrong.
16766 */
16767 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016768list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016769{
16770 long n1, n2;
16771 int error = FALSE;
16772
16773 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16774 || arg->vval.v_list->lv_len != 2)
16775 return FAIL;
16776 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16777 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16778# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016779 tm->HighPart = n1;
16780 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016781# else
16782 tm->tv_sec = n1;
16783 tm->tv_usec = n2;
16784# endif
16785 return error ? FAIL : OK;
16786}
16787#endif /* FEAT_RELTIME */
16788
16789/*
16790 * "reltime()" function
16791 */
16792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016793f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016794{
16795#ifdef FEAT_RELTIME
16796 proftime_T res;
16797 proftime_T start;
16798
16799 if (argvars[0].v_type == VAR_UNKNOWN)
16800 {
16801 /* No arguments: get current time. */
16802 profile_start(&res);
16803 }
16804 else if (argvars[1].v_type == VAR_UNKNOWN)
16805 {
16806 if (list2proftime(&argvars[0], &res) == FAIL)
16807 return;
16808 profile_end(&res);
16809 }
16810 else
16811 {
16812 /* Two arguments: compute the difference. */
16813 if (list2proftime(&argvars[0], &start) == FAIL
16814 || list2proftime(&argvars[1], &res) == FAIL)
16815 return;
16816 profile_sub(&res, &start);
16817 }
16818
16819 if (rettv_list_alloc(rettv) == OK)
16820 {
16821 long n1, n2;
16822
16823# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016824 n1 = res.HighPart;
16825 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016826# else
16827 n1 = res.tv_sec;
16828 n2 = res.tv_usec;
16829# endif
16830 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16831 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16832 }
16833#endif
16834}
16835
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016836#ifdef FEAT_FLOAT
16837/*
16838 * "reltimefloat()" function
16839 */
16840 static void
16841f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16842{
16843# ifdef FEAT_RELTIME
16844 proftime_T tm;
16845# endif
16846
16847 rettv->v_type = VAR_FLOAT;
16848 rettv->vval.v_float = 0;
16849# ifdef FEAT_RELTIME
16850 if (list2proftime(&argvars[0], &tm) == OK)
16851 rettv->vval.v_float = profile_float(&tm);
16852# endif
16853}
16854#endif
16855
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016856/*
16857 * "reltimestr()" function
16858 */
16859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016860f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016861{
16862#ifdef FEAT_RELTIME
16863 proftime_T tm;
16864#endif
16865
16866 rettv->v_type = VAR_STRING;
16867 rettv->vval.v_string = NULL;
16868#ifdef FEAT_RELTIME
16869 if (list2proftime(&argvars[0], &tm) == OK)
16870 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16871#endif
16872}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016873
Bram Moolenaar0d660222005-01-07 21:51:51 +000016874#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016875static void make_connection(void);
16876static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016877
16878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016879make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016880{
16881 if (X_DISPLAY == NULL
16882# ifdef FEAT_GUI
16883 && !gui.in_use
16884# endif
16885 )
16886 {
16887 x_force_connect = TRUE;
16888 setup_term_clip();
16889 x_force_connect = FALSE;
16890 }
16891}
16892
16893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016894check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016895{
16896 make_connection();
16897 if (X_DISPLAY == NULL)
16898 {
16899 EMSG(_("E240: No connection to Vim server"));
16900 return FAIL;
16901 }
16902 return OK;
16903}
16904#endif
16905
16906#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016907static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016908
16909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016910remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016911{
16912 char_u *server_name;
16913 char_u *keys;
16914 char_u *r = NULL;
16915 char_u buf[NUMBUFLEN];
16916# ifdef WIN32
16917 HWND w;
16918# else
16919 Window w;
16920# endif
16921
16922 if (check_restricted() || check_secure())
16923 return;
16924
16925# ifdef FEAT_X11
16926 if (check_connection() == FAIL)
16927 return;
16928# endif
16929
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016930 server_name = get_tv_string_chk(&argvars[0]);
16931 if (server_name == NULL)
16932 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016933 keys = get_tv_string_buf(&argvars[1], buf);
16934# ifdef WIN32
16935 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16936# else
16937 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16938 < 0)
16939# endif
16940 {
16941 if (r != NULL)
16942 EMSG(r); /* sending worked but evaluation failed */
16943 else
16944 EMSG2(_("E241: Unable to send to %s"), server_name);
16945 return;
16946 }
16947
16948 rettv->vval.v_string = r;
16949
16950 if (argvars[2].v_type != VAR_UNKNOWN)
16951 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016952 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016953 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016954 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016955
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016956 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016957 v.di_tv.v_type = VAR_STRING;
16958 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016959 idvar = get_tv_string_chk(&argvars[2]);
16960 if (idvar != NULL)
16961 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016962 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016963 }
16964}
16965#endif
16966
16967/*
16968 * "remote_expr()" function
16969 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016971f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016972{
16973 rettv->v_type = VAR_STRING;
16974 rettv->vval.v_string = NULL;
16975#ifdef FEAT_CLIENTSERVER
16976 remote_common(argvars, rettv, TRUE);
16977#endif
16978}
16979
16980/*
16981 * "remote_foreground()" function
16982 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016984f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016985{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016986#ifdef FEAT_CLIENTSERVER
16987# ifdef WIN32
16988 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016989 {
16990 char_u *server_name = get_tv_string_chk(&argvars[0]);
16991
16992 if (server_name != NULL)
16993 serverForeground(server_name);
16994 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016995# else
16996 /* Send a foreground() expression to the server. */
16997 argvars[1].v_type = VAR_STRING;
16998 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16999 argvars[2].v_type = VAR_UNKNOWN;
17000 remote_common(argvars, rettv, TRUE);
17001 vim_free(argvars[1].vval.v_string);
17002# endif
17003#endif
17004}
17005
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017007f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017008{
17009#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000017010 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017011 char_u *s = NULL;
17012# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017013 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017014# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017015 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017016
17017 if (check_restricted() || check_secure())
17018 {
17019 rettv->vval.v_number = -1;
17020 return;
17021 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017022 serverid = get_tv_string_chk(&argvars[0]);
17023 if (serverid == NULL)
17024 {
17025 rettv->vval.v_number = -1;
17026 return; /* type error; errmsg already given */
17027 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017028# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017029 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017030 if (n == 0)
17031 rettv->vval.v_number = -1;
17032 else
17033 {
17034 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
17035 rettv->vval.v_number = (s != NULL);
17036 }
17037# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 if (check_connection() == FAIL)
17039 return;
17040
17041 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017042 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017043# endif
17044
17045 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
17046 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017047 char_u *retvar;
17048
Bram Moolenaar33570922005-01-25 22:26:29 +000017049 v.di_tv.v_type = VAR_STRING;
17050 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017051 retvar = get_tv_string_chk(&argvars[1]);
17052 if (retvar != NULL)
17053 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000017054 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017055 }
17056#else
17057 rettv->vval.v_number = -1;
17058#endif
17059}
17060
Bram Moolenaar0d660222005-01-07 21:51:51 +000017061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017062f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063{
17064 char_u *r = NULL;
17065
17066#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017067 char_u *serverid = get_tv_string_chk(&argvars[0]);
17068
17069 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000017070 {
17071# ifdef WIN32
17072 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000017073 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017074
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010017075 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076 if (n != 0)
17077 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
17078 if (r == NULL)
17079# else
17080 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017081 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017082# endif
17083 EMSG(_("E277: Unable to read a server reply"));
17084 }
17085#endif
17086 rettv->v_type = VAR_STRING;
17087 rettv->vval.v_string = r;
17088}
17089
17090/*
17091 * "remote_send()" function
17092 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017094f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095{
17096 rettv->v_type = VAR_STRING;
17097 rettv->vval.v_string = NULL;
17098#ifdef FEAT_CLIENTSERVER
17099 remote_common(argvars, rettv, FALSE);
17100#endif
17101}
17102
17103/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000017104 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017105 */
17106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017107f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017108{
Bram Moolenaar33570922005-01-25 22:26:29 +000017109 list_T *l;
17110 listitem_T *item, *item2;
17111 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017112 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017113 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000017114 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000017115 dict_T *d;
17116 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020017117 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017118
Bram Moolenaar8c711452005-01-14 21:53:12 +000017119 if (argvars[0].v_type == VAR_DICT)
17120 {
17121 if (argvars[2].v_type != VAR_UNKNOWN)
17122 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017123 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017124 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000017125 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017126 key = get_tv_string_chk(&argvars[1]);
17127 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017128 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017129 di = dict_find(d, key, -1);
17130 if (di == NULL)
17131 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020017132 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
17133 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017134 {
17135 *rettv = di->di_tv;
17136 init_tv(&di->di_tv);
17137 dictitem_remove(d, di);
17138 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000017139 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000017140 }
17141 }
17142 else if (argvars[0].v_type != VAR_LIST)
17143 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017144 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017145 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017147 int error = FALSE;
17148
17149 idx = get_tv_number_chk(&argvars[1], &error);
17150 if (error)
17151 ; /* type error: do nothing, errmsg already given */
17152 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017153 EMSGN(_(e_listidx), idx);
17154 else
17155 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017156 if (argvars[2].v_type == VAR_UNKNOWN)
17157 {
17158 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017159 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017160 *rettv = item->li_tv;
17161 vim_free(item);
17162 }
17163 else
17164 {
17165 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017166 end = get_tv_number_chk(&argvars[2], &error);
17167 if (error)
17168 ; /* type error: do nothing */
17169 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017170 EMSGN(_(e_listidx), end);
17171 else
17172 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000017173 int cnt = 0;
17174
17175 for (li = item; li != NULL; li = li->li_next)
17176 {
17177 ++cnt;
17178 if (li == item2)
17179 break;
17180 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017181 if (li == NULL) /* didn't find "item2" after "item" */
17182 EMSG(_(e_invrange));
17183 else
17184 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020017185 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017186 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017187 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017188 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017189 l->lv_first = item;
17190 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017191 item->li_prev = NULL;
17192 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017193 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017194 }
17195 }
17196 }
17197 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017198 }
17199 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200}
17201
17202/*
17203 * "rename({from}, {to})" function
17204 */
17205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017206f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017207{
17208 char_u buf[NUMBUFLEN];
17209
17210 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017211 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017212 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017213 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
17214 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000017215}
17216
17217/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017218 * "repeat()" function
17219 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017221f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017222{
17223 char_u *p;
17224 int n;
17225 int slen;
17226 int len;
17227 char_u *r;
17228 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017229
17230 n = get_tv_number(&argvars[1]);
17231 if (argvars[0].v_type == VAR_LIST)
17232 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017233 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017234 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017235 if (list_extend(rettv->vval.v_list,
17236 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017237 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000017238 }
17239 else
17240 {
17241 p = get_tv_string(&argvars[0]);
17242 rettv->v_type = VAR_STRING;
17243 rettv->vval.v_string = NULL;
17244
17245 slen = (int)STRLEN(p);
17246 len = slen * n;
17247 if (len <= 0)
17248 return;
17249
17250 r = alloc(len + 1);
17251 if (r != NULL)
17252 {
17253 for (i = 0; i < n; i++)
17254 mch_memmove(r + i * slen, p, (size_t)slen);
17255 r[len] = NUL;
17256 }
17257
17258 rettv->vval.v_string = r;
17259 }
17260}
17261
17262/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 * "resolve()" function
17264 */
17265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017266f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017267{
17268 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020017269#ifdef HAVE_READLINK
17270 char_u *buf = NULL;
17271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000017272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017273 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274#ifdef FEAT_SHORTCUT
17275 {
17276 char_u *v = NULL;
17277
17278 v = mch_resolve_shortcut(p);
17279 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017280 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017281 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017282 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017283 }
17284#else
17285# ifdef HAVE_READLINK
17286 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017287 char_u *cpy;
17288 int len;
17289 char_u *remain = NULL;
17290 char_u *q;
17291 int is_relative_to_current = FALSE;
17292 int has_trailing_pathsep = FALSE;
17293 int limit = 100;
17294
17295 p = vim_strsave(p);
17296
17297 if (p[0] == '.' && (vim_ispathsep(p[1])
17298 || (p[1] == '.' && (vim_ispathsep(p[2])))))
17299 is_relative_to_current = TRUE;
17300
17301 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017302 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017303 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017304 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020017305 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
17306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017307
17308 q = getnextcomp(p);
17309 if (*q != NUL)
17310 {
17311 /* Separate the first path component in "p", and keep the
17312 * remainder (beginning with the path separator). */
17313 remain = vim_strsave(q - 1);
17314 q[-1] = NUL;
17315 }
17316
Bram Moolenaard9462e32011-04-11 21:35:11 +020017317 buf = alloc(MAXPATHL + 1);
17318 if (buf == NULL)
17319 goto fail;
17320
Bram Moolenaar071d4272004-06-13 20:20:40 +000017321 for (;;)
17322 {
17323 for (;;)
17324 {
17325 len = readlink((char *)p, (char *)buf, MAXPATHL);
17326 if (len <= 0)
17327 break;
17328 buf[len] = NUL;
17329
17330 if (limit-- == 0)
17331 {
17332 vim_free(p);
17333 vim_free(remain);
17334 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017335 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017336 goto fail;
17337 }
17338
17339 /* Ensure that the result will have a trailing path separator
17340 * if the argument has one. */
17341 if (remain == NULL && has_trailing_pathsep)
17342 add_pathsep(buf);
17343
17344 /* Separate the first path component in the link value and
17345 * concatenate the remainders. */
17346 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17347 if (*q != NUL)
17348 {
17349 if (remain == NULL)
17350 remain = vim_strsave(q - 1);
17351 else
17352 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017353 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 if (cpy != NULL)
17355 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017356 vim_free(remain);
17357 remain = cpy;
17358 }
17359 }
17360 q[-1] = NUL;
17361 }
17362
17363 q = gettail(p);
17364 if (q > p && *q == NUL)
17365 {
17366 /* Ignore trailing path separator. */
17367 q[-1] = NUL;
17368 q = gettail(p);
17369 }
17370 if (q > p && !mch_isFullName(buf))
17371 {
17372 /* symlink is relative to directory of argument */
17373 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17374 if (cpy != NULL)
17375 {
17376 STRCPY(cpy, p);
17377 STRCPY(gettail(cpy), buf);
17378 vim_free(p);
17379 p = cpy;
17380 }
17381 }
17382 else
17383 {
17384 vim_free(p);
17385 p = vim_strsave(buf);
17386 }
17387 }
17388
17389 if (remain == NULL)
17390 break;
17391
17392 /* Append the first path component of "remain" to "p". */
17393 q = getnextcomp(remain + 1);
17394 len = q - remain - (*q != NUL);
17395 cpy = vim_strnsave(p, STRLEN(p) + len);
17396 if (cpy != NULL)
17397 {
17398 STRNCAT(cpy, remain, len);
17399 vim_free(p);
17400 p = cpy;
17401 }
17402 /* Shorten "remain". */
17403 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017404 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017405 else
17406 {
17407 vim_free(remain);
17408 remain = NULL;
17409 }
17410 }
17411
17412 /* If the result is a relative path name, make it explicitly relative to
17413 * the current directory if and only if the argument had this form. */
17414 if (!vim_ispathsep(*p))
17415 {
17416 if (is_relative_to_current
17417 && *p != NUL
17418 && !(p[0] == '.'
17419 && (p[1] == NUL
17420 || vim_ispathsep(p[1])
17421 || (p[1] == '.'
17422 && (p[2] == NUL
17423 || vim_ispathsep(p[2]))))))
17424 {
17425 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017426 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017427 if (cpy != NULL)
17428 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 vim_free(p);
17430 p = cpy;
17431 }
17432 }
17433 else if (!is_relative_to_current)
17434 {
17435 /* Strip leading "./". */
17436 q = p;
17437 while (q[0] == '.' && vim_ispathsep(q[1]))
17438 q += 2;
17439 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017440 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017441 }
17442 }
17443
17444 /* Ensure that the result will have no trailing path separator
17445 * if the argument had none. But keep "/" or "//". */
17446 if (!has_trailing_pathsep)
17447 {
17448 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017449 if (after_pathsep(p, q))
17450 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017451 }
17452
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017453 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017454 }
17455# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017456 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017457# endif
17458#endif
17459
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017460 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017461
17462#ifdef HAVE_READLINK
17463fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017464 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017465#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017466 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017467}
17468
17469/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017470 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017471 */
17472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017473f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017474{
Bram Moolenaar33570922005-01-25 22:26:29 +000017475 list_T *l;
17476 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477
Bram Moolenaar0d660222005-01-07 21:51:51 +000017478 if (argvars[0].v_type != VAR_LIST)
17479 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017480 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017481 && !tv_check_lock(l->lv_lock,
17482 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017483 {
17484 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017485 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017486 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017487 while (li != NULL)
17488 {
17489 ni = li->li_prev;
17490 list_append(l, li);
17491 li = ni;
17492 }
17493 rettv->vval.v_list = l;
17494 rettv->v_type = VAR_LIST;
17495 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017496 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017497 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017498}
17499
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017500#define SP_NOMOVE 0x01 /* don't move cursor */
17501#define SP_REPEAT 0x02 /* repeat to find outer pair */
17502#define SP_RETCOUNT 0x04 /* return matchcount */
17503#define SP_SETPCMARK 0x08 /* set previous context mark */
17504#define SP_START 0x10 /* accept match at start position */
17505#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17506#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017507#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017508
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017509static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017510
17511/*
17512 * Get flags for a search function.
17513 * Possibly sets "p_ws".
17514 * Returns BACKWARD, FORWARD or zero (for an error).
17515 */
17516 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017517get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017518{
17519 int dir = FORWARD;
17520 char_u *flags;
17521 char_u nbuf[NUMBUFLEN];
17522 int mask;
17523
17524 if (varp->v_type != VAR_UNKNOWN)
17525 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017526 flags = get_tv_string_buf_chk(varp, nbuf);
17527 if (flags == NULL)
17528 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017529 while (*flags != NUL)
17530 {
17531 switch (*flags)
17532 {
17533 case 'b': dir = BACKWARD; break;
17534 case 'w': p_ws = TRUE; break;
17535 case 'W': p_ws = FALSE; break;
17536 default: mask = 0;
17537 if (flagsp != NULL)
17538 switch (*flags)
17539 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017540 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017541 case 'e': mask = SP_END; break;
17542 case 'm': mask = SP_RETCOUNT; break;
17543 case 'n': mask = SP_NOMOVE; break;
17544 case 'p': mask = SP_SUBPAT; break;
17545 case 'r': mask = SP_REPEAT; break;
17546 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017547 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017548 }
17549 if (mask == 0)
17550 {
17551 EMSG2(_(e_invarg2), flags);
17552 dir = 0;
17553 }
17554 else
17555 *flagsp |= mask;
17556 }
17557 if (dir == 0)
17558 break;
17559 ++flags;
17560 }
17561 }
17562 return dir;
17563}
17564
Bram Moolenaar071d4272004-06-13 20:20:40 +000017565/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017566 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017567 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017568 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017569search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017570{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017571 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 char_u *pat;
17573 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017574 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017575 int save_p_ws = p_ws;
17576 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017577 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017578 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017579 proftime_T tm;
17580#ifdef FEAT_RELTIME
17581 long time_limit = 0;
17582#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017583 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017584 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017585
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017586 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017587 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017588 if (dir == 0)
17589 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017590 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017591 if (flags & SP_START)
17592 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017593 if (flags & SP_END)
17594 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017595 if (flags & SP_COLUMN)
17596 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017597
Bram Moolenaar76929292008-01-06 19:07:36 +000017598 /* Optional arguments: line number to stop searching and timeout. */
17599 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017600 {
17601 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17602 if (lnum_stop < 0)
17603 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017604#ifdef FEAT_RELTIME
17605 if (argvars[3].v_type != VAR_UNKNOWN)
17606 {
17607 time_limit = get_tv_number_chk(&argvars[3], NULL);
17608 if (time_limit < 0)
17609 goto theend;
17610 }
17611#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017612 }
17613
Bram Moolenaar76929292008-01-06 19:07:36 +000017614#ifdef FEAT_RELTIME
17615 /* Set the time limit, if there is one. */
17616 profile_setlimit(time_limit, &tm);
17617#endif
17618
Bram Moolenaar231334e2005-07-25 20:46:57 +000017619 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017620 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017621 * Check to make sure only those flags are set.
17622 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17623 * flags cannot be set. Check for that condition also.
17624 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017625 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017626 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017627 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017628 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017629 goto theend;
17630 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017631
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017632 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017633 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017634 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017635 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017636 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017637 if (flags & SP_SUBPAT)
17638 retval = subpatnum;
17639 else
17640 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017641 if (flags & SP_SETPCMARK)
17642 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017643 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017644 if (match_pos != NULL)
17645 {
17646 /* Store the match cursor position */
17647 match_pos->lnum = pos.lnum;
17648 match_pos->col = pos.col + 1;
17649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017650 /* "/$" will put the cursor after the end of the line, may need to
17651 * correct that here */
17652 check_cursor();
17653 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017654
17655 /* If 'n' flag is used: restore cursor position. */
17656 if (flags & SP_NOMOVE)
17657 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017658 else
17659 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017660theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017661 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017662
17663 return retval;
17664}
17665
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017666#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017667
17668/*
17669 * round() is not in C90, use ceil() or floor() instead.
17670 */
17671 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017672vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017673{
17674 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17675}
17676
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017677/*
17678 * "round({float})" function
17679 */
17680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017681f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017682{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017683 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017684
17685 rettv->v_type = VAR_FLOAT;
17686 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017687 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017688 else
17689 rettv->vval.v_float = 0.0;
17690}
17691#endif
17692
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017693/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017694 * "screenattr()" function
17695 */
17696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017697f_screenattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017698{
17699 int row;
17700 int col;
17701 int c;
17702
17703 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17704 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17705 if (row < 0 || row >= screen_Rows
17706 || col < 0 || col >= screen_Columns)
17707 c = -1;
17708 else
17709 c = ScreenAttrs[LineOffset[row] + col];
17710 rettv->vval.v_number = c;
17711}
17712
17713/*
17714 * "screenchar()" function
17715 */
17716 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017717f_screenchar(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017718{
17719 int row;
17720 int col;
17721 int off;
17722 int c;
17723
17724 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17725 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17726 if (row < 0 || row >= screen_Rows
17727 || col < 0 || col >= screen_Columns)
17728 c = -1;
17729 else
17730 {
17731 off = LineOffset[row] + col;
17732#ifdef FEAT_MBYTE
17733 if (enc_utf8 && ScreenLinesUC[off] != 0)
17734 c = ScreenLinesUC[off];
17735 else
17736#endif
17737 c = ScreenLines[off];
17738 }
17739 rettv->vval.v_number = c;
17740}
17741
17742/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017743 * "screencol()" function
17744 *
17745 * First column is 1 to be consistent with virtcol().
17746 */
17747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017748f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017749{
17750 rettv->vval.v_number = screen_screencol() + 1;
17751}
17752
17753/*
17754 * "screenrow()" function
17755 */
17756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017757f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017758{
17759 rettv->vval.v_number = screen_screenrow() + 1;
17760}
17761
17762/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017763 * "search()" function
17764 */
17765 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017766f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017767{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017768 int flags = 0;
17769
17770 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771}
17772
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017774 * "searchdecl()" function
17775 */
17776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017777f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017778{
17779 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017780 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017781 int error = FALSE;
17782 char_u *name;
17783
17784 rettv->vval.v_number = 1; /* default: FAIL */
17785
17786 name = get_tv_string_chk(&argvars[0]);
17787 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017788 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017789 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017790 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17791 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17792 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017793 if (!error && name != NULL)
17794 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017795 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017796}
17797
17798/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017799 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017801 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017802searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803{
17804 char_u *spat, *mpat, *epat;
17805 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807 int dir;
17808 int flags = 0;
17809 char_u nbuf1[NUMBUFLEN];
17810 char_u nbuf2[NUMBUFLEN];
17811 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017812 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017813 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017814 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815
Bram Moolenaar071d4272004-06-13 20:20:40 +000017816 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017817 spat = get_tv_string_chk(&argvars[0]);
17818 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17819 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17820 if (spat == NULL || mpat == NULL || epat == NULL)
17821 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017822
Bram Moolenaar071d4272004-06-13 20:20:40 +000017823 /* Handle the optional fourth argument: flags */
17824 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017825 if (dir == 0)
17826 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017827
17828 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017829 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17830 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017831 if ((flags & (SP_END | SP_SUBPAT)) != 0
17832 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017833 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017834 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017835 goto theend;
17836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017837
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017838 /* Using 'r' implies 'W', otherwise it doesn't work. */
17839 if (flags & SP_REPEAT)
17840 p_ws = FALSE;
17841
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017842 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017843 if (argvars[3].v_type == VAR_UNKNOWN
17844 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 skip = (char_u *)"";
17846 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017847 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017848 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017849 if (argvars[5].v_type != VAR_UNKNOWN)
17850 {
17851 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17852 if (lnum_stop < 0)
17853 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017854#ifdef FEAT_RELTIME
17855 if (argvars[6].v_type != VAR_UNKNOWN)
17856 {
17857 time_limit = get_tv_number_chk(&argvars[6], NULL);
17858 if (time_limit < 0)
17859 goto theend;
17860 }
17861#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017862 }
17863 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017864 if (skip == NULL)
17865 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017867 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017868 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017869
17870theend:
17871 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017872
17873 return retval;
17874}
17875
17876/*
17877 * "searchpair()" function
17878 */
17879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017880f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017881{
17882 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17883}
17884
17885/*
17886 * "searchpairpos()" function
17887 */
17888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017889f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017890{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017891 pos_T match_pos;
17892 int lnum = 0;
17893 int col = 0;
17894
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017895 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017896 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017897
17898 if (searchpair_cmn(argvars, &match_pos) > 0)
17899 {
17900 lnum = match_pos.lnum;
17901 col = match_pos.col;
17902 }
17903
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017904 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17905 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017906}
17907
17908/*
17909 * Search for a start/middle/end thing.
17910 * Used by searchpair(), see its documentation for the details.
17911 * Returns 0 or -1 for no match,
17912 */
17913 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017914do_searchpair(
17915 char_u *spat, /* start pattern */
17916 char_u *mpat, /* middle pattern */
17917 char_u *epat, /* end pattern */
17918 int dir, /* BACKWARD or FORWARD */
17919 char_u *skip, /* skip expression */
17920 int flags, /* SP_SETPCMARK and other SP_ values */
17921 pos_T *match_pos,
17922 linenr_T lnum_stop, /* stop at this line if not zero */
17923 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017924{
17925 char_u *save_cpo;
17926 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17927 long retval = 0;
17928 pos_T pos;
17929 pos_T firstpos;
17930 pos_T foundpos;
17931 pos_T save_cursor;
17932 pos_T save_pos;
17933 int n;
17934 int r;
17935 int nest = 1;
17936 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017937 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017938 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017939
17940 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17941 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017942 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017943
Bram Moolenaar76929292008-01-06 19:07:36 +000017944#ifdef FEAT_RELTIME
17945 /* Set the time limit, if there is one. */
17946 profile_setlimit(time_limit, &tm);
17947#endif
17948
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017949 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17950 * start/middle/end (pat3, for the top pair). */
17951 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17952 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17953 if (pat2 == NULL || pat3 == NULL)
17954 goto theend;
17955 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17956 if (*mpat == NUL)
17957 STRCPY(pat3, pat2);
17958 else
17959 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17960 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017961 if (flags & SP_START)
17962 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017963
Bram Moolenaar071d4272004-06-13 20:20:40 +000017964 save_cursor = curwin->w_cursor;
17965 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017966 clearpos(&firstpos);
17967 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017968 pat = pat3;
17969 for (;;)
17970 {
17971 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017972 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017973 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17974 /* didn't find it or found the first match again: FAIL */
17975 break;
17976
17977 if (firstpos.lnum == 0)
17978 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017979 if (equalpos(pos, foundpos))
17980 {
17981 /* Found the same position again. Can happen with a pattern that
17982 * has "\zs" at the end and searching backwards. Advance one
17983 * character and try again. */
17984 if (dir == BACKWARD)
17985 decl(&pos);
17986 else
17987 incl(&pos);
17988 }
17989 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017990
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017991 /* clear the start flag to avoid getting stuck here */
17992 options &= ~SEARCH_START;
17993
Bram Moolenaar071d4272004-06-13 20:20:40 +000017994 /* If the skip pattern matches, ignore this match. */
17995 if (*skip != NUL)
17996 {
17997 save_pos = curwin->w_cursor;
17998 curwin->w_cursor = pos;
17999 r = eval_to_bool(skip, &err, NULL, FALSE);
18000 curwin->w_cursor = save_pos;
18001 if (err)
18002 {
18003 /* Evaluating {skip} caused an error, break here. */
18004 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018005 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018006 break;
18007 }
18008 if (r)
18009 continue;
18010 }
18011
18012 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
18013 {
18014 /* Found end when searching backwards or start when searching
18015 * forward: nested pair. */
18016 ++nest;
18017 pat = pat2; /* nested, don't search for middle */
18018 }
18019 else
18020 {
18021 /* Found end when searching forward or start when searching
18022 * backward: end of (nested) pair; or found middle in outer pair. */
18023 if (--nest == 1)
18024 pat = pat3; /* outer level, search for middle */
18025 }
18026
18027 if (nest == 0)
18028 {
18029 /* Found the match: return matchcount or line number. */
18030 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018031 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018032 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018033 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000018034 if (flags & SP_SETPCMARK)
18035 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000018036 curwin->w_cursor = pos;
18037 if (!(flags & SP_REPEAT))
18038 break;
18039 nest = 1; /* search for next unmatched */
18040 }
18041 }
18042
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018043 if (match_pos != NULL)
18044 {
18045 /* Store the match cursor position */
18046 match_pos->lnum = curwin->w_cursor.lnum;
18047 match_pos->col = curwin->w_cursor.col + 1;
18048 }
18049
Bram Moolenaar071d4272004-06-13 20:20:40 +000018050 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018051 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018052 curwin->w_cursor = save_cursor;
18053
18054theend:
18055 vim_free(pat2);
18056 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000018057 if (p_cpo == empty_option)
18058 p_cpo = save_cpo;
18059 else
18060 /* Darn, evaluating the {skip} expression changed the value. */
18061 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000018062
18063 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018064}
18065
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018066/*
18067 * "searchpos()" function
18068 */
18069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018070f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018071{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018072 pos_T match_pos;
18073 int lnum = 0;
18074 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018075 int n;
18076 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018077
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018078 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018079 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018080
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018081 n = search_cmn(argvars, &match_pos, &flags);
18082 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018083 {
18084 lnum = match_pos.lnum;
18085 col = match_pos.col;
18086 }
18087
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018088 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
18089 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000018090 if (flags & SP_SUBPAT)
18091 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018092}
18093
Bram Moolenaar0d660222005-01-07 21:51:51 +000018094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018097#ifdef FEAT_CLIENTSERVER
18098 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018099 char_u *server = get_tv_string_chk(&argvars[0]);
18100 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101
Bram Moolenaar0d660222005-01-07 21:51:51 +000018102 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018103 if (server == NULL || reply == NULL)
18104 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018105 if (check_restricted() || check_secure())
18106 return;
18107# ifdef FEAT_X11
18108 if (check_connection() == FAIL)
18109 return;
18110# endif
18111
18112 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018113 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018114 EMSG(_("E258: Unable to send to client"));
18115 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018116 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018117 rettv->vval.v_number = 0;
18118#else
18119 rettv->vval.v_number = -1;
18120#endif
18121}
18122
Bram Moolenaar0d660222005-01-07 21:51:51 +000018123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018124f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018125{
18126 char_u *r = NULL;
18127
18128#ifdef FEAT_CLIENTSERVER
18129# ifdef WIN32
18130 r = serverGetVimNames();
18131# else
18132 make_connection();
18133 if (X_DISPLAY != NULL)
18134 r = serverGetVimNames(X_DISPLAY);
18135# endif
18136#endif
18137 rettv->v_type = VAR_STRING;
18138 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018139}
18140
18141/*
18142 * "setbufvar()" function
18143 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018145f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018146{
18147 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018148 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018150 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018151 char_u nbuf[NUMBUFLEN];
18152
18153 if (check_restricted() || check_secure())
18154 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018155 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
18156 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010018157 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 varp = &argvars[2];
18159
18160 if (buf != NULL && varname != NULL && varp != NULL)
18161 {
18162 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018163 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018164
18165 if (*varname == '&')
18166 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018167 long numval;
18168 char_u *strval;
18169 int error = FALSE;
18170
Bram Moolenaar071d4272004-06-13 20:20:40 +000018171 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018172 numval = get_tv_number_chk(varp, &error);
18173 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018174 if (!error && strval != NULL)
18175 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 }
18177 else
18178 {
18179 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
18180 if (bufvarname != NULL)
18181 {
18182 STRCPY(bufvarname, "b:");
18183 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000018184 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018185 vim_free(bufvarname);
18186 }
18187 }
18188
18189 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018190 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018191 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192}
18193
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018194 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018195f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018196{
18197 dict_T *d;
18198 dictitem_T *di;
18199 char_u *csearch;
18200
18201 if (argvars[0].v_type != VAR_DICT)
18202 {
18203 EMSG(_(e_dictreq));
18204 return;
18205 }
18206
18207 if ((d = argvars[0].vval.v_dict) != NULL)
18208 {
18209 csearch = get_dict_string(d, (char_u *)"char", FALSE);
18210 if (csearch != NULL)
18211 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018212#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018213 if (enc_utf8)
18214 {
18215 int pcc[MAX_MCO];
18216 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018217
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018218 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
18219 }
18220 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020018221#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020018222 set_last_csearch(PTR2CHAR(csearch),
18223 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020018224 }
18225
18226 di = dict_find(d, (char_u *)"forward", -1);
18227 if (di != NULL)
18228 set_csearch_direction(get_tv_number(&di->di_tv)
18229 ? FORWARD : BACKWARD);
18230
18231 di = dict_find(d, (char_u *)"until", -1);
18232 if (di != NULL)
18233 set_csearch_until(!!get_tv_number(&di->di_tv));
18234 }
18235}
18236
Bram Moolenaar071d4272004-06-13 20:20:40 +000018237/*
18238 * "setcmdpos()" function
18239 */
18240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018241f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018242{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018243 int pos = (int)get_tv_number(&argvars[0]) - 1;
18244
18245 if (pos >= 0)
18246 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018247}
18248
18249/*
18250 * "setline()" function
18251 */
18252 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018253f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018254{
18255 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000018256 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018257 list_T *l = NULL;
18258 listitem_T *li = NULL;
18259 long added = 0;
18260 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018262 lnum = get_tv_lnum(&argvars[0]);
18263 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018264 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018265 l = argvars[1].vval.v_list;
18266 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018268 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018270
Bram Moolenaar798b30b2009-04-22 10:56:16 +000018271 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018272 for (;;)
18273 {
18274 if (l != NULL)
18275 {
18276 /* list argument, get next string */
18277 if (li == NULL)
18278 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018279 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018280 li = li->li_next;
18281 }
18282
18283 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018284 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018285 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020018286
18287 /* When coming here from Insert mode, sync undo, so that this can be
18288 * undone separately from what was previously inserted. */
18289 if (u_sync_once == 2)
18290 {
18291 u_sync_once = 1; /* notify that u_sync() was called */
18292 u_sync(TRUE);
18293 }
18294
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018295 if (lnum <= curbuf->b_ml.ml_line_count)
18296 {
18297 /* existing line, replace it */
18298 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18299 {
18300 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018301 if (lnum == curwin->w_cursor.lnum)
18302 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018303 rettv->vval.v_number = 0; /* OK */
18304 }
18305 }
18306 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18307 {
18308 /* lnum is one past the last line, append the line */
18309 ++added;
18310 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18311 rettv->vval.v_number = 0; /* OK */
18312 }
18313
18314 if (l == NULL) /* only one string argument */
18315 break;
18316 ++lnum;
18317 }
18318
18319 if (added > 0)
18320 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018321}
18322
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018323static 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 +000018324
Bram Moolenaar071d4272004-06-13 20:20:40 +000018325/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018326 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018327 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018329set_qf_ll_list(
18330 win_T *wp UNUSED,
18331 typval_T *list_arg UNUSED,
18332 typval_T *action_arg UNUSED,
18333 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018334{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018335#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018336 char_u *act;
18337 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018338#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018339
Bram Moolenaar2641f772005-03-25 21:58:17 +000018340 rettv->vval.v_number = -1;
18341
18342#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018343 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018344 EMSG(_(e_listreq));
18345 else
18346 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018347 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018348
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018349 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018350 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018351 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018352 if (act == NULL)
18353 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018354 if (*act == 'a' || *act == 'r')
18355 action = *act;
18356 }
18357
Bram Moolenaar81484f42012-12-05 15:16:47 +010018358 if (l != NULL && set_errorlist(wp, l, action,
18359 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018360 rettv->vval.v_number = 0;
18361 }
18362#endif
18363}
18364
18365/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018366 * "setloclist()" function
18367 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018369f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018370{
18371 win_T *win;
18372
18373 rettv->vval.v_number = -1;
18374
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018375 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018376 if (win != NULL)
18377 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18378}
18379
18380/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018381 * "setmatches()" function
18382 */
18383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018384f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018385{
18386#ifdef FEAT_SEARCH_EXTRA
18387 list_T *l;
18388 listitem_T *li;
18389 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018390 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018391
18392 rettv->vval.v_number = -1;
18393 if (argvars[0].v_type != VAR_LIST)
18394 {
18395 EMSG(_(e_listreq));
18396 return;
18397 }
18398 if ((l = argvars[0].vval.v_list) != NULL)
18399 {
18400
18401 /* To some extent make sure that we are dealing with a list from
18402 * "getmatches()". */
18403 li = l->lv_first;
18404 while (li != NULL)
18405 {
18406 if (li->li_tv.v_type != VAR_DICT
18407 || (d = li->li_tv.vval.v_dict) == NULL)
18408 {
18409 EMSG(_(e_invarg));
18410 return;
18411 }
18412 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018413 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18414 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018415 && dict_find(d, (char_u *)"priority", -1) != NULL
18416 && dict_find(d, (char_u *)"id", -1) != NULL))
18417 {
18418 EMSG(_(e_invarg));
18419 return;
18420 }
18421 li = li->li_next;
18422 }
18423
18424 clear_matches(curwin);
18425 li = l->lv_first;
18426 while (li != NULL)
18427 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018428 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018429 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018430 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018431 char_u *group;
18432 int priority;
18433 int id;
18434 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018435
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018436 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018437 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18438 {
18439 if (s == NULL)
18440 {
18441 s = list_alloc();
18442 if (s == NULL)
18443 return;
18444 }
18445
18446 /* match from matchaddpos() */
18447 for (i = 1; i < 9; i++)
18448 {
18449 sprintf((char *)buf, (char *)"pos%d", i);
18450 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18451 {
18452 if (di->di_tv.v_type != VAR_LIST)
18453 return;
18454
18455 list_append_tv(s, &di->di_tv);
18456 s->lv_refcount++;
18457 }
18458 else
18459 break;
18460 }
18461 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018462
18463 group = get_dict_string(d, (char_u *)"group", FALSE);
18464 priority = (int)get_dict_number(d, (char_u *)"priority");
18465 id = (int)get_dict_number(d, (char_u *)"id");
18466 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18467 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18468 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018469 if (i == 0)
18470 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018471 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018472 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018473 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018474 }
18475 else
18476 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018477 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018478 list_unref(s);
18479 s = NULL;
18480 }
18481
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018482 li = li->li_next;
18483 }
18484 rettv->vval.v_number = 0;
18485 }
18486#endif
18487}
18488
18489/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018490 * "setpos()" function
18491 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018493f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018494{
18495 pos_T pos;
18496 int fnum;
18497 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018498 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018499
Bram Moolenaar08250432008-02-13 11:42:46 +000018500 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018501 name = get_tv_string_chk(argvars);
18502 if (name != NULL)
18503 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018504 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018505 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018506 if (--pos.col < 0)
18507 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018508 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018509 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018510 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018511 if (fnum == curbuf->b_fnum)
18512 {
18513 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018514 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018515 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018516 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018517 curwin->w_set_curswant = FALSE;
18518 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018519 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018520 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018521 }
18522 else
18523 EMSG(_(e_invarg));
18524 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018525 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18526 {
18527 /* set mark */
18528 if (setmark_pos(name[1], &pos, fnum) == OK)
18529 rettv->vval.v_number = 0;
18530 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018531 else
18532 EMSG(_(e_invarg));
18533 }
18534 }
18535}
18536
18537/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018538 * "setqflist()" function
18539 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018541f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018542{
18543 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18544}
18545
18546/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018547 * "setreg()" function
18548 */
18549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018550f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551{
18552 int regname;
18553 char_u *strregname;
18554 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018555 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018556 int append;
18557 char_u yank_type;
18558 long block_len;
18559
18560 block_len = -1;
18561 yank_type = MAUTO;
18562 append = FALSE;
18563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018564 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018565 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018567 if (strregname == NULL)
18568 return; /* type error; errmsg already given */
18569 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 if (regname == 0 || regname == '@')
18571 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018572
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018573 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018575 stropt = get_tv_string_chk(&argvars[2]);
18576 if (stropt == NULL)
18577 return; /* type error */
18578 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018579 switch (*stropt)
18580 {
18581 case 'a': case 'A': /* append */
18582 append = TRUE;
18583 break;
18584 case 'v': case 'c': /* character-wise selection */
18585 yank_type = MCHAR;
18586 break;
18587 case 'V': case 'l': /* line-wise selection */
18588 yank_type = MLINE;
18589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018590 case 'b': case Ctrl_V: /* block-wise selection */
18591 yank_type = MBLOCK;
18592 if (VIM_ISDIGIT(stropt[1]))
18593 {
18594 ++stropt;
18595 block_len = getdigits(&stropt) - 1;
18596 --stropt;
18597 }
18598 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018599 }
18600 }
18601
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018602 if (argvars[1].v_type == VAR_LIST)
18603 {
18604 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018605 char_u **allocval;
18606 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018607 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018608 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018609 int len = argvars[1].vval.v_list->lv_len;
18610 listitem_T *li;
18611
Bram Moolenaar7d647822014-04-05 21:28:56 +020018612 /* First half: use for pointers to result lines; second half: use for
18613 * pointers to allocated copies. */
18614 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018615 if (lstval == NULL)
18616 return;
18617 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018618 allocval = lstval + len + 2;
18619 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018620
18621 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18622 li = li->li_next)
18623 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018624 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018625 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018626 goto free_lstval;
18627 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018628 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018629 /* Need to make a copy, next get_tv_string_buf_chk() will
18630 * overwrite the string. */
18631 strval = vim_strsave(buf);
18632 if (strval == NULL)
18633 goto free_lstval;
18634 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018635 }
18636 *curval++ = strval;
18637 }
18638 *curval++ = NULL;
18639
18640 write_reg_contents_lst(regname, lstval, -1,
18641 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018642free_lstval:
18643 while (curallocval > allocval)
18644 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018645 vim_free(lstval);
18646 }
18647 else
18648 {
18649 strval = get_tv_string_chk(&argvars[1]);
18650 if (strval == NULL)
18651 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018652 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018653 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018654 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018655 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656}
18657
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018658/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018659 * "settabvar()" function
18660 */
18661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018662f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018663{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018664#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018665 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018666 tabpage_T *tp;
18667#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018668 char_u *varname, *tabvarname;
18669 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018670
18671 rettv->vval.v_number = 0;
18672
18673 if (check_restricted() || check_secure())
18674 return;
18675
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018676#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018677 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018678#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018679 varname = get_tv_string_chk(&argvars[1]);
18680 varp = &argvars[2];
18681
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018682 if (varname != NULL && varp != NULL
18683#ifdef FEAT_WINDOWS
18684 && tp != NULL
18685#endif
18686 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018687 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018688#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018689 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018690 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018691#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018692
18693 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18694 if (tabvarname != NULL)
18695 {
18696 STRCPY(tabvarname, "t:");
18697 STRCPY(tabvarname + 2, varname);
18698 set_var(tabvarname, varp, TRUE);
18699 vim_free(tabvarname);
18700 }
18701
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018702#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018703 /* Restore current tabpage */
18704 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018705 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018706#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018707 }
18708}
18709
18710/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018711 * "settabwinvar()" function
18712 */
18713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018714f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018715{
18716 setwinvar(argvars, rettv, 1);
18717}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018718
18719/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018720 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018723f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018724{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018725 setwinvar(argvars, rettv, 0);
18726}
18727
18728/*
18729 * "setwinvar()" and "settabwinvar()" functions
18730 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018731
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018733setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018734{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018735 win_T *win;
18736#ifdef FEAT_WINDOWS
18737 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018738 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018739 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018740#endif
18741 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018742 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018744 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
18746 if (check_restricted() || check_secure())
18747 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018748
18749#ifdef FEAT_WINDOWS
18750 if (off == 1)
18751 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18752 else
18753 tp = curtab;
18754#endif
18755 win = find_win_by_nr(&argvars[off], tp);
18756 varname = get_tv_string_chk(&argvars[off + 1]);
18757 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018758
18759 if (win != NULL && varname != NULL && varp != NULL)
18760 {
18761#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018762 need_switch_win = !(tp == curtab && win == curwin);
18763 if (!need_switch_win
18764 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018766 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018767 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018768 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018769 long numval;
18770 char_u *strval;
18771 int error = FALSE;
18772
18773 ++varname;
18774 numval = get_tv_number_chk(varp, &error);
18775 strval = get_tv_string_buf_chk(varp, nbuf);
18776 if (!error && strval != NULL)
18777 set_option_value(varname, numval, strval, OPT_LOCAL);
18778 }
18779 else
18780 {
18781 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18782 if (winvarname != NULL)
18783 {
18784 STRCPY(winvarname, "w:");
18785 STRCPY(winvarname + 2, varname);
18786 set_var(winvarname, varp, TRUE);
18787 vim_free(winvarname);
18788 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018789 }
18790 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018791#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018792 if (need_switch_win)
18793 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018794#endif
18795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018796}
18797
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018798#ifdef FEAT_CRYPT
18799/*
18800 * "sha256({string})" function
18801 */
18802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018803f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018804{
18805 char_u *p;
18806
18807 p = get_tv_string(&argvars[0]);
18808 rettv->vval.v_string = vim_strsave(
18809 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18810 rettv->v_type = VAR_STRING;
18811}
18812#endif /* FEAT_CRYPT */
18813
Bram Moolenaar071d4272004-06-13 20:20:40 +000018814/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018815 * "shellescape({string})" function
18816 */
18817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018818f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018819{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018820 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018821 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018822 rettv->v_type = VAR_STRING;
18823}
18824
18825/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018826 * shiftwidth() function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018830{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018831 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018832}
18833
18834/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018835 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018836 */
18837 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018838f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018839{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018841
Bram Moolenaar0d660222005-01-07 21:51:51 +000018842 p = get_tv_string(&argvars[0]);
18843 rettv->vval.v_string = vim_strsave(p);
18844 simplify_filename(rettv->vval.v_string); /* simplify in place */
18845 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018846}
18847
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018848#ifdef FEAT_FLOAT
18849/*
18850 * "sin()" function
18851 */
18852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018853f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018854{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018855 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018856
18857 rettv->v_type = VAR_FLOAT;
18858 if (get_float_arg(argvars, &f) == OK)
18859 rettv->vval.v_float = sin(f);
18860 else
18861 rettv->vval.v_float = 0.0;
18862}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018863
18864/*
18865 * "sinh()" function
18866 */
18867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018868f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018869{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018870 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018871
18872 rettv->v_type = VAR_FLOAT;
18873 if (get_float_arg(argvars, &f) == OK)
18874 rettv->vval.v_float = sinh(f);
18875 else
18876 rettv->vval.v_float = 0.0;
18877}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018878#endif
18879
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880static int
18881#ifdef __BORLANDC__
18882 _RTLENTRYF
18883#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018884 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018885static int
18886#ifdef __BORLANDC__
18887 _RTLENTRYF
18888#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018889 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018890
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018891/* struct used in the array that's given to qsort() */
18892typedef struct
18893{
18894 listitem_T *item;
18895 int idx;
18896} sortItem_T;
18897
Bram Moolenaar0b962472016-02-22 22:51:33 +010018898/* struct storing information about current sort */
18899typedef struct
18900{
18901 int item_compare_ic;
18902 int item_compare_numeric;
18903 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018904#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018905 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018906#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018907 char_u *item_compare_func;
18908 dict_T *item_compare_selfdict;
18909 int item_compare_func_err;
18910 int item_compare_keep_zero;
18911} sortinfo_T;
18912static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018913static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018914#define ITEM_COMPARE_FAIL 999
18915
Bram Moolenaar071d4272004-06-13 20:20:40 +000018916/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018917 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018918 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018919 static int
18920#ifdef __BORLANDC__
18921_RTLENTRYF
18922#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018923item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018924{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018925 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018926 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018927 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018928 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018929 int res;
18930 char_u numbuf1[NUMBUFLEN];
18931 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018932
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018933 si1 = (sortItem_T *)s1;
18934 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018935 tv1 = &si1->item->li_tv;
18936 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018937
Bram Moolenaar0b962472016-02-22 22:51:33 +010018938 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018939 {
18940 long v1 = get_tv_number(tv1);
18941 long v2 = get_tv_number(tv2);
18942
18943 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18944 }
18945
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018946#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018947 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018948 {
18949 float_T v1 = get_tv_float(tv1);
18950 float_T v2 = get_tv_float(tv2);
18951
18952 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18953 }
18954#endif
18955
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018956 /* tv2string() puts quotes around a string and allocates memory. Don't do
18957 * that for string variables. Use a single quote when comparing with a
18958 * non-string to do what the docs promise. */
18959 if (tv1->v_type == VAR_STRING)
18960 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018961 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018962 p1 = (char_u *)"'";
18963 else
18964 p1 = tv1->vval.v_string;
18965 }
18966 else
18967 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18968 if (tv2->v_type == VAR_STRING)
18969 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018970 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018971 p2 = (char_u *)"'";
18972 else
18973 p2 = tv2->vval.v_string;
18974 }
18975 else
18976 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018977 if (p1 == NULL)
18978 p1 = (char_u *)"";
18979 if (p2 == NULL)
18980 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018981 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018982 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018983 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018984 res = STRICMP(p1, p2);
18985 else
18986 res = STRCMP(p1, p2);
18987 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018989 {
18990 double n1, n2;
18991 n1 = strtod((char *)p1, (char **)&p1);
18992 n2 = strtod((char *)p2, (char **)&p2);
18993 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18994 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018995
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018996 /* When the result would be zero, compare the item indexes. Makes the
18997 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018998 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018999 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019000
Bram Moolenaar0d660222005-01-07 21:51:51 +000019001 vim_free(tofree1);
19002 vim_free(tofree2);
19003 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019004}
19005
19006 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000019007#ifdef __BORLANDC__
19008_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000019009#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010019010item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019012 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000019014 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019015 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000019016 int dummy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019018 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019019 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019020 return 0;
19021
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019022 si1 = (sortItem_T *)s1;
19023 si2 = (sortItem_T *)s2;
19024
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019025 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000019026 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019027 copy_tv(&si1->item->li_tv, &argv[0]);
19028 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019029
19030 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019031 res = call_func(sortinfo->item_compare_func,
Bram Moolenaar4e221c92016-02-23 13:20:22 +010019032 (int)STRLEN(sortinfo->item_compare_func),
Bram Moolenaar5f894962011-06-19 02:55:37 +020019033 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar0b962472016-02-22 22:51:33 +010019034 sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019035 clear_tv(&argv[0]);
19036 clear_tv(&argv[1]);
19037
19038 if (res == FAIL)
19039 res = ITEM_COMPARE_FAIL;
19040 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019041 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
19042 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000019043 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019044 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019045
19046 /* When the result would be zero, compare the pointers themselves. Makes
19047 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019048 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019049 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020019050
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 return res;
19052}
19053
19054/*
19055 * "sort({list})" function
19056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019058do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019059{
Bram Moolenaar33570922005-01-25 22:26:29 +000019060 list_T *l;
19061 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019062 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019063 sortinfo_T *old_sortinfo;
19064 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019065 long len;
19066 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019067
Bram Moolenaar0b962472016-02-22 22:51:33 +010019068 /* Pointer to current info struct used in compare function. Save and
19069 * restore the current one for nested calls. */
19070 old_sortinfo = sortinfo;
19071 sortinfo = &info;
19072
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019074 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000019075 else
19076 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020019078 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020019079 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
19080 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010019081 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019082 rettv->vval.v_list = l;
19083 rettv->v_type = VAR_LIST;
19084 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085
Bram Moolenaar0d660222005-01-07 21:51:51 +000019086 len = list_len(l);
19087 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019088 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089
Bram Moolenaar0b962472016-02-22 22:51:33 +010019090 info.item_compare_ic = FALSE;
19091 info.item_compare_numeric = FALSE;
19092 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019093#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019094 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019095#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019096 info.item_compare_func = NULL;
19097 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 if (argvars[1].v_type != VAR_UNKNOWN)
19099 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020019100 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019101 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019102 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019103 else
19104 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019105 int error = FALSE;
19106
19107 i = get_tv_number_chk(&argvars[1], &error);
19108 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019109 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000019110 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019111 info.item_compare_ic = TRUE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010019113 info.item_compare_func = get_tv_string(&argvars[1]);
19114 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019115 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019116 if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019117 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019118 info.item_compare_func = NULL;
19119 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019120 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019121 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019122 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019123 info.item_compare_func = NULL;
19124 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010019125 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019126#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010019127 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019128 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019129 info.item_compare_func = NULL;
19130 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010019131 }
19132#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010019133 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020019134 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010019135 info.item_compare_func = NULL;
19136 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020019137 }
19138 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019139 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020019140
19141 if (argvars[2].v_type != VAR_UNKNOWN)
19142 {
19143 /* optional third argument: {dict} */
19144 if (argvars[2].v_type != VAR_DICT)
19145 {
19146 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010019147 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019148 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019149 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020019150 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019151 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019152
Bram Moolenaar0d660222005-01-07 21:51:51 +000019153 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019154 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000019155 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010019156 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019157
Bram Moolenaar327aa022014-03-25 18:24:23 +010019158 i = 0;
19159 if (sort)
19160 {
19161 /* sort(): ptrs will be the list to sort */
19162 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019163 {
19164 ptrs[i].item = li;
19165 ptrs[i].idx = i;
19166 ++i;
19167 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010019168
Bram Moolenaar0b962472016-02-22 22:51:33 +010019169 info.item_compare_func_err = FALSE;
19170 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019171 /* test the compare function */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019172 if (info.item_compare_func != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010019173 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000019174 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019175 EMSG(_("E702: Sort compare function failed"));
19176 else
19177 {
19178 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019179 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010019180 info.item_compare_func == NULL
19181 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019182
Bram Moolenaar0b962472016-02-22 22:51:33 +010019183 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019184 {
19185 /* Clear the List and append the items in sorted order. */
19186 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
19187 l->lv_len = 0;
19188 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019189 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019190 }
19191 }
19192 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019193 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000019194 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019195 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010019196
19197 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010019198 info.item_compare_func_err = FALSE;
19199 info.item_compare_keep_zero = TRUE;
19200 item_compare_func_ptr = info.item_compare_func
Bram Moolenaar327aa022014-03-25 18:24:23 +010019201 ? item_compare2 : item_compare;
19202
19203 for (li = l->lv_first; li != NULL && li->li_next != NULL;
19204 li = li->li_next)
19205 {
19206 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
19207 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019208 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010019209 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019210 {
19211 EMSG(_("E882: Uniq compare function failed"));
19212 break;
19213 }
19214 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019215
Bram Moolenaar0b962472016-02-22 22:51:33 +010019216 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019217 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010019218 while (--i >= 0)
19219 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019220 li = ptrs[i].item->li_next;
19221 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019222 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019223 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019224 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020019225 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010019226 list_fix_watch(l, li);
19227 listitem_free(li);
19228 l->lv_len--;
19229 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019230 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019231 }
19232
19233 vim_free(ptrs);
19234 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010019235theend:
19236 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019237}
19238
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019239/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010019240 * "sort({list})" function
19241 */
19242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019243f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019244{
19245 do_sort_uniq(argvars, rettv, TRUE);
19246}
19247
19248/*
19249 * "uniq({list})" function
19250 */
19251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019252f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010019253{
19254 do_sort_uniq(argvars, rettv, FALSE);
19255}
19256
19257/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019258 * "soundfold({word})" function
19259 */
19260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019261f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019262{
19263 char_u *s;
19264
19265 rettv->v_type = VAR_STRING;
19266 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019267#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000019268 rettv->vval.v_string = eval_soundfold(s);
19269#else
19270 rettv->vval.v_string = vim_strsave(s);
19271#endif
19272}
19273
19274/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019275 * "spellbadword()" function
19276 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019278f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019279{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019280 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019281 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019282 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019283
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019284 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019285 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019286
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019287#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019288 if (argvars[0].v_type == VAR_UNKNOWN)
19289 {
19290 /* Find the start and length of the badly spelled word. */
19291 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19292 if (len != 0)
19293 word = ml_get_cursor();
19294 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019295 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019296 {
19297 char_u *str = get_tv_string_chk(&argvars[0]);
19298 int capcol = -1;
19299
19300 if (str != NULL)
19301 {
19302 /* Check the argument for spelling. */
19303 while (*str != NUL)
19304 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019305 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019306 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019307 {
19308 word = str;
19309 break;
19310 }
19311 str += len;
19312 }
19313 }
19314 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019315#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019316
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019317 list_append_string(rettv->vval.v_list, word, len);
19318 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019319 attr == HLF_SPB ? "bad" :
19320 attr == HLF_SPR ? "rare" :
19321 attr == HLF_SPL ? "local" :
19322 attr == HLF_SPC ? "caps" :
19323 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019324}
19325
19326/*
19327 * "spellsuggest()" function
19328 */
19329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019330f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019331{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019332#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019333 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019334 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019335 int maxcount;
19336 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019337 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019338 listitem_T *li;
19339 int need_capital = FALSE;
19340#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019341
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019342 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019343 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019344
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019345#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019346 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019347 {
19348 str = get_tv_string(&argvars[0]);
19349 if (argvars[1].v_type != VAR_UNKNOWN)
19350 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019351 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019352 if (maxcount <= 0)
19353 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019354 if (argvars[2].v_type != VAR_UNKNOWN)
19355 {
19356 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19357 if (typeerr)
19358 return;
19359 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019360 }
19361 else
19362 maxcount = 25;
19363
Bram Moolenaar4770d092006-01-12 23:22:24 +000019364 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019365
19366 for (i = 0; i < ga.ga_len; ++i)
19367 {
19368 str = ((char_u **)ga.ga_data)[i];
19369
19370 li = listitem_alloc();
19371 if (li == NULL)
19372 vim_free(str);
19373 else
19374 {
19375 li->li_tv.v_type = VAR_STRING;
19376 li->li_tv.v_lock = 0;
19377 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019378 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019379 }
19380 }
19381 ga_clear(&ga);
19382 }
19383#endif
19384}
19385
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019387f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019388{
19389 char_u *str;
19390 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019391 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019392 regmatch_T regmatch;
19393 char_u patbuf[NUMBUFLEN];
19394 char_u *save_cpo;
19395 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019397 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019399
19400 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19401 save_cpo = p_cpo;
19402 p_cpo = (char_u *)"";
19403
19404 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019405 if (argvars[1].v_type != VAR_UNKNOWN)
19406 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19408 if (pat == NULL)
19409 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019410 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019411 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019412 }
19413 if (pat == NULL || *pat == NUL)
19414 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019415
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019416 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019417 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019418 if (typeerr)
19419 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019420
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19422 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019423 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019424 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019425 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019426 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019427 if (*str == NUL)
19428 match = FALSE; /* empty item at the end */
19429 else
19430 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431 if (match)
19432 end = regmatch.startp[0];
19433 else
19434 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019435 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19436 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019438 if (list_append_string(rettv->vval.v_list, str,
19439 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019441 }
19442 if (!match)
19443 break;
19444 /* Advance to just after the match. */
19445 if (regmatch.endp[0] > str)
19446 col = 0;
19447 else
19448 {
19449 /* Don't get stuck at the same match. */
19450#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019451 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019452#else
19453 col = 1;
19454#endif
19455 }
19456 str = regmatch.endp[0];
19457 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019458
Bram Moolenaar473de612013-06-08 18:19:48 +020019459 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019461
Bram Moolenaar0d660222005-01-07 21:51:51 +000019462 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019463}
19464
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019465#ifdef FEAT_FLOAT
19466/*
19467 * "sqrt()" function
19468 */
19469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019470f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019471{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019472 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019473
19474 rettv->v_type = VAR_FLOAT;
19475 if (get_float_arg(argvars, &f) == OK)
19476 rettv->vval.v_float = sqrt(f);
19477 else
19478 rettv->vval.v_float = 0.0;
19479}
19480
19481/*
19482 * "str2float()" function
19483 */
19484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019485f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019486{
19487 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19488
19489 if (*p == '+')
19490 p = skipwhite(p + 1);
19491 (void)string2float(p, &rettv->vval.v_float);
19492 rettv->v_type = VAR_FLOAT;
19493}
19494#endif
19495
Bram Moolenaar2c932302006-03-18 21:42:09 +000019496/*
19497 * "str2nr()" function
19498 */
19499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019500f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019501{
19502 int base = 10;
19503 char_u *p;
19504 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019505 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019506
19507 if (argvars[1].v_type != VAR_UNKNOWN)
19508 {
19509 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019510 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019511 {
19512 EMSG(_(e_invarg));
19513 return;
19514 }
19515 }
19516
19517 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019518 if (*p == '+')
19519 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019520 switch (base)
19521 {
19522 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19523 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19524 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19525 default: what = 0;
19526 }
19527 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019528 rettv->vval.v_number = n;
19529}
19530
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531#ifdef HAVE_STRFTIME
19532/*
19533 * "strftime({format}[, {time}])" function
19534 */
19535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019536f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019537{
19538 char_u result_buf[256];
19539 struct tm *curtime;
19540 time_t seconds;
19541 char_u *p;
19542
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019543 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019544
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019545 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019546 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 seconds = time(NULL);
19548 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019549 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550 curtime = localtime(&seconds);
19551 /* MSVC returns NULL for an invalid value of seconds. */
19552 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019553 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 else
19555 {
19556# ifdef FEAT_MBYTE
19557 vimconv_T conv;
19558 char_u *enc;
19559
19560 conv.vc_type = CONV_NONE;
19561 enc = enc_locale();
19562 convert_setup(&conv, p_enc, enc);
19563 if (conv.vc_type != CONV_NONE)
19564 p = string_convert(&conv, p, NULL);
19565# endif
19566 if (p != NULL)
19567 (void)strftime((char *)result_buf, sizeof(result_buf),
19568 (char *)p, curtime);
19569 else
19570 result_buf[0] = NUL;
19571
19572# ifdef FEAT_MBYTE
19573 if (conv.vc_type != CONV_NONE)
19574 vim_free(p);
19575 convert_setup(&conv, enc, p_enc);
19576 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019577 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019578 else
19579# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019580 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581
19582# ifdef FEAT_MBYTE
19583 /* Release conversion descriptors */
19584 convert_setup(&conv, NULL, NULL);
19585 vim_free(enc);
19586# endif
19587 }
19588}
19589#endif
19590
19591/*
19592 * "stridx()" function
19593 */
19594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019595f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596{
19597 char_u buf[NUMBUFLEN];
19598 char_u *needle;
19599 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019600 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019601 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019602 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019603
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019604 needle = get_tv_string_chk(&argvars[1]);
19605 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019606 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019607 if (needle == NULL || haystack == NULL)
19608 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019609
Bram Moolenaar33570922005-01-25 22:26:29 +000019610 if (argvars[2].v_type != VAR_UNKNOWN)
19611 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019612 int error = FALSE;
19613
19614 start_idx = get_tv_number_chk(&argvars[2], &error);
19615 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019616 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019617 if (start_idx >= 0)
19618 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019619 }
19620
19621 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19622 if (pos != NULL)
19623 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019624}
19625
19626/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019627 * "string()" function
19628 */
19629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019630f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019631{
19632 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019633 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019634
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019635 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019636 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019637 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019638 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019639 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019640}
19641
19642/*
19643 * "strlen()" function
19644 */
19645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019646f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 rettv->vval.v_number = (varnumber_T)(STRLEN(
19649 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019650}
19651
19652/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019653 * "strchars()" function
19654 */
19655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019656f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019657{
19658 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019659 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019660#ifdef FEAT_MBYTE
19661 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019662 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019663#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019664
19665 if (argvars[1].v_type != VAR_UNKNOWN)
19666 skipcc = get_tv_number_chk(&argvars[1], NULL);
19667 if (skipcc < 0 || skipcc > 1)
19668 EMSG(_(e_invarg));
19669 else
19670 {
19671#ifdef FEAT_MBYTE
19672 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19673 while (*s != NUL)
19674 {
19675 func_mb_ptr2char_adv(&s);
19676 ++len;
19677 }
19678 rettv->vval.v_number = len;
19679#else
19680 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19681#endif
19682 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019683}
19684
19685/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019686 * "strdisplaywidth()" function
19687 */
19688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019689f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019690{
19691 char_u *s = get_tv_string(&argvars[0]);
19692 int col = 0;
19693
19694 if (argvars[1].v_type != VAR_UNKNOWN)
19695 col = get_tv_number(&argvars[1]);
19696
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019697 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019698}
19699
19700/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019701 * "strwidth()" function
19702 */
19703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019704f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019705{
19706 char_u *s = get_tv_string(&argvars[0]);
19707
19708 rettv->vval.v_number = (varnumber_T)(
19709#ifdef FEAT_MBYTE
19710 mb_string2cells(s, -1)
19711#else
19712 STRLEN(s)
19713#endif
19714 );
19715}
19716
19717/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019718 * "strpart()" function
19719 */
19720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019721f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722{
19723 char_u *p;
19724 int n;
19725 int len;
19726 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019727 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019728
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019729 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019730 slen = (int)STRLEN(p);
19731
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019732 n = get_tv_number_chk(&argvars[1], &error);
19733 if (error)
19734 len = 0;
19735 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737 else
19738 len = slen - n; /* default len: all bytes that are available. */
19739
19740 /*
19741 * Only return the overlap between the specified part and the actual
19742 * string.
19743 */
19744 if (n < 0)
19745 {
19746 len += n;
19747 n = 0;
19748 }
19749 else if (n > slen)
19750 n = slen;
19751 if (len < 0)
19752 len = 0;
19753 else if (n + len > slen)
19754 len = slen - n;
19755
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019756 rettv->v_type = VAR_STRING;
19757 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019758}
19759
19760/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019761 * "strridx()" function
19762 */
19763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019764f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019765{
19766 char_u buf[NUMBUFLEN];
19767 char_u *needle;
19768 char_u *haystack;
19769 char_u *rest;
19770 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019771 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019772
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019773 needle = get_tv_string_chk(&argvars[1]);
19774 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019775
19776 rettv->vval.v_number = -1;
19777 if (needle == NULL || haystack == NULL)
19778 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019779
19780 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019781 if (argvars[2].v_type != VAR_UNKNOWN)
19782 {
19783 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019784 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019785 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019786 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019787 }
19788 else
19789 end_idx = haystack_len;
19790
Bram Moolenaar0d660222005-01-07 21:51:51 +000019791 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019792 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019793 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019794 lastmatch = haystack + end_idx;
19795 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019796 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019797 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019798 for (rest = haystack; *rest != '\0'; ++rest)
19799 {
19800 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019801 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019802 break;
19803 lastmatch = rest;
19804 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019805 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019806
19807 if (lastmatch == NULL)
19808 rettv->vval.v_number = -1;
19809 else
19810 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19811}
19812
19813/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 * "strtrans()" function
19815 */
19816 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019817f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019818{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019819 rettv->v_type = VAR_STRING;
19820 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821}
19822
19823/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019824 * "submatch()" function
19825 */
19826 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019827f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019828{
Bram Moolenaar41571762014-04-02 19:00:58 +020019829 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019830 int no;
19831 int retList = 0;
19832
19833 no = (int)get_tv_number_chk(&argvars[0], &error);
19834 if (error)
19835 return;
19836 error = FALSE;
19837 if (argvars[1].v_type != VAR_UNKNOWN)
19838 retList = get_tv_number_chk(&argvars[1], &error);
19839 if (error)
19840 return;
19841
19842 if (retList == 0)
19843 {
19844 rettv->v_type = VAR_STRING;
19845 rettv->vval.v_string = reg_submatch(no);
19846 }
19847 else
19848 {
19849 rettv->v_type = VAR_LIST;
19850 rettv->vval.v_list = reg_submatch_list(no);
19851 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019852}
19853
19854/*
19855 * "substitute()" function
19856 */
19857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019858f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019859{
19860 char_u patbuf[NUMBUFLEN];
19861 char_u subbuf[NUMBUFLEN];
19862 char_u flagsbuf[NUMBUFLEN];
19863
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019864 char_u *str = get_tv_string_chk(&argvars[0]);
19865 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19866 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19867 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19868
Bram Moolenaar0d660222005-01-07 21:51:51 +000019869 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019870 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19871 rettv->vval.v_string = NULL;
19872 else
19873 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019874}
19875
19876/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019877 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019878 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019880f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019881{
19882 int id = 0;
19883#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019884 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019885 long col;
19886 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019887 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019888
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019889 lnum = get_tv_lnum(argvars); /* -1 on type error */
19890 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19891 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019892
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019893 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019894 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019895 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019896#endif
19897
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019898 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899}
19900
19901/*
19902 * "synIDattr(id, what [, mode])" function
19903 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019906{
19907 char_u *p = NULL;
19908#ifdef FEAT_SYN_HL
19909 int id;
19910 char_u *what;
19911 char_u *mode;
19912 char_u modebuf[NUMBUFLEN];
19913 int modec;
19914
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019915 id = get_tv_number(&argvars[0]);
19916 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019917 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019918 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019919 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019920 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019921 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019922 modec = 0; /* replace invalid with current */
19923 }
19924 else
19925 {
19926#ifdef FEAT_GUI
19927 if (gui.in_use)
19928 modec = 'g';
19929 else
19930#endif
19931 if (t_colors > 1)
19932 modec = 'c';
19933 else
19934 modec = 't';
19935 }
19936
19937
19938 switch (TOLOWER_ASC(what[0]))
19939 {
19940 case 'b':
19941 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19942 p = highlight_color(id, what, modec);
19943 else /* bold */
19944 p = highlight_has_attr(id, HL_BOLD, modec);
19945 break;
19946
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019947 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019948 p = highlight_color(id, what, modec);
19949 break;
19950
19951 case 'i':
19952 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19953 p = highlight_has_attr(id, HL_INVERSE, modec);
19954 else /* italic */
19955 p = highlight_has_attr(id, HL_ITALIC, modec);
19956 break;
19957
19958 case 'n': /* name */
19959 p = get_highlight_name(NULL, id - 1);
19960 break;
19961
19962 case 'r': /* reverse */
19963 p = highlight_has_attr(id, HL_INVERSE, modec);
19964 break;
19965
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019966 case 's':
19967 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19968 p = highlight_color(id, what, modec);
19969 else /* standout */
19970 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971 break;
19972
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019973 case 'u':
19974 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19975 /* underline */
19976 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19977 else
19978 /* undercurl */
19979 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019980 break;
19981 }
19982
19983 if (p != NULL)
19984 p = vim_strsave(p);
19985#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019986 rettv->v_type = VAR_STRING;
19987 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019988}
19989
19990/*
19991 * "synIDtrans(id)" function
19992 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019993 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019994f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019995{
19996 int id;
19997
19998#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019999 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020000
20001 if (id > 0)
20002 id = syn_get_final_id(id);
20003 else
20004#endif
20005 id = 0;
20006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020007 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020008}
20009
20010/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020011 * "synconcealed(lnum, col)" function
20012 */
20013 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020014f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020020015{
20016#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20017 long lnum;
20018 long col;
20019 int syntax_flags = 0;
20020 int cchar;
20021 int matchid = 0;
20022 char_u str[NUMBUFLEN];
20023#endif
20024
20025 rettv->v_type = VAR_LIST;
20026 rettv->vval.v_list = NULL;
20027
20028#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
20029 lnum = get_tv_lnum(argvars); /* -1 on type error */
20030 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20031
20032 vim_memset(str, NUL, sizeof(str));
20033
20034 if (rettv_list_alloc(rettv) != FAIL)
20035 {
20036 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
20037 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
20038 && curwin->w_p_cole > 0)
20039 {
20040 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
20041 syntax_flags = get_syntax_info(&matchid);
20042
20043 /* get the conceal character */
20044 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
20045 {
20046 cchar = syn_get_sub_char();
20047 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
20048 cchar = lcs_conceal;
20049 if (cchar != NUL)
20050 {
20051# ifdef FEAT_MBYTE
20052 if (has_mbyte)
20053 (*mb_char2bytes)(cchar, str);
20054 else
20055# endif
20056 str[0] = cchar;
20057 }
20058 }
20059 }
20060
20061 list_append_number(rettv->vval.v_list,
20062 (syntax_flags & HL_CONCEAL) != 0);
20063 /* -1 to auto-determine strlen */
20064 list_append_string(rettv->vval.v_list, str, -1);
20065 list_append_number(rettv->vval.v_list, matchid);
20066 }
20067#endif
20068}
20069
20070/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020071 * "synstack(lnum, col)" function
20072 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020074f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020075{
20076#ifdef FEAT_SYN_HL
20077 long lnum;
20078 long col;
20079 int i;
20080 int id;
20081#endif
20082
20083 rettv->v_type = VAR_LIST;
20084 rettv->vval.v_list = NULL;
20085
20086#ifdef FEAT_SYN_HL
20087 lnum = get_tv_lnum(argvars); /* -1 on type error */
20088 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
20089
20090 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020020091 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020092 && rettv_list_alloc(rettv) != FAIL)
20093 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000020094 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000020095 for (i = 0; ; ++i)
20096 {
20097 id = syn_get_stack_item(i);
20098 if (id < 0)
20099 break;
20100 if (list_append_number(rettv->vval.v_list, id) == FAIL)
20101 break;
20102 }
20103 }
20104#endif
20105}
20106
Bram Moolenaar071d4272004-06-13 20:20:40 +000020107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020108get_cmd_output_as_rettv(
20109 typval_T *argvars,
20110 typval_T *rettv,
20111 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020112{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020113 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020114 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020115 char_u *infile = NULL;
20116 char_u buf[NUMBUFLEN];
20117 int err = FALSE;
20118 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020119 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020020120 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020121
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020122 rettv->v_type = VAR_STRING;
20123 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020124 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020125 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020126
Bram Moolenaar49cd9572005-01-03 21:06:01 +000020127 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020128 {
20129 /*
20130 * Write the string to a temp file, to be used for input of the shell
20131 * command.
20132 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020133 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020134 {
20135 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020136 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020137 }
20138
20139 fd = mch_fopen((char *)infile, WRITEBIN);
20140 if (fd == NULL)
20141 {
20142 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020143 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020144 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020145 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020146 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020147 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
20148 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000020149 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020150 else
20151 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020152 size_t len;
20153
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020154 p = get_tv_string_buf_chk(&argvars[1], buf);
20155 if (p == NULL)
20156 {
20157 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020158 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020159 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020020160 len = STRLEN(p);
20161 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020162 err = TRUE;
20163 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020164 if (fclose(fd) != 0)
20165 err = TRUE;
20166 if (err)
20167 {
20168 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020169 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020170 }
20171 }
20172
Bram Moolenaar52a72462014-08-29 15:53:52 +020020173 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
20174 * echoes typeahead, that messes up the display. */
20175 if (!msg_silent)
20176 flags += SHELL_COOKED;
20177
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020178 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020179 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020180 int len;
20181 listitem_T *li;
20182 char_u *s = NULL;
20183 char_u *start;
20184 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020185 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020186
Bram Moolenaar52a72462014-08-29 15:53:52 +020020187 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020188 if (res == NULL)
20189 goto errret;
20190
20191 list = list_alloc();
20192 if (list == NULL)
20193 goto errret;
20194
20195 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020196 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020197 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020198 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020199 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020200 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020201
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020202 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020203 if (s == NULL)
20204 goto errret;
20205
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020206 for (p = s; start < end; ++p, ++start)
20207 *p = *start == NUL ? NL : *start;
20208 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020209
20210 li = listitem_alloc();
20211 if (li == NULL)
20212 {
20213 vim_free(s);
20214 goto errret;
20215 }
20216 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010020217 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020218 li->li_tv.vval.v_string = s;
20219 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020220 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020221
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020020222 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020223 rettv->v_type = VAR_LIST;
20224 rettv->vval.v_list = list;
20225 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020226 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020227 else
20228 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020020229 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020230#ifdef USE_CR
20231 /* translate <CR> into <NL> */
20232 if (res != NULL)
20233 {
20234 char_u *s;
20235
20236 for (s = res; *s; ++s)
20237 {
20238 if (*s == CAR)
20239 *s = NL;
20240 }
20241 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020242#else
20243# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020244 /* translate <CR><NL> into <NL> */
20245 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020246 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020247 char_u *s, *d;
20248
20249 d = res;
20250 for (s = res; *s; ++s)
20251 {
20252 if (s[0] == CAR && s[1] == NL)
20253 ++s;
20254 *d++ = *s;
20255 }
20256 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020257 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000020258# endif
20259#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020260 rettv->vval.v_string = res;
20261 res = NULL;
20262 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020263
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020264errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000020265 if (infile != NULL)
20266 {
20267 mch_remove(infile);
20268 vim_free(infile);
20269 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020270 if (res != NULL)
20271 vim_free(res);
20272 if (list != NULL)
20273 list_free(list, TRUE);
20274}
20275
20276/*
20277 * "system()" function
20278 */
20279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020280f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020281{
20282 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20283}
20284
20285/*
20286 * "systemlist()" function
20287 */
20288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020289f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020290{
20291 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020292}
20293
20294/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020295 * "tabpagebuflist()" function
20296 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020298f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020299{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020300#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020301 tabpage_T *tp;
20302 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020303
20304 if (argvars[0].v_type == VAR_UNKNOWN)
20305 wp = firstwin;
20306 else
20307 {
20308 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20309 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020310 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020311 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020312 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020313 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020314 for (; wp != NULL; wp = wp->w_next)
20315 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020316 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020317 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020318 }
20319#endif
20320}
20321
20322
20323/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020324 * "tabpagenr()" function
20325 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020327f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020328{
20329 int nr = 1;
20330#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020331 char_u *arg;
20332
20333 if (argvars[0].v_type != VAR_UNKNOWN)
20334 {
20335 arg = get_tv_string_chk(&argvars[0]);
20336 nr = 0;
20337 if (arg != NULL)
20338 {
20339 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020340 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020341 else
20342 EMSG2(_(e_invexpr2), arg);
20343 }
20344 }
20345 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020346 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020347#endif
20348 rettv->vval.v_number = nr;
20349}
20350
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020351
20352#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020353static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020354
20355/*
20356 * Common code for tabpagewinnr() and winnr().
20357 */
20358 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020359get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020360{
20361 win_T *twin;
20362 int nr = 1;
20363 win_T *wp;
20364 char_u *arg;
20365
20366 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20367 if (argvar->v_type != VAR_UNKNOWN)
20368 {
20369 arg = get_tv_string_chk(argvar);
20370 if (arg == NULL)
20371 nr = 0; /* type error; errmsg already given */
20372 else if (STRCMP(arg, "$") == 0)
20373 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20374 else if (STRCMP(arg, "#") == 0)
20375 {
20376 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20377 if (twin == NULL)
20378 nr = 0;
20379 }
20380 else
20381 {
20382 EMSG2(_(e_invexpr2), arg);
20383 nr = 0;
20384 }
20385 }
20386
20387 if (nr > 0)
20388 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20389 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020390 {
20391 if (wp == NULL)
20392 {
20393 /* didn't find it in this tabpage */
20394 nr = 0;
20395 break;
20396 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020397 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020398 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020399 return nr;
20400}
20401#endif
20402
20403/*
20404 * "tabpagewinnr()" function
20405 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020407f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020408{
20409 int nr = 1;
20410#ifdef FEAT_WINDOWS
20411 tabpage_T *tp;
20412
20413 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20414 if (tp == NULL)
20415 nr = 0;
20416 else
20417 nr = get_winnr(tp, &argvars[1]);
20418#endif
20419 rettv->vval.v_number = nr;
20420}
20421
20422
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020423/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020424 * "tagfiles()" function
20425 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020427f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020428{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020429 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020430 tagname_T tn;
20431 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020433 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020434 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020435 fname = alloc(MAXPATHL);
20436 if (fname == NULL)
20437 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020438
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020439 for (first = TRUE; ; first = FALSE)
20440 if (get_tagfname(&tn, first, fname) == FAIL
20441 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020442 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020443 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020444 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020445}
20446
20447/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020448 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020449 */
20450 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020451f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020452{
20453 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020454
20455 tag_pattern = get_tv_string(&argvars[0]);
20456
20457 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020458 if (*tag_pattern == NUL)
20459 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020461 if (rettv_list_alloc(rettv) == OK)
20462 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020463}
20464
20465/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020466 * "tempname()" function
20467 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020469f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020470{
20471 static int x = 'A';
20472
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020473 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020474 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020475
20476 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20477 * names. Skip 'I' and 'O', they are used for shell redirection. */
20478 do
20479 {
20480 if (x == 'Z')
20481 x = '0';
20482 else if (x == '9')
20483 x = 'A';
20484 else
20485 {
20486#ifdef EBCDIC
20487 if (x == 'I')
20488 x = 'J';
20489 else if (x == 'R')
20490 x = 'S';
20491 else
20492#endif
20493 ++x;
20494 }
20495 } while (x == 'I' || x == 'O');
20496}
20497
20498/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020499 * "test(list)" function: Just checking the walls...
20500 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020502f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020503{
20504 /* Used for unit testing. Change the code below to your liking. */
20505#if 0
20506 listitem_T *li;
20507 list_T *l;
20508 char_u *bad, *good;
20509
20510 if (argvars[0].v_type != VAR_LIST)
20511 return;
20512 l = argvars[0].vval.v_list;
20513 if (l == NULL)
20514 return;
20515 li = l->lv_first;
20516 if (li == NULL)
20517 return;
20518 bad = get_tv_string(&li->li_tv);
20519 li = li->li_next;
20520 if (li == NULL)
20521 return;
20522 good = get_tv_string(&li->li_tv);
20523 rettv->vval.v_number = test_edit_score(bad, good);
20524#endif
20525}
20526
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020527#ifdef FEAT_FLOAT
20528/*
20529 * "tan()" function
20530 */
20531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020532f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020533{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020534 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020535
20536 rettv->v_type = VAR_FLOAT;
20537 if (get_float_arg(argvars, &f) == OK)
20538 rettv->vval.v_float = tan(f);
20539 else
20540 rettv->vval.v_float = 0.0;
20541}
20542
20543/*
20544 * "tanh()" function
20545 */
20546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020547f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020548{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020549 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020550
20551 rettv->v_type = VAR_FLOAT;
20552 if (get_float_arg(argvars, &f) == OK)
20553 rettv->vval.v_float = tanh(f);
20554 else
20555 rettv->vval.v_float = 0.0;
20556}
20557#endif
20558
Bram Moolenaard52d9742005-08-21 22:20:28 +000020559/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560 * "tolower(string)" function
20561 */
20562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020563f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564{
20565 char_u *p;
20566
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020567 p = vim_strsave(get_tv_string(&argvars[0]));
20568 rettv->v_type = VAR_STRING;
20569 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570
20571 if (p != NULL)
20572 while (*p != NUL)
20573 {
20574#ifdef FEAT_MBYTE
20575 int l;
20576
20577 if (enc_utf8)
20578 {
20579 int c, lc;
20580
20581 c = utf_ptr2char(p);
20582 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020583 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584 /* TODO: reallocate string when byte count changes. */
20585 if (utf_char2len(lc) == l)
20586 utf_char2bytes(lc, p);
20587 p += l;
20588 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020589 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590 p += l; /* skip multi-byte character */
20591 else
20592#endif
20593 {
20594 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20595 ++p;
20596 }
20597 }
20598}
20599
20600/*
20601 * "toupper(string)" function
20602 */
20603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020607 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020608}
20609
20610/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020611 * "tr(string, fromstr, tostr)" function
20612 */
20613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020614f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020615{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020616 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020617 char_u *fromstr;
20618 char_u *tostr;
20619 char_u *p;
20620#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020621 int inlen;
20622 int fromlen;
20623 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020624 int idx;
20625 char_u *cpstr;
20626 int cplen;
20627 int first = TRUE;
20628#endif
20629 char_u buf[NUMBUFLEN];
20630 char_u buf2[NUMBUFLEN];
20631 garray_T ga;
20632
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020633 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020634 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20635 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020636
20637 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020638 rettv->v_type = VAR_STRING;
20639 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020640 if (fromstr == NULL || tostr == NULL)
20641 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020642 ga_init2(&ga, (int)sizeof(char), 80);
20643
20644#ifdef FEAT_MBYTE
20645 if (!has_mbyte)
20646#endif
20647 /* not multi-byte: fromstr and tostr must be the same length */
20648 if (STRLEN(fromstr) != STRLEN(tostr))
20649 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020650#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020651error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020652#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020653 EMSG2(_(e_invarg2), fromstr);
20654 ga_clear(&ga);
20655 return;
20656 }
20657
20658 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020659 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020660 {
20661#ifdef FEAT_MBYTE
20662 if (has_mbyte)
20663 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020664 inlen = (*mb_ptr2len)(in_str);
20665 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020666 cplen = inlen;
20667 idx = 0;
20668 for (p = fromstr; *p != NUL; p += fromlen)
20669 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020670 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020671 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020672 {
20673 for (p = tostr; *p != NUL; p += tolen)
20674 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020675 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020676 if (idx-- == 0)
20677 {
20678 cplen = tolen;
20679 cpstr = p;
20680 break;
20681 }
20682 }
20683 if (*p == NUL) /* tostr is shorter than fromstr */
20684 goto error;
20685 break;
20686 }
20687 ++idx;
20688 }
20689
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020690 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020691 {
20692 /* Check that fromstr and tostr have the same number of
20693 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020694 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020695 first = FALSE;
20696 for (p = tostr; *p != NUL; p += tolen)
20697 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020698 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020699 --idx;
20700 }
20701 if (idx != 0)
20702 goto error;
20703 }
20704
Bram Moolenaarcde88542015-08-11 19:14:00 +020020705 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020706 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020707 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020708
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020709 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020710 }
20711 else
20712#endif
20713 {
20714 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020715 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020716 if (p != NULL)
20717 ga_append(&ga, tostr[p - fromstr]);
20718 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020719 ga_append(&ga, *in_str);
20720 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020721 }
20722 }
20723
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020724 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020725 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020726 ga_append(&ga, NUL);
20727
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020728 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020729}
20730
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020731#ifdef FEAT_FLOAT
20732/*
20733 * "trunc({float})" function
20734 */
20735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020736f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020737{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020738 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020739
20740 rettv->v_type = VAR_FLOAT;
20741 if (get_float_arg(argvars, &f) == OK)
20742 /* trunc() is not in C90, use floor() or ceil() instead. */
20743 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20744 else
20745 rettv->vval.v_float = 0.0;
20746}
20747#endif
20748
Bram Moolenaar8299df92004-07-10 09:47:34 +000020749/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 * "type(expr)" function
20751 */
20752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020753f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020754{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020755 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020756
20757 switch (argvars[0].v_type)
20758 {
20759 case VAR_NUMBER: n = 0; break;
20760 case VAR_STRING: n = 1; break;
20761 case VAR_FUNC: n = 2; break;
20762 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020763 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020764 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020765 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020766 if (argvars[0].vval.v_number == VVAL_FALSE
20767 || argvars[0].vval.v_number == VVAL_TRUE)
20768 n = 6;
20769 else
20770 n = 7;
20771 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020772 case VAR_JOB: n = 8; break;
20773 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020774 case VAR_UNKNOWN:
20775 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20776 n = -1;
20777 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020778 }
20779 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780}
20781
20782/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020783 * "undofile(name)" function
20784 */
20785 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020786f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020787{
20788 rettv->v_type = VAR_STRING;
20789#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020790 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020791 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020792
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020793 if (*fname == NUL)
20794 {
20795 /* If there is no file name there will be no undo file. */
20796 rettv->vval.v_string = NULL;
20797 }
20798 else
20799 {
20800 char_u *ffname = FullName_save(fname, FALSE);
20801
20802 if (ffname != NULL)
20803 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20804 vim_free(ffname);
20805 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020806 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020807#else
20808 rettv->vval.v_string = NULL;
20809#endif
20810}
20811
20812/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020813 * "undotree()" function
20814 */
20815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020816f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020817{
20818 if (rettv_dict_alloc(rettv) == OK)
20819 {
20820 dict_T *dict = rettv->vval.v_dict;
20821 list_T *list;
20822
Bram Moolenaar730cde92010-06-27 05:18:54 +020020823 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020824 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020825 dict_add_nr_str(dict, "save_last",
20826 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020827 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20828 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020829 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020830
20831 list = list_alloc();
20832 if (list != NULL)
20833 {
20834 u_eval_tree(curbuf->b_u_oldhead, list);
20835 dict_add_list(dict, "entries", list);
20836 }
20837 }
20838}
20839
20840/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020841 * "values(dict)" function
20842 */
20843 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020844f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020845{
20846 dict_list(argvars, rettv, 1);
20847}
20848
20849/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020850 * "virtcol(string)" function
20851 */
20852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020853f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020854{
20855 colnr_T vcol = 0;
20856 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020857 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020859 fp = var2fpos(&argvars[0], FALSE, &fnum);
20860 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20861 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020862 {
20863 getvvcol(curwin, fp, NULL, NULL, &vcol);
20864 ++vcol;
20865 }
20866
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020867 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868}
20869
20870/*
20871 * "visualmode()" function
20872 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020874f_visualmode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020875{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020876 char_u str[2];
20877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020878 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020879 str[0] = curbuf->b_visual_mode_eval;
20880 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020881 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020882
20883 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020884 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020886}
20887
20888/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020889 * "wildmenumode()" function
20890 */
20891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020892f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020893{
20894#ifdef FEAT_WILDMENU
20895 if (wild_menu_showing)
20896 rettv->vval.v_number = 1;
20897#endif
20898}
20899
20900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020901 * "winbufnr(nr)" function
20902 */
20903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020905{
20906 win_T *wp;
20907
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020908 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020909 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020910 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020912 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913}
20914
20915/*
20916 * "wincol()" function
20917 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020919f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020920{
20921 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020922 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923}
20924
20925/*
20926 * "winheight(nr)" function
20927 */
20928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020929f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020930{
20931 win_T *wp;
20932
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020933 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020934 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020935 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020936 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020937 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020938}
20939
20940/*
20941 * "winline()" function
20942 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020944f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020945{
20946 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020947 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020948}
20949
20950/*
20951 * "winnr()" function
20952 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020954f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020955{
20956 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020957
Bram Moolenaar071d4272004-06-13 20:20:40 +000020958#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020959 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020961 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962}
20963
20964/*
20965 * "winrestcmd()" function
20966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020968f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020969{
20970#ifdef FEAT_WINDOWS
20971 win_T *wp;
20972 int winnr = 1;
20973 garray_T ga;
20974 char_u buf[50];
20975
20976 ga_init2(&ga, (int)sizeof(char), 70);
20977 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20978 {
20979 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20980 ga_concat(&ga, buf);
20981# ifdef FEAT_VERTSPLIT
20982 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20983 ga_concat(&ga, buf);
20984# endif
20985 ++winnr;
20986 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020987 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020988
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020989 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020990#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020991 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020992#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020993 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020994}
20995
20996/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020997 * "winrestview()" function
20998 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021000f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021001{
21002 dict_T *dict;
21003
21004 if (argvars[0].v_type != VAR_DICT
21005 || (dict = argvars[0].vval.v_dict) == NULL)
21006 EMSG(_(e_invarg));
21007 else
21008 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020021009 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
21010 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
21011 if (dict_find(dict, (char_u *)"col", -1) != NULL)
21012 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021013#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020021014 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
21015 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021016#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021017 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
21018 {
21019 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
21020 curwin->w_set_curswant = FALSE;
21021 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021022
Bram Moolenaar82c25852014-05-28 16:47:16 +020021023 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
21024 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021025#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020021026 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
21027 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021028#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020021029 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
21030 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
21031 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
21032 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021033
21034 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020021035 win_new_height(curwin, curwin->w_height);
21036# ifdef FEAT_VERTSPLIT
21037 win_new_width(curwin, W_WIDTH(curwin));
21038# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020021039 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021040
Bram Moolenaarb851a962014-10-31 15:45:52 +010021041 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021042 curwin->w_topline = 1;
21043 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
21044 curwin->w_topline = curbuf->b_ml.ml_line_count;
21045#ifdef FEAT_DIFF
21046 check_topfill(curwin, TRUE);
21047#endif
21048 }
21049}
21050
21051/*
21052 * "winsaveview()" function
21053 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021054 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021055f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021056{
21057 dict_T *dict;
21058
Bram Moolenaara800b422010-06-27 01:15:55 +020021059 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021060 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020021061 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021062
21063 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
21064 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
21065#ifdef FEAT_VIRTUALEDIT
21066 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
21067#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000021068 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000021069 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
21070
21071 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
21072#ifdef FEAT_DIFF
21073 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
21074#endif
21075 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
21076 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
21077}
21078
21079/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021080 * "winwidth(nr)" function
21081 */
21082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021083f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021084{
21085 win_T *wp;
21086
Bram Moolenaar99ebf042006-04-15 20:28:54 +000021087 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021089 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021090 else
21091#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021092 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021094 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021095#endif
21096}
21097
Bram Moolenaar071d4272004-06-13 20:20:40 +000021098/*
Bram Moolenaared767a22016-01-03 22:49:16 +010021099 * "wordcount()" function
21100 */
21101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021102f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010021103{
21104 if (rettv_dict_alloc(rettv) == FAIL)
21105 return;
21106 cursor_pos_info(rettv->vval.v_dict);
21107}
21108
21109/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021110 * Write list of strings to file
21111 */
21112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021113write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021114{
21115 listitem_T *li;
21116 int c;
21117 int ret = OK;
21118 char_u *s;
21119
21120 for (li = list->lv_first; li != NULL; li = li->li_next)
21121 {
21122 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
21123 {
21124 if (*s == '\n')
21125 c = putc(NUL, fd);
21126 else
21127 c = putc(*s, fd);
21128 if (c == EOF)
21129 {
21130 ret = FAIL;
21131 break;
21132 }
21133 }
21134 if (!binary || li->li_next != NULL)
21135 if (putc('\n', fd) == EOF)
21136 {
21137 ret = FAIL;
21138 break;
21139 }
21140 if (ret == FAIL)
21141 {
21142 EMSG(_(e_write));
21143 break;
21144 }
21145 }
21146 return ret;
21147}
21148
21149/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021150 * "writefile()" function
21151 */
21152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021153f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021154{
21155 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021156 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021157 char_u *fname;
21158 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021159 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021160
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000021161 if (check_restricted() || check_secure())
21162 return;
21163
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021164 if (argvars[0].v_type != VAR_LIST)
21165 {
21166 EMSG2(_(e_listarg), "writefile()");
21167 return;
21168 }
21169 if (argvars[0].vval.v_list == NULL)
21170 return;
21171
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021172 if (argvars[2].v_type != VAR_UNKNOWN)
21173 {
21174 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
21175 binary = TRUE;
21176 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
21177 append = TRUE;
21178 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021179
21180 /* Always open the file in binary mode, library functions have a mind of
21181 * their own about CR-LF conversion. */
21182 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010021183 if (*fname == NUL || (fd = mch_fopen((char *)fname,
21184 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021185 {
21186 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
21187 ret = -1;
21188 }
21189 else
21190 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020021191 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
21192 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021193 fclose(fd);
21194 }
21195
21196 rettv->vval.v_number = ret;
21197}
21198
21199/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021200 * "xor(expr, expr)" function
21201 */
21202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021203f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021204{
21205 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21206 ^ get_tv_number_chk(&argvars[1], NULL);
21207}
21208
21209
21210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021212 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021213 */
21214 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021215var2fpos(
21216 typval_T *varp,
21217 int dollar_lnum, /* TRUE when $ is last line */
21218 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021220 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021221 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021222 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021223
Bram Moolenaara5525202006-03-02 22:52:09 +000021224 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021225 if (varp->v_type == VAR_LIST)
21226 {
21227 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021228 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021229 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021230 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021231
21232 l = varp->vval.v_list;
21233 if (l == NULL)
21234 return NULL;
21235
21236 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021237 pos.lnum = list_find_nr(l, 0L, &error);
21238 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021239 return NULL; /* invalid line number */
21240
21241 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021242 pos.col = list_find_nr(l, 1L, &error);
21243 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021244 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021245 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021246
21247 /* We accept "$" for the column number: last column. */
21248 li = list_find(l, 1L);
21249 if (li != NULL && li->li_tv.v_type == VAR_STRING
21250 && li->li_tv.vval.v_string != NULL
21251 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21252 pos.col = len + 1;
21253
Bram Moolenaara5525202006-03-02 22:52:09 +000021254 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021255 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021256 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021257 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021258
Bram Moolenaara5525202006-03-02 22:52:09 +000021259#ifdef FEAT_VIRTUALEDIT
21260 /* Get the virtual offset. Defaults to zero. */
21261 pos.coladd = list_find_nr(l, 2L, &error);
21262 if (error)
21263 pos.coladd = 0;
21264#endif
21265
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021266 return &pos;
21267 }
21268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021269 name = get_tv_string_chk(varp);
21270 if (name == NULL)
21271 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021272 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021273 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021274 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21275 {
21276 if (VIsual_active)
21277 return &VIsual;
21278 return &curwin->w_cursor;
21279 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021280 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021281 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021282 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021283 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21284 return NULL;
21285 return pp;
21286 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021287
21288#ifdef FEAT_VIRTUALEDIT
21289 pos.coladd = 0;
21290#endif
21291
Bram Moolenaar477933c2007-07-17 14:32:23 +000021292 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021293 {
21294 pos.col = 0;
21295 if (name[1] == '0') /* "w0": first visible line */
21296 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021297 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021298 pos.lnum = curwin->w_topline;
21299 return &pos;
21300 }
21301 else if (name[1] == '$') /* "w$": last visible line */
21302 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021303 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021304 pos.lnum = curwin->w_botline - 1;
21305 return &pos;
21306 }
21307 }
21308 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021310 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021311 {
21312 pos.lnum = curbuf->b_ml.ml_line_count;
21313 pos.col = 0;
21314 }
21315 else
21316 {
21317 pos.lnum = curwin->w_cursor.lnum;
21318 pos.col = (colnr_T)STRLEN(ml_get_curline());
21319 }
21320 return &pos;
21321 }
21322 return NULL;
21323}
21324
21325/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021326 * Convert list in "arg" into a position and optional file number.
21327 * When "fnump" is NULL there is no file number, only 3 items.
21328 * Note that the column is passed on as-is, the caller may want to decrement
21329 * it to use 1 for the first column.
21330 * Return FAIL when conversion is not possible, doesn't check the position for
21331 * validity.
21332 */
21333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021334list2fpos(
21335 typval_T *arg,
21336 pos_T *posp,
21337 int *fnump,
21338 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021339{
21340 list_T *l = arg->vval.v_list;
21341 long i = 0;
21342 long n;
21343
Bram Moolenaar493c1782014-05-28 14:34:46 +020021344 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21345 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021346 if (arg->v_type != VAR_LIST
21347 || l == NULL
21348 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021349 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021350 return FAIL;
21351
21352 if (fnump != NULL)
21353 {
21354 n = list_find_nr(l, i++, NULL); /* fnum */
21355 if (n < 0)
21356 return FAIL;
21357 if (n == 0)
21358 n = curbuf->b_fnum; /* current buffer */
21359 *fnump = n;
21360 }
21361
21362 n = list_find_nr(l, i++, NULL); /* lnum */
21363 if (n < 0)
21364 return FAIL;
21365 posp->lnum = n;
21366
21367 n = list_find_nr(l, i++, NULL); /* col */
21368 if (n < 0)
21369 return FAIL;
21370 posp->col = n;
21371
21372#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021373 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021374 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021375 posp->coladd = 0;
21376 else
21377 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021378#endif
21379
Bram Moolenaar493c1782014-05-28 14:34:46 +020021380 if (curswantp != NULL)
21381 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21382
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021383 return OK;
21384}
21385
21386/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 * Get the length of an environment variable name.
21388 * Advance "arg" to the first character after the name.
21389 * Return 0 for error.
21390 */
21391 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021392get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021393{
21394 char_u *p;
21395 int len;
21396
21397 for (p = *arg; vim_isIDc(*p); ++p)
21398 ;
21399 if (p == *arg) /* no name found */
21400 return 0;
21401
21402 len = (int)(p - *arg);
21403 *arg = p;
21404 return len;
21405}
21406
21407/*
21408 * Get the length of the name of a function or internal variable.
21409 * "arg" is advanced to the first non-white character after the name.
21410 * Return 0 if something is wrong.
21411 */
21412 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021413get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021414{
21415 char_u *p;
21416 int len;
21417
21418 /* Find the end of the name. */
21419 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021420 {
21421 if (*p == ':')
21422 {
21423 /* "s:" is start of "s:var", but "n:" is not and can be used in
21424 * slice "[n:]". Also "xx:" is not a namespace. */
21425 len = (int)(p - *arg);
21426 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21427 || len > 1)
21428 break;
21429 }
21430 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021431 if (p == *arg) /* no name found */
21432 return 0;
21433
21434 len = (int)(p - *arg);
21435 *arg = skipwhite(p);
21436
21437 return len;
21438}
21439
21440/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021441 * Get the length of the name of a variable or function.
21442 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021443 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021444 * Return -1 if curly braces expansion failed.
21445 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446 * If the name contains 'magic' {}'s, expand them and return the
21447 * expanded name in an allocated string via 'alias' - caller must free.
21448 */
21449 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021450get_name_len(
21451 char_u **arg,
21452 char_u **alias,
21453 int evaluate,
21454 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455{
21456 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457 char_u *p;
21458 char_u *expr_start;
21459 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021460
21461 *alias = NULL; /* default to no alias */
21462
21463 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21464 && (*arg)[2] == (int)KE_SNR)
21465 {
21466 /* hard coded <SNR>, already translated */
21467 *arg += 3;
21468 return get_id_len(arg) + 3;
21469 }
21470 len = eval_fname_script(*arg);
21471 if (len > 0)
21472 {
21473 /* literal "<SID>", "s:" or "<SNR>" */
21474 *arg += len;
21475 }
21476
Bram Moolenaar071d4272004-06-13 20:20:40 +000021477 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021478 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021479 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021480 p = find_name_end(*arg, &expr_start, &expr_end,
21481 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 if (expr_start != NULL)
21483 {
21484 char_u *temp_string;
21485
21486 if (!evaluate)
21487 {
21488 len += (int)(p - *arg);
21489 *arg = skipwhite(p);
21490 return len;
21491 }
21492
21493 /*
21494 * Include any <SID> etc in the expanded string:
21495 * Thus the -len here.
21496 */
21497 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21498 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021499 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500 *alias = temp_string;
21501 *arg = skipwhite(p);
21502 return (int)STRLEN(temp_string);
21503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021504
21505 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021506 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507 EMSG2(_(e_invexpr2), *arg);
21508
21509 return len;
21510}
21511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021512/*
21513 * Find the end of a variable or function name, taking care of magic braces.
21514 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21515 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021516 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021517 * Return a pointer to just after the name. Equal to "arg" if there is no
21518 * valid name.
21519 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021521find_name_end(
21522 char_u *arg,
21523 char_u **expr_start,
21524 char_u **expr_end,
21525 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021527 int mb_nest = 0;
21528 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021530 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021532 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021534 *expr_start = NULL;
21535 *expr_end = NULL;
21536 }
21537
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021538 /* Quick check for valid starting character. */
21539 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21540 return arg;
21541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021542 for (p = arg; *p != NUL
21543 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021544 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021545 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021546 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021547 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021548 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021549 if (*p == '\'')
21550 {
21551 /* skip over 'string' to avoid counting [ and ] inside it. */
21552 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21553 ;
21554 if (*p == NUL)
21555 break;
21556 }
21557 else if (*p == '"')
21558 {
21559 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21560 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21561 if (*p == '\\' && p[1] != NUL)
21562 ++p;
21563 if (*p == NUL)
21564 break;
21565 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021566 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21567 {
21568 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021569 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021570 len = (int)(p - arg);
21571 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021572 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021573 break;
21574 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021575
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021576 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021578 if (*p == '[')
21579 ++br_nest;
21580 else if (*p == ']')
21581 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021582 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021583
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021584 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021586 if (*p == '{')
21587 {
21588 mb_nest++;
21589 if (expr_start != NULL && *expr_start == NULL)
21590 *expr_start = p;
21591 }
21592 else if (*p == '}')
21593 {
21594 mb_nest--;
21595 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21596 *expr_end = p;
21597 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021598 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599 }
21600
21601 return p;
21602}
21603
21604/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021605 * Expands out the 'magic' {}'s in a variable/function name.
21606 * Note that this can call itself recursively, to deal with
21607 * constructs like foo{bar}{baz}{bam}
21608 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21609 * "in_start" ^
21610 * "expr_start" ^
21611 * "expr_end" ^
21612 * "in_end" ^
21613 *
21614 * Returns a new allocated string, which the caller must free.
21615 * Returns NULL for failure.
21616 */
21617 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021618make_expanded_name(
21619 char_u *in_start,
21620 char_u *expr_start,
21621 char_u *expr_end,
21622 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021623{
21624 char_u c1;
21625 char_u *retval = NULL;
21626 char_u *temp_result;
21627 char_u *nextcmd = NULL;
21628
21629 if (expr_end == NULL || in_end == NULL)
21630 return NULL;
21631 *expr_start = NUL;
21632 *expr_end = NUL;
21633 c1 = *in_end;
21634 *in_end = NUL;
21635
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021636 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021637 if (temp_result != NULL && nextcmd == NULL)
21638 {
21639 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21640 + (in_end - expr_end) + 1));
21641 if (retval != NULL)
21642 {
21643 STRCPY(retval, in_start);
21644 STRCAT(retval, temp_result);
21645 STRCAT(retval, expr_end + 1);
21646 }
21647 }
21648 vim_free(temp_result);
21649
21650 *in_end = c1; /* put char back for error messages */
21651 *expr_start = '{';
21652 *expr_end = '}';
21653
21654 if (retval != NULL)
21655 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021656 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021657 if (expr_start != NULL)
21658 {
21659 /* Further expansion! */
21660 temp_result = make_expanded_name(retval, expr_start,
21661 expr_end, temp_result);
21662 vim_free(retval);
21663 retval = temp_result;
21664 }
21665 }
21666
21667 return retval;
21668}
21669
21670/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021671 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021672 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021673 */
21674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021675eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021676{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021677 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21678}
21679
21680/*
21681 * Return TRUE if character "c" can be used as the first character in a
21682 * variable or function name (excluding '{' and '}').
21683 */
21684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021685eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021686{
21687 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021688}
21689
21690/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 * Set number v: variable to "val".
21692 */
21693 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021694set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021695{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021696 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697}
21698
21699/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021700 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701 */
21702 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021703get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021704{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021705 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021706}
21707
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021708/*
21709 * Get string v: variable value. Uses a static buffer, can only be used once.
21710 */
21711 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021712get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021713{
21714 return get_tv_string(&vimvars[idx].vv_tv);
21715}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021716
Bram Moolenaar071d4272004-06-13 20:20:40 +000021717/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021718 * Get List v: variable value. Caller must take care of reference count when
21719 * needed.
21720 */
21721 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021722get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021723{
21724 return vimvars[idx].vv_list;
21725}
21726
21727/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021728 * Set v:char to character "c".
21729 */
21730 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021731set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021732{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021733 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021734
21735#ifdef FEAT_MBYTE
21736 if (has_mbyte)
21737 buf[(*mb_char2bytes)(c, buf)] = NUL;
21738 else
21739#endif
21740 {
21741 buf[0] = c;
21742 buf[1] = NUL;
21743 }
21744 set_vim_var_string(VV_CHAR, buf, -1);
21745}
21746
21747/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021748 * Set v:count to "count" and v:count1 to "count1".
21749 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750 */
21751 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021752set_vcount(
21753 long count,
21754 long count1,
21755 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021756{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021757 if (set_prevcount)
21758 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021759 vimvars[VV_COUNT].vv_nr = count;
21760 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021761}
21762
21763/*
21764 * Set string v: variable to a copy of "val".
21765 */
21766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021767set_vim_var_string(
21768 int idx,
21769 char_u *val,
21770 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021771{
Bram Moolenaara542c682016-01-31 16:28:04 +010021772 clear_tv(&vimvars[idx].vv_di.di_tv);
21773 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021774 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021775 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021776 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021777 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021778 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021779 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021780}
21781
21782/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021783 * Set List v: variable to "val".
21784 */
21785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021786set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021787{
Bram Moolenaara542c682016-01-31 16:28:04 +010021788 clear_tv(&vimvars[idx].vv_di.di_tv);
21789 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021790 vimvars[idx].vv_list = val;
21791 if (val != NULL)
21792 ++val->lv_refcount;
21793}
21794
21795/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021796 * Set Dictionary v: variable to "val".
21797 */
21798 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021799set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021800{
21801 int todo;
21802 hashitem_T *hi;
21803
Bram Moolenaara542c682016-01-31 16:28:04 +010021804 clear_tv(&vimvars[idx].vv_di.di_tv);
21805 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021806 vimvars[idx].vv_dict = val;
21807 if (val != NULL)
21808 {
21809 ++val->dv_refcount;
21810
21811 /* Set readonly */
21812 todo = (int)val->dv_hashtab.ht_used;
21813 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21814 {
21815 if (HASHITEM_EMPTY(hi))
21816 continue;
21817 --todo;
21818 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21819 }
21820 }
21821}
21822
21823/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021824 * Set v:register if needed.
21825 */
21826 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021827set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021828{
21829 char_u regname;
21830
21831 if (c == 0 || c == ' ')
21832 regname = '"';
21833 else
21834 regname = c;
21835 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021836 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021837 set_vim_var_string(VV_REG, &regname, 1);
21838}
21839
21840/*
21841 * Get or set v:exception. If "oldval" == NULL, return the current value.
21842 * Otherwise, restore the value to "oldval" and return NULL.
21843 * Must always be called in pairs to save and restore v:exception! Does not
21844 * take care of memory allocations.
21845 */
21846 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021847v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021848{
21849 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021850 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021851
Bram Moolenaare9a41262005-01-15 22:18:47 +000021852 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021853 return NULL;
21854}
21855
21856/*
21857 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21858 * Otherwise, restore the value to "oldval" and return NULL.
21859 * Must always be called in pairs to save and restore v:throwpoint! Does not
21860 * take care of memory allocations.
21861 */
21862 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021863v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021864{
21865 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021866 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021867
Bram Moolenaare9a41262005-01-15 22:18:47 +000021868 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021869 return NULL;
21870}
21871
21872#if defined(FEAT_AUTOCMD) || defined(PROTO)
21873/*
21874 * Set v:cmdarg.
21875 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21876 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21877 * Must always be called in pairs!
21878 */
21879 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021880set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021881{
21882 char_u *oldval;
21883 char_u *newval;
21884 unsigned len;
21885
Bram Moolenaare9a41262005-01-15 22:18:47 +000021886 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021887 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021888 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021889 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021890 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021891 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021892 }
21893
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021894 if (eap->force_bin == FORCE_BIN)
21895 len = 6;
21896 else if (eap->force_bin == FORCE_NOBIN)
21897 len = 8;
21898 else
21899 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021900
21901 if (eap->read_edit)
21902 len += 7;
21903
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021904 if (eap->force_ff != 0)
21905 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21906# ifdef FEAT_MBYTE
21907 if (eap->force_enc != 0)
21908 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021909 if (eap->bad_char != 0)
21910 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021911# endif
21912
21913 newval = alloc(len + 1);
21914 if (newval == NULL)
21915 return NULL;
21916
21917 if (eap->force_bin == FORCE_BIN)
21918 sprintf((char *)newval, " ++bin");
21919 else if (eap->force_bin == FORCE_NOBIN)
21920 sprintf((char *)newval, " ++nobin");
21921 else
21922 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021923
21924 if (eap->read_edit)
21925 STRCAT(newval, " ++edit");
21926
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021927 if (eap->force_ff != 0)
21928 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21929 eap->cmd + eap->force_ff);
21930# ifdef FEAT_MBYTE
21931 if (eap->force_enc != 0)
21932 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21933 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021934 if (eap->bad_char == BAD_KEEP)
21935 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21936 else if (eap->bad_char == BAD_DROP)
21937 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21938 else if (eap->bad_char != 0)
21939 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021940# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021941 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021942 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021943}
21944#endif
21945
21946/*
21947 * Get the value of internal variable "name".
21948 * Return OK or FAIL.
21949 */
21950 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021951get_var_tv(
21952 char_u *name,
21953 int len, /* length of "name" */
21954 typval_T *rettv, /* NULL when only checking existence */
21955 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21956 int verbose, /* may give error message */
21957 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021958{
21959 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021960 typval_T *tv = NULL;
21961 typval_T atv;
21962 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021963 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021964
21965 /* truncate the name, so that we can use strcmp() */
21966 cc = name[len];
21967 name[len] = NUL;
21968
21969 /*
21970 * Check for "b:changedtick".
21971 */
21972 if (STRCMP(name, "b:changedtick") == 0)
21973 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021974 atv.v_type = VAR_NUMBER;
21975 atv.vval.v_number = curbuf->b_changedtick;
21976 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 }
21978
21979 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021980 * Check for user-defined variables.
21981 */
21982 else
21983 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021984 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021985 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021986 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021987 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021988 if (dip != NULL)
21989 *dip = v;
21990 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021991 }
21992
Bram Moolenaare9a41262005-01-15 22:18:47 +000021993 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021995 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021996 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021997 ret = FAIL;
21998 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021999 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022000 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001
22002 name[len] = cc;
22003
22004 return ret;
22005}
22006
22007/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022008 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
22009 * Also handle function call with Funcref variable: func(expr)
22010 * Can all be combined: dict.func(expr)[idx]['func'](expr)
22011 */
22012 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022013handle_subscript(
22014 char_u **arg,
22015 typval_T *rettv,
22016 int evaluate, /* do more than finding the end */
22017 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022018{
22019 int ret = OK;
22020 dict_T *selfdict = NULL;
22021 char_u *s;
22022 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000022023 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022024
22025 while (ret == OK
22026 && (**arg == '['
22027 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022028 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022029 && !vim_iswhite(*(*arg - 1)))
22030 {
22031 if (**arg == '(')
22032 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000022033 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022034 if (evaluate)
22035 {
22036 functv = *rettv;
22037 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022038
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022039 /* Invoke the function. Recursive! */
22040 s = functv.vval.v_string;
22041 }
22042 else
22043 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022044 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000022045 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
22046 &len, evaluate, selfdict);
22047
22048 /* Clear the funcref afterwards, so that deleting it while
22049 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010022050 if (evaluate)
22051 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000022052
22053 /* Stop the expression evaluation when immediately aborting on
22054 * error, or when an interrupt occurred or an exception was thrown
22055 * but not caught. */
22056 if (aborting())
22057 {
22058 if (ret == OK)
22059 clear_tv(rettv);
22060 ret = FAIL;
22061 }
22062 dict_unref(selfdict);
22063 selfdict = NULL;
22064 }
22065 else /* **arg == '[' || **arg == '.' */
22066 {
22067 dict_unref(selfdict);
22068 if (rettv->v_type == VAR_DICT)
22069 {
22070 selfdict = rettv->vval.v_dict;
22071 if (selfdict != NULL)
22072 ++selfdict->dv_refcount;
22073 }
22074 else
22075 selfdict = NULL;
22076 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
22077 {
22078 clear_tv(rettv);
22079 ret = FAIL;
22080 }
22081 }
22082 }
22083 dict_unref(selfdict);
22084 return ret;
22085}
22086
22087/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022088 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022089 * value).
22090 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010022091 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022092alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022093{
Bram Moolenaar33570922005-01-25 22:26:29 +000022094 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022095}
22096
22097/*
22098 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022099 * The string "s" must have been allocated, it is consumed.
22100 * Return NULL for out of memory, the variable otherwise.
22101 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022102 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022103alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022104{
Bram Moolenaar33570922005-01-25 22:26:29 +000022105 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022106
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022107 rettv = alloc_tv();
22108 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022109 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022110 rettv->v_type = VAR_STRING;
22111 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
22113 else
22114 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022115 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022116}
22117
22118/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000022121 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022122free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022123{
22124 if (varp != NULL)
22125 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 switch (varp->v_type)
22127 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022128 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022129 func_unref(varp->vval.v_string);
22130 /*FALLTHROUGH*/
22131 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022132 vim_free(varp->vval.v_string);
22133 break;
22134 case VAR_LIST:
22135 list_unref(varp->vval.v_list);
22136 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022137 case VAR_DICT:
22138 dict_unref(varp->vval.v_dict);
22139 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022140 case VAR_JOB:
22141#ifdef FEAT_JOB
22142 job_unref(varp->vval.v_job);
22143 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022144#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022145 case VAR_CHANNEL:
22146#ifdef FEAT_CHANNEL
22147 channel_unref(varp->vval.v_channel);
22148 break;
22149#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022150 case VAR_NUMBER:
22151 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022152 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022153 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022154 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022155 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022156 vim_free(varp);
22157 }
22158}
22159
22160/*
22161 * Free the memory for a variable value and set the value to NULL or 0.
22162 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022163 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022164clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165{
22166 if (varp != NULL)
22167 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022168 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022170 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022171 func_unref(varp->vval.v_string);
22172 /*FALLTHROUGH*/
22173 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022174 vim_free(varp->vval.v_string);
22175 varp->vval.v_string = NULL;
22176 break;
22177 case VAR_LIST:
22178 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022179 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022180 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022181 case VAR_DICT:
22182 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022183 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022184 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022185 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022186 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022187 varp->vval.v_number = 0;
22188 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022189 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022190#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022191 varp->vval.v_float = 0.0;
22192 break;
22193#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022194 case VAR_JOB:
22195#ifdef FEAT_JOB
22196 job_unref(varp->vval.v_job);
22197 varp->vval.v_job = NULL;
22198#endif
22199 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022200 case VAR_CHANNEL:
22201#ifdef FEAT_CHANNEL
22202 channel_unref(varp->vval.v_channel);
22203 varp->vval.v_channel = NULL;
22204#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022205 case VAR_UNKNOWN:
22206 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022208 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 }
22210}
22211
22212/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022213 * Set the value of a variable to NULL without freeing items.
22214 */
22215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022216init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022217{
22218 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022219 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022220}
22221
22222/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022223 * Get the number value of a variable.
22224 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022225 * For incompatible types, return 0.
22226 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22227 * caller of incompatible types: it sets *denote to TRUE if "denote"
22228 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 */
22230 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022231get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022233 int error = FALSE;
22234
22235 return get_tv_number_chk(varp, &error); /* return 0L on error */
22236}
22237
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022238 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022239get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022240{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022241 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022242
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022243 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022244 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022245 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022246 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022247 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022248#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022249 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022250 break;
22251#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022252 case VAR_FUNC:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022253 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022254 break;
22255 case VAR_STRING:
22256 if (varp->vval.v_string != NULL)
22257 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022258 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022259 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022260 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022261 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022262 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022263 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022264 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022265 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022266 case VAR_SPECIAL:
22267 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22268 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022269 case VAR_JOB:
22270#ifdef FEAT_JOB
22271 EMSG(_("E910: Using a Job as a Number"));
22272 break;
22273#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022274 case VAR_CHANNEL:
22275#ifdef FEAT_CHANNEL
22276 EMSG(_("E913: Using a Channel as a Number"));
22277 break;
22278#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022279 case VAR_UNKNOWN:
22280 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022281 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022283 if (denote == NULL) /* useful for values that must be unsigned */
22284 n = -1;
22285 else
22286 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022287 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288}
22289
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022290#ifdef FEAT_FLOAT
22291 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022292get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022293{
22294 switch (varp->v_type)
22295 {
22296 case VAR_NUMBER:
22297 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022298 case VAR_FLOAT:
22299 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022300 case VAR_FUNC:
22301 EMSG(_("E891: Using a Funcref as a Float"));
22302 break;
22303 case VAR_STRING:
22304 EMSG(_("E892: Using a String as a Float"));
22305 break;
22306 case VAR_LIST:
22307 EMSG(_("E893: Using a List as a Float"));
22308 break;
22309 case VAR_DICT:
22310 EMSG(_("E894: Using a Dictionary as a Float"));
22311 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022312 case VAR_SPECIAL:
22313 EMSG(_("E907: Using a special value as a Float"));
22314 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022315 case VAR_JOB:
22316# ifdef FEAT_JOB
22317 EMSG(_("E911: Using a Job as a Float"));
22318 break;
22319# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022320 case VAR_CHANNEL:
22321# ifdef FEAT_CHANNEL
22322 EMSG(_("E914: Using a Channel as a Float"));
22323 break;
22324# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022325 case VAR_UNKNOWN:
22326 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022327 break;
22328 }
22329 return 0;
22330}
22331#endif
22332
Bram Moolenaar071d4272004-06-13 20:20:40 +000022333/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022334 * Get the lnum from the first argument.
22335 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022336 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 */
22338 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022339get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022340{
Bram Moolenaar33570922005-01-25 22:26:29 +000022341 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 linenr_T lnum;
22343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022344 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022345 if (lnum == 0) /* no valid number, try using line() */
22346 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022347 rettv.v_type = VAR_NUMBER;
22348 f_line(argvars, &rettv);
22349 lnum = rettv.vval.v_number;
22350 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 }
22352 return lnum;
22353}
22354
22355/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022356 * Get the lnum from the first argument.
22357 * Also accepts "$", then "buf" is used.
22358 * Returns 0 on error.
22359 */
22360 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022361get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022362{
22363 if (argvars[0].v_type == VAR_STRING
22364 && argvars[0].vval.v_string != NULL
22365 && argvars[0].vval.v_string[0] == '$'
22366 && buf != NULL)
22367 return buf->b_ml.ml_line_count;
22368 return get_tv_number_chk(&argvars[0], NULL);
22369}
22370
22371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 * Get the string value of a variable.
22373 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022374 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22375 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022376 * If the String variable has never been set, return an empty string.
22377 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022378 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22379 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 */
22381 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022382get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383{
22384 static char_u mybuf[NUMBUFLEN];
22385
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022386 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387}
22388
22389 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022390get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022392 char_u *res = get_tv_string_buf_chk(varp, buf);
22393
22394 return res != NULL ? res : (char_u *)"";
22395}
22396
Bram Moolenaar7d647822014-04-05 21:28:56 +020022397/*
22398 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22399 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022400 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022401get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022402{
22403 static char_u mybuf[NUMBUFLEN];
22404
22405 return get_tv_string_buf_chk(varp, mybuf);
22406}
22407
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022408 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022409get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022410{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022411 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022413 case VAR_NUMBER:
22414 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22415 return buf;
22416 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022417 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022418 break;
22419 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022420 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022421 break;
22422 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022423 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022424 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022425 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022426#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022427 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022428 break;
22429#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022430 case VAR_STRING:
22431 if (varp->vval.v_string != NULL)
22432 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022433 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022434 case VAR_SPECIAL:
22435 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22436 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022437 case VAR_JOB:
22438#ifdef FEAT_JOB
22439 {
22440 job_T *job = varp->vval.v_job;
22441 char *status = job->jv_status == JOB_FAILED ? "fail"
22442 : job->jv_status == JOB_ENDED ? "dead"
22443 : "run";
22444# ifdef UNIX
22445 vim_snprintf((char *)buf, NUMBUFLEN,
22446 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022447# elif defined(WIN32)
22448 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022449 "process %ld %s",
22450 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022451 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022452# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022453 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022454 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22455# endif
22456 return buf;
22457 }
22458#endif
22459 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022460 case VAR_CHANNEL:
22461#ifdef FEAT_CHANNEL
22462 {
22463 channel_T *channel = varp->vval.v_channel;
22464 char *status = channel_status(channel);
22465
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022466 if (channel == NULL)
22467 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22468 else
22469 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022470 "channel %d %s", channel->ch_id, status);
22471 return buf;
22472 }
22473#endif
22474 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022475 case VAR_UNKNOWN:
22476 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022477 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022479 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022480}
22481
22482/*
22483 * Find variable "name" in the list of variables.
22484 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022485 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022486 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022487 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022490find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022491{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494
Bram Moolenaara7043832005-01-21 11:56:39 +000022495 ht = find_var_ht(name, &varname);
22496 if (htp != NULL)
22497 *htp = ht;
22498 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022499 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022500 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022501}
22502
22503/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022504 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022505 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022507 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022508find_var_in_ht(
22509 hashtab_T *ht,
22510 int htname,
22511 char_u *varname,
22512 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022513{
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 hashitem_T *hi;
22515
22516 if (*varname == NUL)
22517 {
22518 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022519 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022520 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022521 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022522 case 'g': return &globvars_var;
22523 case 'v': return &vimvars_var;
22524 case 'b': return &curbuf->b_bufvar;
22525 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022526#ifdef FEAT_WINDOWS
22527 case 't': return &curtab->tp_winvar;
22528#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022529 case 'l': return current_funccal == NULL
22530 ? NULL : &current_funccal->l_vars_var;
22531 case 'a': return current_funccal == NULL
22532 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022533 }
22534 return NULL;
22535 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022536
22537 hi = hash_find(ht, varname);
22538 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022539 {
22540 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022541 * worked find the variable again. Don't auto-load a script if it was
22542 * loaded already, otherwise it would be loaded every time when
22543 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022544 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022545 {
22546 /* Note: script_autoload() may make "hi" invalid. It must either
22547 * be obtained again or not used. */
22548 if (!script_autoload(varname, FALSE) || aborting())
22549 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022550 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022551 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022552 if (HASHITEM_EMPTY(hi))
22553 return NULL;
22554 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022555 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022556}
22557
22558/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022559 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022560 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022561 * Set "varname" to the start of name without ':'.
22562 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022564find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022565{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022566 hashitem_T *hi;
22567
Bram Moolenaar73627d02015-08-11 15:46:09 +020022568 if (name[0] == NUL)
22569 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022570 if (name[1] != ':')
22571 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022572 /* The name must not start with a colon or #. */
22573 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574 return NULL;
22575 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022576
22577 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022578 hi = hash_find(&compat_hashtab, name);
22579 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022580 return &compat_hashtab;
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022583 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022584 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022585 }
22586 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022587 if (*name == 'g') /* global variable */
22588 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022589 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22590 */
22591 if (vim_strchr(name + 2, ':') != NULL
22592 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022593 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022594 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022595 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022596 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022597 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022598#ifdef FEAT_WINDOWS
22599 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022600 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022601#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 if (*name == 'v') /* v: variable */
22603 return &vimvarht;
22604 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022605 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022606 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022607 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022608 if (*name == 's' /* script variable */
22609 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22610 return &SCRIPT_VARS(current_SID);
22611 return NULL;
22612}
22613
22614/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022615 * Get function call environment based on bactrace debug level
22616 */
22617 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022618get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022619{
22620 int i;
22621 funccall_T *funccal;
22622 funccall_T *temp_funccal;
22623
22624 funccal = current_funccal;
22625 if (debug_backtrace_level > 0)
22626 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022627 for (i = 0; i < debug_backtrace_level; i++)
22628 {
22629 temp_funccal = funccal->caller;
22630 if (temp_funccal)
22631 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022632 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022633 /* backtrace level overflow. reset to max */
22634 debug_backtrace_level = i;
22635 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022636 }
22637 return funccal;
22638}
22639
22640/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022642 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022643 * Returns NULL when it doesn't exist.
22644 */
22645 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022646get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647{
Bram Moolenaar33570922005-01-25 22:26:29 +000022648 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022649
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022650 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022651 if (v == NULL)
22652 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022653 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022654}
22655
22656/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022657 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022658 * sourcing this script and when executing functions defined in the script.
22659 */
22660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022661new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662{
Bram Moolenaara7043832005-01-21 11:56:39 +000022663 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 hashtab_T *ht;
22665 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022666
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22668 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022669 /* Re-allocating ga_data means that an ht_array pointing to
22670 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022671 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022672 for (i = 1; i <= ga_scripts.ga_len; ++i)
22673 {
22674 ht = &SCRIPT_VARS(i);
22675 if (ht->ht_mask == HT_INIT_SIZE - 1)
22676 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022677 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022678 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022679 }
22680
Bram Moolenaar071d4272004-06-13 20:20:40 +000022681 while (ga_scripts.ga_len < id)
22682 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022683 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022684 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022685 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 }
22688 }
22689}
22690
22691/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022692 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22693 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694 */
22695 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022696init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022697{
Bram Moolenaar33570922005-01-25 22:26:29 +000022698 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022699 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022700 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022701 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022702 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 dict_var->di_tv.vval.v_dict = dict;
22704 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022705 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022706 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22707 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022708}
22709
22710/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022711 * Unreference a dictionary initialized by init_var_dict().
22712 */
22713 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022714unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022715{
22716 /* Now the dict needs to be freed if no one else is using it, go back to
22717 * normal reference counting. */
22718 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22719 dict_unref(dict);
22720}
22721
22722/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022723 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022724 * Frees all allocated variables and the value they contain.
22725 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022726 */
22727 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022728vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022729{
22730 vars_clear_ext(ht, TRUE);
22731}
22732
22733/*
22734 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22735 */
22736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022737vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022738{
Bram Moolenaara7043832005-01-21 11:56:39 +000022739 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022740 hashitem_T *hi;
22741 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022744 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022745 for (hi = ht->ht_array; todo > 0; ++hi)
22746 {
22747 if (!HASHITEM_EMPTY(hi))
22748 {
22749 --todo;
22750
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022752 * ht_array might change then. hash_clear() takes care of it
22753 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022754 v = HI2DI(hi);
22755 if (free_val)
22756 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022757 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022758 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022759 }
22760 }
22761 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022762 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022763}
22764
Bram Moolenaara7043832005-01-21 11:56:39 +000022765/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 * Delete a variable from hashtab "ht" at item "hi".
22767 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022770delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022771{
Bram Moolenaar33570922005-01-25 22:26:29 +000022772 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022773
22774 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022775 clear_tv(&di->di_tv);
22776 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022777}
22778
22779/*
22780 * List the value of one internal variable.
22781 */
22782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022783list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022784{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785 char_u *tofree;
22786 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022787 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022788
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022789 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022790 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022791 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022792 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022793}
22794
Bram Moolenaar071d4272004-06-13 20:20:40 +000022795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022796list_one_var_a(
22797 char_u *prefix,
22798 char_u *name,
22799 int type,
22800 char_u *string,
22801 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022802{
Bram Moolenaar31859182007-08-14 20:41:13 +000022803 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22804 msg_start();
22805 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022806 if (name != NULL) /* "a:" vars don't have a name stored */
22807 msg_puts(name);
22808 msg_putchar(' ');
22809 msg_advance(22);
22810 if (type == VAR_NUMBER)
22811 msg_putchar('#');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022812 else if (type == VAR_FUNC)
22813 msg_putchar('*');
22814 else if (type == VAR_LIST)
22815 {
22816 msg_putchar('[');
22817 if (*string == '[')
22818 ++string;
22819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022820 else if (type == VAR_DICT)
22821 {
22822 msg_putchar('{');
22823 if (*string == '{')
22824 ++string;
22825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 else
22827 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022828
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022830
22831 if (type == VAR_FUNC)
22832 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022833 if (*first)
22834 {
22835 msg_clr_eos();
22836 *first = FALSE;
22837 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022838}
22839
22840/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022841 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022842 * If the variable already exists, the value is updated.
22843 * Otherwise the variable is created.
22844 */
22845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022846set_var(
22847 char_u *name,
22848 typval_T *tv,
22849 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022850{
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022852 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022853 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022854
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022855 ht = find_var_ht(name, &varname);
22856 if (ht == NULL || *varname == NUL)
22857 {
22858 EMSG2(_(e_illvar), name);
22859 return;
22860 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022861 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022862
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022863 if (tv->v_type == VAR_FUNC && var_check_func_name(name, v == NULL))
22864 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022865
Bram Moolenaar33570922005-01-25 22:26:29 +000022866 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022867 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022868 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022869 if (var_check_ro(v->di_flags, name, FALSE)
22870 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022871 return;
22872 if (v->di_tv.v_type != tv->v_type
22873 && !((v->di_tv.v_type == VAR_STRING
22874 || v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022875 && (tv->v_type == VAR_STRING
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022876 || tv->v_type == VAR_NUMBER))
22877#ifdef FEAT_FLOAT
22878 && !((v->di_tv.v_type == VAR_NUMBER
22879 || v->di_tv.v_type == VAR_FLOAT)
22880 && (tv->v_type == VAR_NUMBER
22881 || tv->v_type == VAR_FLOAT))
22882#endif
22883 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022884 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +000022885 EMSG2(_("E706: Variable type mismatch for: %s"), name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022886 return;
22887 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022888
22889 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022890 * Handle setting internal v: variables separately where needed to
22891 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022892 */
22893 if (ht == &vimvarht)
22894 {
22895 if (v->di_tv.v_type == VAR_STRING)
22896 {
22897 vim_free(v->di_tv.vval.v_string);
22898 if (copy || tv->v_type != VAR_STRING)
22899 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22900 else
22901 {
22902 /* Take over the string to avoid an extra alloc/free. */
22903 v->di_tv.vval.v_string = tv->vval.v_string;
22904 tv->vval.v_string = NULL;
22905 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022906 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022907 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022908 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022909 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022910 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022911 if (STRCMP(varname, "searchforward") == 0)
22912 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022913#ifdef FEAT_SEARCH_EXTRA
22914 else if (STRCMP(varname, "hlsearch") == 0)
22915 {
22916 no_hlsearch = !v->di_tv.vval.v_number;
22917 redraw_all_later(SOME_VALID);
22918 }
22919#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022920 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022921 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022922 else if (v->di_tv.v_type != tv->v_type)
22923 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022924 }
22925
22926 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927 }
22928 else /* add a new variable */
22929 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022930 /* Can't add "v:" variable. */
22931 if (ht == &vimvarht)
22932 {
22933 EMSG2(_(e_illvar), name);
22934 return;
22935 }
22936
Bram Moolenaar92124a32005-06-17 22:03:40 +000022937 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022938 if (!valid_varname(varname))
22939 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022941 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22942 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022943 if (v == NULL)
22944 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022945 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022946 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022947 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022948 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022949 return;
22950 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022951 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022952 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022953
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022954 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022955 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022956 else
22957 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022958 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022959 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022960 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022961 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962}
22963
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022964/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022965 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022966 * Also give an error message.
22967 */
22968 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022969var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022970{
22971 if (flags & DI_FLAGS_RO)
22972 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022973 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022974 return TRUE;
22975 }
22976 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22977 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022978 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022979 return TRUE;
22980 }
22981 return FALSE;
22982}
22983
22984/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022985 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22986 * Also give an error message.
22987 */
22988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022989var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022990{
22991 if (flags & DI_FLAGS_FIX)
22992 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022993 EMSG2(_("E795: Cannot delete variable %s"),
22994 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022995 return TRUE;
22996 }
22997 return FALSE;
22998}
22999
23000/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023001 * Check if a funcref is assigned to a valid variable name.
23002 * Return TRUE and give an error if not.
23003 */
23004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023005var_check_func_name(
23006 char_u *name, /* points to start of variable name */
23007 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023008{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020023009 /* Allow for w: b: s: and t:. */
23010 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023011 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
23012 ? name[2] : name[0]))
23013 {
23014 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
23015 name);
23016 return TRUE;
23017 }
23018 /* Don't allow hiding a function. When "v" is not NULL we might be
23019 * assigning another function to the same var, the type is checked
23020 * below. */
23021 if (new_var && function_exists(name))
23022 {
23023 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
23024 name);
23025 return TRUE;
23026 }
23027 return FALSE;
23028}
23029
23030/*
23031 * Check if a variable name is valid.
23032 * Return FALSE and give an error if not.
23033 */
23034 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023035valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020023036{
23037 char_u *p;
23038
23039 for (p = varname; *p != NUL; ++p)
23040 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
23041 && *p != AUTOLOAD_CHAR)
23042 {
23043 EMSG2(_(e_illvar), varname);
23044 return FALSE;
23045 }
23046 return TRUE;
23047}
23048
23049/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023050 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020023051 * Also give an error message, using "name" or _("name") when use_gettext is
23052 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023053 */
23054 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023055tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023056{
23057 if (lock & VAR_LOCKED)
23058 {
23059 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023060 name == NULL ? (char_u *)_("Unknown")
23061 : use_gettext ? (char_u *)_(name)
23062 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023063 return TRUE;
23064 }
23065 if (lock & VAR_FIXED)
23066 {
23067 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020023068 name == NULL ? (char_u *)_("Unknown")
23069 : use_gettext ? (char_u *)_(name)
23070 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023071 return TRUE;
23072 }
23073 return FALSE;
23074}
23075
23076/*
Bram Moolenaar33570922005-01-25 22:26:29 +000023077 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023078 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023079 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000023080 * It is OK for "from" and "to" to point to the same item. This is used to
23081 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023082 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010023083 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023084copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023085{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023086 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023087 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023088 switch (from->v_type)
23089 {
23090 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023091 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023092 to->vval.v_number = from->vval.v_number;
23093 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023094 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023095#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023096 to->vval.v_float = from->vval.v_float;
23097 break;
23098#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010023099 case VAR_JOB:
Bram Moolenaarcb4b0122016-02-07 14:53:21 +010023100#ifdef FEAT_JOB
Bram Moolenaar835dc632016-02-07 14:27:38 +010023101 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010023102 if (to->vval.v_job != NULL)
23103 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010023104 break;
23105#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010023106 case VAR_CHANNEL:
23107#ifdef FEAT_CHANNEL
23108 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010023109 if (to->vval.v_channel != NULL)
23110 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010023111 break;
23112#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023113 case VAR_STRING:
23114 case VAR_FUNC:
23115 if (from->vval.v_string == NULL)
23116 to->vval.v_string = NULL;
23117 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023118 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023119 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023120 if (from->v_type == VAR_FUNC)
23121 func_ref(to->vval.v_string);
23122 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023123 break;
23124 case VAR_LIST:
23125 if (from->vval.v_list == NULL)
23126 to->vval.v_list = NULL;
23127 else
23128 {
23129 to->vval.v_list = from->vval.v_list;
23130 ++to->vval.v_list->lv_refcount;
23131 }
23132 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023133 case VAR_DICT:
23134 if (from->vval.v_dict == NULL)
23135 to->vval.v_dict = NULL;
23136 else
23137 {
23138 to->vval.v_dict = from->vval.v_dict;
23139 ++to->vval.v_dict->dv_refcount;
23140 }
23141 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023142 case VAR_UNKNOWN:
23143 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023144 break;
23145 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023146}
23147
23148/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023149 * Make a copy of an item.
23150 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023151 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23152 * reference to an already copied list/dict can be used.
23153 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023154 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023155 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023156item_copy(
23157 typval_T *from,
23158 typval_T *to,
23159 int deep,
23160 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023161{
23162 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023163 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023164
Bram Moolenaar33570922005-01-25 22:26:29 +000023165 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023166 {
23167 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023168 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023169 }
23170 ++recurse;
23171
23172 switch (from->v_type)
23173 {
23174 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023175 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023176 case VAR_STRING:
23177 case VAR_FUNC:
Bram Moolenaar15550002016-01-31 18:45:24 +010023178 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023179 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023180 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023181 copy_tv(from, to);
23182 break;
23183 case VAR_LIST:
23184 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023185 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023186 if (from->vval.v_list == NULL)
23187 to->vval.v_list = NULL;
23188 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23189 {
23190 /* use the copy made earlier */
23191 to->vval.v_list = from->vval.v_list->lv_copylist;
23192 ++to->vval.v_list->lv_refcount;
23193 }
23194 else
23195 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23196 if (to->vval.v_list == NULL)
23197 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023198 break;
23199 case VAR_DICT:
23200 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023201 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023202 if (from->vval.v_dict == NULL)
23203 to->vval.v_dict = NULL;
23204 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23205 {
23206 /* use the copy made earlier */
23207 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23208 ++to->vval.v_dict->dv_refcount;
23209 }
23210 else
23211 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23212 if (to->vval.v_dict == NULL)
23213 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023214 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023215 case VAR_UNKNOWN:
23216 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023217 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023218 }
23219 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023220 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023221}
23222
23223/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 * ":echo expr1 ..." print each argument separated with a space, add a
23225 * newline at the end.
23226 * ":echon expr1 ..." print each argument plain.
23227 */
23228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023229ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023230{
23231 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023232 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023233 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023234 char_u *p;
23235 int needclr = TRUE;
23236 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023237 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238
23239 if (eap->skip)
23240 ++emsg_skip;
23241 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23242 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023243 /* If eval1() causes an error message the text from the command may
23244 * still need to be cleared. E.g., "echo 22,44". */
23245 need_clr_eos = needclr;
23246
Bram Moolenaar071d4272004-06-13 20:20:40 +000023247 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023248 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023249 {
23250 /*
23251 * Report the invalid expression unless the expression evaluation
23252 * has been cancelled due to an aborting error, an interrupt, or an
23253 * exception.
23254 */
23255 if (!aborting())
23256 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023257 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 break;
23259 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023260 need_clr_eos = FALSE;
23261
Bram Moolenaar071d4272004-06-13 20:20:40 +000023262 if (!eap->skip)
23263 {
23264 if (atstart)
23265 {
23266 atstart = FALSE;
23267 /* Call msg_start() after eval1(), evaluating the expression
23268 * may cause a message to appear. */
23269 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023270 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023271 /* Mark the saved text as finishing the line, so that what
23272 * follows is displayed on a new line when scrolling back
23273 * at the more prompt. */
23274 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 }
23278 else if (eap->cmdidx == CMD_echo)
23279 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023280 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023281 if (p != NULL)
23282 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023283 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023284 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023286 if (*p != TAB && needclr)
23287 {
23288 /* remove any text still there from the command */
23289 msg_clr_eos();
23290 needclr = FALSE;
23291 }
23292 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023293 }
23294 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023295 {
23296#ifdef FEAT_MBYTE
23297 if (has_mbyte)
23298 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023299 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023300
23301 (void)msg_outtrans_len_attr(p, i, echo_attr);
23302 p += i - 1;
23303 }
23304 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023306 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23307 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023309 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023310 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023311 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023312 arg = skipwhite(arg);
23313 }
23314 eap->nextcmd = check_nextcmd(arg);
23315
23316 if (eap->skip)
23317 --emsg_skip;
23318 else
23319 {
23320 /* remove text that may still be there from the command */
23321 if (needclr)
23322 msg_clr_eos();
23323 if (eap->cmdidx == CMD_echo)
23324 msg_end();
23325 }
23326}
23327
23328/*
23329 * ":echohl {name}".
23330 */
23331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023332ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023333{
23334 int id;
23335
23336 id = syn_name2id(eap->arg);
23337 if (id == 0)
23338 echo_attr = 0;
23339 else
23340 echo_attr = syn_id2attr(id);
23341}
23342
23343/*
23344 * ":execute expr1 ..." execute the result of an expression.
23345 * ":echomsg expr1 ..." Print a message
23346 * ":echoerr expr1 ..." Print an error
23347 * Each gets spaces around each argument and a newline at the end for
23348 * echo commands
23349 */
23350 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023351ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352{
23353 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023354 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023355 int ret = OK;
23356 char_u *p;
23357 garray_T ga;
23358 int len;
23359 int save_did_emsg;
23360
23361 ga_init2(&ga, 1, 80);
23362
23363 if (eap->skip)
23364 ++emsg_skip;
23365 while (*arg != NUL && *arg != '|' && *arg != '\n')
23366 {
23367 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023368 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 {
23370 /*
23371 * Report the invalid expression unless the expression evaluation
23372 * has been cancelled due to an aborting error, an interrupt, or an
23373 * exception.
23374 */
23375 if (!aborting())
23376 EMSG2(_(e_invexpr2), p);
23377 ret = FAIL;
23378 break;
23379 }
23380
23381 if (!eap->skip)
23382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023383 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023384 len = (int)STRLEN(p);
23385 if (ga_grow(&ga, len + 2) == FAIL)
23386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023387 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023388 ret = FAIL;
23389 break;
23390 }
23391 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023392 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023393 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023394 ga.ga_len += len;
23395 }
23396
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023397 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023398 arg = skipwhite(arg);
23399 }
23400
23401 if (ret != FAIL && ga.ga_data != NULL)
23402 {
23403 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023404 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023405 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023406 out_flush();
23407 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023408 else if (eap->cmdidx == CMD_echoerr)
23409 {
23410 /* We don't want to abort following commands, restore did_emsg. */
23411 save_did_emsg = did_emsg;
23412 EMSG((char_u *)ga.ga_data);
23413 if (!force_abort)
23414 did_emsg = save_did_emsg;
23415 }
23416 else if (eap->cmdidx == CMD_execute)
23417 do_cmdline((char_u *)ga.ga_data,
23418 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23419 }
23420
23421 ga_clear(&ga);
23422
23423 if (eap->skip)
23424 --emsg_skip;
23425
23426 eap->nextcmd = check_nextcmd(arg);
23427}
23428
23429/*
23430 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23431 * "arg" points to the "&" or '+' when called, to "option" when returning.
23432 * Returns NULL when no option name found. Otherwise pointer to the char
23433 * after the option name.
23434 */
23435 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023436find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023437{
23438 char_u *p = *arg;
23439
23440 ++p;
23441 if (*p == 'g' && p[1] == ':')
23442 {
23443 *opt_flags = OPT_GLOBAL;
23444 p += 2;
23445 }
23446 else if (*p == 'l' && p[1] == ':')
23447 {
23448 *opt_flags = OPT_LOCAL;
23449 p += 2;
23450 }
23451 else
23452 *opt_flags = 0;
23453
23454 if (!ASCII_ISALPHA(*p))
23455 return NULL;
23456 *arg = p;
23457
23458 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23459 p += 4; /* termcap option */
23460 else
23461 while (ASCII_ISALPHA(*p))
23462 ++p;
23463 return p;
23464}
23465
23466/*
23467 * ":function"
23468 */
23469 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023470ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023471{
23472 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023473 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023474 int j;
23475 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023477 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 char_u *name = NULL;
23479 char_u *p;
23480 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023481 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 garray_T newargs;
23483 garray_T newlines;
23484 int varargs = FALSE;
23485 int mustend = FALSE;
23486 int flags = 0;
23487 ufunc_T *fp;
23488 int indent;
23489 int nesting;
23490 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023491 dictitem_T *v;
23492 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023493 static int func_nr = 0; /* number for nameless function */
23494 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023495 hashtab_T *ht;
23496 int todo;
23497 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023498 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023499
23500 /*
23501 * ":function" without argument: list functions.
23502 */
23503 if (ends_excmd(*eap->arg))
23504 {
23505 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023506 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023507 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023508 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023509 {
23510 if (!HASHITEM_EMPTY(hi))
23511 {
23512 --todo;
23513 fp = HI2UF(hi);
23514 if (!isdigit(*fp->uf_name))
23515 list_func_head(fp, FALSE);
23516 }
23517 }
23518 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023519 eap->nextcmd = check_nextcmd(eap->arg);
23520 return;
23521 }
23522
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023523 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023524 * ":function /pat": list functions matching pattern.
23525 */
23526 if (*eap->arg == '/')
23527 {
23528 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23529 if (!eap->skip)
23530 {
23531 regmatch_T regmatch;
23532
23533 c = *p;
23534 *p = NUL;
23535 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23536 *p = c;
23537 if (regmatch.regprog != NULL)
23538 {
23539 regmatch.rm_ic = p_ic;
23540
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023541 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023542 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23543 {
23544 if (!HASHITEM_EMPTY(hi))
23545 {
23546 --todo;
23547 fp = HI2UF(hi);
23548 if (!isdigit(*fp->uf_name)
23549 && vim_regexec(&regmatch, fp->uf_name, 0))
23550 list_func_head(fp, FALSE);
23551 }
23552 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023553 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023554 }
23555 }
23556 if (*p == '/')
23557 ++p;
23558 eap->nextcmd = check_nextcmd(p);
23559 return;
23560 }
23561
23562 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023563 * Get the function name. There are these situations:
23564 * func normal function name
23565 * "name" == func, "fudi.fd_dict" == NULL
23566 * dict.func new dictionary entry
23567 * "name" == NULL, "fudi.fd_dict" set,
23568 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23569 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023570 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023571 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23572 * dict.func existing dict entry that's not a Funcref
23573 * "name" == NULL, "fudi.fd_dict" set,
23574 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023575 * s:func script-local function name
23576 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023579 name = trans_function_name(&p, eap->skip, 0, &fudi);
23580 paren = (vim_strchr(p, '(') != NULL);
23581 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023582 {
23583 /*
23584 * Return on an invalid expression in braces, unless the expression
23585 * evaluation has been cancelled due to an aborting error, an
23586 * interrupt, or an exception.
23587 */
23588 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023589 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023590 if (!eap->skip && fudi.fd_newkey != NULL)
23591 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023592 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023593 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023594 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023595 else
23596 eap->skip = TRUE;
23597 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023598
Bram Moolenaar071d4272004-06-13 20:20:40 +000023599 /* An error in a function call during evaluation of an expression in magic
23600 * braces should not cause the function not to be defined. */
23601 saved_did_emsg = did_emsg;
23602 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023603
23604 /*
23605 * ":function func" with only function name: list function.
23606 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023607 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023608 {
23609 if (!ends_excmd(*skipwhite(p)))
23610 {
23611 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023612 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023613 }
23614 eap->nextcmd = check_nextcmd(p);
23615 if (eap->nextcmd != NULL)
23616 *p = NUL;
23617 if (!eap->skip && !got_int)
23618 {
23619 fp = find_func(name);
23620 if (fp != NULL)
23621 {
23622 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023623 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023625 if (FUNCLINE(fp, j) == NULL)
23626 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 msg_putchar('\n');
23628 msg_outnum((long)(j + 1));
23629 if (j < 9)
23630 msg_putchar(' ');
23631 if (j < 99)
23632 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023633 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 out_flush(); /* show a line at a time */
23635 ui_breakcheck();
23636 }
23637 if (!got_int)
23638 {
23639 msg_putchar('\n');
23640 msg_puts((char_u *)" endfunction");
23641 }
23642 }
23643 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023644 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023646 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023647 }
23648
23649 /*
23650 * ":function name(arg1, arg2)" Define function.
23651 */
23652 p = skipwhite(p);
23653 if (*p != '(')
23654 {
23655 if (!eap->skip)
23656 {
23657 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023658 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 }
23660 /* attempt to continue by skipping some text */
23661 if (vim_strchr(p, '(') != NULL)
23662 p = vim_strchr(p, '(');
23663 }
23664 p = skipwhite(p + 1);
23665
23666 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23667 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23668
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023669 if (!eap->skip)
23670 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023671 /* Check the name of the function. Unless it's a dictionary function
23672 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023673 if (name != NULL)
23674 arg = name;
23675 else
23676 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023677 if (arg != NULL && (fudi.fd_di == NULL
23678 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023679 {
23680 if (*arg == K_SPECIAL)
23681 j = 3;
23682 else
23683 j = 0;
23684 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23685 : eval_isnamec(arg[j])))
23686 ++j;
23687 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023688 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023689 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023690 /* Disallow using the g: dict. */
23691 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23692 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023693 }
23694
Bram Moolenaar071d4272004-06-13 20:20:40 +000023695 /*
23696 * Isolate the arguments: "arg1, arg2, ...)"
23697 */
23698 while (*p != ')')
23699 {
23700 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23701 {
23702 varargs = TRUE;
23703 p += 3;
23704 mustend = TRUE;
23705 }
23706 else
23707 {
23708 arg = p;
23709 while (ASCII_ISALNUM(*p) || *p == '_')
23710 ++p;
23711 if (arg == p || isdigit(*arg)
23712 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23713 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23714 {
23715 if (!eap->skip)
23716 EMSG2(_("E125: Illegal argument: %s"), arg);
23717 break;
23718 }
23719 if (ga_grow(&newargs, 1) == FAIL)
23720 goto erret;
23721 c = *p;
23722 *p = NUL;
23723 arg = vim_strsave(arg);
23724 if (arg == NULL)
23725 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023726
23727 /* Check for duplicate argument name. */
23728 for (i = 0; i < newargs.ga_len; ++i)
23729 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23730 {
23731 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023732 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023733 goto erret;
23734 }
23735
Bram Moolenaar071d4272004-06-13 20:20:40 +000023736 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23737 *p = c;
23738 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023739 if (*p == ',')
23740 ++p;
23741 else
23742 mustend = TRUE;
23743 }
23744 p = skipwhite(p);
23745 if (mustend && *p != ')')
23746 {
23747 if (!eap->skip)
23748 EMSG2(_(e_invarg2), eap->arg);
23749 break;
23750 }
23751 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023752 if (*p != ')')
23753 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023754 ++p; /* skip the ')' */
23755
Bram Moolenaare9a41262005-01-15 22:18:47 +000023756 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 for (;;)
23758 {
23759 p = skipwhite(p);
23760 if (STRNCMP(p, "range", 5) == 0)
23761 {
23762 flags |= FC_RANGE;
23763 p += 5;
23764 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023765 else if (STRNCMP(p, "dict", 4) == 0)
23766 {
23767 flags |= FC_DICT;
23768 p += 4;
23769 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023770 else if (STRNCMP(p, "abort", 5) == 0)
23771 {
23772 flags |= FC_ABORT;
23773 p += 5;
23774 }
23775 else
23776 break;
23777 }
23778
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023779 /* When there is a line break use what follows for the function body.
23780 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23781 if (*p == '\n')
23782 line_arg = p + 1;
23783 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 EMSG(_(e_trailing));
23785
23786 /*
23787 * Read the body of the function, until ":endfunction" is found.
23788 */
23789 if (KeyTyped)
23790 {
23791 /* Check if the function already exists, don't let the user type the
23792 * whole function before telling him it doesn't work! For a script we
23793 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023794 if (!eap->skip && !eap->forceit)
23795 {
23796 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23797 EMSG(_(e_funcdict));
23798 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023799 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023801
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023802 if (!eap->skip && did_emsg)
23803 goto erret;
23804
Bram Moolenaar071d4272004-06-13 20:20:40 +000023805 msg_putchar('\n'); /* don't overwrite the function name */
23806 cmdline_row = msg_row;
23807 }
23808
23809 indent = 2;
23810 nesting = 0;
23811 for (;;)
23812 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023813 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023814 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023815 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023816 saved_wait_return = FALSE;
23817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023818 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023819 sourcing_lnum_off = sourcing_lnum;
23820
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023821 if (line_arg != NULL)
23822 {
23823 /* Use eap->arg, split up in parts by line breaks. */
23824 theline = line_arg;
23825 p = vim_strchr(theline, '\n');
23826 if (p == NULL)
23827 line_arg += STRLEN(line_arg);
23828 else
23829 {
23830 *p = NUL;
23831 line_arg = p + 1;
23832 }
23833 }
23834 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 theline = getcmdline(':', 0L, indent);
23836 else
23837 theline = eap->getline(':', eap->cookie, indent);
23838 if (KeyTyped)
23839 lines_left = Rows - 1;
23840 if (theline == NULL)
23841 {
23842 EMSG(_("E126: Missing :endfunction"));
23843 goto erret;
23844 }
23845
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023846 /* Detect line continuation: sourcing_lnum increased more than one. */
23847 if (sourcing_lnum > sourcing_lnum_off + 1)
23848 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23849 else
23850 sourcing_lnum_off = 0;
23851
Bram Moolenaar071d4272004-06-13 20:20:40 +000023852 if (skip_until != NULL)
23853 {
23854 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23855 * don't check for ":endfunc". */
23856 if (STRCMP(theline, skip_until) == 0)
23857 {
23858 vim_free(skip_until);
23859 skip_until = NULL;
23860 }
23861 }
23862 else
23863 {
23864 /* skip ':' and blanks*/
23865 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23866 ;
23867
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023868 /* Check for "endfunction". */
23869 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023870 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023871 if (line_arg == NULL)
23872 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 break;
23874 }
23875
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023876 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023877 * at "end". */
23878 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23879 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023880 else if (STRNCMP(p, "if", 2) == 0
23881 || STRNCMP(p, "wh", 2) == 0
23882 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023883 || STRNCMP(p, "try", 3) == 0)
23884 indent += 2;
23885
23886 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023887 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023888 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023889 if (*p == '!')
23890 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 p += eval_fname_script(p);
Bram Moolenaaref923902014-12-13 21:00:55 +010023892 vim_free(trans_function_name(&p, TRUE, 0, NULL));
23893 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023894 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023895 ++nesting;
23896 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023897 }
23898 }
23899
23900 /* Check for ":append" or ":insert". */
23901 p = skip_range(p, NULL);
23902 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23903 || (p[0] == 'i'
23904 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23905 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23906 skip_until = vim_strsave((char_u *)".");
23907
23908 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23909 arg = skipwhite(skiptowhite(p));
23910 if (arg[0] == '<' && arg[1] =='<'
23911 && ((p[0] == 'p' && p[1] == 'y'
23912 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23913 || (p[0] == 'p' && p[1] == 'e'
23914 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23915 || (p[0] == 't' && p[1] == 'c'
23916 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023917 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23918 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023919 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23920 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023921 || (p[0] == 'm' && p[1] == 'z'
23922 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023923 ))
23924 {
23925 /* ":python <<" continues until a dot, like ":append" */
23926 p = skipwhite(arg + 2);
23927 if (*p == NUL)
23928 skip_until = vim_strsave((char_u *)".");
23929 else
23930 skip_until = vim_strsave(p);
23931 }
23932 }
23933
23934 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023935 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023936 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023937 if (line_arg == NULL)
23938 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023939 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023940 }
23941
23942 /* Copy the line to newly allocated memory. get_one_sourceline()
23943 * allocates 250 bytes per line, this saves 80% on average. The cost
23944 * is an extra alloc/free. */
23945 p = vim_strsave(theline);
23946 if (p != NULL)
23947 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023948 if (line_arg == NULL)
23949 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023950 theline = p;
23951 }
23952
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023953 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23954
23955 /* Add NULL lines for continuation lines, so that the line count is
23956 * equal to the index in the growarray. */
23957 while (sourcing_lnum_off-- > 0)
23958 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023959
23960 /* Check for end of eap->arg. */
23961 if (line_arg != NULL && *line_arg == NUL)
23962 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023963 }
23964
23965 /* Don't define the function when skipping commands or when an error was
23966 * detected. */
23967 if (eap->skip || did_emsg)
23968 goto erret;
23969
23970 /*
23971 * If there are no errors, add the function
23972 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023973 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023974 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023975 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023976 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023977 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023978 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023979 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023980 goto erret;
23981 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023982
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023983 fp = find_func(name);
23984 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023986 if (!eap->forceit)
23987 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023988 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989 goto erret;
23990 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023991 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023992 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023993 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994 name);
23995 goto erret;
23996 }
23997 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023998 ga_clear_strings(&(fp->uf_args));
23999 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024000 vim_free(name);
24001 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024003 }
24004 else
24005 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024006 char numbuf[20];
24007
24008 fp = NULL;
24009 if (fudi.fd_newkey == NULL && !eap->forceit)
24010 {
24011 EMSG(_(e_funcdict));
24012 goto erret;
24013 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000024014 if (fudi.fd_di == NULL)
24015 {
24016 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024017 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024018 goto erret;
24019 }
24020 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020024021 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000024022 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024023
24024 /* Give the function a sequential number. Can only be used with a
24025 * Funcref! */
24026 vim_free(name);
24027 sprintf(numbuf, "%d", ++func_nr);
24028 name = vim_strsave((char_u *)numbuf);
24029 if (name == NULL)
24030 goto erret;
24031 }
24032
24033 if (fp == NULL)
24034 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024035 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024036 {
24037 int slen, plen;
24038 char_u *scriptname;
24039
24040 /* Check that the autoload name matches the script name. */
24041 j = FAIL;
24042 if (sourcing_name != NULL)
24043 {
24044 scriptname = autoload_name(name);
24045 if (scriptname != NULL)
24046 {
24047 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024048 plen = (int)STRLEN(p);
24049 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024050 if (slen > plen && fnamecmp(p,
24051 sourcing_name + slen - plen) == 0)
24052 j = OK;
24053 vim_free(scriptname);
24054 }
24055 }
24056 if (j == FAIL)
24057 {
24058 EMSG2(_("E746: Function name does not match script file name: %s"), name);
24059 goto erret;
24060 }
24061 }
24062
24063 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 if (fp == NULL)
24065 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024066
24067 if (fudi.fd_dict != NULL)
24068 {
24069 if (fudi.fd_di == NULL)
24070 {
24071 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024072 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024073 if (fudi.fd_di == NULL)
24074 {
24075 vim_free(fp);
24076 goto erret;
24077 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024078 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
24079 {
24080 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000024081 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024082 goto erret;
24083 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024084 }
24085 else
24086 /* overwrite existing dict entry */
24087 clear_tv(&fudi.fd_di->di_tv);
24088 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024089 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024090 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024091 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000024092
24093 /* behave like "dict" was used */
24094 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024095 }
24096
Bram Moolenaar071d4272004-06-13 20:20:40 +000024097 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024098 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010024099 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
24100 {
24101 vim_free(fp);
24102 goto erret;
24103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024104 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 fp->uf_args = newargs;
24106 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024107#ifdef FEAT_PROFILE
24108 fp->uf_tml_count = NULL;
24109 fp->uf_tml_total = NULL;
24110 fp->uf_tml_self = NULL;
24111 fp->uf_profiling = FALSE;
24112 if (prof_def_func())
24113 func_do_profile(fp);
24114#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024115 fp->uf_varargs = varargs;
24116 fp->uf_flags = flags;
24117 fp->uf_calls = 0;
24118 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024119 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024120
24121erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024122 ga_clear_strings(&newargs);
24123 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024124ret_free:
24125 vim_free(skip_until);
24126 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024127 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024128 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024129 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024130}
24131
24132/*
24133 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024134 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024135 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024136 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024137 * TFN_INT: internal function name OK
24138 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024139 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024140 * Advances "pp" to just after the function name (if no error).
24141 */
24142 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024143trans_function_name(
24144 char_u **pp,
24145 int skip, /* only find the end, don't evaluate */
24146 int flags,
24147 funcdict_T *fdp) /* return: info about dictionary used */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024148{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024149 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024150 char_u *start;
24151 char_u *end;
24152 int lead;
24153 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024154 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024155 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024156
24157 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024158 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024159 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024160
24161 /* Check for hard coded <SNR>: already translated function ID (from a user
24162 * command). */
24163 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24164 && (*pp)[2] == (int)KE_SNR)
24165 {
24166 *pp += 3;
24167 len = get_id_len(pp) + 3;
24168 return vim_strnsave(start, len);
24169 }
24170
24171 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24172 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024174 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024175 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024176
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024177 /* Note that TFN_ flags use the same values as GLV_ flags. */
24178 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024179 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024180 if (end == start)
24181 {
24182 if (!skip)
24183 EMSG(_("E129: Function name required"));
24184 goto theend;
24185 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024186 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024187 {
24188 /*
24189 * Report an invalid expression in braces, unless the expression
24190 * evaluation has been cancelled due to an aborting error, an
24191 * interrupt, or an exception.
24192 */
24193 if (!aborting())
24194 {
24195 if (end != NULL)
24196 EMSG2(_(e_invarg2), start);
24197 }
24198 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024199 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024200 goto theend;
24201 }
24202
24203 if (lv.ll_tv != NULL)
24204 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024205 if (fdp != NULL)
24206 {
24207 fdp->fd_dict = lv.ll_dict;
24208 fdp->fd_newkey = lv.ll_newkey;
24209 lv.ll_newkey = NULL;
24210 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024211 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024212 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24213 {
24214 name = vim_strsave(lv.ll_tv->vval.v_string);
24215 *pp = end;
24216 }
24217 else
24218 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024219 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24220 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024221 EMSG(_(e_funcref));
24222 else
24223 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024224 name = NULL;
24225 }
24226 goto theend;
24227 }
24228
24229 if (lv.ll_name == NULL)
24230 {
24231 /* Error found, but continue after the function name. */
24232 *pp = end;
24233 goto theend;
24234 }
24235
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024236 /* Check if the name is a Funcref. If so, use the value. */
24237 if (lv.ll_exp_name != NULL)
24238 {
24239 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024240 name = deref_func_name(lv.ll_exp_name, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024241 if (name == lv.ll_exp_name)
24242 name = NULL;
24243 }
24244 else
24245 {
24246 len = (int)(end - *pp);
Bram Moolenaar8822a9c2014-01-14 19:44:34 +010024247 name = deref_func_name(*pp, &len, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024248 if (name == *pp)
24249 name = NULL;
24250 }
24251 if (name != NULL)
24252 {
24253 name = vim_strsave(name);
24254 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024255 if (STRNCMP(name, "<SNR>", 5) == 0)
24256 {
24257 /* Change "<SNR>" to the byte sequence. */
24258 name[0] = K_SPECIAL;
24259 name[1] = KS_EXTRA;
24260 name[2] = (int)KE_SNR;
24261 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24262 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024263 goto theend;
24264 }
24265
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024266 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024267 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024268 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024269 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24270 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24271 {
24272 /* When there was "s:" already or the name expanded to get a
24273 * leading "s:" then remove it. */
24274 lv.ll_name += 2;
24275 len -= 2;
24276 lead = 2;
24277 }
24278 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024279 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024280 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024281 /* skip over "s:" and "g:" */
24282 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024283 lv.ll_name += 2;
24284 len = (int)(end - lv.ll_name);
24285 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024286
24287 /*
24288 * Copy the function name to allocated memory.
24289 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24290 * Accept <SNR>123_name() outside a script.
24291 */
24292 if (skip)
24293 lead = 0; /* do nothing */
24294 else if (lead > 0)
24295 {
24296 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024297 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24298 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024299 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024300 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024301 if (current_SID <= 0)
24302 {
24303 EMSG(_(e_usingsid));
24304 goto theend;
24305 }
24306 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24307 lead += (int)STRLEN(sid_buf);
24308 }
24309 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024310 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024311 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024312 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024313 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024314 goto theend;
24315 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024316 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024317 {
24318 char_u *cp = vim_strchr(lv.ll_name, ':');
24319
24320 if (cp != NULL && cp < end)
24321 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024322 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024323 goto theend;
24324 }
24325 }
24326
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024327 name = alloc((unsigned)(len + lead + 1));
24328 if (name != NULL)
24329 {
24330 if (lead > 0)
24331 {
24332 name[0] = K_SPECIAL;
24333 name[1] = KS_EXTRA;
24334 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024335 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024336 STRCPY(name + 3, sid_buf);
24337 }
24338 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024339 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024340 }
24341 *pp = end;
24342
24343theend:
24344 clear_lval(&lv);
24345 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024346}
24347
24348/*
24349 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24350 * Return 2 if "p" starts with "s:".
24351 * Return 0 otherwise.
24352 */
24353 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024354eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024355{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024356 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24357 * the standard library function. */
24358 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24359 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024360 return 5;
24361 if (p[0] == 's' && p[1] == ':')
24362 return 2;
24363 return 0;
24364}
24365
24366/*
24367 * Return TRUE if "p" starts with "<SID>" or "s:".
24368 * Only works if eval_fname_script() returned non-zero for "p"!
24369 */
24370 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024371eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024372{
24373 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24374}
24375
24376/*
24377 * List the head of the function: "name(arg1, arg2)".
24378 */
24379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024380list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024381{
24382 int j;
24383
24384 msg_start();
24385 if (indent)
24386 MSG_PUTS(" ");
24387 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024388 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024389 {
24390 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024391 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024392 }
24393 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024394 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024395 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024396 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024397 {
24398 if (j)
24399 MSG_PUTS(", ");
24400 msg_puts(FUNCARG(fp, j));
24401 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024402 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024403 {
24404 if (j)
24405 MSG_PUTS(", ");
24406 MSG_PUTS("...");
24407 }
24408 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024409 if (fp->uf_flags & FC_ABORT)
24410 MSG_PUTS(" abort");
24411 if (fp->uf_flags & FC_RANGE)
24412 MSG_PUTS(" range");
24413 if (fp->uf_flags & FC_DICT)
24414 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024415 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024416 if (p_verbose > 0)
24417 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024418}
24419
24420/*
24421 * Find a function by name, return pointer to it in ufuncs.
24422 * Return NULL for unknown function.
24423 */
24424 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024425find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024426{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024427 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024428
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024429 hi = hash_find(&func_hashtab, name);
24430 if (!HASHITEM_EMPTY(hi))
24431 return HI2UF(hi);
24432 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024433}
24434
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024435#if defined(EXITFREE) || defined(PROTO)
24436 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024437free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024438{
24439 hashitem_T *hi;
24440
24441 /* Need to start all over every time, because func_free() may change the
24442 * hash table. */
24443 while (func_hashtab.ht_used > 0)
24444 for (hi = func_hashtab.ht_array; ; ++hi)
24445 if (!HASHITEM_EMPTY(hi))
24446 {
24447 func_free(HI2UF(hi));
24448 break;
24449 }
24450}
24451#endif
24452
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024454translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024455{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024456 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024457 return find_internal_func(name) >= 0;
24458 return find_func(name) != NULL;
24459}
24460
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024461/*
24462 * Return TRUE if a function "name" exists.
24463 */
24464 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024465function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024466{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024467 char_u *nm = name;
24468 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024469 int n = FALSE;
24470
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024471 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
24472 NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024473 nm = skipwhite(nm);
24474
24475 /* Only accept "funcname", "funcname ", "funcname (..." and
24476 * "funcname(...", not "funcname!...". */
24477 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024478 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024479 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024480 return n;
24481}
24482
Bram Moolenaara1544c02013-05-30 12:35:52 +020024483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024484get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024485{
24486 char_u *nm = name;
24487 char_u *p;
24488
24489 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL);
24490
24491 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024492 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024493 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024494
Bram Moolenaara1544c02013-05-30 12:35:52 +020024495 vim_free(p);
24496 return NULL;
24497}
24498
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024499/*
24500 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024501 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24502 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024503 */
24504 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024505builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024506{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024507 char_u *p;
24508
24509 if (!ASCII_ISLOWER(name[0]))
24510 return FALSE;
24511 p = vim_strchr(name, AUTOLOAD_CHAR);
24512 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024513}
24514
Bram Moolenaar05159a02005-02-26 23:04:13 +000024515#if defined(FEAT_PROFILE) || defined(PROTO)
24516/*
24517 * Start profiling function "fp".
24518 */
24519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024520func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024521{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024522 int len = fp->uf_lines.ga_len;
24523
24524 if (len == 0)
24525 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024526 fp->uf_tm_count = 0;
24527 profile_zero(&fp->uf_tm_self);
24528 profile_zero(&fp->uf_tm_total);
24529 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024530 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024531 if (fp->uf_tml_total == NULL)
24532 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024533 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024534 if (fp->uf_tml_self == NULL)
24535 fp->uf_tml_self = (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 fp->uf_tml_idx = -1;
24538 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24539 || fp->uf_tml_self == NULL)
24540 return; /* out of memory */
24541
24542 fp->uf_profiling = TRUE;
24543}
24544
24545/*
24546 * Dump the profiling results for all functions in file "fd".
24547 */
24548 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024549func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024550{
24551 hashitem_T *hi;
24552 int todo;
24553 ufunc_T *fp;
24554 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024555 ufunc_T **sorttab;
24556 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024557
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024558 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024559 if (todo == 0)
24560 return; /* nothing to dump */
24561
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024562 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024563
Bram Moolenaar05159a02005-02-26 23:04:13 +000024564 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24565 {
24566 if (!HASHITEM_EMPTY(hi))
24567 {
24568 --todo;
24569 fp = HI2UF(hi);
24570 if (fp->uf_profiling)
24571 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024572 if (sorttab != NULL)
24573 sorttab[st_len++] = fp;
24574
Bram Moolenaar05159a02005-02-26 23:04:13 +000024575 if (fp->uf_name[0] == K_SPECIAL)
24576 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24577 else
24578 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24579 if (fp->uf_tm_count == 1)
24580 fprintf(fd, "Called 1 time\n");
24581 else
24582 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24583 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24584 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24585 fprintf(fd, "\n");
24586 fprintf(fd, "count total (s) self (s)\n");
24587
24588 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24589 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024590 if (FUNCLINE(fp, i) == NULL)
24591 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024592 prof_func_line(fd, fp->uf_tml_count[i],
24593 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024594 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24595 }
24596 fprintf(fd, "\n");
24597 }
24598 }
24599 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024600
24601 if (sorttab != NULL && st_len > 0)
24602 {
24603 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24604 prof_total_cmp);
24605 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24606 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24607 prof_self_cmp);
24608 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24609 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024610
24611 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024612}
Bram Moolenaar73830342005-02-28 22:48:19 +000024613
24614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024615prof_sort_list(
24616 FILE *fd,
24617 ufunc_T **sorttab,
24618 int st_len,
24619 char *title,
24620 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024621{
24622 int i;
24623 ufunc_T *fp;
24624
24625 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24626 fprintf(fd, "count total (s) self (s) function\n");
24627 for (i = 0; i < 20 && i < st_len; ++i)
24628 {
24629 fp = sorttab[i];
24630 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24631 prefer_self);
24632 if (fp->uf_name[0] == K_SPECIAL)
24633 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24634 else
24635 fprintf(fd, " %s()\n", fp->uf_name);
24636 }
24637 fprintf(fd, "\n");
24638}
24639
24640/*
24641 * Print the count and times for one function or function line.
24642 */
24643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024644prof_func_line(
24645 FILE *fd,
24646 int count,
24647 proftime_T *total,
24648 proftime_T *self,
24649 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024650{
24651 if (count > 0)
24652 {
24653 fprintf(fd, "%5d ", count);
24654 if (prefer_self && profile_equal(total, self))
24655 fprintf(fd, " ");
24656 else
24657 fprintf(fd, "%s ", profile_msg(total));
24658 if (!prefer_self && profile_equal(total, self))
24659 fprintf(fd, " ");
24660 else
24661 fprintf(fd, "%s ", profile_msg(self));
24662 }
24663 else
24664 fprintf(fd, " ");
24665}
24666
24667/*
24668 * Compare function for total time sorting.
24669 */
24670 static int
24671#ifdef __BORLANDC__
24672_RTLENTRYF
24673#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024674prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024675{
24676 ufunc_T *p1, *p2;
24677
24678 p1 = *(ufunc_T **)s1;
24679 p2 = *(ufunc_T **)s2;
24680 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24681}
24682
24683/*
24684 * Compare function for self time sorting.
24685 */
24686 static int
24687#ifdef __BORLANDC__
24688_RTLENTRYF
24689#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024690prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024691{
24692 ufunc_T *p1, *p2;
24693
24694 p1 = *(ufunc_T **)s1;
24695 p2 = *(ufunc_T **)s2;
24696 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24697}
24698
Bram Moolenaar05159a02005-02-26 23:04:13 +000024699#endif
24700
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024701/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024702 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024703 * Return TRUE if a package was loaded.
24704 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024706script_autoload(
24707 char_u *name,
24708 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024709{
24710 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024711 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024712 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024713 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024714
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024715 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024716 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024717 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024718 return FALSE;
24719
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024720 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024721
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024722 /* Find the name in the list of previously loaded package names. Skip
24723 * "autoload/", it's always the same. */
24724 for (i = 0; i < ga_loaded.ga_len; ++i)
24725 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24726 break;
24727 if (!reload && i < ga_loaded.ga_len)
24728 ret = FALSE; /* was loaded already */
24729 else
24730 {
24731 /* Remember the name if it wasn't loaded already. */
24732 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24733 {
24734 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24735 tofree = NULL;
24736 }
24737
24738 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar90cfdbe2005-08-12 19:59:19 +000024739 if (source_runtime(scriptname, FALSE) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024740 ret = TRUE;
24741 }
24742
24743 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024744 return ret;
24745}
24746
24747/*
24748 * Return the autoload script name for a function or variable name.
24749 * Returns NULL when out of memory.
24750 */
24751 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024752autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024753{
24754 char_u *p;
24755 char_u *scriptname;
24756
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024757 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024758 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24759 if (scriptname == NULL)
24760 return FALSE;
24761 STRCPY(scriptname, "autoload/");
24762 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024763 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024764 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024765 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024766 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024767 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024768}
24769
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24771
24772/*
24773 * Function given to ExpandGeneric() to obtain the list of user defined
24774 * function names.
24775 */
24776 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024777get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024778{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024779 static long_u done;
24780 static hashitem_T *hi;
24781 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782
24783 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024784 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024785 done = 0;
24786 hi = func_hashtab.ht_array;
24787 }
24788 if (done < func_hashtab.ht_used)
24789 {
24790 if (done++ > 0)
24791 ++hi;
24792 while (HASHITEM_EMPTY(hi))
24793 ++hi;
24794 fp = HI2UF(hi);
24795
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024796 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024797 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024798
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024799 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24800 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801
24802 cat_func_name(IObuff, fp);
24803 if (xp->xp_context != EXPAND_USER_FUNC)
24804 {
24805 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024806 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807 STRCAT(IObuff, ")");
24808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024809 return IObuff;
24810 }
24811 return NULL;
24812}
24813
24814#endif /* FEAT_CMDL_COMPL */
24815
24816/*
24817 * Copy the function name of "fp" to buffer "buf".
24818 * "buf" must be able to hold the function name plus three bytes.
24819 * Takes care of script-local function names.
24820 */
24821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024822cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024823{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024824 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024825 {
24826 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024827 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024828 }
24829 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024831}
24832
24833/*
24834 * ":delfunction {name}"
24835 */
24836 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024837ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024838{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024839 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 char_u *p;
24841 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024842 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024843
24844 p = eap->arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024845 name = trans_function_name(&p, eap->skip, 0, &fudi);
24846 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024847 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024848 {
24849 if (fudi.fd_dict != NULL && !eap->skip)
24850 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024851 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024853 if (!ends_excmd(*skipwhite(p)))
24854 {
24855 vim_free(name);
24856 EMSG(_(e_trailing));
24857 return;
24858 }
24859 eap->nextcmd = check_nextcmd(p);
24860 if (eap->nextcmd != NULL)
24861 *p = NUL;
24862
24863 if (!eap->skip)
24864 fp = find_func(name);
24865 vim_free(name);
24866
24867 if (!eap->skip)
24868 {
24869 if (fp == NULL)
24870 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024871 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024872 return;
24873 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024874 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024875 {
24876 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24877 return;
24878 }
24879
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024880 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024881 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024882 /* Delete the dict item that refers to the function, it will
24883 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024884 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024885 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024886 else
24887 func_free(fp);
24888 }
24889}
24890
24891/*
24892 * Free a function and remove it from the list of functions.
24893 */
24894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024895func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024896{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024897 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024898
24899 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024900 ga_clear_strings(&(fp->uf_args));
24901 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024902#ifdef FEAT_PROFILE
24903 vim_free(fp->uf_tml_count);
24904 vim_free(fp->uf_tml_total);
24905 vim_free(fp->uf_tml_self);
24906#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024907
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024908 /* remove the function from the function hashtable */
24909 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24910 if (HASHITEM_EMPTY(hi))
24911 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024912 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024913 hash_remove(&func_hashtab, hi);
24914
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024915 vim_free(fp);
24916}
24917
24918/*
24919 * Unreference a Function: decrement the reference count and free it when it
24920 * becomes zero. Only for numbered functions.
24921 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024923func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024924{
24925 ufunc_T *fp;
24926
24927 if (name != NULL && isdigit(*name))
24928 {
24929 fp = find_func(name);
24930 if (fp == NULL)
24931 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024932 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024933 {
24934 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024935 * when "uf_calls" becomes zero. */
24936 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024937 func_free(fp);
24938 }
24939 }
24940}
24941
24942/*
24943 * Count a reference to a Function.
24944 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024946func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024947{
24948 ufunc_T *fp;
24949
24950 if (name != NULL && isdigit(*name))
24951 {
24952 fp = find_func(name);
24953 if (fp == NULL)
24954 EMSG2(_(e_intern2), "func_ref()");
24955 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024956 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024957 }
24958}
24959
24960/*
24961 * Call a user function.
24962 */
24963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024964call_user_func(
24965 ufunc_T *fp, /* pointer to function */
24966 int argcount, /* nr of args */
24967 typval_T *argvars, /* arguments */
24968 typval_T *rettv, /* return value */
24969 linenr_T firstline, /* first line of range */
24970 linenr_T lastline, /* last line of range */
24971 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024972{
Bram Moolenaar33570922005-01-25 22:26:29 +000024973 char_u *save_sourcing_name;
24974 linenr_T save_sourcing_lnum;
24975 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024976 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 int save_did_emsg;
24978 static int depth = 0;
24979 dictitem_T *v;
24980 int fixvar_idx = 0; /* index in fixvar[] */
24981 int i;
24982 int ai;
24983 char_u numbuf[NUMBUFLEN];
24984 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024985 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024986#ifdef FEAT_PROFILE
24987 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024988 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990
24991 /* If depth of calling is getting too high, don't execute the function */
24992 if (depth >= p_mfd)
24993 {
24994 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024995 rettv->v_type = VAR_NUMBER;
24996 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024997 return;
24998 }
24999 ++depth;
25000
25001 line_breakcheck(); /* check for CTRL-C hit */
25002
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025003 fc = (funccall_T *)alloc(sizeof(funccall_T));
25004 fc->caller = current_funccal;
25005 current_funccal = fc;
25006 fc->func = fp;
25007 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025008 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025009 fc->linenr = 0;
25010 fc->returned = FALSE;
25011 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025012 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025013 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
25014 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025015
Bram Moolenaar33570922005-01-25 22:26:29 +000025016 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025017 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000025018 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
25019 * each argument variable and saves a lot of time.
25020 */
25021 /*
25022 * Init l: variables.
25023 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025024 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000025025 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000025026 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025027 /* Set l:self to "selfdict". Use "name" to avoid a warning from
25028 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025029 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000025030 name = v->di_key;
25031 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000025032 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025033 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025034 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025035 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000025036 v->di_tv.vval.v_dict = selfdict;
25037 ++selfdict->dv_refcount;
25038 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000025039
Bram Moolenaar33570922005-01-25 22:26:29 +000025040 /*
25041 * Init a: variables.
25042 * Set a:0 to "argcount".
25043 * Set a:000 to a list with room for the "..." arguments.
25044 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020025045 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025046 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025047 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025048 /* Use "name" to avoid a warning from some compiler that checks the
25049 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025050 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000025051 name = v->di_key;
25052 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000025053 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025054 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025055 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025056 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025057 v->di_tv.vval.v_list = &fc->l_varlist;
25058 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
25059 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
25060 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025061
25062 /*
25063 * Set a:firstline to "firstline" and a:lastline to "lastline".
25064 * Set a:name to named arguments.
25065 * Set a:N to the "..." arguments.
25066 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025067 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025068 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025069 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000025070 (varnumber_T)lastline);
25071 for (i = 0; i < argcount; ++i)
25072 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025073 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000025074 if (ai < 0)
25075 /* named argument a:name */
25076 name = FUNCARG(fp, i);
25077 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000025078 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025079 /* "..." argument a:1, a:2, etc. */
25080 sprintf((char *)numbuf, "%d", ai + 1);
25081 name = numbuf;
25082 }
25083 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
25084 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025085 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000025086 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25087 }
25088 else
25089 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025090 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
25091 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000025092 if (v == NULL)
25093 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020025094 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000025095 }
25096 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025097 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000025098
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025099 /* Note: the values are copied directly to avoid alloc/free.
25100 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025101 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025102 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025103
25104 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25105 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025106 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25107 fc->l_listitems[ai].li_tv = argvars[i];
25108 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025109 }
25110 }
25111
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 /* Don't redraw while executing the function. */
25113 ++RedrawingDisabled;
25114 save_sourcing_name = sourcing_name;
25115 save_sourcing_lnum = sourcing_lnum;
25116 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025117 /* need space for function name + ("function " + 3) or "[number]" */
25118 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25119 + STRLEN(fp->uf_name) + 20;
25120 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121 if (sourcing_name != NULL)
25122 {
25123 if (save_sourcing_name != NULL
25124 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025125 sprintf((char *)sourcing_name, "%s[%d]..",
25126 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025127 else
25128 STRCPY(sourcing_name, "function ");
25129 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25130
25131 if (p_verbose >= 12)
25132 {
25133 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025134 verbose_enter_scroll();
25135
Bram Moolenaar555b2802005-05-19 21:08:39 +000025136 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025137 if (p_verbose >= 14)
25138 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025139 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025140 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025141 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025142 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025143
25144 msg_puts((char_u *)"(");
25145 for (i = 0; i < argcount; ++i)
25146 {
25147 if (i > 0)
25148 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025149 if (argvars[i].v_type == VAR_NUMBER)
25150 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025151 else
25152 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025153 /* Do not want errors such as E724 here. */
25154 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025155 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025156 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025157 if (s != NULL)
25158 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025159 if (vim_strsize(s) > MSG_BUF_CLEN)
25160 {
25161 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25162 s = buf;
25163 }
25164 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025165 vim_free(tofree);
25166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025167 }
25168 }
25169 msg_puts((char_u *)")");
25170 }
25171 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025172
25173 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025174 --no_wait_return;
25175 }
25176 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025177#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025178 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025179 {
25180 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25181 func_do_profile(fp);
25182 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025183 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025184 {
25185 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025186 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025187 profile_zero(&fp->uf_tm_children);
25188 }
25189 script_prof_save(&wait_start);
25190 }
25191#endif
25192
Bram Moolenaar071d4272004-06-13 20:20:40 +000025193 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025194 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025195 save_did_emsg = did_emsg;
25196 did_emsg = FALSE;
25197
25198 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025199 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025200 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25201
25202 --RedrawingDisabled;
25203
25204 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025205 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025207 clear_tv(rettv);
25208 rettv->v_type = VAR_NUMBER;
25209 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025210 }
25211
Bram Moolenaar05159a02005-02-26 23:04:13 +000025212#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025213 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025214 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025215 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025216 profile_end(&call_start);
25217 profile_sub_wait(&wait_start, &call_start);
25218 profile_add(&fp->uf_tm_total, &call_start);
25219 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025220 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025221 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025222 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25223 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025224 }
25225 }
25226#endif
25227
Bram Moolenaar071d4272004-06-13 20:20:40 +000025228 /* when being verbose, mention the return value */
25229 if (p_verbose >= 12)
25230 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025231 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025232 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025233
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025235 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025236 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025237 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025238 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025239 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025240 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025241 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025242 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025243 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025244 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025245
Bram Moolenaar555b2802005-05-19 21:08:39 +000025246 /* The value may be very long. Skip the middle part, so that we
25247 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025248 * truncate it at the end. Don't want errors such as E724 here. */
25249 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025250 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025251 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025252 if (s != NULL)
25253 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025254 if (vim_strsize(s) > MSG_BUF_CLEN)
25255 {
25256 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25257 s = buf;
25258 }
25259 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025260 vim_free(tofree);
25261 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025262 }
25263 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025264
25265 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 --no_wait_return;
25267 }
25268
25269 vim_free(sourcing_name);
25270 sourcing_name = save_sourcing_name;
25271 sourcing_lnum = save_sourcing_lnum;
25272 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025273#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025274 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025275 script_prof_restore(&wait_start);
25276#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025277
25278 if (p_verbose >= 12 && sourcing_name != NULL)
25279 {
25280 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025281 verbose_enter_scroll();
25282
Bram Moolenaar555b2802005-05-19 21:08:39 +000025283 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025284 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025285
25286 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287 --no_wait_return;
25288 }
25289
25290 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025291 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025292 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025293
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025294 /* If the a:000 list and the l: and a: dicts are not referenced we can
25295 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025296 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25297 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25298 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25299 {
25300 free_funccal(fc, FALSE);
25301 }
25302 else
25303 {
25304 hashitem_T *hi;
25305 listitem_T *li;
25306 int todo;
25307
25308 /* "fc" is still in use. This can happen when returning "a:000" or
25309 * assigning "l:" to a global variable.
25310 * Link "fc" in the list for garbage collection later. */
25311 fc->caller = previous_funccal;
25312 previous_funccal = fc;
25313
25314 /* Make a copy of the a: variables, since we didn't do that above. */
25315 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25316 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25317 {
25318 if (!HASHITEM_EMPTY(hi))
25319 {
25320 --todo;
25321 v = HI2DI(hi);
25322 copy_tv(&v->di_tv, &v->di_tv);
25323 }
25324 }
25325
25326 /* Make a copy of the a:000 items, since we didn't do that above. */
25327 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25328 copy_tv(&li->li_tv, &li->li_tv);
25329 }
25330}
25331
25332/*
25333 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025334 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025335 */
25336 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025337can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025338{
25339 return (fc->l_varlist.lv_copyID != copyID
25340 && fc->l_vars.dv_copyID != copyID
25341 && fc->l_avars.dv_copyID != copyID);
25342}
25343
25344/*
25345 * Free "fc" and what it contains.
25346 */
25347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025348free_funccal(
25349 funccall_T *fc,
25350 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025351{
25352 listitem_T *li;
25353
25354 /* The a: variables typevals may not have been allocated, only free the
25355 * allocated variables. */
25356 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25357
25358 /* free all l: variables */
25359 vars_clear(&fc->l_vars.dv_hashtab);
25360
25361 /* Free the a:000 variables if they were allocated. */
25362 if (free_val)
25363 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25364 clear_tv(&li->li_tv);
25365
25366 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025367}
25368
25369/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025370 * Add a number variable "name" to dict "dp" with value "nr".
25371 */
25372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025373add_nr_var(
25374 dict_T *dp,
25375 dictitem_T *v,
25376 char *name,
25377 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025378{
25379 STRCPY(v->di_key, name);
25380 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25381 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25382 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025383 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025384 v->di_tv.vval.v_number = nr;
25385}
25386
25387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025388 * ":return [expr]"
25389 */
25390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025391ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392{
25393 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025394 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395 int returning = FALSE;
25396
25397 if (current_funccal == NULL)
25398 {
25399 EMSG(_("E133: :return not inside a function"));
25400 return;
25401 }
25402
25403 if (eap->skip)
25404 ++emsg_skip;
25405
25406 eap->nextcmd = NULL;
25407 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025408 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409 {
25410 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025411 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025413 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 }
25415 /* It's safer to return also on error. */
25416 else if (!eap->skip)
25417 {
25418 /*
25419 * Return unless the expression evaluation has been cancelled due to an
25420 * aborting error, an interrupt, or an exception.
25421 */
25422 if (!aborting())
25423 returning = do_return(eap, FALSE, TRUE, NULL);
25424 }
25425
25426 /* When skipping or the return gets pending, advance to the next command
25427 * in this line (!returning). Otherwise, ignore the rest of the line.
25428 * Following lines will be ignored by get_func_line(). */
25429 if (returning)
25430 eap->nextcmd = NULL;
25431 else if (eap->nextcmd == NULL) /* no argument */
25432 eap->nextcmd = check_nextcmd(arg);
25433
25434 if (eap->skip)
25435 --emsg_skip;
25436}
25437
25438/*
25439 * Return from a function. Possibly makes the return pending. Also called
25440 * for a pending return at the ":endtry" or after returning from an extra
25441 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025442 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025443 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025444 * FALSE when the return gets pending.
25445 */
25446 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025447do_return(
25448 exarg_T *eap,
25449 int reanimate,
25450 int is_cmd,
25451 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452{
25453 int idx;
25454 struct condstack *cstack = eap->cstack;
25455
25456 if (reanimate)
25457 /* Undo the return. */
25458 current_funccal->returned = FALSE;
25459
25460 /*
25461 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25462 * not in its finally clause (which then is to be executed next) is found.
25463 * In this case, make the ":return" pending for execution at the ":endtry".
25464 * Otherwise, return normally.
25465 */
25466 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25467 if (idx >= 0)
25468 {
25469 cstack->cs_pending[idx] = CSTP_RETURN;
25470
25471 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025472 /* A pending return again gets pending. "rettv" points to an
25473 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025474 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025475 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 else
25477 {
25478 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025479 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025480 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025481 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025483 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484 {
25485 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025486 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025487 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025488 else
25489 EMSG(_(e_outofmem));
25490 }
25491 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025492 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025493
25494 if (reanimate)
25495 {
25496 /* The pending return value could be overwritten by a ":return"
25497 * without argument in a finally clause; reset the default
25498 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025499 current_funccal->rettv->v_type = VAR_NUMBER;
25500 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501 }
25502 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025503 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 }
25505 else
25506 {
25507 current_funccal->returned = TRUE;
25508
25509 /* If the return is carried out now, store the return value. For
25510 * a return immediately after reanimation, the value is already
25511 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025512 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025513 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025514 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025515 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025516 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025517 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025518 }
25519 }
25520
25521 return idx < 0;
25522}
25523
25524/*
25525 * Free the variable with a pending return value.
25526 */
25527 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025528discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529{
Bram Moolenaar33570922005-01-25 22:26:29 +000025530 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531}
25532
25533/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025534 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025535 * is an allocated string. Used by report_pending() for verbose messages.
25536 */
25537 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025538get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025540 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025541 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025542 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025543
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025544 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025545 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025546 if (s == NULL)
25547 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025548
25549 STRCPY(IObuff, ":return ");
25550 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25551 if (STRLEN(s) + 8 >= IOSIZE)
25552 STRCPY(IObuff + IOSIZE - 4, "...");
25553 vim_free(tofree);
25554 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025555}
25556
25557/*
25558 * Get next function line.
25559 * Called by do_cmdline() to get the next line.
25560 * Returns allocated string, or NULL for end of function.
25561 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025563get_func_line(
25564 int c UNUSED,
25565 void *cookie,
25566 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567{
Bram Moolenaar33570922005-01-25 22:26:29 +000025568 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025569 ufunc_T *fp = fcp->func;
25570 char_u *retval;
25571 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025572
25573 /* If breakpoints have been added/deleted need to check for it. */
25574 if (fcp->dbg_tick != debug_tick)
25575 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025576 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025577 sourcing_lnum);
25578 fcp->dbg_tick = debug_tick;
25579 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025580#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025581 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025582 func_line_end(cookie);
25583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584
Bram Moolenaar05159a02005-02-26 23:04:13 +000025585 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025586 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25587 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588 retval = NULL;
25589 else
25590 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025591 /* Skip NULL lines (continuation lines). */
25592 while (fcp->linenr < gap->ga_len
25593 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25594 ++fcp->linenr;
25595 if (fcp->linenr >= gap->ga_len)
25596 retval = NULL;
25597 else
25598 {
25599 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25600 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025601#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025602 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025603 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025604#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025606 }
25607
25608 /* Did we encounter a breakpoint? */
25609 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25610 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025611 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025612 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025613 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 sourcing_lnum);
25615 fcp->dbg_tick = debug_tick;
25616 }
25617
25618 return retval;
25619}
25620
Bram Moolenaar05159a02005-02-26 23:04:13 +000025621#if defined(FEAT_PROFILE) || defined(PROTO)
25622/*
25623 * Called when starting to read a function line.
25624 * "sourcing_lnum" must be correct!
25625 * When skipping lines it may not actually be executed, but we won't find out
25626 * until later and we need to store the time now.
25627 */
25628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025629func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025630{
25631 funccall_T *fcp = (funccall_T *)cookie;
25632 ufunc_T *fp = fcp->func;
25633
25634 if (fp->uf_profiling && sourcing_lnum >= 1
25635 && sourcing_lnum <= fp->uf_lines.ga_len)
25636 {
25637 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025638 /* Skip continuation lines. */
25639 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25640 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025641 fp->uf_tml_execed = FALSE;
25642 profile_start(&fp->uf_tml_start);
25643 profile_zero(&fp->uf_tml_children);
25644 profile_get_wait(&fp->uf_tml_wait);
25645 }
25646}
25647
25648/*
25649 * Called when actually executing a function line.
25650 */
25651 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025652func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025653{
25654 funccall_T *fcp = (funccall_T *)cookie;
25655 ufunc_T *fp = fcp->func;
25656
25657 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25658 fp->uf_tml_execed = TRUE;
25659}
25660
25661/*
25662 * Called when done with a function line.
25663 */
25664 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025665func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025666{
25667 funccall_T *fcp = (funccall_T *)cookie;
25668 ufunc_T *fp = fcp->func;
25669
25670 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25671 {
25672 if (fp->uf_tml_execed)
25673 {
25674 ++fp->uf_tml_count[fp->uf_tml_idx];
25675 profile_end(&fp->uf_tml_start);
25676 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025677 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025678 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25679 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025680 }
25681 fp->uf_tml_idx = -1;
25682 }
25683}
25684#endif
25685
Bram Moolenaar071d4272004-06-13 20:20:40 +000025686/*
25687 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025688 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025689 */
25690 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025691func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025692{
Bram Moolenaar33570922005-01-25 22:26:29 +000025693 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025694
25695 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25696 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025697 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025698 || fcp->returned);
25699}
25700
25701/*
25702 * return TRUE if cookie indicates a function which "abort"s on errors.
25703 */
25704 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025705func_has_abort(
25706 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025708 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025709}
25710
25711#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25712typedef enum
25713{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025714 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25715 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25716 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717} var_flavour_T;
25718
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025719static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025720
25721 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025722var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723{
25724 char_u *p = varname;
25725
25726 if (ASCII_ISUPPER(*p))
25727 {
25728 while (*(++p))
25729 if (ASCII_ISLOWER(*p))
25730 return VAR_FLAVOUR_SESSION;
25731 return VAR_FLAVOUR_VIMINFO;
25732 }
25733 else
25734 return VAR_FLAVOUR_DEFAULT;
25735}
25736#endif
25737
25738#if defined(FEAT_VIMINFO) || defined(PROTO)
25739/*
25740 * Restore global vars that start with a capital from the viminfo file
25741 */
25742 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025743read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025744{
25745 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025746 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025747 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025748 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025749
25750 if (!writing && (find_viminfo_parameter('!') != NULL))
25751 {
25752 tab = vim_strchr(virp->vir_line + 1, '\t');
25753 if (tab != NULL)
25754 {
25755 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025756 switch (*tab)
25757 {
25758 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025759#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025760 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025761#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025762 case 'D': type = VAR_DICT; break;
25763 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025764 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025766
25767 tab = vim_strchr(tab, '\t');
25768 if (tab != NULL)
25769 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025770 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025771 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025772 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025774#ifdef FEAT_FLOAT
25775 else if (type == VAR_FLOAT)
25776 (void)string2float(tab + 1, &tv.vval.v_float);
25777#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025778 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025779 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025780 if (type == VAR_DICT || type == VAR_LIST)
25781 {
25782 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25783
25784 if (etv == NULL)
25785 /* Failed to parse back the dict or list, use it as a
25786 * string. */
25787 tv.v_type = VAR_STRING;
25788 else
25789 {
25790 vim_free(tv.vval.v_string);
25791 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025792 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025793 }
25794 }
25795
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025796 /* when in a function use global variables */
25797 save_funccal = current_funccal;
25798 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025799 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025800 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025801
25802 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025803 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025804 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25805 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025806 }
25807 }
25808 }
25809
25810 return viminfo_readline(virp);
25811}
25812
25813/*
25814 * Write global vars that start with a capital to the viminfo file
25815 */
25816 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025817write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818{
Bram Moolenaar33570922005-01-25 22:26:29 +000025819 hashitem_T *hi;
25820 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025821 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025822 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025823 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025824 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025825 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025826
25827 if (find_viminfo_parameter('!') == NULL)
25828 return;
25829
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025830 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025831
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025832 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025833 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025834 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025835 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025837 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025838 this_var = HI2DI(hi);
25839 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025840 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025841 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025842 {
25843 case VAR_STRING: s = "STR"; break;
25844 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025845 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025846 case VAR_DICT: s = "DIC"; break;
25847 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025848 case VAR_SPECIAL: s = "XPL"; break;
25849
25850 case VAR_UNKNOWN:
25851 case VAR_FUNC:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025852 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025853 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025854 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025855 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025856 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025857 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025858 if (p != NULL)
25859 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025860 vim_free(tofree);
25861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862 }
25863 }
25864}
25865#endif
25866
25867#if defined(FEAT_SESSION) || defined(PROTO)
25868 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025869store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870{
Bram Moolenaar33570922005-01-25 22:26:29 +000025871 hashitem_T *hi;
25872 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025873 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025874 char_u *p, *t;
25875
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025876 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025877 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025879 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025881 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025882 this_var = HI2DI(hi);
25883 if ((this_var->di_tv.v_type == VAR_NUMBER
25884 || this_var->di_tv.v_type == VAR_STRING)
25885 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025886 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025887 /* Escape special characters with a backslash. Turn a LF and
25888 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025889 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025890 (char_u *)"\\\"\n\r");
25891 if (p == NULL) /* out of memory */
25892 break;
25893 for (t = p; *t != NUL; ++t)
25894 if (*t == '\n')
25895 *t = 'n';
25896 else if (*t == '\r')
25897 *t = 'r';
25898 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025899 this_var->di_key,
25900 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25901 : ' ',
25902 p,
25903 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25904 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025905 || put_eol(fd) == FAIL)
25906 {
25907 vim_free(p);
25908 return FAIL;
25909 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 vim_free(p);
25911 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025912#ifdef FEAT_FLOAT
25913 else if (this_var->di_tv.v_type == VAR_FLOAT
25914 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25915 {
25916 float_T f = this_var->di_tv.vval.v_float;
25917 int sign = ' ';
25918
25919 if (f < 0)
25920 {
25921 f = -f;
25922 sign = '-';
25923 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025924 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025925 this_var->di_key, sign, f) < 0)
25926 || put_eol(fd) == FAIL)
25927 return FAIL;
25928 }
25929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 }
25931 }
25932 return OK;
25933}
25934#endif
25935
Bram Moolenaar661b1822005-07-28 22:36:45 +000025936/*
25937 * Display script name where an item was last set.
25938 * Should only be invoked when 'verbose' is non-zero.
25939 */
25940 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025941last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025942{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025943 char_u *p;
25944
Bram Moolenaar661b1822005-07-28 22:36:45 +000025945 if (scriptID != 0)
25946 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025947 p = home_replace_save(NULL, get_scriptname(scriptID));
25948 if (p != NULL)
25949 {
25950 verbose_enter();
25951 MSG_PUTS(_("\n\tLast set from "));
25952 MSG_PUTS(p);
25953 vim_free(p);
25954 verbose_leave();
25955 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025956 }
25957}
25958
Bram Moolenaard812df62008-11-09 12:46:09 +000025959/*
25960 * List v:oldfiles in a nice way.
25961 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025962 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025963ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025964{
25965 list_T *l = vimvars[VV_OLDFILES].vv_list;
25966 listitem_T *li;
25967 int nr = 0;
25968
25969 if (l == NULL)
25970 msg((char_u *)_("No old files"));
25971 else
25972 {
25973 msg_start();
25974 msg_scroll = TRUE;
25975 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25976 {
25977 msg_outnum((long)++nr);
25978 MSG_PUTS(": ");
25979 msg_outtrans(get_tv_string(&li->li_tv));
25980 msg_putchar('\n');
25981 out_flush(); /* output one line at a time */
25982 ui_breakcheck();
25983 }
25984 /* Assume "got_int" was set to truncate the listing. */
25985 got_int = FALSE;
25986
25987#ifdef FEAT_BROWSE_CMD
25988 if (cmdmod.browse)
25989 {
25990 quit_more = FALSE;
25991 nr = prompt_for_number(FALSE);
25992 msg_starthere();
25993 if (nr > 0)
25994 {
25995 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25996 (long)nr);
25997
25998 if (p != NULL)
25999 {
26000 p = expand_env_save(p);
26001 eap->arg = p;
26002 eap->cmdidx = CMD_edit;
26003 cmdmod.browse = FALSE;
26004 do_exedit(eap, NULL);
26005 vim_free(p);
26006 }
26007 }
26008 }
26009#endif
26010 }
26011}
26012
Bram Moolenaar53744302015-07-17 17:38:22 +020026013/* reset v:option_new, v:option_old and v:option_type */
26014 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010026015reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020026016{
26017 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
26018 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
26019 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
26020}
26021
26022
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023#endif /* FEAT_EVAL */
26024
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026026#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026027
26028#ifdef WIN3264
26029/*
26030 * Functions for ":8" filename modifier: get 8.3 version of a filename.
26031 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010026032static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
26033static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
26034static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035
26036/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026037 * Get the short path (8.3) for the filename in "fnamep".
26038 * Only works for a valid file name.
26039 * When the path gets longer "fnamep" is changed and the allocated buffer
26040 * is put in "bufp".
26041 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
26042 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 */
26044 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026045get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026046{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026047 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048 char_u *newbuf;
26049
26050 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026051 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026052 if (l > len - 1)
26053 {
26054 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026055 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 newbuf = vim_strnsave(*fnamep, l);
26057 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026058 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026059
26060 vim_free(*bufp);
26061 *fnamep = *bufp = newbuf;
26062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026063 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026064 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 }
26066
26067 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026068 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026069}
26070
26071/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026072 * Get the short path (8.3) for the filename in "fname". The converted
26073 * path is returned in "bufp".
26074 *
26075 * Some of the directories specified in "fname" may not exist. This function
26076 * will shorten the existing directories at the beginning of the path and then
26077 * append the remaining non-existing path.
26078 *
26079 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020026080 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026081 * bufp - Pointer to an allocated buffer for the filename.
26082 * fnamelen - Length of the filename pointed to by fname
26083 *
26084 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000026085 */
26086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026087shortpath_for_invalid_fname(
26088 char_u **fname,
26089 char_u **bufp,
26090 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026092 char_u *short_fname, *save_fname, *pbuf_unused;
26093 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026095 int old_len, len;
26096 int new_len, sfx_len;
26097 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026098
26099 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026100 old_len = *fnamelen;
26101 save_fname = vim_strnsave(*fname, old_len);
26102 pbuf_unused = NULL;
26103 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026104
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026105 endp = save_fname + old_len - 1; /* Find the end of the copy */
26106 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026108 /*
26109 * Try shortening the supplied path till it succeeds by removing one
26110 * directory at a time from the tail of the path.
26111 */
26112 len = 0;
26113 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026114 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026115 /* go back one path-separator */
26116 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26117 --endp;
26118 if (endp <= save_fname)
26119 break; /* processed the complete path */
26120
26121 /*
26122 * Replace the path separator with a NUL and try to shorten the
26123 * resulting path.
26124 */
26125 ch = *endp;
26126 *endp = 0;
26127 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026128 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026129 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26130 {
26131 retval = FAIL;
26132 goto theend;
26133 }
26134 *endp = ch; /* preserve the string */
26135
26136 if (len > 0)
26137 break; /* successfully shortened the path */
26138
26139 /* failed to shorten the path. Skip the path separator */
26140 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026141 }
26142
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026143 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026144 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026145 /*
26146 * Succeeded in shortening the path. Now concatenate the shortened
26147 * path with the remaining path at the tail.
26148 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026149
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026150 /* Compute the length of the new path. */
26151 sfx_len = (int)(save_endp - endp) + 1;
26152 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026153
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026154 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026156 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026157 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026158 /* There is not enough space in the currently allocated string,
26159 * copy it to a buffer big enough. */
26160 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026161 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026162 {
26163 retval = FAIL;
26164 goto theend;
26165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166 }
26167 else
26168 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026169 /* Transfer short_fname to the main buffer (it's big enough),
26170 * unless get_short_pathname() did its work in-place. */
26171 *fname = *bufp = save_fname;
26172 if (short_fname != save_fname)
26173 vim_strncpy(save_fname, short_fname, len);
26174 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026175 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026176
26177 /* concat the not-shortened part of the path */
26178 vim_strncpy(*fname + len, endp, sfx_len);
26179 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026181
26182theend:
26183 vim_free(pbuf_unused);
26184 vim_free(save_fname);
26185
26186 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026187}
26188
26189/*
26190 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026191 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192 */
26193 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026194shortpath_for_partial(
26195 char_u **fnamep,
26196 char_u **bufp,
26197 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026198{
26199 int sepcount, len, tflen;
26200 char_u *p;
26201 char_u *pbuf, *tfname;
26202 int hasTilde;
26203
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026204 /* Count up the path separators from the RHS.. so we know which part
26205 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026206 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026207 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026208 if (vim_ispathsep(*p))
26209 ++sepcount;
26210
26211 /* Need full path first (use expand_env() to remove a "~/") */
26212 hasTilde = (**fnamep == '~');
26213 if (hasTilde)
26214 pbuf = tfname = expand_env_save(*fnamep);
26215 else
26216 pbuf = tfname = FullName_save(*fnamep, FALSE);
26217
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026218 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026220 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26221 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026222
26223 if (len == 0)
26224 {
26225 /* Don't have a valid filename, so shorten the rest of the
26226 * path if we can. This CAN give us invalid 8.3 filenames, but
26227 * there's not a lot of point in guessing what it might be.
26228 */
26229 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026230 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26231 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026232 }
26233
26234 /* Count the paths backward to find the beginning of the desired string. */
26235 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026236 {
26237#ifdef FEAT_MBYTE
26238 if (has_mbyte)
26239 p -= mb_head_off(tfname, p);
26240#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026241 if (vim_ispathsep(*p))
26242 {
26243 if (sepcount == 0 || (hasTilde && sepcount == 1))
26244 break;
26245 else
26246 sepcount --;
26247 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026248 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 if (hasTilde)
26250 {
26251 --p;
26252 if (p >= tfname)
26253 *p = '~';
26254 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026255 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026256 }
26257 else
26258 ++p;
26259
26260 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26261 vim_free(*bufp);
26262 *fnamelen = (int)STRLEN(p);
26263 *bufp = pbuf;
26264 *fnamep = p;
26265
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026266 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026267}
26268#endif /* WIN3264 */
26269
26270/*
26271 * Adjust a filename, according to a string of modifiers.
26272 * *fnamep must be NUL terminated when called. When returning, the length is
26273 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026274 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026275 * When there is an error, *fnamep is set to NULL.
26276 */
26277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026278modify_fname(
26279 char_u *src, /* string with modifiers */
26280 int *usedlen, /* characters after src that are used */
26281 char_u **fnamep, /* file name so far */
26282 char_u **bufp, /* buffer for allocated file name or NULL */
26283 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026284{
26285 int valid = 0;
26286 char_u *tail;
26287 char_u *s, *p, *pbuf;
26288 char_u dirname[MAXPATHL];
26289 int c;
26290 int has_fullname = 0;
26291#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026292 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 int has_shortname = 0;
26294#endif
26295
26296repeat:
26297 /* ":p" - full path/file_name */
26298 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26299 {
26300 has_fullname = 1;
26301
26302 valid |= VALID_PATH;
26303 *usedlen += 2;
26304
26305 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26306 if ((*fnamep)[0] == '~'
26307#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26308 && ((*fnamep)[1] == '/'
26309# ifdef BACKSLASH_IN_FILENAME
26310 || (*fnamep)[1] == '\\'
26311# endif
26312 || (*fnamep)[1] == NUL)
26313
26314#endif
26315 )
26316 {
26317 *fnamep = expand_env_save(*fnamep);
26318 vim_free(*bufp); /* free any allocated file name */
26319 *bufp = *fnamep;
26320 if (*fnamep == NULL)
26321 return -1;
26322 }
26323
26324 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026325 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026326 {
26327 if (vim_ispathsep(*p)
26328 && p[1] == '.'
26329 && (p[2] == NUL
26330 || vim_ispathsep(p[2])
26331 || (p[2] == '.'
26332 && (p[3] == NUL || vim_ispathsep(p[3])))))
26333 break;
26334 }
26335
26336 /* FullName_save() is slow, don't use it when not needed. */
26337 if (*p != NUL || !vim_isAbsName(*fnamep))
26338 {
26339 *fnamep = FullName_save(*fnamep, *p != NUL);
26340 vim_free(*bufp); /* free any allocated file name */
26341 *bufp = *fnamep;
26342 if (*fnamep == NULL)
26343 return -1;
26344 }
26345
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026346#ifdef WIN3264
26347# if _WIN32_WINNT >= 0x0500
26348 if (vim_strchr(*fnamep, '~') != NULL)
26349 {
26350 /* Expand 8.3 filename to full path. Needed to make sure the same
26351 * file does not have two different names.
26352 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26353 p = alloc(_MAX_PATH + 1);
26354 if (p != NULL)
26355 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026356 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026357 {
26358 vim_free(*bufp);
26359 *bufp = *fnamep = p;
26360 }
26361 else
26362 vim_free(p);
26363 }
26364 }
26365# endif
26366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026367 /* Append a path separator to a directory. */
26368 if (mch_isdir(*fnamep))
26369 {
26370 /* Make room for one or two extra characters. */
26371 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26372 vim_free(*bufp); /* free any allocated file name */
26373 *bufp = *fnamep;
26374 if (*fnamep == NULL)
26375 return -1;
26376 add_pathsep(*fnamep);
26377 }
26378 }
26379
26380 /* ":." - path relative to the current directory */
26381 /* ":~" - path relative to the home directory */
26382 /* ":8" - shortname path - postponed till after */
26383 while (src[*usedlen] == ':'
26384 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26385 {
26386 *usedlen += 2;
26387 if (c == '8')
26388 {
26389#ifdef WIN3264
26390 has_shortname = 1; /* Postpone this. */
26391#endif
26392 continue;
26393 }
26394 pbuf = NULL;
26395 /* Need full path first (use expand_env() to remove a "~/") */
26396 if (!has_fullname)
26397 {
26398 if (c == '.' && **fnamep == '~')
26399 p = pbuf = expand_env_save(*fnamep);
26400 else
26401 p = pbuf = FullName_save(*fnamep, FALSE);
26402 }
26403 else
26404 p = *fnamep;
26405
26406 has_fullname = 0;
26407
26408 if (p != NULL)
26409 {
26410 if (c == '.')
26411 {
26412 mch_dirname(dirname, MAXPATHL);
26413 s = shorten_fname(p, dirname);
26414 if (s != NULL)
26415 {
26416 *fnamep = s;
26417 if (pbuf != NULL)
26418 {
26419 vim_free(*bufp); /* free any allocated file name */
26420 *bufp = pbuf;
26421 pbuf = NULL;
26422 }
26423 }
26424 }
26425 else
26426 {
26427 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26428 /* Only replace it when it starts with '~' */
26429 if (*dirname == '~')
26430 {
26431 s = vim_strsave(dirname);
26432 if (s != NULL)
26433 {
26434 *fnamep = s;
26435 vim_free(*bufp);
26436 *bufp = s;
26437 }
26438 }
26439 }
26440 vim_free(pbuf);
26441 }
26442 }
26443
26444 tail = gettail(*fnamep);
26445 *fnamelen = (int)STRLEN(*fnamep);
26446
26447 /* ":h" - head, remove "/file_name", can be repeated */
26448 /* Don't remove the first "/" or "c:\" */
26449 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26450 {
26451 valid |= VALID_HEAD;
26452 *usedlen += 2;
26453 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026454 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026455 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026456 *fnamelen = (int)(tail - *fnamep);
26457#ifdef VMS
26458 if (*fnamelen > 0)
26459 *fnamelen += 1; /* the path separator is part of the path */
26460#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026461 if (*fnamelen == 0)
26462 {
26463 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26464 p = vim_strsave((char_u *)".");
26465 if (p == NULL)
26466 return -1;
26467 vim_free(*bufp);
26468 *bufp = *fnamep = tail = p;
26469 *fnamelen = 1;
26470 }
26471 else
26472 {
26473 while (tail > s && !after_pathsep(s, tail))
26474 mb_ptr_back(*fnamep, tail);
26475 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476 }
26477
26478 /* ":8" - shortname */
26479 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26480 {
26481 *usedlen += 2;
26482#ifdef WIN3264
26483 has_shortname = 1;
26484#endif
26485 }
26486
26487#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026488 /*
26489 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026490 */
26491 if (has_shortname)
26492 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026493 /* Copy the string if it is shortened by :h and when it wasn't copied
26494 * yet, because we are going to change it in place. Avoids changing
26495 * the buffer name for "%:8". */
26496 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026497 {
26498 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026499 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026500 return -1;
26501 vim_free(*bufp);
26502 *bufp = *fnamep = p;
26503 }
26504
26505 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026506 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507 if (!has_fullname && !vim_isAbsName(*fnamep))
26508 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026509 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026510 return -1;
26511 }
26512 else
26513 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026514 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026515
Bram Moolenaardc935552011-08-17 15:23:23 +020026516 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026517 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026518 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026519 return -1;
26520
26521 if (l == 0)
26522 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026523 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026524 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026525 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026526 return -1;
26527 }
26528 *fnamelen = l;
26529 }
26530 }
26531#endif /* WIN3264 */
26532
26533 /* ":t" - tail, just the basename */
26534 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26535 {
26536 *usedlen += 2;
26537 *fnamelen -= (int)(tail - *fnamep);
26538 *fnamep = tail;
26539 }
26540
26541 /* ":e" - extension, can be repeated */
26542 /* ":r" - root, without extension, can be repeated */
26543 while (src[*usedlen] == ':'
26544 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26545 {
26546 /* find a '.' in the tail:
26547 * - for second :e: before the current fname
26548 * - otherwise: The last '.'
26549 */
26550 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26551 s = *fnamep - 2;
26552 else
26553 s = *fnamep + *fnamelen - 1;
26554 for ( ; s > tail; --s)
26555 if (s[0] == '.')
26556 break;
26557 if (src[*usedlen + 1] == 'e') /* :e */
26558 {
26559 if (s > tail)
26560 {
26561 *fnamelen += (int)(*fnamep - (s + 1));
26562 *fnamep = s + 1;
26563#ifdef VMS
26564 /* cut version from the extension */
26565 s = *fnamep + *fnamelen - 1;
26566 for ( ; s > *fnamep; --s)
26567 if (s[0] == ';')
26568 break;
26569 if (s > *fnamep)
26570 *fnamelen = s - *fnamep;
26571#endif
26572 }
26573 else if (*fnamep <= tail)
26574 *fnamelen = 0;
26575 }
26576 else /* :r */
26577 {
26578 if (s > tail) /* remove one extension */
26579 *fnamelen = (int)(s - *fnamep);
26580 }
26581 *usedlen += 2;
26582 }
26583
26584 /* ":s?pat?foo?" - substitute */
26585 /* ":gs?pat?foo?" - global substitute */
26586 if (src[*usedlen] == ':'
26587 && (src[*usedlen + 1] == 's'
26588 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26589 {
26590 char_u *str;
26591 char_u *pat;
26592 char_u *sub;
26593 int sep;
26594 char_u *flags;
26595 int didit = FALSE;
26596
26597 flags = (char_u *)"";
26598 s = src + *usedlen + 2;
26599 if (src[*usedlen + 1] == 'g')
26600 {
26601 flags = (char_u *)"g";
26602 ++s;
26603 }
26604
26605 sep = *s++;
26606 if (sep)
26607 {
26608 /* find end of pattern */
26609 p = vim_strchr(s, sep);
26610 if (p != NULL)
26611 {
26612 pat = vim_strnsave(s, (int)(p - s));
26613 if (pat != NULL)
26614 {
26615 s = p + 1;
26616 /* find end of substitution */
26617 p = vim_strchr(s, sep);
26618 if (p != NULL)
26619 {
26620 sub = vim_strnsave(s, (int)(p - s));
26621 str = vim_strnsave(*fnamep, *fnamelen);
26622 if (sub != NULL && str != NULL)
26623 {
26624 *usedlen = (int)(p + 1 - src);
26625 s = do_string_sub(str, pat, sub, flags);
26626 if (s != NULL)
26627 {
26628 *fnamep = s;
26629 *fnamelen = (int)STRLEN(s);
26630 vim_free(*bufp);
26631 *bufp = s;
26632 didit = TRUE;
26633 }
26634 }
26635 vim_free(sub);
26636 vim_free(str);
26637 }
26638 vim_free(pat);
26639 }
26640 }
26641 /* after using ":s", repeat all the modifiers */
26642 if (didit)
26643 goto repeat;
26644 }
26645 }
26646
Bram Moolenaar26df0922014-02-23 23:39:13 +010026647 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26648 {
26649 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26650 if (p == NULL)
26651 return -1;
26652 vim_free(*bufp);
26653 *bufp = *fnamep = p;
26654 *fnamelen = (int)STRLEN(p);
26655 *usedlen += 2;
26656 }
26657
Bram Moolenaar071d4272004-06-13 20:20:40 +000026658 return valid;
26659}
26660
26661/*
26662 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26663 * "flags" can be "g" to do a global substitute.
26664 * Returns an allocated string, NULL for error.
26665 */
26666 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026667do_string_sub(
26668 char_u *str,
26669 char_u *pat,
26670 char_u *sub,
26671 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026672{
26673 int sublen;
26674 regmatch_T regmatch;
26675 int i;
26676 int do_all;
26677 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026678 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026679 garray_T ga;
26680 char_u *ret;
26681 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026682 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026683
26684 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26685 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026686 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026687
26688 ga_init2(&ga, 1, 200);
26689
26690 do_all = (flags[0] == 'g');
26691
26692 regmatch.rm_ic = p_ic;
26693 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26694 if (regmatch.regprog != NULL)
26695 {
26696 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026697 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026698 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26699 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026700 /* Skip empty match except for first match. */
26701 if (regmatch.startp[0] == regmatch.endp[0])
26702 {
26703 if (zero_width == regmatch.startp[0])
26704 {
26705 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026706 i = MB_PTR2LEN(tail);
26707 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26708 (size_t)i);
26709 ga.ga_len += i;
26710 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026711 continue;
26712 }
26713 zero_width = regmatch.startp[0];
26714 }
26715
Bram Moolenaar071d4272004-06-13 20:20:40 +000026716 /*
26717 * Get some space for a temporary buffer to do the substitution
26718 * into. It will contain:
26719 * - The text up to where the match is.
26720 * - The substituted text.
26721 * - The text after the match.
26722 */
26723 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026724 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026725 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26726 {
26727 ga_clear(&ga);
26728 break;
26729 }
26730
26731 /* copy the text up to where the match is */
26732 i = (int)(regmatch.startp[0] - tail);
26733 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26734 /* add the substituted text */
26735 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26736 + ga.ga_len + i, TRUE, TRUE, FALSE);
26737 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026738 tail = regmatch.endp[0];
26739 if (*tail == NUL)
26740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026741 if (!do_all)
26742 break;
26743 }
26744
26745 if (ga.ga_data != NULL)
26746 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26747
Bram Moolenaar473de612013-06-08 18:19:48 +020026748 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026749 }
26750
26751 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26752 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026753 if (p_cpo == empty_option)
26754 p_cpo = save_cpo;
26755 else
26756 /* Darn, evaluating {sub} expression changed the value. */
26757 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026758
26759 return ret;
26760}
26761
26762#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */