blob: 55d61e79bdd03a39c7bee3f3414c95e7930d0612 [file] [log] [blame]
Bram Moolenaar071d4272004-06-13 20:20:40 +00001/* vi:set ts=8 sts=4 sw=4:
2 *
3 * VIM - Vi IMproved by Bram Moolenaar
4 *
5 * Do ":help uganda" in Vim to read copying and usage conditions.
6 * Do ":help credits" in Vim to see a list of people who contributed.
7 * See README.txt for an overview of the Vim source code.
8 */
9
10/*
11 * eval.c: Expression evaluation.
12 */
Bram Moolenaarfefecb02016-02-27 21:27:20 +010013#define USING_FLOAT_STUFF
Bram Moolenaar071d4272004-06-13 20:20:40 +000014
15#include "vim.h"
16
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017#if defined(FEAT_EVAL) || defined(PROTO)
18
Bram Moolenaar071d4272004-06-13 20:20:40 +000019#ifdef AMIGA
20# include <time.h> /* for strftime() */
21#endif
22
Bram Moolenaar314f11d2010-08-09 22:07:08 +020023#ifdef VMS
24# include <float.h>
25#endif
26
Bram Moolenaar071d4272004-06-13 20:20:40 +000027#ifdef MACOS
28# include <time.h> /* for time_t */
29#endif
30
Bram Moolenaar33570922005-01-25 22:26:29 +000031#define DICT_MAXNEST 100 /* maximum nesting of lists and dicts */
Bram Moolenaar071d4272004-06-13 20:20:40 +000032
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000033#define DO_NOT_FREE_CNT 99999 /* refcount for dict or list that should not
34 be freed. */
35
Bram Moolenaar071d4272004-06-13 20:20:40 +000036/*
Bram Moolenaar33570922005-01-25 22:26:29 +000037 * In a hashtab item "hi_key" points to "di_key" in a dictitem.
38 * This avoids adding a pointer to the hashtab item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000039 * DI2HIKEY() converts a dictitem pointer to a hashitem key pointer.
40 * HIKEY2DI() converts a hashitem key pointer to a dictitem pointer.
41 * HI2DI() converts a hashitem pointer to a dictitem pointer.
42 */
Bram Moolenaar33570922005-01-25 22:26:29 +000043static dictitem_T dumdi;
Bram Moolenaara7043832005-01-21 11:56:39 +000044#define DI2HIKEY(di) ((di)->di_key)
Bram Moolenaar33570922005-01-25 22:26:29 +000045#define HIKEY2DI(p) ((dictitem_T *)(p - (dumdi.di_key - (char_u *)&dumdi)))
Bram Moolenaara7043832005-01-21 11:56:39 +000046#define HI2DI(hi) HIKEY2DI((hi)->hi_key)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000047
48/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000049 * Structure returned by get_lval() and used by set_var_lval().
50 * For a plain name:
51 * "name" points to the variable name.
52 * "exp_name" is NULL.
53 * "tv" is NULL
54 * For a magic braces name:
55 * "name" points to the expanded variable name.
56 * "exp_name" is non-NULL, to be freed later.
57 * "tv" is NULL
58 * For an index in a list:
59 * "name" points to the (expanded) variable name.
60 * "exp_name" NULL or non-NULL, to be freed later.
61 * "tv" points to the (first) list item value
62 * "li" points to the (first) list item
63 * "range", "n1", "n2" and "empty2" indicate what items are used.
64 * For an existing Dict item:
65 * "name" points to the (expanded) variable name.
66 * "exp_name" NULL or non-NULL, to be freed later.
67 * "tv" points to the dict item value
68 * "newkey" is NULL
69 * For a non-existing Dict item:
70 * "name" points to the (expanded) variable name.
71 * "exp_name" NULL or non-NULL, to be freed later.
Bram Moolenaar33570922005-01-25 22:26:29 +000072 * "tv" points to the Dictionary typval_T
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000073 * "newkey" is the key for the new item.
74 */
75typedef struct lval_S
76{
77 char_u *ll_name; /* start of variable name (can be NULL) */
78 char_u *ll_exp_name; /* NULL or expanded name in allocated memory. */
Bram Moolenaar33570922005-01-25 22:26:29 +000079 typval_T *ll_tv; /* Typeval of item being used. If "newkey"
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000080 isn't NULL it's the Dict to which to add
81 the item. */
Bram Moolenaar33570922005-01-25 22:26:29 +000082 listitem_T *ll_li; /* The list item or NULL. */
83 list_T *ll_list; /* The list or NULL. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000084 int ll_range; /* TRUE when a [i:j] range was used */
85 long ll_n1; /* First index for list */
86 long ll_n2; /* Second index for list range */
87 int ll_empty2; /* Second index is empty: [i:] */
Bram Moolenaar33570922005-01-25 22:26:29 +000088 dict_T *ll_dict; /* The Dictionary or NULL */
89 dictitem_T *ll_di; /* The dictitem or NULL */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000090 char_u *ll_newkey; /* New key for Dict in alloc. mem or NULL. */
Bram Moolenaar33570922005-01-25 22:26:29 +000091} lval_T;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000092
Bram Moolenaarc70646c2005-01-04 21:52:38 +000093static char *e_letunexp = N_("E18: Unexpected characters in :let");
Bram Moolenaare49b69a2005-01-08 16:11:57 +000094static char *e_listidx = N_("E684: list index out of range: %ld");
Bram Moolenaarc70646c2005-01-04 21:52:38 +000095static char *e_undefvar = N_("E121: Undefined variable: %s");
96static char *e_missbrac = N_("E111: Missing ']'");
Bram Moolenaar8c711452005-01-14 21:53:12 +000097static char *e_listarg = N_("E686: Argument of %s must be a List");
Bram Moolenaar13fcaaf2005-04-15 21:13:42 +000098static char *e_listdictarg = N_("E712: Argument of %s must be a List or Dictionary");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000099static char *e_listreq = N_("E714: List required");
100static char *e_dictreq = N_("E715: Dictionary required");
Bram Moolenaar8c711452005-01-14 21:53:12 +0000101static char *e_toomanyarg = N_("E118: Too many arguments for function: %s");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000102static char *e_dictkey = N_("E716: Key not present in Dictionary: %s");
103static char *e_funcexts = N_("E122: Function %s already exists, add ! to replace it");
104static char *e_funcdict = N_("E717: Dictionary entry already exists");
105static char *e_funcref = N_("E718: Funcref required");
106static char *e_dictrange = N_("E719: Cannot use [:] with a Dictionary");
107static char *e_letwrong = N_("E734: Wrong variable type for %s=");
Bram Moolenaar05159a02005-02-26 23:04:13 +0000108static char *e_nofunc = N_("E130: Unknown function: %s");
Bram Moolenaar92124a32005-06-17 22:03:40 +0000109static char *e_illvar = N_("E461: Illegal variable name: %s");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200110#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +0200111static char *e_float_as_string = N_("E806: using Float as a String");
Bram Moolenaar8c0e3222013-06-16 17:32:40 +0200112#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000113
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +0100114#define NAMESPACE_CHAR (char_u *)"abglstvw"
115
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200116static dictitem_T globvars_var; /* variable used for g: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000117#define globvarht globvardict.dv_hashtab
Bram Moolenaar071d4272004-06-13 20:20:40 +0000118
119/*
Bram Moolenaar532c7802005-01-27 14:44:31 +0000120 * Old Vim variables such as "v:version" are also available without the "v:".
121 * Also in functions. We need a special hashtable for them.
122 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000123static hashtab_T compat_hashtab;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000124
125/*
Bram Moolenaard9fba312005-06-26 22:34:35 +0000126 * When recursively copying lists and dicts we need to remember which ones we
127 * have done to avoid endless recursiveness. This unique ID is used for that.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000128 * The last bit is used for previous_funccal, ignored when comparing.
Bram Moolenaard9fba312005-06-26 22:34:35 +0000129 */
130static int current_copyID = 0;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +0000131#define COPYID_INC 2
132#define COPYID_MASK (~0x1)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000133
Bram Moolenaar8502c702014-06-17 12:51:16 +0200134/* Abort conversion to string after a recursion error. */
135static int did_echo_string_emsg = FALSE;
136
Bram Moolenaard9fba312005-06-26 22:34:35 +0000137/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000138 * Array to hold the hashtab with variables local to each sourced script.
139 * Each item holds a variable (nameless) that points to the dict_T.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000140 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000141typedef struct
142{
143 dictitem_T sv_var;
144 dict_T sv_dict;
145} scriptvar_T;
146
Bram Moolenaar9577c3e2010-05-14 12:16:25 +0200147static garray_T ga_scripts = {0, 0, sizeof(scriptvar_T *), 4, NULL};
148#define SCRIPT_SV(id) (((scriptvar_T **)ga_scripts.ga_data)[(id) - 1])
149#define SCRIPT_VARS(id) (SCRIPT_SV(id)->sv_dict.dv_hashtab)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000150
151static int echo_attr = 0; /* attributes used for ":echo" */
152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000153/* Values for trans_function_name() argument: */
154#define TFN_INT 1 /* internal function name OK */
155#define TFN_QUIET 2 /* no error messages */
Bram Moolenaar6d977d62014-01-14 15:24:39 +0100156#define TFN_NO_AUTOLOAD 4 /* do not use script autoloading */
157
158/* Values for get_lval() flags argument: */
159#define GLV_QUIET TFN_QUIET /* no error messages */
160#define GLV_NO_AUTOLOAD TFN_NO_AUTOLOAD /* do not use script autoloading */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000161
Bram Moolenaar071d4272004-06-13 20:20:40 +0000162/*
163 * Structure to hold info for a user function.
164 */
165typedef struct ufunc ufunc_T;
166
167struct ufunc
168{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000169 int uf_varargs; /* variable nr of arguments */
170 int uf_flags;
171 int uf_calls; /* nr of active calls */
172 garray_T uf_args; /* arguments */
173 garray_T uf_lines; /* function lines */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000174#ifdef FEAT_PROFILE
175 int uf_profiling; /* TRUE when func is being profiled */
176 /* profiling the function as a whole */
177 int uf_tm_count; /* nr of calls */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000178 proftime_T uf_tm_total; /* time spent in function + children */
179 proftime_T uf_tm_self; /* time spent in function itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000180 proftime_T uf_tm_children; /* time spent in children this call */
181 /* profiling the function per line */
182 int *uf_tml_count; /* nr of times line was executed */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000183 proftime_T *uf_tml_total; /* time spent in a line + children */
184 proftime_T *uf_tml_self; /* time spent in a line itself */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000185 proftime_T uf_tml_start; /* start time for current line */
186 proftime_T uf_tml_children; /* time spent in children for this line */
187 proftime_T uf_tml_wait; /* start wait time for current line */
188 int uf_tml_idx; /* index of line being timed; -1 if none */
189 int uf_tml_execed; /* line being timed was executed */
190#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000191 scid_T uf_script_ID; /* ID of script where function was defined,
Bram Moolenaar071d4272004-06-13 20:20:40 +0000192 used for s: variables */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000193 int uf_refcount; /* for numbered function: reference count */
194 char_u uf_name[1]; /* name of function (actually longer); can
195 start with <SNR>123_ (<SNR> is K_SPECIAL
196 KS_EXTRA KE_SNR) */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000197};
198
199/* function flags */
200#define FC_ABORT 1 /* abort function on error */
201#define FC_RANGE 2 /* function accepts range */
Bram Moolenaare9a41262005-01-15 22:18:47 +0000202#define FC_DICT 4 /* Dict function, uses "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000203
204/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000205 * All user-defined functions are found in this hashtable.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000206 */
Bram Moolenaar4debb442005-06-01 21:57:40 +0000207static hashtab_T func_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000208
Bram Moolenaarc9b4b052006-04-30 18:54:39 +0000209/* The names of packages that once were loaded are remembered. */
Bram Moolenaaraa35dd12006-04-29 22:03:41 +0000210static garray_T ga_loaded = {0, 0, sizeof(char_u *), 4, NULL};
211
Bram Moolenaar5f436fc2016-03-22 22:34:03 +0100212/* List heads for garbage collection. Although there can be a reference loop
213 * from partial to dict to partial, we don't need to keep track of the partial,
214 * since it will get freed when the dict is unused and gets freed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000215static dict_T *first_dict = NULL; /* list of all dicts */
216static list_T *first_list = NULL; /* list of all lists */
217
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000218/* From user function to hashitem and back. */
219static ufunc_T dumuf;
220#define UF2HIKEY(fp) ((fp)->uf_name)
221#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
222#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
223
224#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
225#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000226
Bram Moolenaar33570922005-01-25 22:26:29 +0000227#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
228#define VAR_SHORT_LEN 20 /* short variable name length */
229#define FIXVAR_CNT 12 /* number of fixed variables */
230
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000232typedef struct funccall_S funccall_T;
233
234struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000235{
236 ufunc_T *func; /* function being called */
237 int linenr; /* next line to be executed */
238 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000239 struct /* fixed variables for arguments */
240 {
241 dictitem_T var; /* variable (without room for name) */
242 char_u room[VAR_SHORT_LEN]; /* room for the name */
243 } fixvar[FIXVAR_CNT];
244 dict_T l_vars; /* l: local function variables */
245 dictitem_T l_vars_var; /* variable for l: scope */
246 dict_T l_avars; /* a: argument variables */
247 dictitem_T l_avars_var; /* variable for a: scope */
248 list_T l_varlist; /* list for a:000 */
249 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
250 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000251 linenr_T breakpoint; /* next line with breakpoint or zero */
252 int dbg_tick; /* debug_tick when breakpoint was set */
253 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000254#ifdef FEAT_PROFILE
255 proftime_T prof_child; /* time spent in a child */
256#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000257 funccall_T *caller; /* calling function or NULL */
258};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259
260/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000261 * Info used by a ":for" loop.
262 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000263typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000264{
265 int fi_semicolon; /* TRUE if ending in '; var]' */
266 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000267 listwatch_T fi_lw; /* keep an eye on the item used. */
268 list_T *fi_list; /* list being used */
269} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000270
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000271/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000272 * Struct used by trans_function_name()
273 */
274typedef struct
275{
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000277 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000278 dictitem_T *fd_di; /* Dictionary item used */
279} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000280
Bram Moolenaara7043832005-01-21 11:56:39 +0000281
282/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000283 * Array to hold the value of v: variables.
284 * The value is in a dictitem, so that it can also be used in the v: scope.
285 * The reason to use this table anyway is for very quick access to the
286 * variables with the VV_ defines.
287 */
288#include "version.h"
289
290/* values for vv_flags: */
291#define VV_COMPAT 1 /* compatible, also used without "v:" */
292#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000293#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000295#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000296
297static struct vimvar
298{
299 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000300 dictitem_T vv_di; /* value and name for key */
301 char vv_filler[16]; /* space for LONGEST name below!!! */
302 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
303} vimvars[VV_LEN] =
304{
305 /*
306 * The order here must match the VV_ defines in vim.h!
307 * Initializing a union does not work, leave tv.vval empty to get zero's.
308 */
309 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
310 {VV_NAME("count1", VAR_NUMBER), VV_RO},
311 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
312 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
313 {VV_NAME("warningmsg", VAR_STRING), 0},
314 {VV_NAME("statusmsg", VAR_STRING), 0},
315 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
317 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
318 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
319 {VV_NAME("termresponse", VAR_STRING), VV_RO},
320 {VV_NAME("fname", VAR_STRING), VV_RO},
321 {VV_NAME("lang", VAR_STRING), VV_RO},
322 {VV_NAME("lc_time", VAR_STRING), VV_RO},
323 {VV_NAME("ctype", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
325 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
326 {VV_NAME("fname_in", VAR_STRING), VV_RO},
327 {VV_NAME("fname_out", VAR_STRING), VV_RO},
328 {VV_NAME("fname_new", VAR_STRING), VV_RO},
329 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
330 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
331 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
334 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
335 {VV_NAME("progname", VAR_STRING), VV_RO},
336 {VV_NAME("servername", VAR_STRING), VV_RO},
337 {VV_NAME("dying", VAR_NUMBER), VV_RO},
338 {VV_NAME("exception", VAR_STRING), VV_RO},
339 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
340 {VV_NAME("register", VAR_STRING), VV_RO},
341 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
342 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000343 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
344 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000345 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000346 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
347 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000348 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
352 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000353 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000354 {VV_NAME("swapname", VAR_STRING), VV_RO},
355 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000356 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200357 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000358 {VV_NAME("mouse_win", VAR_NUMBER), 0},
359 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
360 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000361 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000362 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100363 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000364 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200365 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200366 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200367 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200368 {VV_NAME("option_new", VAR_STRING), VV_RO},
369 {VV_NAME("option_old", VAR_STRING), VV_RO},
370 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100371 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100372 {VV_NAME("false", VAR_SPECIAL), VV_RO},
373 {VV_NAME("true", VAR_SPECIAL), VV_RO},
374 {VV_NAME("null", VAR_SPECIAL), VV_RO},
375 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000376};
377
378/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000379#define vv_type vv_di.di_tv.v_type
380#define vv_nr vv_di.di_tv.vval.v_number
381#define vv_float vv_di.di_tv.vval.v_float
382#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000383#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200384#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000385#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000386
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200387static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000388#define vimvarht vimvardict.dv_hashtab
389
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100390static void prepare_vimvar(int idx, typval_T *save_tv);
391static void restore_vimvar(int idx, typval_T *save_tv);
392static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
393static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
394static char_u *skip_var_one(char_u *arg);
395static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
396static void list_glob_vars(int *first);
397static void list_buf_vars(int *first);
398static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000401#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100402static void list_vim_vars(int *first);
403static void list_script_vars(int *first);
404static void list_func_vars(int *first);
405static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
406static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
407static int check_changedtick(char_u *arg);
408static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
409static void clear_lval(lval_T *lp);
410static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
411static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
412static void list_fix_watch(list_T *l, listitem_T *item);
413static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
414static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
415static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
416static void item_lock(typval_T *tv, int deep, int lock);
417static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000418
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100419static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
420static int eval1(char_u **arg, typval_T *rettv, int evaluate);
421static int eval2(char_u **arg, typval_T *rettv, int evaluate);
422static int eval3(char_u **arg, typval_T *rettv, int evaluate);
423static int eval4(char_u **arg, typval_T *rettv, int evaluate);
424static int eval5(char_u **arg, typval_T *rettv, int evaluate);
425static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
426static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000427
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100428static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
429static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
431static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
432static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
433static long list_len(list_T *l);
434static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
435static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
436static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
437static long list_find_nr(list_T *l, long idx, int *errorp);
438static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100439static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
440static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
441static list_T *list_copy(list_T *orig, int deep, int copyID);
442static char_u *list2string(typval_T *tv, int copyID);
443static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
444static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
445static int free_unref_items(int copyID);
446static dictitem_T *dictitem_copy(dictitem_T *org);
447static void dictitem_remove(dict_T *dict, dictitem_T *item);
448static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
449static long dict_len(dict_T *d);
450static char_u *dict2string(typval_T *tv, int copyID);
451static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
452static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
453static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
454static char_u *string_quote(char_u *str, int function);
455static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
456static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100457static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
458static 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, partial_T *partial, dict_T *selfdict);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100459static void emsg_funcname(char *ermsg, char_u *name);
460static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000461
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000462#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100463static void f_abs(typval_T *argvars, typval_T *rettv);
464static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000465#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100466static void f_add(typval_T *argvars, typval_T *rettv);
467static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
468static void f_and(typval_T *argvars, typval_T *rettv);
469static void f_append(typval_T *argvars, typval_T *rettv);
470static void f_argc(typval_T *argvars, typval_T *rettv);
471static void f_argidx(typval_T *argvars, typval_T *rettv);
472static void f_arglistid(typval_T *argvars, typval_T *rettv);
473static void f_argv(typval_T *argvars, typval_T *rettv);
474static void f_assert_equal(typval_T *argvars, typval_T *rettv);
475static void f_assert_exception(typval_T *argvars, typval_T *rettv);
476static void f_assert_fails(typval_T *argvars, typval_T *rettv);
477static void f_assert_false(typval_T *argvars, typval_T *rettv);
478static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000479#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100480static void f_asin(typval_T *argvars, typval_T *rettv);
481static void f_atan(typval_T *argvars, typval_T *rettv);
482static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000483#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100484static void f_browse(typval_T *argvars, typval_T *rettv);
485static void f_browsedir(typval_T *argvars, typval_T *rettv);
486static void f_bufexists(typval_T *argvars, typval_T *rettv);
487static void f_buflisted(typval_T *argvars, typval_T *rettv);
488static void f_bufloaded(typval_T *argvars, typval_T *rettv);
489static void f_bufname(typval_T *argvars, typval_T *rettv);
490static void f_bufnr(typval_T *argvars, typval_T *rettv);
491static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
492static void f_byte2line(typval_T *argvars, typval_T *rettv);
493static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
494static void f_byteidx(typval_T *argvars, typval_T *rettv);
495static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
496static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100498static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000499#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100500#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100501static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100502static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
503static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100504static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100505static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100506static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100507static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
509static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100510static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100511static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100512static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
513static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100514static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100515static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100516#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100517static void f_changenr(typval_T *argvars, typval_T *rettv);
518static void f_char2nr(typval_T *argvars, typval_T *rettv);
519static void f_cindent(typval_T *argvars, typval_T *rettv);
520static void f_clearmatches(typval_T *argvars, typval_T *rettv);
521static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000522#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100523static void f_complete(typval_T *argvars, typval_T *rettv);
524static void f_complete_add(typval_T *argvars, typval_T *rettv);
525static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000526#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_confirm(typval_T *argvars, typval_T *rettv);
528static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_cos(typval_T *argvars, typval_T *rettv);
531static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100533static void f_count(typval_T *argvars, typval_T *rettv);
534static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
535static void f_cursor(typval_T *argsvars, typval_T *rettv);
536static void f_deepcopy(typval_T *argvars, typval_T *rettv);
537static void f_delete(typval_T *argvars, typval_T *rettv);
538static void f_did_filetype(typval_T *argvars, typval_T *rettv);
539static void f_diff_filler(typval_T *argvars, typval_T *rettv);
540static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100541static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100542static void f_empty(typval_T *argvars, typval_T *rettv);
543static void f_escape(typval_T *argvars, typval_T *rettv);
544static void f_eval(typval_T *argvars, typval_T *rettv);
545static void f_eventhandler(typval_T *argvars, typval_T *rettv);
546static void f_executable(typval_T *argvars, typval_T *rettv);
547static void f_exepath(typval_T *argvars, typval_T *rettv);
548static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200549#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100550static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200551#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100552static void f_expand(typval_T *argvars, typval_T *rettv);
553static void f_extend(typval_T *argvars, typval_T *rettv);
554static void f_feedkeys(typval_T *argvars, typval_T *rettv);
555static void f_filereadable(typval_T *argvars, typval_T *rettv);
556static void f_filewritable(typval_T *argvars, typval_T *rettv);
557static void f_filter(typval_T *argvars, typval_T *rettv);
558static void f_finddir(typval_T *argvars, typval_T *rettv);
559static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000560#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100561static void f_float2nr(typval_T *argvars, typval_T *rettv);
562static void f_floor(typval_T *argvars, typval_T *rettv);
563static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000564#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100565static void f_fnameescape(typval_T *argvars, typval_T *rettv);
566static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
567static void f_foldclosed(typval_T *argvars, typval_T *rettv);
568static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
569static void f_foldlevel(typval_T *argvars, typval_T *rettv);
570static void f_foldtext(typval_T *argvars, typval_T *rettv);
571static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
572static void f_foreground(typval_T *argvars, typval_T *rettv);
573static void f_function(typval_T *argvars, typval_T *rettv);
574static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
575static void f_get(typval_T *argvars, typval_T *rettv);
576static void f_getbufline(typval_T *argvars, typval_T *rettv);
577static void f_getbufvar(typval_T *argvars, typval_T *rettv);
578static void f_getchar(typval_T *argvars, typval_T *rettv);
579static void f_getcharmod(typval_T *argvars, typval_T *rettv);
580static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
581static void f_getcmdline(typval_T *argvars, typval_T *rettv);
582static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
583static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
584static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
585static void f_getcwd(typval_T *argvars, typval_T *rettv);
586static void f_getfontname(typval_T *argvars, typval_T *rettv);
587static void f_getfperm(typval_T *argvars, typval_T *rettv);
588static void f_getfsize(typval_T *argvars, typval_T *rettv);
589static void f_getftime(typval_T *argvars, typval_T *rettv);
590static void f_getftype(typval_T *argvars, typval_T *rettv);
591static void f_getline(typval_T *argvars, typval_T *rettv);
592static void f_getmatches(typval_T *argvars, typval_T *rettv);
593static void f_getpid(typval_T *argvars, typval_T *rettv);
594static void f_getcurpos(typval_T *argvars, typval_T *rettv);
595static void f_getpos(typval_T *argvars, typval_T *rettv);
596static void f_getqflist(typval_T *argvars, typval_T *rettv);
597static void f_getreg(typval_T *argvars, typval_T *rettv);
598static void f_getregtype(typval_T *argvars, typval_T *rettv);
599static void f_gettabvar(typval_T *argvars, typval_T *rettv);
600static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
601static void f_getwinposx(typval_T *argvars, typval_T *rettv);
602static void f_getwinposy(typval_T *argvars, typval_T *rettv);
603static void f_getwinvar(typval_T *argvars, typval_T *rettv);
604static void f_glob(typval_T *argvars, typval_T *rettv);
605static void f_globpath(typval_T *argvars, typval_T *rettv);
606static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
607static void f_has(typval_T *argvars, typval_T *rettv);
608static void f_has_key(typval_T *argvars, typval_T *rettv);
609static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
610static void f_hasmapto(typval_T *argvars, typval_T *rettv);
611static void f_histadd(typval_T *argvars, typval_T *rettv);
612static void f_histdel(typval_T *argvars, typval_T *rettv);
613static void f_histget(typval_T *argvars, typval_T *rettv);
614static void f_histnr(typval_T *argvars, typval_T *rettv);
615static void f_hlID(typval_T *argvars, typval_T *rettv);
616static void f_hlexists(typval_T *argvars, typval_T *rettv);
617static void f_hostname(typval_T *argvars, typval_T *rettv);
618static void f_iconv(typval_T *argvars, typval_T *rettv);
619static void f_indent(typval_T *argvars, typval_T *rettv);
620static void f_index(typval_T *argvars, typval_T *rettv);
621static void f_input(typval_T *argvars, typval_T *rettv);
622static void f_inputdialog(typval_T *argvars, typval_T *rettv);
623static void f_inputlist(typval_T *argvars, typval_T *rettv);
624static void f_inputrestore(typval_T *argvars, typval_T *rettv);
625static void f_inputsave(typval_T *argvars, typval_T *rettv);
626static void f_inputsecret(typval_T *argvars, typval_T *rettv);
627static void f_insert(typval_T *argvars, typval_T *rettv);
628static void f_invert(typval_T *argvars, typval_T *rettv);
629static void f_isdirectory(typval_T *argvars, typval_T *rettv);
630static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100631#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
632static void f_isnan(typval_T *argvars, typval_T *rettv);
633#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100634static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100635#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100636static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100637static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100638static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100639static void f_job_start(typval_T *argvars, typval_T *rettv);
640static void f_job_stop(typval_T *argvars, typval_T *rettv);
641static void f_job_status(typval_T *argvars, typval_T *rettv);
642#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100643static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100644static void f_js_decode(typval_T *argvars, typval_T *rettv);
645static void f_js_encode(typval_T *argvars, typval_T *rettv);
646static void f_json_decode(typval_T *argvars, typval_T *rettv);
647static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100648static void f_keys(typval_T *argvars, typval_T *rettv);
649static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
650static void f_len(typval_T *argvars, typval_T *rettv);
651static void f_libcall(typval_T *argvars, typval_T *rettv);
652static void f_libcallnr(typval_T *argvars, typval_T *rettv);
653static void f_line(typval_T *argvars, typval_T *rettv);
654static void f_line2byte(typval_T *argvars, typval_T *rettv);
655static void f_lispindent(typval_T *argvars, typval_T *rettv);
656static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100658static void f_log(typval_T *argvars, typval_T *rettv);
659static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000660#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200661#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100662static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200663#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100664static void f_map(typval_T *argvars, typval_T *rettv);
665static void f_maparg(typval_T *argvars, typval_T *rettv);
666static void f_mapcheck(typval_T *argvars, typval_T *rettv);
667static void f_match(typval_T *argvars, typval_T *rettv);
668static void f_matchadd(typval_T *argvars, typval_T *rettv);
669static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
670static void f_matcharg(typval_T *argvars, typval_T *rettv);
671static void f_matchdelete(typval_T *argvars, typval_T *rettv);
672static void f_matchend(typval_T *argvars, typval_T *rettv);
673static void f_matchlist(typval_T *argvars, typval_T *rettv);
674static void f_matchstr(typval_T *argvars, typval_T *rettv);
675static void f_max(typval_T *argvars, typval_T *rettv);
676static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000677#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100678static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000679#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100681#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100683#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
685static void f_nr2char(typval_T *argvars, typval_T *rettv);
686static void f_or(typval_T *argvars, typval_T *rettv);
687static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100688#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100690#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000691#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100692static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
695static void f_printf(typval_T *argvars, typval_T *rettv);
696static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200697#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
700#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100701static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200702#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_range(typval_T *argvars, typval_T *rettv);
704static void f_readfile(typval_T *argvars, typval_T *rettv);
705static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100706#ifdef FEAT_FLOAT
707static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
708#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100709static void f_reltimestr(typval_T *argvars, typval_T *rettv);
710static void f_remote_expr(typval_T *argvars, typval_T *rettv);
711static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
712static void f_remote_peek(typval_T *argvars, typval_T *rettv);
713static void f_remote_read(typval_T *argvars, typval_T *rettv);
714static void f_remote_send(typval_T *argvars, typval_T *rettv);
715static void f_remove(typval_T *argvars, typval_T *rettv);
716static void f_rename(typval_T *argvars, typval_T *rettv);
717static void f_repeat(typval_T *argvars, typval_T *rettv);
718static void f_resolve(typval_T *argvars, typval_T *rettv);
719static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000720#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100721static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_screenattr(typval_T *argvars, typval_T *rettv);
724static void f_screenchar(typval_T *argvars, typval_T *rettv);
725static void f_screencol(typval_T *argvars, typval_T *rettv);
726static void f_screenrow(typval_T *argvars, typval_T *rettv);
727static void f_search(typval_T *argvars, typval_T *rettv);
728static void f_searchdecl(typval_T *argvars, typval_T *rettv);
729static void f_searchpair(typval_T *argvars, typval_T *rettv);
730static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
731static void f_searchpos(typval_T *argvars, typval_T *rettv);
732static void f_server2client(typval_T *argvars, typval_T *rettv);
733static void f_serverlist(typval_T *argvars, typval_T *rettv);
734static void f_setbufvar(typval_T *argvars, typval_T *rettv);
735static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
736static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100737static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100738static void f_setline(typval_T *argvars, typval_T *rettv);
739static void f_setloclist(typval_T *argvars, typval_T *rettv);
740static void f_setmatches(typval_T *argvars, typval_T *rettv);
741static void f_setpos(typval_T *argvars, typval_T *rettv);
742static void f_setqflist(typval_T *argvars, typval_T *rettv);
743static void f_setreg(typval_T *argvars, typval_T *rettv);
744static void f_settabvar(typval_T *argvars, typval_T *rettv);
745static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
746static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100747#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100748static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_shellescape(typval_T *argvars, typval_T *rettv);
751static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
752static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sin(typval_T *argvars, typval_T *rettv);
755static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000756#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100757static void f_sort(typval_T *argvars, typval_T *rettv);
758static void f_soundfold(typval_T *argvars, typval_T *rettv);
759static void f_spellbadword(typval_T *argvars, typval_T *rettv);
760static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
761static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_sqrt(typval_T *argvars, typval_T *rettv);
764static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000765#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_str2nr(typval_T *argvars, typval_T *rettv);
767static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000768#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100769static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_stridx(typval_T *argvars, typval_T *rettv);
772static void f_string(typval_T *argvars, typval_T *rettv);
773static void f_strlen(typval_T *argvars, typval_T *rettv);
774static void f_strpart(typval_T *argvars, typval_T *rettv);
775static void f_strridx(typval_T *argvars, typval_T *rettv);
776static void f_strtrans(typval_T *argvars, typval_T *rettv);
777static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
778static void f_strwidth(typval_T *argvars, typval_T *rettv);
779static void f_submatch(typval_T *argvars, typval_T *rettv);
780static void f_substitute(typval_T *argvars, typval_T *rettv);
781static void f_synID(typval_T *argvars, typval_T *rettv);
782static void f_synIDattr(typval_T *argvars, typval_T *rettv);
783static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
784static void f_synstack(typval_T *argvars, typval_T *rettv);
785static void f_synconcealed(typval_T *argvars, typval_T *rettv);
786static void f_system(typval_T *argvars, typval_T *rettv);
787static void f_systemlist(typval_T *argvars, typval_T *rettv);
788static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
790static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
791static void f_taglist(typval_T *argvars, typval_T *rettv);
792static void f_tagfiles(typval_T *argvars, typval_T *rettv);
793static void f_tempname(typval_T *argvars, typval_T *rettv);
794static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100796static void f_tan(typval_T *argvars, typval_T *rettv);
797static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200798#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100799#ifdef FEAT_TIMERS
800static void f_timer_start(typval_T *argvars, typval_T *rettv);
801static void f_timer_stop(typval_T *argvars, typval_T *rettv);
802#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100803static void f_tolower(typval_T *argvars, typval_T *rettv);
804static void f_toupper(typval_T *argvars, typval_T *rettv);
805static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000806#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100807static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_type(typval_T *argvars, typval_T *rettv);
810static void f_undofile(typval_T *argvars, typval_T *rettv);
811static void f_undotree(typval_T *argvars, typval_T *rettv);
812static void f_uniq(typval_T *argvars, typval_T *rettv);
813static void f_values(typval_T *argvars, typval_T *rettv);
814static void f_virtcol(typval_T *argvars, typval_T *rettv);
815static void f_visualmode(typval_T *argvars, typval_T *rettv);
816static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100817static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100818static void f_win_getid(typval_T *argvars, typval_T *rettv);
819static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
820static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
821static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100822static void f_winbufnr(typval_T *argvars, typval_T *rettv);
823static void f_wincol(typval_T *argvars, typval_T *rettv);
824static void f_winheight(typval_T *argvars, typval_T *rettv);
825static void f_winline(typval_T *argvars, typval_T *rettv);
826static void f_winnr(typval_T *argvars, typval_T *rettv);
827static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
828static void f_winrestview(typval_T *argvars, typval_T *rettv);
829static void f_winsaveview(typval_T *argvars, typval_T *rettv);
830static void f_winwidth(typval_T *argvars, typval_T *rettv);
831static void f_writefile(typval_T *argvars, typval_T *rettv);
832static void f_wordcount(typval_T *argvars, typval_T *rettv);
833static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000834
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100835static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
836static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
837static int get_env_len(char_u **arg);
838static int get_id_len(char_u **arg);
839static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
840static 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 +0000841#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
842#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
843 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100844static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
845static int eval_isnamec(int c);
846static int eval_isnamec1(int c);
847static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
848static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100849static typval_T *alloc_string_tv(char_u *string);
850static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100851#ifdef FEAT_FLOAT
852static float_T get_tv_float(typval_T *varp);
853#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100854static linenr_T get_tv_lnum(typval_T *argvars);
855static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static 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);
Bram Moolenaar65639032016-03-16 21:40:30 +0100872static char_u *trans_function_name(char_u **pp, int skip, int flags, funcdict_T *fd, partial_T **partial);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100873static 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 Moolenaar1735bc92016-03-14 23:05:14 +01001685 &doesrange, TRUE, NULL, 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 Moolenaar1735bc92016-03-14 23:05:14 +01003101 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003102 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003103 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003104 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003105 break;
3106
3107 case VAR_LIST:
3108 if (*op != '+' || tv2->v_type != VAR_LIST)
3109 break;
3110 /* List += List */
3111 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3112 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3113 return OK;
3114
3115 case VAR_NUMBER:
3116 case VAR_STRING:
3117 if (tv2->v_type == VAR_LIST)
3118 break;
3119 if (*op == '+' || *op == '-')
3120 {
3121 /* nr += nr or nr -= nr*/
3122 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003123#ifdef FEAT_FLOAT
3124 if (tv2->v_type == VAR_FLOAT)
3125 {
3126 float_T f = n;
3127
3128 if (*op == '+')
3129 f += tv2->vval.v_float;
3130 else
3131 f -= tv2->vval.v_float;
3132 clear_tv(tv1);
3133 tv1->v_type = VAR_FLOAT;
3134 tv1->vval.v_float = f;
3135 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003136 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003137#endif
3138 {
3139 if (*op == '+')
3140 n += get_tv_number(tv2);
3141 else
3142 n -= get_tv_number(tv2);
3143 clear_tv(tv1);
3144 tv1->v_type = VAR_NUMBER;
3145 tv1->vval.v_number = n;
3146 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003147 }
3148 else
3149 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003150 if (tv2->v_type == VAR_FLOAT)
3151 break;
3152
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003153 /* str .= str */
3154 s = get_tv_string(tv1);
3155 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3156 clear_tv(tv1);
3157 tv1->v_type = VAR_STRING;
3158 tv1->vval.v_string = s;
3159 }
3160 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003163#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003164 {
3165 float_T f;
3166
3167 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3168 && tv2->v_type != VAR_NUMBER
3169 && tv2->v_type != VAR_STRING))
3170 break;
3171 if (tv2->v_type == VAR_FLOAT)
3172 f = tv2->vval.v_float;
3173 else
3174 f = get_tv_number(tv2);
3175 if (*op == '+')
3176 tv1->vval.v_float += f;
3177 else
3178 tv1->vval.v_float -= f;
3179 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003180#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003181 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003182 }
3183 }
3184
3185 EMSG2(_(e_letwrong), op);
3186 return FAIL;
3187}
3188
3189/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003190 * Add a watcher to a list.
3191 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003192 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003193list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003194{
3195 lw->lw_next = l->lv_watch;
3196 l->lv_watch = lw;
3197}
3198
3199/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003200 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201 * No warning when it isn't found...
3202 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003203 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003204list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003205{
Bram Moolenaar33570922005-01-25 22:26:29 +00003206 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003207
3208 lwp = &l->lv_watch;
3209 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3210 {
3211 if (lw == lwrem)
3212 {
3213 *lwp = lw->lw_next;
3214 break;
3215 }
3216 lwp = &lw->lw_next;
3217 }
3218}
3219
3220/*
3221 * Just before removing an item from a list: advance watchers to the next
3222 * item.
3223 */
3224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003225list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003226{
Bram Moolenaar33570922005-01-25 22:26:29 +00003227 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003228
3229 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3230 if (lw->lw_item == item)
3231 lw->lw_item = item->li_next;
3232}
3233
3234/*
3235 * Evaluate the expression used in a ":for var in expr" command.
3236 * "arg" points to "var".
3237 * Set "*errp" to TRUE for an error, FALSE otherwise;
3238 * Return a pointer that holds the info. Null when there is an error.
3239 */
3240 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003241eval_for_line(
3242 char_u *arg,
3243 int *errp,
3244 char_u **nextcmdp,
3245 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003246{
Bram Moolenaar33570922005-01-25 22:26:29 +00003247 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003249 typval_T tv;
3250 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003251
3252 *errp = TRUE; /* default: there is an error */
3253
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 if (fi == NULL)
3256 return NULL;
3257
3258 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3259 if (expr == NULL)
3260 return fi;
3261
3262 expr = skipwhite(expr);
3263 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3264 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003265 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003266 return fi;
3267 }
3268
3269 if (skip)
3270 ++emsg_skip;
3271 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3272 {
3273 *errp = FALSE;
3274 if (!skip)
3275 {
3276 l = tv.vval.v_list;
3277 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003278 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003280 clear_tv(&tv);
3281 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003282 else
3283 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003284 /* No need to increment the refcount, it's already set for the
3285 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 fi->fi_list = l;
3287 list_add_watch(l, &fi->fi_lw);
3288 fi->fi_lw.lw_item = l->lv_first;
3289 }
3290 }
3291 }
3292 if (skip)
3293 --emsg_skip;
3294
3295 return fi;
3296}
3297
3298/*
3299 * Use the first item in a ":for" list. Advance to the next.
3300 * Assign the values to the variable (list). "arg" points to the first one.
3301 * Return TRUE when a valid item was found, FALSE when at end of list or
3302 * something wrong.
3303 */
3304 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003305next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003306{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003307 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003308 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003309 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003310
3311 item = fi->fi_lw.lw_item;
3312 if (item == NULL)
3313 result = FALSE;
3314 else
3315 {
3316 fi->fi_lw.lw_item = item->li_next;
3317 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3318 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3319 }
3320 return result;
3321}
3322
3323/*
3324 * Free the structure used to store info used by ":for".
3325 */
3326 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003327free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003328{
Bram Moolenaar33570922005-01-25 22:26:29 +00003329 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003331 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003332 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003334 list_unref(fi->fi_list);
3335 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003336 vim_free(fi);
3337}
3338
Bram Moolenaar071d4272004-06-13 20:20:40 +00003339#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3340
3341 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003342set_context_for_expression(
3343 expand_T *xp,
3344 char_u *arg,
3345 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346{
3347 int got_eq = FALSE;
3348 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003349 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003350
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003351 if (cmdidx == CMD_let)
3352 {
3353 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003354 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 {
3356 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003357 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 {
3359 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003360 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003361 if (vim_iswhite(*p))
3362 break;
3363 }
3364 return;
3365 }
3366 }
3367 else
3368 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3369 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003370 while ((xp->xp_pattern = vim_strpbrk(arg,
3371 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3372 {
3373 c = *xp->xp_pattern;
3374 if (c == '&')
3375 {
3376 c = xp->xp_pattern[1];
3377 if (c == '&')
3378 {
3379 ++xp->xp_pattern;
3380 xp->xp_context = cmdidx != CMD_let || got_eq
3381 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3382 }
3383 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003384 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003385 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003386 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3387 xp->xp_pattern += 2;
3388
3389 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003390 }
3391 else if (c == '$')
3392 {
3393 /* environment variable */
3394 xp->xp_context = EXPAND_ENV_VARS;
3395 }
3396 else if (c == '=')
3397 {
3398 got_eq = TRUE;
3399 xp->xp_context = EXPAND_EXPRESSION;
3400 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003401 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003402 && xp->xp_context == EXPAND_FUNCTIONS
3403 && vim_strchr(xp->xp_pattern, '(') == NULL)
3404 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003405 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003406 break;
3407 }
3408 else if (cmdidx != CMD_let || got_eq)
3409 {
3410 if (c == '"') /* string */
3411 {
3412 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3413 if (c == '\\' && xp->xp_pattern[1] != NUL)
3414 ++xp->xp_pattern;
3415 xp->xp_context = EXPAND_NOTHING;
3416 }
3417 else if (c == '\'') /* literal string */
3418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003419 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003420 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3421 /* skip */ ;
3422 xp->xp_context = EXPAND_NOTHING;
3423 }
3424 else if (c == '|')
3425 {
3426 if (xp->xp_pattern[1] == '|')
3427 {
3428 ++xp->xp_pattern;
3429 xp->xp_context = EXPAND_EXPRESSION;
3430 }
3431 else
3432 xp->xp_context = EXPAND_COMMANDS;
3433 }
3434 else
3435 xp->xp_context = EXPAND_EXPRESSION;
3436 }
3437 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003438 /* Doesn't look like something valid, expand as an expression
3439 * anyway. */
3440 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003441 arg = xp->xp_pattern;
3442 if (*arg != NUL)
3443 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3444 /* skip */ ;
3445 }
3446 xp->xp_pattern = arg;
3447}
3448
3449#endif /* FEAT_CMDL_COMPL */
3450
3451/*
3452 * ":1,25call func(arg1, arg2)" function call.
3453 */
3454 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003455ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456{
3457 char_u *arg = eap->arg;
3458 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003459 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003460 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003461 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003462 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 linenr_T lnum;
3464 int doesrange;
3465 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003466 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003467 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003469 if (eap->skip)
3470 {
3471 /* trans_function_name() doesn't work well when skipping, use eval0()
3472 * instead to skip to any following command, e.g. for:
3473 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003474 ++emsg_skip;
3475 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3476 clear_tv(&rettv);
3477 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003478 return;
3479 }
3480
Bram Moolenaar65639032016-03-16 21:40:30 +01003481 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003482 if (fudi.fd_newkey != NULL)
3483 {
3484 /* Still need to give an error message for missing key. */
3485 EMSG2(_(e_dictkey), fudi.fd_newkey);
3486 vim_free(fudi.fd_newkey);
3487 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003488 if (tofree == NULL)
3489 return;
3490
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003491 /* Increase refcount on dictionary, it could get deleted when evaluating
3492 * the arguments. */
3493 if (fudi.fd_dict != NULL)
3494 ++fudi.fd_dict->dv_refcount;
3495
Bram Moolenaar65639032016-03-16 21:40:30 +01003496 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3497 * contents. For VAR_PARTIAL get its partial, unless we already have one
3498 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003499 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003500 name = deref_func_name(tofree, &len,
3501 partial != NULL ? NULL : &partial, FALSE);
3502
Bram Moolenaar532c7802005-01-27 14:44:31 +00003503 /* Skip white space to allow ":call func ()". Not good, but required for
3504 * backward compatibility. */
3505 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003506 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003507
3508 if (*startarg != '(')
3509 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003510 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 goto end;
3512 }
3513
3514 /*
3515 * When skipping, evaluate the function once, to find the end of the
3516 * arguments.
3517 * When the function takes a range, this is discovered after the first
3518 * call, and the loop is broken.
3519 */
3520 if (eap->skip)
3521 {
3522 ++emsg_skip;
3523 lnum = eap->line2; /* do it once, also with an invalid range */
3524 }
3525 else
3526 lnum = eap->line1;
3527 for ( ; lnum <= eap->line2; ++lnum)
3528 {
3529 if (!eap->skip && eap->addr_count > 0)
3530 {
3531 curwin->w_cursor.lnum = lnum;
3532 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003533#ifdef FEAT_VIRTUALEDIT
3534 curwin->w_cursor.coladd = 0;
3535#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003536 }
3537 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003538 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003539 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003540 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003541 {
3542 failed = TRUE;
3543 break;
3544 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003545
3546 /* Handle a function returning a Funcref, Dictionary or List. */
3547 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3548 {
3549 failed = TRUE;
3550 break;
3551 }
3552
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003553 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 if (doesrange || eap->skip)
3555 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003556
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003558 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003559 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003560 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (aborting())
3562 break;
3563 }
3564 if (eap->skip)
3565 --emsg_skip;
3566
3567 if (!failed)
3568 {
3569 /* Check for trailing illegal characters and a following command. */
3570 if (!ends_excmd(*arg))
3571 {
3572 emsg_severe = TRUE;
3573 EMSG(_(e_trailing));
3574 }
3575 else
3576 eap->nextcmd = check_nextcmd(arg);
3577 }
3578
3579end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003580 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003581 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003582}
3583
3584/*
3585 * ":unlet[!] var1 ... " command.
3586 */
3587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003588ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003590 ex_unletlock(eap, eap->arg, 0);
3591}
3592
3593/*
3594 * ":lockvar" and ":unlockvar" commands
3595 */
3596 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003597ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003598{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003599 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003600 int deep = 2;
3601
3602 if (eap->forceit)
3603 deep = -1;
3604 else if (vim_isdigit(*arg))
3605 {
3606 deep = getdigits(&arg);
3607 arg = skipwhite(arg);
3608 }
3609
3610 ex_unletlock(eap, arg, deep);
3611}
3612
3613/*
3614 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3615 */
3616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003617ex_unletlock(
3618 exarg_T *eap,
3619 char_u *argstart,
3620 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003621{
3622 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003624 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003625 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626
3627 do
3628 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003629 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003630 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003631 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 if (lv.ll_name == NULL)
3633 error = TRUE; /* error but continue parsing */
3634 if (name_end == NULL || (!vim_iswhite(*name_end)
3635 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003637 if (name_end != NULL)
3638 {
3639 emsg_severe = TRUE;
3640 EMSG(_(e_trailing));
3641 }
3642 if (!(eap->skip || error))
3643 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003644 break;
3645 }
3646
3647 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003648 {
3649 if (eap->cmdidx == CMD_unlet)
3650 {
3651 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3652 error = TRUE;
3653 }
3654 else
3655 {
3656 if (do_lock_var(&lv, name_end, deep,
3657 eap->cmdidx == CMD_lockvar) == FAIL)
3658 error = TRUE;
3659 }
3660 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003661
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003662 if (!eap->skip)
3663 clear_lval(&lv);
3664
Bram Moolenaar071d4272004-06-13 20:20:40 +00003665 arg = skipwhite(name_end);
3666 } while (!ends_excmd(*arg));
3667
3668 eap->nextcmd = check_nextcmd(arg);
3669}
3670
Bram Moolenaar8c711452005-01-14 21:53:12 +00003671 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003672do_unlet_var(
3673 lval_T *lp,
3674 char_u *name_end,
3675 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003676{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003677 int ret = OK;
3678 int cc;
3679
3680 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003682 cc = *name_end;
3683 *name_end = NUL;
3684
3685 /* Normal name or expanded name. */
3686 if (check_changedtick(lp->ll_name))
3687 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003688 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003691 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003692 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003693 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003694 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003695 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003696 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 else if (lp->ll_range)
3698 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003699 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003700 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003701 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003702
3703 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3704 {
3705 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003707 return FAIL;
3708 ll_li = li;
3709 ++ll_n1;
3710 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003711
3712 /* Delete a range of List items. */
3713 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3714 {
3715 li = lp->ll_li->li_next;
3716 listitem_remove(lp->ll_list, lp->ll_li);
3717 lp->ll_li = li;
3718 ++lp->ll_n1;
3719 }
3720 }
3721 else
3722 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003723 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724 /* unlet a List item. */
3725 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003728 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 }
3730
3731 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003732}
3733
Bram Moolenaar071d4272004-06-13 20:20:40 +00003734/*
3735 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003736 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737 */
3738 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003739do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740{
Bram Moolenaar33570922005-01-25 22:26:29 +00003741 hashtab_T *ht;
3742 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003743 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003744 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003745 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003746
Bram Moolenaar33570922005-01-25 22:26:29 +00003747 ht = find_var_ht(name, &varname);
3748 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003750 if (ht == &globvarht)
3751 d = &globvardict;
3752 else if (current_funccal != NULL
3753 && ht == &current_funccal->l_vars.dv_hashtab)
3754 d = &current_funccal->l_vars;
3755 else if (ht == &compat_hashtab)
3756 d = &vimvardict;
3757 else
3758 {
3759 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3760 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3761 }
3762 if (d == NULL)
3763 {
3764 EMSG2(_(e_intern2), "do_unlet()");
3765 return FAIL;
3766 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003767 hi = hash_find(ht, varname);
3768 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003769 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003770 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003771 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003772 || var_check_ro(di->di_flags, name, FALSE)
3773 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003774 return FAIL;
3775
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003776 delete_var(ht, hi);
3777 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003778 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003779 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003780 if (forceit)
3781 return OK;
3782 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003783 return FAIL;
3784}
3785
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003786/*
3787 * Lock or unlock variable indicated by "lp".
3788 * "deep" is the levels to go (-1 for unlimited);
3789 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3790 */
3791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003792do_lock_var(
3793 lval_T *lp,
3794 char_u *name_end,
3795 int deep,
3796 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003797{
3798 int ret = OK;
3799 int cc;
3800 dictitem_T *di;
3801
3802 if (deep == 0) /* nothing to do */
3803 return OK;
3804
3805 if (lp->ll_tv == NULL)
3806 {
3807 cc = *name_end;
3808 *name_end = NUL;
3809
3810 /* Normal name or expanded name. */
3811 if (check_changedtick(lp->ll_name))
3812 ret = FAIL;
3813 else
3814 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003815 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003816 if (di == NULL)
3817 ret = FAIL;
3818 else
3819 {
3820 if (lock)
3821 di->di_flags |= DI_FLAGS_LOCK;
3822 else
3823 di->di_flags &= ~DI_FLAGS_LOCK;
3824 item_lock(&di->di_tv, deep, lock);
3825 }
3826 }
3827 *name_end = cc;
3828 }
3829 else if (lp->ll_range)
3830 {
3831 listitem_T *li = lp->ll_li;
3832
3833 /* (un)lock a range of List items. */
3834 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3835 {
3836 item_lock(&li->li_tv, deep, lock);
3837 li = li->li_next;
3838 ++lp->ll_n1;
3839 }
3840 }
3841 else if (lp->ll_list != NULL)
3842 /* (un)lock a List item. */
3843 item_lock(&lp->ll_li->li_tv, deep, lock);
3844 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003845 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003846 item_lock(&lp->ll_di->di_tv, deep, lock);
3847
3848 return ret;
3849}
3850
3851/*
3852 * Lock or unlock an item. "deep" is nr of levels to go.
3853 */
3854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003855item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003856{
3857 static int recurse = 0;
3858 list_T *l;
3859 listitem_T *li;
3860 dict_T *d;
3861 hashitem_T *hi;
3862 int todo;
3863
3864 if (recurse >= DICT_MAXNEST)
3865 {
3866 EMSG(_("E743: variable nested too deep for (un)lock"));
3867 return;
3868 }
3869 if (deep == 0)
3870 return;
3871 ++recurse;
3872
3873 /* lock/unlock the item itself */
3874 if (lock)
3875 tv->v_lock |= VAR_LOCKED;
3876 else
3877 tv->v_lock &= ~VAR_LOCKED;
3878
3879 switch (tv->v_type)
3880 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003881 case VAR_UNKNOWN:
3882 case VAR_NUMBER:
3883 case VAR_STRING:
3884 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003885 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003886 case VAR_FLOAT:
3887 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003888 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003889 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003890 break;
3891
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003892 case VAR_LIST:
3893 if ((l = tv->vval.v_list) != NULL)
3894 {
3895 if (lock)
3896 l->lv_lock |= VAR_LOCKED;
3897 else
3898 l->lv_lock &= ~VAR_LOCKED;
3899 if (deep < 0 || deep > 1)
3900 /* recursive: lock/unlock the items the List contains */
3901 for (li = l->lv_first; li != NULL; li = li->li_next)
3902 item_lock(&li->li_tv, deep - 1, lock);
3903 }
3904 break;
3905 case VAR_DICT:
3906 if ((d = tv->vval.v_dict) != NULL)
3907 {
3908 if (lock)
3909 d->dv_lock |= VAR_LOCKED;
3910 else
3911 d->dv_lock &= ~VAR_LOCKED;
3912 if (deep < 0 || deep > 1)
3913 {
3914 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003915 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003916 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3917 {
3918 if (!HASHITEM_EMPTY(hi))
3919 {
3920 --todo;
3921 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3922 }
3923 }
3924 }
3925 }
3926 }
3927 --recurse;
3928}
3929
Bram Moolenaara40058a2005-07-11 22:42:07 +00003930/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003931 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3932 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933 */
3934 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003935tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003936{
3937 return (tv->v_lock & VAR_LOCKED)
3938 || (tv->v_type == VAR_LIST
3939 && tv->vval.v_list != NULL
3940 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3941 || (tv->v_type == VAR_DICT
3942 && tv->vval.v_dict != NULL
3943 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3944}
3945
Bram Moolenaar071d4272004-06-13 20:20:40 +00003946#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3947/*
3948 * Delete all "menutrans_" variables.
3949 */
3950 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003951del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003952{
Bram Moolenaar33570922005-01-25 22:26:29 +00003953 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003954 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003957 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003958 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003959 {
3960 if (!HASHITEM_EMPTY(hi))
3961 {
3962 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3964 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003965 }
3966 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003967 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968}
3969#endif
3970
3971#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3972
3973/*
3974 * Local string buffer for the next two functions to store a variable name
3975 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3976 * get_user_var_name().
3977 */
3978
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003979static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003980
3981static char_u *varnamebuf = NULL;
3982static int varnamebuflen = 0;
3983
3984/*
3985 * Function to concatenate a prefix and a variable name.
3986 */
3987 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003988cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003989{
3990 int len;
3991
3992 len = (int)STRLEN(name) + 3;
3993 if (len > varnamebuflen)
3994 {
3995 vim_free(varnamebuf);
3996 len += 10; /* some additional space */
3997 varnamebuf = alloc(len);
3998 if (varnamebuf == NULL)
3999 {
4000 varnamebuflen = 0;
4001 return NULL;
4002 }
4003 varnamebuflen = len;
4004 }
4005 *varnamebuf = prefix;
4006 varnamebuf[1] = ':';
4007 STRCPY(varnamebuf + 2, name);
4008 return varnamebuf;
4009}
4010
4011/*
4012 * Function given to ExpandGeneric() to obtain the list of user defined
4013 * (global/buffer/window/built-in) variable names.
4014 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004015 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004016get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004017{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004018 static long_u gdone;
4019 static long_u bdone;
4020 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004021#ifdef FEAT_WINDOWS
4022 static long_u tdone;
4023#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004024 static int vidx;
4025 static hashitem_T *hi;
4026 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004027
4028 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004029 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004030 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004031#ifdef FEAT_WINDOWS
4032 tdone = 0;
4033#endif
4034 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004035
4036 /* Global variables */
4037 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004038 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004039 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004040 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004041 else
4042 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 while (HASHITEM_EMPTY(hi))
4044 ++hi;
4045 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4046 return cat_prefix_varname('g', hi->hi_key);
4047 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004048 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004049
4050 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004051 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004052 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004053 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004054 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004056 else
4057 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004058 while (HASHITEM_EMPTY(hi))
4059 ++hi;
4060 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004063 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004064 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065 return (char_u *)"b:changedtick";
4066 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004067
4068 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004069 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004070 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004071 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004072 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004074 else
4075 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004076 while (HASHITEM_EMPTY(hi))
4077 ++hi;
4078 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004081#ifdef FEAT_WINDOWS
4082 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004083 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084 if (tdone < ht->ht_used)
4085 {
4086 if (tdone++ == 0)
4087 hi = ht->ht_array;
4088 else
4089 ++hi;
4090 while (HASHITEM_EMPTY(hi))
4091 ++hi;
4092 return cat_prefix_varname('t', hi->hi_key);
4093 }
4094#endif
4095
Bram Moolenaar33570922005-01-25 22:26:29 +00004096 /* v: variables */
4097 if (vidx < VV_LEN)
4098 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004099
4100 vim_free(varnamebuf);
4101 varnamebuf = NULL;
4102 varnamebuflen = 0;
4103 return NULL;
4104}
4105
4106#endif /* FEAT_CMDL_COMPL */
4107
4108/*
4109 * types for expressions.
4110 */
4111typedef enum
4112{
4113 TYPE_UNKNOWN = 0
4114 , TYPE_EQUAL /* == */
4115 , TYPE_NEQUAL /* != */
4116 , TYPE_GREATER /* > */
4117 , TYPE_GEQUAL /* >= */
4118 , TYPE_SMALLER /* < */
4119 , TYPE_SEQUAL /* <= */
4120 , TYPE_MATCH /* =~ */
4121 , TYPE_NOMATCH /* !~ */
4122} exptype_T;
4123
4124/*
4125 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004126 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004127 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4128 */
4129
4130/*
4131 * Handle zero level expression.
4132 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004133 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004134 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004135 * Return OK or FAIL.
4136 */
4137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004138eval0(
4139 char_u *arg,
4140 typval_T *rettv,
4141 char_u **nextcmd,
4142 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004143{
4144 int ret;
4145 char_u *p;
4146
4147 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004148 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004149 if (ret == FAIL || !ends_excmd(*p))
4150 {
4151 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004152 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004153 /*
4154 * Report the invalid expression unless the expression evaluation has
4155 * been cancelled due to an aborting error, an interrupt, or an
4156 * exception.
4157 */
4158 if (!aborting())
4159 EMSG2(_(e_invexpr2), arg);
4160 ret = FAIL;
4161 }
4162 if (nextcmd != NULL)
4163 *nextcmd = check_nextcmd(p);
4164
4165 return ret;
4166}
4167
4168/*
4169 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004170 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004171 *
4172 * "arg" must point to the first non-white of the expression.
4173 * "arg" is advanced to the next non-white after the recognized expression.
4174 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004175 * Note: "rettv.v_lock" is not set.
4176 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004177 * Return OK or FAIL.
4178 */
4179 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004180eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004183 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184
4185 /*
4186 * Get the first variable.
4187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004188 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004189 return FAIL;
4190
4191 if ((*arg)[0] == '?')
4192 {
4193 result = FALSE;
4194 if (evaluate)
4195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004196 int error = FALSE;
4197
4198 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004199 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004200 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004201 if (error)
4202 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 }
4204
4205 /*
4206 * Get the second variable.
4207 */
4208 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004209 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004210 return FAIL;
4211
4212 /*
4213 * Check for the ":".
4214 */
4215 if ((*arg)[0] != ':')
4216 {
4217 EMSG(_("E109: Missing ':' after '?'"));
4218 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004219 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004220 return FAIL;
4221 }
4222
4223 /*
4224 * Get the third variable.
4225 */
4226 *arg = skipwhite(*arg + 1);
4227 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4228 {
4229 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 return FAIL;
4232 }
4233 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004234 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236
4237 return OK;
4238}
4239
4240/*
4241 * Handle first level expression:
4242 * expr2 || expr2 || expr2 logical OR
4243 *
4244 * "arg" must point to the first non-white of the expression.
4245 * "arg" is advanced to the next non-white after the recognized expression.
4246 *
4247 * Return OK or FAIL.
4248 */
4249 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004250eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004251{
Bram Moolenaar33570922005-01-25 22:26:29 +00004252 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004253 long result;
4254 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004255 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256
4257 /*
4258 * Get the first variable.
4259 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004260 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004261 return FAIL;
4262
4263 /*
4264 * Repeat until there is no following "||".
4265 */
4266 first = TRUE;
4267 result = FALSE;
4268 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4269 {
4270 if (evaluate && first)
4271 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004272 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004274 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (error)
4276 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004277 first = FALSE;
4278 }
4279
4280 /*
4281 * Get the second variable.
4282 */
4283 *arg = skipwhite(*arg + 2);
4284 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4285 return FAIL;
4286
4287 /*
4288 * Compute the result.
4289 */
4290 if (evaluate && !result)
4291 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004292 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004294 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 if (error)
4296 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004297 }
4298 if (evaluate)
4299 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004300 rettv->v_type = VAR_NUMBER;
4301 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004302 }
4303 }
4304
4305 return OK;
4306}
4307
4308/*
4309 * Handle second level expression:
4310 * expr3 && expr3 && expr3 logical AND
4311 *
4312 * "arg" must point to the first non-white of the expression.
4313 * "arg" is advanced to the next non-white after the recognized expression.
4314 *
4315 * Return OK or FAIL.
4316 */
4317 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004318eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004319{
Bram Moolenaar33570922005-01-25 22:26:29 +00004320 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004321 long result;
4322 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004323 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324
4325 /*
4326 * Get the first variable.
4327 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004328 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 return FAIL;
4330
4331 /*
4332 * Repeat until there is no following "&&".
4333 */
4334 first = TRUE;
4335 result = TRUE;
4336 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4337 {
4338 if (evaluate && first)
4339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004340 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004341 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004342 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (error)
4344 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004345 first = FALSE;
4346 }
4347
4348 /*
4349 * Get the second variable.
4350 */
4351 *arg = skipwhite(*arg + 2);
4352 if (eval4(arg, &var2, evaluate && result) == FAIL)
4353 return FAIL;
4354
4355 /*
4356 * Compute the result.
4357 */
4358 if (evaluate && result)
4359 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004360 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004362 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 if (error)
4364 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004365 }
4366 if (evaluate)
4367 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004368 rettv->v_type = VAR_NUMBER;
4369 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004370 }
4371 }
4372
4373 return OK;
4374}
4375
4376/*
4377 * Handle third level expression:
4378 * var1 == var2
4379 * var1 =~ var2
4380 * var1 != var2
4381 * var1 !~ var2
4382 * var1 > var2
4383 * var1 >= var2
4384 * var1 < var2
4385 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004386 * var1 is var2
4387 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004388 *
4389 * "arg" must point to the first non-white of the expression.
4390 * "arg" is advanced to the next non-white after the recognized expression.
4391 *
4392 * Return OK or FAIL.
4393 */
4394 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004395eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004396{
Bram Moolenaar33570922005-01-25 22:26:29 +00004397 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004398 char_u *p;
4399 int i;
4400 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004401 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 int len = 2;
4403 long n1, n2;
4404 char_u *s1, *s2;
4405 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4406 regmatch_T regmatch;
4407 int ic;
4408 char_u *save_cpo;
4409
4410 /*
4411 * Get the first variable.
4412 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004413 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004414 return FAIL;
4415
4416 p = *arg;
4417 switch (p[0])
4418 {
4419 case '=': if (p[1] == '=')
4420 type = TYPE_EQUAL;
4421 else if (p[1] == '~')
4422 type = TYPE_MATCH;
4423 break;
4424 case '!': if (p[1] == '=')
4425 type = TYPE_NEQUAL;
4426 else if (p[1] == '~')
4427 type = TYPE_NOMATCH;
4428 break;
4429 case '>': if (p[1] != '=')
4430 {
4431 type = TYPE_GREATER;
4432 len = 1;
4433 }
4434 else
4435 type = TYPE_GEQUAL;
4436 break;
4437 case '<': if (p[1] != '=')
4438 {
4439 type = TYPE_SMALLER;
4440 len = 1;
4441 }
4442 else
4443 type = TYPE_SEQUAL;
4444 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004445 case 'i': if (p[1] == 's')
4446 {
4447 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4448 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004449 i = p[len];
4450 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004451 {
4452 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4453 type_is = TRUE;
4454 }
4455 }
4456 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 }
4458
4459 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004460 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004461 */
4462 if (type != TYPE_UNKNOWN)
4463 {
4464 /* extra question mark appended: ignore case */
4465 if (p[len] == '?')
4466 {
4467 ic = TRUE;
4468 ++len;
4469 }
4470 /* extra '#' appended: match case */
4471 else if (p[len] == '#')
4472 {
4473 ic = FALSE;
4474 ++len;
4475 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004476 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004477 else
4478 ic = p_ic;
4479
4480 /*
4481 * Get the second variable.
4482 */
4483 *arg = skipwhite(p + len);
4484 if (eval5(arg, &var2, evaluate) == FAIL)
4485 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004486 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 return FAIL;
4488 }
4489
4490 if (evaluate)
4491 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004492 if (type_is && rettv->v_type != var2.v_type)
4493 {
4494 /* For "is" a different type always means FALSE, for "notis"
4495 * it means TRUE. */
4496 n1 = (type == TYPE_NEQUAL);
4497 }
4498 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4499 {
4500 if (type_is)
4501 {
4502 n1 = (rettv->v_type == var2.v_type
4503 && rettv->vval.v_list == var2.vval.v_list);
4504 if (type == TYPE_NEQUAL)
4505 n1 = !n1;
4506 }
4507 else if (rettv->v_type != var2.v_type
4508 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4509 {
4510 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004511 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004512 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004513 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004514 clear_tv(rettv);
4515 clear_tv(&var2);
4516 return FAIL;
4517 }
4518 else
4519 {
4520 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004521 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4522 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004523 if (type == TYPE_NEQUAL)
4524 n1 = !n1;
4525 }
4526 }
4527
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004528 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4529 {
4530 if (type_is)
4531 {
4532 n1 = (rettv->v_type == var2.v_type
4533 && rettv->vval.v_dict == var2.vval.v_dict);
4534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 else if (rettv->v_type != var2.v_type
4538 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4539 {
4540 if (rettv->v_type != var2.v_type)
4541 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4542 else
4543 EMSG(_("E736: Invalid operation for Dictionary"));
4544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 return FAIL;
4547 }
4548 else
4549 {
4550 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004551 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4552 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004553 if (type == TYPE_NEQUAL)
4554 n1 = !n1;
4555 }
4556 }
4557
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004558 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4559 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004560 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004561 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004562 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004563 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004564 clear_tv(rettv);
4565 clear_tv(&var2);
4566 return FAIL;
4567 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004568 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004569 if (type == TYPE_NEQUAL)
4570 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 }
4572
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004573#ifdef FEAT_FLOAT
4574 /*
4575 * If one of the two variables is a float, compare as a float.
4576 * When using "=~" or "!~", always compare as string.
4577 */
4578 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4579 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4580 {
4581 float_T f1, f2;
4582
4583 if (rettv->v_type == VAR_FLOAT)
4584 f1 = rettv->vval.v_float;
4585 else
4586 f1 = get_tv_number(rettv);
4587 if (var2.v_type == VAR_FLOAT)
4588 f2 = var2.vval.v_float;
4589 else
4590 f2 = get_tv_number(&var2);
4591 n1 = FALSE;
4592 switch (type)
4593 {
4594 case TYPE_EQUAL: n1 = (f1 == f2); break;
4595 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4596 case TYPE_GREATER: n1 = (f1 > f2); break;
4597 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4598 case TYPE_SMALLER: n1 = (f1 < f2); break;
4599 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4600 case TYPE_UNKNOWN:
4601 case TYPE_MATCH:
4602 case TYPE_NOMATCH: break; /* avoid gcc warning */
4603 }
4604 }
4605#endif
4606
Bram Moolenaar071d4272004-06-13 20:20:40 +00004607 /*
4608 * If one of the two variables is a number, compare as a number.
4609 * When using "=~" or "!~", always compare as string.
4610 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004611 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004612 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4613 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004614 n1 = get_tv_number(rettv);
4615 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004616 switch (type)
4617 {
4618 case TYPE_EQUAL: n1 = (n1 == n2); break;
4619 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4620 case TYPE_GREATER: n1 = (n1 > n2); break;
4621 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4622 case TYPE_SMALLER: n1 = (n1 < n2); break;
4623 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4624 case TYPE_UNKNOWN:
4625 case TYPE_MATCH:
4626 case TYPE_NOMATCH: break; /* avoid gcc warning */
4627 }
4628 }
4629 else
4630 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004631 s1 = get_tv_string_buf(rettv, buf1);
4632 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004633 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4634 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4635 else
4636 i = 0;
4637 n1 = FALSE;
4638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (i == 0); break;
4641 case TYPE_NEQUAL: n1 = (i != 0); break;
4642 case TYPE_GREATER: n1 = (i > 0); break;
4643 case TYPE_GEQUAL: n1 = (i >= 0); break;
4644 case TYPE_SMALLER: n1 = (i < 0); break;
4645 case TYPE_SEQUAL: n1 = (i <= 0); break;
4646
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH:
4649 /* avoid 'l' flag in 'cpoptions' */
4650 save_cpo = p_cpo;
4651 p_cpo = (char_u *)"";
4652 regmatch.regprog = vim_regcomp(s2,
4653 RE_MAGIC + RE_STRING);
4654 regmatch.rm_ic = ic;
4655 if (regmatch.regprog != NULL)
4656 {
4657 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004658 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004659 if (type == TYPE_NOMATCH)
4660 n1 = !n1;
4661 }
4662 p_cpo = save_cpo;
4663 break;
4664
4665 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4666 }
4667 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004668 clear_tv(rettv);
4669 clear_tv(&var2);
4670 rettv->v_type = VAR_NUMBER;
4671 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004672 }
4673 }
4674
4675 return OK;
4676}
4677
4678/*
4679 * Handle fourth level expression:
4680 * + number addition
4681 * - number subtraction
4682 * . string concatenation
4683 *
4684 * "arg" must point to the first non-white of the expression.
4685 * "arg" is advanced to the next non-white after the recognized expression.
4686 *
4687 * Return OK or FAIL.
4688 */
4689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004690eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691{
Bram Moolenaar33570922005-01-25 22:26:29 +00004692 typval_T var2;
4693 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 int op;
4695 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004696#ifdef FEAT_FLOAT
4697 float_T f1 = 0, f2 = 0;
4698#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004699 char_u *s1, *s2;
4700 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4701 char_u *p;
4702
4703 /*
4704 * Get the first variable.
4705 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004706 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004707 return FAIL;
4708
4709 /*
4710 * Repeat computing, until no '+', '-' or '.' is following.
4711 */
4712 for (;;)
4713 {
4714 op = **arg;
4715 if (op != '+' && op != '-' && op != '.')
4716 break;
4717
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718 if ((op != '+' || rettv->v_type != VAR_LIST)
4719#ifdef FEAT_FLOAT
4720 && (op == '.' || rettv->v_type != VAR_FLOAT)
4721#endif
4722 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004723 {
4724 /* For "list + ...", an illegal use of the first operand as
4725 * a number cannot be determined before evaluating the 2nd
4726 * operand: if this is also a list, all is ok.
4727 * For "something . ...", "something - ..." or "non-list + ...",
4728 * we know that the first operand needs to be a string or number
4729 * without evaluating the 2nd operand. So check before to avoid
4730 * side effects after an error. */
4731 if (evaluate && get_tv_string_chk(rettv) == NULL)
4732 {
4733 clear_tv(rettv);
4734 return FAIL;
4735 }
4736 }
4737
Bram Moolenaar071d4272004-06-13 20:20:40 +00004738 /*
4739 * Get the second variable.
4740 */
4741 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004742 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004743 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004744 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004745 return FAIL;
4746 }
4747
4748 if (evaluate)
4749 {
4750 /*
4751 * Compute the result.
4752 */
4753 if (op == '.')
4754 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004755 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4756 s2 = get_tv_string_buf_chk(&var2, buf2);
4757 if (s2 == NULL) /* type error ? */
4758 {
4759 clear_tv(rettv);
4760 clear_tv(&var2);
4761 return FAIL;
4762 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004763 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004764 clear_tv(rettv);
4765 rettv->v_type = VAR_STRING;
4766 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004768 else if (op == '+' && rettv->v_type == VAR_LIST
4769 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004770 {
4771 /* concatenate Lists */
4772 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4773 &var3) == FAIL)
4774 {
4775 clear_tv(rettv);
4776 clear_tv(&var2);
4777 return FAIL;
4778 }
4779 clear_tv(rettv);
4780 *rettv = var3;
4781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004782 else
4783 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004784 int error = FALSE;
4785
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004786#ifdef FEAT_FLOAT
4787 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004788 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004789 f1 = rettv->vval.v_float;
4790 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004791 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004792 else
4793#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004794 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004795 n1 = get_tv_number_chk(rettv, &error);
4796 if (error)
4797 {
4798 /* This can only happen for "list + non-list". For
4799 * "non-list + ..." or "something - ...", we returned
4800 * before evaluating the 2nd operand. */
4801 clear_tv(rettv);
4802 return FAIL;
4803 }
4804#ifdef FEAT_FLOAT
4805 if (var2.v_type == VAR_FLOAT)
4806 f1 = n1;
4807#endif
4808 }
4809#ifdef FEAT_FLOAT
4810 if (var2.v_type == VAR_FLOAT)
4811 {
4812 f2 = var2.vval.v_float;
4813 n2 = 0;
4814 }
4815 else
4816#endif
4817 {
4818 n2 = get_tv_number_chk(&var2, &error);
4819 if (error)
4820 {
4821 clear_tv(rettv);
4822 clear_tv(&var2);
4823 return FAIL;
4824 }
4825#ifdef FEAT_FLOAT
4826 if (rettv->v_type == VAR_FLOAT)
4827 f2 = n2;
4828#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004829 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004830 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004831
4832#ifdef FEAT_FLOAT
4833 /* If there is a float on either side the result is a float. */
4834 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4835 {
4836 if (op == '+')
4837 f1 = f1 + f2;
4838 else
4839 f1 = f1 - f2;
4840 rettv->v_type = VAR_FLOAT;
4841 rettv->vval.v_float = f1;
4842 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004843 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004844#endif
4845 {
4846 if (op == '+')
4847 n1 = n1 + n2;
4848 else
4849 n1 = n1 - n2;
4850 rettv->v_type = VAR_NUMBER;
4851 rettv->vval.v_number = n1;
4852 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004854 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 }
4856 }
4857 return OK;
4858}
4859
4860/*
4861 * Handle fifth level expression:
4862 * * number multiplication
4863 * / number division
4864 * % number modulo
4865 *
4866 * "arg" must point to the first non-white of the expression.
4867 * "arg" is advanced to the next non-white after the recognized expression.
4868 *
4869 * Return OK or FAIL.
4870 */
4871 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004872eval6(
4873 char_u **arg,
4874 typval_T *rettv,
4875 int evaluate,
4876 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877{
Bram Moolenaar33570922005-01-25 22:26:29 +00004878 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004879 int op;
4880 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004881#ifdef FEAT_FLOAT
4882 int use_float = FALSE;
4883 float_T f1 = 0, f2;
4884#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004885 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004886
4887 /*
4888 * Get the first variable.
4889 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004890 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004891 return FAIL;
4892
4893 /*
4894 * Repeat computing, until no '*', '/' or '%' is following.
4895 */
4896 for (;;)
4897 {
4898 op = **arg;
4899 if (op != '*' && op != '/' && op != '%')
4900 break;
4901
4902 if (evaluate)
4903 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004904#ifdef FEAT_FLOAT
4905 if (rettv->v_type == VAR_FLOAT)
4906 {
4907 f1 = rettv->vval.v_float;
4908 use_float = TRUE;
4909 n1 = 0;
4910 }
4911 else
4912#endif
4913 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004914 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004915 if (error)
4916 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004917 }
4918 else
4919 n1 = 0;
4920
4921 /*
4922 * Get the second variable.
4923 */
4924 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004925 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004926 return FAIL;
4927
4928 if (evaluate)
4929 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004930#ifdef FEAT_FLOAT
4931 if (var2.v_type == VAR_FLOAT)
4932 {
4933 if (!use_float)
4934 {
4935 f1 = n1;
4936 use_float = TRUE;
4937 }
4938 f2 = var2.vval.v_float;
4939 n2 = 0;
4940 }
4941 else
4942#endif
4943 {
4944 n2 = get_tv_number_chk(&var2, &error);
4945 clear_tv(&var2);
4946 if (error)
4947 return FAIL;
4948#ifdef FEAT_FLOAT
4949 if (use_float)
4950 f2 = n2;
4951#endif
4952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953
4954 /*
4955 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004956 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004957 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004958#ifdef FEAT_FLOAT
4959 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004960 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004961 if (op == '*')
4962 f1 = f1 * f2;
4963 else if (op == '/')
4964 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004965# ifdef VMS
4966 /* VMS crashes on divide by zero, work around it */
4967 if (f2 == 0.0)
4968 {
4969 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004970 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004971 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004972 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004973 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004974 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004975 }
4976 else
4977 f1 = f1 / f2;
4978# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004979 /* We rely on the floating point library to handle divide
4980 * by zero to result in "inf" and not a crash. */
4981 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004982# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004985 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00004986 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004987 return FAIL;
4988 }
4989 rettv->v_type = VAR_FLOAT;
4990 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004991 }
4992 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004993#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004994 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004995 if (op == '*')
4996 n1 = n1 * n2;
4997 else if (op == '/')
4998 {
4999 if (n2 == 0) /* give an error message? */
5000 {
5001 if (n1 == 0)
5002 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5003 else if (n1 < 0)
5004 n1 = -0x7fffffffL;
5005 else
5006 n1 = 0x7fffffffL;
5007 }
5008 else
5009 n1 = n1 / n2;
5010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 {
5013 if (n2 == 0) /* give an error message? */
5014 n1 = 0;
5015 else
5016 n1 = n1 % n2;
5017 }
5018 rettv->v_type = VAR_NUMBER;
5019 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005020 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005021 }
5022 }
5023
5024 return OK;
5025}
5026
5027/*
5028 * Handle sixth level expression:
5029 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005030 * "string" string constant
5031 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005032 * &option-name option value
5033 * @r register contents
5034 * identifier variable value
5035 * function() function call
5036 * $VAR environment variable
5037 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005038 * [expr, expr] List
5039 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 *
5041 * Also handle:
5042 * ! in front logical NOT
5043 * - in front unary minus
5044 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005045 * trailing [] subscript in String or List
5046 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005047 *
5048 * "arg" must point to the first non-white of the expression.
5049 * "arg" is advanced to the next non-white after the recognized expression.
5050 *
5051 * Return OK or FAIL.
5052 */
5053 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005054eval7(
5055 char_u **arg,
5056 typval_T *rettv,
5057 int evaluate,
5058 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005060 long n;
5061 int len;
5062 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005063 char_u *start_leader, *end_leader;
5064 int ret = OK;
5065 char_u *alias;
5066
5067 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005068 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005069 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005070 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005071 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072
5073 /*
5074 * Skip '!' and '-' characters. They are handled later.
5075 */
5076 start_leader = *arg;
5077 while (**arg == '!' || **arg == '-' || **arg == '+')
5078 *arg = skipwhite(*arg + 1);
5079 end_leader = *arg;
5080
5081 switch (**arg)
5082 {
5083 /*
5084 * Number constant.
5085 */
5086 case '0':
5087 case '1':
5088 case '2':
5089 case '3':
5090 case '4':
5091 case '5':
5092 case '6':
5093 case '7':
5094 case '8':
5095 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005096 {
5097#ifdef FEAT_FLOAT
5098 char_u *p = skipdigits(*arg + 1);
5099 int get_float = FALSE;
5100
5101 /* We accept a float when the format matches
5102 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005103 * strict to avoid backwards compatibility problems.
5104 * Don't look for a float after the "." operator, so that
5105 * ":let vers = 1.2.3" doesn't fail. */
5106 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005107 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005108 get_float = TRUE;
5109 p = skipdigits(p + 2);
5110 if (*p == 'e' || *p == 'E')
5111 {
5112 ++p;
5113 if (*p == '-' || *p == '+')
5114 ++p;
5115 if (!vim_isdigit(*p))
5116 get_float = FALSE;
5117 else
5118 p = skipdigits(p + 1);
5119 }
5120 if (ASCII_ISALPHA(*p) || *p == '.')
5121 get_float = FALSE;
5122 }
5123 if (get_float)
5124 {
5125 float_T f;
5126
5127 *arg += string2float(*arg, &f);
5128 if (evaluate)
5129 {
5130 rettv->v_type = VAR_FLOAT;
5131 rettv->vval.v_float = f;
5132 }
5133 }
5134 else
5135#endif
5136 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005137 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005138 *arg += len;
5139 if (evaluate)
5140 {
5141 rettv->v_type = VAR_NUMBER;
5142 rettv->vval.v_number = n;
5143 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005144 }
5145 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005146 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005147
5148 /*
5149 * String constant: "string".
5150 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005151 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005152 break;
5153
5154 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005155 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005156 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005157 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005158 break;
5159
5160 /*
5161 * List: [expr, expr]
5162 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005163 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005164 break;
5165
5166 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005167 * Dictionary: {key: val, key: val}
5168 */
5169 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5170 break;
5171
5172 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005173 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005175 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005176 break;
5177
5178 /*
5179 * Environment variable: $VAR.
5180 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005181 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005182 break;
5183
5184 /*
5185 * Register contents: @r.
5186 */
5187 case '@': ++*arg;
5188 if (evaluate)
5189 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005190 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005191 rettv->vval.v_string = get_reg_contents(**arg,
5192 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 }
5194 if (**arg != NUL)
5195 ++*arg;
5196 break;
5197
5198 /*
5199 * nested expression: (expression).
5200 */
5201 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005202 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005203 if (**arg == ')')
5204 ++*arg;
5205 else if (ret == OK)
5206 {
5207 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005208 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005209 ret = FAIL;
5210 }
5211 break;
5212
Bram Moolenaar8c711452005-01-14 21:53:12 +00005213 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 break;
5215 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005216
5217 if (ret == NOTDONE)
5218 {
5219 /*
5220 * Must be a variable or function name.
5221 * Can also be a curly-braces kind of name: {expr}.
5222 */
5223 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005224 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005225 if (alias != NULL)
5226 s = alias;
5227
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005228 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005229 ret = FAIL;
5230 else
5231 {
5232 if (**arg == '(') /* recursive! */
5233 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005234 partial_T *partial;
5235
Bram Moolenaar8c711452005-01-14 21:53:12 +00005236 /* If "s" is the name of a variable of type VAR_FUNC
5237 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005238 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005239
5240 /* Invoke the function. */
5241 ret = get_func_tv(s, len, rettv, arg,
5242 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005243 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005244
5245 /* If evaluate is FALSE rettv->v_type was not set in
5246 * get_func_tv, but it's needed in handle_subscript() to parse
5247 * what follows. So set it here. */
5248 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5249 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005250 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005251 rettv->v_type = VAR_FUNC;
5252 }
5253
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 /* Stop the expression evaluation when immediately
5255 * aborting on error, or when an interrupt occurred or
5256 * an exception was thrown but not caught. */
5257 if (aborting())
5258 {
5259 if (ret == OK)
5260 clear_tv(rettv);
5261 ret = FAIL;
5262 }
5263 }
5264 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005265 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005266 else
5267 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005268 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005269 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005270 }
5271
Bram Moolenaar071d4272004-06-13 20:20:40 +00005272 *arg = skipwhite(*arg);
5273
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005274 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5275 * expr(expr). */
5276 if (ret == OK)
5277 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005278
5279 /*
5280 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5281 */
5282 if (ret == OK && evaluate && end_leader > start_leader)
5283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005284 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005285 int val = 0;
5286#ifdef FEAT_FLOAT
5287 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005288
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005289 if (rettv->v_type == VAR_FLOAT)
5290 f = rettv->vval.v_float;
5291 else
5292#endif
5293 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005294 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005295 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005296 clear_tv(rettv);
5297 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005298 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005299 else
5300 {
5301 while (end_leader > start_leader)
5302 {
5303 --end_leader;
5304 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005305 {
5306#ifdef FEAT_FLOAT
5307 if (rettv->v_type == VAR_FLOAT)
5308 f = !f;
5309 else
5310#endif
5311 val = !val;
5312 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 {
5315#ifdef FEAT_FLOAT
5316 if (rettv->v_type == VAR_FLOAT)
5317 f = -f;
5318 else
5319#endif
5320 val = -val;
5321 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005322 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005323#ifdef FEAT_FLOAT
5324 if (rettv->v_type == VAR_FLOAT)
5325 {
5326 clear_tv(rettv);
5327 rettv->vval.v_float = f;
5328 }
5329 else
5330#endif
5331 {
5332 clear_tv(rettv);
5333 rettv->v_type = VAR_NUMBER;
5334 rettv->vval.v_number = val;
5335 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005336 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005337 }
5338
5339 return ret;
5340}
5341
5342/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005343 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5344 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005345 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5346 */
5347 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005348eval_index(
5349 char_u **arg,
5350 typval_T *rettv,
5351 int evaluate,
5352 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005353{
5354 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005355 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005356 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005357 long len = -1;
5358 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005359 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005360 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005361
Bram Moolenaara03f2332016-02-06 18:09:59 +01005362 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005363 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005364 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005365 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005366 if (verbose)
5367 EMSG(_("E695: Cannot index a Funcref"));
5368 return FAIL;
5369 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005370#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005371 if (verbose)
5372 EMSG(_(e_float_as_string));
5373 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005374#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005375 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005376 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005377 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005378 if (verbose)
5379 EMSG(_("E909: Cannot index a special variable"));
5380 return FAIL;
5381 case VAR_UNKNOWN:
5382 if (evaluate)
5383 return FAIL;
5384 /* FALLTHROUGH */
5385
5386 case VAR_STRING:
5387 case VAR_NUMBER:
5388 case VAR_LIST:
5389 case VAR_DICT:
5390 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005391 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005392
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005393 init_tv(&var1);
5394 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005395 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005396 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005397 /*
5398 * dict.name
5399 */
5400 key = *arg + 1;
5401 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5402 ;
5403 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005404 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005405 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005406 }
5407 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005408 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005409 /*
5410 * something[idx]
5411 *
5412 * Get the (first) variable from inside the [].
5413 */
5414 *arg = skipwhite(*arg + 1);
5415 if (**arg == ':')
5416 empty1 = TRUE;
5417 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5418 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005419 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5420 {
5421 /* not a number or string */
5422 clear_tv(&var1);
5423 return FAIL;
5424 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005425
5426 /*
5427 * Get the second variable from inside the [:].
5428 */
5429 if (**arg == ':')
5430 {
5431 range = TRUE;
5432 *arg = skipwhite(*arg + 1);
5433 if (**arg == ']')
5434 empty2 = TRUE;
5435 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5436 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005437 if (!empty1)
5438 clear_tv(&var1);
5439 return FAIL;
5440 }
5441 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5442 {
5443 /* not a number or string */
5444 if (!empty1)
5445 clear_tv(&var1);
5446 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447 return FAIL;
5448 }
5449 }
5450
5451 /* Check for the ']'. */
5452 if (**arg != ']')
5453 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005454 if (verbose)
5455 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005456 clear_tv(&var1);
5457 if (range)
5458 clear_tv(&var2);
5459 return FAIL;
5460 }
5461 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005462 }
5463
5464 if (evaluate)
5465 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 n1 = 0;
5467 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005468 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005469 n1 = get_tv_number(&var1);
5470 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005471 }
5472 if (range)
5473 {
5474 if (empty2)
5475 n2 = -1;
5476 else
5477 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005478 n2 = get_tv_number(&var2);
5479 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005480 }
5481 }
5482
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005483 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005485 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005486 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005487 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005488 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005489 case VAR_SPECIAL:
5490 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005491 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005492 break; /* not evaluating, skipping over subscript */
5493
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005494 case VAR_NUMBER:
5495 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005496 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005497 len = (long)STRLEN(s);
5498 if (range)
5499 {
5500 /* The resulting variable is a substring. If the indexes
5501 * are out of range the result is empty. */
5502 if (n1 < 0)
5503 {
5504 n1 = len + n1;
5505 if (n1 < 0)
5506 n1 = 0;
5507 }
5508 if (n2 < 0)
5509 n2 = len + n2;
5510 else if (n2 >= len)
5511 n2 = len;
5512 if (n1 >= len || n2 < 0 || n1 > n2)
5513 s = NULL;
5514 else
5515 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5516 }
5517 else
5518 {
5519 /* The resulting variable is a string of a single
5520 * character. If the index is too big or negative the
5521 * result is empty. */
5522 if (n1 >= len || n1 < 0)
5523 s = NULL;
5524 else
5525 s = vim_strnsave(s + n1, 1);
5526 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005527 clear_tv(rettv);
5528 rettv->v_type = VAR_STRING;
5529 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005530 break;
5531
5532 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005533 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005534 if (n1 < 0)
5535 n1 = len + n1;
5536 if (!empty1 && (n1 < 0 || n1 >= len))
5537 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005538 /* For a range we allow invalid values and return an empty
5539 * list. A list index out of range is an error. */
5540 if (!range)
5541 {
5542 if (verbose)
5543 EMSGN(_(e_listidx), n1);
5544 return FAIL;
5545 }
5546 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005547 }
5548 if (range)
5549 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005550 list_T *l;
5551 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552
5553 if (n2 < 0)
5554 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005555 else if (n2 >= len)
5556 n2 = len - 1;
5557 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005558 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 l = list_alloc();
5560 if (l == NULL)
5561 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005562 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005563 n1 <= n2; ++n1)
5564 {
5565 if (list_append_tv(l, &item->li_tv) == FAIL)
5566 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005567 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005568 return FAIL;
5569 }
5570 item = item->li_next;
5571 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005572 clear_tv(rettv);
5573 rettv->v_type = VAR_LIST;
5574 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005575 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005576 }
5577 else
5578 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005579 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005580 clear_tv(rettv);
5581 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 }
5583 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005584
5585 case VAR_DICT:
5586 if (range)
5587 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005588 if (verbose)
5589 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005590 if (len == -1)
5591 clear_tv(&var1);
5592 return FAIL;
5593 }
5594 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005595 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005596
5597 if (len == -1)
5598 {
5599 key = get_tv_string(&var1);
5600 if (*key == NUL)
5601 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005602 if (verbose)
5603 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005604 clear_tv(&var1);
5605 return FAIL;
5606 }
5607 }
5608
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005609 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005610
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005611 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005612 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005613 if (len == -1)
5614 clear_tv(&var1);
5615 if (item == NULL)
5616 return FAIL;
5617
5618 copy_tv(&item->di_tv, &var1);
5619 clear_tv(rettv);
5620 *rettv = var1;
5621 }
5622 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005623 }
5624 }
5625
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005626 return OK;
5627}
5628
5629/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005630 * Get an option value.
5631 * "arg" points to the '&' or '+' before the option name.
5632 * "arg" is advanced to character after the option name.
5633 * Return OK or FAIL.
5634 */
5635 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005636get_option_tv(
5637 char_u **arg,
5638 typval_T *rettv, /* when NULL, only check if option exists */
5639 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005640{
5641 char_u *option_end;
5642 long numval;
5643 char_u *stringval;
5644 int opt_type;
5645 int c;
5646 int working = (**arg == '+'); /* has("+option") */
5647 int ret = OK;
5648 int opt_flags;
5649
5650 /*
5651 * Isolate the option name and find its value.
5652 */
5653 option_end = find_option_end(arg, &opt_flags);
5654 if (option_end == NULL)
5655 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005656 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005657 EMSG2(_("E112: Option name missing: %s"), *arg);
5658 return FAIL;
5659 }
5660
5661 if (!evaluate)
5662 {
5663 *arg = option_end;
5664 return OK;
5665 }
5666
5667 c = *option_end;
5668 *option_end = NUL;
5669 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005670 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005671
5672 if (opt_type == -3) /* invalid name */
5673 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005674 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005675 EMSG2(_("E113: Unknown option: %s"), *arg);
5676 ret = FAIL;
5677 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 {
5680 if (opt_type == -2) /* hidden string option */
5681 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005682 rettv->v_type = VAR_STRING;
5683 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005684 }
5685 else if (opt_type == -1) /* hidden number option */
5686 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005687 rettv->v_type = VAR_NUMBER;
5688 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005689 }
5690 else if (opt_type == 1) /* number option */
5691 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv->v_type = VAR_NUMBER;
5693 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 }
5695 else /* string option */
5696 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 rettv->v_type = VAR_STRING;
5698 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005699 }
5700 }
5701 else if (working && (opt_type == -2 || opt_type == -1))
5702 ret = FAIL;
5703
5704 *option_end = c; /* put back for error messages */
5705 *arg = option_end;
5706
5707 return ret;
5708}
5709
5710/*
5711 * Allocate a variable for a string constant.
5712 * Return OK or FAIL.
5713 */
5714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005715get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716{
5717 char_u *p;
5718 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 int extra = 0;
5720
5721 /*
5722 * Find the end of the string, skipping backslashed characters.
5723 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005724 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005725 {
5726 if (*p == '\\' && p[1] != NUL)
5727 {
5728 ++p;
5729 /* A "\<x>" form occupies at least 4 characters, and produces up
5730 * to 6 characters: reserve space for 2 extra */
5731 if (*p == '<')
5732 extra += 2;
5733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005734 }
5735
5736 if (*p != '"')
5737 {
5738 EMSG2(_("E114: Missing quote: %s"), *arg);
5739 return FAIL;
5740 }
5741
5742 /* If only parsing, set *arg and return here */
5743 if (!evaluate)
5744 {
5745 *arg = p + 1;
5746 return OK;
5747 }
5748
5749 /*
5750 * Copy the string into allocated memory, handling backslashed
5751 * characters.
5752 */
5753 name = alloc((unsigned)(p - *arg + extra));
5754 if (name == NULL)
5755 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005756 rettv->v_type = VAR_STRING;
5757 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005758
Bram Moolenaar8c711452005-01-14 21:53:12 +00005759 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005760 {
5761 if (*p == '\\')
5762 {
5763 switch (*++p)
5764 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005765 case 'b': *name++ = BS; ++p; break;
5766 case 'e': *name++ = ESC; ++p; break;
5767 case 'f': *name++ = FF; ++p; break;
5768 case 'n': *name++ = NL; ++p; break;
5769 case 'r': *name++ = CAR; ++p; break;
5770 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005771
5772 case 'X': /* hex: "\x1", "\x12" */
5773 case 'x':
5774 case 'u': /* Unicode: "\u0023" */
5775 case 'U':
5776 if (vim_isxdigit(p[1]))
5777 {
5778 int n, nr;
5779 int c = toupper(*p);
5780
5781 if (c == 'X')
5782 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005783 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005784 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005785 else
5786 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005787 nr = 0;
5788 while (--n >= 0 && vim_isxdigit(p[1]))
5789 {
5790 ++p;
5791 nr = (nr << 4) + hex2nr(*p);
5792 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005793 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005794#ifdef FEAT_MBYTE
5795 /* For "\u" store the number according to
5796 * 'encoding'. */
5797 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005798 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005799 else
5800#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005801 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005802 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 break;
5804
5805 /* octal: "\1", "\12", "\123" */
5806 case '0':
5807 case '1':
5808 case '2':
5809 case '3':
5810 case '4':
5811 case '5':
5812 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005813 case '7': *name = *p++ - '0';
5814 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005815 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005816 *name = (*name << 3) + *p++ - '0';
5817 if (*p >= '0' && *p <= '7')
5818 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 break;
5822
5823 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005824 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 if (extra != 0)
5826 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005827 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 break;
5829 }
5830 /* FALLTHROUGH */
5831
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005833 break;
5834 }
5835 }
5836 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005837 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838
Bram Moolenaar071d4272004-06-13 20:20:40 +00005839 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005840 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 *arg = p + 1;
5842
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 return OK;
5844}
5845
5846/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005847 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005848 * Return OK or FAIL.
5849 */
5850 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005851get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852{
5853 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005854 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005855 int reduce = 0;
5856
5857 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005858 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005859 */
5860 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5861 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005863 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005864 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005865 break;
5866 ++reduce;
5867 ++p;
5868 }
5869 }
5870
Bram Moolenaar8c711452005-01-14 21:53:12 +00005871 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005872 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005873 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 return FAIL;
5875 }
5876
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 if (!evaluate)
5879 {
5880 *arg = p + 1;
5881 return OK;
5882 }
5883
5884 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005885 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005886 */
5887 str = alloc((unsigned)((p - *arg) - reduce));
5888 if (str == NULL)
5889 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 rettv->v_type = VAR_STRING;
5891 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005892
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005897 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005898 break;
5899 ++p;
5900 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005901 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005902 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005903 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005904 *arg = p + 1;
5905
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005906 return OK;
5907}
5908
5909/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005910 * Allocate a variable for a List and fill it from "*arg".
5911 * Return OK or FAIL.
5912 */
5913 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005914get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005915{
Bram Moolenaar33570922005-01-25 22:26:29 +00005916 list_T *l = NULL;
5917 typval_T tv;
5918 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005919
5920 if (evaluate)
5921 {
5922 l = list_alloc();
5923 if (l == NULL)
5924 return FAIL;
5925 }
5926
5927 *arg = skipwhite(*arg + 1);
5928 while (**arg != ']' && **arg != NUL)
5929 {
5930 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5931 goto failret;
5932 if (evaluate)
5933 {
5934 item = listitem_alloc();
5935 if (item != NULL)
5936 {
5937 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005938 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005939 list_append(l, item);
5940 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005941 else
5942 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005943 }
5944
5945 if (**arg == ']')
5946 break;
5947 if (**arg != ',')
5948 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005949 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005950 goto failret;
5951 }
5952 *arg = skipwhite(*arg + 1);
5953 }
5954
5955 if (**arg != ']')
5956 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005957 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958failret:
5959 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005960 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 return FAIL;
5962 }
5963
5964 *arg = skipwhite(*arg + 1);
5965 if (evaluate)
5966 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005967 rettv->v_type = VAR_LIST;
5968 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 ++l->lv_refcount;
5970 }
5971
5972 return OK;
5973}
5974
5975/*
5976 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005977 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005978 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005979 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005980list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005981{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00005982 list_T *l;
5983
5984 l = (list_T *)alloc_clear(sizeof(list_T));
5985 if (l != NULL)
5986 {
5987 /* Prepend the list to the list of lists for garbage collection. */
5988 if (first_list != NULL)
5989 first_list->lv_used_prev = l;
5990 l->lv_used_prev = NULL;
5991 l->lv_used_next = first_list;
5992 first_list = l;
5993 }
5994 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005995}
5996
5997/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00005998 * Allocate an empty list for a return value.
5999 * Returns OK or FAIL.
6000 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006001 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006003{
6004 list_T *l = list_alloc();
6005
6006 if (l == NULL)
6007 return FAIL;
6008
6009 rettv->vval.v_list = l;
6010 rettv->v_type = VAR_LIST;
6011 ++l->lv_refcount;
6012 return OK;
6013}
6014
6015/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006016 * Unreference a list: decrement the reference count and free it when it
6017 * becomes zero.
6018 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006019 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006020list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006021{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006022 if (l != NULL && --l->lv_refcount <= 0)
6023 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006024}
6025
6026/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006027 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006028 * Ignores the reference count.
6029 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006030 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006031list_free(
6032 list_T *l,
6033 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006034{
Bram Moolenaar33570922005-01-25 22:26:29 +00006035 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006037 /* Remove the list from the list of lists for garbage collection. */
6038 if (l->lv_used_prev == NULL)
6039 first_list = l->lv_used_next;
6040 else
6041 l->lv_used_prev->lv_used_next = l->lv_used_next;
6042 if (l->lv_used_next != NULL)
6043 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6044
Bram Moolenaard9fba312005-06-26 22:34:35 +00006045 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006047 /* Remove the item before deleting it. */
6048 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006049 if (recurse || (item->li_tv.v_type != VAR_LIST
6050 && item->li_tv.v_type != VAR_DICT))
6051 clear_tv(&item->li_tv);
6052 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053 }
6054 vim_free(l);
6055}
6056
6057/*
6058 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006059 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006061 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006062listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006063{
Bram Moolenaar33570922005-01-25 22:26:29 +00006064 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065}
6066
6067/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006068 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006069 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006070 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006071listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006073 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006074 vim_free(item);
6075}
6076
6077/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006078 * Remove a list item from a List and free it. Also clears the value.
6079 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006080 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006081listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006082{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006083 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006084 listitem_free(item);
6085}
6086
6087/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 * Get the number of items in a list.
6089 */
6090 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 if (l == NULL)
6094 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006095 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096}
6097
6098/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006099 * Return TRUE when two lists have exactly the same values.
6100 */
6101 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006102list_equal(
6103 list_T *l1,
6104 list_T *l2,
6105 int ic, /* ignore case for strings */
6106 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006107{
Bram Moolenaar33570922005-01-25 22:26:29 +00006108 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006109
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006110 if (l1 == NULL || l2 == NULL)
6111 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006112 if (l1 == l2)
6113 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006114 if (list_len(l1) != list_len(l2))
6115 return FALSE;
6116
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006117 for (item1 = l1->lv_first, item2 = l2->lv_first;
6118 item1 != NULL && item2 != NULL;
6119 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006120 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 return FALSE;
6122 return item1 == NULL && item2 == NULL;
6123}
6124
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006125/*
6126 * Return the dictitem that an entry in a hashtable points to.
6127 */
6128 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006129dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006130{
6131 return HI2DI(hi);
6132}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006133
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006134/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006135 * Return TRUE when two dictionaries have exactly the same key/values.
6136 */
6137 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006138dict_equal(
6139 dict_T *d1,
6140 dict_T *d2,
6141 int ic, /* ignore case for strings */
6142 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006143{
Bram Moolenaar33570922005-01-25 22:26:29 +00006144 hashitem_T *hi;
6145 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006146 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006147
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006148 if (d1 == NULL || d2 == NULL)
6149 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006150 if (d1 == d2)
6151 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006152 if (dict_len(d1) != dict_len(d2))
6153 return FALSE;
6154
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006155 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006156 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006158 if (!HASHITEM_EMPTY(hi))
6159 {
6160 item2 = dict_find(d2, hi->hi_key, -1);
6161 if (item2 == NULL)
6162 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006163 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006164 return FALSE;
6165 --todo;
6166 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167 }
6168 return TRUE;
6169}
6170
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006171static int tv_equal_recurse_limit;
6172
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006173/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006174 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006175 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006176 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006177 */
6178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006179tv_equal(
6180 typval_T *tv1,
6181 typval_T *tv2,
6182 int ic, /* ignore case */
6183 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006184{
6185 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006186 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006187 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006188 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006189
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006190 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6191 if ((tv1->v_type == VAR_FUNC
6192 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6193 && (tv2->v_type == VAR_FUNC
6194 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6195 {
6196 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6197 : tv1->vval.v_partial->pt_name;
6198 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6199 : tv2->vval.v_partial->pt_name;
6200 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6201 }
6202
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006203 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006204 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006205
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006206 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 * recursiveness to a limit. We guess they are equal then.
6208 * A fixed limit has the problem of still taking an awful long time.
6209 * Reduce the limit every time running into it. That should work fine for
6210 * deeply linked structures that are not recursively linked and catch
6211 * recursiveness quickly. */
6212 if (!recursive)
6213 tv_equal_recurse_limit = 1000;
6214 if (recursive_cnt >= tv_equal_recurse_limit)
6215 {
6216 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006217 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006218 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006219
6220 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006221 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006222 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006223 ++recursive_cnt;
6224 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6225 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006226 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006227
6228 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006229 ++recursive_cnt;
6230 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6231 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006232 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006233
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006234 case VAR_NUMBER:
6235 return tv1->vval.v_number == tv2->vval.v_number;
6236
6237 case VAR_STRING:
6238 s1 = get_tv_string_buf(tv1, buf1);
6239 s2 = get_tv_string_buf(tv2, buf2);
6240 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006241
6242 case VAR_SPECIAL:
6243 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006244
6245 case VAR_FLOAT:
6246#ifdef FEAT_FLOAT
6247 return tv1->vval.v_float == tv2->vval.v_float;
6248#endif
6249 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006250#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006251 return tv1->vval.v_job == tv2->vval.v_job;
6252#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006253 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006254#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006255 return tv1->vval.v_channel == tv2->vval.v_channel;
6256#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006257 case VAR_FUNC:
6258 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006259 case VAR_UNKNOWN:
6260 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006261 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006262
Bram Moolenaara03f2332016-02-06 18:09:59 +01006263 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6264 * does not equal anything, not even itself. */
6265 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006266}
6267
6268/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006269 * Locate item with index "n" in list "l" and return it.
6270 * A negative index is counted from the end; -1 is the last item.
6271 * Returns NULL when "n" is out of range.
6272 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006273 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006274list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006275{
Bram Moolenaar33570922005-01-25 22:26:29 +00006276 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006277 long idx;
6278
6279 if (l == NULL)
6280 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006281
6282 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006283 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006284 n = l->lv_len + n;
6285
6286 /* Check for index out of range. */
6287 if (n < 0 || n >= l->lv_len)
6288 return NULL;
6289
6290 /* When there is a cached index may start search from there. */
6291 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006292 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006293 if (n < l->lv_idx / 2)
6294 {
6295 /* closest to the start of the list */
6296 item = l->lv_first;
6297 idx = 0;
6298 }
6299 else if (n > (l->lv_idx + l->lv_len) / 2)
6300 {
6301 /* closest to the end of the list */
6302 item = l->lv_last;
6303 idx = l->lv_len - 1;
6304 }
6305 else
6306 {
6307 /* closest to the cached index */
6308 item = l->lv_idx_item;
6309 idx = l->lv_idx;
6310 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 }
6312 else
6313 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006314 if (n < l->lv_len / 2)
6315 {
6316 /* closest to the start of the list */
6317 item = l->lv_first;
6318 idx = 0;
6319 }
6320 else
6321 {
6322 /* closest to the end of the list */
6323 item = l->lv_last;
6324 idx = l->lv_len - 1;
6325 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006326 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006327
6328 while (n > idx)
6329 {
6330 /* search forward */
6331 item = item->li_next;
6332 ++idx;
6333 }
6334 while (n < idx)
6335 {
6336 /* search backward */
6337 item = item->li_prev;
6338 --idx;
6339 }
6340
6341 /* cache the used index */
6342 l->lv_idx = idx;
6343 l->lv_idx_item = item;
6344
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 return item;
6346}
6347
6348/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006349 * Get list item "l[idx]" as a number.
6350 */
6351 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006352list_find_nr(
6353 list_T *l,
6354 long idx,
6355 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006356{
6357 listitem_T *li;
6358
6359 li = list_find(l, idx);
6360 if (li == NULL)
6361 {
6362 if (errorp != NULL)
6363 *errorp = TRUE;
6364 return -1L;
6365 }
6366 return get_tv_number_chk(&li->li_tv, errorp);
6367}
6368
6369/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006370 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6371 */
6372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006373list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006374{
6375 listitem_T *li;
6376
6377 li = list_find(l, idx - 1);
6378 if (li == NULL)
6379 {
6380 EMSGN(_(e_listidx), idx);
6381 return NULL;
6382 }
6383 return get_tv_string(&li->li_tv);
6384}
6385
6386/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006387 * Locate "item" list "l" and return its index.
6388 * Returns -1 when "item" is not in the list.
6389 */
6390 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006391list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006392{
6393 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006394 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006395
6396 if (l == NULL)
6397 return -1;
6398 idx = 0;
6399 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6400 ++idx;
6401 if (li == NULL)
6402 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006403 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006404}
6405
6406/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006407 * Append item "item" to the end of list "l".
6408 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006409 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006411{
6412 if (l->lv_last == NULL)
6413 {
6414 /* empty list */
6415 l->lv_first = item;
6416 l->lv_last = item;
6417 item->li_prev = NULL;
6418 }
6419 else
6420 {
6421 l->lv_last->li_next = item;
6422 item->li_prev = l->lv_last;
6423 l->lv_last = item;
6424 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006425 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 item->li_next = NULL;
6427}
6428
6429/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006430 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006431 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006432 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006433 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006434list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006435{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006436 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437
Bram Moolenaar05159a02005-02-26 23:04:13 +00006438 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006439 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006440 copy_tv(tv, &li->li_tv);
6441 list_append(l, li);
6442 return OK;
6443}
6444
6445/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006446 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006447 * Return FAIL when out of memory.
6448 */
6449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006451{
6452 listitem_T *li = listitem_alloc();
6453
6454 if (li == NULL)
6455 return FAIL;
6456 li->li_tv.v_type = VAR_DICT;
6457 li->li_tv.v_lock = 0;
6458 li->li_tv.vval.v_dict = dict;
6459 list_append(list, li);
6460 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461 return OK;
6462}
6463
6464/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006465 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006466 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006467 * Returns FAIL when out of memory.
6468 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006470list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006471{
6472 listitem_T *li = listitem_alloc();
6473
6474 if (li == NULL)
6475 return FAIL;
6476 list_append(l, li);
6477 li->li_tv.v_type = VAR_STRING;
6478 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006479 if (str == NULL)
6480 li->li_tv.vval.v_string = NULL;
6481 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006482 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006483 return FAIL;
6484 return OK;
6485}
6486
6487/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006488 * Append "n" to list "l".
6489 * Returns FAIL when out of memory.
6490 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006491 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006492list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006493{
6494 listitem_T *li;
6495
6496 li = listitem_alloc();
6497 if (li == NULL)
6498 return FAIL;
6499 li->li_tv.v_type = VAR_NUMBER;
6500 li->li_tv.v_lock = 0;
6501 li->li_tv.vval.v_number = n;
6502 list_append(l, li);
6503 return OK;
6504}
6505
6506/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006507 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006508 * If "item" is NULL append at the end.
6509 * Return FAIL when out of memory.
6510 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006513{
Bram Moolenaar33570922005-01-25 22:26:29 +00006514 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006515
6516 if (ni == NULL)
6517 return FAIL;
6518 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006519 list_insert(l, ni, item);
6520 return OK;
6521}
6522
6523 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006524list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006525{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006526 if (item == NULL)
6527 /* Append new item at end of list. */
6528 list_append(l, ni);
6529 else
6530 {
6531 /* Insert new item before existing item. */
6532 ni->li_prev = item->li_prev;
6533 ni->li_next = item;
6534 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006535 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006536 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006537 ++l->lv_idx;
6538 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006540 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006542 l->lv_idx_item = NULL;
6543 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006544 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006545 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006547}
6548
6549/*
6550 * Extend "l1" with "l2".
6551 * If "bef" is NULL append at the end, otherwise insert before this item.
6552 * Returns FAIL when out of memory.
6553 */
6554 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006555list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556{
Bram Moolenaar33570922005-01-25 22:26:29 +00006557 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006558 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006560 /* We also quit the loop when we have inserted the original item count of
6561 * the list, avoid a hang when we extend a list with itself. */
6562 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6564 return FAIL;
6565 return OK;
6566}
6567
6568/*
6569 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6570 * Return FAIL when out of memory.
6571 */
6572 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006573list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006574{
Bram Moolenaar33570922005-01-25 22:26:29 +00006575 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006577 if (l1 == NULL || l2 == NULL)
6578 return FAIL;
6579
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006580 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006581 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 if (l == NULL)
6583 return FAIL;
6584 tv->v_type = VAR_LIST;
6585 tv->vval.v_list = l;
6586
6587 /* append all items from the second list */
6588 return list_extend(l, l2, NULL);
6589}
6590
6591/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006592 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006593 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006594 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006595 * Returns NULL when out of memory.
6596 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006597 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006598list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006599{
Bram Moolenaar33570922005-01-25 22:26:29 +00006600 list_T *copy;
6601 listitem_T *item;
6602 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006603
6604 if (orig == NULL)
6605 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006606
6607 copy = list_alloc();
6608 if (copy != NULL)
6609 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 if (copyID != 0)
6611 {
6612 /* Do this before adding the items, because one of the items may
6613 * refer back to this list. */
6614 orig->lv_copyID = copyID;
6615 orig->lv_copylist = copy;
6616 }
6617 for (item = orig->lv_first; item != NULL && !got_int;
6618 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 {
6620 ni = listitem_alloc();
6621 if (ni == NULL)
6622 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006623 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006624 {
6625 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6626 {
6627 vim_free(ni);
6628 break;
6629 }
6630 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006631 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006632 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006633 list_append(copy, ni);
6634 }
6635 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 if (item != NULL)
6637 {
6638 list_unref(copy);
6639 copy = NULL;
6640 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006641 }
6642
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006643 return copy;
6644}
6645
6646/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006647 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006648 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006649 * This used to be called list_remove, but that conflicts with a Sun header
6650 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006652 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006653vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006654{
Bram Moolenaar33570922005-01-25 22:26:29 +00006655 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006656
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006657 /* notify watchers */
6658 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006660 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006661 list_fix_watch(l, ip);
6662 if (ip == item2)
6663 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006664 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006665
6666 if (item2->li_next == NULL)
6667 l->lv_last = item->li_prev;
6668 else
6669 item2->li_next->li_prev = item->li_prev;
6670 if (item->li_prev == NULL)
6671 l->lv_first = item2->li_next;
6672 else
6673 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006674 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675}
6676
6677/*
6678 * Return an allocated string with the string representation of a list.
6679 * May return NULL.
6680 */
6681 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006682list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683{
6684 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685
6686 if (tv->vval.v_list == NULL)
6687 return NULL;
6688 ga_init2(&ga, (int)sizeof(char), 80);
6689 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006690 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006691 {
6692 vim_free(ga.ga_data);
6693 return NULL;
6694 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695 ga_append(&ga, ']');
6696 ga_append(&ga, NUL);
6697 return (char_u *)ga.ga_data;
6698}
6699
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006700typedef struct join_S {
6701 char_u *s;
6702 char_u *tofree;
6703} join_T;
6704
6705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006706list_join_inner(
6707 garray_T *gap, /* to store the result in */
6708 list_T *l,
6709 char_u *sep,
6710 int echo_style,
6711 int copyID,
6712 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006713{
6714 int i;
6715 join_T *p;
6716 int len;
6717 int sumlen = 0;
6718 int first = TRUE;
6719 char_u *tofree;
6720 char_u numbuf[NUMBUFLEN];
6721 listitem_T *item;
6722 char_u *s;
6723
6724 /* Stringify each item in the list. */
6725 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6726 {
6727 if (echo_style)
6728 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6729 else
6730 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6731 if (s == NULL)
6732 return FAIL;
6733
6734 len = (int)STRLEN(s);
6735 sumlen += len;
6736
Bram Moolenaarcde88542015-08-11 19:14:00 +02006737 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006738 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6739 if (tofree != NULL || s != numbuf)
6740 {
6741 p->s = s;
6742 p->tofree = tofree;
6743 }
6744 else
6745 {
6746 p->s = vim_strnsave(s, len);
6747 p->tofree = p->s;
6748 }
6749
6750 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006751 if (did_echo_string_emsg) /* recursion error, bail out */
6752 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006753 }
6754
6755 /* Allocate result buffer with its total size, avoid re-allocation and
6756 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6757 if (join_gap->ga_len >= 2)
6758 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6759 if (ga_grow(gap, sumlen + 2) == FAIL)
6760 return FAIL;
6761
6762 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6763 {
6764 if (first)
6765 first = FALSE;
6766 else
6767 ga_concat(gap, sep);
6768 p = ((join_T *)join_gap->ga_data) + i;
6769
6770 if (p->s != NULL)
6771 ga_concat(gap, p->s);
6772 line_breakcheck();
6773 }
6774
6775 return OK;
6776}
6777
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006778/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006779 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006780 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006781 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006782 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006783 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006784list_join(
6785 garray_T *gap,
6786 list_T *l,
6787 char_u *sep,
6788 int echo_style,
6789 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006790{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006791 garray_T join_ga;
6792 int retval;
6793 join_T *p;
6794 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795
Bram Moolenaard39a7512015-04-16 22:51:22 +02006796 if (l->lv_len < 1)
6797 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006798 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6799 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6800
6801 /* Dispose each item in join_ga. */
6802 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006803 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006804 p = (join_T *)join_ga.ga_data;
6805 for (i = 0; i < join_ga.ga_len; ++i)
6806 {
6807 vim_free(p->tofree);
6808 ++p;
6809 }
6810 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006812
6813 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814}
6815
6816/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006817 * Return the next (unique) copy ID.
6818 * Used for serializing nested structures.
6819 */
6820 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006821get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006822{
6823 current_copyID += COPYID_INC;
6824 return current_copyID;
6825}
6826
6827/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006828 * Garbage collection for lists and dictionaries.
6829 *
6830 * We use reference counts to be able to free most items right away when they
6831 * are no longer used. But for composite items it's possible that it becomes
6832 * unused while the reference count is > 0: When there is a recursive
6833 * reference. Example:
6834 * :let l = [1, 2, 3]
6835 * :let d = {9: l}
6836 * :let l[1] = d
6837 *
6838 * Since this is quite unusual we handle this with garbage collection: every
6839 * once in a while find out which lists and dicts are not referenced from any
6840 * variable.
6841 *
6842 * Here is a good reference text about garbage collection (refers to Python
6843 * but it applies to all reference-counting mechanisms):
6844 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006845 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006846
6847/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 * Do garbage collection for lists and dicts.
6849 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006850 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006851 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006852garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006853{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006854 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006855 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006856 buf_T *buf;
6857 win_T *wp;
6858 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006859 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006860 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006861 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006862#ifdef FEAT_WINDOWS
6863 tabpage_T *tp;
6864#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006865
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006866 /* Only do this once. */
6867 want_garbage_collect = FALSE;
6868 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006869 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006870
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006871 /* We advance by two because we add one for items referenced through
6872 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006873 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 /*
6876 * 1. Go through all accessible variables and mark all lists and dicts
6877 * with copyID.
6878 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006879
6880 /* Don't free variables in the previous_funccal list unless they are only
6881 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006882 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006883 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6884 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006885 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6886 NULL);
6887 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6888 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006889 }
6890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /* script-local variables */
6892 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006893 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894
6895 /* buffer-local variables */
6896 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006897 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6898 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006899
6900 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006901 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006902 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6903 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006904#ifdef FEAT_AUTOCMD
6905 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006906 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6907 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006908#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006909
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006910#ifdef FEAT_WINDOWS
6911 /* tabpage-local variables */
6912 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006915#endif
6916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
6920 /* function-local variables */
6921 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6922 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6924 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925 }
6926
Bram Moolenaard812df62008-11-09 12:46:09 +00006927 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006929
Bram Moolenaar1dced572012-04-05 16:54:08 +02006930#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006931 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006932#endif
6933
Bram Moolenaardb913952012-06-29 12:54:53 +02006934#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006935 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006936#endif
6937
6938#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006940#endif
6941
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006942#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006943 abort = abort || set_ref_in_channel(copyID);
6944#endif
6945
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006946 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006947 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 /*
6949 * 2. Free lists and dictionaries that are not referenced.
6950 */
6951 did_free = free_unref_items(copyID);
6952
6953 /*
6954 * 3. Check if any funccal can be freed now.
6955 */
6956 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006957 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 if (can_free_funccal(*pfc, copyID))
6959 {
6960 fc = *pfc;
6961 *pfc = fc->caller;
6962 free_funccal(fc, TRUE);
6963 did_free = TRUE;
6964 did_free_funccal = TRUE;
6965 }
6966 else
6967 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006968 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006969 if (did_free_funccal)
6970 /* When a funccal was freed some more items might be garbage
6971 * collected, so run again. */
6972 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 else if (p_verbose > 0)
6975 {
6976 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6977 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006978
6979 return did_free;
6980}
6981
6982/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006983 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 */
6985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006986free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987{
Bram Moolenaare71eea82015-02-03 17:10:06 +01006988 dict_T *dd, *dd_next;
6989 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006990 int did_free = FALSE;
6991
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006992 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006994 */
6995 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01006996 {
6997 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006999 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007000 /* Free the Dictionary and ordinary items it contains, but don't
7001 * recurse into Lists and Dictionaries, they will be in the list
7002 * of dicts or list of lists. */
7003 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007004 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007005 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007006 dd = dd_next;
7007 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008
7009 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007010 * Go through the list of lists and free items without the copyID.
7011 * But don't free a list that has a watcher (used in a for loop), these
7012 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 */
7014 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007015 {
7016 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007017 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7018 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 /* Free the List and ordinary items it contains, but don't recurse
7021 * into Lists and Dictionaries, they will be in the list of dicts
7022 * or list of lists. */
7023 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007026 ll = ll_next;
7027 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007028
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 return did_free;
7030}
7031
7032/*
7033 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007034 * "list_stack" is used to add lists to be marked. Can be NULL.
7035 *
7036 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007037 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007038 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007039set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040{
7041 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007042 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007044 hashtab_T *cur_ht;
7045 ht_stack_T *ht_stack = NULL;
7046 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007047
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007048 cur_ht = ht;
7049 for (;;)
7050 {
7051 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007052 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 /* Mark each item in the hashtab. If the item contains a hashtab
7054 * it is added to ht_stack, if it contains a list it is added to
7055 * list_stack. */
7056 todo = (int)cur_ht->ht_used;
7057 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7058 if (!HASHITEM_EMPTY(hi))
7059 {
7060 --todo;
7061 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7062 &ht_stack, list_stack);
7063 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007064 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007065
7066 if (ht_stack == NULL)
7067 break;
7068
7069 /* take an item from the stack */
7070 cur_ht = ht_stack->ht;
7071 tempitem = ht_stack;
7072 ht_stack = ht_stack->prev;
7073 free(tempitem);
7074 }
7075
7076 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007077}
7078
7079/*
7080 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7082 *
7083 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007086set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007087{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007088 listitem_T *li;
7089 int abort = FALSE;
7090 list_T *cur_l;
7091 list_stack_T *list_stack = NULL;
7092 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007094 cur_l = l;
7095 for (;;)
7096 {
7097 if (!abort)
7098 /* Mark each item in the list. If the item contains a hashtab
7099 * it is added to ht_stack, if it contains a list it is added to
7100 * list_stack. */
7101 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7102 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7103 ht_stack, &list_stack);
7104 if (list_stack == NULL)
7105 break;
7106
7107 /* take an item from the stack */
7108 cur_l = list_stack->list;
7109 tempitem = list_stack;
7110 list_stack = list_stack->prev;
7111 free(tempitem);
7112 }
7113
7114 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007115}
7116
7117/*
7118 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007119 * "list_stack" is used to add lists to be marked. Can be NULL.
7120 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7121 *
7122 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007123 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007124 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007125set_ref_in_item(
7126 typval_T *tv,
7127 int copyID,
7128 ht_stack_T **ht_stack,
7129 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007130{
7131 dict_T *dd;
7132 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007133 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007134
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007135 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007136 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007137 if (tv->v_type == VAR_DICT)
7138 dd = tv->vval.v_dict;
7139 else if (tv->vval.v_partial != NULL)
7140 dd = tv->vval.v_partial->pt_dict;
7141 else
7142 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007143 if (dd != NULL && dd->dv_copyID != copyID)
7144 {
7145 /* Didn't see this dict yet. */
7146 dd->dv_copyID = copyID;
7147 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007148 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007149 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7150 }
7151 else
7152 {
7153 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7154 if (newitem == NULL)
7155 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007156 else
7157 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007158 newitem->ht = &dd->dv_hashtab;
7159 newitem->prev = *ht_stack;
7160 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007161 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 }
7164 }
7165 else if (tv->v_type == VAR_LIST)
7166 {
7167 ll = tv->vval.v_list;
7168 if (ll != NULL && ll->lv_copyID != copyID)
7169 {
7170 /* Didn't see this list yet. */
7171 ll->lv_copyID = copyID;
7172 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007174 abort = set_ref_in_list(ll, copyID, ht_stack);
7175 }
7176 else
7177 {
7178 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007179 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007180 if (newitem == NULL)
7181 abort = TRUE;
7182 else
7183 {
7184 newitem->list = ll;
7185 newitem->prev = *list_stack;
7186 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007187 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007188 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007189 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007190 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007191 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192}
7193
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007194 static void
7195partial_free(partial_T *pt, int free_dict)
7196{
7197 int i;
7198
7199 for (i = 0; i < pt->pt_argc; ++i)
7200 clear_tv(&pt->pt_argv[i]);
7201 vim_free(pt->pt_argv);
7202 if (free_dict)
7203 dict_unref(pt->pt_dict);
7204 func_unref(pt->pt_name);
7205 vim_free(pt->pt_name);
7206 vim_free(pt);
7207}
7208
7209/*
7210 * Unreference a closure: decrement the reference count and free it when it
7211 * becomes zero.
7212 */
7213 void
7214partial_unref(partial_T *pt)
7215{
7216 if (pt != NULL && --pt->pt_refcount <= 0)
7217 partial_free(pt, TRUE);
7218}
7219
Bram Moolenaard9fba312005-06-26 22:34:35 +00007220/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007221 * Allocate an empty header for a dictionary.
7222 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007223 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007224dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007225{
Bram Moolenaar33570922005-01-25 22:26:29 +00007226 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007227
Bram Moolenaar33570922005-01-25 22:26:29 +00007228 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007230 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007231 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007232 if (first_dict != NULL)
7233 first_dict->dv_used_prev = d;
7234 d->dv_used_next = first_dict;
7235 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007236 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007237
Bram Moolenaar33570922005-01-25 22:26:29 +00007238 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007239 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007240 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007241 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007242 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007243 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007244 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245}
7246
7247/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007248 * Allocate an empty dict for a return value.
7249 * Returns OK or FAIL.
7250 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007251 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007252rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007253{
7254 dict_T *d = dict_alloc();
7255
7256 if (d == NULL)
7257 return FAIL;
7258
7259 rettv->vval.v_dict = d;
7260 rettv->v_type = VAR_DICT;
7261 ++d->dv_refcount;
7262 return OK;
7263}
7264
7265
7266/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007267 * Unreference a Dictionary: decrement the reference count and free it when it
7268 * becomes zero.
7269 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007270 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007272{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007273 if (d != NULL && --d->dv_refcount <= 0)
7274 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007275}
7276
7277/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007278 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007279 * Ignores the reference count.
7280 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007281 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007282dict_free(
7283 dict_T *d,
7284 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007285{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007286 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007287 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007288 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007289
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007290 /* Remove the dict from the list of dicts for garbage collection. */
7291 if (d->dv_used_prev == NULL)
7292 first_dict = d->dv_used_next;
7293 else
7294 d->dv_used_prev->dv_used_next = d->dv_used_next;
7295 if (d->dv_used_next != NULL)
7296 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7297
7298 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007299 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007300 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007303 if (!HASHITEM_EMPTY(hi))
7304 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007305 /* Remove the item before deleting it, just in case there is
7306 * something recursive causing trouble. */
7307 di = HI2DI(hi);
7308 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007309 if (recurse || (di->di_tv.v_type != VAR_LIST
7310 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007311 {
7312 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7313 {
7314 partial_T *pt = di->di_tv.vval.v_partial;
7315
7316 /* We unref the partial but not the dict it refers to. */
7317 if (pt != NULL && --pt->pt_refcount == 0)
7318 partial_free(pt, FALSE);
7319 }
7320 else
7321 clear_tv(&di->di_tv);
7322 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007323 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 --todo;
7325 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007326 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007327 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007328 vim_free(d);
7329}
7330
7331/*
7332 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007333 * The "key" is copied to the new item.
7334 * Note that the value of the item "di_tv" still needs to be initialized!
7335 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007336 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007337 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007338dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007339{
Bram Moolenaar33570922005-01-25 22:26:29 +00007340 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007341
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007342 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007344 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007346 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007347 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007348 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349}
7350
7351/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007352 * Make a copy of a Dictionary item.
7353 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007355dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007356{
Bram Moolenaar33570922005-01-25 22:26:29 +00007357 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007358
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007359 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7360 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007361 if (di != NULL)
7362 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007363 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007364 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007365 copy_tv(&org->di_tv, &di->di_tv);
7366 }
7367 return di;
7368}
7369
7370/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007371 * Remove item "item" from Dictionary "dict" and free it.
7372 */
7373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007374dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007377
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007379 if (HASHITEM_EMPTY(hi))
7380 EMSG2(_(e_intern2), "dictitem_remove()");
7381 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007382 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007383 dictitem_free(item);
7384}
7385
7386/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007387 * Free a dict item. Also clears the value.
7388 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007389 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007390dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007391{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007392 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007393 if (item->di_flags & DI_FLAGS_ALLOC)
7394 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007395}
7396
7397/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7399 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007400 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007401 * Returns NULL when out of memory.
7402 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007404dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007405{
Bram Moolenaar33570922005-01-25 22:26:29 +00007406 dict_T *copy;
7407 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007408 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007410
7411 if (orig == NULL)
7412 return NULL;
7413
7414 copy = dict_alloc();
7415 if (copy != NULL)
7416 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007417 if (copyID != 0)
7418 {
7419 orig->dv_copyID = copyID;
7420 orig->dv_copydict = copy;
7421 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007422 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007423 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007425 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 --todo;
7428
7429 di = dictitem_alloc(hi->hi_key);
7430 if (di == NULL)
7431 break;
7432 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007433 {
7434 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7435 copyID) == FAIL)
7436 {
7437 vim_free(di);
7438 break;
7439 }
7440 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007441 else
7442 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7443 if (dict_add(copy, di) == FAIL)
7444 {
7445 dictitem_free(di);
7446 break;
7447 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007448 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007449 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007450
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 if (todo > 0)
7453 {
7454 dict_unref(copy);
7455 copy = NULL;
7456 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007457 }
7458
7459 return copy;
7460}
7461
7462/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007463 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007464 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007465 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007466 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007467dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007468{
Bram Moolenaar33570922005-01-25 22:26:29 +00007469 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007470}
7471
Bram Moolenaar8c711452005-01-14 21:53:12 +00007472/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007473 * Add a number or string entry to dictionary "d".
7474 * When "str" is NULL use number "nr", otherwise use "str".
7475 * Returns FAIL when out of memory and when key already exists.
7476 */
7477 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007478dict_add_nr_str(
7479 dict_T *d,
7480 char *key,
7481 long nr,
7482 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007483{
7484 dictitem_T *item;
7485
7486 item = dictitem_alloc((char_u *)key);
7487 if (item == NULL)
7488 return FAIL;
7489 item->di_tv.v_lock = 0;
7490 if (str == NULL)
7491 {
7492 item->di_tv.v_type = VAR_NUMBER;
7493 item->di_tv.vval.v_number = nr;
7494 }
7495 else
7496 {
7497 item->di_tv.v_type = VAR_STRING;
7498 item->di_tv.vval.v_string = vim_strsave(str);
7499 }
7500 if (dict_add(d, item) == FAIL)
7501 {
7502 dictitem_free(item);
7503 return FAIL;
7504 }
7505 return OK;
7506}
7507
7508/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007509 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007510 * Returns FAIL when out of memory and when key already exists.
7511 */
7512 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007513dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007514{
7515 dictitem_T *item;
7516
7517 item = dictitem_alloc((char_u *)key);
7518 if (item == NULL)
7519 return FAIL;
7520 item->di_tv.v_lock = 0;
7521 item->di_tv.v_type = VAR_LIST;
7522 item->di_tv.vval.v_list = list;
7523 if (dict_add(d, item) == FAIL)
7524 {
7525 dictitem_free(item);
7526 return FAIL;
7527 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007528 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007529 return OK;
7530}
7531
7532/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007533 * Get the number of items in a Dictionary.
7534 */
7535 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007536dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007537{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007538 if (d == NULL)
7539 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007540 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007541}
7542
7543/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007544 * Find item "key[len]" in Dictionary "d".
7545 * If "len" is negative use strlen(key).
7546 * Returns NULL when not found.
7547 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007548 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007549dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007550{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007551#define AKEYLEN 200
7552 char_u buf[AKEYLEN];
7553 char_u *akey;
7554 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007555 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007556
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007557 if (len < 0)
7558 akey = key;
7559 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007560 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007561 tofree = akey = vim_strnsave(key, len);
7562 if (akey == NULL)
7563 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007564 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007565 else
7566 {
7567 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007568 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007569 akey = buf;
7570 }
7571
Bram Moolenaar33570922005-01-25 22:26:29 +00007572 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007573 vim_free(tofree);
7574 if (HASHITEM_EMPTY(hi))
7575 return NULL;
7576 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577}
7578
7579/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007580 * Get a string item from a dictionary.
7581 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007582 * Returns NULL if the entry doesn't exist or out of memory.
7583 */
7584 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007585get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007586{
7587 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007588 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007589
7590 di = dict_find(d, key, -1);
7591 if (di == NULL)
7592 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007593 s = get_tv_string(&di->di_tv);
7594 if (save && s != NULL)
7595 s = vim_strsave(s);
7596 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007597}
7598
7599/*
7600 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007601 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007602 */
7603 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007605{
7606 dictitem_T *di;
7607
7608 di = dict_find(d, key, -1);
7609 if (di == NULL)
7610 return 0;
7611 return get_tv_number(&di->di_tv);
7612}
7613
7614/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007615 * Return an allocated string with the string representation of a Dictionary.
7616 * May return NULL.
7617 */
7618 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007619dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007620{
7621 garray_T ga;
7622 int first = TRUE;
7623 char_u *tofree;
7624 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007625 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007626 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007627 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007628 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007629
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007630 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 return NULL;
7632 ga_init2(&ga, (int)sizeof(char), 80);
7633 ga_append(&ga, '{');
7634
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007635 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007636 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007638 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007640 --todo;
7641
7642 if (first)
7643 first = FALSE;
7644 else
7645 ga_concat(&ga, (char_u *)", ");
7646
7647 tofree = string_quote(hi->hi_key, FALSE);
7648 if (tofree != NULL)
7649 {
7650 ga_concat(&ga, tofree);
7651 vim_free(tofree);
7652 }
7653 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007654 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 if (s != NULL)
7656 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007657 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007658 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007659 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007660 line_breakcheck();
7661
Bram Moolenaar8c711452005-01-14 21:53:12 +00007662 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007664 if (todo > 0)
7665 {
7666 vim_free(ga.ga_data);
7667 return NULL;
7668 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007669
7670 ga_append(&ga, '}');
7671 ga_append(&ga, NUL);
7672 return (char_u *)ga.ga_data;
7673}
7674
7675/*
7676 * Allocate a variable for a Dictionary and fill it from "*arg".
7677 * Return OK or FAIL. Returns NOTDONE for {expr}.
7678 */
7679 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007680get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681{
Bram Moolenaar33570922005-01-25 22:26:29 +00007682 dict_T *d = NULL;
7683 typval_T tvkey;
7684 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007685 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007686 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007688 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689
7690 /*
7691 * First check if it's not a curly-braces thing: {expr}.
7692 * Must do this without evaluating, otherwise a function may be called
7693 * twice. Unfortunately this means we need to call eval1() twice for the
7694 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007695 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007697 if (*start != '}')
7698 {
7699 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7700 return FAIL;
7701 if (*start == '}')
7702 return NOTDONE;
7703 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007704
7705 if (evaluate)
7706 {
7707 d = dict_alloc();
7708 if (d == NULL)
7709 return FAIL;
7710 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007711 tvkey.v_type = VAR_UNKNOWN;
7712 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007713
7714 *arg = skipwhite(*arg + 1);
7715 while (**arg != '}' && **arg != NUL)
7716 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007717 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007718 goto failret;
7719 if (**arg != ':')
7720 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007721 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007722 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 goto failret;
7724 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007725 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007727 key = get_tv_string_buf_chk(&tvkey, buf);
7728 if (key == NULL || *key == NUL)
7729 {
7730 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7731 if (key != NULL)
7732 EMSG(_(e_emptykey));
7733 clear_tv(&tvkey);
7734 goto failret;
7735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007736 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737
7738 *arg = skipwhite(*arg + 1);
7739 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7740 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007741 if (evaluate)
7742 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007743 goto failret;
7744 }
7745 if (evaluate)
7746 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007747 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007748 if (item != NULL)
7749 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007750 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007751 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007752 clear_tv(&tv);
7753 goto failret;
7754 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007755 item = dictitem_alloc(key);
7756 clear_tv(&tvkey);
7757 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007759 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007760 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007761 if (dict_add(d, item) == FAIL)
7762 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 }
7764 }
7765
7766 if (**arg == '}')
7767 break;
7768 if (**arg != ',')
7769 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007770 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 goto failret;
7772 }
7773 *arg = skipwhite(*arg + 1);
7774 }
7775
7776 if (**arg != '}')
7777 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007778 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779failret:
7780 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007781 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 return FAIL;
7783 }
7784
7785 *arg = skipwhite(*arg + 1);
7786 if (evaluate)
7787 {
7788 rettv->v_type = VAR_DICT;
7789 rettv->vval.v_dict = d;
7790 ++d->dv_refcount;
7791 }
7792
7793 return OK;
7794}
7795
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007796#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007797#endif
7798
Bram Moolenaar17a13432016-01-24 14:22:10 +01007799 static char *
7800get_var_special_name(int nr)
7801{
7802 switch (nr)
7803 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007804 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007805 case VVAL_TRUE: return "v:true";
7806 case VVAL_NONE: return "v:none";
7807 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007808 }
7809 EMSG2(_(e_intern2), "get_var_special_name()");
7810 return "42";
7811}
7812
Bram Moolenaar8c711452005-01-14 21:53:12 +00007813/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007814 * Return a string with the string representation of a variable.
7815 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007816 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007817 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007818 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007819 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 */
7821 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007822echo_string(
7823 typval_T *tv,
7824 char_u **tofree,
7825 char_u *numbuf,
7826 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007827{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007828 static int recurse = 0;
7829 char_u *r = NULL;
7830
Bram Moolenaar33570922005-01-25 22:26:29 +00007831 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007832 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007833 if (!did_echo_string_emsg)
7834 {
7835 /* Only give this message once for a recursive call to avoid
7836 * flooding the user with errors. And stop iterating over lists
7837 * and dicts. */
7838 did_echo_string_emsg = TRUE;
7839 EMSG(_("E724: variable nested too deep for displaying"));
7840 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007841 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007842 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007843 }
7844 ++recurse;
7845
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846 switch (tv->v_type)
7847 {
7848 case VAR_FUNC:
7849 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 r = tv->vval.v_string;
7851 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007852
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007853 case VAR_PARTIAL:
7854 *tofree = NULL;
7855 /* TODO: arguments */
7856 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7857 break;
7858
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007859 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007860 if (tv->vval.v_list == NULL)
7861 {
7862 *tofree = NULL;
7863 r = NULL;
7864 }
7865 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7866 {
7867 *tofree = NULL;
7868 r = (char_u *)"[...]";
7869 }
7870 else
7871 {
7872 tv->vval.v_list->lv_copyID = copyID;
7873 *tofree = list2string(tv, copyID);
7874 r = *tofree;
7875 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007877
Bram Moolenaar8c711452005-01-14 21:53:12 +00007878 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879 if (tv->vval.v_dict == NULL)
7880 {
7881 *tofree = NULL;
7882 r = NULL;
7883 }
7884 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7885 {
7886 *tofree = NULL;
7887 r = (char_u *)"{...}";
7888 }
7889 else
7890 {
7891 tv->vval.v_dict->dv_copyID = copyID;
7892 *tofree = dict2string(tv, copyID);
7893 r = *tofree;
7894 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007895 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007896
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007897 case VAR_STRING:
7898 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007899 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007900 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007901 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007902 *tofree = NULL;
7903 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007904 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007905
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007906 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007907#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007908 *tofree = NULL;
7909 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7910 r = numbuf;
7911 break;
7912#endif
7913
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007914 case VAR_SPECIAL:
7915 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007916 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007917 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007918 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007919
Bram Moolenaar8502c702014-06-17 12:51:16 +02007920 if (--recurse == 0)
7921 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007922 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007923}
7924
7925/*
7926 * Return a string with the string representation of a variable.
7927 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7928 * "numbuf" is used for a number.
7929 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007930 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007931 */
7932 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007933tv2string(
7934 typval_T *tv,
7935 char_u **tofree,
7936 char_u *numbuf,
7937 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007938{
7939 switch (tv->v_type)
7940 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007941 case VAR_FUNC:
7942 *tofree = string_quote(tv->vval.v_string, TRUE);
7943 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007944 case VAR_PARTIAL:
Bram Moolenaar5c291542016-03-19 20:05:45 +01007945 {
7946 partial_T *pt = tv->vval.v_partial;
7947 char_u *fname = string_quote(pt == NULL ? NULL
7948 : pt->pt_name, FALSE);
7949 garray_T ga;
7950 int i;
7951 char_u *tf;
7952
7953 ga_init2(&ga, 1, 100);
7954 ga_concat(&ga, (char_u *)"function(");
7955 if (fname != NULL)
7956 {
7957 ga_concat(&ga, fname);
7958 vim_free(fname);
7959 }
7960 if (pt != NULL && pt->pt_argc > 0)
7961 {
7962 ga_concat(&ga, (char_u *)", [");
7963 for (i = 0; i < pt->pt_argc; ++i)
7964 {
7965 if (i > 0)
7966 ga_concat(&ga, (char_u *)", ");
7967 ga_concat(&ga,
7968 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7969 vim_free(tf);
7970 }
7971 ga_concat(&ga, (char_u *)"]");
7972 }
7973 if (pt != NULL && pt->pt_dict != NULL)
7974 {
7975 typval_T dtv;
7976
7977 ga_concat(&ga, (char_u *)", ");
7978 dtv.v_type = VAR_DICT;
7979 dtv.vval.v_dict = pt->pt_dict;
7980 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7981 vim_free(tf);
7982 }
7983 ga_concat(&ga, (char_u *)")");
7984
7985 *tofree = ga.ga_data;
7986 return *tofree;
7987 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007988 case VAR_STRING:
7989 *tofree = string_quote(tv->vval.v_string, FALSE);
7990 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007991 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007992#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007993 *tofree = NULL;
7994 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7995 return numbuf;
7996#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007997 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007999 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008000 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008001 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008002 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008003 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008004 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008005 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008006 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008007}
8008
8009/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008010 * Return string "str" in ' quotes, doubling ' characters.
8011 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008012 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 */
8014 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008015string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008016{
Bram Moolenaar33570922005-01-25 22:26:29 +00008017 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008018 char_u *p, *r, *s;
8019
Bram Moolenaar33570922005-01-25 22:26:29 +00008020 len = (function ? 13 : 3);
8021 if (str != NULL)
8022 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008023 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008024 for (p = str; *p != NUL; mb_ptr_adv(p))
8025 if (*p == '\'')
8026 ++len;
8027 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008028 s = r = alloc(len);
8029 if (r != NULL)
8030 {
8031 if (function)
8032 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008033 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 r += 10;
8035 }
8036 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008037 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 if (str != NULL)
8039 for (p = str; *p != NUL; )
8040 {
8041 if (*p == '\'')
8042 *r++ = '\'';
8043 MB_COPY_CHAR(p, r);
8044 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008045 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008046 if (function)
8047 *r++ = ')';
8048 *r++ = NUL;
8049 }
8050 return s;
8051}
8052
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008053#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008054/*
8055 * Convert the string "text" to a floating point number.
8056 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8057 * this always uses a decimal point.
8058 * Returns the length of the text that was consumed.
8059 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008060 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008061string2float(
8062 char_u *text,
8063 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008064{
8065 char *s = (char *)text;
8066 float_T f;
8067
8068 f = strtod(s, &s);
8069 *value = f;
8070 return (int)((char_u *)s - text);
8071}
8072#endif
8073
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008074/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008075 * Get the value of an environment variable.
8076 * "arg" is pointing to the '$'. It is advanced to after the name.
8077 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008078 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008079 */
8080 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008081get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082{
8083 char_u *string = NULL;
8084 int len;
8085 int cc;
8086 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008087 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008088
8089 ++*arg;
8090 name = *arg;
8091 len = get_env_len(arg);
8092 if (evaluate)
8093 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008094 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008095 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008096
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008097 cc = name[len];
8098 name[len] = NUL;
8099 /* first try vim_getenv(), fast for normal environment vars */
8100 string = vim_getenv(name, &mustfree);
8101 if (string != NULL && *string != NUL)
8102 {
8103 if (!mustfree)
8104 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008105 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008106 else
8107 {
8108 if (mustfree)
8109 vim_free(string);
8110
8111 /* next try expanding things like $VIM and ${HOME} */
8112 string = expand_env_save(name - 1);
8113 if (string != NULL && *string == '$')
8114 {
8115 vim_free(string);
8116 string = NULL;
8117 }
8118 }
8119 name[len] = cc;
8120
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008121 rettv->v_type = VAR_STRING;
8122 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
8124
8125 return OK;
8126}
8127
8128/*
8129 * Array with names and number of arguments of all internal functions
8130 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8131 */
8132static struct fst
8133{
8134 char *f_name; /* function name */
8135 char f_min_argc; /* minimal number of arguments */
8136 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008137 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008138 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139} functions[] =
8140{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008141#ifdef FEAT_FLOAT
8142 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008143 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008144#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008145 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008146 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008147 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008148 {"append", 2, 2, f_append},
8149 {"argc", 0, 0, f_argc},
8150 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008151 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008152 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008153#ifdef FEAT_FLOAT
8154 {"asin", 1, 1, f_asin}, /* WJMc */
8155#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008156 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008157 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008158 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008159 {"assert_false", 1, 2, f_assert_false},
8160 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008161#ifdef FEAT_FLOAT
8162 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008163 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008165 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008166 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008167 {"bufexists", 1, 1, f_bufexists},
8168 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8169 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8170 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8171 {"buflisted", 1, 1, f_buflisted},
8172 {"bufloaded", 1, 1, f_bufloaded},
8173 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008174 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008175 {"bufwinnr", 1, 1, f_bufwinnr},
8176 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008177 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008178 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008179 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008180#ifdef FEAT_FLOAT
8181 {"ceil", 1, 1, f_ceil},
8182#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008183#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008184 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008185 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8186 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008187 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008188 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008189 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008190 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008191 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008192 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008193 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008194 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008195 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8196 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008197 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008198 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008199#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008200 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008201 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008202 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008203 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008204 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008205#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008206 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008207 {"complete_add", 1, 1, f_complete_add},
8208 {"complete_check", 0, 0, f_complete_check},
8209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008210 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008211 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008212#ifdef FEAT_FLOAT
8213 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008214 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008215#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008216 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008217 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008218 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008219 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008220 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008222 {"diff_filler", 1, 1, f_diff_filler},
8223 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008224 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008225 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008226 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008227 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008228 {"eventhandler", 0, 0, f_eventhandler},
8229 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008230 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008231 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008232#ifdef FEAT_FLOAT
8233 {"exp", 1, 1, f_exp},
8234#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008235 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008236 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008237 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8239 {"filereadable", 1, 1, f_filereadable},
8240 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008242 {"finddir", 1, 3, f_finddir},
8243 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008244#ifdef FEAT_FLOAT
8245 {"float2nr", 1, 1, f_float2nr},
8246 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008247 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008248#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008249 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"fnamemodify", 2, 2, f_fnamemodify},
8251 {"foldclosed", 1, 1, f_foldclosed},
8252 {"foldclosedend", 1, 1, f_foldclosedend},
8253 {"foldlevel", 1, 1, f_foldlevel},
8254 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008255 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008257 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008258 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008259 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008260 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008261 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008262 {"getchar", 0, 1, f_getchar},
8263 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008264 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008265 {"getcmdline", 0, 0, f_getcmdline},
8266 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008267 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008268 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008269 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008270 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008271 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008272 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"getfsize", 1, 1, f_getfsize},
8274 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008275 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008276 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008277 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008278 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008279 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008280 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008281 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008282 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008283 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008284 {"gettabvar", 2, 3, f_gettabvar},
8285 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008286 {"getwinposx", 0, 0, f_getwinposx},
8287 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008288 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008289 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008290 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008291 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008293 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008294 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008295 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008296 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8297 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8298 {"histadd", 2, 2, f_histadd},
8299 {"histdel", 1, 2, f_histdel},
8300 {"histget", 1, 2, f_histget},
8301 {"histnr", 1, 1, f_histnr},
8302 {"hlID", 1, 1, f_hlID},
8303 {"hlexists", 1, 1, f_hlexists},
8304 {"hostname", 0, 0, f_hostname},
8305 {"iconv", 3, 3, f_iconv},
8306 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008307 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008308 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008310 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"inputrestore", 0, 0, f_inputrestore},
8312 {"inputsave", 0, 0, f_inputsave},
8313 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008314 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008315 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008316 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008317 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008318#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8319 {"isnan", 1, 1, f_isnan},
8320#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008321 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008322#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008323 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008324 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008325 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008326 {"job_start", 1, 2, f_job_start},
8327 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008328 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008329#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008330 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008331 {"js_decode", 1, 1, f_js_decode},
8332 {"js_encode", 1, 1, f_js_encode},
8333 {"json_decode", 1, 1, f_json_decode},
8334 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008335 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008337 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"libcall", 3, 3, f_libcall},
8339 {"libcallnr", 3, 3, f_libcallnr},
8340 {"line", 1, 1, f_line},
8341 {"line2byte", 1, 1, f_line2byte},
8342 {"lispindent", 1, 1, f_lispindent},
8343 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008344#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008345 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008346 {"log10", 1, 1, f_log10},
8347#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008348#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008349 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008350#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008351 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008352 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008353 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008354 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008355 {"matchadd", 2, 5, f_matchadd},
8356 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008357 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008358 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008359 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008360 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008361 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008362 {"max", 1, 1, f_max},
8363 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008364#ifdef vim_mkdir
8365 {"mkdir", 1, 3, f_mkdir},
8366#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008367 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008368#ifdef FEAT_MZSCHEME
8369 {"mzeval", 1, 1, f_mzeval},
8370#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008371 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008372 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008373 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008374 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008375#ifdef FEAT_PERL
8376 {"perleval", 1, 1, f_perleval},
8377#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008378#ifdef FEAT_FLOAT
8379 {"pow", 2, 2, f_pow},
8380#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008381 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008382 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008383 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008384#ifdef FEAT_PYTHON3
8385 {"py3eval", 1, 1, f_py3eval},
8386#endif
8387#ifdef FEAT_PYTHON
8388 {"pyeval", 1, 1, f_pyeval},
8389#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008390 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008391 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008392 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008393#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008394 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008395#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008396 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"remote_expr", 2, 3, f_remote_expr},
8398 {"remote_foreground", 1, 1, f_remote_foreground},
8399 {"remote_peek", 1, 2, f_remote_peek},
8400 {"remote_read", 1, 1, f_remote_read},
8401 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008402 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008403 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008404 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008405 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008406 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008407#ifdef FEAT_FLOAT
8408 {"round", 1, 1, f_round},
8409#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008410 {"screenattr", 2, 2, f_screenattr},
8411 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008412 {"screencol", 0, 0, f_screencol},
8413 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008414 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008415 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008416 {"searchpair", 3, 7, f_searchpair},
8417 {"searchpairpos", 3, 7, f_searchpairpos},
8418 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008419 {"server2client", 2, 2, f_server2client},
8420 {"serverlist", 0, 0, f_serverlist},
8421 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008422 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008424 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008425 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008426 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008427 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008428 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008429 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008430 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008431 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008432 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008433 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008434#ifdef FEAT_CRYPT
8435 {"sha256", 1, 1, f_sha256},
8436#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008437 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008438 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008439 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008440#ifdef FEAT_FLOAT
8441 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008442 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008443#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008444 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008445 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008446 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008447 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008448 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008449#ifdef FEAT_FLOAT
8450 {"sqrt", 1, 1, f_sqrt},
8451 {"str2float", 1, 1, f_str2float},
8452#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008453 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008454 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008455 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456#ifdef HAVE_STRFTIME
8457 {"strftime", 1, 2, f_strftime},
8458#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008459 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008460 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008461 {"strlen", 1, 1, f_strlen},
8462 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008463 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008465 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008466 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008467 {"substitute", 4, 4, f_substitute},
8468 {"synID", 3, 3, f_synID},
8469 {"synIDattr", 2, 3, f_synIDattr},
8470 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008471 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008472 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008473 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008474 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008475 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008476 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008477 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008478 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008479 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008480#ifdef FEAT_FLOAT
8481 {"tan", 1, 1, f_tan},
8482 {"tanh", 1, 1, f_tanh},
8483#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008485 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008486#ifdef FEAT_TIMERS
8487 {"timer_start", 2, 3, f_timer_start},
8488 {"timer_stop", 1, 1, f_timer_stop},
8489#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"tolower", 1, 1, f_tolower},
8491 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008492 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008493#ifdef FEAT_FLOAT
8494 {"trunc", 1, 1, f_trunc},
8495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008496 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008497 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008498 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008499 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008500 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"virtcol", 1, 1, f_virtcol},
8502 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008503 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008504 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008505 {"win_getid", 0, 2, f_win_getid},
8506 {"win_gotoid", 1, 1, f_win_gotoid},
8507 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8508 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"winbufnr", 1, 1, f_winbufnr},
8510 {"wincol", 0, 0, f_wincol},
8511 {"winheight", 1, 1, f_winheight},
8512 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008513 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008514 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008515 {"winrestview", 1, 1, f_winrestview},
8516 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008517 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008518 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008519 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008520 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008521};
8522
8523#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8524
8525/*
8526 * Function given to ExpandGeneric() to obtain the list of internal
8527 * or user defined function names.
8528 */
8529 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531{
8532 static int intidx = -1;
8533 char_u *name;
8534
8535 if (idx == 0)
8536 intidx = -1;
8537 if (intidx < 0)
8538 {
8539 name = get_user_func_name(xp, idx);
8540 if (name != NULL)
8541 return name;
8542 }
8543 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8544 {
8545 STRCPY(IObuff, functions[intidx].f_name);
8546 STRCAT(IObuff, "(");
8547 if (functions[intidx].f_max_argc == 0)
8548 STRCAT(IObuff, ")");
8549 return IObuff;
8550 }
8551
8552 return NULL;
8553}
8554
8555/*
8556 * Function given to ExpandGeneric() to obtain the list of internal or
8557 * user defined variable or function names.
8558 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008559 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008560get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008561{
8562 static int intidx = -1;
8563 char_u *name;
8564
8565 if (idx == 0)
8566 intidx = -1;
8567 if (intidx < 0)
8568 {
8569 name = get_function_name(xp, idx);
8570 if (name != NULL)
8571 return name;
8572 }
8573 return get_user_var_name(xp, ++intidx);
8574}
8575
8576#endif /* FEAT_CMDL_COMPL */
8577
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008578#if defined(EBCDIC) || defined(PROTO)
8579/*
8580 * Compare struct fst by function name.
8581 */
8582 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008583compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008584{
8585 struct fst *p1 = (struct fst *)s1;
8586 struct fst *p2 = (struct fst *)s2;
8587
8588 return STRCMP(p1->f_name, p2->f_name);
8589}
8590
8591/*
8592 * Sort the function table by function name.
8593 * The sorting of the table above is ASCII dependant.
8594 * On machines using EBCDIC we have to sort it.
8595 */
8596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008597sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008598{
8599 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8600
8601 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8602}
8603#endif
8604
8605
Bram Moolenaar071d4272004-06-13 20:20:40 +00008606/*
8607 * Find internal function in table above.
8608 * Return index, or -1 if not found
8609 */
8610 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008611find_internal_func(
8612 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008613{
8614 int first = 0;
8615 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8616 int cmp;
8617 int x;
8618
8619 /*
8620 * Find the function name in the table. Binary search.
8621 */
8622 while (first <= last)
8623 {
8624 x = first + ((unsigned)(last - first) >> 1);
8625 cmp = STRCMP(name, functions[x].f_name);
8626 if (cmp < 0)
8627 last = x - 1;
8628 else if (cmp > 0)
8629 first = x + 1;
8630 else
8631 return x;
8632 }
8633 return -1;
8634}
8635
8636/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008637 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8638 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008639 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8640 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008641 */
8642 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008643deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008644{
Bram Moolenaar33570922005-01-25 22:26:29 +00008645 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008646 int cc;
8647
Bram Moolenaar65639032016-03-16 21:40:30 +01008648 if (partialp != NULL)
8649 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008651 cc = name[*lenp];
8652 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008653 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008655 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008657 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 {
8659 *lenp = 0;
8660 return (char_u *)""; /* just in case */
8661 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008662 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008663 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008664 }
8665
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008666 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8667 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008668 partial_T *pt = v->di_tv.vval.v_partial;
8669
8670 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008671 {
8672 *lenp = 0;
8673 return (char_u *)""; /* just in case */
8674 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008675 if (partialp != NULL)
8676 *partialp = pt;
8677 *lenp = (int)STRLEN(pt->pt_name);
8678 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008679 }
8680
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008681 return name;
8682}
8683
8684/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008685 * Allocate a variable for the result of a function.
8686 * Return OK or FAIL.
8687 */
8688 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008689get_func_tv(
8690 char_u *name, /* name of the function */
8691 int len, /* length of "name" */
8692 typval_T *rettv,
8693 char_u **arg, /* argument, pointing to the '(' */
8694 linenr_T firstline, /* first line of range */
8695 linenr_T lastline, /* last line of range */
8696 int *doesrange, /* return: function handled range */
8697 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008698 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008699 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008700{
8701 char_u *argp;
8702 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008703 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 int argcount = 0; /* number of arguments found */
8705
8706 /*
8707 * Get the arguments.
8708 */
8709 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008710 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 {
8712 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8713 if (*argp == ')' || *argp == ',' || *argp == NUL)
8714 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008715 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8716 {
8717 ret = FAIL;
8718 break;
8719 }
8720 ++argcount;
8721 if (*argp != ',')
8722 break;
8723 }
8724 if (*argp == ')')
8725 ++argp;
8726 else
8727 ret = FAIL;
8728
8729 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008730 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008731 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008733 {
8734 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008735 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008736 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008737 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008739
8740 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008741 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008742
8743 *arg = skipwhite(argp);
8744 return ret;
8745}
8746
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008747#define ERROR_UNKNOWN 0
8748#define ERROR_TOOMANY 1
8749#define ERROR_TOOFEW 2
8750#define ERROR_SCRIPT 3
8751#define ERROR_DICT 4
8752#define ERROR_NONE 5
8753#define ERROR_OTHER 6
8754#define FLEN_FIXED 40
8755
8756/*
8757 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8758 * Change <SNR>123_name() to K_SNR 123_name().
8759 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8760 * (slow).
8761 */
8762 static char_u *
8763fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8764{
8765 int llen;
8766 char_u *fname;
8767 int i;
8768
8769 llen = eval_fname_script(name);
8770 if (llen > 0)
8771 {
8772 fname_buf[0] = K_SPECIAL;
8773 fname_buf[1] = KS_EXTRA;
8774 fname_buf[2] = (int)KE_SNR;
8775 i = 3;
8776 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8777 {
8778 if (current_SID <= 0)
8779 *error = ERROR_SCRIPT;
8780 else
8781 {
8782 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8783 i = (int)STRLEN(fname_buf);
8784 }
8785 }
8786 if (i + STRLEN(name + llen) < FLEN_FIXED)
8787 {
8788 STRCPY(fname_buf + i, name + llen);
8789 fname = fname_buf;
8790 }
8791 else
8792 {
8793 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8794 if (fname == NULL)
8795 *error = ERROR_OTHER;
8796 else
8797 {
8798 *tofree = fname;
8799 mch_memmove(fname, fname_buf, (size_t)i);
8800 STRCPY(fname + i, name + llen);
8801 }
8802 }
8803 }
8804 else
8805 fname = name;
8806 return fname;
8807}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008808
8809/*
8810 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008811 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008812 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008813 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008814 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008815call_func(
8816 char_u *funcname, /* name of the function */
8817 int len, /* length of "name" */
8818 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008819 int argcount_in, /* number of "argvars" */
8820 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008821 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008822 linenr_T firstline, /* first line of range */
8823 linenr_T lastline, /* last line of range */
8824 int *doesrange, /* return: function handled range */
8825 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008826 partial_T *partial, /* optional, can be NULL */
8827 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828{
8829 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 int error = ERROR_NONE;
8831 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008833 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008834 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008835 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008836 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008837 int argcount = argcount_in;
8838 typval_T *argvars = argvars_in;
8839 dict_T *selfdict = selfdict_in;
8840 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8841 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008842
8843 /* Make a copy of the name, if it comes from a funcref variable it could
8844 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008845 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008846 if (name == NULL)
8847 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008848
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008849 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850
8851 *doesrange = FALSE;
8852
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008853 if (partial != NULL)
8854 {
8855 if (partial->pt_dict != NULL)
8856 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008857 /* When the function has a partial with a dict and there is a dict
8858 * argument, use the dict argument. That is backwards compatible.
8859 */
8860 if (selfdict_in == NULL)
8861 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008862 }
8863 if (error == ERROR_NONE && partial->pt_argc > 0)
8864 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008865 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8866 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8867 for (i = 0; i < argcount_in; ++i)
8868 argv[i + argv_clear] = argvars_in[i];
8869 argvars = argv;
8870 argcount = partial->pt_argc + argcount_in;
8871 }
8872 }
8873
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
8875 /* execute the function if no errors detected and executing */
8876 if (evaluate && error == ERROR_NONE)
8877 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008878 char_u *rfname = fname;
8879
8880 /* Ignore "g:" before a function name. */
8881 if (fname[0] == 'g' && fname[1] == ':')
8882 rfname = fname + 2;
8883
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008884 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8885 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008886 error = ERROR_UNKNOWN;
8887
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008888 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008889 {
8890 /*
8891 * User defined function.
8892 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008893 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008894
Bram Moolenaar071d4272004-06-13 20:20:40 +00008895#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008896 /* Trigger FuncUndefined event, may load the function. */
8897 if (fp == NULL
8898 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008899 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008900 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008901 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008902 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008903 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008904 }
8905#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008906 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008907 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008908 {
8909 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008910 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008911 }
8912
Bram Moolenaar071d4272004-06-13 20:20:40 +00008913 if (fp != NULL)
8914 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008915 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008916 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008917 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008919 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008921 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008922 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 else
8924 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008925 int did_save_redo = FALSE;
8926
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 /*
8928 * Call the user function.
8929 * Save and restore search patterns, script variables and
8930 * redo buffer.
8931 */
8932 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008933#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008934 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008935#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008936 {
8937 saveRedobuff();
8938 did_save_redo = TRUE;
8939 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008940 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008941 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008942 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008943 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8944 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8945 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008946 /* Function was unreferenced while being used, free it
8947 * now. */
8948 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008949 if (did_save_redo)
8950 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008951 restore_search_patterns();
8952 error = ERROR_NONE;
8953 }
8954 }
8955 }
8956 else
8957 {
8958 /*
8959 * Find the function name in the table, call its implementation.
8960 */
8961 i = find_internal_func(fname);
8962 if (i >= 0)
8963 {
8964 if (argcount < functions[i].f_min_argc)
8965 error = ERROR_TOOFEW;
8966 else if (argcount > functions[i].f_max_argc)
8967 error = ERROR_TOOMANY;
8968 else
8969 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008970 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008971 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008972 error = ERROR_NONE;
8973 }
8974 }
8975 }
8976 /*
8977 * The function call (or "FuncUndefined" autocommand sequence) might
8978 * have been aborted by an error, an interrupt, or an explicitly thrown
8979 * exception that has not been caught so far. This situation can be
8980 * tested for by calling aborting(). For an error in an internal
8981 * function or for the "E132" error in call_user_func(), however, the
8982 * throw point at which the "force_abort" flag (temporarily reset by
8983 * emsg()) is normally updated has not been reached yet. We need to
8984 * update that flag first to make aborting() reliable.
8985 */
8986 update_force_abort();
8987 }
8988 if (error == ERROR_NONE)
8989 ret = OK;
8990
8991 /*
8992 * Report an error unless the argument evaluation or function call has been
8993 * cancelled due to an aborting error, an interrupt, or an exception.
8994 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008995 if (!aborting())
8996 {
8997 switch (error)
8998 {
8999 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009000 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009001 break;
9002 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009003 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009004 break;
9005 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009006 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009007 name);
9008 break;
9009 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009010 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009011 name);
9012 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009013 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009014 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009015 name);
9016 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009017 }
9018 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009019
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009020 while (argv_clear > 0)
9021 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009022 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009023 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009024
9025 return ret;
9026}
9027
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009028/*
9029 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009030 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009031 */
9032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009033emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009034{
9035 char_u *p;
9036
9037 if (*name == K_SPECIAL)
9038 p = concat_str((char_u *)"<SNR>", name + 3);
9039 else
9040 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009041 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009042 if (p != name)
9043 vim_free(p);
9044}
9045
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009046/*
9047 * Return TRUE for a non-zero Number and a non-empty String.
9048 */
9049 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009051{
9052 return ((argvars[0].v_type == VAR_NUMBER
9053 && argvars[0].vval.v_number != 0)
9054 || (argvars[0].v_type == VAR_STRING
9055 && argvars[0].vval.v_string != NULL
9056 && *argvars[0].vval.v_string != NUL));
9057}
9058
Bram Moolenaar071d4272004-06-13 20:20:40 +00009059/*********************************************
9060 * Implementation of the built-in functions
9061 */
9062
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009063#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009064static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009065
9066/*
9067 * Get the float value of "argvars[0]" into "f".
9068 * Returns FAIL when the argument is not a Number or Float.
9069 */
9070 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009071get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009072{
9073 if (argvars[0].v_type == VAR_FLOAT)
9074 {
9075 *f = argvars[0].vval.v_float;
9076 return OK;
9077 }
9078 if (argvars[0].v_type == VAR_NUMBER)
9079 {
9080 *f = (float_T)argvars[0].vval.v_number;
9081 return OK;
9082 }
9083 EMSG(_("E808: Number or Float required"));
9084 return FAIL;
9085}
9086
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009087/*
9088 * "abs(expr)" function
9089 */
9090 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009091f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009092{
9093 if (argvars[0].v_type == VAR_FLOAT)
9094 {
9095 rettv->v_type = VAR_FLOAT;
9096 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9097 }
9098 else
9099 {
9100 varnumber_T n;
9101 int error = FALSE;
9102
9103 n = get_tv_number_chk(&argvars[0], &error);
9104 if (error)
9105 rettv->vval.v_number = -1;
9106 else if (n > 0)
9107 rettv->vval.v_number = n;
9108 else
9109 rettv->vval.v_number = -n;
9110 }
9111}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009112
9113/*
9114 * "acos()" function
9115 */
9116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009117f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009118{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009119 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009120
9121 rettv->v_type = VAR_FLOAT;
9122 if (get_float_arg(argvars, &f) == OK)
9123 rettv->vval.v_float = acos(f);
9124 else
9125 rettv->vval.v_float = 0.0;
9126}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009127#endif
9128
Bram Moolenaar071d4272004-06-13 20:20:40 +00009129/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009130 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009131 */
9132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009133f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009134{
Bram Moolenaar33570922005-01-25 22:26:29 +00009135 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009137 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009138 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009139 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009140 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009141 && !tv_check_lock(l->lv_lock,
9142 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009143 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009144 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009145 }
9146 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 EMSG(_(e_listreq));
9148}
9149
9150/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009151 * "alloc_fail(id, countdown, repeat)" function
9152 */
9153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009154f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009155{
9156 if (argvars[0].v_type != VAR_NUMBER
9157 || argvars[0].vval.v_number <= 0
9158 || argvars[1].v_type != VAR_NUMBER
9159 || argvars[1].vval.v_number < 0
9160 || argvars[2].v_type != VAR_NUMBER)
9161 EMSG(_(e_invarg));
9162 else
9163 {
9164 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009165 if (alloc_fail_id >= aid_last)
9166 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009167 alloc_fail_countdown = argvars[1].vval.v_number;
9168 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009169 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009170 }
9171}
9172
9173/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009174 * "and(expr, expr)" function
9175 */
9176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009177f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009178{
9179 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9180 & get_tv_number_chk(&argvars[1], NULL);
9181}
9182
9183/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009184 * "append(lnum, string/list)" function
9185 */
9186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009187f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009188{
9189 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009190 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009191 list_T *l = NULL;
9192 listitem_T *li = NULL;
9193 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009194 long added = 0;
9195
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009196 /* When coming here from Insert mode, sync undo, so that this can be
9197 * undone separately from what was previously inserted. */
9198 if (u_sync_once == 2)
9199 {
9200 u_sync_once = 1; /* notify that u_sync() was called */
9201 u_sync(TRUE);
9202 }
9203
Bram Moolenaar0d660222005-01-07 21:51:51 +00009204 lnum = get_tv_lnum(argvars);
9205 if (lnum >= 0
9206 && lnum <= curbuf->b_ml.ml_line_count
9207 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009208 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009209 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009210 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009211 l = argvars[1].vval.v_list;
9212 if (l == NULL)
9213 return;
9214 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009215 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009216 for (;;)
9217 {
9218 if (l == NULL)
9219 tv = &argvars[1]; /* append a string */
9220 else if (li == NULL)
9221 break; /* end of list */
9222 else
9223 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009224 line = get_tv_string_chk(tv);
9225 if (line == NULL) /* type error */
9226 {
9227 rettv->vval.v_number = 1; /* Failed */
9228 break;
9229 }
9230 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009231 ++added;
9232 if (l == NULL)
9233 break;
9234 li = li->li_next;
9235 }
9236
9237 appended_lines_mark(lnum, added);
9238 if (curwin->w_cursor.lnum > lnum)
9239 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009240 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 else
9242 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009243}
9244
9245/*
9246 * "argc()" function
9247 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009248 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009249f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009250{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009251 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009252}
9253
9254/*
9255 * "argidx()" function
9256 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009258f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009260 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009261}
9262
9263/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009264 * "arglistid()" function
9265 */
9266 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009267f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009268{
9269 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009270
9271 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009272 wp = find_tabwin(&argvars[0], &argvars[1]);
9273 if (wp != NULL)
9274 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009275}
9276
9277/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278 * "argv(nr)" function
9279 */
9280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009281f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009282{
9283 int idx;
9284
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009285 if (argvars[0].v_type != VAR_UNKNOWN)
9286 {
9287 idx = get_tv_number_chk(&argvars[0], NULL);
9288 if (idx >= 0 && idx < ARGCOUNT)
9289 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9290 else
9291 rettv->vval.v_string = NULL;
9292 rettv->v_type = VAR_STRING;
9293 }
9294 else if (rettv_list_alloc(rettv) == OK)
9295 for (idx = 0; idx < ARGCOUNT; ++idx)
9296 list_append_string(rettv->vval.v_list,
9297 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009298}
9299
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009300static void prepare_assert_error(garray_T*gap);
9301static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9302static void assert_error(garray_T *gap);
9303static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009304
9305/*
9306 * Prepare "gap" for an assert error and add the sourcing position.
9307 */
9308 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009309prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310{
9311 char buf[NUMBUFLEN];
9312
9313 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009314 if (sourcing_name != NULL)
9315 {
9316 ga_concat(gap, sourcing_name);
9317 if (sourcing_lnum > 0)
9318 ga_concat(gap, (char_u *)" ");
9319 }
9320 if (sourcing_lnum > 0)
9321 {
9322 sprintf(buf, "line %ld", (long)sourcing_lnum);
9323 ga_concat(gap, (char_u *)buf);
9324 }
9325 if (sourcing_name != NULL || sourcing_lnum > 0)
9326 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327}
9328
9329/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 * Append "str" to "gap", escaping unprintable characters.
9331 * Changes NL to \n, CR to \r, etc.
9332 */
9333 static void
9334ga_concat_esc(garray_T *gap, char_u *str)
9335{
9336 char_u *p;
9337 char_u buf[NUMBUFLEN];
9338
Bram Moolenaarf1551962016-03-15 12:55:58 +01009339 if (str == NULL)
9340 {
9341 ga_concat(gap, (char_u *)"NULL");
9342 return;
9343 }
9344
Bram Moolenaar23689172016-02-15 22:37:37 +01009345 for (p = str; *p != NUL; ++p)
9346 switch (*p)
9347 {
9348 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9349 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9350 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9351 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9352 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9353 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9354 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9355 default:
9356 if (*p < ' ')
9357 {
9358 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9359 ga_concat(gap, buf);
9360 }
9361 else
9362 ga_append(gap, *p);
9363 break;
9364 }
9365}
9366
9367/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009368 * Fill "gap" with information about an assert error.
9369 */
9370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009371fill_assert_error(
9372 garray_T *gap,
9373 typval_T *opt_msg_tv,
9374 char_u *exp_str,
9375 typval_T *exp_tv,
9376 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009377{
9378 char_u numbuf[NUMBUFLEN];
9379 char_u *tofree;
9380
9381 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9382 {
9383 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9384 vim_free(tofree);
9385 }
9386 else
9387 {
9388 ga_concat(gap, (char_u *)"Expected ");
9389 if (exp_str == NULL)
9390 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009391 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009392 vim_free(tofree);
9393 }
9394 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009395 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009396 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009397 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009398 vim_free(tofree);
9399 }
9400}
Bram Moolenaar43345542015-11-29 17:35:35 +01009401
9402/*
9403 * Add an assert error to v:errors.
9404 */
9405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009406assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009407{
9408 struct vimvar *vp = &vimvars[VV_ERRORS];
9409
9410 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9411 /* Make sure v:errors is a list. */
9412 set_vim_var_list(VV_ERRORS, list_alloc());
9413 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9414}
9415
Bram Moolenaar43345542015-11-29 17:35:35 +01009416/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009417 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009418 */
9419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009420f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009421{
9422 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009423
9424 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9425 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009426 prepare_assert_error(&ga);
9427 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9428 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009429 ga_clear(&ga);
9430 }
9431}
9432
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009433/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009434 * "assert_exception(string[, msg])" function
9435 */
9436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009437f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009438{
9439 garray_T ga;
9440 char *error;
9441
9442 error = (char *)get_tv_string_chk(&argvars[0]);
9443 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9444 {
9445 prepare_assert_error(&ga);
9446 ga_concat(&ga, (char_u *)"v:exception is not set");
9447 assert_error(&ga);
9448 ga_clear(&ga);
9449 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009450 else if (error != NULL
9451 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009452 {
9453 prepare_assert_error(&ga);
9454 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9455 &vimvars[VV_EXCEPTION].vv_tv);
9456 assert_error(&ga);
9457 ga_clear(&ga);
9458 }
9459}
9460
9461/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009462 * "assert_fails(cmd [, error])" function
9463 */
9464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009465f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009466{
9467 char_u *cmd = get_tv_string_chk(&argvars[0]);
9468 garray_T ga;
9469
9470 called_emsg = FALSE;
9471 suppress_errthrow = TRUE;
9472 emsg_silent = TRUE;
9473 do_cmdline_cmd(cmd);
9474 if (!called_emsg)
9475 {
9476 prepare_assert_error(&ga);
9477 ga_concat(&ga, (char_u *)"command did not fail: ");
9478 ga_concat(&ga, cmd);
9479 assert_error(&ga);
9480 ga_clear(&ga);
9481 }
9482 else if (argvars[1].v_type != VAR_UNKNOWN)
9483 {
9484 char_u buf[NUMBUFLEN];
9485 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9486
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009487 if (error == NULL
9488 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009489 {
9490 prepare_assert_error(&ga);
9491 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9492 &vimvars[VV_ERRMSG].vv_tv);
9493 assert_error(&ga);
9494 ga_clear(&ga);
9495 }
9496 }
9497
9498 called_emsg = FALSE;
9499 suppress_errthrow = FALSE;
9500 emsg_silent = FALSE;
9501 emsg_on_display = FALSE;
9502 set_vim_var_string(VV_ERRMSG, NULL, 0);
9503}
9504
9505/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009506 * Common for assert_true() and assert_false().
9507 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009508 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009509assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009510{
9511 int error = FALSE;
9512 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009513
Bram Moolenaar37127922016-02-06 20:29:28 +01009514 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009515 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009516 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009517 if (argvars[0].v_type != VAR_NUMBER
9518 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9519 || error)
9520 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009521 prepare_assert_error(&ga);
9522 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009523 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009524 NULL, &argvars[0]);
9525 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009526 ga_clear(&ga);
9527 }
9528}
9529
9530/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009531 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009532 */
9533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009535{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009536 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009537}
9538
9539/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009540 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009541 */
9542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009543f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009544{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009545 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009546}
9547
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009548#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009549/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009550 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009551 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009553f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009554{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009555 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009556
9557 rettv->v_type = VAR_FLOAT;
9558 if (get_float_arg(argvars, &f) == OK)
9559 rettv->vval.v_float = asin(f);
9560 else
9561 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009562}
9563
9564/*
9565 * "atan()" function
9566 */
9567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009568f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009569{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009570 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009571
9572 rettv->v_type = VAR_FLOAT;
9573 if (get_float_arg(argvars, &f) == OK)
9574 rettv->vval.v_float = atan(f);
9575 else
9576 rettv->vval.v_float = 0.0;
9577}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009578
9579/*
9580 * "atan2()" function
9581 */
9582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009583f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009584{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009585 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009586
9587 rettv->v_type = VAR_FLOAT;
9588 if (get_float_arg(argvars, &fx) == OK
9589 && get_float_arg(&argvars[1], &fy) == OK)
9590 rettv->vval.v_float = atan2(fx, fy);
9591 else
9592 rettv->vval.v_float = 0.0;
9593}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009594#endif
9595
Bram Moolenaar071d4272004-06-13 20:20:40 +00009596/*
9597 * "browse(save, title, initdir, default)" function
9598 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009601{
9602#ifdef FEAT_BROWSE
9603 int save;
9604 char_u *title;
9605 char_u *initdir;
9606 char_u *defname;
9607 char_u buf[NUMBUFLEN];
9608 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009609 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009610
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009611 save = get_tv_number_chk(&argvars[0], &error);
9612 title = get_tv_string_chk(&argvars[1]);
9613 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9614 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009616 if (error || title == NULL || initdir == NULL || defname == NULL)
9617 rettv->vval.v_string = NULL;
9618 else
9619 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009620 do_browse(save ? BROWSE_SAVE : 0,
9621 title, defname, NULL, initdir, NULL, curbuf);
9622#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009623 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009624#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009626}
9627
9628/*
9629 * "browsedir(title, initdir)" function
9630 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009633{
9634#ifdef FEAT_BROWSE
9635 char_u *title;
9636 char_u *initdir;
9637 char_u buf[NUMBUFLEN];
9638
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009639 title = get_tv_string_chk(&argvars[0]);
9640 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009641
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009642 if (title == NULL || initdir == NULL)
9643 rettv->vval.v_string = NULL;
9644 else
9645 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009646 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009647#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009648 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009649#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009650 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009651}
9652
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009653static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009654
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655/*
9656 * Find a buffer by number or exact name.
9657 */
9658 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009659find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009660{
9661 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009663 if (avar->v_type == VAR_NUMBER)
9664 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009665 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009667 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009668 if (buf == NULL)
9669 {
9670 /* No full path name match, try a match with a URL or a "nofile"
9671 * buffer, these don't use the full path. */
9672 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9673 if (buf->b_fname != NULL
9674 && (path_with_url(buf->b_fname)
9675#ifdef FEAT_QUICKFIX
9676 || bt_nofile(buf)
9677#endif
9678 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009679 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009680 break;
9681 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009682 }
9683 return buf;
9684}
9685
9686/*
9687 * "bufexists(expr)" function
9688 */
9689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009690f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009691{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009692 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009693}
9694
9695/*
9696 * "buflisted(expr)" function
9697 */
9698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009699f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700{
9701 buf_T *buf;
9702
9703 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009704 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705}
9706
9707/*
9708 * "bufloaded(expr)" function
9709 */
9710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009711f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712{
9713 buf_T *buf;
9714
9715 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009716 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009717}
9718
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009719 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009720buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009721{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009722 int save_magic;
9723 char_u *save_cpo;
9724 buf_T *buf;
9725
Bram Moolenaar071d4272004-06-13 20:20:40 +00009726 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9727 save_magic = p_magic;
9728 p_magic = TRUE;
9729 save_cpo = p_cpo;
9730 p_cpo = (char_u *)"";
9731
9732 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009733 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009734
9735 p_magic = save_magic;
9736 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009737 return buf;
9738}
9739
9740/*
9741 * Get buffer by number or pattern.
9742 */
9743 static buf_T *
9744get_buf_tv(typval_T *tv, int curtab_only)
9745{
9746 char_u *name = tv->vval.v_string;
9747 buf_T *buf;
9748
9749 if (tv->v_type == VAR_NUMBER)
9750 return buflist_findnr((int)tv->vval.v_number);
9751 if (tv->v_type != VAR_STRING)
9752 return NULL;
9753 if (name == NULL || *name == NUL)
9754 return curbuf;
9755 if (name[0] == '$' && name[1] == NUL)
9756 return lastbuf;
9757
9758 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759
9760 /* If not found, try expanding the name, like done for bufexists(). */
9761 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763
9764 return buf;
9765}
9766
9767/*
9768 * "bufname(expr)" function
9769 */
9770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009771f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009772{
9773 buf_T *buf;
9774
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009775 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009776 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009777 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009778 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009780 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009782 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783 --emsg_off;
9784}
9785
9786/*
9787 * "bufnr(expr)" function
9788 */
9789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009790f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791{
9792 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009793 int error = FALSE;
9794 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009796 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009797 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009798 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009799 --emsg_off;
9800
9801 /* If the buffer isn't found and the second argument is not zero create a
9802 * new buffer. */
9803 if (buf == NULL
9804 && argvars[1].v_type != VAR_UNKNOWN
9805 && get_tv_number_chk(&argvars[1], &error) != 0
9806 && !error
9807 && (name = get_tv_string_chk(&argvars[0])) != NULL
9808 && !error)
9809 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9810
Bram Moolenaar071d4272004-06-13 20:20:40 +00009811 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009812 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009813 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009814 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009815}
9816
9817/*
9818 * "bufwinnr(nr)" function
9819 */
9820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009821f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009822{
9823#ifdef FEAT_WINDOWS
9824 win_T *wp;
9825 int winnr = 0;
9826#endif
9827 buf_T *buf;
9828
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009829 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009831 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832#ifdef FEAT_WINDOWS
9833 for (wp = firstwin; wp; wp = wp->w_next)
9834 {
9835 ++winnr;
9836 if (wp->w_buffer == buf)
9837 break;
9838 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009839 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009841 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842#endif
9843 --emsg_off;
9844}
9845
9846/*
9847 * "byte2line(byte)" function
9848 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009850f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009851{
9852#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009853 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009854#else
9855 long boff = 0;
9856
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009857 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 (linenr_T)0, &boff);
9863#endif
9864}
9865
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009866 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009867byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009868{
9869#ifdef FEAT_MBYTE
9870 char_u *t;
9871#endif
9872 char_u *str;
9873 long idx;
9874
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009875 str = get_tv_string_chk(&argvars[0]);
9876 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009877 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009879 return;
9880
9881#ifdef FEAT_MBYTE
9882 t = str;
9883 for ( ; idx > 0; idx--)
9884 {
9885 if (*t == NUL) /* EOL reached */
9886 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009887 if (enc_utf8 && comp)
9888 t += utf_ptr2len(t);
9889 else
9890 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009891 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009892 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009893#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009894 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009895 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009896#endif
9897}
9898
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009899/*
9900 * "byteidx()" function
9901 */
9902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009903f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009904{
9905 byteidx(argvars, rettv, FALSE);
9906}
9907
9908/*
9909 * "byteidxcomp()" function
9910 */
9911 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009912f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009913{
9914 byteidx(argvars, rettv, TRUE);
9915}
9916
Bram Moolenaardb913952012-06-29 12:54:53 +02009917 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009918func_call(
9919 char_u *name,
9920 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009921 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009922 dict_T *selfdict,
9923 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009924{
9925 listitem_T *item;
9926 typval_T argv[MAX_FUNC_ARGS + 1];
9927 int argc = 0;
9928 int dummy;
9929 int r = 0;
9930
9931 for (item = args->vval.v_list->lv_first; item != NULL;
9932 item = item->li_next)
9933 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009934 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009935 {
9936 EMSG(_("E699: Too many arguments"));
9937 break;
9938 }
9939 /* Make a copy of each argument. This is needed to be able to set
9940 * v_lock to VAR_FIXED in the copy without changing the original list.
9941 */
9942 copy_tv(&item->li_tv, &argv[argc++]);
9943 }
9944
9945 if (item == NULL)
9946 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9947 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009948 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009949
9950 /* Free the arguments. */
9951 while (argc > 0)
9952 clear_tv(&argv[--argc]);
9953
9954 return r;
9955}
9956
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009957/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009958 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009959 */
9960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009962{
9963 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009964 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009965 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009966
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009967 if (argvars[1].v_type != VAR_LIST)
9968 {
9969 EMSG(_(e_listreq));
9970 return;
9971 }
9972 if (argvars[1].vval.v_list == NULL)
9973 return;
9974
9975 if (argvars[0].v_type == VAR_FUNC)
9976 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009977 else if (argvars[0].v_type == VAR_PARTIAL)
9978 {
9979 partial = argvars[0].vval.v_partial;
9980 func = partial->pt_name;
9981 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009982 else
9983 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009984 if (*func == NUL)
9985 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009986
Bram Moolenaare9a41262005-01-15 22:18:47 +00009987 if (argvars[2].v_type != VAR_UNKNOWN)
9988 {
9989 if (argvars[2].v_type != VAR_DICT)
9990 {
9991 EMSG(_(e_dictreq));
9992 return;
9993 }
9994 selfdict = argvars[2].vval.v_dict;
9995 }
9996
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009997 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009998}
9999
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010000#ifdef FEAT_FLOAT
10001/*
10002 * "ceil({float})" function
10003 */
10004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010005f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010006{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010007 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010008
10009 rettv->v_type = VAR_FLOAT;
10010 if (get_float_arg(argvars, &f) == OK)
10011 rettv->vval.v_float = ceil(f);
10012 else
10013 rettv->vval.v_float = 0.0;
10014}
10015#endif
10016
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010017#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010018/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010019 * "ch_close()" function
10020 */
10021 static void
10022f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10023{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010024 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010025
10026 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010027 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010028 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010029 channel_clear(channel);
10030 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010031}
10032
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010033/*
10034 * "ch_getbufnr()" function
10035 */
10036 static void
10037f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10038{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010039 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010040
10041 rettv->vval.v_number = -1;
10042 if (channel != NULL)
10043 {
10044 char_u *what = get_tv_string(&argvars[1]);
10045 int part;
10046
10047 if (STRCMP(what, "err") == 0)
10048 part = PART_ERR;
10049 else if (STRCMP(what, "out") == 0)
10050 part = PART_OUT;
10051 else if (STRCMP(what, "in") == 0)
10052 part = PART_IN;
10053 else
10054 part = PART_SOCK;
10055 if (channel->ch_part[part].ch_buffer != NULL)
10056 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10057 }
10058}
10059
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010060/*
10061 * "ch_getjob()" function
10062 */
10063 static void
10064f_ch_getjob(typval_T *argvars, typval_T *rettv)
10065{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010066 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010067
10068 if (channel != NULL)
10069 {
10070 rettv->v_type = VAR_JOB;
10071 rettv->vval.v_job = channel->ch_job;
10072 if (channel->ch_job != NULL)
10073 ++channel->ch_job->jv_refcount;
10074 }
10075}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010076
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010077/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010078 * "ch_info()" function
10079 */
10080 static void
10081f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10082{
10083 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10084
10085 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10086 channel_info(channel, rettv->vval.v_dict);
10087}
10088
10089/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010090 * "ch_log()" function
10091 */
10092 static void
10093f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10094{
10095 char_u *msg = get_tv_string(&argvars[0]);
10096 channel_T *channel = NULL;
10097
10098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010099 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010100
10101 ch_log(channel, (char *)msg);
10102}
10103
10104/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010105 * "ch_logfile()" function
10106 */
10107 static void
10108f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10109{
10110 char_u *fname;
10111 char_u *opt = (char_u *)"";
10112 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010113
10114 fname = get_tv_string(&argvars[0]);
10115 if (argvars[1].v_type == VAR_STRING)
10116 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010117 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010118}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010119
10120/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010121 * "ch_open()" function
10122 */
10123 static void
10124f_ch_open(typval_T *argvars, typval_T *rettv)
10125{
Bram Moolenaar77073442016-02-13 23:23:53 +010010126 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010127 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010128}
10129
10130/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010131 * "ch_read()" function
10132 */
10133 static void
10134f_ch_read(typval_T *argvars, typval_T *rettv)
10135{
10136 common_channel_read(argvars, rettv, FALSE);
10137}
10138
10139/*
10140 * "ch_readraw()" function
10141 */
10142 static void
10143f_ch_readraw(typval_T *argvars, typval_T *rettv)
10144{
10145 common_channel_read(argvars, rettv, TRUE);
10146}
10147
10148/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010149 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010150 */
10151 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010152f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10153{
10154 ch_expr_common(argvars, rettv, TRUE);
10155}
10156
10157/*
10158 * "ch_sendexpr()" function
10159 */
10160 static void
10161f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10162{
10163 ch_expr_common(argvars, rettv, FALSE);
10164}
10165
10166/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010167 * "ch_evalraw()" function
10168 */
10169 static void
10170f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10171{
10172 ch_raw_common(argvars, rettv, TRUE);
10173}
10174
10175/*
10176 * "ch_sendraw()" function
10177 */
10178 static void
10179f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10180{
10181 ch_raw_common(argvars, rettv, FALSE);
10182}
10183
10184/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010185 * "ch_setoptions()" function
10186 */
10187 static void
10188f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10189{
10190 channel_T *channel;
10191 jobopt_T opt;
10192
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010193 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010194 if (channel == NULL)
10195 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010196 clear_job_options(&opt);
10197 if (get_job_options(&argvars[1], &opt,
10198 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010199 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010200 channel_set_options(channel, &opt);
10201}
10202
10203/*
10204 * "ch_status()" function
10205 */
10206 static void
10207f_ch_status(typval_T *argvars, typval_T *rettv)
10208{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010209 channel_T *channel;
10210
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010211 /* return an empty string by default */
10212 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010213 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010214
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010215 channel = get_channel_arg(&argvars[0], FALSE);
10216 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010217}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010218#endif
10219
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010220/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010221 * "changenr()" function
10222 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010224f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010225{
10226 rettv->vval.v_number = curbuf->b_u_seq_cur;
10227}
10228
10229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010230 * "char2nr(string)" function
10231 */
10232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010233f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010234{
10235#ifdef FEAT_MBYTE
10236 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010237 {
10238 int utf8 = 0;
10239
10240 if (argvars[1].v_type != VAR_UNKNOWN)
10241 utf8 = get_tv_number_chk(&argvars[1], NULL);
10242
10243 if (utf8)
10244 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10245 else
10246 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10247 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010248 else
10249#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010250 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010251}
10252
10253/*
10254 * "cindent(lnum)" function
10255 */
10256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010257f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010258{
10259#ifdef FEAT_CINDENT
10260 pos_T pos;
10261 linenr_T lnum;
10262
10263 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010264 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010265 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10266 {
10267 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010268 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010269 curwin->w_cursor = pos;
10270 }
10271 else
10272#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010273 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010274}
10275
10276/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010277 * "clearmatches()" function
10278 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010280f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010281{
10282#ifdef FEAT_SEARCH_EXTRA
10283 clear_matches(curwin);
10284#endif
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "col(string)" function
10289 */
10290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293 colnr_T col = 0;
10294 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010295 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010296
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010297 fp = var2fpos(&argvars[0], FALSE, &fnum);
10298 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010299 {
10300 if (fp->col == MAXCOL)
10301 {
10302 /* '> can be MAXCOL, get the length of the line then */
10303 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010304 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305 else
10306 col = MAXCOL;
10307 }
10308 else
10309 {
10310 col = fp->col + 1;
10311#ifdef FEAT_VIRTUALEDIT
10312 /* col(".") when the cursor is on the NUL at the end of the line
10313 * because of "coladd" can be seen as an extra column. */
10314 if (virtual_active() && fp == &curwin->w_cursor)
10315 {
10316 char_u *p = ml_get_cursor();
10317
10318 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10319 curwin->w_virtcol - curwin->w_cursor.coladd))
10320 {
10321# ifdef FEAT_MBYTE
10322 int l;
10323
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010324 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010325 col += l;
10326# else
10327 if (*p != NUL && p[1] == NUL)
10328 ++col;
10329# endif
10330 }
10331 }
10332#endif
10333 }
10334 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010335 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336}
10337
Bram Moolenaar572cb562005-08-05 21:35:02 +000010338#if defined(FEAT_INS_EXPAND)
10339/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010340 * "complete()" function
10341 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010343f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010344{
10345 int startcol;
10346
10347 if ((State & INSERT) == 0)
10348 {
10349 EMSG(_("E785: complete() can only be used in Insert mode"));
10350 return;
10351 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010352
10353 /* Check for undo allowed here, because if something was already inserted
10354 * the line was already saved for undo and this check isn't done. */
10355 if (!undo_allowed())
10356 return;
10357
Bram Moolenaarade00832006-03-10 21:46:58 +000010358 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10359 {
10360 EMSG(_(e_invarg));
10361 return;
10362 }
10363
10364 startcol = get_tv_number_chk(&argvars[0], NULL);
10365 if (startcol <= 0)
10366 return;
10367
10368 set_completion(startcol - 1, argvars[1].vval.v_list);
10369}
10370
10371/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010372 * "complete_add()" function
10373 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010375f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010376{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010377 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010378}
10379
10380/*
10381 * "complete_check()" function
10382 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010384f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010385{
10386 int saved = RedrawingDisabled;
10387
10388 RedrawingDisabled = 0;
10389 ins_compl_check_keys(0);
10390 rettv->vval.v_number = compl_interrupted;
10391 RedrawingDisabled = saved;
10392}
10393#endif
10394
Bram Moolenaar071d4272004-06-13 20:20:40 +000010395/*
10396 * "confirm(message, buttons[, default [, type]])" function
10397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010399f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010400{
10401#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10402 char_u *message;
10403 char_u *buttons = NULL;
10404 char_u buf[NUMBUFLEN];
10405 char_u buf2[NUMBUFLEN];
10406 int def = 1;
10407 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010408 char_u *typestr;
10409 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010410
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010411 message = get_tv_string_chk(&argvars[0]);
10412 if (message == NULL)
10413 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010414 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010415 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010416 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10417 if (buttons == NULL)
10418 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010419 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010420 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010421 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010422 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010423 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010424 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10425 if (typestr == NULL)
10426 error = TRUE;
10427 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010428 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010429 switch (TOUPPER_ASC(*typestr))
10430 {
10431 case 'E': type = VIM_ERROR; break;
10432 case 'Q': type = VIM_QUESTION; break;
10433 case 'I': type = VIM_INFO; break;
10434 case 'W': type = VIM_WARNING; break;
10435 case 'G': type = VIM_GENERIC; break;
10436 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010437 }
10438 }
10439 }
10440 }
10441
10442 if (buttons == NULL || *buttons == NUL)
10443 buttons = (char_u *)_("&Ok");
10444
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010445 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010447 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010448#endif
10449}
10450
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010451/*
10452 * "copy()" function
10453 */
10454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010455f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010456{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010457 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010458}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010460#ifdef FEAT_FLOAT
10461/*
10462 * "cos()" function
10463 */
10464 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010465f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010466{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010467 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010468
10469 rettv->v_type = VAR_FLOAT;
10470 if (get_float_arg(argvars, &f) == OK)
10471 rettv->vval.v_float = cos(f);
10472 else
10473 rettv->vval.v_float = 0.0;
10474}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010475
10476/*
10477 * "cosh()" function
10478 */
10479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010480f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010481{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010482 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010483
10484 rettv->v_type = VAR_FLOAT;
10485 if (get_float_arg(argvars, &f) == OK)
10486 rettv->vval.v_float = cosh(f);
10487 else
10488 rettv->vval.v_float = 0.0;
10489}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010490#endif
10491
Bram Moolenaar071d4272004-06-13 20:20:40 +000010492/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010493 * "count()" function
10494 */
10495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010496f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010497{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010498 long n = 0;
10499 int ic = FALSE;
10500
Bram Moolenaare9a41262005-01-15 22:18:47 +000010501 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010502 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010503 listitem_T *li;
10504 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010505 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010506
Bram Moolenaare9a41262005-01-15 22:18:47 +000010507 if ((l = argvars[0].vval.v_list) != NULL)
10508 {
10509 li = l->lv_first;
10510 if (argvars[2].v_type != VAR_UNKNOWN)
10511 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010512 int error = FALSE;
10513
10514 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010515 if (argvars[3].v_type != VAR_UNKNOWN)
10516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010517 idx = get_tv_number_chk(&argvars[3], &error);
10518 if (!error)
10519 {
10520 li = list_find(l, idx);
10521 if (li == NULL)
10522 EMSGN(_(e_listidx), idx);
10523 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010524 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010525 if (error)
10526 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010527 }
10528
10529 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010530 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010531 ++n;
10532 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010533 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010534 else if (argvars[0].v_type == VAR_DICT)
10535 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010536 int todo;
10537 dict_T *d;
10538 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010539
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010540 if ((d = argvars[0].vval.v_dict) != NULL)
10541 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010542 int error = FALSE;
10543
Bram Moolenaare9a41262005-01-15 22:18:47 +000010544 if (argvars[2].v_type != VAR_UNKNOWN)
10545 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010546 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010547 if (argvars[3].v_type != VAR_UNKNOWN)
10548 EMSG(_(e_invarg));
10549 }
10550
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010551 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010553 {
10554 if (!HASHITEM_EMPTY(hi))
10555 {
10556 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010557 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010558 ++n;
10559 }
10560 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010561 }
10562 }
10563 else
10564 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010565 rettv->vval.v_number = n;
10566}
10567
10568/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10570 *
10571 * Checks the existence of a cscope connection.
10572 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010574f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575{
10576#ifdef FEAT_CSCOPE
10577 int num = 0;
10578 char_u *dbpath = NULL;
10579 char_u *prepend = NULL;
10580 char_u buf[NUMBUFLEN];
10581
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010582 if (argvars[0].v_type != VAR_UNKNOWN
10583 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010584 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010585 num = (int)get_tv_number(&argvars[0]);
10586 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010587 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010588 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010589 }
10590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010591 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010592#endif
10593}
10594
10595/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010596 * "cursor(lnum, col)" function, or
10597 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010598 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010599 * Moves the cursor to the specified line and column.
10600 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010601 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010603f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010604{
10605 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010606#ifdef FEAT_VIRTUALEDIT
10607 long coladd = 0;
10608#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010609 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010610
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010611 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010612 if (argvars[1].v_type == VAR_UNKNOWN)
10613 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010614 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010615 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010616
Bram Moolenaar493c1782014-05-28 14:34:46 +020010617 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010618 {
10619 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010620 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010621 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010622 line = pos.lnum;
10623 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010624#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010625 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010626#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010627 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010628 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010629 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010630 set_curswant = FALSE;
10631 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010632 }
10633 else
10634 {
10635 line = get_tv_lnum(argvars);
10636 col = get_tv_number_chk(&argvars[1], NULL);
10637#ifdef FEAT_VIRTUALEDIT
10638 if (argvars[2].v_type != VAR_UNKNOWN)
10639 coladd = get_tv_number_chk(&argvars[2], NULL);
10640#endif
10641 }
10642 if (line < 0 || col < 0
10643#ifdef FEAT_VIRTUALEDIT
10644 || coladd < 0
10645#endif
10646 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010647 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 if (line > 0)
10649 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 if (col > 0)
10651 curwin->w_cursor.col = col - 1;
10652#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010653 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010654#endif
10655
10656 /* Make sure the cursor is in a valid position. */
10657 check_cursor();
10658#ifdef FEAT_MBYTE
10659 /* Correct cursor for multi-byte character. */
10660 if (has_mbyte)
10661 mb_adjust_cursor();
10662#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010663
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010664 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010665 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010666}
10667
10668/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010669 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010670 */
10671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010672f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010673{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010674 int noref = 0;
10675
10676 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010677 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010678 if (noref < 0 || noref > 1)
10679 EMSG(_(e_invarg));
10680 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010681 {
10682 current_copyID += COPYID_INC;
10683 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10684 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010685}
10686
10687/*
10688 * "delete()" function
10689 */
10690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010691f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010692{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010693 char_u nbuf[NUMBUFLEN];
10694 char_u *name;
10695 char_u *flags;
10696
10697 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010698 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010699 return;
10700
10701 name = get_tv_string(&argvars[0]);
10702 if (name == NULL || *name == NUL)
10703 {
10704 EMSG(_(e_invarg));
10705 return;
10706 }
10707
10708 if (argvars[1].v_type != VAR_UNKNOWN)
10709 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010710 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010711 flags = (char_u *)"";
10712
10713 if (*flags == NUL)
10714 /* delete a file */
10715 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10716 else if (STRCMP(flags, "d") == 0)
10717 /* delete an empty directory */
10718 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10719 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010720 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010721 rettv->vval.v_number = delete_recursive(name);
10722 else
10723 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
10726/*
10727 * "did_filetype()" function
10728 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010730f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
10732#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010733 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734#endif
10735}
10736
10737/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010738 * "diff_filler()" function
10739 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010740 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010741f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010742{
10743#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010744 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010745#endif
10746}
10747
10748/*
10749 * "diff_hlID()" function
10750 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010751 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010752f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010753{
10754#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010755 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010756 static linenr_T prev_lnum = 0;
10757 static int changedtick = 0;
10758 static int fnum = 0;
10759 static int change_start = 0;
10760 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010761 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010762 int filler_lines;
10763 int col;
10764
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010765 if (lnum < 0) /* ignore type error in {lnum} arg */
10766 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010767 if (lnum != prev_lnum
10768 || changedtick != curbuf->b_changedtick
10769 || fnum != curbuf->b_fnum)
10770 {
10771 /* New line, buffer, change: need to get the values. */
10772 filler_lines = diff_check(curwin, lnum);
10773 if (filler_lines < 0)
10774 {
10775 if (filler_lines == -1)
10776 {
10777 change_start = MAXCOL;
10778 change_end = -1;
10779 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10780 hlID = HLF_ADD; /* added line */
10781 else
10782 hlID = HLF_CHD; /* changed line */
10783 }
10784 else
10785 hlID = HLF_ADD; /* added line */
10786 }
10787 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010788 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010789 prev_lnum = lnum;
10790 changedtick = curbuf->b_changedtick;
10791 fnum = curbuf->b_fnum;
10792 }
10793
10794 if (hlID == HLF_CHD || hlID == HLF_TXD)
10795 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010796 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010797 if (col >= change_start && col <= change_end)
10798 hlID = HLF_TXD; /* changed text */
10799 else
10800 hlID = HLF_CHD; /* changed line */
10801 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010802 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010803#endif
10804}
10805
10806/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010807 * "disable_char_avail_for_testing({expr})" function
10808 */
10809 static void
10810f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10811{
10812 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10813}
10814
10815/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010816 * "empty({expr})" function
10817 */
10818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010819f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010820{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010821 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010822
10823 switch (argvars[0].v_type)
10824 {
10825 case VAR_STRING:
10826 case VAR_FUNC:
10827 n = argvars[0].vval.v_string == NULL
10828 || *argvars[0].vval.v_string == NUL;
10829 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010830 case VAR_PARTIAL:
10831 n = FALSE;
10832 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010833 case VAR_NUMBER:
10834 n = argvars[0].vval.v_number == 0;
10835 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010836 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010837#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010838 n = argvars[0].vval.v_float == 0.0;
10839 break;
10840#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010841 case VAR_LIST:
10842 n = argvars[0].vval.v_list == NULL
10843 || argvars[0].vval.v_list->lv_first == NULL;
10844 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010845 case VAR_DICT:
10846 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010847 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010848 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010849 case VAR_SPECIAL:
10850 n = argvars[0].vval.v_number != VVAL_TRUE;
10851 break;
10852
Bram Moolenaar835dc632016-02-07 14:27:38 +010010853 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010854#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010855 n = argvars[0].vval.v_job == NULL
10856 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10857 break;
10858#endif
10859 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010860#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010861 n = argvars[0].vval.v_channel == NULL
10862 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010863 break;
10864#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010865 case VAR_UNKNOWN:
10866 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10867 n = TRUE;
10868 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010869 }
10870
10871 rettv->vval.v_number = n;
10872}
10873
10874/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010875 * "escape({string}, {chars})" function
10876 */
10877 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010878f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010879{
10880 char_u buf[NUMBUFLEN];
10881
Bram Moolenaar758711c2005-02-02 23:11:38 +000010882 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10883 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010884 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010885}
10886
10887/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010888 * "eval()" function
10889 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010891f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010892{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010893 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010894
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010895 s = get_tv_string_chk(&argvars[0]);
10896 if (s != NULL)
10897 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010898
Bram Moolenaar615b9972015-01-14 17:15:05 +010010899 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010900 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10901 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010902 if (p != NULL && !aborting())
10903 EMSG2(_(e_invexpr2), p);
10904 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010905 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010906 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010907 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010908 else if (*s != NUL)
10909 EMSG(_(e_trailing));
10910}
10911
10912/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010913 * "eventhandler()" function
10914 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010916f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010917{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010918 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010919}
10920
10921/*
10922 * "executable()" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010927 char_u *name = get_tv_string(&argvars[0]);
10928
10929 /* Check in $PATH and also check directly if there is a directory name. */
10930 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10931 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010932}
10933
10934/*
10935 * "exepath()" function
10936 */
10937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010938f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010939{
10940 char_u *p = NULL;
10941
Bram Moolenaarb5971142015-03-21 17:32:19 +010010942 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010943 rettv->v_type = VAR_STRING;
10944 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945}
10946
10947/*
10948 * "exists()" function
10949 */
10950 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010951f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010952{
10953 char_u *p;
10954 char_u *name;
10955 int n = FALSE;
10956 int len = 0;
10957
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010958 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010959 if (*p == '$') /* environment variable */
10960 {
10961 /* first try "normal" environment variables (fast) */
10962 if (mch_getenv(p + 1) != NULL)
10963 n = TRUE;
10964 else
10965 {
10966 /* try expanding things like $VIM and ${HOME} */
10967 p = expand_env_save(p);
10968 if (p != NULL && *p != '$')
10969 n = TRUE;
10970 vim_free(p);
10971 }
10972 }
10973 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010974 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010975 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010976 if (*skipwhite(p) != NUL)
10977 n = FALSE; /* trailing garbage */
10978 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010979 else if (*p == '*') /* internal or user defined function */
10980 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010981 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010982 }
10983 else if (*p == ':')
10984 {
10985 n = cmd_exists(p + 1);
10986 }
10987 else if (*p == '#')
10988 {
10989#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010990 if (p[1] == '#')
10991 n = autocmd_supported(p + 2);
10992 else
10993 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994#endif
10995 }
10996 else /* internal variable */
10997 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010998 char_u *tofree;
10999 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011000
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011001 /* get_name_len() takes care of expanding curly braces */
11002 name = p;
11003 len = get_name_len(&p, &tofree, TRUE, FALSE);
11004 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011005 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011006 if (tofree != NULL)
11007 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011008 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011009 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011011 /* handle d.key, l[idx], f(expr) */
11012 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11013 if (n)
11014 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011015 }
11016 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011017 if (*p != NUL)
11018 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011019
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011020 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011021 }
11022
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011023 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011024}
11025
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011026#ifdef FEAT_FLOAT
11027/*
11028 * "exp()" function
11029 */
11030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011031f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011032{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011033 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011034
11035 rettv->v_type = VAR_FLOAT;
11036 if (get_float_arg(argvars, &f) == OK)
11037 rettv->vval.v_float = exp(f);
11038 else
11039 rettv->vval.v_float = 0.0;
11040}
11041#endif
11042
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043/*
11044 * "expand()" function
11045 */
11046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011047f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011048{
11049 char_u *s;
11050 int len;
11051 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011052 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011053 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011054 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011055 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011056
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011057 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011058 if (argvars[1].v_type != VAR_UNKNOWN
11059 && argvars[2].v_type != VAR_UNKNOWN
11060 && get_tv_number_chk(&argvars[2], &error)
11061 && !error)
11062 {
11063 rettv->v_type = VAR_LIST;
11064 rettv->vval.v_list = NULL;
11065 }
11066
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011067 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 if (*s == '%' || *s == '#' || *s == '<')
11069 {
11070 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011071 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011072 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011073 if (rettv->v_type == VAR_LIST)
11074 {
11075 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11076 list_append_string(rettv->vval.v_list, result, -1);
11077 else
11078 vim_free(result);
11079 }
11080 else
11081 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082 }
11083 else
11084 {
11085 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011086 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011087 if (argvars[1].v_type != VAR_UNKNOWN
11088 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011089 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011090 if (!error)
11091 {
11092 ExpandInit(&xpc);
11093 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011094 if (p_wic)
11095 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011096 if (rettv->v_type == VAR_STRING)
11097 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11098 options, WILD_ALL);
11099 else if (rettv_list_alloc(rettv) != FAIL)
11100 {
11101 int i;
11102
11103 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11104 for (i = 0; i < xpc.xp_numfiles; i++)
11105 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11106 ExpandCleanup(&xpc);
11107 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011108 }
11109 else
11110 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 }
11112}
11113
11114/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011115 * Go over all entries in "d2" and add them to "d1".
11116 * When "action" is "error" then a duplicate key is an error.
11117 * When "action" is "force" then a duplicate key is overwritten.
11118 * Otherwise duplicate keys are ignored ("action" is "keep").
11119 */
11120 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011121dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011122{
11123 dictitem_T *di1;
11124 hashitem_T *hi2;
11125 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011126 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011127
11128 todo = (int)d2->dv_hashtab.ht_used;
11129 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11130 {
11131 if (!HASHITEM_EMPTY(hi2))
11132 {
11133 --todo;
11134 di1 = dict_find(d1, hi2->hi_key, -1);
11135 if (d1->dv_scope != 0)
11136 {
11137 /* Disallow replacing a builtin function in l: and g:.
11138 * Check the key to be valid when adding to any
11139 * scope. */
11140 if (d1->dv_scope == VAR_DEF_SCOPE
11141 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11142 && var_check_func_name(hi2->hi_key,
11143 di1 == NULL))
11144 break;
11145 if (!valid_varname(hi2->hi_key))
11146 break;
11147 }
11148 if (di1 == NULL)
11149 {
11150 di1 = dictitem_copy(HI2DI(hi2));
11151 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11152 dictitem_free(di1);
11153 }
11154 else if (*action == 'e')
11155 {
11156 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11157 break;
11158 }
11159 else if (*action == 'f' && HI2DI(hi2) != di1)
11160 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011161 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11162 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011163 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011164 clear_tv(&di1->di_tv);
11165 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11166 }
11167 }
11168 }
11169}
11170
11171/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011172 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011173 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011174 */
11175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011176f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011177{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011178 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011179
Bram Moolenaare9a41262005-01-15 22:18:47 +000011180 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011181 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011182 list_T *l1, *l2;
11183 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011184 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011185 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011186
Bram Moolenaare9a41262005-01-15 22:18:47 +000011187 l1 = argvars[0].vval.v_list;
11188 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011189 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011190 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011191 {
11192 if (argvars[2].v_type != VAR_UNKNOWN)
11193 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011194 before = get_tv_number_chk(&argvars[2], &error);
11195 if (error)
11196 return; /* type error; errmsg already given */
11197
Bram Moolenaar758711c2005-02-02 23:11:38 +000011198 if (before == l1->lv_len)
11199 item = NULL;
11200 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011201 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011202 item = list_find(l1, before);
11203 if (item == NULL)
11204 {
11205 EMSGN(_(e_listidx), before);
11206 return;
11207 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011208 }
11209 }
11210 else
11211 item = NULL;
11212 list_extend(l1, l2, item);
11213
Bram Moolenaare9a41262005-01-15 22:18:47 +000011214 copy_tv(&argvars[0], rettv);
11215 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011216 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011217 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11218 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011219 dict_T *d1, *d2;
11220 char_u *action;
11221 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222
11223 d1 = argvars[0].vval.v_dict;
11224 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011225 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011226 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 {
11228 /* Check the third argument. */
11229 if (argvars[2].v_type != VAR_UNKNOWN)
11230 {
11231 static char *(av[]) = {"keep", "force", "error"};
11232
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011233 action = get_tv_string_chk(&argvars[2]);
11234 if (action == NULL)
11235 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 for (i = 0; i < 3; ++i)
11237 if (STRCMP(action, av[i]) == 0)
11238 break;
11239 if (i == 3)
11240 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011241 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 return;
11243 }
11244 }
11245 else
11246 action = (char_u *)"force";
11247
Bram Moolenaara9922d62013-05-30 13:01:18 +020011248 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 copy_tv(&argvars[0], rettv);
11251 }
11252 }
11253 else
11254 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011255}
11256
11257/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011258 * "feedkeys()" function
11259 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011261f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011262{
11263 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011264 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011265 char_u *keys, *flags;
11266 char_u nbuf[NUMBUFLEN];
11267 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011268 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011269 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011270
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011271 /* This is not allowed in the sandbox. If the commands would still be
11272 * executed in the sandbox it would be OK, but it probably happens later,
11273 * when "sandbox" is no longer set. */
11274 if (check_secure())
11275 return;
11276
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011277 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011278
11279 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011280 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011281 flags = get_tv_string_buf(&argvars[1], nbuf);
11282 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011283 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011284 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011285 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011286 case 'n': remap = FALSE; break;
11287 case 'm': remap = TRUE; break;
11288 case 't': typed = TRUE; break;
11289 case 'i': insert = TRUE; break;
11290 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011291 }
11292 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011293 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011294
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011295 if (*keys != NUL || execute)
11296 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011297 /* Need to escape K_SPECIAL and CSI before putting the string in the
11298 * typeahead buffer. */
11299 keys_esc = vim_strsave_escape_csi(keys);
11300 if (keys_esc != NULL)
11301 {
11302 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011303 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011304 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011305 if (vgetc_busy)
11306 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011307 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011308 {
11309 int save_msg_scroll = msg_scroll;
11310
11311 /* Avoid a 1 second delay when the keys start Insert mode. */
11312 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011313 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011314 msg_scroll |= save_msg_scroll;
11315 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011316 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011317 }
11318}
11319
11320/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011321 * "filereadable()" function
11322 */
11323 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011324f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011325{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011326 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011327 char_u *p;
11328 int n;
11329
Bram Moolenaarc236c162008-07-13 17:41:49 +000011330#ifndef O_NONBLOCK
11331# define O_NONBLOCK 0
11332#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011333 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011334 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11335 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011336 {
11337 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011338 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011339 }
11340 else
11341 n = FALSE;
11342
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011343 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011344}
11345
11346/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011347 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011348 * rights to write into.
11349 */
11350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011351f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011352{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011353 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011354}
11355
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011356 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011357findfilendir(
11358 typval_T *argvars UNUSED,
11359 typval_T *rettv,
11360 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011361{
11362#ifdef FEAT_SEARCHPATH
11363 char_u *fname;
11364 char_u *fresult = NULL;
11365 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11366 char_u *p;
11367 char_u pathbuf[NUMBUFLEN];
11368 int count = 1;
11369 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011370 int error = FALSE;
11371#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011372
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011373 rettv->vval.v_string = NULL;
11374 rettv->v_type = VAR_STRING;
11375
11376#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011377 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011378
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011379 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011380 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011381 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11382 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011383 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011384 else
11385 {
11386 if (*p != NUL)
11387 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011390 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011391 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011392 }
11393
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011394 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11395 error = TRUE;
11396
11397 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011398 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011399 do
11400 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011401 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011402 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011403 fresult = find_file_in_path_option(first ? fname : NULL,
11404 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011405 0, first, path,
11406 find_what,
11407 curbuf->b_ffname,
11408 find_what == FINDFILE_DIR
11409 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011410 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011411
11412 if (fresult != NULL && rettv->v_type == VAR_LIST)
11413 list_append_string(rettv->vval.v_list, fresult, -1);
11414
11415 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011416 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011417
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011418 if (rettv->v_type == VAR_STRING)
11419 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011420#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011421}
11422
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011423static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11424static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011425
11426/*
11427 * Implementation of map() and filter().
11428 */
11429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011430filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011431{
11432 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011433 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011434 listitem_T *li, *nli;
11435 list_T *l = NULL;
11436 dictitem_T *di;
11437 hashtab_T *ht;
11438 hashitem_T *hi;
11439 dict_T *d = NULL;
11440 typval_T save_val;
11441 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011442 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011443 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011444 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011445 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011446 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011447 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011448 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011449
Bram Moolenaare9a41262005-01-15 22:18:47 +000011450 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011451 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011452 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011453 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011454 return;
11455 }
11456 else if (argvars[0].v_type == VAR_DICT)
11457 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011458 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011459 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011460 return;
11461 }
11462 else
11463 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011464 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011465 return;
11466 }
11467
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 expr = get_tv_string_buf_chk(&argvars[1], buf);
11469 /* On type errors, the preceding call has already displayed an error
11470 * message. Avoid a misleading error message for an empty string that
11471 * was not passed as argument. */
11472 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 prepare_vimvar(VV_VAL, &save_val);
11475 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011476
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011477 /* We reset "did_emsg" to be able to detect whether an error
11478 * occurred during evaluation of the expression. */
11479 save_did_emsg = did_emsg;
11480 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011481
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011482 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011483 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011484 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011485 vimvars[VV_KEY].vv_type = VAR_STRING;
11486
11487 ht = &d->dv_hashtab;
11488 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011489 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011490 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011491 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011492 if (!HASHITEM_EMPTY(hi))
11493 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011494 int r;
11495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 --todo;
11497 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011498 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011499 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11500 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011501 break;
11502 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011503 r = filter_map_one(&di->di_tv, expr, map, &rem);
11504 clear_tv(&vimvars[VV_KEY].vv_tv);
11505 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011506 break;
11507 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011508 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011509 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11510 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011511 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011512 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011513 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011514 }
11515 }
11516 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 }
11518 else
11519 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011520 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11521
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011522 for (li = l->lv_first; li != NULL; li = nli)
11523 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011524 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011525 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011527 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011528 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011529 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011530 break;
11531 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011533 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011534 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011535 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011536
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011537 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011538 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011539
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011540 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011541 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011542
11543 copy_tv(&argvars[0], rettv);
11544}
11545
11546 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011547filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011548{
Bram Moolenaar33570922005-01-25 22:26:29 +000011549 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011550 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011551 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011552
Bram Moolenaar33570922005-01-25 22:26:29 +000011553 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011554 s = expr;
11555 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011556 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011557 if (*s != NUL) /* check for trailing chars after expr */
11558 {
11559 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011560 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011561 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011562 }
11563 if (map)
11564 {
11565 /* map(): replace the list item value */
11566 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011567 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011568 *tv = rettv;
11569 }
11570 else
11571 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 int error = FALSE;
11573
Bram Moolenaare9a41262005-01-15 22:18:47 +000011574 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011576 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011577 /* On type error, nothing has been removed; return FAIL to stop the
11578 * loop. The error message was given by get_tv_number_chk(). */
11579 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011580 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011581 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011582 retval = OK;
11583theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011584 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011585 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011586}
11587
11588/*
11589 * "filter()" function
11590 */
11591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011592f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011593{
11594 filter_map(argvars, rettv, FALSE);
11595}
11596
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011597/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011598 * "finddir({fname}[, {path}[, {count}]])" function
11599 */
11600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011601f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011602{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011603 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011604}
11605
11606/*
11607 * "findfile({fname}[, {path}[, {count}]])" function
11608 */
11609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011610f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011611{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011612 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011613}
11614
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011615#ifdef FEAT_FLOAT
11616/*
11617 * "float2nr({float})" function
11618 */
11619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011620f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011621{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011622 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011623
11624 if (get_float_arg(argvars, &f) == OK)
11625 {
11626 if (f < -0x7fffffff)
11627 rettv->vval.v_number = -0x7fffffff;
11628 else if (f > 0x7fffffff)
11629 rettv->vval.v_number = 0x7fffffff;
11630 else
11631 rettv->vval.v_number = (varnumber_T)f;
11632 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011633}
11634
11635/*
11636 * "floor({float})" function
11637 */
11638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011639f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011640{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011641 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011642
11643 rettv->v_type = VAR_FLOAT;
11644 if (get_float_arg(argvars, &f) == OK)
11645 rettv->vval.v_float = floor(f);
11646 else
11647 rettv->vval.v_float = 0.0;
11648}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011649
11650/*
11651 * "fmod()" function
11652 */
11653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011654f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011655{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011656 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011657
11658 rettv->v_type = VAR_FLOAT;
11659 if (get_float_arg(argvars, &fx) == OK
11660 && get_float_arg(&argvars[1], &fy) == OK)
11661 rettv->vval.v_float = fmod(fx, fy);
11662 else
11663 rettv->vval.v_float = 0.0;
11664}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011665#endif
11666
Bram Moolenaar0d660222005-01-07 21:51:51 +000011667/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011668 * "fnameescape({string})" function
11669 */
11670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011671f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011672{
11673 rettv->vval.v_string = vim_strsave_fnameescape(
11674 get_tv_string(&argvars[0]), FALSE);
11675 rettv->v_type = VAR_STRING;
11676}
11677
11678/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011679 * "fnamemodify({fname}, {mods})" function
11680 */
11681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011682f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011683{
11684 char_u *fname;
11685 char_u *mods;
11686 int usedlen = 0;
11687 int len;
11688 char_u *fbuf = NULL;
11689 char_u buf[NUMBUFLEN];
11690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011691 fname = get_tv_string_chk(&argvars[0]);
11692 mods = get_tv_string_buf_chk(&argvars[1], buf);
11693 if (fname == NULL || mods == NULL)
11694 fname = NULL;
11695 else
11696 {
11697 len = (int)STRLEN(fname);
11698 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11699 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011700
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011701 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011702 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011703 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011704 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011705 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011706 vim_free(fbuf);
11707}
11708
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011709static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710
11711/*
11712 * "foldclosed()" function
11713 */
11714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011715foldclosed_both(
11716 typval_T *argvars UNUSED,
11717 typval_T *rettv,
11718 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719{
11720#ifdef FEAT_FOLDING
11721 linenr_T lnum;
11722 linenr_T first, last;
11723
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011724 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011725 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11726 {
11727 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11728 {
11729 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011730 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011731 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011732 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011733 return;
11734 }
11735 }
11736#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011737 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011738}
11739
11740/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011741 * "foldclosed()" function
11742 */
11743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011744f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011745{
11746 foldclosed_both(argvars, rettv, FALSE);
11747}
11748
11749/*
11750 * "foldclosedend()" function
11751 */
11752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011753f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011754{
11755 foldclosed_both(argvars, rettv, TRUE);
11756}
11757
11758/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759 * "foldlevel()" function
11760 */
11761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011762f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011763{
11764#ifdef FEAT_FOLDING
11765 linenr_T lnum;
11766
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011767 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011769 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011770#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011771}
11772
11773/*
11774 * "foldtext()" function
11775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011777f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778{
11779#ifdef FEAT_FOLDING
11780 linenr_T lnum;
11781 char_u *s;
11782 char_u *r;
11783 int len;
11784 char *txt;
11785#endif
11786
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->v_type = VAR_STRING;
11788 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011790 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11791 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11792 <= curbuf->b_ml.ml_line_count
11793 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011794 {
11795 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011796 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11797 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798 {
11799 if (!linewhite(lnum))
11800 break;
11801 ++lnum;
11802 }
11803
11804 /* Find interesting text in this line. */
11805 s = skipwhite(ml_get(lnum));
11806 /* skip C comment-start */
11807 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011809 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011810 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011811 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011812 {
11813 s = skipwhite(ml_get(lnum + 1));
11814 if (*s == '*')
11815 s = skipwhite(s + 1);
11816 }
11817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818 txt = _("+-%s%3ld lines: ");
11819 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011820 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821 + 20 /* for %3ld */
11822 + STRLEN(s))); /* concatenated */
11823 if (r != NULL)
11824 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011825 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11826 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11827 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828 len = (int)STRLEN(r);
11829 STRCAT(r, s);
11830 /* remove 'foldmarker' and 'commentstring' */
11831 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011832 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011833 }
11834 }
11835#endif
11836}
11837
11838/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011839 * "foldtextresult(lnum)" function
11840 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011841 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011842f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011843{
11844#ifdef FEAT_FOLDING
11845 linenr_T lnum;
11846 char_u *text;
11847 char_u buf[51];
11848 foldinfo_T foldinfo;
11849 int fold_count;
11850#endif
11851
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011852 rettv->v_type = VAR_STRING;
11853 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011854#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011855 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011856 /* treat illegal types and illegal string values for {lnum} the same */
11857 if (lnum < 0)
11858 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011859 fold_count = foldedCount(curwin, lnum, &foldinfo);
11860 if (fold_count > 0)
11861 {
11862 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11863 &foldinfo, buf);
11864 if (text == buf)
11865 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011866 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011867 }
11868#endif
11869}
11870
11871/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011872 * "foreground()" function
11873 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011875f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877#ifdef FEAT_GUI
11878 if (gui.in_use)
11879 gui_mch_set_foreground();
11880#else
11881# ifdef WIN32
11882 win32_set_foreground();
11883# endif
11884#endif
11885}
11886
11887/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011888 * "function()" function
11889 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011892{
11893 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011894 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011895 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011896 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011897
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011898 if (argvars[0].v_type == VAR_FUNC)
11899 {
11900 /* function(MyFunc, [arg], dict) */
11901 s = argvars[0].vval.v_string;
11902 }
11903 else if (argvars[0].v_type == VAR_PARTIAL
11904 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011905 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011906 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011907 arg_pt = argvars[0].vval.v_partial;
11908 s = arg_pt->pt_name;
11909 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011910 else
11911 {
11912 /* function('MyFunc', [arg], dict) */
11913 s = get_tv_string(&argvars[0]);
11914 use_string = TRUE;
11915 }
11916
11917 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011918 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011919 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011920 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11921 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011922 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011923 else
11924 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011925 int dict_idx = 0;
11926 int arg_idx = 0;
11927 list_T *list = NULL;
11928
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011929 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011930 {
11931 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011932 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011933
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011934 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11935 * also be called from another script. Using trans_function_name()
11936 * would also work, but some plugins depend on the name being
11937 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011938 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011939 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11940 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011941 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011942 STRCPY(name, sid_buf);
11943 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011944 }
11945 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011946 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011947 name = vim_strsave(s);
11948
11949 if (argvars[1].v_type != VAR_UNKNOWN)
11950 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011951 if (argvars[2].v_type != VAR_UNKNOWN)
11952 {
11953 /* function(name, [args], dict) */
11954 arg_idx = 1;
11955 dict_idx = 2;
11956 }
11957 else if (argvars[1].v_type == VAR_DICT)
11958 /* function(name, dict) */
11959 dict_idx = 1;
11960 else
11961 /* function(name, [args]) */
11962 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011963 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011964 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011965 if (argvars[dict_idx].v_type != VAR_DICT)
11966 {
11967 EMSG(_("E922: expected a dict"));
11968 vim_free(name);
11969 return;
11970 }
11971 if (argvars[dict_idx].vval.v_dict == NULL)
11972 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011973 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011974 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011975 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011976 if (argvars[arg_idx].v_type != VAR_LIST)
11977 {
11978 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11979 vim_free(name);
11980 return;
11981 }
11982 list = argvars[arg_idx].vval.v_list;
11983 if (list == NULL || list->lv_len == 0)
11984 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011985 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011986 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011987 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011988 {
11989 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011990
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011991 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010011992 if (pt == NULL)
11993 vim_free(name);
11994 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011995 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011996 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011997 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011998 listitem_T *li;
11999 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012000 int arg_len = 0;
12001 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012002
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012003 if (arg_pt != NULL)
12004 arg_len = arg_pt->pt_argc;
12005 if (list != NULL)
12006 lv_len = list->lv_len;
12007 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012008 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012009 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012010 if (pt->pt_argv == NULL)
12011 {
12012 vim_free(pt);
12013 vim_free(name);
12014 return;
12015 }
12016 else
12017 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012018 for (i = 0; i < arg_len; i++)
12019 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12020 if (lv_len > 0)
12021 for (li = list->lv_first; li != NULL;
12022 li = li->li_next)
12023 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012024 }
12025 }
12026
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012027 /* For "function(dict.func, [], dict)" and "func" is a partial
12028 * use "dict". That is backwards compatible. */
12029 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012030 {
12031 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12032 ++pt->pt_dict->dv_refcount;
12033 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012034 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012035 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012036 pt->pt_dict = arg_pt->pt_dict;
12037 if (pt->pt_dict != NULL)
12038 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012039 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012040
12041 pt->pt_refcount = 1;
12042 pt->pt_name = name;
12043 func_ref(pt->pt_name);
12044 }
12045 rettv->v_type = VAR_PARTIAL;
12046 rettv->vval.v_partial = pt;
12047 }
12048 else
12049 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012050 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012051 rettv->v_type = VAR_FUNC;
12052 rettv->vval.v_string = name;
12053 func_ref(name);
12054 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012055 }
12056}
12057
12058/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012059 * "garbagecollect()" function
12060 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012061 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012062f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012063{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012064 /* This is postponed until we are back at the toplevel, because we may be
12065 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12066 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012067
12068 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12069 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012070}
12071
12072/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012073 * "get()" function
12074 */
12075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012076f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012077{
Bram Moolenaar33570922005-01-25 22:26:29 +000012078 listitem_T *li;
12079 list_T *l;
12080 dictitem_T *di;
12081 dict_T *d;
12082 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012083
Bram Moolenaare9a41262005-01-15 22:18:47 +000012084 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012085 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012086 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012087 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012088 int error = FALSE;
12089
12090 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12091 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012092 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012093 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012094 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012095 else if (argvars[0].v_type == VAR_DICT)
12096 {
12097 if ((d = argvars[0].vval.v_dict) != NULL)
12098 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012099 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012100 if (di != NULL)
12101 tv = &di->di_tv;
12102 }
12103 }
12104 else
12105 EMSG2(_(e_listdictarg), "get()");
12106
12107 if (tv == NULL)
12108 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012109 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012110 copy_tv(&argvars[2], rettv);
12111 }
12112 else
12113 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012114}
12115
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012116static 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 +000012117
12118/*
12119 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012120 * Return a range (from start to end) of lines in rettv from the specified
12121 * buffer.
12122 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012123 */
12124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012125get_buffer_lines(
12126 buf_T *buf,
12127 linenr_T start,
12128 linenr_T end,
12129 int retlist,
12130 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012131{
12132 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012133
Bram Moolenaar959a1432013-12-14 12:17:38 +010012134 rettv->v_type = VAR_STRING;
12135 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012136 if (retlist && rettv_list_alloc(rettv) == FAIL)
12137 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012138
12139 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12140 return;
12141
12142 if (!retlist)
12143 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012144 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12145 p = ml_get_buf(buf, start, FALSE);
12146 else
12147 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012148 rettv->vval.v_string = vim_strsave(p);
12149 }
12150 else
12151 {
12152 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012153 return;
12154
12155 if (start < 1)
12156 start = 1;
12157 if (end > buf->b_ml.ml_line_count)
12158 end = buf->b_ml.ml_line_count;
12159 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012160 if (list_append_string(rettv->vval.v_list,
12161 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012162 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012163 }
12164}
12165
12166/*
12167 * "getbufline()" function
12168 */
12169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012170f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012171{
12172 linenr_T lnum;
12173 linenr_T end;
12174 buf_T *buf;
12175
12176 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12177 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012178 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012179 --emsg_off;
12180
Bram Moolenaar661b1822005-07-28 22:36:45 +000012181 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012182 if (argvars[2].v_type == VAR_UNKNOWN)
12183 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012184 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012185 end = get_tv_lnum_buf(&argvars[2], buf);
12186
Bram Moolenaar342337a2005-07-21 21:11:17 +000012187 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012188}
12189
Bram Moolenaar0d660222005-01-07 21:51:51 +000012190/*
12191 * "getbufvar()" function
12192 */
12193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012194f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012195{
12196 buf_T *buf;
12197 buf_T *save_curbuf;
12198 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012199 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012200 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012201
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012202 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12203 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012204 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012205 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012206
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012207 rettv->v_type = VAR_STRING;
12208 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012209
12210 if (buf != NULL && varname != NULL)
12211 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012212 /* set curbuf to be our buf, temporarily */
12213 save_curbuf = curbuf;
12214 curbuf = buf;
12215
Bram Moolenaar0d660222005-01-07 21:51:51 +000012216 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012217 {
12218 if (get_option_tv(&varname, rettv, TRUE) == OK)
12219 done = TRUE;
12220 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012221 else if (STRCMP(varname, "changedtick") == 0)
12222 {
12223 rettv->v_type = VAR_NUMBER;
12224 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012225 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012226 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012227 else
12228 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012229 /* Look up the variable. */
12230 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12231 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12232 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012233 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012234 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012235 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012236 done = TRUE;
12237 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012238 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012239
12240 /* restore previous notion of curbuf */
12241 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242 }
12243
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012244 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12245 /* use the default value */
12246 copy_tv(&argvars[2], rettv);
12247
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248 --emsg_off;
12249}
12250
12251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012252 * "getchar()" function
12253 */
12254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012255f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012256{
12257 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012259
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012260 /* Position the cursor. Needed after a message that ends in a space. */
12261 windgoto(msg_row, msg_col);
12262
Bram Moolenaar071d4272004-06-13 20:20:40 +000012263 ++no_mapping;
12264 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012265 for (;;)
12266 {
12267 if (argvars[0].v_type == VAR_UNKNOWN)
12268 /* getchar(): blocking wait. */
12269 n = safe_vgetc();
12270 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12271 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012272 n = vpeekc_any();
12273 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012274 /* illegal argument or getchar(0) and no char avail: return zero */
12275 n = 0;
12276 else
12277 /* getchar(0) and char avail: return char */
12278 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012279
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012280 if (n == K_IGNORE)
12281 continue;
12282 break;
12283 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012284 --no_mapping;
12285 --allow_keys;
12286
Bram Moolenaar219b8702006-11-01 14:32:36 +000012287 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12288 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12289 vimvars[VV_MOUSE_COL].vv_nr = 0;
12290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012291 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012292 if (IS_SPECIAL(n) || mod_mask != 0)
12293 {
12294 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12295 int i = 0;
12296
12297 /* Turn a special key into three bytes, plus modifier. */
12298 if (mod_mask != 0)
12299 {
12300 temp[i++] = K_SPECIAL;
12301 temp[i++] = KS_MODIFIER;
12302 temp[i++] = mod_mask;
12303 }
12304 if (IS_SPECIAL(n))
12305 {
12306 temp[i++] = K_SPECIAL;
12307 temp[i++] = K_SECOND(n);
12308 temp[i++] = K_THIRD(n);
12309 }
12310#ifdef FEAT_MBYTE
12311 else if (has_mbyte)
12312 i += (*mb_char2bytes)(n, temp + i);
12313#endif
12314 else
12315 temp[i++] = n;
12316 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012317 rettv->v_type = VAR_STRING;
12318 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012319
12320#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012321 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012322 {
12323 int row = mouse_row;
12324 int col = mouse_col;
12325 win_T *win;
12326 linenr_T lnum;
12327# ifdef FEAT_WINDOWS
12328 win_T *wp;
12329# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012330 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012331
12332 if (row >= 0 && col >= 0)
12333 {
12334 /* Find the window at the mouse coordinates and compute the
12335 * text position. */
12336 win = mouse_find_win(&row, &col);
12337 (void)mouse_comp_pos(win, &row, &col, &lnum);
12338# ifdef FEAT_WINDOWS
12339 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012340 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012341# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012342 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012343 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12344 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12345 }
12346 }
12347#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012348 }
12349}
12350
12351/*
12352 * "getcharmod()" function
12353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012354 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012355f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012356{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012357 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012358}
12359
12360/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012361 * "getcharsearch()" function
12362 */
12363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012364f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012365{
12366 if (rettv_dict_alloc(rettv) != FAIL)
12367 {
12368 dict_T *dict = rettv->vval.v_dict;
12369
12370 dict_add_nr_str(dict, "char", 0L, last_csearch());
12371 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12372 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12373 }
12374}
12375
12376/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012377 * "getcmdline()" function
12378 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012380f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012381{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012382 rettv->v_type = VAR_STRING;
12383 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012384}
12385
12386/*
12387 * "getcmdpos()" function
12388 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012390f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012391{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012392 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012393}
12394
12395/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012396 * "getcmdtype()" function
12397 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012399f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012400{
12401 rettv->v_type = VAR_STRING;
12402 rettv->vval.v_string = alloc(2);
12403 if (rettv->vval.v_string != NULL)
12404 {
12405 rettv->vval.v_string[0] = get_cmdline_type();
12406 rettv->vval.v_string[1] = NUL;
12407 }
12408}
12409
12410/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012411 * "getcmdwintype()" function
12412 */
12413 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012414f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012415{
12416 rettv->v_type = VAR_STRING;
12417 rettv->vval.v_string = NULL;
12418#ifdef FEAT_CMDWIN
12419 rettv->vval.v_string = alloc(2);
12420 if (rettv->vval.v_string != NULL)
12421 {
12422 rettv->vval.v_string[0] = cmdwin_type;
12423 rettv->vval.v_string[1] = NUL;
12424 }
12425#endif
12426}
12427
12428/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012429 * "getcwd()" function
12430 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012432f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012434 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012435 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012438 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012439
12440 wp = find_tabwin(&argvars[0], &argvars[1]);
12441 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012443 if (wp->w_localdir != NULL)
12444 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12445 else if(globaldir != NULL)
12446 rettv->vval.v_string = vim_strsave(globaldir);
12447 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012448 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012449 cwd = alloc(MAXPATHL);
12450 if (cwd != NULL)
12451 {
12452 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12453 rettv->vval.v_string = vim_strsave(cwd);
12454 vim_free(cwd);
12455 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012456 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012457#ifdef BACKSLASH_IN_FILENAME
12458 if (rettv->vval.v_string != NULL)
12459 slash_adjust(rettv->vval.v_string);
12460#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012461 }
12462}
12463
12464/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012465 * "getfontname()" function
12466 */
12467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012468f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012469{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012470 rettv->v_type = VAR_STRING;
12471 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012472#ifdef FEAT_GUI
12473 if (gui.in_use)
12474 {
12475 GuiFont font;
12476 char_u *name = NULL;
12477
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012478 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012479 {
12480 /* Get the "Normal" font. Either the name saved by
12481 * hl_set_font_name() or from the font ID. */
12482 font = gui.norm_font;
12483 name = hl_get_font_name();
12484 }
12485 else
12486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012487 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012488 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12489 return;
12490 font = gui_mch_get_font(name, FALSE);
12491 if (font == NOFONT)
12492 return; /* Invalid font name, return empty string. */
12493 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012494 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012495 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012496 gui_mch_free_font(font);
12497 }
12498#endif
12499}
12500
12501/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012502 * "getfperm({fname})" function
12503 */
12504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012505f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012506{
12507 char_u *fname;
12508 struct stat st;
12509 char_u *perm = NULL;
12510 char_u flags[] = "rwx";
12511 int i;
12512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012514
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012515 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012516 if (mch_stat((char *)fname, &st) >= 0)
12517 {
12518 perm = vim_strsave((char_u *)"---------");
12519 if (perm != NULL)
12520 {
12521 for (i = 0; i < 9; i++)
12522 {
12523 if (st.st_mode & (1 << (8 - i)))
12524 perm[i] = flags[i % 3];
12525 }
12526 }
12527 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012529}
12530
12531/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012532 * "getfsize({fname})" function
12533 */
12534 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012535f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012536{
12537 char_u *fname;
12538 struct stat st;
12539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012540 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012541
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012542 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012543
12544 if (mch_stat((char *)fname, &st) >= 0)
12545 {
12546 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012547 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012548 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012549 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012550 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012551
12552 /* non-perfect check for overflow */
12553 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12554 rettv->vval.v_number = -2;
12555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012556 }
12557 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012558 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012559}
12560
12561/*
12562 * "getftime({fname})" function
12563 */
12564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012565f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012566{
12567 char_u *fname;
12568 struct stat st;
12569
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012570 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012571
12572 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012574 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012576}
12577
12578/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012579 * "getftype({fname})" function
12580 */
12581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012582f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012583{
12584 char_u *fname;
12585 struct stat st;
12586 char_u *type = NULL;
12587 char *t;
12588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012592 if (mch_lstat((char *)fname, &st) >= 0)
12593 {
12594#ifdef S_ISREG
12595 if (S_ISREG(st.st_mode))
12596 t = "file";
12597 else if (S_ISDIR(st.st_mode))
12598 t = "dir";
12599# ifdef S_ISLNK
12600 else if (S_ISLNK(st.st_mode))
12601 t = "link";
12602# endif
12603# ifdef S_ISBLK
12604 else if (S_ISBLK(st.st_mode))
12605 t = "bdev";
12606# endif
12607# ifdef S_ISCHR
12608 else if (S_ISCHR(st.st_mode))
12609 t = "cdev";
12610# endif
12611# ifdef S_ISFIFO
12612 else if (S_ISFIFO(st.st_mode))
12613 t = "fifo";
12614# endif
12615# ifdef S_ISSOCK
12616 else if (S_ISSOCK(st.st_mode))
12617 t = "fifo";
12618# endif
12619 else
12620 t = "other";
12621#else
12622# ifdef S_IFMT
12623 switch (st.st_mode & S_IFMT)
12624 {
12625 case S_IFREG: t = "file"; break;
12626 case S_IFDIR: t = "dir"; break;
12627# ifdef S_IFLNK
12628 case S_IFLNK: t = "link"; break;
12629# endif
12630# ifdef S_IFBLK
12631 case S_IFBLK: t = "bdev"; break;
12632# endif
12633# ifdef S_IFCHR
12634 case S_IFCHR: t = "cdev"; break;
12635# endif
12636# ifdef S_IFIFO
12637 case S_IFIFO: t = "fifo"; break;
12638# endif
12639# ifdef S_IFSOCK
12640 case S_IFSOCK: t = "socket"; break;
12641# endif
12642 default: t = "other";
12643 }
12644# else
12645 if (mch_isdir(fname))
12646 t = "dir";
12647 else
12648 t = "file";
12649# endif
12650#endif
12651 type = vim_strsave((char_u *)t);
12652 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012653 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012654}
12655
12656/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012657 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012658 */
12659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012660f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012661{
12662 linenr_T lnum;
12663 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012664 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012665
12666 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012667 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012668 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012669 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012670 retlist = FALSE;
12671 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012672 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012673 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012674 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012675 retlist = TRUE;
12676 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012677
Bram Moolenaar342337a2005-07-21 21:11:17 +000012678 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012679}
12680
12681/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012682 * "getmatches()" function
12683 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012685f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012686{
12687#ifdef FEAT_SEARCH_EXTRA
12688 dict_T *dict;
12689 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012690 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012691
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012692 if (rettv_list_alloc(rettv) == OK)
12693 {
12694 while (cur != NULL)
12695 {
12696 dict = dict_alloc();
12697 if (dict == NULL)
12698 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012699 if (cur->match.regprog == NULL)
12700 {
12701 /* match added with matchaddpos() */
12702 for (i = 0; i < MAXPOSMATCH; ++i)
12703 {
12704 llpos_T *llpos;
12705 char buf[6];
12706 list_T *l;
12707
12708 llpos = &cur->pos.pos[i];
12709 if (llpos->lnum == 0)
12710 break;
12711 l = list_alloc();
12712 if (l == NULL)
12713 break;
12714 list_append_number(l, (varnumber_T)llpos->lnum);
12715 if (llpos->col > 0)
12716 {
12717 list_append_number(l, (varnumber_T)llpos->col);
12718 list_append_number(l, (varnumber_T)llpos->len);
12719 }
12720 sprintf(buf, "pos%d", i + 1);
12721 dict_add_list(dict, buf, l);
12722 }
12723 }
12724 else
12725 {
12726 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12727 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012728 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012729 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12730 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012731# ifdef FEAT_CONCEAL
12732 if (cur->conceal_char)
12733 {
12734 char_u buf[MB_MAXBYTES + 1];
12735
12736 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12737 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12738 }
12739# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012740 list_append_dict(rettv->vval.v_list, dict);
12741 cur = cur->next;
12742 }
12743 }
12744#endif
12745}
12746
12747/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012748 * "getpid()" function
12749 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012751f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012752{
12753 rettv->vval.v_number = mch_get_pid();
12754}
12755
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012756static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012757
12758/*
12759 * "getcurpos()" function
12760 */
12761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012762f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012763{
12764 getpos_both(argvars, rettv, TRUE);
12765}
12766
Bram Moolenaar18081e32008-02-20 19:11:07 +000012767/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012768 * "getpos(string)" function
12769 */
12770 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012771f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012772{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012773 getpos_both(argvars, rettv, FALSE);
12774}
12775
12776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012777getpos_both(
12778 typval_T *argvars,
12779 typval_T *rettv,
12780 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012781{
Bram Moolenaara5525202006-03-02 22:52:09 +000012782 pos_T *fp;
12783 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012784 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012785
12786 if (rettv_list_alloc(rettv) == OK)
12787 {
12788 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012789 if (getcurpos)
12790 fp = &curwin->w_cursor;
12791 else
12792 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012793 if (fnum != -1)
12794 list_append_number(l, (varnumber_T)fnum);
12795 else
12796 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012797 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12798 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012799 list_append_number(l, (fp != NULL)
12800 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012801 : (varnumber_T)0);
12802 list_append_number(l,
12803#ifdef FEAT_VIRTUALEDIT
12804 (fp != NULL) ? (varnumber_T)fp->coladd :
12805#endif
12806 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012807 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012808 {
12809 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012810 list_append_number(l, curwin->w_curswant == MAXCOL ?
12811 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012812 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012813 }
12814 else
12815 rettv->vval.v_number = FALSE;
12816}
12817
12818/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012819 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012820 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012821 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012822f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012823{
12824#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012825 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012826#endif
12827
Bram Moolenaar2641f772005-03-25 21:58:17 +000012828#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012829 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012830 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012831 wp = NULL;
12832 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12833 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012834 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012835 if (wp == NULL)
12836 return;
12837 }
12838
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012839 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012840 }
12841#endif
12842}
12843
12844/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012845 * "getreg()" function
12846 */
12847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012848f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012849{
12850 char_u *strregname;
12851 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012852 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012853 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012854 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012855
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012856 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012857 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012858 strregname = get_tv_string_chk(&argvars[0]);
12859 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012860 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012861 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012862 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012863 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12864 return_list = get_tv_number_chk(&argvars[2], &error);
12865 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012867 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012868 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012869
12870 if (error)
12871 return;
12872
Bram Moolenaar071d4272004-06-13 20:20:40 +000012873 regname = (strregname == NULL ? '"' : *strregname);
12874 if (regname == 0)
12875 regname = '"';
12876
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012877 if (return_list)
12878 {
12879 rettv->v_type = VAR_LIST;
12880 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12881 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012882 if (rettv->vval.v_list != NULL)
12883 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012884 }
12885 else
12886 {
12887 rettv->v_type = VAR_STRING;
12888 rettv->vval.v_string = get_reg_contents(regname,
12889 arg2 ? GREG_EXPR_SRC : 0);
12890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012891}
12892
12893/*
12894 * "getregtype()" function
12895 */
12896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012897f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898{
12899 char_u *strregname;
12900 int regname;
12901 char_u buf[NUMBUFLEN + 2];
12902 long reglen = 0;
12903
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012904 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012905 {
12906 strregname = get_tv_string_chk(&argvars[0]);
12907 if (strregname == NULL) /* type error; errmsg already given */
12908 {
12909 rettv->v_type = VAR_STRING;
12910 rettv->vval.v_string = NULL;
12911 return;
12912 }
12913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 else
12915 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012916 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012917
12918 regname = (strregname == NULL ? '"' : *strregname);
12919 if (regname == 0)
12920 regname = '"';
12921
12922 buf[0] = NUL;
12923 buf[1] = NUL;
12924 switch (get_reg_type(regname, &reglen))
12925 {
12926 case MLINE: buf[0] = 'V'; break;
12927 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012928 case MBLOCK:
12929 buf[0] = Ctrl_V;
12930 sprintf((char *)buf + 1, "%ld", reglen + 1);
12931 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012932 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012933 rettv->v_type = VAR_STRING;
12934 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935}
12936
12937/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012938 * "gettabvar()" function
12939 */
12940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012941f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012942{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012943 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012944 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012945 dictitem_T *v;
12946 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012947 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012948
12949 rettv->v_type = VAR_STRING;
12950 rettv->vval.v_string = NULL;
12951
12952 varname = get_tv_string_chk(&argvars[1]);
12953 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12954 if (tp != NULL && varname != NULL)
12955 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012956 /* Set tp to be our tabpage, temporarily. Also set the window to the
12957 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012958 if (switch_win(&oldcurwin, &oldtabpage,
12959 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012960 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012961 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012962 /* look up the variable */
12963 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12964 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12965 if (v != NULL)
12966 {
12967 copy_tv(&v->di_tv, rettv);
12968 done = TRUE;
12969 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012970 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012971
12972 /* restore previous notion of curwin */
12973 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012974 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012975
12976 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012977 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012978}
12979
12980/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012981 * "gettabwinvar()" function
12982 */
12983 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012984f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012985{
12986 getwinvar(argvars, rettv, 1);
12987}
12988
12989/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 * "getwinposx()" function
12991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012992 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012993f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012994{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012995 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012996#ifdef FEAT_GUI
12997 if (gui.in_use)
12998 {
12999 int x, y;
13000
13001 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013002 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013003 }
13004#endif
13005}
13006
13007/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013008 * "win_findbuf()" function
13009 */
13010 static void
13011f_win_findbuf(typval_T *argvars, typval_T *rettv)
13012{
13013 if (rettv_list_alloc(rettv) != FAIL)
13014 win_findbuf(argvars, rettv->vval.v_list);
13015}
13016
13017/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013018 * "win_getid()" function
13019 */
13020 static void
13021f_win_getid(typval_T *argvars, typval_T *rettv)
13022{
13023 rettv->vval.v_number = win_getid(argvars);
13024}
13025
13026/*
13027 * "win_gotoid()" function
13028 */
13029 static void
13030f_win_gotoid(typval_T *argvars, typval_T *rettv)
13031{
13032 rettv->vval.v_number = win_gotoid(argvars);
13033}
13034
13035/*
13036 * "win_id2tabwin()" function
13037 */
13038 static void
13039f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13040{
13041 if (rettv_list_alloc(rettv) != FAIL)
13042 win_id2tabwin(argvars, rettv->vval.v_list);
13043}
13044
13045/*
13046 * "win_id2win()" function
13047 */
13048 static void
13049f_win_id2win(typval_T *argvars, typval_T *rettv)
13050{
13051 rettv->vval.v_number = win_id2win(argvars);
13052}
13053
13054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013055 * "getwinposy()" function
13056 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013058f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013059{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061#ifdef FEAT_GUI
13062 if (gui.in_use)
13063 {
13064 int x, y;
13065
13066 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013067 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013068 }
13069#endif
13070}
13071
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013072/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013073 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013074 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013075 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013076find_win_by_nr(
13077 typval_T *vp,
13078 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013079{
13080#ifdef FEAT_WINDOWS
13081 win_T *wp;
13082#endif
13083 int nr;
13084
13085 nr = get_tv_number_chk(vp, NULL);
13086
13087#ifdef FEAT_WINDOWS
13088 if (nr < 0)
13089 return NULL;
13090 if (nr == 0)
13091 return curwin;
13092
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013093 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13094 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013095 if (--nr <= 0)
13096 break;
13097 return wp;
13098#else
13099 if (nr == 0 || nr == 1)
13100 return curwin;
13101 return NULL;
13102#endif
13103}
13104
Bram Moolenaar071d4272004-06-13 20:20:40 +000013105/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013106 * Find window specified by "wvp" in tabpage "tvp".
13107 */
13108 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013109find_tabwin(
13110 typval_T *wvp, /* VAR_UNKNOWN for current window */
13111 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013112{
13113 win_T *wp = NULL;
13114 tabpage_T *tp = NULL;
13115 long n;
13116
13117 if (wvp->v_type != VAR_UNKNOWN)
13118 {
13119 if (tvp->v_type != VAR_UNKNOWN)
13120 {
13121 n = get_tv_number(tvp);
13122 if (n >= 0)
13123 tp = find_tabpage(n);
13124 }
13125 else
13126 tp = curtab;
13127
13128 if (tp != NULL)
13129 wp = find_win_by_nr(wvp, tp);
13130 }
13131 else
13132 wp = curwin;
13133
13134 return wp;
13135}
13136
13137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013138 * "getwinvar()" function
13139 */
13140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013141f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013142{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013143 getwinvar(argvars, rettv, 0);
13144}
13145
13146/*
13147 * getwinvar() and gettabwinvar()
13148 */
13149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013150getwinvar(
13151 typval_T *argvars,
13152 typval_T *rettv,
13153 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013154{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013155 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013156 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013157 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013158 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013159 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013160#ifdef FEAT_WINDOWS
13161 win_T *oldcurwin;
13162 tabpage_T *oldtabpage;
13163 int need_switch_win;
13164#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013165
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013166#ifdef FEAT_WINDOWS
13167 if (off == 1)
13168 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13169 else
13170 tp = curtab;
13171#endif
13172 win = find_win_by_nr(&argvars[off], tp);
13173 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013174 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013176 rettv->v_type = VAR_STRING;
13177 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013178
13179 if (win != NULL && varname != NULL)
13180 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013181#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013182 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013183 * otherwise the window is not valid. Only do this when needed,
13184 * autocommands get blocked. */
13185 need_switch_win = !(tp == curtab && win == curwin);
13186 if (!need_switch_win
13187 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13188#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013189 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013190 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013191 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013192 if (get_option_tv(&varname, rettv, 1) == OK)
13193 done = TRUE;
13194 }
13195 else
13196 {
13197 /* Look up the variable. */
13198 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13199 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13200 varname, FALSE);
13201 if (v != NULL)
13202 {
13203 copy_tv(&v->di_tv, rettv);
13204 done = TRUE;
13205 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013206 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013207 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013208
Bram Moolenaarba117c22015-09-29 16:53:22 +020013209#ifdef FEAT_WINDOWS
13210 if (need_switch_win)
13211 /* restore previous notion of curwin */
13212 restore_win(oldcurwin, oldtabpage, TRUE);
13213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 }
13215
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013216 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13217 /* use the default return value */
13218 copy_tv(&argvars[off + 2], rettv);
13219
Bram Moolenaar071d4272004-06-13 20:20:40 +000013220 --emsg_off;
13221}
13222
13223/*
13224 * "glob()" function
13225 */
13226 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013227f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013228{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013229 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013230 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013231 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013233 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013234 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013236 if (argvars[1].v_type != VAR_UNKNOWN)
13237 {
13238 if (get_tv_number_chk(&argvars[1], &error))
13239 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013240 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013241 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013242 if (get_tv_number_chk(&argvars[2], &error))
13243 {
13244 rettv->v_type = VAR_LIST;
13245 rettv->vval.v_list = NULL;
13246 }
13247 if (argvars[3].v_type != VAR_UNKNOWN
13248 && get_tv_number_chk(&argvars[3], &error))
13249 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013250 }
13251 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013252 if (!error)
13253 {
13254 ExpandInit(&xpc);
13255 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013256 if (p_wic)
13257 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013258 if (rettv->v_type == VAR_STRING)
13259 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013260 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013261 else if (rettv_list_alloc(rettv) != FAIL)
13262 {
13263 int i;
13264
13265 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13266 NULL, options, WILD_ALL_KEEP);
13267 for (i = 0; i < xpc.xp_numfiles; i++)
13268 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13269
13270 ExpandCleanup(&xpc);
13271 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013272 }
13273 else
13274 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275}
13276
13277/*
13278 * "globpath()" function
13279 */
13280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013281f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013283 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013284 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013285 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013286 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013287 garray_T ga;
13288 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013289
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013290 /* When the optional second argument is non-zero, don't remove matches
13291 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013292 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013293 if (argvars[2].v_type != VAR_UNKNOWN)
13294 {
13295 if (get_tv_number_chk(&argvars[2], &error))
13296 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013297 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013298 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013299 if (get_tv_number_chk(&argvars[3], &error))
13300 {
13301 rettv->v_type = VAR_LIST;
13302 rettv->vval.v_list = NULL;
13303 }
13304 if (argvars[4].v_type != VAR_UNKNOWN
13305 && get_tv_number_chk(&argvars[4], &error))
13306 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013307 }
13308 }
13309 if (file != NULL && !error)
13310 {
13311 ga_init2(&ga, (int)sizeof(char_u *), 10);
13312 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13313 if (rettv->v_type == VAR_STRING)
13314 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13315 else if (rettv_list_alloc(rettv) != FAIL)
13316 for (i = 0; i < ga.ga_len; ++i)
13317 list_append_string(rettv->vval.v_list,
13318 ((char_u **)(ga.ga_data))[i], -1);
13319 ga_clear_strings(&ga);
13320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013321 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013323}
13324
13325/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013326 * "glob2regpat()" function
13327 */
13328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013329f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013330{
13331 char_u *pat = get_tv_string_chk(&argvars[0]);
13332
13333 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013334 rettv->vval.v_string = (pat == NULL)
13335 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013336}
13337
13338/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013339 * "has()" function
13340 */
13341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013342f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013343{
13344 int i;
13345 char_u *name;
13346 int n = FALSE;
13347 static char *(has_list[]) =
13348 {
13349#ifdef AMIGA
13350 "amiga",
13351# ifdef FEAT_ARP
13352 "arp",
13353# endif
13354#endif
13355#ifdef __BEOS__
13356 "beos",
13357#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013358#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013359 "mac",
13360#endif
13361#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013362 "macunix", /* built with 'darwin' enabled */
13363#endif
13364#if defined(__APPLE__) && __APPLE__ == 1
13365 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013367#ifdef __QNX__
13368 "qnx",
13369#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370#ifdef UNIX
13371 "unix",
13372#endif
13373#ifdef VMS
13374 "vms",
13375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013376#ifdef WIN32
13377 "win32",
13378#endif
13379#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13380 "win32unix",
13381#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013382#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013383 "win64",
13384#endif
13385#ifdef EBCDIC
13386 "ebcdic",
13387#endif
13388#ifndef CASE_INSENSITIVE_FILENAME
13389 "fname_case",
13390#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013391#ifdef HAVE_ACL
13392 "acl",
13393#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013394#ifdef FEAT_ARABIC
13395 "arabic",
13396#endif
13397#ifdef FEAT_AUTOCMD
13398 "autocmd",
13399#endif
13400#ifdef FEAT_BEVAL
13401 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013402# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13403 "balloon_multiline",
13404# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013405#endif
13406#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13407 "builtin_terms",
13408# ifdef ALL_BUILTIN_TCAPS
13409 "all_builtin_terms",
13410# endif
13411#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013412#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13413 || defined(FEAT_GUI_W32) \
13414 || defined(FEAT_GUI_MOTIF))
13415 "browsefilter",
13416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417#ifdef FEAT_BYTEOFF
13418 "byte_offset",
13419#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013420#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013421 "channel",
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef FEAT_CINDENT
13424 "cindent",
13425#endif
13426#ifdef FEAT_CLIENTSERVER
13427 "clientserver",
13428#endif
13429#ifdef FEAT_CLIPBOARD
13430 "clipboard",
13431#endif
13432#ifdef FEAT_CMDL_COMPL
13433 "cmdline_compl",
13434#endif
13435#ifdef FEAT_CMDHIST
13436 "cmdline_hist",
13437#endif
13438#ifdef FEAT_COMMENTS
13439 "comments",
13440#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013441#ifdef FEAT_CONCEAL
13442 "conceal",
13443#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013444#ifdef FEAT_CRYPT
13445 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013446 "crypt-blowfish",
13447 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013448#endif
13449#ifdef FEAT_CSCOPE
13450 "cscope",
13451#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013452#ifdef FEAT_CURSORBIND
13453 "cursorbind",
13454#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013455#ifdef CURSOR_SHAPE
13456 "cursorshape",
13457#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013458#ifdef DEBUG
13459 "debug",
13460#endif
13461#ifdef FEAT_CON_DIALOG
13462 "dialog_con",
13463#endif
13464#ifdef FEAT_GUI_DIALOG
13465 "dialog_gui",
13466#endif
13467#ifdef FEAT_DIFF
13468 "diff",
13469#endif
13470#ifdef FEAT_DIGRAPHS
13471 "digraphs",
13472#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013473#ifdef FEAT_DIRECTX
13474 "directx",
13475#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013476#ifdef FEAT_DND
13477 "dnd",
13478#endif
13479#ifdef FEAT_EMACS_TAGS
13480 "emacs_tags",
13481#endif
13482 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013483 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013484#ifdef FEAT_SEARCH_EXTRA
13485 "extra_search",
13486#endif
13487#ifdef FEAT_FKMAP
13488 "farsi",
13489#endif
13490#ifdef FEAT_SEARCHPATH
13491 "file_in_path",
13492#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013493#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013494 "filterpipe",
13495#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013496#ifdef FEAT_FIND_ID
13497 "find_in_path",
13498#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013499#ifdef FEAT_FLOAT
13500 "float",
13501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#ifdef FEAT_FOLDING
13503 "folding",
13504#endif
13505#ifdef FEAT_FOOTER
13506 "footer",
13507#endif
13508#if !defined(USE_SYSTEM) && defined(UNIX)
13509 "fork",
13510#endif
13511#ifdef FEAT_GETTEXT
13512 "gettext",
13513#endif
13514#ifdef FEAT_GUI
13515 "gui",
13516#endif
13517#ifdef FEAT_GUI_ATHENA
13518# ifdef FEAT_GUI_NEXTAW
13519 "gui_neXtaw",
13520# else
13521 "gui_athena",
13522# endif
13523#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013524#ifdef FEAT_GUI_GTK
13525 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013526# ifdef USE_GTK3
13527 "gui_gtk3",
13528# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013529 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013530# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013532#ifdef FEAT_GUI_GNOME
13533 "gui_gnome",
13534#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013535#ifdef FEAT_GUI_MAC
13536 "gui_mac",
13537#endif
13538#ifdef FEAT_GUI_MOTIF
13539 "gui_motif",
13540#endif
13541#ifdef FEAT_GUI_PHOTON
13542 "gui_photon",
13543#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013544#ifdef FEAT_GUI_W32
13545 "gui_win32",
13546#endif
13547#ifdef FEAT_HANGULIN
13548 "hangul_input",
13549#endif
13550#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13551 "iconv",
13552#endif
13553#ifdef FEAT_INS_EXPAND
13554 "insert_expand",
13555#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013556#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013557 "job",
13558#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013559#ifdef FEAT_JUMPLIST
13560 "jumplist",
13561#endif
13562#ifdef FEAT_KEYMAP
13563 "keymap",
13564#endif
13565#ifdef FEAT_LANGMAP
13566 "langmap",
13567#endif
13568#ifdef FEAT_LIBCALL
13569 "libcall",
13570#endif
13571#ifdef FEAT_LINEBREAK
13572 "linebreak",
13573#endif
13574#ifdef FEAT_LISP
13575 "lispindent",
13576#endif
13577#ifdef FEAT_LISTCMDS
13578 "listcmds",
13579#endif
13580#ifdef FEAT_LOCALMAP
13581 "localmap",
13582#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013583#ifdef FEAT_LUA
13584# ifndef DYNAMIC_LUA
13585 "lua",
13586# endif
13587#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013588#ifdef FEAT_MENU
13589 "menu",
13590#endif
13591#ifdef FEAT_SESSION
13592 "mksession",
13593#endif
13594#ifdef FEAT_MODIFY_FNAME
13595 "modify_fname",
13596#endif
13597#ifdef FEAT_MOUSE
13598 "mouse",
13599#endif
13600#ifdef FEAT_MOUSESHAPE
13601 "mouseshape",
13602#endif
13603#if defined(UNIX) || defined(VMS)
13604# ifdef FEAT_MOUSE_DEC
13605 "mouse_dec",
13606# endif
13607# ifdef FEAT_MOUSE_GPM
13608 "mouse_gpm",
13609# endif
13610# ifdef FEAT_MOUSE_JSB
13611 "mouse_jsbterm",
13612# endif
13613# ifdef FEAT_MOUSE_NET
13614 "mouse_netterm",
13615# endif
13616# ifdef FEAT_MOUSE_PTERM
13617 "mouse_pterm",
13618# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013619# ifdef FEAT_MOUSE_SGR
13620 "mouse_sgr",
13621# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013622# ifdef FEAT_SYSMOUSE
13623 "mouse_sysmouse",
13624# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013625# ifdef FEAT_MOUSE_URXVT
13626 "mouse_urxvt",
13627# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013628# ifdef FEAT_MOUSE_XTERM
13629 "mouse_xterm",
13630# endif
13631#endif
13632#ifdef FEAT_MBYTE
13633 "multi_byte",
13634#endif
13635#ifdef FEAT_MBYTE_IME
13636 "multi_byte_ime",
13637#endif
13638#ifdef FEAT_MULTI_LANG
13639 "multi_lang",
13640#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013641#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013642#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013643 "mzscheme",
13644#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#ifdef FEAT_OLE
13647 "ole",
13648#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013649 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013650#ifdef FEAT_PATH_EXTRA
13651 "path_extra",
13652#endif
13653#ifdef FEAT_PERL
13654#ifndef DYNAMIC_PERL
13655 "perl",
13656#endif
13657#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013658#ifdef FEAT_PERSISTENT_UNDO
13659 "persistent_undo",
13660#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013661#ifdef FEAT_PYTHON
13662#ifndef DYNAMIC_PYTHON
13663 "python",
13664#endif
13665#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013666#ifdef FEAT_PYTHON3
13667#ifndef DYNAMIC_PYTHON3
13668 "python3",
13669#endif
13670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013671#ifdef FEAT_POSTSCRIPT
13672 "postscript",
13673#endif
13674#ifdef FEAT_PRINTER
13675 "printer",
13676#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013677#ifdef FEAT_PROFILE
13678 "profile",
13679#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013680#ifdef FEAT_RELTIME
13681 "reltime",
13682#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013683#ifdef FEAT_QUICKFIX
13684 "quickfix",
13685#endif
13686#ifdef FEAT_RIGHTLEFT
13687 "rightleft",
13688#endif
13689#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13690 "ruby",
13691#endif
13692#ifdef FEAT_SCROLLBIND
13693 "scrollbind",
13694#endif
13695#ifdef FEAT_CMDL_INFO
13696 "showcmd",
13697 "cmdline_info",
13698#endif
13699#ifdef FEAT_SIGNS
13700 "signs",
13701#endif
13702#ifdef FEAT_SMARTINDENT
13703 "smartindent",
13704#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013705#ifdef STARTUPTIME
13706 "startuptime",
13707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#ifdef FEAT_STL_OPT
13709 "statusline",
13710#endif
13711#ifdef FEAT_SUN_WORKSHOP
13712 "sun_workshop",
13713#endif
13714#ifdef FEAT_NETBEANS_INTG
13715 "netbeans_intg",
13716#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013717#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013718 "spell",
13719#endif
13720#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721 "syntax",
13722#endif
13723#if defined(USE_SYSTEM) || !defined(UNIX)
13724 "system",
13725#endif
13726#ifdef FEAT_TAG_BINS
13727 "tag_binary",
13728#endif
13729#ifdef FEAT_TAG_OLDSTATIC
13730 "tag_old_static",
13731#endif
13732#ifdef FEAT_TAG_ANYWHITE
13733 "tag_any_white",
13734#endif
13735#ifdef FEAT_TCL
13736# ifndef DYNAMIC_TCL
13737 "tcl",
13738# endif
13739#endif
13740#ifdef TERMINFO
13741 "terminfo",
13742#endif
13743#ifdef FEAT_TERMRESPONSE
13744 "termresponse",
13745#endif
13746#ifdef FEAT_TEXTOBJ
13747 "textobjects",
13748#endif
13749#ifdef HAVE_TGETENT
13750 "tgetent",
13751#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013752#ifdef FEAT_TIMERS
13753 "timers",
13754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef FEAT_TITLE
13756 "title",
13757#endif
13758#ifdef FEAT_TOOLBAR
13759 "toolbar",
13760#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013761#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13762 "unnamedplus",
13763#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013764#ifdef FEAT_USR_CMDS
13765 "user-commands", /* was accidentally included in 5.4 */
13766 "user_commands",
13767#endif
13768#ifdef FEAT_VIMINFO
13769 "viminfo",
13770#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013771#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013772 "vertsplit",
13773#endif
13774#ifdef FEAT_VIRTUALEDIT
13775 "virtualedit",
13776#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013777 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013778#ifdef FEAT_VISUALEXTRA
13779 "visualextra",
13780#endif
13781#ifdef FEAT_VREPLACE
13782 "vreplace",
13783#endif
13784#ifdef FEAT_WILDIGN
13785 "wildignore",
13786#endif
13787#ifdef FEAT_WILDMENU
13788 "wildmenu",
13789#endif
13790#ifdef FEAT_WINDOWS
13791 "windows",
13792#endif
13793#ifdef FEAT_WAK
13794 "winaltkeys",
13795#endif
13796#ifdef FEAT_WRITEBACKUP
13797 "writebackup",
13798#endif
13799#ifdef FEAT_XIM
13800 "xim",
13801#endif
13802#ifdef FEAT_XFONTSET
13803 "xfontset",
13804#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013805#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013806 "xpm",
13807 "xpm_w32", /* for backward compatibility */
13808#else
13809# if defined(HAVE_XPM)
13810 "xpm",
13811# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813#ifdef USE_XSMP
13814 "xsmp",
13815#endif
13816#ifdef USE_XSMP_INTERACT
13817 "xsmp_interact",
13818#endif
13819#ifdef FEAT_XCLIPBOARD
13820 "xterm_clipboard",
13821#endif
13822#ifdef FEAT_XTERM_SAVE
13823 "xterm_save",
13824#endif
13825#if defined(UNIX) && defined(FEAT_X11)
13826 "X11",
13827#endif
13828 NULL
13829 };
13830
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013831 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013832 for (i = 0; has_list[i] != NULL; ++i)
13833 if (STRICMP(name, has_list[i]) == 0)
13834 {
13835 n = TRUE;
13836 break;
13837 }
13838
13839 if (n == FALSE)
13840 {
13841 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013842 {
13843 if (name[5] == '-'
13844 && STRLEN(name) > 11
13845 && vim_isdigit(name[6])
13846 && vim_isdigit(name[8])
13847 && vim_isdigit(name[10]))
13848 {
13849 int major = atoi((char *)name + 6);
13850 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013851
13852 /* Expect "patch-9.9.01234". */
13853 n = (major < VIM_VERSION_MAJOR
13854 || (major == VIM_VERSION_MAJOR
13855 && (minor < VIM_VERSION_MINOR
13856 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013857 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013858 }
13859 else
13860 n = has_patch(atoi((char *)name + 5));
13861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862 else if (STRICMP(name, "vim_starting") == 0)
13863 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013864#ifdef FEAT_MBYTE
13865 else if (STRICMP(name, "multi_byte_encoding") == 0)
13866 n = has_mbyte;
13867#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013868#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13869 else if (STRICMP(name, "balloon_multiline") == 0)
13870 n = multiline_balloon_available();
13871#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013872#ifdef DYNAMIC_TCL
13873 else if (STRICMP(name, "tcl") == 0)
13874 n = tcl_enabled(FALSE);
13875#endif
13876#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13877 else if (STRICMP(name, "iconv") == 0)
13878 n = iconv_enabled(FALSE);
13879#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013880#ifdef DYNAMIC_LUA
13881 else if (STRICMP(name, "lua") == 0)
13882 n = lua_enabled(FALSE);
13883#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013884#ifdef DYNAMIC_MZSCHEME
13885 else if (STRICMP(name, "mzscheme") == 0)
13886 n = mzscheme_enabled(FALSE);
13887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013888#ifdef DYNAMIC_RUBY
13889 else if (STRICMP(name, "ruby") == 0)
13890 n = ruby_enabled(FALSE);
13891#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013892#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013893#ifdef DYNAMIC_PYTHON
13894 else if (STRICMP(name, "python") == 0)
13895 n = python_enabled(FALSE);
13896#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013897#endif
13898#ifdef FEAT_PYTHON3
13899#ifdef DYNAMIC_PYTHON3
13900 else if (STRICMP(name, "python3") == 0)
13901 n = python3_enabled(FALSE);
13902#endif
13903#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013904#ifdef DYNAMIC_PERL
13905 else if (STRICMP(name, "perl") == 0)
13906 n = perl_enabled(FALSE);
13907#endif
13908#ifdef FEAT_GUI
13909 else if (STRICMP(name, "gui_running") == 0)
13910 n = (gui.in_use || gui.starting);
13911# ifdef FEAT_GUI_W32
13912 else if (STRICMP(name, "gui_win32s") == 0)
13913 n = gui_is_win32s();
13914# endif
13915# ifdef FEAT_BROWSE
13916 else if (STRICMP(name, "browse") == 0)
13917 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13918# endif
13919#endif
13920#ifdef FEAT_SYN_HL
13921 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013922 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923#endif
13924#if defined(WIN3264)
13925 else if (STRICMP(name, "win95") == 0)
13926 n = mch_windows95();
13927#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013928#ifdef FEAT_NETBEANS_INTG
13929 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013930 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013931#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013932 }
13933
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935}
13936
13937/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013938 * "has_key()" function
13939 */
13940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013941f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013942{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013943 if (argvars[0].v_type != VAR_DICT)
13944 {
13945 EMSG(_(e_dictreq));
13946 return;
13947 }
13948 if (argvars[0].vval.v_dict == NULL)
13949 return;
13950
13951 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013952 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013953}
13954
13955/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013956 * "haslocaldir()" function
13957 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013959f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013960{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013961 win_T *wp = NULL;
13962
13963 wp = find_tabwin(&argvars[0], &argvars[1]);
13964 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013965}
13966
13967/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013968 * "hasmapto()" function
13969 */
13970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013971f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972{
13973 char_u *name;
13974 char_u *mode;
13975 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013976 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013978 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013979 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013980 mode = (char_u *)"nvo";
13981 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013982 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013984 if (argvars[2].v_type != VAR_UNKNOWN)
13985 abbr = get_tv_number(&argvars[2]);
13986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987
Bram Moolenaar2c932302006-03-18 21:42:09 +000013988 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013991 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013992}
13993
13994/*
13995 * "histadd()" function
13996 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013998f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013999{
14000#ifdef FEAT_CMDHIST
14001 int histype;
14002 char_u *str;
14003 char_u buf[NUMBUFLEN];
14004#endif
14005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014006 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014007 if (check_restricted() || check_secure())
14008 return;
14009#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014010 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14011 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 if (histype >= 0)
14013 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014014 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 if (*str != NUL)
14016 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014017 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014018 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014019 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014020 return;
14021 }
14022 }
14023#endif
14024}
14025
14026/*
14027 * "histdel()" function
14028 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014030f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031{
14032#ifdef FEAT_CMDHIST
14033 int n;
14034 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014035 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14038 if (str == NULL)
14039 n = 0;
14040 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014042 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014043 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014045 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014046 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014047 else
14048 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014049 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014050 get_tv_string_buf(&argvars[1], buf));
14051 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014052#endif
14053}
14054
14055/*
14056 * "histget()" function
14057 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014059f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014060{
14061#ifdef FEAT_CMDHIST
14062 int type;
14063 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014064 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014066 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14067 if (str == NULL)
14068 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014070 {
14071 type = get_histtype(str);
14072 if (argvars[1].v_type == VAR_UNKNOWN)
14073 idx = get_history_idx(type);
14074 else
14075 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14076 /* -1 on type error */
14077 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14078 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014079#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014080 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014081#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014082 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083}
14084
14085/*
14086 * "histnr()" function
14087 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014089f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090{
14091 int i;
14092
14093#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014094 char_u *history = get_tv_string_chk(&argvars[0]);
14095
14096 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014097 if (i >= HIST_CMD && i < HIST_COUNT)
14098 i = get_history_idx(i);
14099 else
14100#endif
14101 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014102 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014103}
14104
14105/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014106 * "highlightID(name)" function
14107 */
14108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014109f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014111 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112}
14113
14114/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014115 * "highlight_exists()" function
14116 */
14117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014118f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014119{
14120 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14121}
14122
14123/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014124 * "hostname()" function
14125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014127f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128{
14129 char_u hostname[256];
14130
14131 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014132 rettv->v_type = VAR_STRING;
14133 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014134}
14135
14136/*
14137 * iconv() function
14138 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014140f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141{
14142#ifdef FEAT_MBYTE
14143 char_u buf1[NUMBUFLEN];
14144 char_u buf2[NUMBUFLEN];
14145 char_u *from, *to, *str;
14146 vimconv_T vimconv;
14147#endif
14148
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->v_type = VAR_STRING;
14150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014151
14152#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014153 str = get_tv_string(&argvars[0]);
14154 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14155 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014156 vimconv.vc_type = CONV_NONE;
14157 convert_setup(&vimconv, from, to);
14158
14159 /* If the encodings are equal, no conversion needed. */
14160 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014161 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014162 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014163 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164
14165 convert_setup(&vimconv, NULL, NULL);
14166 vim_free(from);
14167 vim_free(to);
14168#endif
14169}
14170
14171/*
14172 * "indent()" function
14173 */
14174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014175f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014176{
14177 linenr_T lnum;
14178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014180 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014183 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184}
14185
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014186/*
14187 * "index()" function
14188 */
14189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014190f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014191{
Bram Moolenaar33570922005-01-25 22:26:29 +000014192 list_T *l;
14193 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014194 long idx = 0;
14195 int ic = FALSE;
14196
14197 rettv->vval.v_number = -1;
14198 if (argvars[0].v_type != VAR_LIST)
14199 {
14200 EMSG(_(e_listreq));
14201 return;
14202 }
14203 l = argvars[0].vval.v_list;
14204 if (l != NULL)
14205 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014206 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014207 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014208 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014209 int error = FALSE;
14210
Bram Moolenaar758711c2005-02-02 23:11:38 +000014211 /* Start at specified item. Use the cached index that list_find()
14212 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014213 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014214 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014215 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014216 ic = get_tv_number_chk(&argvars[3], &error);
14217 if (error)
14218 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014219 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014220
Bram Moolenaar758711c2005-02-02 23:11:38 +000014221 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014222 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014223 {
14224 rettv->vval.v_number = idx;
14225 break;
14226 }
14227 }
14228}
14229
Bram Moolenaar071d4272004-06-13 20:20:40 +000014230static int inputsecret_flag = 0;
14231
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014232static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014233
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014235 * This function is used by f_input() and f_inputdialog() functions. The third
14236 * argument to f_input() specifies the type of completion to use at the
14237 * prompt. The third argument to f_inputdialog() specifies the value to return
14238 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014239 */
14240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014241get_user_input(
14242 typval_T *argvars,
14243 typval_T *rettv,
14244 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014245{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014246 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014247 char_u *p = NULL;
14248 int c;
14249 char_u buf[NUMBUFLEN];
14250 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014251 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014252 int xp_type = EXPAND_NOTHING;
14253 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014255 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014256 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014257
14258#ifdef NO_CONSOLE_INPUT
14259 /* While starting up, there is no place to enter text. */
14260 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014261 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014262#endif
14263
14264 cmd_silent = FALSE; /* Want to see the prompt. */
14265 if (prompt != NULL)
14266 {
14267 /* Only the part of the message after the last NL is considered as
14268 * prompt for the command line */
14269 p = vim_strrchr(prompt, '\n');
14270 if (p == NULL)
14271 p = prompt;
14272 else
14273 {
14274 ++p;
14275 c = *p;
14276 *p = NUL;
14277 msg_start();
14278 msg_clr_eos();
14279 msg_puts_attr(prompt, echo_attr);
14280 msg_didout = FALSE;
14281 msg_starthere();
14282 *p = c;
14283 }
14284 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014285
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014286 if (argvars[1].v_type != VAR_UNKNOWN)
14287 {
14288 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14289 if (defstr != NULL)
14290 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014292 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014293 {
14294 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014295 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014296 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014297
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014298 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014299 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014300
Bram Moolenaar4463f292005-09-25 22:20:24 +000014301 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14302 if (xp_name == NULL)
14303 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014304
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014305 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014306
Bram Moolenaar4463f292005-09-25 22:20:24 +000014307 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14308 &xp_arg) == FAIL)
14309 return;
14310 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014311 }
14312
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014313 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014314 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014315 int save_ex_normal_busy = ex_normal_busy;
14316 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014317 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014318 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14319 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014320 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014321 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014322 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014323 && argvars[1].v_type != VAR_UNKNOWN
14324 && argvars[2].v_type != VAR_UNKNOWN)
14325 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14326 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014327
14328 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014330 /* since the user typed this, no need to wait for return */
14331 need_wait_return = FALSE;
14332 msg_didout = FALSE;
14333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334 cmd_silent = cmd_silent_save;
14335}
14336
14337/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014338 * "input()" function
14339 * Also handles inputsecret() when inputsecret is set.
14340 */
14341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014342f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014343{
14344 get_user_input(argvars, rettv, FALSE);
14345}
14346
14347/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014348 * "inputdialog()" function
14349 */
14350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014351f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014352{
14353#if defined(FEAT_GUI_TEXTDIALOG)
14354 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14355 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14356 {
14357 char_u *message;
14358 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014359 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014360
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014361 message = get_tv_string_chk(&argvars[0]);
14362 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014363 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014364 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014365 else
14366 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014367 if (message != NULL && defstr != NULL
14368 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014369 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014370 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014371 else
14372 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014373 if (message != NULL && defstr != NULL
14374 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014375 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014376 rettv->vval.v_string = vim_strsave(
14377 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014379 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014381 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014382 }
14383 else
14384#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014385 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014386}
14387
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014388/*
14389 * "inputlist()" function
14390 */
14391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014392f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014393{
14394 listitem_T *li;
14395 int selected;
14396 int mouse_used;
14397
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014398#ifdef NO_CONSOLE_INPUT
14399 /* While starting up, there is no place to enter text. */
14400 if (no_console_input())
14401 return;
14402#endif
14403 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14404 {
14405 EMSG2(_(e_listarg), "inputlist()");
14406 return;
14407 }
14408
14409 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014410 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014411 lines_left = Rows; /* avoid more prompt */
14412 msg_scroll = TRUE;
14413 msg_clr_eos();
14414
14415 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14416 {
14417 msg_puts(get_tv_string(&li->li_tv));
14418 msg_putchar('\n');
14419 }
14420
14421 /* Ask for choice. */
14422 selected = prompt_for_number(&mouse_used);
14423 if (mouse_used)
14424 selected -= lines_left;
14425
14426 rettv->vval.v_number = selected;
14427}
14428
14429
Bram Moolenaar071d4272004-06-13 20:20:40 +000014430static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14431
14432/*
14433 * "inputrestore()" function
14434 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014436f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014437{
14438 if (ga_userinput.ga_len > 0)
14439 {
14440 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014441 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14442 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014443 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444 }
14445 else if (p_verbose > 1)
14446 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014447 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014448 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014449 }
14450}
14451
14452/*
14453 * "inputsave()" function
14454 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014455 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014456f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014457{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014458 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014459 if (ga_grow(&ga_userinput, 1) == OK)
14460 {
14461 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14462 + ga_userinput.ga_len);
14463 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014464 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014465 }
14466 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014467 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014468}
14469
14470/*
14471 * "inputsecret()" function
14472 */
14473 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014474f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014475{
14476 ++cmdline_star;
14477 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014478 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479 --cmdline_star;
14480 --inputsecret_flag;
14481}
14482
14483/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014484 * "insert()" function
14485 */
14486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014487f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014488{
14489 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014490 listitem_T *item;
14491 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014492 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014493
14494 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014495 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014496 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014497 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014498 {
14499 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014500 before = get_tv_number_chk(&argvars[2], &error);
14501 if (error)
14502 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014503
Bram Moolenaar758711c2005-02-02 23:11:38 +000014504 if (before == l->lv_len)
14505 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014506 else
14507 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014508 item = list_find(l, before);
14509 if (item == NULL)
14510 {
14511 EMSGN(_(e_listidx), before);
14512 l = NULL;
14513 }
14514 }
14515 if (l != NULL)
14516 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014517 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014518 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014519 }
14520 }
14521}
14522
14523/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014524 * "invert(expr)" function
14525 */
14526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014527f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014528{
14529 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14530}
14531
14532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533 * "isdirectory()" function
14534 */
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014538 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014539}
14540
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014541/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014542 * "islocked()" function
14543 */
14544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014546{
14547 lval_T lv;
14548 char_u *end;
14549 dictitem_T *di;
14550
14551 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014552 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14553 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014554 if (end != NULL && lv.ll_name != NULL)
14555 {
14556 if (*end != NUL)
14557 EMSG(_(e_trailing));
14558 else
14559 {
14560 if (lv.ll_tv == NULL)
14561 {
14562 if (check_changedtick(lv.ll_name))
14563 rettv->vval.v_number = 1; /* always locked */
14564 else
14565 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014566 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014567 if (di != NULL)
14568 {
14569 /* Consider a variable locked when:
14570 * 1. the variable itself is locked
14571 * 2. the value of the variable is locked.
14572 * 3. the List or Dict value is locked.
14573 */
14574 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14575 || tv_islocked(&di->di_tv));
14576 }
14577 }
14578 }
14579 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014580 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014581 else if (lv.ll_newkey != NULL)
14582 EMSG2(_(e_dictkey), lv.ll_newkey);
14583 else if (lv.ll_list != NULL)
14584 /* List item. */
14585 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14586 else
14587 /* Dictionary item. */
14588 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14589 }
14590 }
14591
14592 clear_lval(&lv);
14593}
14594
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014595#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14596/*
14597 * "isnan()" function
14598 */
14599 static void
14600f_isnan(typval_T *argvars, typval_T *rettv)
14601{
14602 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14603 && isnan(argvars[0].vval.v_float);
14604}
14605#endif
14606
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014607static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014608
14609/*
14610 * Turn a dict into a list:
14611 * "what" == 0: list of keys
14612 * "what" == 1: list of values
14613 * "what" == 2: list of items
14614 */
14615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014616dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014617{
Bram Moolenaar33570922005-01-25 22:26:29 +000014618 list_T *l2;
14619 dictitem_T *di;
14620 hashitem_T *hi;
14621 listitem_T *li;
14622 listitem_T *li2;
14623 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014624 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014625
Bram Moolenaar8c711452005-01-14 21:53:12 +000014626 if (argvars[0].v_type != VAR_DICT)
14627 {
14628 EMSG(_(e_dictreq));
14629 return;
14630 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014631 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014632 return;
14633
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014634 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014635 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014636
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014637 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014638 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014640 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014641 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014642 --todo;
14643 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014644
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014645 li = listitem_alloc();
14646 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014647 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014648 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014649
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014650 if (what == 0)
14651 {
14652 /* keys() */
14653 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014654 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014655 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14656 }
14657 else if (what == 1)
14658 {
14659 /* values() */
14660 copy_tv(&di->di_tv, &li->li_tv);
14661 }
14662 else
14663 {
14664 /* items() */
14665 l2 = list_alloc();
14666 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014667 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014668 li->li_tv.vval.v_list = l2;
14669 if (l2 == NULL)
14670 break;
14671 ++l2->lv_refcount;
14672
14673 li2 = listitem_alloc();
14674 if (li2 == NULL)
14675 break;
14676 list_append(l2, li2);
14677 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014678 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014679 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14680
14681 li2 = listitem_alloc();
14682 if (li2 == NULL)
14683 break;
14684 list_append(l2, li2);
14685 copy_tv(&di->di_tv, &li2->li_tv);
14686 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014687 }
14688 }
14689}
14690
14691/*
14692 * "items(dict)" function
14693 */
14694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014695f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696{
14697 dict_list(argvars, rettv, 2);
14698}
14699
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014700#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014701/*
14702 * Get the job from the argument.
14703 * Returns NULL if the job is invalid.
14704 */
14705 static job_T *
14706get_job_arg(typval_T *tv)
14707{
14708 job_T *job;
14709
14710 if (tv->v_type != VAR_JOB)
14711 {
14712 EMSG2(_(e_invarg2), get_tv_string(tv));
14713 return NULL;
14714 }
14715 job = tv->vval.v_job;
14716
14717 if (job == NULL)
14718 EMSG(_("E916: not a valid job"));
14719 return job;
14720}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014721
Bram Moolenaar835dc632016-02-07 14:27:38 +010014722/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014723 * "job_getchannel()" function
14724 */
14725 static void
14726f_job_getchannel(typval_T *argvars, typval_T *rettv)
14727{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014728 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014729
Bram Moolenaar65edff82016-02-21 16:40:11 +010014730 if (job != NULL)
14731 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014732 rettv->v_type = VAR_CHANNEL;
14733 rettv->vval.v_channel = job->jv_channel;
14734 if (job->jv_channel != NULL)
14735 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014736 }
14737}
14738
14739/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014740 * "job_info()" function
14741 */
14742 static void
14743f_job_info(typval_T *argvars, typval_T *rettv)
14744{
14745 job_T *job = get_job_arg(&argvars[0]);
14746
14747 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14748 job_info(job, rettv->vval.v_dict);
14749}
14750
14751/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014752 * "job_setoptions()" function
14753 */
14754 static void
14755f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14756{
14757 job_T *job = get_job_arg(&argvars[0]);
14758 jobopt_T opt;
14759
14760 if (job == NULL)
14761 return;
14762 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014763 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014764 return;
14765 job_set_options(job, &opt);
14766}
14767
14768/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014769 * "job_start()" function
14770 */
14771 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014772f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014773{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014774 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014775 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014776}
14777
14778/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014779 * "job_status()" function
14780 */
14781 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014782f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014783{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014784 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014785
Bram Moolenaar65edff82016-02-21 16:40:11 +010014786 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014787 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014788 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014789 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014790 }
14791}
14792
14793/*
14794 * "job_stop()" function
14795 */
14796 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014797f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014798{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014799 job_T *job = get_job_arg(&argvars[0]);
14800
14801 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014802 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014803}
14804#endif
14805
Bram Moolenaar071d4272004-06-13 20:20:40 +000014806/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014807 * "join()" function
14808 */
14809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014810f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014811{
14812 garray_T ga;
14813 char_u *sep;
14814
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014815 if (argvars[0].v_type != VAR_LIST)
14816 {
14817 EMSG(_(e_listreq));
14818 return;
14819 }
14820 if (argvars[0].vval.v_list == NULL)
14821 return;
14822 if (argvars[1].v_type == VAR_UNKNOWN)
14823 sep = (char_u *)" ";
14824 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014825 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014826
14827 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014828
14829 if (sep != NULL)
14830 {
14831 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014832 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014833 ga_append(&ga, NUL);
14834 rettv->vval.v_string = (char_u *)ga.ga_data;
14835 }
14836 else
14837 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014838}
14839
14840/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014841 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014842 */
14843 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014844f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014845{
14846 js_read_T reader;
14847
14848 reader.js_buf = get_tv_string(&argvars[0]);
14849 reader.js_fill = NULL;
14850 reader.js_used = 0;
14851 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14852 EMSG(_(e_invarg));
14853}
14854
14855/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014856 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014857 */
14858 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014859f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014860{
14861 rettv->v_type = VAR_STRING;
14862 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14863}
14864
14865/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014866 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014867 */
14868 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014869f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014870{
14871 js_read_T reader;
14872
14873 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014874 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014875 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014876 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014877 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014878}
14879
14880/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014881 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014882 */
14883 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014884f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014885{
14886 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014887 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014888}
14889
14890/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014891 * "keys()" function
14892 */
14893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014894f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014895{
14896 dict_list(argvars, rettv, 0);
14897}
14898
14899/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014900 * "last_buffer_nr()" function.
14901 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014903f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014904{
14905 int n = 0;
14906 buf_T *buf;
14907
14908 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14909 if (n < buf->b_fnum)
14910 n = buf->b_fnum;
14911
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014912 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014913}
14914
14915/*
14916 * "len()" function
14917 */
14918 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014919f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014920{
14921 switch (argvars[0].v_type)
14922 {
14923 case VAR_STRING:
14924 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = (varnumber_T)STRLEN(
14926 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014927 break;
14928 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014929 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014930 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014931 case VAR_DICT:
14932 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14933 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014934 case VAR_UNKNOWN:
14935 case VAR_SPECIAL:
14936 case VAR_FLOAT:
14937 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014938 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014939 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014940 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014941 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014942 break;
14943 }
14944}
14945
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014946static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014947
14948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014949libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014950{
14951#ifdef FEAT_LIBCALL
14952 char_u *string_in;
14953 char_u **string_result;
14954 int nr_result;
14955#endif
14956
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014957 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014958 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014960
14961 if (check_restricted() || check_secure())
14962 return;
14963
14964#ifdef FEAT_LIBCALL
14965 /* The first two args must be strings, otherwise its meaningless */
14966 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14967 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014968 string_in = NULL;
14969 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014970 string_in = argvars[2].vval.v_string;
14971 if (type == VAR_NUMBER)
14972 string_result = NULL;
14973 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014975 if (mch_libcall(argvars[0].vval.v_string,
14976 argvars[1].vval.v_string,
14977 string_in,
14978 argvars[2].vval.v_number,
14979 string_result,
14980 &nr_result) == OK
14981 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014982 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014983 }
14984#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014985}
14986
14987/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014988 * "libcall()" function
14989 */
14990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014991f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014992{
14993 libcall_common(argvars, rettv, VAR_STRING);
14994}
14995
14996/*
14997 * "libcallnr()" function
14998 */
14999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015000f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015001{
15002 libcall_common(argvars, rettv, VAR_NUMBER);
15003}
15004
15005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006 * "line(string)" function
15007 */
15008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015009f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015010{
15011 linenr_T lnum = 0;
15012 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015013 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015014
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015015 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015016 if (fp != NULL)
15017 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015018 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015019}
15020
15021/*
15022 * "line2byte(lnum)" function
15023 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015024 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015025f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015026{
15027#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015028 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015029#else
15030 linenr_T lnum;
15031
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015033 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015035 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015036 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15037 if (rettv->vval.v_number >= 0)
15038 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015039#endif
15040}
15041
15042/*
15043 * "lispindent(lnum)" function
15044 */
15045 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015046f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015047{
15048#ifdef FEAT_LISP
15049 pos_T pos;
15050 linenr_T lnum;
15051
15052 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015053 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015054 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15055 {
15056 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015057 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015058 curwin->w_cursor = pos;
15059 }
15060 else
15061#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015062 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063}
15064
15065/*
15066 * "localtime()" function
15067 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015069f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015070{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015071 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072}
15073
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015074static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
15076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015077get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078{
15079 char_u *keys;
15080 char_u *which;
15081 char_u buf[NUMBUFLEN];
15082 char_u *keys_buf = NULL;
15083 char_u *rhs;
15084 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015085 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015086 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015087 mapblock_T *mp;
15088 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015089
15090 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015091 rettv->v_type = VAR_STRING;
15092 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015094 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015095 if (*keys == NUL)
15096 return;
15097
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015098 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015099 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015100 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015101 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015102 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015103 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015104 if (argvars[3].v_type != VAR_UNKNOWN)
15105 get_dict = get_tv_number(&argvars[3]);
15106 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015108 else
15109 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015110 if (which == NULL)
15111 return;
15112
Bram Moolenaar071d4272004-06-13 20:20:40 +000015113 mode = get_map_mode(&which, 0);
15114
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015115 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015116 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015118
15119 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015120 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015121 /* Return a string. */
15122 if (rhs != NULL)
15123 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124
Bram Moolenaarbd743252010-10-20 21:23:33 +020015125 }
15126 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15127 {
15128 /* Return a dictionary. */
15129 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15130 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15131 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015132
Bram Moolenaarbd743252010-10-20 21:23:33 +020015133 dict_add_nr_str(dict, "lhs", 0L, lhs);
15134 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15135 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15136 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15137 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15138 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15139 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015140 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015141 dict_add_nr_str(dict, "mode", 0L, mapmode);
15142
15143 vim_free(lhs);
15144 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015145 }
15146}
15147
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015148#ifdef FEAT_FLOAT
15149/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015150 * "log()" function
15151 */
15152 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015153f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015154{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015155 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015156
15157 rettv->v_type = VAR_FLOAT;
15158 if (get_float_arg(argvars, &f) == OK)
15159 rettv->vval.v_float = log(f);
15160 else
15161 rettv->vval.v_float = 0.0;
15162}
15163
15164/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015165 * "log10()" function
15166 */
15167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015168f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015169{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015170 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015171
15172 rettv->v_type = VAR_FLOAT;
15173 if (get_float_arg(argvars, &f) == OK)
15174 rettv->vval.v_float = log10(f);
15175 else
15176 rettv->vval.v_float = 0.0;
15177}
15178#endif
15179
Bram Moolenaar1dced572012-04-05 16:54:08 +020015180#ifdef FEAT_LUA
15181/*
15182 * "luaeval()" function
15183 */
15184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015185f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015186{
15187 char_u *str;
15188 char_u buf[NUMBUFLEN];
15189
15190 str = get_tv_string_buf(&argvars[0], buf);
15191 do_luaeval(str, argvars + 1, rettv);
15192}
15193#endif
15194
Bram Moolenaar071d4272004-06-13 20:20:40 +000015195/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015196 * "map()" function
15197 */
15198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015199f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015200{
15201 filter_map(argvars, rettv, TRUE);
15202}
15203
15204/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015205 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015206 */
15207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015208f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015209{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015210 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015211}
15212
15213/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015214 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015215 */
15216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015217f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015218{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015219 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015220}
15221
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015222static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015223
15224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015225find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015226{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015227 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015228 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015229 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015230 char_u *pat;
15231 regmatch_T regmatch;
15232 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015233 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015234 char_u *save_cpo;
15235 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015236 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015237 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015238 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015239 list_T *l = NULL;
15240 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015241 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015242 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015243
15244 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15245 save_cpo = p_cpo;
15246 p_cpo = (char_u *)"";
15247
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015248 rettv->vval.v_number = -1;
15249 if (type == 3)
15250 {
15251 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015252 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015253 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015254 }
15255 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015257 rettv->v_type = VAR_STRING;
15258 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015259 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015261 if (argvars[0].v_type == VAR_LIST)
15262 {
15263 if ((l = argvars[0].vval.v_list) == NULL)
15264 goto theend;
15265 li = l->lv_first;
15266 }
15267 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015268 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015269 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015270 len = (long)STRLEN(str);
15271 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015273 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15274 if (pat == NULL)
15275 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015277 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015279 int error = FALSE;
15280
15281 start = get_tv_number_chk(&argvars[2], &error);
15282 if (error)
15283 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015284 if (l != NULL)
15285 {
15286 li = list_find(l, start);
15287 if (li == NULL)
15288 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015289 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015290 }
15291 else
15292 {
15293 if (start < 0)
15294 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015295 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015296 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015297 /* When "count" argument is there ignore matches before "start",
15298 * otherwise skip part of the string. Differs when pattern is "^"
15299 * or "\<". */
15300 if (argvars[3].v_type != VAR_UNKNOWN)
15301 startcol = start;
15302 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015303 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015304 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015305 len -= start;
15306 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015307 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015308
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015309 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015310 nth = get_tv_number_chk(&argvars[3], &error);
15311 if (error)
15312 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015313 }
15314
15315 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15316 if (regmatch.regprog != NULL)
15317 {
15318 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015319
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015320 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015321 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015322 if (l != NULL)
15323 {
15324 if (li == NULL)
15325 {
15326 match = FALSE;
15327 break;
15328 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015329 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015330 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015331 if (str == NULL)
15332 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015333 }
15334
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015335 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015336
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015337 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015338 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015339 if (l == NULL && !match)
15340 break;
15341
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015342 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015343 if (l != NULL)
15344 {
15345 li = li->li_next;
15346 ++idx;
15347 }
15348 else
15349 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015350#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015351 startcol = (colnr_T)(regmatch.startp[0]
15352 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015353#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015354 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015355#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015356 if (startcol > (colnr_T)len
15357 || str + startcol <= regmatch.startp[0])
15358 {
15359 match = FALSE;
15360 break;
15361 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015362 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015363 }
15364
15365 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015366 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015367 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015368 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015369 int i;
15370
15371 /* return list with matched string and submatches */
15372 for (i = 0; i < NSUBEXP; ++i)
15373 {
15374 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015375 {
15376 if (list_append_string(rettv->vval.v_list,
15377 (char_u *)"", 0) == FAIL)
15378 break;
15379 }
15380 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015381 regmatch.startp[i],
15382 (int)(regmatch.endp[i] - regmatch.startp[i]))
15383 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015384 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015385 }
15386 }
15387 else if (type == 2)
15388 {
15389 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015390 if (l != NULL)
15391 copy_tv(&li->li_tv, rettv);
15392 else
15393 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015394 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015395 }
15396 else if (l != NULL)
15397 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015398 else
15399 {
15400 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015401 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015402 (varnumber_T)(regmatch.startp[0] - str);
15403 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015404 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015405 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015406 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015407 }
15408 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015409 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015410 }
15411
15412theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015413 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015414 p_cpo = save_cpo;
15415}
15416
15417/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015418 * "match()" function
15419 */
15420 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015421f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015422{
15423 find_some_match(argvars, rettv, 1);
15424}
15425
15426/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015427 * "matchadd()" function
15428 */
15429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015430f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015431{
15432#ifdef FEAT_SEARCH_EXTRA
15433 char_u buf[NUMBUFLEN];
15434 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15435 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15436 int prio = 10; /* default priority */
15437 int id = -1;
15438 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015439 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015440
15441 rettv->vval.v_number = -1;
15442
15443 if (grp == NULL || pat == NULL)
15444 return;
15445 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015446 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015447 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015448 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015449 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015450 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015451 if (argvars[4].v_type != VAR_UNKNOWN)
15452 {
15453 if (argvars[4].v_type != VAR_DICT)
15454 {
15455 EMSG(_(e_dictreq));
15456 return;
15457 }
15458 if (dict_find(argvars[4].vval.v_dict,
15459 (char_u *)"conceal", -1) != NULL)
15460 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15461 (char_u *)"conceal", FALSE);
15462 }
15463 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015464 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015465 if (error == TRUE)
15466 return;
15467 if (id >= 1 && id <= 3)
15468 {
15469 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15470 return;
15471 }
15472
Bram Moolenaar6561d522015-07-21 15:48:27 +020015473 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15474 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015475#endif
15476}
15477
15478/*
15479 * "matchaddpos()" function
15480 */
15481 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015482f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015483{
15484#ifdef FEAT_SEARCH_EXTRA
15485 char_u buf[NUMBUFLEN];
15486 char_u *group;
15487 int prio = 10;
15488 int id = -1;
15489 int error = FALSE;
15490 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015491 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015492
15493 rettv->vval.v_number = -1;
15494
15495 group = get_tv_string_buf_chk(&argvars[0], buf);
15496 if (group == NULL)
15497 return;
15498
15499 if (argvars[1].v_type != VAR_LIST)
15500 {
15501 EMSG2(_(e_listarg), "matchaddpos()");
15502 return;
15503 }
15504 l = argvars[1].vval.v_list;
15505 if (l == NULL)
15506 return;
15507
15508 if (argvars[2].v_type != VAR_UNKNOWN)
15509 {
15510 prio = get_tv_number_chk(&argvars[2], &error);
15511 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015512 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015513 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015514 if (argvars[4].v_type != VAR_UNKNOWN)
15515 {
15516 if (argvars[4].v_type != VAR_DICT)
15517 {
15518 EMSG(_(e_dictreq));
15519 return;
15520 }
15521 if (dict_find(argvars[4].vval.v_dict,
15522 (char_u *)"conceal", -1) != NULL)
15523 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15524 (char_u *)"conceal", FALSE);
15525 }
15526 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015527 }
15528 if (error == TRUE)
15529 return;
15530
15531 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15532 if (id == 1 || id == 2)
15533 {
15534 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15535 return;
15536 }
15537
Bram Moolenaar6561d522015-07-21 15:48:27 +020015538 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15539 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015540#endif
15541}
15542
15543/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015544 * "matcharg()" function
15545 */
15546 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015547f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015548{
15549 if (rettv_list_alloc(rettv) == OK)
15550 {
15551#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015552 int id = get_tv_number(&argvars[0]);
15553 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015554
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015555 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015556 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015557 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15558 {
15559 list_append_string(rettv->vval.v_list,
15560 syn_id2name(m->hlg_id), -1);
15561 list_append_string(rettv->vval.v_list, m->pattern, -1);
15562 }
15563 else
15564 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015565 list_append_string(rettv->vval.v_list, NULL, -1);
15566 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015567 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015568 }
15569#endif
15570 }
15571}
15572
15573/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015574 * "matchdelete()" function
15575 */
15576 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015577f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015578{
15579#ifdef FEAT_SEARCH_EXTRA
15580 rettv->vval.v_number = match_delete(curwin,
15581 (int)get_tv_number(&argvars[0]), TRUE);
15582#endif
15583}
15584
15585/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015586 * "matchend()" function
15587 */
15588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015589f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015590{
15591 find_some_match(argvars, rettv, 0);
15592}
15593
15594/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015595 * "matchlist()" function
15596 */
15597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015598f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015599{
15600 find_some_match(argvars, rettv, 3);
15601}
15602
15603/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015604 * "matchstr()" function
15605 */
15606 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015607f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015608{
15609 find_some_match(argvars, rettv, 2);
15610}
15611
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015612static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015613
15614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015615max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015616{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015617 long n = 0;
15618 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015619 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015620
15621 if (argvars[0].v_type == VAR_LIST)
15622 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015623 list_T *l;
15624 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015625
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015626 l = argvars[0].vval.v_list;
15627 if (l != NULL)
15628 {
15629 li = l->lv_first;
15630 if (li != NULL)
15631 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015632 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015633 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015634 {
15635 li = li->li_next;
15636 if (li == NULL)
15637 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015638 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015639 if (domax ? i > n : i < n)
15640 n = i;
15641 }
15642 }
15643 }
15644 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015645 else if (argvars[0].v_type == VAR_DICT)
15646 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015647 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015648 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015649 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015650 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015651
15652 d = argvars[0].vval.v_dict;
15653 if (d != NULL)
15654 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015655 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015656 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015657 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015658 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015659 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015660 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015661 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015662 if (first)
15663 {
15664 n = i;
15665 first = FALSE;
15666 }
15667 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015668 n = i;
15669 }
15670 }
15671 }
15672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015673 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015674 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015675 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015676}
15677
15678/*
15679 * "max()" function
15680 */
15681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015682f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015683{
15684 max_min(argvars, rettv, TRUE);
15685}
15686
15687/*
15688 * "min()" function
15689 */
15690 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015691f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015692{
15693 max_min(argvars, rettv, FALSE);
15694}
15695
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015696static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015697
15698/*
15699 * Create the directory in which "dir" is located, and higher levels when
15700 * needed.
15701 */
15702 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015703mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015704{
15705 char_u *p;
15706 char_u *updir;
15707 int r = FAIL;
15708
15709 /* Get end of directory name in "dir".
15710 * We're done when it's "/" or "c:/". */
15711 p = gettail_sep(dir);
15712 if (p <= get_past_head(dir))
15713 return OK;
15714
15715 /* If the directory exists we're done. Otherwise: create it.*/
15716 updir = vim_strnsave(dir, (int)(p - dir));
15717 if (updir == NULL)
15718 return FAIL;
15719 if (mch_isdir(updir))
15720 r = OK;
15721 else if (mkdir_recurse(updir, prot) == OK)
15722 r = vim_mkdir_emsg(updir, prot);
15723 vim_free(updir);
15724 return r;
15725}
15726
15727#ifdef vim_mkdir
15728/*
15729 * "mkdir()" function
15730 */
15731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015732f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015733{
15734 char_u *dir;
15735 char_u buf[NUMBUFLEN];
15736 int prot = 0755;
15737
15738 rettv->vval.v_number = FAIL;
15739 if (check_restricted() || check_secure())
15740 return;
15741
15742 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015743 if (*dir == NUL)
15744 rettv->vval.v_number = FAIL;
15745 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015746 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015747 if (*gettail(dir) == NUL)
15748 /* remove trailing slashes */
15749 *gettail_sep(dir) = NUL;
15750
15751 if (argvars[1].v_type != VAR_UNKNOWN)
15752 {
15753 if (argvars[2].v_type != VAR_UNKNOWN)
15754 prot = get_tv_number_chk(&argvars[2], NULL);
15755 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15756 mkdir_recurse(dir, prot);
15757 }
15758 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015759 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015760}
15761#endif
15762
Bram Moolenaar0d660222005-01-07 21:51:51 +000015763/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015764 * "mode()" function
15765 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015766 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015767f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015768{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015769 char_u buf[3];
15770
15771 buf[1] = NUL;
15772 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015773
Bram Moolenaar071d4272004-06-13 20:20:40 +000015774 if (VIsual_active)
15775 {
15776 if (VIsual_select)
15777 buf[0] = VIsual_mode + 's' - 'v';
15778 else
15779 buf[0] = VIsual_mode;
15780 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015781 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015782 || State == CONFIRM)
15783 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015784 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015785 if (State == ASKMORE)
15786 buf[1] = 'm';
15787 else if (State == CONFIRM)
15788 buf[1] = '?';
15789 }
15790 else if (State == EXTERNCMD)
15791 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015792 else if (State & INSERT)
15793 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015794#ifdef FEAT_VREPLACE
15795 if (State & VREPLACE_FLAG)
15796 {
15797 buf[0] = 'R';
15798 buf[1] = 'v';
15799 }
15800 else
15801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015802 if (State & REPLACE_FLAG)
15803 buf[0] = 'R';
15804 else
15805 buf[0] = 'i';
15806 }
15807 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015808 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015809 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015810 if (exmode_active)
15811 buf[1] = 'v';
15812 }
15813 else if (exmode_active)
15814 {
15815 buf[0] = 'c';
15816 buf[1] = 'e';
15817 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015818 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015819 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015821 if (finish_op)
15822 buf[1] = 'o';
15823 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015824
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015825 /* Clear out the minor mode when the argument is not a non-zero number or
15826 * non-empty string. */
15827 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015828 buf[1] = NUL;
15829
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015830 rettv->vval.v_string = vim_strsave(buf);
15831 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015832}
15833
Bram Moolenaar429fa852013-04-15 12:27:36 +020015834#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015835/*
15836 * "mzeval()" function
15837 */
15838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015839f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015840{
15841 char_u *str;
15842 char_u buf[NUMBUFLEN];
15843
15844 str = get_tv_string_buf(&argvars[0], buf);
15845 do_mzeval(str, rettv);
15846}
Bram Moolenaar75676462013-01-30 14:55:42 +010015847
15848 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015849mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015850{
15851 typval_T argvars[3];
15852
15853 argvars[0].v_type = VAR_STRING;
15854 argvars[0].vval.v_string = name;
15855 copy_tv(args, &argvars[1]);
15856 argvars[2].v_type = VAR_UNKNOWN;
15857 f_call(argvars, rettv);
15858 clear_tv(&argvars[1]);
15859}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015860#endif
15861
Bram Moolenaar071d4272004-06-13 20:20:40 +000015862/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015863 * "nextnonblank()" function
15864 */
15865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015866f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015867{
15868 linenr_T lnum;
15869
15870 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15871 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015872 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015873 {
15874 lnum = 0;
15875 break;
15876 }
15877 if (*skipwhite(ml_get(lnum)) != NUL)
15878 break;
15879 }
15880 rettv->vval.v_number = lnum;
15881}
15882
15883/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015884 * "nr2char()" function
15885 */
15886 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015887f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015888{
15889 char_u buf[NUMBUFLEN];
15890
15891#ifdef FEAT_MBYTE
15892 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015893 {
15894 int utf8 = 0;
15895
15896 if (argvars[1].v_type != VAR_UNKNOWN)
15897 utf8 = get_tv_number_chk(&argvars[1], NULL);
15898 if (utf8)
15899 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15900 else
15901 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15902 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015903 else
15904#endif
15905 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015906 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015907 buf[1] = NUL;
15908 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015909 rettv->v_type = VAR_STRING;
15910 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015911}
15912
15913/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015914 * "or(expr, expr)" function
15915 */
15916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015917f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015918{
15919 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15920 | get_tv_number_chk(&argvars[1], NULL);
15921}
15922
15923/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015924 * "pathshorten()" function
15925 */
15926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015927f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015928{
15929 char_u *p;
15930
15931 rettv->v_type = VAR_STRING;
15932 p = get_tv_string_chk(&argvars[0]);
15933 if (p == NULL)
15934 rettv->vval.v_string = NULL;
15935 else
15936 {
15937 p = vim_strsave(p);
15938 rettv->vval.v_string = p;
15939 if (p != NULL)
15940 shorten_dir(p);
15941 }
15942}
15943
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015944#ifdef FEAT_PERL
15945/*
15946 * "perleval()" function
15947 */
15948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015949f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015950{
15951 char_u *str;
15952 char_u buf[NUMBUFLEN];
15953
15954 str = get_tv_string_buf(&argvars[0], buf);
15955 do_perleval(str, rettv);
15956}
15957#endif
15958
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015959#ifdef FEAT_FLOAT
15960/*
15961 * "pow()" function
15962 */
15963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015964f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015965{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015966 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015967
15968 rettv->v_type = VAR_FLOAT;
15969 if (get_float_arg(argvars, &fx) == OK
15970 && get_float_arg(&argvars[1], &fy) == OK)
15971 rettv->vval.v_float = pow(fx, fy);
15972 else
15973 rettv->vval.v_float = 0.0;
15974}
15975#endif
15976
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015977/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015978 * "prevnonblank()" function
15979 */
15980 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015981f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015982{
15983 linenr_T lnum;
15984
15985 lnum = get_tv_lnum(argvars);
15986 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15987 lnum = 0;
15988 else
15989 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15990 --lnum;
15991 rettv->vval.v_number = lnum;
15992}
15993
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015994/* This dummy va_list is here because:
15995 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15996 * - locally in the function results in a "used before set" warning
15997 * - using va_start() to initialize it gives "function with fixed args" error */
15998static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015999
Bram Moolenaar8c711452005-01-14 21:53:12 +000016000/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016001 * "printf()" function
16002 */
16003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016004f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016005{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016006 char_u buf[NUMBUFLEN];
16007 int len;
16008 char_u *s;
16009 int saved_did_emsg = did_emsg;
16010 char *fmt;
16011
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016012 rettv->v_type = VAR_STRING;
16013 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016014
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016015 /* Get the required length, allocate the buffer and do it for real. */
16016 did_emsg = FALSE;
16017 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16018 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16019 if (!did_emsg)
16020 {
16021 s = alloc(len + 1);
16022 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016023 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016024 rettv->vval.v_string = s;
16025 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016026 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016027 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016028 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016029}
16030
16031/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016032 * "pumvisible()" function
16033 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016035f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016036{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016037#ifdef FEAT_INS_EXPAND
16038 if (pum_visible())
16039 rettv->vval.v_number = 1;
16040#endif
16041}
16042
Bram Moolenaardb913952012-06-29 12:54:53 +020016043#ifdef FEAT_PYTHON3
16044/*
16045 * "py3eval()" function
16046 */
16047 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016048f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016049{
16050 char_u *str;
16051 char_u buf[NUMBUFLEN];
16052
16053 str = get_tv_string_buf(&argvars[0], buf);
16054 do_py3eval(str, rettv);
16055}
16056#endif
16057
16058#ifdef FEAT_PYTHON
16059/*
16060 * "pyeval()" function
16061 */
16062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016063f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016064{
16065 char_u *str;
16066 char_u buf[NUMBUFLEN];
16067
16068 str = get_tv_string_buf(&argvars[0], buf);
16069 do_pyeval(str, rettv);
16070}
16071#endif
16072
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016073/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016074 * "range()" function
16075 */
16076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016077f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016078{
16079 long start;
16080 long end;
16081 long stride = 1;
16082 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016083 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016084
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016085 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016086 if (argvars[1].v_type == VAR_UNKNOWN)
16087 {
16088 end = start - 1;
16089 start = 0;
16090 }
16091 else
16092 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016093 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016094 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016095 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016096 }
16097
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016098 if (error)
16099 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016100 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016101 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016102 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016103 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016104 else
16105 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016106 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016107 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016108 if (list_append_number(rettv->vval.v_list,
16109 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016110 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016111 }
16112}
16113
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016114/*
16115 * "readfile()" function
16116 */
16117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016118f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016119{
16120 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016121 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016122 char_u *fname;
16123 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016124 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16125 int io_size = sizeof(buf);
16126 int readlen; /* size of last fread() */
16127 char_u *prev = NULL; /* previously read bytes, if any */
16128 long prevlen = 0; /* length of data in prev */
16129 long prevsize = 0; /* size of prev buffer */
16130 long maxline = MAXLNUM;
16131 long cnt = 0;
16132 char_u *p; /* position in buf */
16133 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016134
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016135 if (argvars[1].v_type != VAR_UNKNOWN)
16136 {
16137 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16138 binary = TRUE;
16139 if (argvars[2].v_type != VAR_UNKNOWN)
16140 maxline = get_tv_number(&argvars[2]);
16141 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016142
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016143 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016144 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016145
16146 /* Always open the file in binary mode, library functions have a mind of
16147 * their own about CR-LF conversion. */
16148 fname = get_tv_string(&argvars[0]);
16149 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16150 {
16151 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16152 return;
16153 }
16154
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016155 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016156 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016157 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016158
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016159 /* This for loop processes what was read, but is also entered at end
16160 * of file so that either:
16161 * - an incomplete line gets written
16162 * - a "binary" file gets an empty line at the end if it ends in a
16163 * newline. */
16164 for (p = buf, start = buf;
16165 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16166 ++p)
16167 {
16168 if (*p == '\n' || readlen <= 0)
16169 {
16170 listitem_T *li;
16171 char_u *s = NULL;
16172 long_u len = p - start;
16173
16174 /* Finished a line. Remove CRs before NL. */
16175 if (readlen > 0 && !binary)
16176 {
16177 while (len > 0 && start[len - 1] == '\r')
16178 --len;
16179 /* removal may cross back to the "prev" string */
16180 if (len == 0)
16181 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16182 --prevlen;
16183 }
16184 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016185 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016186 else
16187 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016188 /* Change "prev" buffer to be the right size. This way
16189 * the bytes are only copied once, and very long lines are
16190 * allocated only once. */
16191 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016192 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016193 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016194 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016195 prev = NULL; /* the list will own the string */
16196 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016197 }
16198 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016199 if (s == NULL)
16200 {
16201 do_outofmem_msg((long_u) prevlen + len + 1);
16202 failed = TRUE;
16203 break;
16204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016205
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016206 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016207 {
16208 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016209 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016210 break;
16211 }
16212 li->li_tv.v_type = VAR_STRING;
16213 li->li_tv.v_lock = 0;
16214 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016215 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016216
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016217 start = p + 1; /* step over newline */
16218 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016219 break;
16220 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016221 else if (*p == NUL)
16222 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016223#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016224 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16225 * when finding the BF and check the previous two bytes. */
16226 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016227 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016228 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16229 * + 1, these may be in the "prev" string. */
16230 char_u back1 = p >= buf + 1 ? p[-1]
16231 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16232 char_u back2 = p >= buf + 2 ? p[-2]
16233 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16234 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016236 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016237 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016238 char_u *dest = p - 2;
16239
16240 /* Usually a BOM is at the beginning of a file, and so at
16241 * the beginning of a line; then we can just step over it.
16242 */
16243 if (start == dest)
16244 start = p + 1;
16245 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016246 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016247 /* have to shuffle buf to close gap */
16248 int adjust_prevlen = 0;
16249
16250 if (dest < buf)
16251 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016252 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016253 dest = buf;
16254 }
16255 if (readlen > p - buf + 1)
16256 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16257 readlen -= 3 - adjust_prevlen;
16258 prevlen -= adjust_prevlen;
16259 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016260 }
16261 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016262 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016263#endif
16264 } /* for */
16265
16266 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16267 break;
16268 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016269 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016270 /* There's part of a line in buf, store it in "prev". */
16271 if (p - start + prevlen >= prevsize)
16272 {
16273 /* need bigger "prev" buffer */
16274 char_u *newprev;
16275
16276 /* A common use case is ordinary text files and "prev" gets a
16277 * fragment of a line, so the first allocation is made
16278 * small, to avoid repeatedly 'allocing' large and
16279 * 'reallocing' small. */
16280 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016281 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016282 else
16283 {
16284 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016285 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016286 prevsize = grow50pc > growmin ? grow50pc : growmin;
16287 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016288 newprev = prev == NULL ? alloc(prevsize)
16289 : vim_realloc(prev, prevsize);
16290 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016291 {
16292 do_outofmem_msg((long_u)prevsize);
16293 failed = TRUE;
16294 break;
16295 }
16296 prev = newprev;
16297 }
16298 /* Add the line part to end of "prev". */
16299 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016300 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016301 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016302 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016303
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016304 /*
16305 * For a negative line count use only the lines at the end of the file,
16306 * free the rest.
16307 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016308 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016309 while (cnt > -maxline)
16310 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016311 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016312 --cnt;
16313 }
16314
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016315 if (failed)
16316 {
16317 list_free(rettv->vval.v_list, TRUE);
16318 /* readfile doc says an empty list is returned on error */
16319 rettv->vval.v_list = list_alloc();
16320 }
16321
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016322 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016323 fclose(fd);
16324}
16325
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016326#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016327static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016328
16329/*
16330 * Convert a List to proftime_T.
16331 * Return FAIL when there is something wrong.
16332 */
16333 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016334list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016335{
16336 long n1, n2;
16337 int error = FALSE;
16338
16339 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16340 || arg->vval.v_list->lv_len != 2)
16341 return FAIL;
16342 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16343 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16344# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016345 tm->HighPart = n1;
16346 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016347# else
16348 tm->tv_sec = n1;
16349 tm->tv_usec = n2;
16350# endif
16351 return error ? FAIL : OK;
16352}
16353#endif /* FEAT_RELTIME */
16354
16355/*
16356 * "reltime()" function
16357 */
16358 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016359f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016360{
16361#ifdef FEAT_RELTIME
16362 proftime_T res;
16363 proftime_T start;
16364
16365 if (argvars[0].v_type == VAR_UNKNOWN)
16366 {
16367 /* No arguments: get current time. */
16368 profile_start(&res);
16369 }
16370 else if (argvars[1].v_type == VAR_UNKNOWN)
16371 {
16372 if (list2proftime(&argvars[0], &res) == FAIL)
16373 return;
16374 profile_end(&res);
16375 }
16376 else
16377 {
16378 /* Two arguments: compute the difference. */
16379 if (list2proftime(&argvars[0], &start) == FAIL
16380 || list2proftime(&argvars[1], &res) == FAIL)
16381 return;
16382 profile_sub(&res, &start);
16383 }
16384
16385 if (rettv_list_alloc(rettv) == OK)
16386 {
16387 long n1, n2;
16388
16389# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016390 n1 = res.HighPart;
16391 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016392# else
16393 n1 = res.tv_sec;
16394 n2 = res.tv_usec;
16395# endif
16396 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16397 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16398 }
16399#endif
16400}
16401
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016402#ifdef FEAT_FLOAT
16403/*
16404 * "reltimefloat()" function
16405 */
16406 static void
16407f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16408{
16409# ifdef FEAT_RELTIME
16410 proftime_T tm;
16411# endif
16412
16413 rettv->v_type = VAR_FLOAT;
16414 rettv->vval.v_float = 0;
16415# ifdef FEAT_RELTIME
16416 if (list2proftime(&argvars[0], &tm) == OK)
16417 rettv->vval.v_float = profile_float(&tm);
16418# endif
16419}
16420#endif
16421
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016422/*
16423 * "reltimestr()" function
16424 */
16425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016426f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016427{
16428#ifdef FEAT_RELTIME
16429 proftime_T tm;
16430#endif
16431
16432 rettv->v_type = VAR_STRING;
16433 rettv->vval.v_string = NULL;
16434#ifdef FEAT_RELTIME
16435 if (list2proftime(&argvars[0], &tm) == OK)
16436 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16437#endif
16438}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016439
Bram Moolenaar0d660222005-01-07 21:51:51 +000016440#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016441static void make_connection(void);
16442static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016443
16444 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016445make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016446{
16447 if (X_DISPLAY == NULL
16448# ifdef FEAT_GUI
16449 && !gui.in_use
16450# endif
16451 )
16452 {
16453 x_force_connect = TRUE;
16454 setup_term_clip();
16455 x_force_connect = FALSE;
16456 }
16457}
16458
16459 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016460check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016461{
16462 make_connection();
16463 if (X_DISPLAY == NULL)
16464 {
16465 EMSG(_("E240: No connection to Vim server"));
16466 return FAIL;
16467 }
16468 return OK;
16469}
16470#endif
16471
16472#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016473static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016474
16475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016476remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016477{
16478 char_u *server_name;
16479 char_u *keys;
16480 char_u *r = NULL;
16481 char_u buf[NUMBUFLEN];
16482# ifdef WIN32
16483 HWND w;
16484# else
16485 Window w;
16486# endif
16487
16488 if (check_restricted() || check_secure())
16489 return;
16490
16491# ifdef FEAT_X11
16492 if (check_connection() == FAIL)
16493 return;
16494# endif
16495
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016496 server_name = get_tv_string_chk(&argvars[0]);
16497 if (server_name == NULL)
16498 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016499 keys = get_tv_string_buf(&argvars[1], buf);
16500# ifdef WIN32
16501 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16502# else
16503 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16504 < 0)
16505# endif
16506 {
16507 if (r != NULL)
16508 EMSG(r); /* sending worked but evaluation failed */
16509 else
16510 EMSG2(_("E241: Unable to send to %s"), server_name);
16511 return;
16512 }
16513
16514 rettv->vval.v_string = r;
16515
16516 if (argvars[2].v_type != VAR_UNKNOWN)
16517 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016518 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016519 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016520 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016522 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016523 v.di_tv.v_type = VAR_STRING;
16524 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016525 idvar = get_tv_string_chk(&argvars[2]);
16526 if (idvar != NULL)
16527 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016528 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016529 }
16530}
16531#endif
16532
16533/*
16534 * "remote_expr()" function
16535 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016537f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016538{
16539 rettv->v_type = VAR_STRING;
16540 rettv->vval.v_string = NULL;
16541#ifdef FEAT_CLIENTSERVER
16542 remote_common(argvars, rettv, TRUE);
16543#endif
16544}
16545
16546/*
16547 * "remote_foreground()" function
16548 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016550f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016551{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552#ifdef FEAT_CLIENTSERVER
16553# ifdef WIN32
16554 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016555 {
16556 char_u *server_name = get_tv_string_chk(&argvars[0]);
16557
16558 if (server_name != NULL)
16559 serverForeground(server_name);
16560 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016561# else
16562 /* Send a foreground() expression to the server. */
16563 argvars[1].v_type = VAR_STRING;
16564 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16565 argvars[2].v_type = VAR_UNKNOWN;
16566 remote_common(argvars, rettv, TRUE);
16567 vim_free(argvars[1].vval.v_string);
16568# endif
16569#endif
16570}
16571
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016573f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016574{
16575#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016576 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577 char_u *s = NULL;
16578# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016579 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016581 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016582
16583 if (check_restricted() || check_secure())
16584 {
16585 rettv->vval.v_number = -1;
16586 return;
16587 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016588 serverid = get_tv_string_chk(&argvars[0]);
16589 if (serverid == NULL)
16590 {
16591 rettv->vval.v_number = -1;
16592 return; /* type error; errmsg already given */
16593 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016594# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016595 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016596 if (n == 0)
16597 rettv->vval.v_number = -1;
16598 else
16599 {
16600 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16601 rettv->vval.v_number = (s != NULL);
16602 }
16603# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604 if (check_connection() == FAIL)
16605 return;
16606
16607 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016608 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016609# endif
16610
16611 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16612 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016613 char_u *retvar;
16614
Bram Moolenaar33570922005-01-25 22:26:29 +000016615 v.di_tv.v_type = VAR_STRING;
16616 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016617 retvar = get_tv_string_chk(&argvars[1]);
16618 if (retvar != NULL)
16619 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016620 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 }
16622#else
16623 rettv->vval.v_number = -1;
16624#endif
16625}
16626
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016628f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629{
16630 char_u *r = NULL;
16631
16632#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016633 char_u *serverid = get_tv_string_chk(&argvars[0]);
16634
16635 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016636 {
16637# ifdef WIN32
16638 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016639 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016640
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016641 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016642 if (n != 0)
16643 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16644 if (r == NULL)
16645# else
16646 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016647 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016648# endif
16649 EMSG(_("E277: Unable to read a server reply"));
16650 }
16651#endif
16652 rettv->v_type = VAR_STRING;
16653 rettv->vval.v_string = r;
16654}
16655
16656/*
16657 * "remote_send()" function
16658 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016660f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016661{
16662 rettv->v_type = VAR_STRING;
16663 rettv->vval.v_string = NULL;
16664#ifdef FEAT_CLIENTSERVER
16665 remote_common(argvars, rettv, FALSE);
16666#endif
16667}
16668
16669/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016670 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016671 */
16672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016673f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016674{
Bram Moolenaar33570922005-01-25 22:26:29 +000016675 list_T *l;
16676 listitem_T *item, *item2;
16677 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016678 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016679 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016680 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016681 dict_T *d;
16682 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016683 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016684
Bram Moolenaar8c711452005-01-14 21:53:12 +000016685 if (argvars[0].v_type == VAR_DICT)
16686 {
16687 if (argvars[2].v_type != VAR_UNKNOWN)
16688 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016689 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016690 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016691 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016692 key = get_tv_string_chk(&argvars[1]);
16693 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016694 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016695 di = dict_find(d, key, -1);
16696 if (di == NULL)
16697 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016698 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16699 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016700 {
16701 *rettv = di->di_tv;
16702 init_tv(&di->di_tv);
16703 dictitem_remove(d, di);
16704 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016705 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016706 }
16707 }
16708 else if (argvars[0].v_type != VAR_LIST)
16709 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016710 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016711 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016712 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016713 int error = FALSE;
16714
16715 idx = get_tv_number_chk(&argvars[1], &error);
16716 if (error)
16717 ; /* type error: do nothing, errmsg already given */
16718 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016719 EMSGN(_(e_listidx), idx);
16720 else
16721 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016722 if (argvars[2].v_type == VAR_UNKNOWN)
16723 {
16724 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016725 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016726 *rettv = item->li_tv;
16727 vim_free(item);
16728 }
16729 else
16730 {
16731 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016732 end = get_tv_number_chk(&argvars[2], &error);
16733 if (error)
16734 ; /* type error: do nothing */
16735 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016736 EMSGN(_(e_listidx), end);
16737 else
16738 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016739 int cnt = 0;
16740
16741 for (li = item; li != NULL; li = li->li_next)
16742 {
16743 ++cnt;
16744 if (li == item2)
16745 break;
16746 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016747 if (li == NULL) /* didn't find "item2" after "item" */
16748 EMSG(_(e_invrange));
16749 else
16750 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016751 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016752 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016753 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016754 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016755 l->lv_first = item;
16756 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016757 item->li_prev = NULL;
16758 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016759 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016760 }
16761 }
16762 }
16763 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016764 }
16765 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016766}
16767
16768/*
16769 * "rename({from}, {to})" function
16770 */
16771 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016772f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016773{
16774 char_u buf[NUMBUFLEN];
16775
16776 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016777 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016779 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16780 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781}
16782
16783/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016784 * "repeat()" function
16785 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016786 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016787f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016788{
16789 char_u *p;
16790 int n;
16791 int slen;
16792 int len;
16793 char_u *r;
16794 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016795
16796 n = get_tv_number(&argvars[1]);
16797 if (argvars[0].v_type == VAR_LIST)
16798 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016799 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016800 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 if (list_extend(rettv->vval.v_list,
16802 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016803 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016804 }
16805 else
16806 {
16807 p = get_tv_string(&argvars[0]);
16808 rettv->v_type = VAR_STRING;
16809 rettv->vval.v_string = NULL;
16810
16811 slen = (int)STRLEN(p);
16812 len = slen * n;
16813 if (len <= 0)
16814 return;
16815
16816 r = alloc(len + 1);
16817 if (r != NULL)
16818 {
16819 for (i = 0; i < n; i++)
16820 mch_memmove(r + i * slen, p, (size_t)slen);
16821 r[len] = NUL;
16822 }
16823
16824 rettv->vval.v_string = r;
16825 }
16826}
16827
16828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016829 * "resolve()" function
16830 */
16831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016832f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016833{
16834 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016835#ifdef HAVE_READLINK
16836 char_u *buf = NULL;
16837#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016838
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016839 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016840#ifdef FEAT_SHORTCUT
16841 {
16842 char_u *v = NULL;
16843
16844 v = mch_resolve_shortcut(p);
16845 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016846 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016847 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016848 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016849 }
16850#else
16851# ifdef HAVE_READLINK
16852 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016853 char_u *cpy;
16854 int len;
16855 char_u *remain = NULL;
16856 char_u *q;
16857 int is_relative_to_current = FALSE;
16858 int has_trailing_pathsep = FALSE;
16859 int limit = 100;
16860
16861 p = vim_strsave(p);
16862
16863 if (p[0] == '.' && (vim_ispathsep(p[1])
16864 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16865 is_relative_to_current = TRUE;
16866
16867 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016868 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016869 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016870 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016871 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016873
16874 q = getnextcomp(p);
16875 if (*q != NUL)
16876 {
16877 /* Separate the first path component in "p", and keep the
16878 * remainder (beginning with the path separator). */
16879 remain = vim_strsave(q - 1);
16880 q[-1] = NUL;
16881 }
16882
Bram Moolenaard9462e32011-04-11 21:35:11 +020016883 buf = alloc(MAXPATHL + 1);
16884 if (buf == NULL)
16885 goto fail;
16886
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887 for (;;)
16888 {
16889 for (;;)
16890 {
16891 len = readlink((char *)p, (char *)buf, MAXPATHL);
16892 if (len <= 0)
16893 break;
16894 buf[len] = NUL;
16895
16896 if (limit-- == 0)
16897 {
16898 vim_free(p);
16899 vim_free(remain);
16900 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016901 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 goto fail;
16903 }
16904
16905 /* Ensure that the result will have a trailing path separator
16906 * if the argument has one. */
16907 if (remain == NULL && has_trailing_pathsep)
16908 add_pathsep(buf);
16909
16910 /* Separate the first path component in the link value and
16911 * concatenate the remainders. */
16912 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16913 if (*q != NUL)
16914 {
16915 if (remain == NULL)
16916 remain = vim_strsave(q - 1);
16917 else
16918 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016919 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920 if (cpy != NULL)
16921 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922 vim_free(remain);
16923 remain = cpy;
16924 }
16925 }
16926 q[-1] = NUL;
16927 }
16928
16929 q = gettail(p);
16930 if (q > p && *q == NUL)
16931 {
16932 /* Ignore trailing path separator. */
16933 q[-1] = NUL;
16934 q = gettail(p);
16935 }
16936 if (q > p && !mch_isFullName(buf))
16937 {
16938 /* symlink is relative to directory of argument */
16939 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16940 if (cpy != NULL)
16941 {
16942 STRCPY(cpy, p);
16943 STRCPY(gettail(cpy), buf);
16944 vim_free(p);
16945 p = cpy;
16946 }
16947 }
16948 else
16949 {
16950 vim_free(p);
16951 p = vim_strsave(buf);
16952 }
16953 }
16954
16955 if (remain == NULL)
16956 break;
16957
16958 /* Append the first path component of "remain" to "p". */
16959 q = getnextcomp(remain + 1);
16960 len = q - remain - (*q != NUL);
16961 cpy = vim_strnsave(p, STRLEN(p) + len);
16962 if (cpy != NULL)
16963 {
16964 STRNCAT(cpy, remain, len);
16965 vim_free(p);
16966 p = cpy;
16967 }
16968 /* Shorten "remain". */
16969 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016970 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 else
16972 {
16973 vim_free(remain);
16974 remain = NULL;
16975 }
16976 }
16977
16978 /* If the result is a relative path name, make it explicitly relative to
16979 * the current directory if and only if the argument had this form. */
16980 if (!vim_ispathsep(*p))
16981 {
16982 if (is_relative_to_current
16983 && *p != NUL
16984 && !(p[0] == '.'
16985 && (p[1] == NUL
16986 || vim_ispathsep(p[1])
16987 || (p[1] == '.'
16988 && (p[2] == NUL
16989 || vim_ispathsep(p[2]))))))
16990 {
16991 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016992 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016993 if (cpy != NULL)
16994 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016995 vim_free(p);
16996 p = cpy;
16997 }
16998 }
16999 else if (!is_relative_to_current)
17000 {
17001 /* Strip leading "./". */
17002 q = p;
17003 while (q[0] == '.' && vim_ispathsep(q[1]))
17004 q += 2;
17005 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017006 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007 }
17008 }
17009
17010 /* Ensure that the result will have no trailing path separator
17011 * if the argument had none. But keep "/" or "//". */
17012 if (!has_trailing_pathsep)
17013 {
17014 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017015 if (after_pathsep(p, q))
17016 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017017 }
17018
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017019 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 }
17021# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017022 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023# endif
17024#endif
17025
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017026 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017027
17028#ifdef HAVE_READLINK
17029fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017030 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017031#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017032 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017033}
17034
17035/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017036 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017037 */
17038 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017039f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040{
Bram Moolenaar33570922005-01-25 22:26:29 +000017041 list_T *l;
17042 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017043
Bram Moolenaar0d660222005-01-07 21:51:51 +000017044 if (argvars[0].v_type != VAR_LIST)
17045 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017046 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017047 && !tv_check_lock(l->lv_lock,
17048 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017049 {
17050 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017051 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017052 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017053 while (li != NULL)
17054 {
17055 ni = li->li_prev;
17056 list_append(l, li);
17057 li = ni;
17058 }
17059 rettv->vval.v_list = l;
17060 rettv->v_type = VAR_LIST;
17061 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017062 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017063 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064}
17065
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017066#define SP_NOMOVE 0x01 /* don't move cursor */
17067#define SP_REPEAT 0x02 /* repeat to find outer pair */
17068#define SP_RETCOUNT 0x04 /* return matchcount */
17069#define SP_SETPCMARK 0x08 /* set previous context mark */
17070#define SP_START 0x10 /* accept match at start position */
17071#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17072#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017073#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017074
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017075static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017076
17077/*
17078 * Get flags for a search function.
17079 * Possibly sets "p_ws".
17080 * Returns BACKWARD, FORWARD or zero (for an error).
17081 */
17082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017083get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017084{
17085 int dir = FORWARD;
17086 char_u *flags;
17087 char_u nbuf[NUMBUFLEN];
17088 int mask;
17089
17090 if (varp->v_type != VAR_UNKNOWN)
17091 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017092 flags = get_tv_string_buf_chk(varp, nbuf);
17093 if (flags == NULL)
17094 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017095 while (*flags != NUL)
17096 {
17097 switch (*flags)
17098 {
17099 case 'b': dir = BACKWARD; break;
17100 case 'w': p_ws = TRUE; break;
17101 case 'W': p_ws = FALSE; break;
17102 default: mask = 0;
17103 if (flagsp != NULL)
17104 switch (*flags)
17105 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017106 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017107 case 'e': mask = SP_END; break;
17108 case 'm': mask = SP_RETCOUNT; break;
17109 case 'n': mask = SP_NOMOVE; break;
17110 case 'p': mask = SP_SUBPAT; break;
17111 case 'r': mask = SP_REPEAT; break;
17112 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017113 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017114 }
17115 if (mask == 0)
17116 {
17117 EMSG2(_(e_invarg2), flags);
17118 dir = 0;
17119 }
17120 else
17121 *flagsp |= mask;
17122 }
17123 if (dir == 0)
17124 break;
17125 ++flags;
17126 }
17127 }
17128 return dir;
17129}
17130
Bram Moolenaar071d4272004-06-13 20:20:40 +000017131/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017132 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017133 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017134 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017135search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017137 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017138 char_u *pat;
17139 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017140 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017141 int save_p_ws = p_ws;
17142 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017143 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017144 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017145 proftime_T tm;
17146#ifdef FEAT_RELTIME
17147 long time_limit = 0;
17148#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017149 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017150 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017152 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017153 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017154 if (dir == 0)
17155 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017156 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017157 if (flags & SP_START)
17158 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017159 if (flags & SP_END)
17160 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017161 if (flags & SP_COLUMN)
17162 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017163
Bram Moolenaar76929292008-01-06 19:07:36 +000017164 /* Optional arguments: line number to stop searching and timeout. */
17165 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017166 {
17167 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17168 if (lnum_stop < 0)
17169 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017170#ifdef FEAT_RELTIME
17171 if (argvars[3].v_type != VAR_UNKNOWN)
17172 {
17173 time_limit = get_tv_number_chk(&argvars[3], NULL);
17174 if (time_limit < 0)
17175 goto theend;
17176 }
17177#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017178 }
17179
Bram Moolenaar76929292008-01-06 19:07:36 +000017180#ifdef FEAT_RELTIME
17181 /* Set the time limit, if there is one. */
17182 profile_setlimit(time_limit, &tm);
17183#endif
17184
Bram Moolenaar231334e2005-07-25 20:46:57 +000017185 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017186 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017187 * Check to make sure only those flags are set.
17188 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17189 * flags cannot be set. Check for that condition also.
17190 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017191 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017192 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017193 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017194 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017195 goto theend;
17196 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017197
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017198 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017199 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017200 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017201 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017202 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017203 if (flags & SP_SUBPAT)
17204 retval = subpatnum;
17205 else
17206 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017207 if (flags & SP_SETPCMARK)
17208 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017209 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017210 if (match_pos != NULL)
17211 {
17212 /* Store the match cursor position */
17213 match_pos->lnum = pos.lnum;
17214 match_pos->col = pos.col + 1;
17215 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017216 /* "/$" will put the cursor after the end of the line, may need to
17217 * correct that here */
17218 check_cursor();
17219 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017220
17221 /* If 'n' flag is used: restore cursor position. */
17222 if (flags & SP_NOMOVE)
17223 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017224 else
17225 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017226theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017227 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017228
17229 return retval;
17230}
17231
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017232#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017233
17234/*
17235 * round() is not in C90, use ceil() or floor() instead.
17236 */
17237 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017238vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017239{
17240 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17241}
17242
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017243/*
17244 * "round({float})" function
17245 */
17246 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017247f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017248{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017249 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017250
17251 rettv->v_type = VAR_FLOAT;
17252 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017253 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017254 else
17255 rettv->vval.v_float = 0.0;
17256}
17257#endif
17258
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017259/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017260 * "screenattr()" function
17261 */
17262 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017263f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017264{
17265 int row;
17266 int col;
17267 int c;
17268
17269 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17270 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17271 if (row < 0 || row >= screen_Rows
17272 || col < 0 || col >= screen_Columns)
17273 c = -1;
17274 else
17275 c = ScreenAttrs[LineOffset[row] + col];
17276 rettv->vval.v_number = c;
17277}
17278
17279/*
17280 * "screenchar()" function
17281 */
17282 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017283f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017284{
17285 int row;
17286 int col;
17287 int off;
17288 int c;
17289
17290 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17291 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17292 if (row < 0 || row >= screen_Rows
17293 || col < 0 || col >= screen_Columns)
17294 c = -1;
17295 else
17296 {
17297 off = LineOffset[row] + col;
17298#ifdef FEAT_MBYTE
17299 if (enc_utf8 && ScreenLinesUC[off] != 0)
17300 c = ScreenLinesUC[off];
17301 else
17302#endif
17303 c = ScreenLines[off];
17304 }
17305 rettv->vval.v_number = c;
17306}
17307
17308/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017309 * "screencol()" function
17310 *
17311 * First column is 1 to be consistent with virtcol().
17312 */
17313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017314f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017315{
17316 rettv->vval.v_number = screen_screencol() + 1;
17317}
17318
17319/*
17320 * "screenrow()" function
17321 */
17322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017323f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017324{
17325 rettv->vval.v_number = screen_screenrow() + 1;
17326}
17327
17328/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017329 * "search()" function
17330 */
17331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017332f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017333{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017334 int flags = 0;
17335
17336 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017337}
17338
Bram Moolenaar071d4272004-06-13 20:20:40 +000017339/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017340 * "searchdecl()" function
17341 */
17342 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017343f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017344{
17345 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017346 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017347 int error = FALSE;
17348 char_u *name;
17349
17350 rettv->vval.v_number = 1; /* default: FAIL */
17351
17352 name = get_tv_string_chk(&argvars[0]);
17353 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017354 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017355 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017356 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17357 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17358 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017359 if (!error && name != NULL)
17360 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017361 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017362}
17363
17364/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017365 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017366 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017367 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017368searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017369{
17370 char_u *spat, *mpat, *epat;
17371 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017372 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017373 int dir;
17374 int flags = 0;
17375 char_u nbuf1[NUMBUFLEN];
17376 char_u nbuf2[NUMBUFLEN];
17377 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017378 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017379 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017380 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017381
Bram Moolenaar071d4272004-06-13 20:20:40 +000017382 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017383 spat = get_tv_string_chk(&argvars[0]);
17384 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17385 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17386 if (spat == NULL || mpat == NULL || epat == NULL)
17387 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388
Bram Moolenaar071d4272004-06-13 20:20:40 +000017389 /* Handle the optional fourth argument: flags */
17390 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017391 if (dir == 0)
17392 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017393
17394 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017395 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17396 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017397 if ((flags & (SP_END | SP_SUBPAT)) != 0
17398 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017399 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017400 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017401 goto theend;
17402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017403
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017404 /* Using 'r' implies 'W', otherwise it doesn't work. */
17405 if (flags & SP_REPEAT)
17406 p_ws = FALSE;
17407
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017408 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017409 if (argvars[3].v_type == VAR_UNKNOWN
17410 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017411 skip = (char_u *)"";
17412 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017413 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017414 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017415 if (argvars[5].v_type != VAR_UNKNOWN)
17416 {
17417 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17418 if (lnum_stop < 0)
17419 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017420#ifdef FEAT_RELTIME
17421 if (argvars[6].v_type != VAR_UNKNOWN)
17422 {
17423 time_limit = get_tv_number_chk(&argvars[6], NULL);
17424 if (time_limit < 0)
17425 goto theend;
17426 }
17427#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017428 }
17429 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 if (skip == NULL)
17431 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017432
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017433 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017434 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017435
17436theend:
17437 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017438
17439 return retval;
17440}
17441
17442/*
17443 * "searchpair()" function
17444 */
17445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017446f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017447{
17448 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17449}
17450
17451/*
17452 * "searchpairpos()" function
17453 */
17454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017455f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017456{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017457 pos_T match_pos;
17458 int lnum = 0;
17459 int col = 0;
17460
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017461 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017462 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017463
17464 if (searchpair_cmn(argvars, &match_pos) > 0)
17465 {
17466 lnum = match_pos.lnum;
17467 col = match_pos.col;
17468 }
17469
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017470 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17471 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017472}
17473
17474/*
17475 * Search for a start/middle/end thing.
17476 * Used by searchpair(), see its documentation for the details.
17477 * Returns 0 or -1 for no match,
17478 */
17479 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017480do_searchpair(
17481 char_u *spat, /* start pattern */
17482 char_u *mpat, /* middle pattern */
17483 char_u *epat, /* end pattern */
17484 int dir, /* BACKWARD or FORWARD */
17485 char_u *skip, /* skip expression */
17486 int flags, /* SP_SETPCMARK and other SP_ values */
17487 pos_T *match_pos,
17488 linenr_T lnum_stop, /* stop at this line if not zero */
17489 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017490{
17491 char_u *save_cpo;
17492 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17493 long retval = 0;
17494 pos_T pos;
17495 pos_T firstpos;
17496 pos_T foundpos;
17497 pos_T save_cursor;
17498 pos_T save_pos;
17499 int n;
17500 int r;
17501 int nest = 1;
17502 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017503 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017504 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017505
17506 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17507 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017508 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017509
Bram Moolenaar76929292008-01-06 19:07:36 +000017510#ifdef FEAT_RELTIME
17511 /* Set the time limit, if there is one. */
17512 profile_setlimit(time_limit, &tm);
17513#endif
17514
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017515 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17516 * start/middle/end (pat3, for the top pair). */
17517 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17518 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17519 if (pat2 == NULL || pat3 == NULL)
17520 goto theend;
17521 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17522 if (*mpat == NUL)
17523 STRCPY(pat3, pat2);
17524 else
17525 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17526 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017527 if (flags & SP_START)
17528 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017529
Bram Moolenaar071d4272004-06-13 20:20:40 +000017530 save_cursor = curwin->w_cursor;
17531 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017532 clearpos(&firstpos);
17533 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017534 pat = pat3;
17535 for (;;)
17536 {
17537 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017538 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017539 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17540 /* didn't find it or found the first match again: FAIL */
17541 break;
17542
17543 if (firstpos.lnum == 0)
17544 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017545 if (equalpos(pos, foundpos))
17546 {
17547 /* Found the same position again. Can happen with a pattern that
17548 * has "\zs" at the end and searching backwards. Advance one
17549 * character and try again. */
17550 if (dir == BACKWARD)
17551 decl(&pos);
17552 else
17553 incl(&pos);
17554 }
17555 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017556
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017557 /* clear the start flag to avoid getting stuck here */
17558 options &= ~SEARCH_START;
17559
Bram Moolenaar071d4272004-06-13 20:20:40 +000017560 /* If the skip pattern matches, ignore this match. */
17561 if (*skip != NUL)
17562 {
17563 save_pos = curwin->w_cursor;
17564 curwin->w_cursor = pos;
17565 r = eval_to_bool(skip, &err, NULL, FALSE);
17566 curwin->w_cursor = save_pos;
17567 if (err)
17568 {
17569 /* Evaluating {skip} caused an error, break here. */
17570 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017571 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017572 break;
17573 }
17574 if (r)
17575 continue;
17576 }
17577
17578 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17579 {
17580 /* Found end when searching backwards or start when searching
17581 * forward: nested pair. */
17582 ++nest;
17583 pat = pat2; /* nested, don't search for middle */
17584 }
17585 else
17586 {
17587 /* Found end when searching forward or start when searching
17588 * backward: end of (nested) pair; or found middle in outer pair. */
17589 if (--nest == 1)
17590 pat = pat3; /* outer level, search for middle */
17591 }
17592
17593 if (nest == 0)
17594 {
17595 /* Found the match: return matchcount or line number. */
17596 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017597 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017598 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017599 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017600 if (flags & SP_SETPCMARK)
17601 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017602 curwin->w_cursor = pos;
17603 if (!(flags & SP_REPEAT))
17604 break;
17605 nest = 1; /* search for next unmatched */
17606 }
17607 }
17608
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017609 if (match_pos != NULL)
17610 {
17611 /* Store the match cursor position */
17612 match_pos->lnum = curwin->w_cursor.lnum;
17613 match_pos->col = curwin->w_cursor.col + 1;
17614 }
17615
Bram Moolenaar071d4272004-06-13 20:20:40 +000017616 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017617 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017618 curwin->w_cursor = save_cursor;
17619
17620theend:
17621 vim_free(pat2);
17622 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017623 if (p_cpo == empty_option)
17624 p_cpo = save_cpo;
17625 else
17626 /* Darn, evaluating the {skip} expression changed the value. */
17627 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017628
17629 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017630}
17631
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017632/*
17633 * "searchpos()" function
17634 */
17635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017636f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017637{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017638 pos_T match_pos;
17639 int lnum = 0;
17640 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017641 int n;
17642 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017643
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017644 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017645 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017646
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017647 n = search_cmn(argvars, &match_pos, &flags);
17648 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017649 {
17650 lnum = match_pos.lnum;
17651 col = match_pos.col;
17652 }
17653
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017654 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17655 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017656 if (flags & SP_SUBPAT)
17657 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017658}
17659
Bram Moolenaar0d660222005-01-07 21:51:51 +000017660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017661f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017662{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017663#ifdef FEAT_CLIENTSERVER
17664 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017665 char_u *server = get_tv_string_chk(&argvars[0]);
17666 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667
Bram Moolenaar0d660222005-01-07 21:51:51 +000017668 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017669 if (server == NULL || reply == NULL)
17670 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017671 if (check_restricted() || check_secure())
17672 return;
17673# ifdef FEAT_X11
17674 if (check_connection() == FAIL)
17675 return;
17676# endif
17677
17678 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017680 EMSG(_("E258: Unable to send to client"));
17681 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017682 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017683 rettv->vval.v_number = 0;
17684#else
17685 rettv->vval.v_number = -1;
17686#endif
17687}
17688
Bram Moolenaar0d660222005-01-07 21:51:51 +000017689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017690f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017691{
17692 char_u *r = NULL;
17693
17694#ifdef FEAT_CLIENTSERVER
17695# ifdef WIN32
17696 r = serverGetVimNames();
17697# else
17698 make_connection();
17699 if (X_DISPLAY != NULL)
17700 r = serverGetVimNames(X_DISPLAY);
17701# endif
17702#endif
17703 rettv->v_type = VAR_STRING;
17704 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705}
17706
17707/*
17708 * "setbufvar()" function
17709 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017711f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017712{
17713 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017715 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017716 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017717 char_u nbuf[NUMBUFLEN];
17718
17719 if (check_restricted() || check_secure())
17720 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017721 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17722 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017723 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017724 varp = &argvars[2];
17725
17726 if (buf != NULL && varname != NULL && varp != NULL)
17727 {
17728 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017730
17731 if (*varname == '&')
17732 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017733 long numval;
17734 char_u *strval;
17735 int error = FALSE;
17736
Bram Moolenaar071d4272004-06-13 20:20:40 +000017737 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017738 numval = get_tv_number_chk(varp, &error);
17739 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017740 if (!error && strval != NULL)
17741 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017742 }
17743 else
17744 {
17745 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17746 if (bufvarname != NULL)
17747 {
17748 STRCPY(bufvarname, "b:");
17749 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017750 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751 vim_free(bufvarname);
17752 }
17753 }
17754
17755 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017758}
17759
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017761f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017762{
17763 dict_T *d;
17764 dictitem_T *di;
17765 char_u *csearch;
17766
17767 if (argvars[0].v_type != VAR_DICT)
17768 {
17769 EMSG(_(e_dictreq));
17770 return;
17771 }
17772
17773 if ((d = argvars[0].vval.v_dict) != NULL)
17774 {
17775 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17776 if (csearch != NULL)
17777 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017778#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017779 if (enc_utf8)
17780 {
17781 int pcc[MAX_MCO];
17782 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017783
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017784 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17785 }
17786 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017787#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017788 set_last_csearch(PTR2CHAR(csearch),
17789 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017790 }
17791
17792 di = dict_find(d, (char_u *)"forward", -1);
17793 if (di != NULL)
17794 set_csearch_direction(get_tv_number(&di->di_tv)
17795 ? FORWARD : BACKWARD);
17796
17797 di = dict_find(d, (char_u *)"until", -1);
17798 if (di != NULL)
17799 set_csearch_until(!!get_tv_number(&di->di_tv));
17800 }
17801}
17802
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803/*
17804 * "setcmdpos()" function
17805 */
17806 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017807f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017809 int pos = (int)get_tv_number(&argvars[0]) - 1;
17810
17811 if (pos >= 0)
17812 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813}
17814
17815/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017816 * "setfperm({fname}, {mode})" function
17817 */
17818 static void
17819f_setfperm(typval_T *argvars, typval_T *rettv)
17820{
17821 char_u *fname;
17822 char_u modebuf[NUMBUFLEN];
17823 char_u *mode_str;
17824 int i;
17825 int mask;
17826 int mode = 0;
17827
17828 rettv->vval.v_number = 0;
17829 fname = get_tv_string_chk(&argvars[0]);
17830 if (fname == NULL)
17831 return;
17832 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17833 if (mode_str == NULL)
17834 return;
17835 if (STRLEN(mode_str) != 9)
17836 {
17837 EMSG2(_(e_invarg2), mode_str);
17838 return;
17839 }
17840
17841 mask = 1;
17842 for (i = 8; i >= 0; --i)
17843 {
17844 if (mode_str[i] != '-')
17845 mode |= mask;
17846 mask = mask << 1;
17847 }
17848 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17849}
17850
17851/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852 * "setline()" function
17853 */
17854 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017855f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017856{
17857 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017858 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017859 list_T *l = NULL;
17860 listitem_T *li = NULL;
17861 long added = 0;
17862 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017863
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017864 lnum = get_tv_lnum(&argvars[0]);
17865 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017867 l = argvars[1].vval.v_list;
17868 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017869 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017870 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017871 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017872
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017873 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017874 for (;;)
17875 {
17876 if (l != NULL)
17877 {
17878 /* list argument, get next string */
17879 if (li == NULL)
17880 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017881 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017882 li = li->li_next;
17883 }
17884
17885 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017886 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017887 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017888
17889 /* When coming here from Insert mode, sync undo, so that this can be
17890 * undone separately from what was previously inserted. */
17891 if (u_sync_once == 2)
17892 {
17893 u_sync_once = 1; /* notify that u_sync() was called */
17894 u_sync(TRUE);
17895 }
17896
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017897 if (lnum <= curbuf->b_ml.ml_line_count)
17898 {
17899 /* existing line, replace it */
17900 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17901 {
17902 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017903 if (lnum == curwin->w_cursor.lnum)
17904 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017905 rettv->vval.v_number = 0; /* OK */
17906 }
17907 }
17908 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17909 {
17910 /* lnum is one past the last line, append the line */
17911 ++added;
17912 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17913 rettv->vval.v_number = 0; /* OK */
17914 }
17915
17916 if (l == NULL) /* only one string argument */
17917 break;
17918 ++lnum;
17919 }
17920
17921 if (added > 0)
17922 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017923}
17924
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017925static 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 +000017926
Bram Moolenaar071d4272004-06-13 20:20:40 +000017927/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017928 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017929 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017930 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017931set_qf_ll_list(
17932 win_T *wp UNUSED,
17933 typval_T *list_arg UNUSED,
17934 typval_T *action_arg UNUSED,
17935 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017936{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017937#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017938 char_u *act;
17939 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017940#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017941
Bram Moolenaar2641f772005-03-25 21:58:17 +000017942 rettv->vval.v_number = -1;
17943
17944#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017945 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017946 EMSG(_(e_listreq));
17947 else
17948 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017949 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017950
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017951 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017952 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017953 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017954 if (act == NULL)
17955 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017956 if (*act == 'a' || *act == 'r')
17957 action = *act;
17958 }
17959
Bram Moolenaar81484f42012-12-05 15:16:47 +010017960 if (l != NULL && set_errorlist(wp, l, action,
17961 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017962 rettv->vval.v_number = 0;
17963 }
17964#endif
17965}
17966
17967/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017968 * "setloclist()" function
17969 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017971f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017972{
17973 win_T *win;
17974
17975 rettv->vval.v_number = -1;
17976
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017977 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017978 if (win != NULL)
17979 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17980}
17981
17982/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017983 * "setmatches()" function
17984 */
17985 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017986f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017987{
17988#ifdef FEAT_SEARCH_EXTRA
17989 list_T *l;
17990 listitem_T *li;
17991 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017992 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017993
17994 rettv->vval.v_number = -1;
17995 if (argvars[0].v_type != VAR_LIST)
17996 {
17997 EMSG(_(e_listreq));
17998 return;
17999 }
18000 if ((l = argvars[0].vval.v_list) != NULL)
18001 {
18002
18003 /* To some extent make sure that we are dealing with a list from
18004 * "getmatches()". */
18005 li = l->lv_first;
18006 while (li != NULL)
18007 {
18008 if (li->li_tv.v_type != VAR_DICT
18009 || (d = li->li_tv.vval.v_dict) == NULL)
18010 {
18011 EMSG(_(e_invarg));
18012 return;
18013 }
18014 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018015 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18016 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018017 && dict_find(d, (char_u *)"priority", -1) != NULL
18018 && dict_find(d, (char_u *)"id", -1) != NULL))
18019 {
18020 EMSG(_(e_invarg));
18021 return;
18022 }
18023 li = li->li_next;
18024 }
18025
18026 clear_matches(curwin);
18027 li = l->lv_first;
18028 while (li != NULL)
18029 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018030 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018031 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018032 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018033 char_u *group;
18034 int priority;
18035 int id;
18036 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018037
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018038 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018039 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18040 {
18041 if (s == NULL)
18042 {
18043 s = list_alloc();
18044 if (s == NULL)
18045 return;
18046 }
18047
18048 /* match from matchaddpos() */
18049 for (i = 1; i < 9; i++)
18050 {
18051 sprintf((char *)buf, (char *)"pos%d", i);
18052 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18053 {
18054 if (di->di_tv.v_type != VAR_LIST)
18055 return;
18056
18057 list_append_tv(s, &di->di_tv);
18058 s->lv_refcount++;
18059 }
18060 else
18061 break;
18062 }
18063 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018064
18065 group = get_dict_string(d, (char_u *)"group", FALSE);
18066 priority = (int)get_dict_number(d, (char_u *)"priority");
18067 id = (int)get_dict_number(d, (char_u *)"id");
18068 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18069 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18070 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018071 if (i == 0)
18072 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018073 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018074 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018075 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018076 }
18077 else
18078 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018079 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018080 list_unref(s);
18081 s = NULL;
18082 }
18083
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018084 li = li->li_next;
18085 }
18086 rettv->vval.v_number = 0;
18087 }
18088#endif
18089}
18090
18091/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018092 * "setpos()" function
18093 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018096{
18097 pos_T pos;
18098 int fnum;
18099 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018100 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018101
Bram Moolenaar08250432008-02-13 11:42:46 +000018102 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018103 name = get_tv_string_chk(argvars);
18104 if (name != NULL)
18105 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018106 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018107 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018108 if (--pos.col < 0)
18109 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018110 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018111 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018112 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018113 if (fnum == curbuf->b_fnum)
18114 {
18115 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018116 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018117 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018118 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018119 curwin->w_set_curswant = FALSE;
18120 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018121 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018122 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018123 }
18124 else
18125 EMSG(_(e_invarg));
18126 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018127 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18128 {
18129 /* set mark */
18130 if (setmark_pos(name[1], &pos, fnum) == OK)
18131 rettv->vval.v_number = 0;
18132 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018133 else
18134 EMSG(_(e_invarg));
18135 }
18136 }
18137}
18138
18139/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018140 * "setqflist()" function
18141 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018143f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018144{
18145 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18146}
18147
18148/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018149 * "setreg()" function
18150 */
18151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018152f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018153{
18154 int regname;
18155 char_u *strregname;
18156 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018157 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018158 int append;
18159 char_u yank_type;
18160 long block_len;
18161
18162 block_len = -1;
18163 yank_type = MAUTO;
18164 append = FALSE;
18165
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018166 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018167 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018168
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018169 if (strregname == NULL)
18170 return; /* type error; errmsg already given */
18171 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018172 if (regname == 0 || regname == '@')
18173 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018174
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018175 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018176 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018177 stropt = get_tv_string_chk(&argvars[2]);
18178 if (stropt == NULL)
18179 return; /* type error */
18180 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018181 switch (*stropt)
18182 {
18183 case 'a': case 'A': /* append */
18184 append = TRUE;
18185 break;
18186 case 'v': case 'c': /* character-wise selection */
18187 yank_type = MCHAR;
18188 break;
18189 case 'V': case 'l': /* line-wise selection */
18190 yank_type = MLINE;
18191 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018192 case 'b': case Ctrl_V: /* block-wise selection */
18193 yank_type = MBLOCK;
18194 if (VIM_ISDIGIT(stropt[1]))
18195 {
18196 ++stropt;
18197 block_len = getdigits(&stropt) - 1;
18198 --stropt;
18199 }
18200 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201 }
18202 }
18203
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018204 if (argvars[1].v_type == VAR_LIST)
18205 {
18206 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018207 char_u **allocval;
18208 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018209 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018210 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018211 int len = argvars[1].vval.v_list->lv_len;
18212 listitem_T *li;
18213
Bram Moolenaar7d647822014-04-05 21:28:56 +020018214 /* First half: use for pointers to result lines; second half: use for
18215 * pointers to allocated copies. */
18216 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018217 if (lstval == NULL)
18218 return;
18219 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018220 allocval = lstval + len + 2;
18221 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018222
18223 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18224 li = li->li_next)
18225 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018226 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018227 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018228 goto free_lstval;
18229 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018230 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018231 /* Need to make a copy, next get_tv_string_buf_chk() will
18232 * overwrite the string. */
18233 strval = vim_strsave(buf);
18234 if (strval == NULL)
18235 goto free_lstval;
18236 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018237 }
18238 *curval++ = strval;
18239 }
18240 *curval++ = NULL;
18241
18242 write_reg_contents_lst(regname, lstval, -1,
18243 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018244free_lstval:
18245 while (curallocval > allocval)
18246 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018247 vim_free(lstval);
18248 }
18249 else
18250 {
18251 strval = get_tv_string_chk(&argvars[1]);
18252 if (strval == NULL)
18253 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018254 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018255 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018256 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018257 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018258}
18259
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018260/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018261 * "settabvar()" function
18262 */
18263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018264f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018265{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018266#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018267 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018268 tabpage_T *tp;
18269#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018270 char_u *varname, *tabvarname;
18271 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018272
18273 rettv->vval.v_number = 0;
18274
18275 if (check_restricted() || check_secure())
18276 return;
18277
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018278#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018279 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018280#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018281 varname = get_tv_string_chk(&argvars[1]);
18282 varp = &argvars[2];
18283
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018284 if (varname != NULL && varp != NULL
18285#ifdef FEAT_WINDOWS
18286 && tp != NULL
18287#endif
18288 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018289 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018290#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018291 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018292 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018293#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018294
18295 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18296 if (tabvarname != NULL)
18297 {
18298 STRCPY(tabvarname, "t:");
18299 STRCPY(tabvarname + 2, varname);
18300 set_var(tabvarname, varp, TRUE);
18301 vim_free(tabvarname);
18302 }
18303
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018304#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018305 /* Restore current tabpage */
18306 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018307 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018308#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018309 }
18310}
18311
18312/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018313 * "settabwinvar()" function
18314 */
18315 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018316f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018317{
18318 setwinvar(argvars, rettv, 1);
18319}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018320
18321/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018322 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018323 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018324 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018325f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018326{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018327 setwinvar(argvars, rettv, 0);
18328}
18329
18330/*
18331 * "setwinvar()" and "settabwinvar()" functions
18332 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018333
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018335setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018336{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018337 win_T *win;
18338#ifdef FEAT_WINDOWS
18339 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018340 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018341 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018342#endif
18343 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018344 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018345 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018346 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018347
18348 if (check_restricted() || check_secure())
18349 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018350
18351#ifdef FEAT_WINDOWS
18352 if (off == 1)
18353 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18354 else
18355 tp = curtab;
18356#endif
18357 win = find_win_by_nr(&argvars[off], tp);
18358 varname = get_tv_string_chk(&argvars[off + 1]);
18359 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018360
18361 if (win != NULL && varname != NULL && varp != NULL)
18362 {
18363#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018364 need_switch_win = !(tp == curtab && win == curwin);
18365 if (!need_switch_win
18366 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018368 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018369 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018371 long numval;
18372 char_u *strval;
18373 int error = FALSE;
18374
18375 ++varname;
18376 numval = get_tv_number_chk(varp, &error);
18377 strval = get_tv_string_buf_chk(varp, nbuf);
18378 if (!error && strval != NULL)
18379 set_option_value(varname, numval, strval, OPT_LOCAL);
18380 }
18381 else
18382 {
18383 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18384 if (winvarname != NULL)
18385 {
18386 STRCPY(winvarname, "w:");
18387 STRCPY(winvarname + 2, varname);
18388 set_var(winvarname, varp, TRUE);
18389 vim_free(winvarname);
18390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391 }
18392 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018393#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018394 if (need_switch_win)
18395 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396#endif
18397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018398}
18399
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018400#ifdef FEAT_CRYPT
18401/*
18402 * "sha256({string})" function
18403 */
18404 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018405f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018406{
18407 char_u *p;
18408
18409 p = get_tv_string(&argvars[0]);
18410 rettv->vval.v_string = vim_strsave(
18411 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18412 rettv->v_type = VAR_STRING;
18413}
18414#endif /* FEAT_CRYPT */
18415
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018417 * "shellescape({string})" function
18418 */
18419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018420f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018421{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018422 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018423 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018424 rettv->v_type = VAR_STRING;
18425}
18426
18427/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018428 * shiftwidth() function
18429 */
18430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018431f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018432{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018433 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018434}
18435
18436/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018437 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 */
18439 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018440f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018441{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018442 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443
Bram Moolenaar0d660222005-01-07 21:51:51 +000018444 p = get_tv_string(&argvars[0]);
18445 rettv->vval.v_string = vim_strsave(p);
18446 simplify_filename(rettv->vval.v_string); /* simplify in place */
18447 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448}
18449
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018450#ifdef FEAT_FLOAT
18451/*
18452 * "sin()" function
18453 */
18454 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018455f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018456{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018457 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018458
18459 rettv->v_type = VAR_FLOAT;
18460 if (get_float_arg(argvars, &f) == OK)
18461 rettv->vval.v_float = sin(f);
18462 else
18463 rettv->vval.v_float = 0.0;
18464}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018465
18466/*
18467 * "sinh()" function
18468 */
18469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018470f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018471{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018472 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018473
18474 rettv->v_type = VAR_FLOAT;
18475 if (get_float_arg(argvars, &f) == OK)
18476 rettv->vval.v_float = sinh(f);
18477 else
18478 rettv->vval.v_float = 0.0;
18479}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018480#endif
18481
Bram Moolenaar0d660222005-01-07 21:51:51 +000018482static int
18483#ifdef __BORLANDC__
18484 _RTLENTRYF
18485#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018486 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018487static int
18488#ifdef __BORLANDC__
18489 _RTLENTRYF
18490#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018491 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018492
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018493/* struct used in the array that's given to qsort() */
18494typedef struct
18495{
18496 listitem_T *item;
18497 int idx;
18498} sortItem_T;
18499
Bram Moolenaar0b962472016-02-22 22:51:33 +010018500/* struct storing information about current sort */
18501typedef struct
18502{
18503 int item_compare_ic;
18504 int item_compare_numeric;
18505 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018506#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018507 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018508#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018509 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018510 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018511 dict_T *item_compare_selfdict;
18512 int item_compare_func_err;
18513 int item_compare_keep_zero;
18514} sortinfo_T;
18515static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018516static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018517#define ITEM_COMPARE_FAIL 999
18518
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018520 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018521 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018522 static int
18523#ifdef __BORLANDC__
18524_RTLENTRYF
18525#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018526item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018527{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018528 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018529 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018530 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018531 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018532 int res;
18533 char_u numbuf1[NUMBUFLEN];
18534 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018535
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018536 si1 = (sortItem_T *)s1;
18537 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018538 tv1 = &si1->item->li_tv;
18539 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018540
Bram Moolenaar0b962472016-02-22 22:51:33 +010018541 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018542 {
18543 long v1 = get_tv_number(tv1);
18544 long v2 = get_tv_number(tv2);
18545
18546 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18547 }
18548
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018549#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018550 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018551 {
18552 float_T v1 = get_tv_float(tv1);
18553 float_T v2 = get_tv_float(tv2);
18554
18555 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18556 }
18557#endif
18558
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018559 /* tv2string() puts quotes around a string and allocates memory. Don't do
18560 * that for string variables. Use a single quote when comparing with a
18561 * non-string to do what the docs promise. */
18562 if (tv1->v_type == VAR_STRING)
18563 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018564 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018565 p1 = (char_u *)"'";
18566 else
18567 p1 = tv1->vval.v_string;
18568 }
18569 else
18570 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18571 if (tv2->v_type == VAR_STRING)
18572 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018573 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018574 p2 = (char_u *)"'";
18575 else
18576 p2 = tv2->vval.v_string;
18577 }
18578 else
18579 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018580 if (p1 == NULL)
18581 p1 = (char_u *)"";
18582 if (p2 == NULL)
18583 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018584 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018585 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018586 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018587 res = STRICMP(p1, p2);
18588 else
18589 res = STRCMP(p1, p2);
18590 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018591 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018592 {
18593 double n1, n2;
18594 n1 = strtod((char *)p1, (char **)&p1);
18595 n2 = strtod((char *)p2, (char **)&p2);
18596 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18597 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018598
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018599 /* When the result would be zero, compare the item indexes. Makes the
18600 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018601 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018602 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018603
Bram Moolenaar0d660222005-01-07 21:51:51 +000018604 vim_free(tofree1);
18605 vim_free(tofree2);
18606 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018607}
18608
18609 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018610#ifdef __BORLANDC__
18611_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018612#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018613item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018614{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018615 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018616 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018617 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018618 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018619 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018620 char_u *func_name;
18621 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018623 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018624 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018625 return 0;
18626
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018627 si1 = (sortItem_T *)s1;
18628 si2 = (sortItem_T *)s2;
18629
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018630 if (partial == NULL)
18631 func_name = sortinfo->item_compare_func;
18632 else
18633 func_name = partial->pt_name;
18634
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018635 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018636 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018637 copy_tv(&si1->item->li_tv, &argv[0]);
18638 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018639
18640 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018641 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018642 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018643 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018644 clear_tv(&argv[0]);
18645 clear_tv(&argv[1]);
18646
18647 if (res == FAIL)
18648 res = ITEM_COMPARE_FAIL;
18649 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018650 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18651 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018652 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018653 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018654
18655 /* When the result would be zero, compare the pointers themselves. Makes
18656 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018657 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018658 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018659
Bram Moolenaar0d660222005-01-07 21:51:51 +000018660 return res;
18661}
18662
18663/*
18664 * "sort({list})" function
18665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018667do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018668{
Bram Moolenaar33570922005-01-25 22:26:29 +000018669 list_T *l;
18670 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018671 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018672 sortinfo_T *old_sortinfo;
18673 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018674 long len;
18675 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018676
Bram Moolenaar0b962472016-02-22 22:51:33 +010018677 /* Pointer to current info struct used in compare function. Save and
18678 * restore the current one for nested calls. */
18679 old_sortinfo = sortinfo;
18680 sortinfo = &info;
18681
Bram Moolenaar0d660222005-01-07 21:51:51 +000018682 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018683 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018684 else
18685 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018687 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018688 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18689 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018690 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 rettv->vval.v_list = l;
18692 rettv->v_type = VAR_LIST;
18693 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694
Bram Moolenaar0d660222005-01-07 21:51:51 +000018695 len = list_len(l);
18696 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018697 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018698
Bram Moolenaar0b962472016-02-22 22:51:33 +010018699 info.item_compare_ic = FALSE;
18700 info.item_compare_numeric = FALSE;
18701 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018702#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018703 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018704#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018705 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018706 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018707 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018708 if (argvars[1].v_type != VAR_UNKNOWN)
18709 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018710 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018711 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018713 else if (argvars[1].v_type == VAR_PARTIAL)
18714 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018715 else
18716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018717 int error = FALSE;
18718
18719 i = get_tv_number_chk(&argvars[1], &error);
18720 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018722 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018723 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018724 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018725 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018726 else if (i != 0)
18727 {
18728 EMSG(_(e_invarg));
18729 goto theend;
18730 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018731 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018732 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018733 if (*info.item_compare_func == NUL)
18734 {
18735 /* empty string means default sort */
18736 info.item_compare_func = NULL;
18737 }
18738 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018739 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018740 info.item_compare_func = NULL;
18741 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018742 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018743 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018744 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018745 info.item_compare_func = NULL;
18746 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018747 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018748#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018749 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018750 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018751 info.item_compare_func = NULL;
18752 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018753 }
18754#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018755 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018756 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018757 info.item_compare_func = NULL;
18758 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018759 }
18760 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018762
18763 if (argvars[2].v_type != VAR_UNKNOWN)
18764 {
18765 /* optional third argument: {dict} */
18766 if (argvars[2].v_type != VAR_DICT)
18767 {
18768 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018769 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018770 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018771 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018772 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018773 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018774
Bram Moolenaar0d660222005-01-07 21:51:51 +000018775 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018776 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018778 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779
Bram Moolenaar327aa022014-03-25 18:24:23 +010018780 i = 0;
18781 if (sort)
18782 {
18783 /* sort(): ptrs will be the list to sort */
18784 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018785 {
18786 ptrs[i].item = li;
18787 ptrs[i].idx = i;
18788 ++i;
18789 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018790
Bram Moolenaar0b962472016-02-22 22:51:33 +010018791 info.item_compare_func_err = FALSE;
18792 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018793 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018794 if ((info.item_compare_func != NULL
18795 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018796 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018797 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018798 EMSG(_("E702: Sort compare function failed"));
18799 else
18800 {
18801 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018802 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018803 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018804 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018805 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018806
Bram Moolenaar0b962472016-02-22 22:51:33 +010018807 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018808 {
18809 /* Clear the List and append the items in sorted order. */
18810 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18811 l->lv_len = 0;
18812 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018813 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018814 }
18815 }
18816 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018817 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018819 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018820
18821 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018822 info.item_compare_func_err = FALSE;
18823 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018824 item_compare_func_ptr = info.item_compare_func != NULL
18825 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018826 ? item_compare2 : item_compare;
18827
18828 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18829 li = li->li_next)
18830 {
18831 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18832 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018833 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018834 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018835 {
18836 EMSG(_("E882: Uniq compare function failed"));
18837 break;
18838 }
18839 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018840
Bram Moolenaar0b962472016-02-22 22:51:33 +010018841 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018842 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018843 while (--i >= 0)
18844 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018845 li = ptrs[i].item->li_next;
18846 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018847 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018848 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018849 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018850 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018851 list_fix_watch(l, li);
18852 listitem_free(li);
18853 l->lv_len--;
18854 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018855 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018856 }
18857
18858 vim_free(ptrs);
18859 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860theend:
18861 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018862}
18863
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018864/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018865 * "sort({list})" function
18866 */
18867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018868f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018869{
18870 do_sort_uniq(argvars, rettv, TRUE);
18871}
18872
18873/*
18874 * "uniq({list})" function
18875 */
18876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018877f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018878{
18879 do_sort_uniq(argvars, rettv, FALSE);
18880}
18881
18882/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018883 * "soundfold({word})" function
18884 */
18885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018886f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018887{
18888 char_u *s;
18889
18890 rettv->v_type = VAR_STRING;
18891 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018892#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018893 rettv->vval.v_string = eval_soundfold(s);
18894#else
18895 rettv->vval.v_string = vim_strsave(s);
18896#endif
18897}
18898
18899/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018900 * "spellbadword()" function
18901 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018903f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018904{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018905 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018906 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018907 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018908
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018909 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018910 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018912#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018913 if (argvars[0].v_type == VAR_UNKNOWN)
18914 {
18915 /* Find the start and length of the badly spelled word. */
18916 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18917 if (len != 0)
18918 word = ml_get_cursor();
18919 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018920 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018921 {
18922 char_u *str = get_tv_string_chk(&argvars[0]);
18923 int capcol = -1;
18924
18925 if (str != NULL)
18926 {
18927 /* Check the argument for spelling. */
18928 while (*str != NUL)
18929 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018930 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018931 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018932 {
18933 word = str;
18934 break;
18935 }
18936 str += len;
18937 }
18938 }
18939 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018940#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018941
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018942 list_append_string(rettv->vval.v_list, word, len);
18943 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018944 attr == HLF_SPB ? "bad" :
18945 attr == HLF_SPR ? "rare" :
18946 attr == HLF_SPL ? "local" :
18947 attr == HLF_SPC ? "caps" :
18948 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018949}
18950
18951/*
18952 * "spellsuggest()" function
18953 */
18954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018955f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018956{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018957#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018958 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018959 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018960 int maxcount;
18961 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018962 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018963 listitem_T *li;
18964 int need_capital = FALSE;
18965#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018966
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018967 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018968 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018969
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018970#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018971 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018972 {
18973 str = get_tv_string(&argvars[0]);
18974 if (argvars[1].v_type != VAR_UNKNOWN)
18975 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018976 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018977 if (maxcount <= 0)
18978 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018979 if (argvars[2].v_type != VAR_UNKNOWN)
18980 {
18981 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18982 if (typeerr)
18983 return;
18984 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018985 }
18986 else
18987 maxcount = 25;
18988
Bram Moolenaar4770d092006-01-12 23:22:24 +000018989 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018990
18991 for (i = 0; i < ga.ga_len; ++i)
18992 {
18993 str = ((char_u **)ga.ga_data)[i];
18994
18995 li = listitem_alloc();
18996 if (li == NULL)
18997 vim_free(str);
18998 else
18999 {
19000 li->li_tv.v_type = VAR_STRING;
19001 li->li_tv.v_lock = 0;
19002 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019003 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019004 }
19005 }
19006 ga_clear(&ga);
19007 }
19008#endif
19009}
19010
Bram Moolenaar0d660222005-01-07 21:51:51 +000019011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019012f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019013{
19014 char_u *str;
19015 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019016 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019017 regmatch_T regmatch;
19018 char_u patbuf[NUMBUFLEN];
19019 char_u *save_cpo;
19020 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019021 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019022 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019023 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019024
19025 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19026 save_cpo = p_cpo;
19027 p_cpo = (char_u *)"";
19028
19029 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019030 if (argvars[1].v_type != VAR_UNKNOWN)
19031 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019032 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19033 if (pat == NULL)
19034 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019035 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019036 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019037 }
19038 if (pat == NULL || *pat == NUL)
19039 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019040
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019041 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019042 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019043 if (typeerr)
19044 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019045
Bram Moolenaar0d660222005-01-07 21:51:51 +000019046 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19047 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019048 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019049 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019050 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019051 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019052 if (*str == NUL)
19053 match = FALSE; /* empty item at the end */
19054 else
19055 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019056 if (match)
19057 end = regmatch.startp[0];
19058 else
19059 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019060 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19061 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019063 if (list_append_string(rettv->vval.v_list, str,
19064 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019065 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 }
19067 if (!match)
19068 break;
19069 /* Advance to just after the match. */
19070 if (regmatch.endp[0] > str)
19071 col = 0;
19072 else
19073 {
19074 /* Don't get stuck at the same match. */
19075#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019076 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019077#else
19078 col = 1;
19079#endif
19080 }
19081 str = regmatch.endp[0];
19082 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019083
Bram Moolenaar473de612013-06-08 18:19:48 +020019084 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019085 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019086
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019088}
19089
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019090#ifdef FEAT_FLOAT
19091/*
19092 * "sqrt()" function
19093 */
19094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019095f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019096{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019097 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019098
19099 rettv->v_type = VAR_FLOAT;
19100 if (get_float_arg(argvars, &f) == OK)
19101 rettv->vval.v_float = sqrt(f);
19102 else
19103 rettv->vval.v_float = 0.0;
19104}
19105
19106/*
19107 * "str2float()" function
19108 */
19109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019110f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019111{
19112 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19113
19114 if (*p == '+')
19115 p = skipwhite(p + 1);
19116 (void)string2float(p, &rettv->vval.v_float);
19117 rettv->v_type = VAR_FLOAT;
19118}
19119#endif
19120
Bram Moolenaar2c932302006-03-18 21:42:09 +000019121/*
19122 * "str2nr()" function
19123 */
19124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019125f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019126{
19127 int base = 10;
19128 char_u *p;
19129 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019130 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019131
19132 if (argvars[1].v_type != VAR_UNKNOWN)
19133 {
19134 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019135 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019136 {
19137 EMSG(_(e_invarg));
19138 return;
19139 }
19140 }
19141
19142 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019143 if (*p == '+')
19144 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019145 switch (base)
19146 {
19147 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19148 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19149 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19150 default: what = 0;
19151 }
19152 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019153 rettv->vval.v_number = n;
19154}
19155
Bram Moolenaar071d4272004-06-13 20:20:40 +000019156#ifdef HAVE_STRFTIME
19157/*
19158 * "strftime({format}[, {time}])" function
19159 */
19160 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019161f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019162{
19163 char_u result_buf[256];
19164 struct tm *curtime;
19165 time_t seconds;
19166 char_u *p;
19167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019168 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019170 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019171 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019172 seconds = time(NULL);
19173 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019174 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019175 curtime = localtime(&seconds);
19176 /* MSVC returns NULL for an invalid value of seconds. */
19177 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019178 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019179 else
19180 {
19181# ifdef FEAT_MBYTE
19182 vimconv_T conv;
19183 char_u *enc;
19184
19185 conv.vc_type = CONV_NONE;
19186 enc = enc_locale();
19187 convert_setup(&conv, p_enc, enc);
19188 if (conv.vc_type != CONV_NONE)
19189 p = string_convert(&conv, p, NULL);
19190# endif
19191 if (p != NULL)
19192 (void)strftime((char *)result_buf, sizeof(result_buf),
19193 (char *)p, curtime);
19194 else
19195 result_buf[0] = NUL;
19196
19197# ifdef FEAT_MBYTE
19198 if (conv.vc_type != CONV_NONE)
19199 vim_free(p);
19200 convert_setup(&conv, enc, p_enc);
19201 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019202 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203 else
19204# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019205 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019206
19207# ifdef FEAT_MBYTE
19208 /* Release conversion descriptors */
19209 convert_setup(&conv, NULL, NULL);
19210 vim_free(enc);
19211# endif
19212 }
19213}
19214#endif
19215
19216/*
19217 * "stridx()" function
19218 */
19219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019220f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221{
19222 char_u buf[NUMBUFLEN];
19223 char_u *needle;
19224 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019225 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019227 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019229 needle = get_tv_string_chk(&argvars[1]);
19230 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019231 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019232 if (needle == NULL || haystack == NULL)
19233 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019234
Bram Moolenaar33570922005-01-25 22:26:29 +000019235 if (argvars[2].v_type != VAR_UNKNOWN)
19236 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019237 int error = FALSE;
19238
19239 start_idx = get_tv_number_chk(&argvars[2], &error);
19240 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019241 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019242 if (start_idx >= 0)
19243 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019244 }
19245
19246 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19247 if (pos != NULL)
19248 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019249}
19250
19251/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019252 * "string()" function
19253 */
19254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019255f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019256{
19257 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019258 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019259
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019260 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019261 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019262 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019263 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019264 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265}
19266
19267/*
19268 * "strlen()" function
19269 */
19270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019271f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 rettv->vval.v_number = (varnumber_T)(STRLEN(
19274 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275}
19276
19277/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019278 * "strchars()" function
19279 */
19280 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019281f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019282{
19283 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019284 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019285#ifdef FEAT_MBYTE
19286 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019287 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019288#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019289
19290 if (argvars[1].v_type != VAR_UNKNOWN)
19291 skipcc = get_tv_number_chk(&argvars[1], NULL);
19292 if (skipcc < 0 || skipcc > 1)
19293 EMSG(_(e_invarg));
19294 else
19295 {
19296#ifdef FEAT_MBYTE
19297 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19298 while (*s != NUL)
19299 {
19300 func_mb_ptr2char_adv(&s);
19301 ++len;
19302 }
19303 rettv->vval.v_number = len;
19304#else
19305 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19306#endif
19307 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019308}
19309
19310/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019311 * "strdisplaywidth()" function
19312 */
19313 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019314f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019315{
19316 char_u *s = get_tv_string(&argvars[0]);
19317 int col = 0;
19318
19319 if (argvars[1].v_type != VAR_UNKNOWN)
19320 col = get_tv_number(&argvars[1]);
19321
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019322 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019323}
19324
19325/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019326 * "strwidth()" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019330{
19331 char_u *s = get_tv_string(&argvars[0]);
19332
19333 rettv->vval.v_number = (varnumber_T)(
19334#ifdef FEAT_MBYTE
19335 mb_string2cells(s, -1)
19336#else
19337 STRLEN(s)
19338#endif
19339 );
19340}
19341
19342/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019343 * "strpart()" function
19344 */
19345 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019346f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019347{
19348 char_u *p;
19349 int n;
19350 int len;
19351 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019353
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019354 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019355 slen = (int)STRLEN(p);
19356
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019357 n = get_tv_number_chk(&argvars[1], &error);
19358 if (error)
19359 len = 0;
19360 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019361 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019362 else
19363 len = slen - n; /* default len: all bytes that are available. */
19364
19365 /*
19366 * Only return the overlap between the specified part and the actual
19367 * string.
19368 */
19369 if (n < 0)
19370 {
19371 len += n;
19372 n = 0;
19373 }
19374 else if (n > slen)
19375 n = slen;
19376 if (len < 0)
19377 len = 0;
19378 else if (n + len > slen)
19379 len = slen - n;
19380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019381 rettv->v_type = VAR_STRING;
19382 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019383}
19384
19385/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019386 * "strridx()" function
19387 */
19388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019389f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019390{
19391 char_u buf[NUMBUFLEN];
19392 char_u *needle;
19393 char_u *haystack;
19394 char_u *rest;
19395 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019396 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019397
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019398 needle = get_tv_string_chk(&argvars[1]);
19399 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019400
19401 rettv->vval.v_number = -1;
19402 if (needle == NULL || haystack == NULL)
19403 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019404
19405 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019406 if (argvars[2].v_type != VAR_UNKNOWN)
19407 {
19408 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019409 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019410 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019411 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019412 }
19413 else
19414 end_idx = haystack_len;
19415
Bram Moolenaar0d660222005-01-07 21:51:51 +000019416 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019417 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019418 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019419 lastmatch = haystack + end_idx;
19420 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019421 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019422 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019423 for (rest = haystack; *rest != '\0'; ++rest)
19424 {
19425 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019426 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427 break;
19428 lastmatch = rest;
19429 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019430 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019431
19432 if (lastmatch == NULL)
19433 rettv->vval.v_number = -1;
19434 else
19435 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19436}
19437
19438/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019439 * "strtrans()" function
19440 */
19441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019442f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019443{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019444 rettv->v_type = VAR_STRING;
19445 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446}
19447
19448/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019449 * "submatch()" function
19450 */
19451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019452f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019453{
Bram Moolenaar41571762014-04-02 19:00:58 +020019454 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019455 int no;
19456 int retList = 0;
19457
19458 no = (int)get_tv_number_chk(&argvars[0], &error);
19459 if (error)
19460 return;
19461 error = FALSE;
19462 if (argvars[1].v_type != VAR_UNKNOWN)
19463 retList = get_tv_number_chk(&argvars[1], &error);
19464 if (error)
19465 return;
19466
19467 if (retList == 0)
19468 {
19469 rettv->v_type = VAR_STRING;
19470 rettv->vval.v_string = reg_submatch(no);
19471 }
19472 else
19473 {
19474 rettv->v_type = VAR_LIST;
19475 rettv->vval.v_list = reg_submatch_list(no);
19476 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477}
19478
19479/*
19480 * "substitute()" function
19481 */
19482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019483f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019484{
19485 char_u patbuf[NUMBUFLEN];
19486 char_u subbuf[NUMBUFLEN];
19487 char_u flagsbuf[NUMBUFLEN];
19488
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019489 char_u *str = get_tv_string_chk(&argvars[0]);
19490 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19491 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19492 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19493
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019495 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19496 rettv->vval.v_string = NULL;
19497 else
19498 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019499}
19500
19501/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019502 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019505f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019506{
19507 int id = 0;
19508#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019509 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019510 long col;
19511 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019512 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019513
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019514 lnum = get_tv_lnum(argvars); /* -1 on type error */
19515 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19516 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019517
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019518 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019519 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019520 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019521#endif
19522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019523 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019524}
19525
19526/*
19527 * "synIDattr(id, what [, mode])" function
19528 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019530f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019531{
19532 char_u *p = NULL;
19533#ifdef FEAT_SYN_HL
19534 int id;
19535 char_u *what;
19536 char_u *mode;
19537 char_u modebuf[NUMBUFLEN];
19538 int modec;
19539
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019540 id = get_tv_number(&argvars[0]);
19541 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019542 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019544 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019545 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019546 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547 modec = 0; /* replace invalid with current */
19548 }
19549 else
19550 {
19551#ifdef FEAT_GUI
19552 if (gui.in_use)
19553 modec = 'g';
19554 else
19555#endif
19556 if (t_colors > 1)
19557 modec = 'c';
19558 else
19559 modec = 't';
19560 }
19561
19562
19563 switch (TOLOWER_ASC(what[0]))
19564 {
19565 case 'b':
19566 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19567 p = highlight_color(id, what, modec);
19568 else /* bold */
19569 p = highlight_has_attr(id, HL_BOLD, modec);
19570 break;
19571
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019572 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019573 p = highlight_color(id, what, modec);
19574 break;
19575
19576 case 'i':
19577 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19578 p = highlight_has_attr(id, HL_INVERSE, modec);
19579 else /* italic */
19580 p = highlight_has_attr(id, HL_ITALIC, modec);
19581 break;
19582
19583 case 'n': /* name */
19584 p = get_highlight_name(NULL, id - 1);
19585 break;
19586
19587 case 'r': /* reverse */
19588 p = highlight_has_attr(id, HL_INVERSE, modec);
19589 break;
19590
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019591 case 's':
19592 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19593 p = highlight_color(id, what, modec);
19594 else /* standout */
19595 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019596 break;
19597
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019598 case 'u':
19599 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19600 /* underline */
19601 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19602 else
19603 /* undercurl */
19604 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019605 break;
19606 }
19607
19608 if (p != NULL)
19609 p = vim_strsave(p);
19610#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019611 rettv->v_type = VAR_STRING;
19612 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019613}
19614
19615/*
19616 * "synIDtrans(id)" function
19617 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019619f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019620{
19621 int id;
19622
19623#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019624 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625
19626 if (id > 0)
19627 id = syn_get_final_id(id);
19628 else
19629#endif
19630 id = 0;
19631
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019632 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633}
19634
19635/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019636 * "synconcealed(lnum, col)" function
19637 */
19638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019639f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019640{
19641#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19642 long lnum;
19643 long col;
19644 int syntax_flags = 0;
19645 int cchar;
19646 int matchid = 0;
19647 char_u str[NUMBUFLEN];
19648#endif
19649
19650 rettv->v_type = VAR_LIST;
19651 rettv->vval.v_list = NULL;
19652
19653#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19654 lnum = get_tv_lnum(argvars); /* -1 on type error */
19655 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19656
19657 vim_memset(str, NUL, sizeof(str));
19658
19659 if (rettv_list_alloc(rettv) != FAIL)
19660 {
19661 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19662 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19663 && curwin->w_p_cole > 0)
19664 {
19665 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19666 syntax_flags = get_syntax_info(&matchid);
19667
19668 /* get the conceal character */
19669 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19670 {
19671 cchar = syn_get_sub_char();
19672 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19673 cchar = lcs_conceal;
19674 if (cchar != NUL)
19675 {
19676# ifdef FEAT_MBYTE
19677 if (has_mbyte)
19678 (*mb_char2bytes)(cchar, str);
19679 else
19680# endif
19681 str[0] = cchar;
19682 }
19683 }
19684 }
19685
19686 list_append_number(rettv->vval.v_list,
19687 (syntax_flags & HL_CONCEAL) != 0);
19688 /* -1 to auto-determine strlen */
19689 list_append_string(rettv->vval.v_list, str, -1);
19690 list_append_number(rettv->vval.v_list, matchid);
19691 }
19692#endif
19693}
19694
19695/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019696 * "synstack(lnum, col)" function
19697 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019699f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019700{
19701#ifdef FEAT_SYN_HL
19702 long lnum;
19703 long col;
19704 int i;
19705 int id;
19706#endif
19707
19708 rettv->v_type = VAR_LIST;
19709 rettv->vval.v_list = NULL;
19710
19711#ifdef FEAT_SYN_HL
19712 lnum = get_tv_lnum(argvars); /* -1 on type error */
19713 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19714
19715 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019716 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019717 && rettv_list_alloc(rettv) != FAIL)
19718 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019719 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019720 for (i = 0; ; ++i)
19721 {
19722 id = syn_get_stack_item(i);
19723 if (id < 0)
19724 break;
19725 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19726 break;
19727 }
19728 }
19729#endif
19730}
19731
Bram Moolenaar071d4272004-06-13 20:20:40 +000019732 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019733get_cmd_output_as_rettv(
19734 typval_T *argvars,
19735 typval_T *rettv,
19736 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019738 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019739 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019740 char_u *infile = NULL;
19741 char_u buf[NUMBUFLEN];
19742 int err = FALSE;
19743 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019744 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019745 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019746
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019747 rettv->v_type = VAR_STRING;
19748 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019749 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019750 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019751
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019752 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019753 {
19754 /*
19755 * Write the string to a temp file, to be used for input of the shell
19756 * command.
19757 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019758 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019759 {
19760 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019761 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019762 }
19763
19764 fd = mch_fopen((char *)infile, WRITEBIN);
19765 if (fd == NULL)
19766 {
19767 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019768 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019769 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019770 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019771 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019772 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19773 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019774 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019775 else
19776 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019777 size_t len;
19778
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019779 p = get_tv_string_buf_chk(&argvars[1], buf);
19780 if (p == NULL)
19781 {
19782 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019783 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019784 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019785 len = STRLEN(p);
19786 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019787 err = TRUE;
19788 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019789 if (fclose(fd) != 0)
19790 err = TRUE;
19791 if (err)
19792 {
19793 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019794 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019795 }
19796 }
19797
Bram Moolenaar52a72462014-08-29 15:53:52 +020019798 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19799 * echoes typeahead, that messes up the display. */
19800 if (!msg_silent)
19801 flags += SHELL_COOKED;
19802
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019803 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019804 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019805 int len;
19806 listitem_T *li;
19807 char_u *s = NULL;
19808 char_u *start;
19809 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019810 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019811
Bram Moolenaar52a72462014-08-29 15:53:52 +020019812 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019813 if (res == NULL)
19814 goto errret;
19815
19816 list = list_alloc();
19817 if (list == NULL)
19818 goto errret;
19819
19820 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019821 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019822 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019823 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019824 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019825 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019826
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019827 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019828 if (s == NULL)
19829 goto errret;
19830
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019831 for (p = s; start < end; ++p, ++start)
19832 *p = *start == NUL ? NL : *start;
19833 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019834
19835 li = listitem_alloc();
19836 if (li == NULL)
19837 {
19838 vim_free(s);
19839 goto errret;
19840 }
19841 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019842 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019843 li->li_tv.vval.v_string = s;
19844 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019845 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019846
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019847 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019848 rettv->v_type = VAR_LIST;
19849 rettv->vval.v_list = list;
19850 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019851 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019852 else
19853 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019854 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019855#ifdef USE_CR
19856 /* translate <CR> into <NL> */
19857 if (res != NULL)
19858 {
19859 char_u *s;
19860
19861 for (s = res; *s; ++s)
19862 {
19863 if (*s == CAR)
19864 *s = NL;
19865 }
19866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019867#else
19868# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019869 /* translate <CR><NL> into <NL> */
19870 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019872 char_u *s, *d;
19873
19874 d = res;
19875 for (s = res; *s; ++s)
19876 {
19877 if (s[0] == CAR && s[1] == NL)
19878 ++s;
19879 *d++ = *s;
19880 }
19881 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019882 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019883# endif
19884#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019885 rettv->vval.v_string = res;
19886 res = NULL;
19887 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019888
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019889errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019890 if (infile != NULL)
19891 {
19892 mch_remove(infile);
19893 vim_free(infile);
19894 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019895 if (res != NULL)
19896 vim_free(res);
19897 if (list != NULL)
19898 list_free(list, TRUE);
19899}
19900
19901/*
19902 * "system()" function
19903 */
19904 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019905f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019906{
19907 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19908}
19909
19910/*
19911 * "systemlist()" function
19912 */
19913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019914f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019915{
19916 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917}
19918
19919/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019920 * "tabpagebuflist()" function
19921 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019923f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019924{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019925#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019926 tabpage_T *tp;
19927 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019928
19929 if (argvars[0].v_type == VAR_UNKNOWN)
19930 wp = firstwin;
19931 else
19932 {
19933 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19934 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019935 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019936 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019937 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019938 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019939 for (; wp != NULL; wp = wp->w_next)
19940 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019941 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019942 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019943 }
19944#endif
19945}
19946
19947
19948/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019949 * "tabpagenr()" function
19950 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019952f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019953{
19954 int nr = 1;
19955#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019956 char_u *arg;
19957
19958 if (argvars[0].v_type != VAR_UNKNOWN)
19959 {
19960 arg = get_tv_string_chk(&argvars[0]);
19961 nr = 0;
19962 if (arg != NULL)
19963 {
19964 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019965 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019966 else
19967 EMSG2(_(e_invexpr2), arg);
19968 }
19969 }
19970 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019971 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019972#endif
19973 rettv->vval.v_number = nr;
19974}
19975
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019976
19977#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019978static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019979
19980/*
19981 * Common code for tabpagewinnr() and winnr().
19982 */
19983 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019984get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019985{
19986 win_T *twin;
19987 int nr = 1;
19988 win_T *wp;
19989 char_u *arg;
19990
19991 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19992 if (argvar->v_type != VAR_UNKNOWN)
19993 {
19994 arg = get_tv_string_chk(argvar);
19995 if (arg == NULL)
19996 nr = 0; /* type error; errmsg already given */
19997 else if (STRCMP(arg, "$") == 0)
19998 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19999 else if (STRCMP(arg, "#") == 0)
20000 {
20001 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20002 if (twin == NULL)
20003 nr = 0;
20004 }
20005 else
20006 {
20007 EMSG2(_(e_invexpr2), arg);
20008 nr = 0;
20009 }
20010 }
20011
20012 if (nr > 0)
20013 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20014 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020015 {
20016 if (wp == NULL)
20017 {
20018 /* didn't find it in this tabpage */
20019 nr = 0;
20020 break;
20021 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020022 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020023 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020024 return nr;
20025}
20026#endif
20027
20028/*
20029 * "tabpagewinnr()" function
20030 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020032f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020033{
20034 int nr = 1;
20035#ifdef FEAT_WINDOWS
20036 tabpage_T *tp;
20037
20038 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20039 if (tp == NULL)
20040 nr = 0;
20041 else
20042 nr = get_winnr(tp, &argvars[1]);
20043#endif
20044 rettv->vval.v_number = nr;
20045}
20046
20047
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020048/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020049 * "tagfiles()" function
20050 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020052f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020053{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020054 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020055 tagname_T tn;
20056 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020057
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020058 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020059 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020060 fname = alloc(MAXPATHL);
20061 if (fname == NULL)
20062 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020063
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020064 for (first = TRUE; ; first = FALSE)
20065 if (get_tagfname(&tn, first, fname) == FAIL
20066 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020067 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020068 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020069 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020070}
20071
20072/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020073 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020074 */
20075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020076f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020077{
20078 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020079
20080 tag_pattern = get_tv_string(&argvars[0]);
20081
20082 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020083 if (*tag_pattern == NUL)
20084 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020086 if (rettv_list_alloc(rettv) == OK)
20087 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020088}
20089
20090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020091 * "tempname()" function
20092 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020094f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020095{
20096 static int x = 'A';
20097
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020098 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020099 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020100
20101 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20102 * names. Skip 'I' and 'O', they are used for shell redirection. */
20103 do
20104 {
20105 if (x == 'Z')
20106 x = '0';
20107 else if (x == '9')
20108 x = 'A';
20109 else
20110 {
20111#ifdef EBCDIC
20112 if (x == 'I')
20113 x = 'J';
20114 else if (x == 'R')
20115 x = 'S';
20116 else
20117#endif
20118 ++x;
20119 }
20120 } while (x == 'I' || x == 'O');
20121}
20122
20123/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020124 * "test(list)" function: Just checking the walls...
20125 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020127f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020128{
20129 /* Used for unit testing. Change the code below to your liking. */
20130#if 0
20131 listitem_T *li;
20132 list_T *l;
20133 char_u *bad, *good;
20134
20135 if (argvars[0].v_type != VAR_LIST)
20136 return;
20137 l = argvars[0].vval.v_list;
20138 if (l == NULL)
20139 return;
20140 li = l->lv_first;
20141 if (li == NULL)
20142 return;
20143 bad = get_tv_string(&li->li_tv);
20144 li = li->li_next;
20145 if (li == NULL)
20146 return;
20147 good = get_tv_string(&li->li_tv);
20148 rettv->vval.v_number = test_edit_score(bad, good);
20149#endif
20150}
20151
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020152#ifdef FEAT_FLOAT
20153/*
20154 * "tan()" function
20155 */
20156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020157f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020158{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020159 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020160
20161 rettv->v_type = VAR_FLOAT;
20162 if (get_float_arg(argvars, &f) == OK)
20163 rettv->vval.v_float = tan(f);
20164 else
20165 rettv->vval.v_float = 0.0;
20166}
20167
20168/*
20169 * "tanh()" function
20170 */
20171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020172f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020173{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020174 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020175
20176 rettv->v_type = VAR_FLOAT;
20177 if (get_float_arg(argvars, &f) == OK)
20178 rettv->vval.v_float = tanh(f);
20179 else
20180 rettv->vval.v_float = 0.0;
20181}
20182#endif
20183
Bram Moolenaar975b5272016-03-15 23:10:59 +010020184#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20185/*
20186 * Get a callback from "arg". It can be a Funcref or a function name.
20187 * When "arg" is zero return an empty string.
20188 * Return NULL for an invalid argument.
20189 */
20190 char_u *
20191get_callback(typval_T *arg, partial_T **pp)
20192{
20193 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20194 {
20195 *pp = arg->vval.v_partial;
20196 return (*pp)->pt_name;
20197 }
20198 *pp = NULL;
20199 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20200 return arg->vval.v_string;
20201 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20202 return (char_u *)"";
20203 EMSG(_("E921: Invalid callback argument"));
20204 return NULL;
20205}
20206#endif
20207
20208#ifdef FEAT_TIMERS
20209/*
20210 * "timer_start(time, callback [, options])" function
20211 */
20212 static void
20213f_timer_start(typval_T *argvars, typval_T *rettv)
20214{
20215 long msec = get_tv_number(&argvars[0]);
20216 timer_T *timer;
20217 int repeat = 0;
20218 char_u *callback;
20219 dict_T *dict;
20220
20221 if (argvars[2].v_type != VAR_UNKNOWN)
20222 {
20223 if (argvars[2].v_type != VAR_DICT
20224 || (dict = argvars[2].vval.v_dict) == NULL)
20225 {
20226 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20227 return;
20228 }
20229 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20230 repeat = get_dict_number(dict, (char_u *)"repeat");
20231 }
20232
20233 timer = create_timer(msec, repeat);
20234 callback = get_callback(&argvars[1], &timer->tr_partial);
20235 if (callback == NULL)
20236 {
20237 stop_timer(timer);
20238 rettv->vval.v_number = -1;
20239 }
20240 else
20241 {
20242 timer->tr_callback = vim_strsave(callback);
20243 rettv->vval.v_number = timer->tr_id;
20244 }
20245}
20246
20247/*
20248 * "timer_stop(timer)" function
20249 */
20250 static void
20251f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20252{
20253 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20254
20255 if (timer != NULL)
20256 stop_timer(timer);
20257}
20258#endif
20259
Bram Moolenaard52d9742005-08-21 22:20:28 +000020260/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020261 * "tolower(string)" function
20262 */
20263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020264f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020265{
20266 char_u *p;
20267
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020268 p = vim_strsave(get_tv_string(&argvars[0]));
20269 rettv->v_type = VAR_STRING;
20270 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020271
20272 if (p != NULL)
20273 while (*p != NUL)
20274 {
20275#ifdef FEAT_MBYTE
20276 int l;
20277
20278 if (enc_utf8)
20279 {
20280 int c, lc;
20281
20282 c = utf_ptr2char(p);
20283 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020284 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020285 /* TODO: reallocate string when byte count changes. */
20286 if (utf_char2len(lc) == l)
20287 utf_char2bytes(lc, p);
20288 p += l;
20289 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020290 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020291 p += l; /* skip multi-byte character */
20292 else
20293#endif
20294 {
20295 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20296 ++p;
20297 }
20298 }
20299}
20300
20301/*
20302 * "toupper(string)" function
20303 */
20304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020305f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020306{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020307 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020308 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020309}
20310
20311/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020312 * "tr(string, fromstr, tostr)" function
20313 */
20314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020315f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020316{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020317 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020318 char_u *fromstr;
20319 char_u *tostr;
20320 char_u *p;
20321#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020322 int inlen;
20323 int fromlen;
20324 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020325 int idx;
20326 char_u *cpstr;
20327 int cplen;
20328 int first = TRUE;
20329#endif
20330 char_u buf[NUMBUFLEN];
20331 char_u buf2[NUMBUFLEN];
20332 garray_T ga;
20333
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020334 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020335 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20336 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020337
20338 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020339 rettv->v_type = VAR_STRING;
20340 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020341 if (fromstr == NULL || tostr == NULL)
20342 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020343 ga_init2(&ga, (int)sizeof(char), 80);
20344
20345#ifdef FEAT_MBYTE
20346 if (!has_mbyte)
20347#endif
20348 /* not multi-byte: fromstr and tostr must be the same length */
20349 if (STRLEN(fromstr) != STRLEN(tostr))
20350 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020351#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020352error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020353#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020354 EMSG2(_(e_invarg2), fromstr);
20355 ga_clear(&ga);
20356 return;
20357 }
20358
20359 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020360 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020361 {
20362#ifdef FEAT_MBYTE
20363 if (has_mbyte)
20364 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020365 inlen = (*mb_ptr2len)(in_str);
20366 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020367 cplen = inlen;
20368 idx = 0;
20369 for (p = fromstr; *p != NUL; p += fromlen)
20370 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020371 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020372 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020373 {
20374 for (p = tostr; *p != NUL; p += tolen)
20375 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020376 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020377 if (idx-- == 0)
20378 {
20379 cplen = tolen;
20380 cpstr = p;
20381 break;
20382 }
20383 }
20384 if (*p == NUL) /* tostr is shorter than fromstr */
20385 goto error;
20386 break;
20387 }
20388 ++idx;
20389 }
20390
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020391 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020392 {
20393 /* Check that fromstr and tostr have the same number of
20394 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020395 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020396 first = FALSE;
20397 for (p = tostr; *p != NUL; p += tolen)
20398 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020399 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020400 --idx;
20401 }
20402 if (idx != 0)
20403 goto error;
20404 }
20405
Bram Moolenaarcde88542015-08-11 19:14:00 +020020406 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020407 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020408 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020409
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020410 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020411 }
20412 else
20413#endif
20414 {
20415 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020416 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020417 if (p != NULL)
20418 ga_append(&ga, tostr[p - fromstr]);
20419 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020420 ga_append(&ga, *in_str);
20421 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020422 }
20423 }
20424
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020425 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020426 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020427 ga_append(&ga, NUL);
20428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020429 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020430}
20431
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020432#ifdef FEAT_FLOAT
20433/*
20434 * "trunc({float})" function
20435 */
20436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020437f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020438{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020439 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020440
20441 rettv->v_type = VAR_FLOAT;
20442 if (get_float_arg(argvars, &f) == OK)
20443 /* trunc() is not in C90, use floor() or ceil() instead. */
20444 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20445 else
20446 rettv->vval.v_float = 0.0;
20447}
20448#endif
20449
Bram Moolenaar8299df92004-07-10 09:47:34 +000020450/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020451 * "type(expr)" function
20452 */
20453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020454f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020455{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020456 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020457
20458 switch (argvars[0].v_type)
20459 {
20460 case VAR_NUMBER: n = 0; break;
20461 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020462 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020463 case VAR_FUNC: n = 2; break;
20464 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020465 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020466 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020467 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020468 if (argvars[0].vval.v_number == VVAL_FALSE
20469 || argvars[0].vval.v_number == VVAL_TRUE)
20470 n = 6;
20471 else
20472 n = 7;
20473 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020474 case VAR_JOB: n = 8; break;
20475 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020476 case VAR_UNKNOWN:
20477 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20478 n = -1;
20479 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020480 }
20481 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020482}
20483
20484/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020485 * "undofile(name)" function
20486 */
20487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020488f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020489{
20490 rettv->v_type = VAR_STRING;
20491#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020492 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020493 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020494
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020495 if (*fname == NUL)
20496 {
20497 /* If there is no file name there will be no undo file. */
20498 rettv->vval.v_string = NULL;
20499 }
20500 else
20501 {
20502 char_u *ffname = FullName_save(fname, FALSE);
20503
20504 if (ffname != NULL)
20505 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20506 vim_free(ffname);
20507 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020508 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020509#else
20510 rettv->vval.v_string = NULL;
20511#endif
20512}
20513
20514/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020515 * "undotree()" function
20516 */
20517 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020518f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020519{
20520 if (rettv_dict_alloc(rettv) == OK)
20521 {
20522 dict_T *dict = rettv->vval.v_dict;
20523 list_T *list;
20524
Bram Moolenaar730cde92010-06-27 05:18:54 +020020525 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020526 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020527 dict_add_nr_str(dict, "save_last",
20528 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020529 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20530 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020531 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020532
20533 list = list_alloc();
20534 if (list != NULL)
20535 {
20536 u_eval_tree(curbuf->b_u_oldhead, list);
20537 dict_add_list(dict, "entries", list);
20538 }
20539 }
20540}
20541
20542/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020543 * "values(dict)" function
20544 */
20545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020546f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020547{
20548 dict_list(argvars, rettv, 1);
20549}
20550
20551/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020552 * "virtcol(string)" function
20553 */
20554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020555f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556{
20557 colnr_T vcol = 0;
20558 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020559 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020561 fp = var2fpos(&argvars[0], FALSE, &fnum);
20562 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20563 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020564 {
20565 getvvcol(curwin, fp, NULL, NULL, &vcol);
20566 ++vcol;
20567 }
20568
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020569 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020570}
20571
20572/*
20573 * "visualmode()" function
20574 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020576f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020577{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020578 char_u str[2];
20579
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 str[0] = curbuf->b_visual_mode_eval;
20582 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020583 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020584
20585 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020586 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588}
20589
20590/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020591 * "wildmenumode()" function
20592 */
20593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020594f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020595{
20596#ifdef FEAT_WILDMENU
20597 if (wild_menu_showing)
20598 rettv->vval.v_number = 1;
20599#endif
20600}
20601
20602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 * "winbufnr(nr)" function
20604 */
20605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020606f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607{
20608 win_T *wp;
20609
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020610 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020612 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020614 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615}
20616
20617/*
20618 * "wincol()" function
20619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020621f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020622{
20623 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020624 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020625}
20626
20627/*
20628 * "winheight(nr)" function
20629 */
20630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020631f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632{
20633 win_T *wp;
20634
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020635 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020637 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020639 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020640}
20641
20642/*
20643 * "winline()" function
20644 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020645 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020646f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020647{
20648 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020649 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020650}
20651
20652/*
20653 * "winnr()" function
20654 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020656f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657{
20658 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020659
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020661 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664}
20665
20666/*
20667 * "winrestcmd()" function
20668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020670f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671{
20672#ifdef FEAT_WINDOWS
20673 win_T *wp;
20674 int winnr = 1;
20675 garray_T ga;
20676 char_u buf[50];
20677
20678 ga_init2(&ga, (int)sizeof(char), 70);
20679 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20680 {
20681 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20682 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20684 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 ++winnr;
20686 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020687 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020688
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020689 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020690#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020691 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020693 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694}
20695
20696/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020697 * "winrestview()" function
20698 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020700f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020701{
20702 dict_T *dict;
20703
20704 if (argvars[0].v_type != VAR_DICT
20705 || (dict = argvars[0].vval.v_dict) == NULL)
20706 EMSG(_(e_invarg));
20707 else
20708 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020709 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20710 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20711 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20712 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020713#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020714 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20715 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020716#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020717 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20718 {
20719 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20720 curwin->w_set_curswant = FALSE;
20721 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020722
Bram Moolenaar82c25852014-05-28 16:47:16 +020020723 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20724 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020725#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020726 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20727 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020728#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020729 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20730 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20731 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20732 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020733
20734 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020735 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020736# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020737 win_new_width(curwin, W_WIDTH(curwin));
20738# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020739 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020740
Bram Moolenaarb851a962014-10-31 15:45:52 +010020741 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020742 curwin->w_topline = 1;
20743 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20744 curwin->w_topline = curbuf->b_ml.ml_line_count;
20745#ifdef FEAT_DIFF
20746 check_topfill(curwin, TRUE);
20747#endif
20748 }
20749}
20750
20751/*
20752 * "winsaveview()" function
20753 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020754 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020755f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020756{
20757 dict_T *dict;
20758
Bram Moolenaara800b422010-06-27 01:15:55 +020020759 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020760 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020761 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020762
20763 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20764 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20765#ifdef FEAT_VIRTUALEDIT
20766 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20767#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020768 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020769 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20770
20771 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20772#ifdef FEAT_DIFF
20773 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20774#endif
20775 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20776 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20777}
20778
20779/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020780 * "winwidth(nr)" function
20781 */
20782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020783f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020784{
20785 win_T *wp;
20786
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020787 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020789 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020791#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020792 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020794 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795#endif
20796}
20797
Bram Moolenaar071d4272004-06-13 20:20:40 +000020798/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020799 * "wordcount()" function
20800 */
20801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020802f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020803{
20804 if (rettv_dict_alloc(rettv) == FAIL)
20805 return;
20806 cursor_pos_info(rettv->vval.v_dict);
20807}
20808
20809/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020810 * Write list of strings to file
20811 */
20812 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020813write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020814{
20815 listitem_T *li;
20816 int c;
20817 int ret = OK;
20818 char_u *s;
20819
20820 for (li = list->lv_first; li != NULL; li = li->li_next)
20821 {
20822 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20823 {
20824 if (*s == '\n')
20825 c = putc(NUL, fd);
20826 else
20827 c = putc(*s, fd);
20828 if (c == EOF)
20829 {
20830 ret = FAIL;
20831 break;
20832 }
20833 }
20834 if (!binary || li->li_next != NULL)
20835 if (putc('\n', fd) == EOF)
20836 {
20837 ret = FAIL;
20838 break;
20839 }
20840 if (ret == FAIL)
20841 {
20842 EMSG(_(e_write));
20843 break;
20844 }
20845 }
20846 return ret;
20847}
20848
20849/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020850 * "writefile()" function
20851 */
20852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020853f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020854{
20855 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020856 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020857 char_u *fname;
20858 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020859 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020860
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020861 if (check_restricted() || check_secure())
20862 return;
20863
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020864 if (argvars[0].v_type != VAR_LIST)
20865 {
20866 EMSG2(_(e_listarg), "writefile()");
20867 return;
20868 }
20869 if (argvars[0].vval.v_list == NULL)
20870 return;
20871
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020872 if (argvars[2].v_type != VAR_UNKNOWN)
20873 {
20874 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20875 binary = TRUE;
20876 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20877 append = TRUE;
20878 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020879
20880 /* Always open the file in binary mode, library functions have a mind of
20881 * their own about CR-LF conversion. */
20882 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020883 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20884 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020885 {
20886 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20887 ret = -1;
20888 }
20889 else
20890 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020891 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20892 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020893 fclose(fd);
20894 }
20895
20896 rettv->vval.v_number = ret;
20897}
20898
20899/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020900 * "xor(expr, expr)" function
20901 */
20902 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020903f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020904{
20905 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20906 ^ get_tv_number_chk(&argvars[1], NULL);
20907}
20908
20909
20910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020911 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020912 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020913 */
20914 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020915var2fpos(
20916 typval_T *varp,
20917 int dollar_lnum, /* TRUE when $ is last line */
20918 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020919{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020920 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020921 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020922 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020923
Bram Moolenaara5525202006-03-02 22:52:09 +000020924 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020925 if (varp->v_type == VAR_LIST)
20926 {
20927 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020928 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020929 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020930 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020931
20932 l = varp->vval.v_list;
20933 if (l == NULL)
20934 return NULL;
20935
20936 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020937 pos.lnum = list_find_nr(l, 0L, &error);
20938 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020939 return NULL; /* invalid line number */
20940
20941 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020942 pos.col = list_find_nr(l, 1L, &error);
20943 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020944 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020945 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020946
20947 /* We accept "$" for the column number: last column. */
20948 li = list_find(l, 1L);
20949 if (li != NULL && li->li_tv.v_type == VAR_STRING
20950 && li->li_tv.vval.v_string != NULL
20951 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20952 pos.col = len + 1;
20953
Bram Moolenaara5525202006-03-02 22:52:09 +000020954 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020955 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020956 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020957 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020958
Bram Moolenaara5525202006-03-02 22:52:09 +000020959#ifdef FEAT_VIRTUALEDIT
20960 /* Get the virtual offset. Defaults to zero. */
20961 pos.coladd = list_find_nr(l, 2L, &error);
20962 if (error)
20963 pos.coladd = 0;
20964#endif
20965
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020966 return &pos;
20967 }
20968
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020969 name = get_tv_string_chk(varp);
20970 if (name == NULL)
20971 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020972 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020973 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020974 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20975 {
20976 if (VIsual_active)
20977 return &VIsual;
20978 return &curwin->w_cursor;
20979 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020980 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020981 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020982 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020983 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20984 return NULL;
20985 return pp;
20986 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020987
20988#ifdef FEAT_VIRTUALEDIT
20989 pos.coladd = 0;
20990#endif
20991
Bram Moolenaar477933c2007-07-17 14:32:23 +000020992 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020993 {
20994 pos.col = 0;
20995 if (name[1] == '0') /* "w0": first visible line */
20996 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020997 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020998 pos.lnum = curwin->w_topline;
20999 return &pos;
21000 }
21001 else if (name[1] == '$') /* "w$": last visible line */
21002 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021003 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021004 pos.lnum = curwin->w_botline - 1;
21005 return &pos;
21006 }
21007 }
21008 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021009 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021010 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021011 {
21012 pos.lnum = curbuf->b_ml.ml_line_count;
21013 pos.col = 0;
21014 }
21015 else
21016 {
21017 pos.lnum = curwin->w_cursor.lnum;
21018 pos.col = (colnr_T)STRLEN(ml_get_curline());
21019 }
21020 return &pos;
21021 }
21022 return NULL;
21023}
21024
21025/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021026 * Convert list in "arg" into a position and optional file number.
21027 * When "fnump" is NULL there is no file number, only 3 items.
21028 * Note that the column is passed on as-is, the caller may want to decrement
21029 * it to use 1 for the first column.
21030 * Return FAIL when conversion is not possible, doesn't check the position for
21031 * validity.
21032 */
21033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021034list2fpos(
21035 typval_T *arg,
21036 pos_T *posp,
21037 int *fnump,
21038 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021039{
21040 list_T *l = arg->vval.v_list;
21041 long i = 0;
21042 long n;
21043
Bram Moolenaar493c1782014-05-28 14:34:46 +020021044 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21045 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021046 if (arg->v_type != VAR_LIST
21047 || l == NULL
21048 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021049 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021050 return FAIL;
21051
21052 if (fnump != NULL)
21053 {
21054 n = list_find_nr(l, i++, NULL); /* fnum */
21055 if (n < 0)
21056 return FAIL;
21057 if (n == 0)
21058 n = curbuf->b_fnum; /* current buffer */
21059 *fnump = n;
21060 }
21061
21062 n = list_find_nr(l, i++, NULL); /* lnum */
21063 if (n < 0)
21064 return FAIL;
21065 posp->lnum = n;
21066
21067 n = list_find_nr(l, i++, NULL); /* col */
21068 if (n < 0)
21069 return FAIL;
21070 posp->col = n;
21071
21072#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021073 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021074 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021075 posp->coladd = 0;
21076 else
21077 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021078#endif
21079
Bram Moolenaar493c1782014-05-28 14:34:46 +020021080 if (curswantp != NULL)
21081 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21082
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021083 return OK;
21084}
21085
21086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021087 * Get the length of an environment variable name.
21088 * Advance "arg" to the first character after the name.
21089 * Return 0 for error.
21090 */
21091 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021092get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021093{
21094 char_u *p;
21095 int len;
21096
21097 for (p = *arg; vim_isIDc(*p); ++p)
21098 ;
21099 if (p == *arg) /* no name found */
21100 return 0;
21101
21102 len = (int)(p - *arg);
21103 *arg = p;
21104 return len;
21105}
21106
21107/*
21108 * Get the length of the name of a function or internal variable.
21109 * "arg" is advanced to the first non-white character after the name.
21110 * Return 0 if something is wrong.
21111 */
21112 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021113get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114{
21115 char_u *p;
21116 int len;
21117
21118 /* Find the end of the name. */
21119 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021120 {
21121 if (*p == ':')
21122 {
21123 /* "s:" is start of "s:var", but "n:" is not and can be used in
21124 * slice "[n:]". Also "xx:" is not a namespace. */
21125 len = (int)(p - *arg);
21126 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21127 || len > 1)
21128 break;
21129 }
21130 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021131 if (p == *arg) /* no name found */
21132 return 0;
21133
21134 len = (int)(p - *arg);
21135 *arg = skipwhite(p);
21136
21137 return len;
21138}
21139
21140/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021141 * Get the length of the name of a variable or function.
21142 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021143 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021144 * Return -1 if curly braces expansion failed.
21145 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021146 * If the name contains 'magic' {}'s, expand them and return the
21147 * expanded name in an allocated string via 'alias' - caller must free.
21148 */
21149 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021150get_name_len(
21151 char_u **arg,
21152 char_u **alias,
21153 int evaluate,
21154 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021155{
21156 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021157 char_u *p;
21158 char_u *expr_start;
21159 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021160
21161 *alias = NULL; /* default to no alias */
21162
21163 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21164 && (*arg)[2] == (int)KE_SNR)
21165 {
21166 /* hard coded <SNR>, already translated */
21167 *arg += 3;
21168 return get_id_len(arg) + 3;
21169 }
21170 len = eval_fname_script(*arg);
21171 if (len > 0)
21172 {
21173 /* literal "<SID>", "s:" or "<SNR>" */
21174 *arg += len;
21175 }
21176
Bram Moolenaar071d4272004-06-13 20:20:40 +000021177 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021178 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021179 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021180 p = find_name_end(*arg, &expr_start, &expr_end,
21181 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 if (expr_start != NULL)
21183 {
21184 char_u *temp_string;
21185
21186 if (!evaluate)
21187 {
21188 len += (int)(p - *arg);
21189 *arg = skipwhite(p);
21190 return len;
21191 }
21192
21193 /*
21194 * Include any <SID> etc in the expanded string:
21195 * Thus the -len here.
21196 */
21197 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21198 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021199 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021200 *alias = temp_string;
21201 *arg = skipwhite(p);
21202 return (int)STRLEN(temp_string);
21203 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204
21205 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021206 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021207 EMSG2(_(e_invexpr2), *arg);
21208
21209 return len;
21210}
21211
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021212/*
21213 * Find the end of a variable or function name, taking care of magic braces.
21214 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21215 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021216 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021217 * Return a pointer to just after the name. Equal to "arg" if there is no
21218 * valid name.
21219 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021220 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021221find_name_end(
21222 char_u *arg,
21223 char_u **expr_start,
21224 char_u **expr_end,
21225 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 int mb_nest = 0;
21228 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021229 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021230 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021232 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021234 *expr_start = NULL;
21235 *expr_end = NULL;
21236 }
21237
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021238 /* Quick check for valid starting character. */
21239 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21240 return arg;
21241
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021242 for (p = arg; *p != NUL
21243 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021244 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021245 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021246 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021247 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021248 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021249 if (*p == '\'')
21250 {
21251 /* skip over 'string' to avoid counting [ and ] inside it. */
21252 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21253 ;
21254 if (*p == NUL)
21255 break;
21256 }
21257 else if (*p == '"')
21258 {
21259 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21260 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21261 if (*p == '\\' && p[1] != NUL)
21262 ++p;
21263 if (*p == NUL)
21264 break;
21265 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021266 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21267 {
21268 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021269 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021270 len = (int)(p - arg);
21271 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021272 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021273 break;
21274 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021275
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021276 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 if (*p == '[')
21279 ++br_nest;
21280 else if (*p == ']')
21281 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021284 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021285 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021286 if (*p == '{')
21287 {
21288 mb_nest++;
21289 if (expr_start != NULL && *expr_start == NULL)
21290 *expr_start = p;
21291 }
21292 else if (*p == '}')
21293 {
21294 mb_nest--;
21295 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21296 *expr_end = p;
21297 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021298 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021299 }
21300
21301 return p;
21302}
21303
21304/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021305 * Expands out the 'magic' {}'s in a variable/function name.
21306 * Note that this can call itself recursively, to deal with
21307 * constructs like foo{bar}{baz}{bam}
21308 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21309 * "in_start" ^
21310 * "expr_start" ^
21311 * "expr_end" ^
21312 * "in_end" ^
21313 *
21314 * Returns a new allocated string, which the caller must free.
21315 * Returns NULL for failure.
21316 */
21317 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021318make_expanded_name(
21319 char_u *in_start,
21320 char_u *expr_start,
21321 char_u *expr_end,
21322 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021323{
21324 char_u c1;
21325 char_u *retval = NULL;
21326 char_u *temp_result;
21327 char_u *nextcmd = NULL;
21328
21329 if (expr_end == NULL || in_end == NULL)
21330 return NULL;
21331 *expr_start = NUL;
21332 *expr_end = NUL;
21333 c1 = *in_end;
21334 *in_end = NUL;
21335
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021336 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021337 if (temp_result != NULL && nextcmd == NULL)
21338 {
21339 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21340 + (in_end - expr_end) + 1));
21341 if (retval != NULL)
21342 {
21343 STRCPY(retval, in_start);
21344 STRCAT(retval, temp_result);
21345 STRCAT(retval, expr_end + 1);
21346 }
21347 }
21348 vim_free(temp_result);
21349
21350 *in_end = c1; /* put char back for error messages */
21351 *expr_start = '{';
21352 *expr_end = '}';
21353
21354 if (retval != NULL)
21355 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021356 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021357 if (expr_start != NULL)
21358 {
21359 /* Further expansion! */
21360 temp_result = make_expanded_name(retval, expr_start,
21361 expr_end, temp_result);
21362 vim_free(retval);
21363 retval = temp_result;
21364 }
21365 }
21366
21367 return retval;
21368}
21369
21370/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021371 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021372 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021373 */
21374 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021375eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021376{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021377 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21378}
21379
21380/*
21381 * Return TRUE if character "c" can be used as the first character in a
21382 * variable or function name (excluding '{' and '}').
21383 */
21384 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021385eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021386{
21387 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021388}
21389
21390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021391 * Set number v: variable to "val".
21392 */
21393 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021394set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021396 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021397}
21398
21399/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021400 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401 */
21402 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021403get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021405 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406}
21407
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021408/*
21409 * Get string v: variable value. Uses a static buffer, can only be used once.
21410 */
21411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021412get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021413{
21414 return get_tv_string(&vimvars[idx].vv_tv);
21415}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021416
Bram Moolenaar071d4272004-06-13 20:20:40 +000021417/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021418 * Get List v: variable value. Caller must take care of reference count when
21419 * needed.
21420 */
21421 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021422get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021423{
21424 return vimvars[idx].vv_list;
21425}
21426
21427/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021428 * Set v:char to character "c".
21429 */
21430 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021431set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021432{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021433 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021434
21435#ifdef FEAT_MBYTE
21436 if (has_mbyte)
21437 buf[(*mb_char2bytes)(c, buf)] = NUL;
21438 else
21439#endif
21440 {
21441 buf[0] = c;
21442 buf[1] = NUL;
21443 }
21444 set_vim_var_string(VV_CHAR, buf, -1);
21445}
21446
21447/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021448 * Set v:count to "count" and v:count1 to "count1".
21449 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 */
21451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452set_vcount(
21453 long count,
21454 long count1,
21455 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021456{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021457 if (set_prevcount)
21458 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021459 vimvars[VV_COUNT].vv_nr = count;
21460 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021461}
21462
21463/*
21464 * Set string v: variable to a copy of "val".
21465 */
21466 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021467set_vim_var_string(
21468 int idx,
21469 char_u *val,
21470 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021471{
Bram Moolenaara542c682016-01-31 16:28:04 +010021472 clear_tv(&vimvars[idx].vv_di.di_tv);
21473 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021474 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021475 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021477 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021479 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021480}
21481
21482/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021483 * Set List v: variable to "val".
21484 */
21485 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021486set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021487{
Bram Moolenaara542c682016-01-31 16:28:04 +010021488 clear_tv(&vimvars[idx].vv_di.di_tv);
21489 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021490 vimvars[idx].vv_list = val;
21491 if (val != NULL)
21492 ++val->lv_refcount;
21493}
21494
21495/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021496 * Set Dictionary v: variable to "val".
21497 */
21498 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021500{
21501 int todo;
21502 hashitem_T *hi;
21503
Bram Moolenaara542c682016-01-31 16:28:04 +010021504 clear_tv(&vimvars[idx].vv_di.di_tv);
21505 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021506 vimvars[idx].vv_dict = val;
21507 if (val != NULL)
21508 {
21509 ++val->dv_refcount;
21510
21511 /* Set readonly */
21512 todo = (int)val->dv_hashtab.ht_used;
21513 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21514 {
21515 if (HASHITEM_EMPTY(hi))
21516 continue;
21517 --todo;
21518 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21519 }
21520 }
21521}
21522
21523/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021524 * Set v:register if needed.
21525 */
21526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021527set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021528{
21529 char_u regname;
21530
21531 if (c == 0 || c == ' ')
21532 regname = '"';
21533 else
21534 regname = c;
21535 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021536 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 set_vim_var_string(VV_REG, &regname, 1);
21538}
21539
21540/*
21541 * Get or set v:exception. If "oldval" == NULL, return the current value.
21542 * Otherwise, restore the value to "oldval" and return NULL.
21543 * Must always be called in pairs to save and restore v:exception! Does not
21544 * take care of memory allocations.
21545 */
21546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021547v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021548{
21549 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021550 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021551
Bram Moolenaare9a41262005-01-15 22:18:47 +000021552 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021553 return NULL;
21554}
21555
21556/*
21557 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21558 * Otherwise, restore the value to "oldval" and return NULL.
21559 * Must always be called in pairs to save and restore v:throwpoint! Does not
21560 * take care of memory allocations.
21561 */
21562 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021563v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021564{
21565 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021566 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021567
Bram Moolenaare9a41262005-01-15 22:18:47 +000021568 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021569 return NULL;
21570}
21571
21572#if defined(FEAT_AUTOCMD) || defined(PROTO)
21573/*
21574 * Set v:cmdarg.
21575 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21576 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21577 * Must always be called in pairs!
21578 */
21579 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021580set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581{
21582 char_u *oldval;
21583 char_u *newval;
21584 unsigned len;
21585
Bram Moolenaare9a41262005-01-15 22:18:47 +000021586 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021587 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021589 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021590 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021591 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021592 }
21593
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021594 if (eap->force_bin == FORCE_BIN)
21595 len = 6;
21596 else if (eap->force_bin == FORCE_NOBIN)
21597 len = 8;
21598 else
21599 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021600
21601 if (eap->read_edit)
21602 len += 7;
21603
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021604 if (eap->force_ff != 0)
21605 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21606# ifdef FEAT_MBYTE
21607 if (eap->force_enc != 0)
21608 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021609 if (eap->bad_char != 0)
21610 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021611# endif
21612
21613 newval = alloc(len + 1);
21614 if (newval == NULL)
21615 return NULL;
21616
21617 if (eap->force_bin == FORCE_BIN)
21618 sprintf((char *)newval, " ++bin");
21619 else if (eap->force_bin == FORCE_NOBIN)
21620 sprintf((char *)newval, " ++nobin");
21621 else
21622 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021623
21624 if (eap->read_edit)
21625 STRCAT(newval, " ++edit");
21626
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021627 if (eap->force_ff != 0)
21628 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21629 eap->cmd + eap->force_ff);
21630# ifdef FEAT_MBYTE
21631 if (eap->force_enc != 0)
21632 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21633 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021634 if (eap->bad_char == BAD_KEEP)
21635 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21636 else if (eap->bad_char == BAD_DROP)
21637 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21638 else if (eap->bad_char != 0)
21639 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021640# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021641 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021642 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643}
21644#endif
21645
21646/*
21647 * Get the value of internal variable "name".
21648 * Return OK or FAIL.
21649 */
21650 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021651get_var_tv(
21652 char_u *name,
21653 int len, /* length of "name" */
21654 typval_T *rettv, /* NULL when only checking existence */
21655 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21656 int verbose, /* may give error message */
21657 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658{
21659 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021660 typval_T *tv = NULL;
21661 typval_T atv;
21662 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021663 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021664
21665 /* truncate the name, so that we can use strcmp() */
21666 cc = name[len];
21667 name[len] = NUL;
21668
21669 /*
21670 * Check for "b:changedtick".
21671 */
21672 if (STRCMP(name, "b:changedtick") == 0)
21673 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021674 atv.v_type = VAR_NUMBER;
21675 atv.vval.v_number = curbuf->b_changedtick;
21676 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021677 }
21678
21679 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021680 * Check for user-defined variables.
21681 */
21682 else
21683 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021684 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021685 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021686 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021687 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021688 if (dip != NULL)
21689 *dip = v;
21690 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021691 }
21692
Bram Moolenaare9a41262005-01-15 22:18:47 +000021693 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021695 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021696 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 ret = FAIL;
21698 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021699 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021700 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021701
21702 name[len] = cc;
21703
21704 return ret;
21705}
21706
21707/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021708 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21709 * Also handle function call with Funcref variable: func(expr)
21710 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21711 */
21712 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021713handle_subscript(
21714 char_u **arg,
21715 typval_T *rettv,
21716 int evaluate, /* do more than finding the end */
21717 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021718{
21719 int ret = OK;
21720 dict_T *selfdict = NULL;
21721 char_u *s;
21722 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021723 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021724
21725 while (ret == OK
21726 && (**arg == '['
21727 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021728 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21729 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021730 && !vim_iswhite(*(*arg - 1)))
21731 {
21732 if (**arg == '(')
21733 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021734 partial_T *pt = NULL;
21735
Bram Moolenaard9fba312005-06-26 22:34:35 +000021736 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021737 if (evaluate)
21738 {
21739 functv = *rettv;
21740 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021741
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021742 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021743 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021744 {
21745 pt = functv.vval.v_partial;
21746 s = pt->pt_name;
21747 }
21748 else
21749 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021750 }
21751 else
21752 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021753 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021754 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021755 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021756
21757 /* Clear the funcref afterwards, so that deleting it while
21758 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021759 if (evaluate)
21760 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021761
21762 /* Stop the expression evaluation when immediately aborting on
21763 * error, or when an interrupt occurred or an exception was thrown
21764 * but not caught. */
21765 if (aborting())
21766 {
21767 if (ret == OK)
21768 clear_tv(rettv);
21769 ret = FAIL;
21770 }
21771 dict_unref(selfdict);
21772 selfdict = NULL;
21773 }
21774 else /* **arg == '[' || **arg == '.' */
21775 {
21776 dict_unref(selfdict);
21777 if (rettv->v_type == VAR_DICT)
21778 {
21779 selfdict = rettv->vval.v_dict;
21780 if (selfdict != NULL)
21781 ++selfdict->dv_refcount;
21782 }
21783 else
21784 selfdict = NULL;
21785 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21786 {
21787 clear_tv(rettv);
21788 ret = FAIL;
21789 }
21790 }
21791 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021792
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021793 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21794 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021795 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021796 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21797 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021798 char_u *tofree = NULL;
21799 ufunc_T *fp;
21800 char_u fname_buf[FLEN_FIXED + 1];
21801 int error;
21802
21803 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021804 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021805 fp = find_func(fname);
21806 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021807
21808 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021809 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021810 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021811 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21812
21813 if (pt != NULL)
21814 {
21815 pt->pt_refcount = 1;
21816 pt->pt_dict = selfdict;
21817 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021818 if (rettv->v_type == VAR_FUNC)
21819 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021820 /* Just a function: Take over the function name and use
21821 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021822 pt->pt_name = rettv->vval.v_string;
21823 }
21824 else
21825 {
21826 partial_T *ret_pt = rettv->vval.v_partial;
21827 int i;
21828
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021829 /* Partial: copy the function name, use selfdict and copy
21830 * args. Can't take over name or args, the partial might
21831 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021832 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021833 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021834 if (ret_pt->pt_argc > 0)
21835 {
21836 pt->pt_argv = (typval_T *)alloc(
21837 sizeof(typval_T) * ret_pt->pt_argc);
21838 if (pt->pt_argv == NULL)
21839 /* out of memory: drop the arguments */
21840 pt->pt_argc = 0;
21841 else
21842 {
21843 pt->pt_argc = ret_pt->pt_argc;
21844 for (i = 0; i < pt->pt_argc; i++)
21845 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21846 }
21847 }
21848 partial_unref(ret_pt);
21849 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021850 rettv->v_type = VAR_PARTIAL;
21851 rettv->vval.v_partial = pt;
21852 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021853 }
21854 }
21855
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021856 dict_unref(selfdict);
21857 return ret;
21858}
21859
21860/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021861 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021862 * value).
21863 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021864 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021865alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021866{
Bram Moolenaar33570922005-01-25 22:26:29 +000021867 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021868}
21869
21870/*
21871 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021872 * The string "s" must have been allocated, it is consumed.
21873 * Return NULL for out of memory, the variable otherwise.
21874 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021875 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021876alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021877{
Bram Moolenaar33570922005-01-25 22:26:29 +000021878 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021880 rettv = alloc_tv();
21881 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021882 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021883 rettv->v_type = VAR_STRING;
21884 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021885 }
21886 else
21887 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021888 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021889}
21890
21891/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021892 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021893 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021895free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021896{
21897 if (varp != NULL)
21898 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021899 switch (varp->v_type)
21900 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021901 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021902 func_unref(varp->vval.v_string);
21903 /*FALLTHROUGH*/
21904 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021905 vim_free(varp->vval.v_string);
21906 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021907 case VAR_PARTIAL:
21908 partial_unref(varp->vval.v_partial);
21909 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021910 case VAR_LIST:
21911 list_unref(varp->vval.v_list);
21912 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021913 case VAR_DICT:
21914 dict_unref(varp->vval.v_dict);
21915 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021916 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021917#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021918 job_unref(varp->vval.v_job);
21919 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021920#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021921 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021922#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021923 channel_unref(varp->vval.v_channel);
21924 break;
21925#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021926 case VAR_NUMBER:
21927 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021928 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021929 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021930 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021931 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021932 vim_free(varp);
21933 }
21934}
21935
21936/*
21937 * Free the memory for a variable value and set the value to NULL or 0.
21938 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021939 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021940clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021941{
21942 if (varp != NULL)
21943 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021944 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021946 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021947 func_unref(varp->vval.v_string);
21948 /*FALLTHROUGH*/
21949 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 vim_free(varp->vval.v_string);
21951 varp->vval.v_string = NULL;
21952 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021953 case VAR_PARTIAL:
21954 partial_unref(varp->vval.v_partial);
21955 varp->vval.v_partial = NULL;
21956 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021957 case VAR_LIST:
21958 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021959 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021960 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021961 case VAR_DICT:
21962 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021963 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021964 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021965 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021966 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 varp->vval.v_number = 0;
21968 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021969 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021970#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021971 varp->vval.v_float = 0.0;
21972 break;
21973#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021974 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021975#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021976 job_unref(varp->vval.v_job);
21977 varp->vval.v_job = NULL;
21978#endif
21979 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021980 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021981#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021982 channel_unref(varp->vval.v_channel);
21983 varp->vval.v_channel = NULL;
21984#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021985 case VAR_UNKNOWN:
21986 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021988 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021989 }
21990}
21991
21992/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021993 * Set the value of a variable to NULL without freeing items.
21994 */
21995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021996init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021997{
21998 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021999 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022000}
22001
22002/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022003 * Get the number value of a variable.
22004 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022005 * For incompatible types, return 0.
22006 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22007 * caller of incompatible types: it sets *denote to TRUE if "denote"
22008 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022009 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022010 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022011get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022012{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022013 int error = FALSE;
22014
22015 return get_tv_number_chk(varp, &error); /* return 0L on error */
22016}
22017
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022018 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022019get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022020{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022021 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022022
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022023 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022024 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022025 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022026 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022027 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022028#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022029 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022030 break;
22031#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022032 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022033 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022034 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022035 break;
22036 case VAR_STRING:
22037 if (varp->vval.v_string != NULL)
22038 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022039 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022040 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022041 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022042 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022043 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022044 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022045 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022046 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022047 case VAR_SPECIAL:
22048 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22049 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022050 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022051#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022052 EMSG(_("E910: Using a Job as a Number"));
22053 break;
22054#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022055 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022056#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022057 EMSG(_("E913: Using a Channel as a Number"));
22058 break;
22059#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022060 case VAR_UNKNOWN:
22061 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022062 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022064 if (denote == NULL) /* useful for values that must be unsigned */
22065 n = -1;
22066 else
22067 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022068 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022069}
22070
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022071#ifdef FEAT_FLOAT
22072 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022073get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022074{
22075 switch (varp->v_type)
22076 {
22077 case VAR_NUMBER:
22078 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022079 case VAR_FLOAT:
22080 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022081 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022082 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022083 EMSG(_("E891: Using a Funcref as a Float"));
22084 break;
22085 case VAR_STRING:
22086 EMSG(_("E892: Using a String as a Float"));
22087 break;
22088 case VAR_LIST:
22089 EMSG(_("E893: Using a List as a Float"));
22090 break;
22091 case VAR_DICT:
22092 EMSG(_("E894: Using a Dictionary as a Float"));
22093 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022094 case VAR_SPECIAL:
22095 EMSG(_("E907: Using a special value as a Float"));
22096 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022097 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022098# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022099 EMSG(_("E911: Using a Job as a Float"));
22100 break;
22101# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022102 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022103# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022104 EMSG(_("E914: Using a Channel as a Float"));
22105 break;
22106# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022107 case VAR_UNKNOWN:
22108 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022109 break;
22110 }
22111 return 0;
22112}
22113#endif
22114
Bram Moolenaar071d4272004-06-13 20:20:40 +000022115/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022116 * Get the lnum from the first argument.
22117 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022118 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022119 */
22120 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022121get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022122{
Bram Moolenaar33570922005-01-25 22:26:29 +000022123 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022124 linenr_T lnum;
22125
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022126 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127 if (lnum == 0) /* no valid number, try using line() */
22128 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022129 rettv.v_type = VAR_NUMBER;
22130 f_line(argvars, &rettv);
22131 lnum = rettv.vval.v_number;
22132 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022133 }
22134 return lnum;
22135}
22136
22137/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022138 * Get the lnum from the first argument.
22139 * Also accepts "$", then "buf" is used.
22140 * Returns 0 on error.
22141 */
22142 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022143get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022144{
22145 if (argvars[0].v_type == VAR_STRING
22146 && argvars[0].vval.v_string != NULL
22147 && argvars[0].vval.v_string[0] == '$'
22148 && buf != NULL)
22149 return buf->b_ml.ml_line_count;
22150 return get_tv_number_chk(&argvars[0], NULL);
22151}
22152
22153/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022154 * Get the string value of a variable.
22155 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022156 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22157 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022158 * If the String variable has never been set, return an empty string.
22159 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022160 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22161 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022162 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022163 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022164get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022165{
22166 static char_u mybuf[NUMBUFLEN];
22167
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022168 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022169}
22170
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022171 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022174 char_u *res = get_tv_string_buf_chk(varp, buf);
22175
22176 return res != NULL ? res : (char_u *)"";
22177}
22178
Bram Moolenaar7d647822014-04-05 21:28:56 +020022179/*
22180 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22181 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022182 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022183get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022184{
22185 static char_u mybuf[NUMBUFLEN];
22186
22187 return get_tv_string_buf_chk(varp, mybuf);
22188}
22189
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022190 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022191get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022192{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022193 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022195 case VAR_NUMBER:
22196 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22197 return buf;
22198 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022199 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022200 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022201 break;
22202 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022203 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022204 break;
22205 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022206 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022207 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022208 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022209#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022210 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022211 break;
22212#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022213 case VAR_STRING:
22214 if (varp->vval.v_string != NULL)
22215 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022216 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022217 case VAR_SPECIAL:
22218 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22219 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022220 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022221#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022222 {
22223 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022224 char *status;
22225
22226 if (job == NULL)
22227 return (char_u *)"no process";
22228 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022229 : job->jv_status == JOB_ENDED ? "dead"
22230 : "run";
22231# ifdef UNIX
22232 vim_snprintf((char *)buf, NUMBUFLEN,
22233 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022234# elif defined(WIN32)
22235 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022236 "process %ld %s",
22237 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022238 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022239# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022240 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022241 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22242# endif
22243 return buf;
22244 }
22245#endif
22246 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022247 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022248#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022249 {
22250 channel_T *channel = varp->vval.v_channel;
22251 char *status = channel_status(channel);
22252
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022253 if (channel == NULL)
22254 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22255 else
22256 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022257 "channel %d %s", channel->ch_id, status);
22258 return buf;
22259 }
22260#endif
22261 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022262 case VAR_UNKNOWN:
22263 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022264 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022266 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267}
22268
22269/*
22270 * Find variable "name" in the list of variables.
22271 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022272 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022273 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022274 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022275 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022276 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022279 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022280 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022281
Bram Moolenaara7043832005-01-21 11:56:39 +000022282 ht = find_var_ht(name, &varname);
22283 if (htp != NULL)
22284 *htp = ht;
22285 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022286 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022287 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022288}
22289
22290/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022291 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022292 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022293 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022294 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022295find_var_in_ht(
22296 hashtab_T *ht,
22297 int htname,
22298 char_u *varname,
22299 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022300{
Bram Moolenaar33570922005-01-25 22:26:29 +000022301 hashitem_T *hi;
22302
22303 if (*varname == NUL)
22304 {
22305 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022306 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022307 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022308 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022309 case 'g': return &globvars_var;
22310 case 'v': return &vimvars_var;
22311 case 'b': return &curbuf->b_bufvar;
22312 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022313#ifdef FEAT_WINDOWS
22314 case 't': return &curtab->tp_winvar;
22315#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022316 case 'l': return current_funccal == NULL
22317 ? NULL : &current_funccal->l_vars_var;
22318 case 'a': return current_funccal == NULL
22319 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022320 }
22321 return NULL;
22322 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022323
22324 hi = hash_find(ht, varname);
22325 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022326 {
22327 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022328 * worked find the variable again. Don't auto-load a script if it was
22329 * loaded already, otherwise it would be loaded every time when
22330 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022331 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022332 {
22333 /* Note: script_autoload() may make "hi" invalid. It must either
22334 * be obtained again or not used. */
22335 if (!script_autoload(varname, FALSE) || aborting())
22336 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022337 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022338 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022339 if (HASHITEM_EMPTY(hi))
22340 return NULL;
22341 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022342 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022343}
22344
22345/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022346 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022347 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022348 * Set "varname" to the start of name without ':'.
22349 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022351find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022352{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022353 hashitem_T *hi;
22354
Bram Moolenaar73627d02015-08-11 15:46:09 +020022355 if (name[0] == NUL)
22356 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022357 if (name[1] != ':')
22358 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022359 /* The name must not start with a colon or #. */
22360 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022361 return NULL;
22362 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022363
22364 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022365 hi = hash_find(&compat_hashtab, name);
22366 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022367 return &compat_hashtab;
22368
Bram Moolenaar071d4272004-06-13 20:20:40 +000022369 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022370 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022371 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372 }
22373 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022374 if (*name == 'g') /* global variable */
22375 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022376 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22377 */
22378 if (vim_strchr(name + 2, ':') != NULL
22379 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022380 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022382 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022384 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022385#ifdef FEAT_WINDOWS
22386 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022387 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022388#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022389 if (*name == 'v') /* v: variable */
22390 return &vimvarht;
22391 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022392 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022394 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022395 if (*name == 's' /* script variable */
22396 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22397 return &SCRIPT_VARS(current_SID);
22398 return NULL;
22399}
22400
22401/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022402 * Get function call environment based on bactrace debug level
22403 */
22404 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022405get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022406{
22407 int i;
22408 funccall_T *funccal;
22409 funccall_T *temp_funccal;
22410
22411 funccal = current_funccal;
22412 if (debug_backtrace_level > 0)
22413 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022414 for (i = 0; i < debug_backtrace_level; i++)
22415 {
22416 temp_funccal = funccal->caller;
22417 if (temp_funccal)
22418 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022419 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022420 /* backtrace level overflow. reset to max */
22421 debug_backtrace_level = i;
22422 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022423 }
22424 return funccal;
22425}
22426
22427/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022428 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022429 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 * Returns NULL when it doesn't exist.
22431 */
22432 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022433get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434{
Bram Moolenaar33570922005-01-25 22:26:29 +000022435 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022436
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022437 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438 if (v == NULL)
22439 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022441}
22442
22443/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022445 * sourcing this script and when executing functions defined in the script.
22446 */
22447 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022448new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022449{
Bram Moolenaara7043832005-01-21 11:56:39 +000022450 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 hashtab_T *ht;
22452 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022453
Bram Moolenaar071d4272004-06-13 20:20:40 +000022454 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22455 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022456 /* Re-allocating ga_data means that an ht_array pointing to
22457 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022459 for (i = 1; i <= ga_scripts.ga_len; ++i)
22460 {
22461 ht = &SCRIPT_VARS(i);
22462 if (ht->ht_mask == HT_INIT_SIZE - 1)
22463 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022464 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022465 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022466 }
22467
Bram Moolenaar071d4272004-06-13 20:20:40 +000022468 while (ga_scripts.ga_len < id)
22469 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022470 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022471 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022472 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022473 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 }
22475 }
22476}
22477
22478/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022479 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22480 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 */
22482 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022483init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484{
Bram Moolenaar33570922005-01-25 22:26:29 +000022485 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022486 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022487 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022488 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022489 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 dict_var->di_tv.vval.v_dict = dict;
22491 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022492 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22494 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495}
22496
22497/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022498 * Unreference a dictionary initialized by init_var_dict().
22499 */
22500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022501unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022502{
22503 /* Now the dict needs to be freed if no one else is using it, go back to
22504 * normal reference counting. */
22505 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22506 dict_unref(dict);
22507}
22508
22509/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022510 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022511 * Frees all allocated variables and the value they contain.
22512 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022513 */
22514 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022515vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022516{
22517 vars_clear_ext(ht, TRUE);
22518}
22519
22520/*
22521 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22522 */
22523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022524vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525{
Bram Moolenaara7043832005-01-21 11:56:39 +000022526 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022527 hashitem_T *hi;
22528 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529
Bram Moolenaar33570922005-01-25 22:26:29 +000022530 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022531 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022532 for (hi = ht->ht_array; todo > 0; ++hi)
22533 {
22534 if (!HASHITEM_EMPTY(hi))
22535 {
22536 --todo;
22537
Bram Moolenaar33570922005-01-25 22:26:29 +000022538 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022539 * ht_array might change then. hash_clear() takes care of it
22540 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 v = HI2DI(hi);
22542 if (free_val)
22543 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022544 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022546 }
22547 }
22548 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022549 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550}
22551
Bram Moolenaara7043832005-01-21 11:56:39 +000022552/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 * Delete a variable from hashtab "ht" at item "hi".
22554 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022555 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022556 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022557delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022558{
Bram Moolenaar33570922005-01-25 22:26:29 +000022559 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022560
22561 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 clear_tv(&di->di_tv);
22563 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564}
22565
22566/*
22567 * List the value of one internal variable.
22568 */
22569 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022570list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022571{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022572 char_u *tofree;
22573 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022574 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022575
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022576 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022578 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022579 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580}
22581
Bram Moolenaar071d4272004-06-13 20:20:40 +000022582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022583list_one_var_a(
22584 char_u *prefix,
22585 char_u *name,
22586 int type,
22587 char_u *string,
22588 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589{
Bram Moolenaar31859182007-08-14 20:41:13 +000022590 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22591 msg_start();
22592 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022593 if (name != NULL) /* "a:" vars don't have a name stored */
22594 msg_puts(name);
22595 msg_putchar(' ');
22596 msg_advance(22);
22597 if (type == VAR_NUMBER)
22598 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022599 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022600 msg_putchar('*');
22601 else if (type == VAR_LIST)
22602 {
22603 msg_putchar('[');
22604 if (*string == '[')
22605 ++string;
22606 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022607 else if (type == VAR_DICT)
22608 {
22609 msg_putchar('{');
22610 if (*string == '{')
22611 ++string;
22612 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613 else
22614 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022615
Bram Moolenaar071d4272004-06-13 20:20:40 +000022616 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022617
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022618 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022619 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022620 if (*first)
22621 {
22622 msg_clr_eos();
22623 *first = FALSE;
22624 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022625}
22626
22627/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022628 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629 * If the variable already exists, the value is updated.
22630 * Otherwise the variable is created.
22631 */
22632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022633set_var(
22634 char_u *name,
22635 typval_T *tv,
22636 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637{
Bram Moolenaar33570922005-01-25 22:26:29 +000022638 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022639 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022641
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022642 ht = find_var_ht(name, &varname);
22643 if (ht == NULL || *varname == NUL)
22644 {
22645 EMSG2(_(e_illvar), name);
22646 return;
22647 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022648 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022649
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022650 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22651 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022652 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022653
Bram Moolenaar33570922005-01-25 22:26:29 +000022654 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022656 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022657 if (var_check_ro(v->di_flags, name, FALSE)
22658 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022660
22661 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022662 * Handle setting internal v: variables separately where needed to
22663 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 */
22665 if (ht == &vimvarht)
22666 {
22667 if (v->di_tv.v_type == VAR_STRING)
22668 {
22669 vim_free(v->di_tv.vval.v_string);
22670 if (copy || tv->v_type != VAR_STRING)
22671 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22672 else
22673 {
22674 /* Take over the string to avoid an extra alloc/free. */
22675 v->di_tv.vval.v_string = tv->vval.v_string;
22676 tv->vval.v_string = NULL;
22677 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022678 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022679 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022680 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022681 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022683 if (STRCMP(varname, "searchforward") == 0)
22684 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022685#ifdef FEAT_SEARCH_EXTRA
22686 else if (STRCMP(varname, "hlsearch") == 0)
22687 {
22688 no_hlsearch = !v->di_tv.vval.v_number;
22689 redraw_all_later(SOME_VALID);
22690 }
22691#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022692 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022693 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022694 else if (v->di_tv.v_type != tv->v_type)
22695 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022696 }
22697
22698 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022699 }
22700 else /* add a new variable */
22701 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022702 /* Can't add "v:" variable. */
22703 if (ht == &vimvarht)
22704 {
22705 EMSG2(_(e_illvar), name);
22706 return;
22707 }
22708
Bram Moolenaar92124a32005-06-17 22:03:40 +000022709 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022710 if (!valid_varname(varname))
22711 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022712
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022713 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22714 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022715 if (v == NULL)
22716 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022717 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022718 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022719 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022720 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 return;
22722 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022723 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022724 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022725
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022726 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022727 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022728 else
22729 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022731 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022732 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734}
22735
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022736/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022737 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022738 * Also give an error message.
22739 */
22740 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022741var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022742{
22743 if (flags & DI_FLAGS_RO)
22744 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022745 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022746 return TRUE;
22747 }
22748 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22749 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022750 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022751 return TRUE;
22752 }
22753 return FALSE;
22754}
22755
22756/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022757 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22758 * Also give an error message.
22759 */
22760 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022761var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022762{
22763 if (flags & DI_FLAGS_FIX)
22764 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022765 EMSG2(_("E795: Cannot delete variable %s"),
22766 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022767 return TRUE;
22768 }
22769 return FALSE;
22770}
22771
22772/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022773 * Check if a funcref is assigned to a valid variable name.
22774 * Return TRUE and give an error if not.
22775 */
22776 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022777var_check_func_name(
22778 char_u *name, /* points to start of variable name */
22779 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022780{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022781 /* Allow for w: b: s: and t:. */
22782 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022783 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22784 ? name[2] : name[0]))
22785 {
22786 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22787 name);
22788 return TRUE;
22789 }
22790 /* Don't allow hiding a function. When "v" is not NULL we might be
22791 * assigning another function to the same var, the type is checked
22792 * below. */
22793 if (new_var && function_exists(name))
22794 {
22795 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22796 name);
22797 return TRUE;
22798 }
22799 return FALSE;
22800}
22801
22802/*
22803 * Check if a variable name is valid.
22804 * Return FALSE and give an error if not.
22805 */
22806 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022807valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022808{
22809 char_u *p;
22810
22811 for (p = varname; *p != NUL; ++p)
22812 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22813 && *p != AUTOLOAD_CHAR)
22814 {
22815 EMSG2(_(e_illvar), varname);
22816 return FALSE;
22817 }
22818 return TRUE;
22819}
22820
22821/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022822 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022823 * Also give an error message, using "name" or _("name") when use_gettext is
22824 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022825 */
22826 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022827tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022828{
22829 if (lock & VAR_LOCKED)
22830 {
22831 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022832 name == NULL ? (char_u *)_("Unknown")
22833 : use_gettext ? (char_u *)_(name)
22834 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022835 return TRUE;
22836 }
22837 if (lock & VAR_FIXED)
22838 {
22839 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022840 name == NULL ? (char_u *)_("Unknown")
22841 : use_gettext ? (char_u *)_(name)
22842 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022843 return TRUE;
22844 }
22845 return FALSE;
22846}
22847
22848/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022849 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022850 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022851 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022852 * It is OK for "from" and "to" to point to the same item. This is used to
22853 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022854 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022855 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022856copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022857{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022858 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022859 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022860 switch (from->v_type)
22861 {
22862 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022863 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022864 to->vval.v_number = from->vval.v_number;
22865 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022866 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022867#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022868 to->vval.v_float = from->vval.v_float;
22869 break;
22870#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022871 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022872#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022873 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022874 if (to->vval.v_job != NULL)
22875 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022876 break;
22877#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022878 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022879#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022880 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022881 if (to->vval.v_channel != NULL)
22882 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022883 break;
22884#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022885 case VAR_STRING:
22886 case VAR_FUNC:
22887 if (from->vval.v_string == NULL)
22888 to->vval.v_string = NULL;
22889 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022890 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022891 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022892 if (from->v_type == VAR_FUNC)
22893 func_ref(to->vval.v_string);
22894 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022895 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022896 case VAR_PARTIAL:
22897 if (from->vval.v_partial == NULL)
22898 to->vval.v_partial = NULL;
22899 else
22900 {
22901 to->vval.v_partial = from->vval.v_partial;
22902 ++to->vval.v_partial->pt_refcount;
22903 }
22904 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022905 case VAR_LIST:
22906 if (from->vval.v_list == NULL)
22907 to->vval.v_list = NULL;
22908 else
22909 {
22910 to->vval.v_list = from->vval.v_list;
22911 ++to->vval.v_list->lv_refcount;
22912 }
22913 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022914 case VAR_DICT:
22915 if (from->vval.v_dict == NULL)
22916 to->vval.v_dict = NULL;
22917 else
22918 {
22919 to->vval.v_dict = from->vval.v_dict;
22920 ++to->vval.v_dict->dv_refcount;
22921 }
22922 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022923 case VAR_UNKNOWN:
22924 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022925 break;
22926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022927}
22928
22929/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022930 * Make a copy of an item.
22931 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022932 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22933 * reference to an already copied list/dict can be used.
22934 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022935 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022936 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022937item_copy(
22938 typval_T *from,
22939 typval_T *to,
22940 int deep,
22941 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022942{
22943 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022944 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022945
Bram Moolenaar33570922005-01-25 22:26:29 +000022946 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022947 {
22948 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022949 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022950 }
22951 ++recurse;
22952
22953 switch (from->v_type)
22954 {
22955 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022956 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022957 case VAR_STRING:
22958 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022959 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022960 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022961 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022962 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022963 copy_tv(from, to);
22964 break;
22965 case VAR_LIST:
22966 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022967 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022968 if (from->vval.v_list == NULL)
22969 to->vval.v_list = NULL;
22970 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22971 {
22972 /* use the copy made earlier */
22973 to->vval.v_list = from->vval.v_list->lv_copylist;
22974 ++to->vval.v_list->lv_refcount;
22975 }
22976 else
22977 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22978 if (to->vval.v_list == NULL)
22979 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022980 break;
22981 case VAR_DICT:
22982 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022983 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022984 if (from->vval.v_dict == NULL)
22985 to->vval.v_dict = NULL;
22986 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22987 {
22988 /* use the copy made earlier */
22989 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22990 ++to->vval.v_dict->dv_refcount;
22991 }
22992 else
22993 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22994 if (to->vval.v_dict == NULL)
22995 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022996 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022997 case VAR_UNKNOWN:
22998 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022999 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023000 }
23001 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023002 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023003}
23004
23005/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023006 * ":echo expr1 ..." print each argument separated with a space, add a
23007 * newline at the end.
23008 * ":echon expr1 ..." print each argument plain.
23009 */
23010 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023011ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023012{
23013 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023014 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023015 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023016 char_u *p;
23017 int needclr = TRUE;
23018 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023019 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023020
23021 if (eap->skip)
23022 ++emsg_skip;
23023 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23024 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023025 /* If eval1() causes an error message the text from the command may
23026 * still need to be cleared. E.g., "echo 22,44". */
23027 need_clr_eos = needclr;
23028
Bram Moolenaar071d4272004-06-13 20:20:40 +000023029 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023030 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023031 {
23032 /*
23033 * Report the invalid expression unless the expression evaluation
23034 * has been cancelled due to an aborting error, an interrupt, or an
23035 * exception.
23036 */
23037 if (!aborting())
23038 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023039 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023040 break;
23041 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023042 need_clr_eos = FALSE;
23043
Bram Moolenaar071d4272004-06-13 20:20:40 +000023044 if (!eap->skip)
23045 {
23046 if (atstart)
23047 {
23048 atstart = FALSE;
23049 /* Call msg_start() after eval1(), evaluating the expression
23050 * may cause a message to appear. */
23051 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023052 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023053 /* Mark the saved text as finishing the line, so that what
23054 * follows is displayed on a new line when scrolling back
23055 * at the more prompt. */
23056 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023058 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023059 }
23060 else if (eap->cmdidx == CMD_echo)
23061 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023062 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023063 if (p != NULL)
23064 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023066 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023068 if (*p != TAB && needclr)
23069 {
23070 /* remove any text still there from the command */
23071 msg_clr_eos();
23072 needclr = FALSE;
23073 }
23074 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023075 }
23076 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023077 {
23078#ifdef FEAT_MBYTE
23079 if (has_mbyte)
23080 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023081 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023082
23083 (void)msg_outtrans_len_attr(p, i, echo_attr);
23084 p += i - 1;
23085 }
23086 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023088 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23089 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023090 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023091 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023092 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023093 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023094 arg = skipwhite(arg);
23095 }
23096 eap->nextcmd = check_nextcmd(arg);
23097
23098 if (eap->skip)
23099 --emsg_skip;
23100 else
23101 {
23102 /* remove text that may still be there from the command */
23103 if (needclr)
23104 msg_clr_eos();
23105 if (eap->cmdidx == CMD_echo)
23106 msg_end();
23107 }
23108}
23109
23110/*
23111 * ":echohl {name}".
23112 */
23113 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023114ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023115{
23116 int id;
23117
23118 id = syn_name2id(eap->arg);
23119 if (id == 0)
23120 echo_attr = 0;
23121 else
23122 echo_attr = syn_id2attr(id);
23123}
23124
23125/*
23126 * ":execute expr1 ..." execute the result of an expression.
23127 * ":echomsg expr1 ..." Print a message
23128 * ":echoerr expr1 ..." Print an error
23129 * Each gets spaces around each argument and a newline at the end for
23130 * echo commands
23131 */
23132 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023133ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134{
23135 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023136 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023137 int ret = OK;
23138 char_u *p;
23139 garray_T ga;
23140 int len;
23141 int save_did_emsg;
23142
23143 ga_init2(&ga, 1, 80);
23144
23145 if (eap->skip)
23146 ++emsg_skip;
23147 while (*arg != NUL && *arg != '|' && *arg != '\n')
23148 {
23149 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023150 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023151 {
23152 /*
23153 * Report the invalid expression unless the expression evaluation
23154 * has been cancelled due to an aborting error, an interrupt, or an
23155 * exception.
23156 */
23157 if (!aborting())
23158 EMSG2(_(e_invexpr2), p);
23159 ret = FAIL;
23160 break;
23161 }
23162
23163 if (!eap->skip)
23164 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023165 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166 len = (int)STRLEN(p);
23167 if (ga_grow(&ga, len + 2) == FAIL)
23168 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023169 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 ret = FAIL;
23171 break;
23172 }
23173 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023174 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023175 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023176 ga.ga_len += len;
23177 }
23178
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023179 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 arg = skipwhite(arg);
23181 }
23182
23183 if (ret != FAIL && ga.ga_data != NULL)
23184 {
23185 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023186 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023187 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023188 out_flush();
23189 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023190 else if (eap->cmdidx == CMD_echoerr)
23191 {
23192 /* We don't want to abort following commands, restore did_emsg. */
23193 save_did_emsg = did_emsg;
23194 EMSG((char_u *)ga.ga_data);
23195 if (!force_abort)
23196 did_emsg = save_did_emsg;
23197 }
23198 else if (eap->cmdidx == CMD_execute)
23199 do_cmdline((char_u *)ga.ga_data,
23200 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23201 }
23202
23203 ga_clear(&ga);
23204
23205 if (eap->skip)
23206 --emsg_skip;
23207
23208 eap->nextcmd = check_nextcmd(arg);
23209}
23210
23211/*
23212 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23213 * "arg" points to the "&" or '+' when called, to "option" when returning.
23214 * Returns NULL when no option name found. Otherwise pointer to the char
23215 * after the option name.
23216 */
23217 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023218find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219{
23220 char_u *p = *arg;
23221
23222 ++p;
23223 if (*p == 'g' && p[1] == ':')
23224 {
23225 *opt_flags = OPT_GLOBAL;
23226 p += 2;
23227 }
23228 else if (*p == 'l' && p[1] == ':')
23229 {
23230 *opt_flags = OPT_LOCAL;
23231 p += 2;
23232 }
23233 else
23234 *opt_flags = 0;
23235
23236 if (!ASCII_ISALPHA(*p))
23237 return NULL;
23238 *arg = p;
23239
23240 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23241 p += 4; /* termcap option */
23242 else
23243 while (ASCII_ISALPHA(*p))
23244 ++p;
23245 return p;
23246}
23247
23248/*
23249 * ":function"
23250 */
23251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023252ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023253{
23254 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023255 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 int j;
23257 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023258 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023259 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023260 char_u *name = NULL;
23261 char_u *p;
23262 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023263 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023264 garray_T newargs;
23265 garray_T newlines;
23266 int varargs = FALSE;
23267 int mustend = FALSE;
23268 int flags = 0;
23269 ufunc_T *fp;
23270 int indent;
23271 int nesting;
23272 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023273 dictitem_T *v;
23274 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023275 static int func_nr = 0; /* number for nameless function */
23276 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023277 hashtab_T *ht;
23278 int todo;
23279 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023280 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281
23282 /*
23283 * ":function" without argument: list functions.
23284 */
23285 if (ends_excmd(*eap->arg))
23286 {
23287 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023288 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023289 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023290 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023291 {
23292 if (!HASHITEM_EMPTY(hi))
23293 {
23294 --todo;
23295 fp = HI2UF(hi);
23296 if (!isdigit(*fp->uf_name))
23297 list_func_head(fp, FALSE);
23298 }
23299 }
23300 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023301 eap->nextcmd = check_nextcmd(eap->arg);
23302 return;
23303 }
23304
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023305 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023306 * ":function /pat": list functions matching pattern.
23307 */
23308 if (*eap->arg == '/')
23309 {
23310 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23311 if (!eap->skip)
23312 {
23313 regmatch_T regmatch;
23314
23315 c = *p;
23316 *p = NUL;
23317 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23318 *p = c;
23319 if (regmatch.regprog != NULL)
23320 {
23321 regmatch.rm_ic = p_ic;
23322
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023323 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023324 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23325 {
23326 if (!HASHITEM_EMPTY(hi))
23327 {
23328 --todo;
23329 fp = HI2UF(hi);
23330 if (!isdigit(*fp->uf_name)
23331 && vim_regexec(&regmatch, fp->uf_name, 0))
23332 list_func_head(fp, FALSE);
23333 }
23334 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023335 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023336 }
23337 }
23338 if (*p == '/')
23339 ++p;
23340 eap->nextcmd = check_nextcmd(p);
23341 return;
23342 }
23343
23344 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023345 * Get the function name. There are these situations:
23346 * func normal function name
23347 * "name" == func, "fudi.fd_dict" == NULL
23348 * dict.func new dictionary entry
23349 * "name" == NULL, "fudi.fd_dict" set,
23350 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23351 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023352 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023353 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23354 * dict.func existing dict entry that's not a Funcref
23355 * "name" == NULL, "fudi.fd_dict" set,
23356 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023357 * s:func script-local function name
23358 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023359 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023360 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023361 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023362 paren = (vim_strchr(p, '(') != NULL);
23363 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023364 {
23365 /*
23366 * Return on an invalid expression in braces, unless the expression
23367 * evaluation has been cancelled due to an aborting error, an
23368 * interrupt, or an exception.
23369 */
23370 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023372 if (!eap->skip && fudi.fd_newkey != NULL)
23373 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023374 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023375 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023376 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023377 else
23378 eap->skip = TRUE;
23379 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023380
Bram Moolenaar071d4272004-06-13 20:20:40 +000023381 /* An error in a function call during evaluation of an expression in magic
23382 * braces should not cause the function not to be defined. */
23383 saved_did_emsg = did_emsg;
23384 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023385
23386 /*
23387 * ":function func" with only function name: list function.
23388 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023389 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 {
23391 if (!ends_excmd(*skipwhite(p)))
23392 {
23393 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023394 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023395 }
23396 eap->nextcmd = check_nextcmd(p);
23397 if (eap->nextcmd != NULL)
23398 *p = NUL;
23399 if (!eap->skip && !got_int)
23400 {
23401 fp = find_func(name);
23402 if (fp != NULL)
23403 {
23404 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023405 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023407 if (FUNCLINE(fp, j) == NULL)
23408 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 msg_putchar('\n');
23410 msg_outnum((long)(j + 1));
23411 if (j < 9)
23412 msg_putchar(' ');
23413 if (j < 99)
23414 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023415 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023416 out_flush(); /* show a line at a time */
23417 ui_breakcheck();
23418 }
23419 if (!got_int)
23420 {
23421 msg_putchar('\n');
23422 msg_puts((char_u *)" endfunction");
23423 }
23424 }
23425 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023426 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023427 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023428 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023429 }
23430
23431 /*
23432 * ":function name(arg1, arg2)" Define function.
23433 */
23434 p = skipwhite(p);
23435 if (*p != '(')
23436 {
23437 if (!eap->skip)
23438 {
23439 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023440 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 }
23442 /* attempt to continue by skipping some text */
23443 if (vim_strchr(p, '(') != NULL)
23444 p = vim_strchr(p, '(');
23445 }
23446 p = skipwhite(p + 1);
23447
23448 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23449 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23450
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023451 if (!eap->skip)
23452 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023453 /* Check the name of the function. Unless it's a dictionary function
23454 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023455 if (name != NULL)
23456 arg = name;
23457 else
23458 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023459 if (arg != NULL && (fudi.fd_di == NULL
23460 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023461 {
23462 if (*arg == K_SPECIAL)
23463 j = 3;
23464 else
23465 j = 0;
23466 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23467 : eval_isnamec(arg[j])))
23468 ++j;
23469 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023470 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023471 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023472 /* Disallow using the g: dict. */
23473 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23474 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023475 }
23476
Bram Moolenaar071d4272004-06-13 20:20:40 +000023477 /*
23478 * Isolate the arguments: "arg1, arg2, ...)"
23479 */
23480 while (*p != ')')
23481 {
23482 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23483 {
23484 varargs = TRUE;
23485 p += 3;
23486 mustend = TRUE;
23487 }
23488 else
23489 {
23490 arg = p;
23491 while (ASCII_ISALNUM(*p) || *p == '_')
23492 ++p;
23493 if (arg == p || isdigit(*arg)
23494 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23495 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23496 {
23497 if (!eap->skip)
23498 EMSG2(_("E125: Illegal argument: %s"), arg);
23499 break;
23500 }
23501 if (ga_grow(&newargs, 1) == FAIL)
23502 goto erret;
23503 c = *p;
23504 *p = NUL;
23505 arg = vim_strsave(arg);
23506 if (arg == NULL)
23507 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023508
23509 /* Check for duplicate argument name. */
23510 for (i = 0; i < newargs.ga_len; ++i)
23511 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23512 {
23513 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023514 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023515 goto erret;
23516 }
23517
Bram Moolenaar071d4272004-06-13 20:20:40 +000023518 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23519 *p = c;
23520 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 if (*p == ',')
23522 ++p;
23523 else
23524 mustend = TRUE;
23525 }
23526 p = skipwhite(p);
23527 if (mustend && *p != ')')
23528 {
23529 if (!eap->skip)
23530 EMSG2(_(e_invarg2), eap->arg);
23531 break;
23532 }
23533 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023534 if (*p != ')')
23535 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023536 ++p; /* skip the ')' */
23537
Bram Moolenaare9a41262005-01-15 22:18:47 +000023538 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023539 for (;;)
23540 {
23541 p = skipwhite(p);
23542 if (STRNCMP(p, "range", 5) == 0)
23543 {
23544 flags |= FC_RANGE;
23545 p += 5;
23546 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023547 else if (STRNCMP(p, "dict", 4) == 0)
23548 {
23549 flags |= FC_DICT;
23550 p += 4;
23551 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023552 else if (STRNCMP(p, "abort", 5) == 0)
23553 {
23554 flags |= FC_ABORT;
23555 p += 5;
23556 }
23557 else
23558 break;
23559 }
23560
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023561 /* When there is a line break use what follows for the function body.
23562 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23563 if (*p == '\n')
23564 line_arg = p + 1;
23565 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023566 EMSG(_(e_trailing));
23567
23568 /*
23569 * Read the body of the function, until ":endfunction" is found.
23570 */
23571 if (KeyTyped)
23572 {
23573 /* Check if the function already exists, don't let the user type the
23574 * whole function before telling him it doesn't work! For a script we
23575 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023576 if (!eap->skip && !eap->forceit)
23577 {
23578 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23579 EMSG(_(e_funcdict));
23580 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023581 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023582 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023584 if (!eap->skip && did_emsg)
23585 goto erret;
23586
Bram Moolenaar071d4272004-06-13 20:20:40 +000023587 msg_putchar('\n'); /* don't overwrite the function name */
23588 cmdline_row = msg_row;
23589 }
23590
23591 indent = 2;
23592 nesting = 0;
23593 for (;;)
23594 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023595 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023596 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023597 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023598 saved_wait_return = FALSE;
23599 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023600 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023601 sourcing_lnum_off = sourcing_lnum;
23602
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023603 if (line_arg != NULL)
23604 {
23605 /* Use eap->arg, split up in parts by line breaks. */
23606 theline = line_arg;
23607 p = vim_strchr(theline, '\n');
23608 if (p == NULL)
23609 line_arg += STRLEN(line_arg);
23610 else
23611 {
23612 *p = NUL;
23613 line_arg = p + 1;
23614 }
23615 }
23616 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023617 theline = getcmdline(':', 0L, indent);
23618 else
23619 theline = eap->getline(':', eap->cookie, indent);
23620 if (KeyTyped)
23621 lines_left = Rows - 1;
23622 if (theline == NULL)
23623 {
23624 EMSG(_("E126: Missing :endfunction"));
23625 goto erret;
23626 }
23627
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023628 /* Detect line continuation: sourcing_lnum increased more than one. */
23629 if (sourcing_lnum > sourcing_lnum_off + 1)
23630 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23631 else
23632 sourcing_lnum_off = 0;
23633
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 if (skip_until != NULL)
23635 {
23636 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23637 * don't check for ":endfunc". */
23638 if (STRCMP(theline, skip_until) == 0)
23639 {
23640 vim_free(skip_until);
23641 skip_until = NULL;
23642 }
23643 }
23644 else
23645 {
23646 /* skip ':' and blanks*/
23647 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23648 ;
23649
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023650 /* Check for "endfunction". */
23651 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023653 if (line_arg == NULL)
23654 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023655 break;
23656 }
23657
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023658 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023659 * at "end". */
23660 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23661 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023662 else if (STRNCMP(p, "if", 2) == 0
23663 || STRNCMP(p, "wh", 2) == 0
23664 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023665 || STRNCMP(p, "try", 3) == 0)
23666 indent += 2;
23667
23668 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023669 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023670 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023671 if (*p == '!')
23672 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023673 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023674 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023675 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023676 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023677 ++nesting;
23678 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023679 }
23680 }
23681
23682 /* Check for ":append" or ":insert". */
23683 p = skip_range(p, NULL);
23684 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23685 || (p[0] == 'i'
23686 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23687 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23688 skip_until = vim_strsave((char_u *)".");
23689
23690 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23691 arg = skipwhite(skiptowhite(p));
23692 if (arg[0] == '<' && arg[1] =='<'
23693 && ((p[0] == 'p' && p[1] == 'y'
23694 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23695 || (p[0] == 'p' && p[1] == 'e'
23696 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23697 || (p[0] == 't' && p[1] == 'c'
23698 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023699 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23700 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023701 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23702 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023703 || (p[0] == 'm' && p[1] == 'z'
23704 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 ))
23706 {
23707 /* ":python <<" continues until a dot, like ":append" */
23708 p = skipwhite(arg + 2);
23709 if (*p == NUL)
23710 skip_until = vim_strsave((char_u *)".");
23711 else
23712 skip_until = vim_strsave(p);
23713 }
23714 }
23715
23716 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023717 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023718 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023719 if (line_arg == NULL)
23720 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023721 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023722 }
23723
23724 /* Copy the line to newly allocated memory. get_one_sourceline()
23725 * allocates 250 bytes per line, this saves 80% on average. The cost
23726 * is an extra alloc/free. */
23727 p = vim_strsave(theline);
23728 if (p != NULL)
23729 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023730 if (line_arg == NULL)
23731 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023732 theline = p;
23733 }
23734
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023735 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23736
23737 /* Add NULL lines for continuation lines, so that the line count is
23738 * equal to the index in the growarray. */
23739 while (sourcing_lnum_off-- > 0)
23740 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023741
23742 /* Check for end of eap->arg. */
23743 if (line_arg != NULL && *line_arg == NUL)
23744 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023745 }
23746
23747 /* Don't define the function when skipping commands or when an error was
23748 * detected. */
23749 if (eap->skip || did_emsg)
23750 goto erret;
23751
23752 /*
23753 * If there are no errors, add the function
23754 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023755 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023756 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023757 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023758 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023759 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023760 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023761 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023762 goto erret;
23763 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023764
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023765 fp = find_func(name);
23766 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023767 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 if (!eap->forceit)
23769 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023770 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023771 goto erret;
23772 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023773 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023774 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023775 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023776 name);
23777 goto erret;
23778 }
23779 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023780 ga_clear_strings(&(fp->uf_args));
23781 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023782 vim_free(name);
23783 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 }
23786 else
23787 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023788 char numbuf[20];
23789
23790 fp = NULL;
23791 if (fudi.fd_newkey == NULL && !eap->forceit)
23792 {
23793 EMSG(_(e_funcdict));
23794 goto erret;
23795 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023796 if (fudi.fd_di == NULL)
23797 {
23798 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023799 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023800 goto erret;
23801 }
23802 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023803 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023804 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023805
23806 /* Give the function a sequential number. Can only be used with a
23807 * Funcref! */
23808 vim_free(name);
23809 sprintf(numbuf, "%d", ++func_nr);
23810 name = vim_strsave((char_u *)numbuf);
23811 if (name == NULL)
23812 goto erret;
23813 }
23814
23815 if (fp == NULL)
23816 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023817 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023818 {
23819 int slen, plen;
23820 char_u *scriptname;
23821
23822 /* Check that the autoload name matches the script name. */
23823 j = FAIL;
23824 if (sourcing_name != NULL)
23825 {
23826 scriptname = autoload_name(name);
23827 if (scriptname != NULL)
23828 {
23829 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023830 plen = (int)STRLEN(p);
23831 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023832 if (slen > plen && fnamecmp(p,
23833 sourcing_name + slen - plen) == 0)
23834 j = OK;
23835 vim_free(scriptname);
23836 }
23837 }
23838 if (j == FAIL)
23839 {
23840 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23841 goto erret;
23842 }
23843 }
23844
23845 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 if (fp == NULL)
23847 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023848
23849 if (fudi.fd_dict != NULL)
23850 {
23851 if (fudi.fd_di == NULL)
23852 {
23853 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023854 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023855 if (fudi.fd_di == NULL)
23856 {
23857 vim_free(fp);
23858 goto erret;
23859 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023860 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23861 {
23862 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023863 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023864 goto erret;
23865 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023866 }
23867 else
23868 /* overwrite existing dict entry */
23869 clear_tv(&fudi.fd_di->di_tv);
23870 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023871 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023872 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023873 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023874
23875 /* behave like "dict" was used */
23876 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 }
23878
Bram Moolenaar071d4272004-06-13 20:20:40 +000023879 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023880 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023881 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23882 {
23883 vim_free(fp);
23884 goto erret;
23885 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023886 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023887 fp->uf_args = newargs;
23888 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023889#ifdef FEAT_PROFILE
23890 fp->uf_tml_count = NULL;
23891 fp->uf_tml_total = NULL;
23892 fp->uf_tml_self = NULL;
23893 fp->uf_profiling = FALSE;
23894 if (prof_def_func())
23895 func_do_profile(fp);
23896#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023897 fp->uf_varargs = varargs;
23898 fp->uf_flags = flags;
23899 fp->uf_calls = 0;
23900 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023901 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023902
23903erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023904 ga_clear_strings(&newargs);
23905 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023906ret_free:
23907 vim_free(skip_until);
23908 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023909 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023910 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023911 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023912}
23913
23914/*
23915 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023916 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023917 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023918 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023919 * TFN_INT: internal function name OK
23920 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023921 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023922 * Advances "pp" to just after the function name (if no error).
23923 */
23924 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023925trans_function_name(
23926 char_u **pp,
23927 int skip, /* only find the end, don't evaluate */
23928 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023929 funcdict_T *fdp, /* return: info about dictionary used */
23930 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023932 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023933 char_u *start;
23934 char_u *end;
23935 int lead;
23936 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023937 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023938 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023939
23940 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023941 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023942 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023943
23944 /* Check for hard coded <SNR>: already translated function ID (from a user
23945 * command). */
23946 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23947 && (*pp)[2] == (int)KE_SNR)
23948 {
23949 *pp += 3;
23950 len = get_id_len(pp) + 3;
23951 return vim_strnsave(start, len);
23952 }
23953
23954 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23955 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023957 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023958 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023959
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023960 /* Note that TFN_ flags use the same values as GLV_ flags. */
23961 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023962 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023963 if (end == start)
23964 {
23965 if (!skip)
23966 EMSG(_("E129: Function name required"));
23967 goto theend;
23968 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023969 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023970 {
23971 /*
23972 * Report an invalid expression in braces, unless the expression
23973 * evaluation has been cancelled due to an aborting error, an
23974 * interrupt, or an exception.
23975 */
23976 if (!aborting())
23977 {
23978 if (end != NULL)
23979 EMSG2(_(e_invarg2), start);
23980 }
23981 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023982 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023983 goto theend;
23984 }
23985
23986 if (lv.ll_tv != NULL)
23987 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023988 if (fdp != NULL)
23989 {
23990 fdp->fd_dict = lv.ll_dict;
23991 fdp->fd_newkey = lv.ll_newkey;
23992 lv.ll_newkey = NULL;
23993 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023994 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023995 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23996 {
23997 name = vim_strsave(lv.ll_tv->vval.v_string);
23998 *pp = end;
23999 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024000 else if (lv.ll_tv->v_type == VAR_PARTIAL
24001 && lv.ll_tv->vval.v_partial != NULL)
24002 {
24003 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24004 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024005 if (partial != NULL)
24006 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024007 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024008 else
24009 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024010 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24011 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012 EMSG(_(e_funcref));
24013 else
24014 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024015 name = NULL;
24016 }
24017 goto theend;
24018 }
24019
24020 if (lv.ll_name == NULL)
24021 {
24022 /* Error found, but continue after the function name. */
24023 *pp = end;
24024 goto theend;
24025 }
24026
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024027 /* Check if the name is a Funcref. If so, use the value. */
24028 if (lv.ll_exp_name != NULL)
24029 {
24030 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024031 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024032 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024033 if (name == lv.ll_exp_name)
24034 name = NULL;
24035 }
24036 else
24037 {
24038 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024039 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024040 if (name == *pp)
24041 name = NULL;
24042 }
24043 if (name != NULL)
24044 {
24045 name = vim_strsave(name);
24046 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024047 if (STRNCMP(name, "<SNR>", 5) == 0)
24048 {
24049 /* Change "<SNR>" to the byte sequence. */
24050 name[0] = K_SPECIAL;
24051 name[1] = KS_EXTRA;
24052 name[2] = (int)KE_SNR;
24053 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24054 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024055 goto theend;
24056 }
24057
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024058 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024059 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024060 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024061 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24062 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24063 {
24064 /* When there was "s:" already or the name expanded to get a
24065 * leading "s:" then remove it. */
24066 lv.ll_name += 2;
24067 len -= 2;
24068 lead = 2;
24069 }
24070 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024071 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024072 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024073 /* skip over "s:" and "g:" */
24074 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024075 lv.ll_name += 2;
24076 len = (int)(end - lv.ll_name);
24077 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024078
24079 /*
24080 * Copy the function name to allocated memory.
24081 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24082 * Accept <SNR>123_name() outside a script.
24083 */
24084 if (skip)
24085 lead = 0; /* do nothing */
24086 else if (lead > 0)
24087 {
24088 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024089 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24090 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024091 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024092 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024093 if (current_SID <= 0)
24094 {
24095 EMSG(_(e_usingsid));
24096 goto theend;
24097 }
24098 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24099 lead += (int)STRLEN(sid_buf);
24100 }
24101 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024102 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024103 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024104 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024105 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024106 goto theend;
24107 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024108 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024109 {
24110 char_u *cp = vim_strchr(lv.ll_name, ':');
24111
24112 if (cp != NULL && cp < end)
24113 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024114 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024115 goto theend;
24116 }
24117 }
24118
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024119 name = alloc((unsigned)(len + lead + 1));
24120 if (name != NULL)
24121 {
24122 if (lead > 0)
24123 {
24124 name[0] = K_SPECIAL;
24125 name[1] = KS_EXTRA;
24126 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024127 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024128 STRCPY(name + 3, sid_buf);
24129 }
24130 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024131 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024132 }
24133 *pp = end;
24134
24135theend:
24136 clear_lval(&lv);
24137 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024138}
24139
24140/*
24141 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24142 * Return 2 if "p" starts with "s:".
24143 * Return 0 otherwise.
24144 */
24145 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024146eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024147{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024148 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24149 * the standard library function. */
24150 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24151 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024152 return 5;
24153 if (p[0] == 's' && p[1] == ':')
24154 return 2;
24155 return 0;
24156}
24157
24158/*
24159 * Return TRUE if "p" starts with "<SID>" or "s:".
24160 * Only works if eval_fname_script() returned non-zero for "p"!
24161 */
24162 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024163eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024164{
24165 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24166}
24167
24168/*
24169 * List the head of the function: "name(arg1, arg2)".
24170 */
24171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024172list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024173{
24174 int j;
24175
24176 msg_start();
24177 if (indent)
24178 MSG_PUTS(" ");
24179 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024180 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024181 {
24182 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024183 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024184 }
24185 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024186 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024187 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024188 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024189 {
24190 if (j)
24191 MSG_PUTS(", ");
24192 msg_puts(FUNCARG(fp, j));
24193 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024194 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024195 {
24196 if (j)
24197 MSG_PUTS(", ");
24198 MSG_PUTS("...");
24199 }
24200 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024201 if (fp->uf_flags & FC_ABORT)
24202 MSG_PUTS(" abort");
24203 if (fp->uf_flags & FC_RANGE)
24204 MSG_PUTS(" range");
24205 if (fp->uf_flags & FC_DICT)
24206 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024207 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024208 if (p_verbose > 0)
24209 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024210}
24211
24212/*
24213 * Find a function by name, return pointer to it in ufuncs.
24214 * Return NULL for unknown function.
24215 */
24216 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024217find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024218{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024219 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024220
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024221 hi = hash_find(&func_hashtab, name);
24222 if (!HASHITEM_EMPTY(hi))
24223 return HI2UF(hi);
24224 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225}
24226
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024227#if defined(EXITFREE) || defined(PROTO)
24228 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024229free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024230{
24231 hashitem_T *hi;
24232
24233 /* Need to start all over every time, because func_free() may change the
24234 * hash table. */
24235 while (func_hashtab.ht_used > 0)
24236 for (hi = func_hashtab.ht_array; ; ++hi)
24237 if (!HASHITEM_EMPTY(hi))
24238 {
24239 func_free(HI2UF(hi));
24240 break;
24241 }
24242}
24243#endif
24244
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024245 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024246translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024247{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024248 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024249 return find_internal_func(name) >= 0;
24250 return find_func(name) != NULL;
24251}
24252
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024253/*
24254 * Return TRUE if a function "name" exists.
24255 */
24256 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024257function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024258{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024259 char_u *nm = name;
24260 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024261 int n = FALSE;
24262
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024263 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024264 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024265 nm = skipwhite(nm);
24266
24267 /* Only accept "funcname", "funcname ", "funcname (..." and
24268 * "funcname(...", not "funcname!...". */
24269 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024270 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024271 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024272 return n;
24273}
24274
Bram Moolenaara1544c02013-05-30 12:35:52 +020024275 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024276get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024277{
24278 char_u *nm = name;
24279 char_u *p;
24280
Bram Moolenaar65639032016-03-16 21:40:30 +010024281 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024282
24283 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024284 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024285 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024286
Bram Moolenaara1544c02013-05-30 12:35:52 +020024287 vim_free(p);
24288 return NULL;
24289}
24290
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024291/*
24292 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024293 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24294 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024295 */
24296 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024297builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024298{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024299 char_u *p;
24300
24301 if (!ASCII_ISLOWER(name[0]))
24302 return FALSE;
24303 p = vim_strchr(name, AUTOLOAD_CHAR);
24304 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024305}
24306
Bram Moolenaar05159a02005-02-26 23:04:13 +000024307#if defined(FEAT_PROFILE) || defined(PROTO)
24308/*
24309 * Start profiling function "fp".
24310 */
24311 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024312func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024313{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024314 int len = fp->uf_lines.ga_len;
24315
24316 if (len == 0)
24317 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024318 fp->uf_tm_count = 0;
24319 profile_zero(&fp->uf_tm_self);
24320 profile_zero(&fp->uf_tm_total);
24321 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024322 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024323 if (fp->uf_tml_total == NULL)
24324 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024325 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024326 if (fp->uf_tml_self == NULL)
24327 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024328 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024329 fp->uf_tml_idx = -1;
24330 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24331 || fp->uf_tml_self == NULL)
24332 return; /* out of memory */
24333
24334 fp->uf_profiling = TRUE;
24335}
24336
24337/*
24338 * Dump the profiling results for all functions in file "fd".
24339 */
24340 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024341func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024342{
24343 hashitem_T *hi;
24344 int todo;
24345 ufunc_T *fp;
24346 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024347 ufunc_T **sorttab;
24348 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024349
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024350 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024351 if (todo == 0)
24352 return; /* nothing to dump */
24353
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024354 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024355
Bram Moolenaar05159a02005-02-26 23:04:13 +000024356 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24357 {
24358 if (!HASHITEM_EMPTY(hi))
24359 {
24360 --todo;
24361 fp = HI2UF(hi);
24362 if (fp->uf_profiling)
24363 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024364 if (sorttab != NULL)
24365 sorttab[st_len++] = fp;
24366
Bram Moolenaar05159a02005-02-26 23:04:13 +000024367 if (fp->uf_name[0] == K_SPECIAL)
24368 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24369 else
24370 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24371 if (fp->uf_tm_count == 1)
24372 fprintf(fd, "Called 1 time\n");
24373 else
24374 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24375 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24376 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24377 fprintf(fd, "\n");
24378 fprintf(fd, "count total (s) self (s)\n");
24379
24380 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24381 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024382 if (FUNCLINE(fp, i) == NULL)
24383 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024384 prof_func_line(fd, fp->uf_tml_count[i],
24385 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024386 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24387 }
24388 fprintf(fd, "\n");
24389 }
24390 }
24391 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024392
24393 if (sorttab != NULL && st_len > 0)
24394 {
24395 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24396 prof_total_cmp);
24397 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24398 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24399 prof_self_cmp);
24400 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24401 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024402
24403 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024404}
Bram Moolenaar73830342005-02-28 22:48:19 +000024405
24406 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024407prof_sort_list(
24408 FILE *fd,
24409 ufunc_T **sorttab,
24410 int st_len,
24411 char *title,
24412 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024413{
24414 int i;
24415 ufunc_T *fp;
24416
24417 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24418 fprintf(fd, "count total (s) self (s) function\n");
24419 for (i = 0; i < 20 && i < st_len; ++i)
24420 {
24421 fp = sorttab[i];
24422 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24423 prefer_self);
24424 if (fp->uf_name[0] == K_SPECIAL)
24425 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24426 else
24427 fprintf(fd, " %s()\n", fp->uf_name);
24428 }
24429 fprintf(fd, "\n");
24430}
24431
24432/*
24433 * Print the count and times for one function or function line.
24434 */
24435 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024436prof_func_line(
24437 FILE *fd,
24438 int count,
24439 proftime_T *total,
24440 proftime_T *self,
24441 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024442{
24443 if (count > 0)
24444 {
24445 fprintf(fd, "%5d ", count);
24446 if (prefer_self && profile_equal(total, self))
24447 fprintf(fd, " ");
24448 else
24449 fprintf(fd, "%s ", profile_msg(total));
24450 if (!prefer_self && profile_equal(total, self))
24451 fprintf(fd, " ");
24452 else
24453 fprintf(fd, "%s ", profile_msg(self));
24454 }
24455 else
24456 fprintf(fd, " ");
24457}
24458
24459/*
24460 * Compare function for total time sorting.
24461 */
24462 static int
24463#ifdef __BORLANDC__
24464_RTLENTRYF
24465#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024466prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024467{
24468 ufunc_T *p1, *p2;
24469
24470 p1 = *(ufunc_T **)s1;
24471 p2 = *(ufunc_T **)s2;
24472 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24473}
24474
24475/*
24476 * Compare function for self time sorting.
24477 */
24478 static int
24479#ifdef __BORLANDC__
24480_RTLENTRYF
24481#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024482prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024483{
24484 ufunc_T *p1, *p2;
24485
24486 p1 = *(ufunc_T **)s1;
24487 p2 = *(ufunc_T **)s2;
24488 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24489}
24490
Bram Moolenaar05159a02005-02-26 23:04:13 +000024491#endif
24492
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024493/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024494 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024495 * Return TRUE if a package was loaded.
24496 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024497 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024498script_autoload(
24499 char_u *name,
24500 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024501{
24502 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024503 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024504 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024505 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024506
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024507 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024508 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024509 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024510 return FALSE;
24511
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024512 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024513
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024514 /* Find the name in the list of previously loaded package names. Skip
24515 * "autoload/", it's always the same. */
24516 for (i = 0; i < ga_loaded.ga_len; ++i)
24517 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24518 break;
24519 if (!reload && i < ga_loaded.ga_len)
24520 ret = FALSE; /* was loaded already */
24521 else
24522 {
24523 /* Remember the name if it wasn't loaded already. */
24524 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24525 {
24526 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24527 tofree = NULL;
24528 }
24529
24530 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024531 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024532 ret = TRUE;
24533 }
24534
24535 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024536 return ret;
24537}
24538
24539/*
24540 * Return the autoload script name for a function or variable name.
24541 * Returns NULL when out of memory.
24542 */
24543 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024544autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024545{
24546 char_u *p;
24547 char_u *scriptname;
24548
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024549 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024550 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24551 if (scriptname == NULL)
24552 return FALSE;
24553 STRCPY(scriptname, "autoload/");
24554 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024555 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024556 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024557 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024558 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024559 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024560}
24561
Bram Moolenaar071d4272004-06-13 20:20:40 +000024562#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24563
24564/*
24565 * Function given to ExpandGeneric() to obtain the list of user defined
24566 * function names.
24567 */
24568 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024569get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024570{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024571 static long_u done;
24572 static hashitem_T *hi;
24573 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024574
24575 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024576 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024577 done = 0;
24578 hi = func_hashtab.ht_array;
24579 }
24580 if (done < func_hashtab.ht_used)
24581 {
24582 if (done++ > 0)
24583 ++hi;
24584 while (HASHITEM_EMPTY(hi))
24585 ++hi;
24586 fp = HI2UF(hi);
24587
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024588 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024589 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024590
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024591 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24592 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024593
24594 cat_func_name(IObuff, fp);
24595 if (xp->xp_context != EXPAND_USER_FUNC)
24596 {
24597 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024598 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024599 STRCAT(IObuff, ")");
24600 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024601 return IObuff;
24602 }
24603 return NULL;
24604}
24605
24606#endif /* FEAT_CMDL_COMPL */
24607
24608/*
24609 * Copy the function name of "fp" to buffer "buf".
24610 * "buf" must be able to hold the function name plus three bytes.
24611 * Takes care of script-local function names.
24612 */
24613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024614cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024615{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024616 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024617 {
24618 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024619 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620 }
24621 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024622 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024623}
24624
24625/*
24626 * ":delfunction {name}"
24627 */
24628 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024629ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024630{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024631 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024632 char_u *p;
24633 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024634 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024635
24636 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024637 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024638 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024639 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024640 {
24641 if (fudi.fd_dict != NULL && !eap->skip)
24642 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024644 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645 if (!ends_excmd(*skipwhite(p)))
24646 {
24647 vim_free(name);
24648 EMSG(_(e_trailing));
24649 return;
24650 }
24651 eap->nextcmd = check_nextcmd(p);
24652 if (eap->nextcmd != NULL)
24653 *p = NUL;
24654
24655 if (!eap->skip)
24656 fp = find_func(name);
24657 vim_free(name);
24658
24659 if (!eap->skip)
24660 {
24661 if (fp == NULL)
24662 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024663 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024664 return;
24665 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024666 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
24668 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24669 return;
24670 }
24671
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024672 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024674 /* Delete the dict item that refers to the function, it will
24675 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024676 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024677 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024678 else
24679 func_free(fp);
24680 }
24681}
24682
24683/*
24684 * Free a function and remove it from the list of functions.
24685 */
24686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024687func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024689 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024690
24691 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024692 ga_clear_strings(&(fp->uf_args));
24693 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024694#ifdef FEAT_PROFILE
24695 vim_free(fp->uf_tml_count);
24696 vim_free(fp->uf_tml_total);
24697 vim_free(fp->uf_tml_self);
24698#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024699
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024700 /* remove the function from the function hashtable */
24701 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24702 if (HASHITEM_EMPTY(hi))
24703 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024704 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024705 hash_remove(&func_hashtab, hi);
24706
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024707 vim_free(fp);
24708}
24709
24710/*
24711 * Unreference a Function: decrement the reference count and free it when it
24712 * becomes zero. Only for numbered functions.
24713 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024715func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024716{
24717 ufunc_T *fp;
24718
24719 if (name != NULL && isdigit(*name))
24720 {
24721 fp = find_func(name);
24722 if (fp == NULL)
24723 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024724 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024725 {
24726 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024727 * when "uf_calls" becomes zero. */
24728 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024729 func_free(fp);
24730 }
24731 }
24732}
24733
24734/*
24735 * Count a reference to a Function.
24736 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024737 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024738func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024739{
24740 ufunc_T *fp;
24741
24742 if (name != NULL && isdigit(*name))
24743 {
24744 fp = find_func(name);
24745 if (fp == NULL)
24746 EMSG2(_(e_intern2), "func_ref()");
24747 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024748 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 }
24750}
24751
24752/*
24753 * Call a user function.
24754 */
24755 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024756call_user_func(
24757 ufunc_T *fp, /* pointer to function */
24758 int argcount, /* nr of args */
24759 typval_T *argvars, /* arguments */
24760 typval_T *rettv, /* return value */
24761 linenr_T firstline, /* first line of range */
24762 linenr_T lastline, /* last line of range */
24763 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024764{
Bram Moolenaar33570922005-01-25 22:26:29 +000024765 char_u *save_sourcing_name;
24766 linenr_T save_sourcing_lnum;
24767 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024768 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024769 int save_did_emsg;
24770 static int depth = 0;
24771 dictitem_T *v;
24772 int fixvar_idx = 0; /* index in fixvar[] */
24773 int i;
24774 int ai;
24775 char_u numbuf[NUMBUFLEN];
24776 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024777 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024778#ifdef FEAT_PROFILE
24779 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024780 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024781#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024782
24783 /* If depth of calling is getting too high, don't execute the function */
24784 if (depth >= p_mfd)
24785 {
24786 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024787 rettv->v_type = VAR_NUMBER;
24788 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024789 return;
24790 }
24791 ++depth;
24792
24793 line_breakcheck(); /* check for CTRL-C hit */
24794
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024795 fc = (funccall_T *)alloc(sizeof(funccall_T));
24796 fc->caller = current_funccal;
24797 current_funccal = fc;
24798 fc->func = fp;
24799 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024800 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024801 fc->linenr = 0;
24802 fc->returned = FALSE;
24803 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024804 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024805 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24806 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024807
Bram Moolenaar33570922005-01-25 22:26:29 +000024808 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024809 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024810 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24811 * each argument variable and saves a lot of time.
24812 */
24813 /*
24814 * Init l: variables.
24815 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024816 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024817 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024818 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024819 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24820 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024821 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024822 name = v->di_key;
24823 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024824 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024825 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024826 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024827 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024828 v->di_tv.vval.v_dict = selfdict;
24829 ++selfdict->dv_refcount;
24830 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024831
Bram Moolenaar33570922005-01-25 22:26:29 +000024832 /*
24833 * Init a: variables.
24834 * Set a:0 to "argcount".
24835 * Set a:000 to a list with room for the "..." arguments.
24836 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024837 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024838 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024839 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024840 /* Use "name" to avoid a warning from some compiler that checks the
24841 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024842 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024843 name = v->di_key;
24844 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024845 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024846 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024847 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024848 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024849 v->di_tv.vval.v_list = &fc->l_varlist;
24850 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24851 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24852 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024853
24854 /*
24855 * Set a:firstline to "firstline" and a:lastline to "lastline".
24856 * Set a:name to named arguments.
24857 * Set a:N to the "..." arguments.
24858 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024859 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024860 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024861 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024862 (varnumber_T)lastline);
24863 for (i = 0; i < argcount; ++i)
24864 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024865 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024866 if (ai < 0)
24867 /* named argument a:name */
24868 name = FUNCARG(fp, i);
24869 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024870 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024871 /* "..." argument a:1, a:2, etc. */
24872 sprintf((char *)numbuf, "%d", ai + 1);
24873 name = numbuf;
24874 }
24875 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24876 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024877 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24879 }
24880 else
24881 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024882 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24883 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024884 if (v == NULL)
24885 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024886 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024887 }
24888 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024889 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024890
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024891 /* Note: the values are copied directly to avoid alloc/free.
24892 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024893 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024894 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024895
24896 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24897 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024898 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24899 fc->l_listitems[ai].li_tv = argvars[i];
24900 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024901 }
24902 }
24903
Bram Moolenaar071d4272004-06-13 20:20:40 +000024904 /* Don't redraw while executing the function. */
24905 ++RedrawingDisabled;
24906 save_sourcing_name = sourcing_name;
24907 save_sourcing_lnum = sourcing_lnum;
24908 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024909 /* need space for function name + ("function " + 3) or "[number]" */
24910 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24911 + STRLEN(fp->uf_name) + 20;
24912 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 if (sourcing_name != NULL)
24914 {
24915 if (save_sourcing_name != NULL
24916 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024917 sprintf((char *)sourcing_name, "%s[%d]..",
24918 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024919 else
24920 STRCPY(sourcing_name, "function ");
24921 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24922
24923 if (p_verbose >= 12)
24924 {
24925 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024926 verbose_enter_scroll();
24927
Bram Moolenaar555b2802005-05-19 21:08:39 +000024928 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024929 if (p_verbose >= 14)
24930 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024932 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024933 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024934 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024935
24936 msg_puts((char_u *)"(");
24937 for (i = 0; i < argcount; ++i)
24938 {
24939 if (i > 0)
24940 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024941 if (argvars[i].v_type == VAR_NUMBER)
24942 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 else
24944 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024945 /* Do not want errors such as E724 here. */
24946 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024947 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024948 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024949 if (s != NULL)
24950 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024951 if (vim_strsize(s) > MSG_BUF_CLEN)
24952 {
24953 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24954 s = buf;
24955 }
24956 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024957 vim_free(tofree);
24958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024959 }
24960 }
24961 msg_puts((char_u *)")");
24962 }
24963 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024964
24965 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024966 --no_wait_return;
24967 }
24968 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024969#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024970 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024971 {
24972 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24973 func_do_profile(fp);
24974 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024975 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976 {
24977 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024978 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024979 profile_zero(&fp->uf_tm_children);
24980 }
24981 script_prof_save(&wait_start);
24982 }
24983#endif
24984
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024986 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 save_did_emsg = did_emsg;
24988 did_emsg = FALSE;
24989
24990 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024991 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024992 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24993
24994 --RedrawingDisabled;
24995
24996 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024997 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024998 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024999 clear_tv(rettv);
25000 rettv->v_type = VAR_NUMBER;
25001 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025002 }
25003
Bram Moolenaar05159a02005-02-26 23:04:13 +000025004#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025005 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025006 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025007 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025008 profile_end(&call_start);
25009 profile_sub_wait(&wait_start, &call_start);
25010 profile_add(&fp->uf_tm_total, &call_start);
25011 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025012 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025013 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025014 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25015 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025016 }
25017 }
25018#endif
25019
Bram Moolenaar071d4272004-06-13 20:20:40 +000025020 /* when being verbose, mention the return value */
25021 if (p_verbose >= 12)
25022 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025023 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025024 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025
Bram Moolenaar071d4272004-06-13 20:20:40 +000025026 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025027 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025028 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025029 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025030 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025031 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025032 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025033 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025034 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025035 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025036 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025037
Bram Moolenaar555b2802005-05-19 21:08:39 +000025038 /* The value may be very long. Skip the middle part, so that we
25039 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025040 * truncate it at the end. Don't want errors such as E724 here. */
25041 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025042 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025043 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025044 if (s != NULL)
25045 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025046 if (vim_strsize(s) > MSG_BUF_CLEN)
25047 {
25048 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25049 s = buf;
25050 }
25051 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025052 vim_free(tofree);
25053 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 }
25055 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025056
25057 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025058 --no_wait_return;
25059 }
25060
25061 vim_free(sourcing_name);
25062 sourcing_name = save_sourcing_name;
25063 sourcing_lnum = save_sourcing_lnum;
25064 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025066 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025067 script_prof_restore(&wait_start);
25068#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025069
25070 if (p_verbose >= 12 && sourcing_name != NULL)
25071 {
25072 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025073 verbose_enter_scroll();
25074
Bram Moolenaar555b2802005-05-19 21:08:39 +000025075 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025077
25078 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025079 --no_wait_return;
25080 }
25081
25082 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025083 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025085
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025086 /* If the a:000 list and the l: and a: dicts are not referenced we can
25087 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025088 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25089 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25090 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25091 {
25092 free_funccal(fc, FALSE);
25093 }
25094 else
25095 {
25096 hashitem_T *hi;
25097 listitem_T *li;
25098 int todo;
25099
25100 /* "fc" is still in use. This can happen when returning "a:000" or
25101 * assigning "l:" to a global variable.
25102 * Link "fc" in the list for garbage collection later. */
25103 fc->caller = previous_funccal;
25104 previous_funccal = fc;
25105
25106 /* Make a copy of the a: variables, since we didn't do that above. */
25107 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25108 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25109 {
25110 if (!HASHITEM_EMPTY(hi))
25111 {
25112 --todo;
25113 v = HI2DI(hi);
25114 copy_tv(&v->di_tv, &v->di_tv);
25115 }
25116 }
25117
25118 /* Make a copy of the a:000 items, since we didn't do that above. */
25119 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25120 copy_tv(&li->li_tv, &li->li_tv);
25121 }
25122}
25123
25124/*
25125 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025126 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025127 */
25128 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025129can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025130{
25131 return (fc->l_varlist.lv_copyID != copyID
25132 && fc->l_vars.dv_copyID != copyID
25133 && fc->l_avars.dv_copyID != copyID);
25134}
25135
25136/*
25137 * Free "fc" and what it contains.
25138 */
25139 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025140free_funccal(
25141 funccall_T *fc,
25142 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025143{
25144 listitem_T *li;
25145
25146 /* The a: variables typevals may not have been allocated, only free the
25147 * allocated variables. */
25148 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25149
25150 /* free all l: variables */
25151 vars_clear(&fc->l_vars.dv_hashtab);
25152
25153 /* Free the a:000 variables if they were allocated. */
25154 if (free_val)
25155 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25156 clear_tv(&li->li_tv);
25157
25158 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025159}
25160
25161/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025162 * Add a number variable "name" to dict "dp" with value "nr".
25163 */
25164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025165add_nr_var(
25166 dict_T *dp,
25167 dictitem_T *v,
25168 char *name,
25169 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025170{
25171 STRCPY(v->di_key, name);
25172 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25173 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25174 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025175 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025176 v->di_tv.vval.v_number = nr;
25177}
25178
25179/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025180 * ":return [expr]"
25181 */
25182 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025183ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025184{
25185 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025186 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 int returning = FALSE;
25188
25189 if (current_funccal == NULL)
25190 {
25191 EMSG(_("E133: :return not inside a function"));
25192 return;
25193 }
25194
25195 if (eap->skip)
25196 ++emsg_skip;
25197
25198 eap->nextcmd = NULL;
25199 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025200 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025201 {
25202 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025203 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025205 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025206 }
25207 /* It's safer to return also on error. */
25208 else if (!eap->skip)
25209 {
25210 /*
25211 * Return unless the expression evaluation has been cancelled due to an
25212 * aborting error, an interrupt, or an exception.
25213 */
25214 if (!aborting())
25215 returning = do_return(eap, FALSE, TRUE, NULL);
25216 }
25217
25218 /* When skipping or the return gets pending, advance to the next command
25219 * in this line (!returning). Otherwise, ignore the rest of the line.
25220 * Following lines will be ignored by get_func_line(). */
25221 if (returning)
25222 eap->nextcmd = NULL;
25223 else if (eap->nextcmd == NULL) /* no argument */
25224 eap->nextcmd = check_nextcmd(arg);
25225
25226 if (eap->skip)
25227 --emsg_skip;
25228}
25229
25230/*
25231 * Return from a function. Possibly makes the return pending. Also called
25232 * for a pending return at the ":endtry" or after returning from an extra
25233 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025234 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025235 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236 * FALSE when the return gets pending.
25237 */
25238 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025239do_return(
25240 exarg_T *eap,
25241 int reanimate,
25242 int is_cmd,
25243 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025244{
25245 int idx;
25246 struct condstack *cstack = eap->cstack;
25247
25248 if (reanimate)
25249 /* Undo the return. */
25250 current_funccal->returned = FALSE;
25251
25252 /*
25253 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25254 * not in its finally clause (which then is to be executed next) is found.
25255 * In this case, make the ":return" pending for execution at the ":endtry".
25256 * Otherwise, return normally.
25257 */
25258 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25259 if (idx >= 0)
25260 {
25261 cstack->cs_pending[idx] = CSTP_RETURN;
25262
25263 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025264 /* A pending return again gets pending. "rettv" points to an
25265 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025266 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025267 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025268 else
25269 {
25270 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025271 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025272 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025273 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025274
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025275 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025276 {
25277 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025278 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025279 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 else
25281 EMSG(_(e_outofmem));
25282 }
25283 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025284 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025285
25286 if (reanimate)
25287 {
25288 /* The pending return value could be overwritten by a ":return"
25289 * without argument in a finally clause; reset the default
25290 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025291 current_funccal->rettv->v_type = VAR_NUMBER;
25292 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 }
25294 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025295 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296 }
25297 else
25298 {
25299 current_funccal->returned = TRUE;
25300
25301 /* If the return is carried out now, store the return value. For
25302 * a return immediately after reanimation, the value is already
25303 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025304 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025306 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025307 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025308 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025309 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 }
25311 }
25312
25313 return idx < 0;
25314}
25315
25316/*
25317 * Free the variable with a pending return value.
25318 */
25319 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025320discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025321{
Bram Moolenaar33570922005-01-25 22:26:29 +000025322 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025323}
25324
25325/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025326 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025327 * is an allocated string. Used by report_pending() for verbose messages.
25328 */
25329 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025330get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025331{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025332 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025333 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025334 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025336 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025337 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025338 if (s == NULL)
25339 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025340
25341 STRCPY(IObuff, ":return ");
25342 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25343 if (STRLEN(s) + 8 >= IOSIZE)
25344 STRCPY(IObuff + IOSIZE - 4, "...");
25345 vim_free(tofree);
25346 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025347}
25348
25349/*
25350 * Get next function line.
25351 * Called by do_cmdline() to get the next line.
25352 * Returns allocated string, or NULL for end of function.
25353 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025354 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025355get_func_line(
25356 int c UNUSED,
25357 void *cookie,
25358 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025359{
Bram Moolenaar33570922005-01-25 22:26:29 +000025360 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025361 ufunc_T *fp = fcp->func;
25362 char_u *retval;
25363 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025364
25365 /* If breakpoints have been added/deleted need to check for it. */
25366 if (fcp->dbg_tick != debug_tick)
25367 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025368 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025369 sourcing_lnum);
25370 fcp->dbg_tick = debug_tick;
25371 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025372#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025373 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025374 func_line_end(cookie);
25375#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025376
Bram Moolenaar05159a02005-02-26 23:04:13 +000025377 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025378 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25379 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380 retval = NULL;
25381 else
25382 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025383 /* Skip NULL lines (continuation lines). */
25384 while (fcp->linenr < gap->ga_len
25385 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25386 ++fcp->linenr;
25387 if (fcp->linenr >= gap->ga_len)
25388 retval = NULL;
25389 else
25390 {
25391 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25392 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025393#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025394 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025395 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025396#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025397 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025398 }
25399
25400 /* Did we encounter a breakpoint? */
25401 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25402 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025403 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025405 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 sourcing_lnum);
25407 fcp->dbg_tick = debug_tick;
25408 }
25409
25410 return retval;
25411}
25412
Bram Moolenaar05159a02005-02-26 23:04:13 +000025413#if defined(FEAT_PROFILE) || defined(PROTO)
25414/*
25415 * Called when starting to read a function line.
25416 * "sourcing_lnum" must be correct!
25417 * When skipping lines it may not actually be executed, but we won't find out
25418 * until later and we need to store the time now.
25419 */
25420 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025421func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422{
25423 funccall_T *fcp = (funccall_T *)cookie;
25424 ufunc_T *fp = fcp->func;
25425
25426 if (fp->uf_profiling && sourcing_lnum >= 1
25427 && sourcing_lnum <= fp->uf_lines.ga_len)
25428 {
25429 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025430 /* Skip continuation lines. */
25431 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25432 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025433 fp->uf_tml_execed = FALSE;
25434 profile_start(&fp->uf_tml_start);
25435 profile_zero(&fp->uf_tml_children);
25436 profile_get_wait(&fp->uf_tml_wait);
25437 }
25438}
25439
25440/*
25441 * Called when actually executing a function line.
25442 */
25443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025444func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445{
25446 funccall_T *fcp = (funccall_T *)cookie;
25447 ufunc_T *fp = fcp->func;
25448
25449 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25450 fp->uf_tml_execed = TRUE;
25451}
25452
25453/*
25454 * Called when done with a function line.
25455 */
25456 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025457func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025458{
25459 funccall_T *fcp = (funccall_T *)cookie;
25460 ufunc_T *fp = fcp->func;
25461
25462 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25463 {
25464 if (fp->uf_tml_execed)
25465 {
25466 ++fp->uf_tml_count[fp->uf_tml_idx];
25467 profile_end(&fp->uf_tml_start);
25468 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025469 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025470 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25471 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025472 }
25473 fp->uf_tml_idx = -1;
25474 }
25475}
25476#endif
25477
Bram Moolenaar071d4272004-06-13 20:20:40 +000025478/*
25479 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025480 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 */
25482 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025483func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025484{
Bram Moolenaar33570922005-01-25 22:26:29 +000025485 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486
25487 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25488 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025489 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025490 || fcp->returned);
25491}
25492
25493/*
25494 * return TRUE if cookie indicates a function which "abort"s on errors.
25495 */
25496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025497func_has_abort(
25498 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025499{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025500 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025501}
25502
25503#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25504typedef enum
25505{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025506 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25507 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25508 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509} var_flavour_T;
25510
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025511static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512
25513 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025514var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025515{
25516 char_u *p = varname;
25517
25518 if (ASCII_ISUPPER(*p))
25519 {
25520 while (*(++p))
25521 if (ASCII_ISLOWER(*p))
25522 return VAR_FLAVOUR_SESSION;
25523 return VAR_FLAVOUR_VIMINFO;
25524 }
25525 else
25526 return VAR_FLAVOUR_DEFAULT;
25527}
25528#endif
25529
25530#if defined(FEAT_VIMINFO) || defined(PROTO)
25531/*
25532 * Restore global vars that start with a capital from the viminfo file
25533 */
25534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025535read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536{
25537 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025538 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025539 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025540 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025541
25542 if (!writing && (find_viminfo_parameter('!') != NULL))
25543 {
25544 tab = vim_strchr(virp->vir_line + 1, '\t');
25545 if (tab != NULL)
25546 {
25547 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025548 switch (*tab)
25549 {
25550 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025551#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025552 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025553#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025554 case 'D': type = VAR_DICT; break;
25555 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025556 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025557 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025558
25559 tab = vim_strchr(tab, '\t');
25560 if (tab != NULL)
25561 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025562 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025563 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025564 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025566#ifdef FEAT_FLOAT
25567 else if (type == VAR_FLOAT)
25568 (void)string2float(tab + 1, &tv.vval.v_float);
25569#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025570 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025571 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025572 if (type == VAR_DICT || type == VAR_LIST)
25573 {
25574 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25575
25576 if (etv == NULL)
25577 /* Failed to parse back the dict or list, use it as a
25578 * string. */
25579 tv.v_type = VAR_STRING;
25580 else
25581 {
25582 vim_free(tv.vval.v_string);
25583 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025584 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025585 }
25586 }
25587
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025588 /* when in a function use global variables */
25589 save_funccal = current_funccal;
25590 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025591 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025592 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025593
25594 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025595 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025596 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25597 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025598 }
25599 }
25600 }
25601
25602 return viminfo_readline(virp);
25603}
25604
25605/*
25606 * Write global vars that start with a capital to the viminfo file
25607 */
25608 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025609write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610{
Bram Moolenaar33570922005-01-25 22:26:29 +000025611 hashitem_T *hi;
25612 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025613 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025614 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025615 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025616 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025617 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 if (find_viminfo_parameter('!') == NULL)
25620 return;
25621
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025622 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025623
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025624 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025625 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025626 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025627 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025628 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025629 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025630 this_var = HI2DI(hi);
25631 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025632 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025633 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025634 {
25635 case VAR_STRING: s = "STR"; break;
25636 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025637 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025638 case VAR_DICT: s = "DIC"; break;
25639 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025640 case VAR_SPECIAL: s = "XPL"; break;
25641
25642 case VAR_UNKNOWN:
25643 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025644 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025645 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025646 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025647 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025648 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025649 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025650 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025651 if (p != NULL)
25652 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025653 vim_free(tofree);
25654 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025655 }
25656 }
25657}
25658#endif
25659
25660#if defined(FEAT_SESSION) || defined(PROTO)
25661 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025662store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025663{
Bram Moolenaar33570922005-01-25 22:26:29 +000025664 hashitem_T *hi;
25665 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025666 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025667 char_u *p, *t;
25668
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025669 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025670 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025672 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025673 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025674 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025675 this_var = HI2DI(hi);
25676 if ((this_var->di_tv.v_type == VAR_NUMBER
25677 || this_var->di_tv.v_type == VAR_STRING)
25678 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025679 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025680 /* Escape special characters with a backslash. Turn a LF and
25681 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025682 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025683 (char_u *)"\\\"\n\r");
25684 if (p == NULL) /* out of memory */
25685 break;
25686 for (t = p; *t != NUL; ++t)
25687 if (*t == '\n')
25688 *t = 'n';
25689 else if (*t == '\r')
25690 *t = 'r';
25691 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025692 this_var->di_key,
25693 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25694 : ' ',
25695 p,
25696 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25697 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025698 || put_eol(fd) == FAIL)
25699 {
25700 vim_free(p);
25701 return FAIL;
25702 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025703 vim_free(p);
25704 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025705#ifdef FEAT_FLOAT
25706 else if (this_var->di_tv.v_type == VAR_FLOAT
25707 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25708 {
25709 float_T f = this_var->di_tv.vval.v_float;
25710 int sign = ' ';
25711
25712 if (f < 0)
25713 {
25714 f = -f;
25715 sign = '-';
25716 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025717 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025718 this_var->di_key, sign, f) < 0)
25719 || put_eol(fd) == FAIL)
25720 return FAIL;
25721 }
25722#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723 }
25724 }
25725 return OK;
25726}
25727#endif
25728
Bram Moolenaar661b1822005-07-28 22:36:45 +000025729/*
25730 * Display script name where an item was last set.
25731 * Should only be invoked when 'verbose' is non-zero.
25732 */
25733 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025734last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025735{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025736 char_u *p;
25737
Bram Moolenaar661b1822005-07-28 22:36:45 +000025738 if (scriptID != 0)
25739 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025740 p = home_replace_save(NULL, get_scriptname(scriptID));
25741 if (p != NULL)
25742 {
25743 verbose_enter();
25744 MSG_PUTS(_("\n\tLast set from "));
25745 MSG_PUTS(p);
25746 vim_free(p);
25747 verbose_leave();
25748 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025749 }
25750}
25751
Bram Moolenaard812df62008-11-09 12:46:09 +000025752/*
25753 * List v:oldfiles in a nice way.
25754 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025755 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025756ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025757{
25758 list_T *l = vimvars[VV_OLDFILES].vv_list;
25759 listitem_T *li;
25760 int nr = 0;
25761
25762 if (l == NULL)
25763 msg((char_u *)_("No old files"));
25764 else
25765 {
25766 msg_start();
25767 msg_scroll = TRUE;
25768 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25769 {
25770 msg_outnum((long)++nr);
25771 MSG_PUTS(": ");
25772 msg_outtrans(get_tv_string(&li->li_tv));
25773 msg_putchar('\n');
25774 out_flush(); /* output one line at a time */
25775 ui_breakcheck();
25776 }
25777 /* Assume "got_int" was set to truncate the listing. */
25778 got_int = FALSE;
25779
25780#ifdef FEAT_BROWSE_CMD
25781 if (cmdmod.browse)
25782 {
25783 quit_more = FALSE;
25784 nr = prompt_for_number(FALSE);
25785 msg_starthere();
25786 if (nr > 0)
25787 {
25788 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25789 (long)nr);
25790
25791 if (p != NULL)
25792 {
25793 p = expand_env_save(p);
25794 eap->arg = p;
25795 eap->cmdidx = CMD_edit;
25796 cmdmod.browse = FALSE;
25797 do_exedit(eap, NULL);
25798 vim_free(p);
25799 }
25800 }
25801 }
25802#endif
25803 }
25804}
25805
Bram Moolenaar53744302015-07-17 17:38:22 +020025806/* reset v:option_new, v:option_old and v:option_type */
25807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025809{
25810 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25811 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25812 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25813}
25814
25815
Bram Moolenaar071d4272004-06-13 20:20:40 +000025816#endif /* FEAT_EVAL */
25817
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025819#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025820
25821#ifdef WIN3264
25822/*
25823 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25824 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025825static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25826static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25827static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025828
25829/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025830 * Get the short path (8.3) for the filename in "fnamep".
25831 * Only works for a valid file name.
25832 * When the path gets longer "fnamep" is changed and the allocated buffer
25833 * is put in "bufp".
25834 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25835 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025836 */
25837 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025838get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025839{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025840 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025841 char_u *newbuf;
25842
25843 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025844 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845 if (l > len - 1)
25846 {
25847 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025848 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025849 newbuf = vim_strnsave(*fnamep, l);
25850 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025851 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025852
25853 vim_free(*bufp);
25854 *fnamep = *bufp = newbuf;
25855
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025856 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025857 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025858 }
25859
25860 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025861 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025862}
25863
25864/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025865 * Get the short path (8.3) for the filename in "fname". The converted
25866 * path is returned in "bufp".
25867 *
25868 * Some of the directories specified in "fname" may not exist. This function
25869 * will shorten the existing directories at the beginning of the path and then
25870 * append the remaining non-existing path.
25871 *
25872 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025873 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025874 * bufp - Pointer to an allocated buffer for the filename.
25875 * fnamelen - Length of the filename pointed to by fname
25876 *
25877 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878 */
25879 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025880shortpath_for_invalid_fname(
25881 char_u **fname,
25882 char_u **bufp,
25883 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885 char_u *short_fname, *save_fname, *pbuf_unused;
25886 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025887 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025888 int old_len, len;
25889 int new_len, sfx_len;
25890 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891
25892 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025893 old_len = *fnamelen;
25894 save_fname = vim_strnsave(*fname, old_len);
25895 pbuf_unused = NULL;
25896 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025898 endp = save_fname + old_len - 1; /* Find the end of the copy */
25899 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025900
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025901 /*
25902 * Try shortening the supplied path till it succeeds by removing one
25903 * directory at a time from the tail of the path.
25904 */
25905 len = 0;
25906 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025907 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025908 /* go back one path-separator */
25909 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25910 --endp;
25911 if (endp <= save_fname)
25912 break; /* processed the complete path */
25913
25914 /*
25915 * Replace the path separator with a NUL and try to shorten the
25916 * resulting path.
25917 */
25918 ch = *endp;
25919 *endp = 0;
25920 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025921 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025922 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25923 {
25924 retval = FAIL;
25925 goto theend;
25926 }
25927 *endp = ch; /* preserve the string */
25928
25929 if (len > 0)
25930 break; /* successfully shortened the path */
25931
25932 /* failed to shorten the path. Skip the path separator */
25933 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934 }
25935
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025938 /*
25939 * Succeeded in shortening the path. Now concatenate the shortened
25940 * path with the remaining path at the tail.
25941 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025943 /* Compute the length of the new path. */
25944 sfx_len = (int)(save_endp - endp) + 1;
25945 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025946
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025947 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025948 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025949 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 /* There is not enough space in the currently allocated string,
25952 * copy it to a buffer big enough. */
25953 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025954 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025955 {
25956 retval = FAIL;
25957 goto theend;
25958 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 }
25960 else
25961 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025962 /* Transfer short_fname to the main buffer (it's big enough),
25963 * unless get_short_pathname() did its work in-place. */
25964 *fname = *bufp = save_fname;
25965 if (short_fname != save_fname)
25966 vim_strncpy(save_fname, short_fname, len);
25967 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025969
25970 /* concat the not-shortened part of the path */
25971 vim_strncpy(*fname + len, endp, sfx_len);
25972 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025973 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025974
25975theend:
25976 vim_free(pbuf_unused);
25977 vim_free(save_fname);
25978
25979 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025980}
25981
25982/*
25983 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025984 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025985 */
25986 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025987shortpath_for_partial(
25988 char_u **fnamep,
25989 char_u **bufp,
25990 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025991{
25992 int sepcount, len, tflen;
25993 char_u *p;
25994 char_u *pbuf, *tfname;
25995 int hasTilde;
25996
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025997 /* Count up the path separators from the RHS.. so we know which part
25998 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025999 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026000 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026001 if (vim_ispathsep(*p))
26002 ++sepcount;
26003
26004 /* Need full path first (use expand_env() to remove a "~/") */
26005 hasTilde = (**fnamep == '~');
26006 if (hasTilde)
26007 pbuf = tfname = expand_env_save(*fnamep);
26008 else
26009 pbuf = tfname = FullName_save(*fnamep, FALSE);
26010
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026011 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026012
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026013 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26014 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026015
26016 if (len == 0)
26017 {
26018 /* Don't have a valid filename, so shorten the rest of the
26019 * path if we can. This CAN give us invalid 8.3 filenames, but
26020 * there's not a lot of point in guessing what it might be.
26021 */
26022 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026023 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26024 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 }
26026
26027 /* Count the paths backward to find the beginning of the desired string. */
26028 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026029 {
26030#ifdef FEAT_MBYTE
26031 if (has_mbyte)
26032 p -= mb_head_off(tfname, p);
26033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026034 if (vim_ispathsep(*p))
26035 {
26036 if (sepcount == 0 || (hasTilde && sepcount == 1))
26037 break;
26038 else
26039 sepcount --;
26040 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026041 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026042 if (hasTilde)
26043 {
26044 --p;
26045 if (p >= tfname)
26046 *p = '~';
26047 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026048 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 }
26050 else
26051 ++p;
26052
26053 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26054 vim_free(*bufp);
26055 *fnamelen = (int)STRLEN(p);
26056 *bufp = pbuf;
26057 *fnamep = p;
26058
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026059 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060}
26061#endif /* WIN3264 */
26062
26063/*
26064 * Adjust a filename, according to a string of modifiers.
26065 * *fnamep must be NUL terminated when called. When returning, the length is
26066 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026067 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026068 * When there is an error, *fnamep is set to NULL.
26069 */
26070 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026071modify_fname(
26072 char_u *src, /* string with modifiers */
26073 int *usedlen, /* characters after src that are used */
26074 char_u **fnamep, /* file name so far */
26075 char_u **bufp, /* buffer for allocated file name or NULL */
26076 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077{
26078 int valid = 0;
26079 char_u *tail;
26080 char_u *s, *p, *pbuf;
26081 char_u dirname[MAXPATHL];
26082 int c;
26083 int has_fullname = 0;
26084#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026085 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 int has_shortname = 0;
26087#endif
26088
26089repeat:
26090 /* ":p" - full path/file_name */
26091 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26092 {
26093 has_fullname = 1;
26094
26095 valid |= VALID_PATH;
26096 *usedlen += 2;
26097
26098 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26099 if ((*fnamep)[0] == '~'
26100#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26101 && ((*fnamep)[1] == '/'
26102# ifdef BACKSLASH_IN_FILENAME
26103 || (*fnamep)[1] == '\\'
26104# endif
26105 || (*fnamep)[1] == NUL)
26106
26107#endif
26108 )
26109 {
26110 *fnamep = expand_env_save(*fnamep);
26111 vim_free(*bufp); /* free any allocated file name */
26112 *bufp = *fnamep;
26113 if (*fnamep == NULL)
26114 return -1;
26115 }
26116
26117 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026118 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026119 {
26120 if (vim_ispathsep(*p)
26121 && p[1] == '.'
26122 && (p[2] == NUL
26123 || vim_ispathsep(p[2])
26124 || (p[2] == '.'
26125 && (p[3] == NUL || vim_ispathsep(p[3])))))
26126 break;
26127 }
26128
26129 /* FullName_save() is slow, don't use it when not needed. */
26130 if (*p != NUL || !vim_isAbsName(*fnamep))
26131 {
26132 *fnamep = FullName_save(*fnamep, *p != NUL);
26133 vim_free(*bufp); /* free any allocated file name */
26134 *bufp = *fnamep;
26135 if (*fnamep == NULL)
26136 return -1;
26137 }
26138
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026139#ifdef WIN3264
26140# if _WIN32_WINNT >= 0x0500
26141 if (vim_strchr(*fnamep, '~') != NULL)
26142 {
26143 /* Expand 8.3 filename to full path. Needed to make sure the same
26144 * file does not have two different names.
26145 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26146 p = alloc(_MAX_PATH + 1);
26147 if (p != NULL)
26148 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026149 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026150 {
26151 vim_free(*bufp);
26152 *bufp = *fnamep = p;
26153 }
26154 else
26155 vim_free(p);
26156 }
26157 }
26158# endif
26159#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 /* Append a path separator to a directory. */
26161 if (mch_isdir(*fnamep))
26162 {
26163 /* Make room for one or two extra characters. */
26164 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26165 vim_free(*bufp); /* free any allocated file name */
26166 *bufp = *fnamep;
26167 if (*fnamep == NULL)
26168 return -1;
26169 add_pathsep(*fnamep);
26170 }
26171 }
26172
26173 /* ":." - path relative to the current directory */
26174 /* ":~" - path relative to the home directory */
26175 /* ":8" - shortname path - postponed till after */
26176 while (src[*usedlen] == ':'
26177 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26178 {
26179 *usedlen += 2;
26180 if (c == '8')
26181 {
26182#ifdef WIN3264
26183 has_shortname = 1; /* Postpone this. */
26184#endif
26185 continue;
26186 }
26187 pbuf = NULL;
26188 /* Need full path first (use expand_env() to remove a "~/") */
26189 if (!has_fullname)
26190 {
26191 if (c == '.' && **fnamep == '~')
26192 p = pbuf = expand_env_save(*fnamep);
26193 else
26194 p = pbuf = FullName_save(*fnamep, FALSE);
26195 }
26196 else
26197 p = *fnamep;
26198
26199 has_fullname = 0;
26200
26201 if (p != NULL)
26202 {
26203 if (c == '.')
26204 {
26205 mch_dirname(dirname, MAXPATHL);
26206 s = shorten_fname(p, dirname);
26207 if (s != NULL)
26208 {
26209 *fnamep = s;
26210 if (pbuf != NULL)
26211 {
26212 vim_free(*bufp); /* free any allocated file name */
26213 *bufp = pbuf;
26214 pbuf = NULL;
26215 }
26216 }
26217 }
26218 else
26219 {
26220 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26221 /* Only replace it when it starts with '~' */
26222 if (*dirname == '~')
26223 {
26224 s = vim_strsave(dirname);
26225 if (s != NULL)
26226 {
26227 *fnamep = s;
26228 vim_free(*bufp);
26229 *bufp = s;
26230 }
26231 }
26232 }
26233 vim_free(pbuf);
26234 }
26235 }
26236
26237 tail = gettail(*fnamep);
26238 *fnamelen = (int)STRLEN(*fnamep);
26239
26240 /* ":h" - head, remove "/file_name", can be repeated */
26241 /* Don't remove the first "/" or "c:\" */
26242 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26243 {
26244 valid |= VALID_HEAD;
26245 *usedlen += 2;
26246 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026247 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026248 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026249 *fnamelen = (int)(tail - *fnamep);
26250#ifdef VMS
26251 if (*fnamelen > 0)
26252 *fnamelen += 1; /* the path separator is part of the path */
26253#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026254 if (*fnamelen == 0)
26255 {
26256 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26257 p = vim_strsave((char_u *)".");
26258 if (p == NULL)
26259 return -1;
26260 vim_free(*bufp);
26261 *bufp = *fnamep = tail = p;
26262 *fnamelen = 1;
26263 }
26264 else
26265 {
26266 while (tail > s && !after_pathsep(s, tail))
26267 mb_ptr_back(*fnamep, tail);
26268 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026269 }
26270
26271 /* ":8" - shortname */
26272 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26273 {
26274 *usedlen += 2;
26275#ifdef WIN3264
26276 has_shortname = 1;
26277#endif
26278 }
26279
26280#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026281 /*
26282 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026283 */
26284 if (has_shortname)
26285 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026286 /* Copy the string if it is shortened by :h and when it wasn't copied
26287 * yet, because we are going to change it in place. Avoids changing
26288 * the buffer name for "%:8". */
26289 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026290 {
26291 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026292 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026293 return -1;
26294 vim_free(*bufp);
26295 *bufp = *fnamep = p;
26296 }
26297
26298 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026299 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026300 if (!has_fullname && !vim_isAbsName(*fnamep))
26301 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026302 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026303 return -1;
26304 }
26305 else
26306 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026307 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026308
Bram Moolenaardc935552011-08-17 15:23:23 +020026309 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026310 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026311 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026312 return -1;
26313
26314 if (l == 0)
26315 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026316 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026317 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026318 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 return -1;
26320 }
26321 *fnamelen = l;
26322 }
26323 }
26324#endif /* WIN3264 */
26325
26326 /* ":t" - tail, just the basename */
26327 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26328 {
26329 *usedlen += 2;
26330 *fnamelen -= (int)(tail - *fnamep);
26331 *fnamep = tail;
26332 }
26333
26334 /* ":e" - extension, can be repeated */
26335 /* ":r" - root, without extension, can be repeated */
26336 while (src[*usedlen] == ':'
26337 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26338 {
26339 /* find a '.' in the tail:
26340 * - for second :e: before the current fname
26341 * - otherwise: The last '.'
26342 */
26343 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26344 s = *fnamep - 2;
26345 else
26346 s = *fnamep + *fnamelen - 1;
26347 for ( ; s > tail; --s)
26348 if (s[0] == '.')
26349 break;
26350 if (src[*usedlen + 1] == 'e') /* :e */
26351 {
26352 if (s > tail)
26353 {
26354 *fnamelen += (int)(*fnamep - (s + 1));
26355 *fnamep = s + 1;
26356#ifdef VMS
26357 /* cut version from the extension */
26358 s = *fnamep + *fnamelen - 1;
26359 for ( ; s > *fnamep; --s)
26360 if (s[0] == ';')
26361 break;
26362 if (s > *fnamep)
26363 *fnamelen = s - *fnamep;
26364#endif
26365 }
26366 else if (*fnamep <= tail)
26367 *fnamelen = 0;
26368 }
26369 else /* :r */
26370 {
26371 if (s > tail) /* remove one extension */
26372 *fnamelen = (int)(s - *fnamep);
26373 }
26374 *usedlen += 2;
26375 }
26376
26377 /* ":s?pat?foo?" - substitute */
26378 /* ":gs?pat?foo?" - global substitute */
26379 if (src[*usedlen] == ':'
26380 && (src[*usedlen + 1] == 's'
26381 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26382 {
26383 char_u *str;
26384 char_u *pat;
26385 char_u *sub;
26386 int sep;
26387 char_u *flags;
26388 int didit = FALSE;
26389
26390 flags = (char_u *)"";
26391 s = src + *usedlen + 2;
26392 if (src[*usedlen + 1] == 'g')
26393 {
26394 flags = (char_u *)"g";
26395 ++s;
26396 }
26397
26398 sep = *s++;
26399 if (sep)
26400 {
26401 /* find end of pattern */
26402 p = vim_strchr(s, sep);
26403 if (p != NULL)
26404 {
26405 pat = vim_strnsave(s, (int)(p - s));
26406 if (pat != NULL)
26407 {
26408 s = p + 1;
26409 /* find end of substitution */
26410 p = vim_strchr(s, sep);
26411 if (p != NULL)
26412 {
26413 sub = vim_strnsave(s, (int)(p - s));
26414 str = vim_strnsave(*fnamep, *fnamelen);
26415 if (sub != NULL && str != NULL)
26416 {
26417 *usedlen = (int)(p + 1 - src);
26418 s = do_string_sub(str, pat, sub, flags);
26419 if (s != NULL)
26420 {
26421 *fnamep = s;
26422 *fnamelen = (int)STRLEN(s);
26423 vim_free(*bufp);
26424 *bufp = s;
26425 didit = TRUE;
26426 }
26427 }
26428 vim_free(sub);
26429 vim_free(str);
26430 }
26431 vim_free(pat);
26432 }
26433 }
26434 /* after using ":s", repeat all the modifiers */
26435 if (didit)
26436 goto repeat;
26437 }
26438 }
26439
Bram Moolenaar26df0922014-02-23 23:39:13 +010026440 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26441 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026442 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026443 c = (*fnamep)[*fnamelen];
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026444 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026445 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026446 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026447 if (p == NULL)
26448 return -1;
26449 vim_free(*bufp);
26450 *bufp = *fnamep = p;
26451 *fnamelen = (int)STRLEN(p);
26452 *usedlen += 2;
26453 }
26454
Bram Moolenaar071d4272004-06-13 20:20:40 +000026455 return valid;
26456}
26457
26458/*
26459 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26460 * "flags" can be "g" to do a global substitute.
26461 * Returns an allocated string, NULL for error.
26462 */
26463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026464do_string_sub(
26465 char_u *str,
26466 char_u *pat,
26467 char_u *sub,
26468 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026469{
26470 int sublen;
26471 regmatch_T regmatch;
26472 int i;
26473 int do_all;
26474 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026475 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026476 garray_T ga;
26477 char_u *ret;
26478 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026479 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026480
26481 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26482 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026483 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026484
26485 ga_init2(&ga, 1, 200);
26486
26487 do_all = (flags[0] == 'g');
26488
26489 regmatch.rm_ic = p_ic;
26490 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26491 if (regmatch.regprog != NULL)
26492 {
26493 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026494 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026495 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26496 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026497 /* Skip empty match except for first match. */
26498 if (regmatch.startp[0] == regmatch.endp[0])
26499 {
26500 if (zero_width == regmatch.startp[0])
26501 {
26502 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026503 i = MB_PTR2LEN(tail);
26504 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26505 (size_t)i);
26506 ga.ga_len += i;
26507 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026508 continue;
26509 }
26510 zero_width = regmatch.startp[0];
26511 }
26512
Bram Moolenaar071d4272004-06-13 20:20:40 +000026513 /*
26514 * Get some space for a temporary buffer to do the substitution
26515 * into. It will contain:
26516 * - The text up to where the match is.
26517 * - The substituted text.
26518 * - The text after the match.
26519 */
26520 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026521 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026522 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26523 {
26524 ga_clear(&ga);
26525 break;
26526 }
26527
26528 /* copy the text up to where the match is */
26529 i = (int)(regmatch.startp[0] - tail);
26530 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26531 /* add the substituted text */
26532 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26533 + ga.ga_len + i, TRUE, TRUE, FALSE);
26534 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026535 tail = regmatch.endp[0];
26536 if (*tail == NUL)
26537 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538 if (!do_all)
26539 break;
26540 }
26541
26542 if (ga.ga_data != NULL)
26543 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26544
Bram Moolenaar473de612013-06-08 18:19:48 +020026545 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026546 }
26547
26548 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26549 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026550 if (p_cpo == empty_option)
26551 p_cpo = save_cpo;
26552 else
26553 /* Darn, evaluating {sub} expression changed the value. */
26554 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026555
26556 return ret;
26557}
26558
26559#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */