blob: 9f6db3123b7dd29458ef84777c8646742ce93334 [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 Moolenaarbee6c0c2016-03-25 15:40:50 +0100295#define VV_NAME(s, t) s, {{t, 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 Moolenaarbee6c0c2016-03-25 15:40:50 +0100300 dictitem16_T vv_di; /* value and name for key (max 16 chars!) */
Bram Moolenaar33570922005-01-25 22:26:29 +0000301 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
302} vimvars[VV_LEN] =
303{
304 /*
305 * The order here must match the VV_ defines in vim.h!
306 * Initializing a union does not work, leave tv.vval empty to get zero's.
307 */
308 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
309 {VV_NAME("count1", VAR_NUMBER), VV_RO},
310 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
311 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
312 {VV_NAME("warningmsg", VAR_STRING), 0},
313 {VV_NAME("statusmsg", VAR_STRING), 0},
314 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
315 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
316 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
317 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
318 {VV_NAME("termresponse", VAR_STRING), VV_RO},
319 {VV_NAME("fname", VAR_STRING), VV_RO},
320 {VV_NAME("lang", VAR_STRING), VV_RO},
321 {VV_NAME("lc_time", VAR_STRING), VV_RO},
322 {VV_NAME("ctype", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
324 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
325 {VV_NAME("fname_in", VAR_STRING), VV_RO},
326 {VV_NAME("fname_out", VAR_STRING), VV_RO},
327 {VV_NAME("fname_new", VAR_STRING), VV_RO},
328 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
329 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
330 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
332 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
333 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
334 {VV_NAME("progname", VAR_STRING), VV_RO},
335 {VV_NAME("servername", VAR_STRING), VV_RO},
336 {VV_NAME("dying", VAR_NUMBER), VV_RO},
337 {VV_NAME("exception", VAR_STRING), VV_RO},
338 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
339 {VV_NAME("register", VAR_STRING), VV_RO},
340 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
341 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000342 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
343 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000344 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000345 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
346 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000347 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
351 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000352 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000353 {VV_NAME("swapname", VAR_STRING), VV_RO},
354 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000355 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200356 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000357 {VV_NAME("mouse_win", VAR_NUMBER), 0},
358 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
359 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000360 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000361 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100362 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000363 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200364 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200365 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200366 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200367 {VV_NAME("option_new", VAR_STRING), VV_RO},
368 {VV_NAME("option_old", VAR_STRING), VV_RO},
369 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100370 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100371 {VV_NAME("false", VAR_SPECIAL), VV_RO},
372 {VV_NAME("true", VAR_SPECIAL), VV_RO},
373 {VV_NAME("null", VAR_SPECIAL), VV_RO},
374 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar14735512016-03-26 21:00:08 +0100375 {VV_NAME("vim_did_enter", VAR_NUMBER), 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);
Bram Moolenaarea6553b2016-03-27 15:13:38 +0200478static void f_assert_match(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100479static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000480#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100481static void f_asin(typval_T *argvars, typval_T *rettv);
482static void f_atan(typval_T *argvars, typval_T *rettv);
483static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000484#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100485static void f_browse(typval_T *argvars, typval_T *rettv);
486static void f_browsedir(typval_T *argvars, typval_T *rettv);
487static void f_bufexists(typval_T *argvars, typval_T *rettv);
488static void f_buflisted(typval_T *argvars, typval_T *rettv);
489static void f_bufloaded(typval_T *argvars, typval_T *rettv);
490static void f_bufname(typval_T *argvars, typval_T *rettv);
491static void f_bufnr(typval_T *argvars, typval_T *rettv);
492static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
493static void f_byte2line(typval_T *argvars, typval_T *rettv);
494static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
495static void f_byteidx(typval_T *argvars, typval_T *rettv);
496static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
497static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000498#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100499static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000500#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100501#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100502static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100503static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
504static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100505static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100506static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar03602ec2016-03-20 20:57:45 +0100507static void f_ch_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100508static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100509static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
510static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100511static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100512static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
514static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100515static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100516static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100517#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100518static void f_changenr(typval_T *argvars, typval_T *rettv);
519static void f_char2nr(typval_T *argvars, typval_T *rettv);
520static void f_cindent(typval_T *argvars, typval_T *rettv);
521static void f_clearmatches(typval_T *argvars, typval_T *rettv);
522static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_complete(typval_T *argvars, typval_T *rettv);
525static void f_complete_add(typval_T *argvars, typval_T *rettv);
526static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000527#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100528static void f_confirm(typval_T *argvars, typval_T *rettv);
529static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000530#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100531static void f_cos(typval_T *argvars, typval_T *rettv);
532static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000533#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100534static void f_count(typval_T *argvars, typval_T *rettv);
535static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
536static void f_cursor(typval_T *argsvars, typval_T *rettv);
537static void f_deepcopy(typval_T *argvars, typval_T *rettv);
538static void f_delete(typval_T *argvars, typval_T *rettv);
539static void f_did_filetype(typval_T *argvars, typval_T *rettv);
540static void f_diff_filler(typval_T *argvars, typval_T *rettv);
541static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100542static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100543static void f_empty(typval_T *argvars, typval_T *rettv);
544static void f_escape(typval_T *argvars, typval_T *rettv);
545static void f_eval(typval_T *argvars, typval_T *rettv);
546static void f_eventhandler(typval_T *argvars, typval_T *rettv);
547static void f_executable(typval_T *argvars, typval_T *rettv);
548static void f_exepath(typval_T *argvars, typval_T *rettv);
549static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200550#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100551static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200552#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100553static void f_expand(typval_T *argvars, typval_T *rettv);
554static void f_extend(typval_T *argvars, typval_T *rettv);
555static void f_feedkeys(typval_T *argvars, typval_T *rettv);
556static void f_filereadable(typval_T *argvars, typval_T *rettv);
557static void f_filewritable(typval_T *argvars, typval_T *rettv);
558static void f_filter(typval_T *argvars, typval_T *rettv);
559static void f_finddir(typval_T *argvars, typval_T *rettv);
560static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_float2nr(typval_T *argvars, typval_T *rettv);
563static void f_floor(typval_T *argvars, typval_T *rettv);
564static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000565#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100566static void f_fnameescape(typval_T *argvars, typval_T *rettv);
567static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
568static void f_foldclosed(typval_T *argvars, typval_T *rettv);
569static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
570static void f_foldlevel(typval_T *argvars, typval_T *rettv);
571static void f_foldtext(typval_T *argvars, typval_T *rettv);
572static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
573static void f_foreground(typval_T *argvars, typval_T *rettv);
574static void f_function(typval_T *argvars, typval_T *rettv);
575static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
576static void f_get(typval_T *argvars, typval_T *rettv);
577static void f_getbufline(typval_T *argvars, typval_T *rettv);
578static void f_getbufvar(typval_T *argvars, typval_T *rettv);
579static void f_getchar(typval_T *argvars, typval_T *rettv);
580static void f_getcharmod(typval_T *argvars, typval_T *rettv);
581static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
582static void f_getcmdline(typval_T *argvars, typval_T *rettv);
583static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
584static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
585static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
586static void f_getcwd(typval_T *argvars, typval_T *rettv);
587static void f_getfontname(typval_T *argvars, typval_T *rettv);
588static void f_getfperm(typval_T *argvars, typval_T *rettv);
589static void f_getfsize(typval_T *argvars, typval_T *rettv);
590static void f_getftime(typval_T *argvars, typval_T *rettv);
591static void f_getftype(typval_T *argvars, typval_T *rettv);
592static void f_getline(typval_T *argvars, typval_T *rettv);
593static void f_getmatches(typval_T *argvars, typval_T *rettv);
594static void f_getpid(typval_T *argvars, typval_T *rettv);
595static void f_getcurpos(typval_T *argvars, typval_T *rettv);
596static void f_getpos(typval_T *argvars, typval_T *rettv);
597static void f_getqflist(typval_T *argvars, typval_T *rettv);
598static void f_getreg(typval_T *argvars, typval_T *rettv);
599static void f_getregtype(typval_T *argvars, typval_T *rettv);
600static void f_gettabvar(typval_T *argvars, typval_T *rettv);
601static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
602static void f_getwinposx(typval_T *argvars, typval_T *rettv);
603static void f_getwinposy(typval_T *argvars, typval_T *rettv);
604static void f_getwinvar(typval_T *argvars, typval_T *rettv);
605static void f_glob(typval_T *argvars, typval_T *rettv);
606static void f_globpath(typval_T *argvars, typval_T *rettv);
607static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
608static void f_has(typval_T *argvars, typval_T *rettv);
609static void f_has_key(typval_T *argvars, typval_T *rettv);
610static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
611static void f_hasmapto(typval_T *argvars, typval_T *rettv);
612static void f_histadd(typval_T *argvars, typval_T *rettv);
613static void f_histdel(typval_T *argvars, typval_T *rettv);
614static void f_histget(typval_T *argvars, typval_T *rettv);
615static void f_histnr(typval_T *argvars, typval_T *rettv);
616static void f_hlID(typval_T *argvars, typval_T *rettv);
617static void f_hlexists(typval_T *argvars, typval_T *rettv);
618static void f_hostname(typval_T *argvars, typval_T *rettv);
619static void f_iconv(typval_T *argvars, typval_T *rettv);
620static void f_indent(typval_T *argvars, typval_T *rettv);
621static void f_index(typval_T *argvars, typval_T *rettv);
622static void f_input(typval_T *argvars, typval_T *rettv);
623static void f_inputdialog(typval_T *argvars, typval_T *rettv);
624static void f_inputlist(typval_T *argvars, typval_T *rettv);
625static void f_inputrestore(typval_T *argvars, typval_T *rettv);
626static void f_inputsave(typval_T *argvars, typval_T *rettv);
627static void f_inputsecret(typval_T *argvars, typval_T *rettv);
628static void f_insert(typval_T *argvars, typval_T *rettv);
629static void f_invert(typval_T *argvars, typval_T *rettv);
630static void f_isdirectory(typval_T *argvars, typval_T *rettv);
631static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100632#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
633static void f_isnan(typval_T *argvars, typval_T *rettv);
634#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100635static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100636#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100637static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100638static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100639static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100640static void f_job_start(typval_T *argvars, typval_T *rettv);
641static void f_job_stop(typval_T *argvars, typval_T *rettv);
642static void f_job_status(typval_T *argvars, typval_T *rettv);
643#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100644static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100645static void f_js_decode(typval_T *argvars, typval_T *rettv);
646static void f_js_encode(typval_T *argvars, typval_T *rettv);
647static void f_json_decode(typval_T *argvars, typval_T *rettv);
648static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100649static void f_keys(typval_T *argvars, typval_T *rettv);
650static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
651static void f_len(typval_T *argvars, typval_T *rettv);
652static void f_libcall(typval_T *argvars, typval_T *rettv);
653static void f_libcallnr(typval_T *argvars, typval_T *rettv);
654static void f_line(typval_T *argvars, typval_T *rettv);
655static void f_line2byte(typval_T *argvars, typval_T *rettv);
656static void f_lispindent(typval_T *argvars, typval_T *rettv);
657static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000658#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_log(typval_T *argvars, typval_T *rettv);
660static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000661#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200662#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100663static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200664#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100665static void f_map(typval_T *argvars, typval_T *rettv);
666static void f_maparg(typval_T *argvars, typval_T *rettv);
667static void f_mapcheck(typval_T *argvars, typval_T *rettv);
668static void f_match(typval_T *argvars, typval_T *rettv);
669static void f_matchadd(typval_T *argvars, typval_T *rettv);
670static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
671static void f_matcharg(typval_T *argvars, typval_T *rettv);
672static void f_matchdelete(typval_T *argvars, typval_T *rettv);
673static void f_matchend(typval_T *argvars, typval_T *rettv);
674static void f_matchlist(typval_T *argvars, typval_T *rettv);
675static void f_matchstr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +0200676static void f_matchstrpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_max(typval_T *argvars, typval_T *rettv);
678static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000679#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100680static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000681#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100682static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100683#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100684static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100685#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
687static void f_nr2char(typval_T *argvars, typval_T *rettv);
688static void f_or(typval_T *argvars, typval_T *rettv);
689static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100690#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100692#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000693#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100694static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000695#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100696static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
697static void f_printf(typval_T *argvars, typval_T *rettv);
698static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200701#endif
702#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100703static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200704#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100705static void f_range(typval_T *argvars, typval_T *rettv);
706static void f_readfile(typval_T *argvars, typval_T *rettv);
707static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100708#ifdef FEAT_FLOAT
709static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
710#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100711static void f_reltimestr(typval_T *argvars, typval_T *rettv);
712static void f_remote_expr(typval_T *argvars, typval_T *rettv);
713static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
714static void f_remote_peek(typval_T *argvars, typval_T *rettv);
715static void f_remote_read(typval_T *argvars, typval_T *rettv);
716static void f_remote_send(typval_T *argvars, typval_T *rettv);
717static void f_remove(typval_T *argvars, typval_T *rettv);
718static void f_rename(typval_T *argvars, typval_T *rettv);
719static void f_repeat(typval_T *argvars, typval_T *rettv);
720static void f_resolve(typval_T *argvars, typval_T *rettv);
721static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000722#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100723static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000724#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100725static void f_screenattr(typval_T *argvars, typval_T *rettv);
726static void f_screenchar(typval_T *argvars, typval_T *rettv);
727static void f_screencol(typval_T *argvars, typval_T *rettv);
728static void f_screenrow(typval_T *argvars, typval_T *rettv);
729static void f_search(typval_T *argvars, typval_T *rettv);
730static void f_searchdecl(typval_T *argvars, typval_T *rettv);
731static void f_searchpair(typval_T *argvars, typval_T *rettv);
732static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
733static void f_searchpos(typval_T *argvars, typval_T *rettv);
734static void f_server2client(typval_T *argvars, typval_T *rettv);
735static void f_serverlist(typval_T *argvars, typval_T *rettv);
736static void f_setbufvar(typval_T *argvars, typval_T *rettv);
737static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
738static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100739static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100740static void f_setline(typval_T *argvars, typval_T *rettv);
741static void f_setloclist(typval_T *argvars, typval_T *rettv);
742static void f_setmatches(typval_T *argvars, typval_T *rettv);
743static void f_setpos(typval_T *argvars, typval_T *rettv);
744static void f_setqflist(typval_T *argvars, typval_T *rettv);
745static void f_setreg(typval_T *argvars, typval_T *rettv);
746static void f_settabvar(typval_T *argvars, typval_T *rettv);
747static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
748static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100749#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100750static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100751#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100752static void f_shellescape(typval_T *argvars, typval_T *rettv);
753static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
754static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000755#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100756static void f_sin(typval_T *argvars, typval_T *rettv);
757static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000758#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100759static void f_sort(typval_T *argvars, typval_T *rettv);
760static void f_soundfold(typval_T *argvars, typval_T *rettv);
761static void f_spellbadword(typval_T *argvars, typval_T *rettv);
762static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
763static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000764#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100765static void f_sqrt(typval_T *argvars, typval_T *rettv);
766static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_str2nr(typval_T *argvars, typval_T *rettv);
769static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000770#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100771static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000772#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100773static void f_stridx(typval_T *argvars, typval_T *rettv);
774static void f_string(typval_T *argvars, typval_T *rettv);
775static void f_strlen(typval_T *argvars, typval_T *rettv);
776static void f_strpart(typval_T *argvars, typval_T *rettv);
777static void f_strridx(typval_T *argvars, typval_T *rettv);
778static void f_strtrans(typval_T *argvars, typval_T *rettv);
779static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
780static void f_strwidth(typval_T *argvars, typval_T *rettv);
781static void f_submatch(typval_T *argvars, typval_T *rettv);
782static void f_substitute(typval_T *argvars, typval_T *rettv);
783static void f_synID(typval_T *argvars, typval_T *rettv);
784static void f_synIDattr(typval_T *argvars, typval_T *rettv);
785static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
786static void f_synstack(typval_T *argvars, typval_T *rettv);
787static void f_synconcealed(typval_T *argvars, typval_T *rettv);
788static void f_system(typval_T *argvars, typval_T *rettv);
789static void f_systemlist(typval_T *argvars, typval_T *rettv);
790static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
791static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
792static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
793static void f_taglist(typval_T *argvars, typval_T *rettv);
794static void f_tagfiles(typval_T *argvars, typval_T *rettv);
795static void f_tempname(typval_T *argvars, typval_T *rettv);
796static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200797#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100798static void f_tan(typval_T *argvars, typval_T *rettv);
799static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200800#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100801#ifdef FEAT_TIMERS
802static void f_timer_start(typval_T *argvars, typval_T *rettv);
803static void f_timer_stop(typval_T *argvars, typval_T *rettv);
804#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100805static void f_tolower(typval_T *argvars, typval_T *rettv);
806static void f_toupper(typval_T *argvars, typval_T *rettv);
807static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000808#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100809static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000810#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100811static void f_type(typval_T *argvars, typval_T *rettv);
812static void f_undofile(typval_T *argvars, typval_T *rettv);
813static void f_undotree(typval_T *argvars, typval_T *rettv);
814static void f_uniq(typval_T *argvars, typval_T *rettv);
815static void f_values(typval_T *argvars, typval_T *rettv);
816static void f_virtcol(typval_T *argvars, typval_T *rettv);
817static void f_visualmode(typval_T *argvars, typval_T *rettv);
818static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100819static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100820static void f_win_getid(typval_T *argvars, typval_T *rettv);
821static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
822static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
823static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100824static void f_winbufnr(typval_T *argvars, typval_T *rettv);
825static void f_wincol(typval_T *argvars, typval_T *rettv);
826static void f_winheight(typval_T *argvars, typval_T *rettv);
827static void f_winline(typval_T *argvars, typval_T *rettv);
828static void f_winnr(typval_T *argvars, typval_T *rettv);
829static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
830static void f_winrestview(typval_T *argvars, typval_T *rettv);
831static void f_winsaveview(typval_T *argvars, typval_T *rettv);
832static void f_winwidth(typval_T *argvars, typval_T *rettv);
833static void f_writefile(typval_T *argvars, typval_T *rettv);
834static void f_wordcount(typval_T *argvars, typval_T *rettv);
835static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000836
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100837static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
838static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
839static int get_env_len(char_u **arg);
840static int get_id_len(char_u **arg);
841static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
842static 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 +0000843#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
844#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
845 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
847static int eval_isnamec(int c);
848static int eval_isnamec1(int c);
849static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
850static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static typval_T *alloc_string_tv(char_u *string);
852static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100853#ifdef FEAT_FLOAT
854static float_T get_tv_float(typval_T *varp);
855#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100856static linenr_T get_tv_lnum(typval_T *argvars);
857static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100858static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
859static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
860static hashtab_T *find_var_ht(char_u *name, char_u **varname);
861static funccall_T *get_funccal(void);
862static void vars_clear_ext(hashtab_T *ht, int free_val);
863static void delete_var(hashtab_T *ht, hashitem_T *hi);
864static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
865static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
866static void set_var(char_u *name, typval_T *varp, int copy);
867static int var_check_ro(int flags, char_u *name, int use_gettext);
868static int var_check_fixed(int flags, char_u *name, int use_gettext);
869static int var_check_func_name(char_u *name, int new_var);
870static int valid_varname(char_u *varname);
871static int tv_check_lock(int lock, char_u *name, int use_gettext);
872static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
873static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100874static 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 +0100875static int eval_fname_script(char_u *p);
876static int eval_fname_sid(char_u *p);
877static void list_func_head(ufunc_T *fp, int indent);
878static ufunc_T *find_func(char_u *name);
879static int function_exists(char_u *name);
880static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000881#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100882static void func_do_profile(ufunc_T *fp);
883static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
884static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000885static int
886# ifdef __BORLANDC__
887 _RTLENTRYF
888# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000890static int
891# ifdef __BORLANDC__
892 _RTLENTRYF
893# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100894 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000895#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100896static int script_autoload(char_u *name, int reload);
897static char_u *autoload_name(char_u *name);
898static void cat_func_name(char_u *buf, ufunc_T *fp);
899static void func_free(ufunc_T *fp);
900static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
901static int can_free_funccal(funccall_T *fc, int copyID) ;
902static void free_funccal(funccall_T *fc, int free_val);
903static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
904static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
905static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
906static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
907static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
908static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
909static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
910static int write_list(FILE *fd, list_T *list, int binary);
911static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000912
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200913
914#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100915static int compare_func_name(const void *s1, const void *s2);
916static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200917#endif
918
Bram Moolenaar33570922005-01-25 22:26:29 +0000919/*
920 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000921 */
922 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100923eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000924{
Bram Moolenaar33570922005-01-25 22:26:29 +0000925 int i;
926 struct vimvar *p;
927
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200928 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
929 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200930 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000931 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000932 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000933
934 for (i = 0; i < VV_LEN; ++i)
935 {
936 p = &vimvars[i];
Bram Moolenaaref9d9b92016-03-28 22:44:50 +0200937 if (STRLEN(p->vv_name) > 16)
938 {
939 EMSG("INTERNAL: name too long, increase size of dictitem16_T");
940 getout(1);
941 }
Bram Moolenaar33570922005-01-25 22:26:29 +0000942 STRCPY(p->vv_di.di_key, p->vv_name);
943 if (p->vv_flags & VV_RO)
944 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
945 else if (p->vv_flags & VV_RO_SBX)
946 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
947 else
948 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000949
950 /* add to v: scope dict, unless the value is not always available */
951 if (p->vv_type != VAR_UNKNOWN)
952 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000953 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000954 /* add to compat scope dict */
955 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000956 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100957 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
958
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000959 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100960 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200961 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100962 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100963
964 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
965 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
966 set_vim_var_nr(VV_NONE, VVAL_NONE);
967 set_vim_var_nr(VV_NULL, VVAL_NULL);
968
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200969 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200970
971#ifdef EBCDIC
972 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100973 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200974 */
975 sortFunctions();
976#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000977}
978
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000979#if defined(EXITFREE) || defined(PROTO)
980 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100981eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000982{
983 int i;
984 struct vimvar *p;
985
986 for (i = 0; i < VV_LEN; ++i)
987 {
988 p = &vimvars[i];
989 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000990 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000991 vim_free(p->vv_str);
992 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000993 }
994 else if (p->vv_di.di_tv.v_type == VAR_LIST)
995 {
996 list_unref(p->vv_list);
997 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000999 }
1000 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +00001001 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001002 hash_clear(&compat_hashtab);
1003
Bram Moolenaard9fba312005-06-26 22:34:35 +00001004 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001005# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001006 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001007# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001008
1009 /* global variables */
1010 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001011
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001012 /* autoloaded script names */
1013 ga_clear_strings(&ga_loaded);
1014
Bram Moolenaarcca74132013-09-25 21:00:28 +02001015 /* Script-local variables. First clear all the variables and in a second
1016 * loop free the scriptvar_T, because a variable in one script might hold
1017 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001018 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001019 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001020 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001021 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001022 ga_clear(&ga_scripts);
1023
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001024 /* unreferenced lists and dicts */
1025 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001026
1027 /* functions */
1028 free_all_functions();
1029 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001030}
1031#endif
1032
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034 * Return the name of the executed function.
1035 */
1036 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001037func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001038{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001039 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001040}
1041
1042/*
1043 * Return the address holding the next breakpoint line for a funccall cookie.
1044 */
1045 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001046func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001047{
Bram Moolenaar33570922005-01-25 22:26:29 +00001048 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001049}
1050
1051/*
1052 * Return the address holding the debug tick for a funccall cookie.
1053 */
1054 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001055func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001056{
Bram Moolenaar33570922005-01-25 22:26:29 +00001057 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001058}
1059
1060/*
1061 * Return the nesting level for a funccall cookie.
1062 */
1063 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001064func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065{
Bram Moolenaar33570922005-01-25 22:26:29 +00001066 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001067}
1068
1069/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001070funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001072/* pointer to list of previously used funccal, still around because some
1073 * item in it is still being used. */
1074funccall_T *previous_funccal = NULL;
1075
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076/*
1077 * Return TRUE when a function was ended by a ":return" command.
1078 */
1079 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001080current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081{
1082 return current_funccal->returned;
1083}
1084
1085
1086/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001087 * Set an internal variable to a string value. Creates the variable if it does
1088 * not already exist.
1089 */
1090 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001091set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001092{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001093 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001094 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095
1096 val = vim_strsave(value);
1097 if (val != NULL)
1098 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001099 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001100 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001101 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001102 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001103 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001104 }
1105 }
1106}
1107
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001108static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001109static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110static char_u *redir_endp = NULL;
1111static char_u *redir_varname = NULL;
1112
1113/*
1114 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001115 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001116 * Returns OK if successfully completed the setup. FAIL otherwise.
1117 */
1118 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001119var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001120{
1121 int save_emsg;
1122 int err;
1123 typval_T tv;
1124
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001125 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001126 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 {
1128 EMSG(_(e_invarg));
1129 return FAIL;
1130 }
1131
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001132 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001133 redir_varname = vim_strsave(name);
1134 if (redir_varname == NULL)
1135 return FAIL;
1136
1137 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1138 if (redir_lval == NULL)
1139 {
1140 var_redir_stop();
1141 return FAIL;
1142 }
1143
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001144 /* The output is stored in growarray "redir_ga" until redirection ends. */
1145 ga_init2(&redir_ga, (int)sizeof(char), 500);
1146
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001148 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001149 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001150 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1151 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001152 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 if (redir_endp != NULL && *redir_endp != NUL)
1154 /* Trailing characters are present after the variable name */
1155 EMSG(_(e_trailing));
1156 else
1157 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001158 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001159 var_redir_stop();
1160 return FAIL;
1161 }
1162
1163 /* check if we can write to the variable: set it to or append an empty
1164 * string */
1165 save_emsg = did_emsg;
1166 did_emsg = FALSE;
1167 tv.v_type = VAR_STRING;
1168 tv.vval.v_string = (char_u *)"";
1169 if (append)
1170 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1171 else
1172 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001173 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001174 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001175 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176 if (err)
1177 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001178 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001179 var_redir_stop();
1180 return FAIL;
1181 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001182
1183 return OK;
1184}
1185
1186/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001187 * Append "value[value_len]" to the variable set by var_redir_start().
1188 * The actual appending is postponed until redirection ends, because the value
1189 * appended may in fact be the string we write to, changing it may cause freed
1190 * memory to be used:
1191 * :redir => foo
1192 * :let foo
1193 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001194 */
1195 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001196var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199
1200 if (redir_lval == NULL)
1201 return;
1202
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001203 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001204 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001206 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001207
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001208 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001209 {
1210 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001211 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001212 }
1213 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001215}
1216
1217/*
1218 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001219 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 */
1221 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001222var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001223{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001224 typval_T tv;
1225
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001226 if (redir_lval != NULL)
1227 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001228 /* If there was no error: assign the text to the variable. */
1229 if (redir_endp != NULL)
1230 {
1231 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1232 tv.v_type = VAR_STRING;
1233 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001234 /* Call get_lval() again, if it's inside a Dict or List it may
1235 * have changed. */
1236 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001237 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001238 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1239 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1240 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001241 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001242
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001243 /* free the collected output */
1244 vim_free(redir_ga.ga_data);
1245 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001246
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001247 vim_free(redir_lval);
1248 redir_lval = NULL;
1249 }
1250 vim_free(redir_varname);
1251 redir_varname = NULL;
1252}
1253
Bram Moolenaar071d4272004-06-13 20:20:40 +00001254# if defined(FEAT_MBYTE) || defined(PROTO)
1255 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001256eval_charconvert(
1257 char_u *enc_from,
1258 char_u *enc_to,
1259 char_u *fname_from,
1260 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001261{
1262 int err = FALSE;
1263
1264 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1265 set_vim_var_string(VV_CC_TO, enc_to, -1);
1266 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1267 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1268 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1269 err = TRUE;
1270 set_vim_var_string(VV_CC_FROM, NULL, -1);
1271 set_vim_var_string(VV_CC_TO, NULL, -1);
1272 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1273 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1274
1275 if (err)
1276 return FAIL;
1277 return OK;
1278}
1279# endif
1280
1281# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1282 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001283eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001284{
1285 int err = FALSE;
1286
1287 set_vim_var_string(VV_FNAME_IN, fname, -1);
1288 set_vim_var_string(VV_CMDARG, args, -1);
1289 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1290 err = TRUE;
1291 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1292 set_vim_var_string(VV_CMDARG, NULL, -1);
1293
1294 if (err)
1295 {
1296 mch_remove(fname);
1297 return FAIL;
1298 }
1299 return OK;
1300}
1301# endif
1302
1303# if defined(FEAT_DIFF) || defined(PROTO)
1304 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001305eval_diff(
1306 char_u *origfile,
1307 char_u *newfile,
1308 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001309{
1310 int err = FALSE;
1311
1312 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1313 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1314 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1315 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1316 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1317 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1318 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1319}
1320
1321 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001322eval_patch(
1323 char_u *origfile,
1324 char_u *difffile,
1325 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001326{
1327 int err;
1328
1329 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1330 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1331 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1332 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1333 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1334 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1335 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1336}
1337# endif
1338
1339/*
1340 * Top level evaluation function, returning a boolean.
1341 * Sets "error" to TRUE if there was an error.
1342 * Return TRUE or FALSE.
1343 */
1344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001345eval_to_bool(
1346 char_u *arg,
1347 int *error,
1348 char_u **nextcmd,
1349 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001350{
Bram Moolenaar33570922005-01-25 22:26:29 +00001351 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 int retval = FALSE;
1353
1354 if (skip)
1355 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001356 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001357 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001358 else
1359 {
1360 *error = FALSE;
1361 if (!skip)
1362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001363 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001364 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001365 }
1366 }
1367 if (skip)
1368 --emsg_skip;
1369
1370 return retval;
1371}
1372
1373/*
1374 * Top level evaluation function, returning a string. If "skip" is TRUE,
1375 * only parsing to "nextcmd" is done, without reporting errors. Return
1376 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1377 */
1378 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001379eval_to_string_skip(
1380 char_u *arg,
1381 char_u **nextcmd,
1382 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001383{
Bram Moolenaar33570922005-01-25 22:26:29 +00001384 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 char_u *retval;
1386
1387 if (skip)
1388 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001389 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001390 retval = NULL;
1391 else
1392 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001393 retval = vim_strsave(get_tv_string(&tv));
1394 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001395 }
1396 if (skip)
1397 --emsg_skip;
1398
1399 return retval;
1400}
1401
1402/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403 * Skip over an expression at "*pp".
1404 * Return FAIL for an error, OK otherwise.
1405 */
1406 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001407skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001408{
Bram Moolenaar33570922005-01-25 22:26:29 +00001409 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001410
1411 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001412 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001413}
1414
1415/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001417 * When "convert" is TRUE convert a List into a sequence of lines and convert
1418 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001419 * Return pointer to allocated memory, or NULL for failure.
1420 */
1421 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001422eval_to_string(
1423 char_u *arg,
1424 char_u **nextcmd,
1425 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001426{
Bram Moolenaar33570922005-01-25 22:26:29 +00001427 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001428 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001430#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001431 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001432#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001433
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001434 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001435 retval = NULL;
1436 else
1437 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001438 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001439 {
1440 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001441 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001442 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001443 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001444 if (tv.vval.v_list->lv_len > 0)
1445 ga_append(&ga, NL);
1446 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001447 ga_append(&ga, NUL);
1448 retval = (char_u *)ga.ga_data;
1449 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001450#ifdef FEAT_FLOAT
1451 else if (convert && tv.v_type == VAR_FLOAT)
1452 {
1453 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1454 retval = vim_strsave(numbuf);
1455 }
1456#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001457 else
1458 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001459 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001460 }
1461
1462 return retval;
1463}
1464
1465/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001466 * Call eval_to_string() without using current local variables and using
1467 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468 */
1469 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001470eval_to_string_safe(
1471 char_u *arg,
1472 char_u **nextcmd,
1473 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001474{
1475 char_u *retval;
1476 void *save_funccalp;
1477
1478 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001479 if (use_sandbox)
1480 ++sandbox;
1481 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001482 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001483 if (use_sandbox)
1484 --sandbox;
1485 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001486 restore_funccal(save_funccalp);
1487 return retval;
1488}
1489
Bram Moolenaar071d4272004-06-13 20:20:40 +00001490/*
1491 * Top level evaluation function, returning a number.
1492 * Evaluates "expr" silently.
1493 * Returns -1 for an error.
1494 */
1495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001496eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001497{
Bram Moolenaar33570922005-01-25 22:26:29 +00001498 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001500 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001501
1502 ++emsg_off;
1503
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001504 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001505 retval = -1;
1506 else
1507 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001508 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001509 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001510 }
1511 --emsg_off;
1512
1513 return retval;
1514}
1515
Bram Moolenaara40058a2005-07-11 22:42:07 +00001516/*
1517 * Prepare v: variable "idx" to be used.
1518 * Save the current typeval in "save_tv".
1519 * When not used yet add the variable to the v: hashtable.
1520 */
1521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001522prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001523{
1524 *save_tv = vimvars[idx].vv_tv;
1525 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1526 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1527}
1528
1529/*
1530 * Restore v: variable "idx" to typeval "save_tv".
1531 * When no longer defined, remove the variable from the v: hashtable.
1532 */
1533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001534restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001535{
1536 hashitem_T *hi;
1537
Bram Moolenaara40058a2005-07-11 22:42:07 +00001538 vimvars[idx].vv_tv = *save_tv;
1539 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1540 {
1541 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1542 if (HASHITEM_EMPTY(hi))
1543 EMSG2(_(e_intern2), "restore_vimvar()");
1544 else
1545 hash_remove(&vimvarht, hi);
1546 }
1547}
1548
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001549#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001550/*
1551 * Evaluate an expression to a list with suggestions.
1552 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001553 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001554 */
1555 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001556eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001557{
1558 typval_T save_val;
1559 typval_T rettv;
1560 list_T *list = NULL;
1561 char_u *p = skipwhite(expr);
1562
1563 /* Set "v:val" to the bad word. */
1564 prepare_vimvar(VV_VAL, &save_val);
1565 vimvars[VV_VAL].vv_type = VAR_STRING;
1566 vimvars[VV_VAL].vv_str = badword;
1567 if (p_verbose == 0)
1568 ++emsg_off;
1569
1570 if (eval1(&p, &rettv, TRUE) == OK)
1571 {
1572 if (rettv.v_type != VAR_LIST)
1573 clear_tv(&rettv);
1574 else
1575 list = rettv.vval.v_list;
1576 }
1577
1578 if (p_verbose == 0)
1579 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001580 restore_vimvar(VV_VAL, &save_val);
1581
1582 return list;
1583}
1584
1585/*
1586 * "list" is supposed to contain two items: a word and a number. Return the
1587 * word in "pp" and the number as the return value.
1588 * Return -1 if anything isn't right.
1589 * Used to get the good word and score from the eval_spell_expr() result.
1590 */
1591 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001592get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001593{
1594 listitem_T *li;
1595
1596 li = list->lv_first;
1597 if (li == NULL)
1598 return -1;
1599 *pp = get_tv_string(&li->li_tv);
1600
1601 li = li->li_next;
1602 if (li == NULL)
1603 return -1;
1604 return get_tv_number(&li->li_tv);
1605}
1606#endif
1607
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001608/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 * Top level evaluation function.
1610 * Returns an allocated typval_T with the result.
1611 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001612 */
1613 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001614eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001615{
1616 typval_T *tv;
1617
1618 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001619 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001620 {
1621 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001622 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001623 }
1624
1625 return tv;
1626}
1627
1628
Bram Moolenaar071d4272004-06-13 20:20:40 +00001629/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001630 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001631 * Uses argv[argc] for the function arguments. Only Number and String
1632 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001633 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001634 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001635 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001636call_vim_function(
1637 char_u *func,
1638 int argc,
1639 char_u **argv,
1640 int safe, /* use the sandbox */
1641 int str_arg_only, /* all arguments are strings */
1642 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643{
Bram Moolenaar33570922005-01-25 22:26:29 +00001644 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645 long n;
1646 int len;
1647 int i;
1648 int doesrange;
1649 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001650 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001651
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001652 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001653 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001654 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001655
1656 for (i = 0; i < argc; i++)
1657 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001658 /* Pass a NULL or empty argument as an empty string */
1659 if (argv[i] == NULL || *argv[i] == NUL)
1660 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001661 argvars[i].v_type = VAR_STRING;
1662 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001663 continue;
1664 }
1665
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001666 if (str_arg_only)
1667 len = 0;
1668 else
1669 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001670 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671 if (len != 0 && len == (int)STRLEN(argv[i]))
1672 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001673 argvars[i].v_type = VAR_NUMBER;
1674 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001675 }
1676 else
1677 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001678 argvars[i].v_type = VAR_STRING;
1679 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001680 }
1681 }
1682
1683 if (safe)
1684 {
1685 save_funccalp = save_funccal();
1686 ++sandbox;
1687 }
1688
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001689 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1690 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001691 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001692 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001693 if (safe)
1694 {
1695 --sandbox;
1696 restore_funccal(save_funccalp);
1697 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001698 vim_free(argvars);
1699
1700 if (ret == FAIL)
1701 clear_tv(rettv);
1702
1703 return ret;
1704}
1705
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001706/*
1707 * Call vimL function "func" and return the result as a number.
1708 * Returns -1 when calling the function fails.
1709 * Uses argv[argc] for the function arguments.
1710 */
1711 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001712call_func_retnr(
1713 char_u *func,
1714 int argc,
1715 char_u **argv,
1716 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001717{
1718 typval_T rettv;
1719 long retval;
1720
1721 /* All arguments are passed as strings, no conversion to number. */
1722 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1723 return -1;
1724
1725 retval = get_tv_number_chk(&rettv, NULL);
1726 clear_tv(&rettv);
1727 return retval;
1728}
1729
1730#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1731 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1732
Bram Moolenaar4f688582007-07-24 12:34:30 +00001733# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001734/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001735 * Call vimL function "func" and return the result as a string.
1736 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001737 * Uses argv[argc] for the function arguments.
1738 */
1739 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001740call_func_retstr(
1741 char_u *func,
1742 int argc,
1743 char_u **argv,
1744 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745{
1746 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001747 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001749 /* All arguments are passed as strings, no conversion to number. */
1750 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 return NULL;
1752
1753 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001754 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001755 return retval;
1756}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001757# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001758
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001759/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001760 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001761 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001762 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001763 */
1764 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001765call_func_retlist(
1766 char_u *func,
1767 int argc,
1768 char_u **argv,
1769 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001770{
1771 typval_T rettv;
1772
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001773 /* All arguments are passed as strings, no conversion to number. */
1774 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001775 return NULL;
1776
1777 if (rettv.v_type != VAR_LIST)
1778 {
1779 clear_tv(&rettv);
1780 return NULL;
1781 }
1782
1783 return rettv.vval.v_list;
1784}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785#endif
1786
1787/*
1788 * Save the current function call pointer, and set it to NULL.
1789 * Used when executing autocommands and for ":source".
1790 */
1791 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001792save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001793{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001794 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001795
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796 current_funccal = NULL;
1797 return (void *)fc;
1798}
1799
1800 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001801restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001802{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001803 funccall_T *fc = (funccall_T *)vfc;
1804
1805 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001806}
1807
Bram Moolenaar05159a02005-02-26 23:04:13 +00001808#if defined(FEAT_PROFILE) || defined(PROTO)
1809/*
1810 * Prepare profiling for entering a child or something else that is not
1811 * counted for the script/function itself.
1812 * Should always be called in pair with prof_child_exit().
1813 */
1814 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001815prof_child_enter(
1816 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001817{
1818 funccall_T *fc = current_funccal;
1819
1820 if (fc != NULL && fc->func->uf_profiling)
1821 profile_start(&fc->prof_child);
1822 script_prof_save(tm);
1823}
1824
1825/*
1826 * Take care of time spent in a child.
1827 * Should always be called after prof_child_enter().
1828 */
1829 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001830prof_child_exit(
1831 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001832{
1833 funccall_T *fc = current_funccal;
1834
1835 if (fc != NULL && fc->func->uf_profiling)
1836 {
1837 profile_end(&fc->prof_child);
1838 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1839 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1840 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1841 }
1842 script_prof_restore(tm);
1843}
1844#endif
1845
1846
Bram Moolenaar071d4272004-06-13 20:20:40 +00001847#ifdef FEAT_FOLDING
1848/*
1849 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1850 * it in "*cp". Doesn't give error messages.
1851 */
1852 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001853eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854{
Bram Moolenaar33570922005-01-25 22:26:29 +00001855 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001856 int retval;
1857 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001858 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1859 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001860
1861 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001862 if (use_sandbox)
1863 ++sandbox;
1864 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001865 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001866 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001867 retval = 0;
1868 else
1869 {
1870 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001871 if (tv.v_type == VAR_NUMBER)
1872 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001873 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 retval = 0;
1875 else
1876 {
1877 /* If the result is a string, check if there is a non-digit before
1878 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001879 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880 if (!VIM_ISDIGIT(*s) && *s != '-')
1881 *cp = *s++;
1882 retval = atol((char *)s);
1883 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001884 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885 }
1886 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001887 if (use_sandbox)
1888 --sandbox;
1889 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001890
1891 return retval;
1892}
1893#endif
1894
Bram Moolenaar071d4272004-06-13 20:20:40 +00001895/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 * ":let" list all variable values
1897 * ":let var1 var2" list variable values
1898 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001899 * ":let var += expr" assignment command.
1900 * ":let var -= expr" assignment command.
1901 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001903 */
1904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001905ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906{
1907 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001908 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001909 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001911 int var_count = 0;
1912 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001913 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001914 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001915 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001916
Bram Moolenaardb552d602006-03-23 22:59:57 +00001917 argend = skip_var_list(arg, &var_count, &semicolon);
1918 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001920 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1921 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001922 expr = skipwhite(argend);
1923 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1924 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001926 /*
1927 * ":let" without "=": list variables
1928 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001929 if (*arg == '[')
1930 EMSG(_(e_invarg));
1931 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001932 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001933 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001934 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001935 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001936 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 list_glob_vars(&first);
1938 list_buf_vars(&first);
1939 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001940#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001941 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001942#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001943 list_script_vars(&first);
1944 list_func_vars(&first);
1945 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001946 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 eap->nextcmd = check_nextcmd(arg);
1948 }
1949 else
1950 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001951 op[0] = '=';
1952 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001953 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001954 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001955 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1956 op[0] = *expr; /* +=, -= or .= */
1957 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001958 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001959 else
1960 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001961
Bram Moolenaar071d4272004-06-13 20:20:40 +00001962 if (eap->skip)
1963 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001964 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001965 if (eap->skip)
1966 {
1967 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001968 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001969 --emsg_skip;
1970 }
1971 else if (i != FAIL)
1972 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001973 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001974 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001975 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001976 }
1977 }
1978}
1979
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980/*
1981 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1982 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001983 * When "nextchars" is not NULL it points to a string with characters that
1984 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1985 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986 * Returns OK or FAIL;
1987 */
1988 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001989ex_let_vars(
1990 char_u *arg_start,
1991 typval_T *tv,
1992 int copy, /* copy values from "tv", don't move */
1993 int semicolon, /* from skip_var_list() */
1994 int var_count, /* from skip_var_list() */
1995 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996{
1997 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001998 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00002000 listitem_T *item;
2001 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002002
2003 if (*arg != '[')
2004 {
2005 /*
2006 * ":let var = expr" or ":for var in list"
2007 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002008 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002009 return FAIL;
2010 return OK;
2011 }
2012
2013 /*
2014 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2015 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002016 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002017 {
2018 EMSG(_(e_listreq));
2019 return FAIL;
2020 }
2021
2022 i = list_len(l);
2023 if (semicolon == 0 && var_count < i)
2024 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002025 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002026 return FAIL;
2027 }
2028 if (var_count - semicolon > i)
2029 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002030 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002031 return FAIL;
2032 }
2033
2034 item = l->lv_first;
2035 while (*arg != ']')
2036 {
2037 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002038 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002039 item = item->li_next;
2040 if (arg == NULL)
2041 return FAIL;
2042
2043 arg = skipwhite(arg);
2044 if (*arg == ';')
2045 {
2046 /* Put the rest of the list (may be empty) in the var after ';'.
2047 * Create a new list for this. */
2048 l = list_alloc();
2049 if (l == NULL)
2050 return FAIL;
2051 while (item != NULL)
2052 {
2053 list_append_tv(l, &item->li_tv);
2054 item = item->li_next;
2055 }
2056
2057 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002058 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002059 ltv.vval.v_list = l;
2060 l->lv_refcount = 1;
2061
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002062 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2063 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002064 clear_tv(&ltv);
2065 if (arg == NULL)
2066 return FAIL;
2067 break;
2068 }
2069 else if (*arg != ',' && *arg != ']')
2070 {
2071 EMSG2(_(e_intern2), "ex_let_vars()");
2072 return FAIL;
2073 }
2074 }
2075
2076 return OK;
2077}
2078
2079/*
2080 * Skip over assignable variable "var" or list of variables "[var, var]".
2081 * Used for ":let varvar = expr" and ":for varvar in expr".
2082 * For "[var, var]" increment "*var_count" for each variable.
2083 * for "[var, var; var]" set "semicolon".
2084 * Return NULL for an error.
2085 */
2086 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002087skip_var_list(
2088 char_u *arg,
2089 int *var_count,
2090 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002091{
2092 char_u *p, *s;
2093
2094 if (*arg == '[')
2095 {
2096 /* "[var, var]": find the matching ']'. */
2097 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002098 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002099 {
2100 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2101 s = skip_var_one(p);
2102 if (s == p)
2103 {
2104 EMSG2(_(e_invarg2), p);
2105 return NULL;
2106 }
2107 ++*var_count;
2108
2109 p = skipwhite(s);
2110 if (*p == ']')
2111 break;
2112 else if (*p == ';')
2113 {
2114 if (*semicolon == 1)
2115 {
2116 EMSG(_("Double ; in list of variables"));
2117 return NULL;
2118 }
2119 *semicolon = 1;
2120 }
2121 else if (*p != ',')
2122 {
2123 EMSG2(_(e_invarg2), p);
2124 return NULL;
2125 }
2126 }
2127 return p + 1;
2128 }
2129 else
2130 return skip_var_one(arg);
2131}
2132
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002133/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002134 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002135 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002136 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002137 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002138skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002139{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002140 if (*arg == '@' && arg[1] != NUL)
2141 return arg + 2;
2142 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2143 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002144}
2145
Bram Moolenaara7043832005-01-21 11:56:39 +00002146/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 * List variables for hashtab "ht" with prefix "prefix".
2148 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002151list_hashtable_vars(
2152 hashtab_T *ht,
2153 char_u *prefix,
2154 int empty,
2155 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002156{
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 hashitem_T *hi;
2158 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002159 int todo;
2160
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002161 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002162 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2163 {
2164 if (!HASHITEM_EMPTY(hi))
2165 {
2166 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002167 di = HI2DI(hi);
2168 if (empty || di->di_tv.v_type != VAR_STRING
2169 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002170 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002171 }
2172 }
2173}
2174
2175/*
2176 * List global variables.
2177 */
2178 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002179list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002180{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002181 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002182}
2183
2184/*
2185 * List buffer variables.
2186 */
2187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002188list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002189{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002190 char_u numbuf[NUMBUFLEN];
2191
Bram Moolenaar429fa852013-04-15 12:27:36 +02002192 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002193 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002194
2195 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002196 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2197 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198}
2199
2200/*
2201 * List window variables.
2202 */
2203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002204list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002205{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002206 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002207 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002208}
2209
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210#ifdef FEAT_WINDOWS
2211/*
2212 * List tab page variables.
2213 */
2214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002215list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002216{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002217 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002219}
2220#endif
2221
Bram Moolenaara7043832005-01-21 11:56:39 +00002222/*
2223 * List Vim variables.
2224 */
2225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002226list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002227{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002229}
2230
2231/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002232 * List script-local variables, if there is a script.
2233 */
2234 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002235list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002236{
2237 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002238 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2239 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002240}
2241
2242/*
2243 * List function variables, if there is a function.
2244 */
2245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002246list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002247{
2248 if (current_funccal != NULL)
2249 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002250 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002251}
2252
2253/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002254 * List variables in "arg".
2255 */
2256 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002257list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002258{
2259 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002260 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002261 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 char_u *name_start;
2263 char_u *arg_subsc;
2264 char_u *tofree;
2265 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002266
2267 while (!ends_excmd(*arg) && !got_int)
2268 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002271 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002272 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2273 {
2274 emsg_severe = TRUE;
2275 EMSG(_(e_trailing));
2276 break;
2277 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002278 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002279 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002280 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002281 /* get_name_len() takes care of expanding curly braces */
2282 name_start = name = arg;
2283 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2284 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002285 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002286 /* This is mainly to keep test 49 working: when expanding
2287 * curly braces fails overrule the exception error message. */
2288 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002289 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002290 emsg_severe = TRUE;
2291 EMSG2(_(e_invarg2), arg);
2292 break;
2293 }
2294 error = TRUE;
2295 }
2296 else
2297 {
2298 if (tofree != NULL)
2299 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002300 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002301 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 else
2303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 /* handle d.key, l[idx], f(expr) */
2305 arg_subsc = arg;
2306 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002308 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002309 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002310 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002312 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002313 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002314 case 'g': list_glob_vars(first); break;
2315 case 'b': list_buf_vars(first); break;
2316 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002317#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002318 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002319#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002320 case 'v': list_vim_vars(first); break;
2321 case 's': list_script_vars(first); break;
2322 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002323 default:
2324 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002325 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002326 }
2327 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002328 {
2329 char_u numbuf[NUMBUFLEN];
2330 char_u *tf;
2331 int c;
2332 char_u *s;
2333
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002334 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002335 c = *arg;
2336 *arg = NUL;
2337 list_one_var_a((char_u *)"",
2338 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002339 tv.v_type,
2340 s == NULL ? (char_u *)"" : s,
2341 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342 *arg = c;
2343 vim_free(tf);
2344 }
2345 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002347 }
2348 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002349
2350 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002351 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002352
2353 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002354 }
2355
2356 return arg;
2357}
2358
2359/*
2360 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2361 * Returns a pointer to the char just after the var name.
2362 * Returns NULL if there is an error.
2363 */
2364 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002365ex_let_one(
2366 char_u *arg, /* points to variable name */
2367 typval_T *tv, /* value to assign to variable */
2368 int copy, /* copy value from "tv" */
2369 char_u *endchars, /* valid chars after variable name or NULL */
2370 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002371{
2372 int c1;
2373 char_u *name;
2374 char_u *p;
2375 char_u *arg_end = NULL;
2376 int len;
2377 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002378 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002379
2380 /*
2381 * ":let $VAR = expr": Set environment variable.
2382 */
2383 if (*arg == '$')
2384 {
2385 /* Find the end of the name. */
2386 ++arg;
2387 name = arg;
2388 len = get_env_len(&arg);
2389 if (len == 0)
2390 EMSG2(_(e_invarg2), name - 1);
2391 else
2392 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002393 if (op != NULL && (*op == '+' || *op == '-'))
2394 EMSG2(_(e_letwrong), op);
2395 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002396 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002397 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002398 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002399 {
2400 c1 = name[len];
2401 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002402 p = get_tv_string_chk(tv);
2403 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002404 {
2405 int mustfree = FALSE;
2406 char_u *s = vim_getenv(name, &mustfree);
2407
2408 if (s != NULL)
2409 {
2410 p = tofree = concat_str(s, p);
2411 if (mustfree)
2412 vim_free(s);
2413 }
2414 }
2415 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002416 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002417 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002418 if (STRICMP(name, "HOME") == 0)
2419 init_homedir();
2420 else if (didset_vim && STRICMP(name, "VIM") == 0)
2421 didset_vim = FALSE;
2422 else if (didset_vimruntime
2423 && STRICMP(name, "VIMRUNTIME") == 0)
2424 didset_vimruntime = FALSE;
2425 arg_end = arg;
2426 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002427 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002428 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002429 }
2430 }
2431 }
2432
2433 /*
2434 * ":let &option = expr": Set option value.
2435 * ":let &l:option = expr": Set local option value.
2436 * ":let &g:option = expr": Set global option value.
2437 */
2438 else if (*arg == '&')
2439 {
2440 /* Find the end of the name. */
2441 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002442 if (p == NULL || (endchars != NULL
2443 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002444 EMSG(_(e_letunexp));
2445 else
2446 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002447 long n;
2448 int opt_type;
2449 long numval;
2450 char_u *stringval = NULL;
2451 char_u *s;
2452
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002453 c1 = *p;
2454 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002455
2456 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002457 s = get_tv_string_chk(tv); /* != NULL if number or string */
2458 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002459 {
2460 opt_type = get_option_value(arg, &numval,
2461 &stringval, opt_flags);
2462 if ((opt_type == 1 && *op == '.')
2463 || (opt_type == 0 && *op != '.'))
2464 EMSG2(_(e_letwrong), op);
2465 else
2466 {
2467 if (opt_type == 1) /* number */
2468 {
2469 if (*op == '+')
2470 n = numval + n;
2471 else
2472 n = numval - n;
2473 }
2474 else if (opt_type == 0 && stringval != NULL) /* string */
2475 {
2476 s = concat_str(stringval, s);
2477 vim_free(stringval);
2478 stringval = s;
2479 }
2480 }
2481 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002482 if (s != NULL)
2483 {
2484 set_option_value(arg, n, s, opt_flags);
2485 arg_end = p;
2486 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002487 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002489 }
2490 }
2491
2492 /*
2493 * ":let @r = expr": Set register contents.
2494 */
2495 else if (*arg == '@')
2496 {
2497 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002498 if (op != NULL && (*op == '+' || *op == '-'))
2499 EMSG2(_(e_letwrong), op);
2500 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002501 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002502 EMSG(_(e_letunexp));
2503 else
2504 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002505 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 char_u *s;
2507
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002508 p = get_tv_string_chk(tv);
2509 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002511 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002512 if (s != NULL)
2513 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002514 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002515 vim_free(s);
2516 }
2517 }
2518 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002519 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002520 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002521 arg_end = arg + 1;
2522 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002523 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 }
2525 }
2526
2527 /*
2528 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002529 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002530 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002531 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002532 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002533 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002534
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002535 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002538 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2539 EMSG(_(e_letunexp));
2540 else
2541 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002542 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002543 arg_end = p;
2544 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002545 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002546 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002547 }
2548
2549 else
2550 EMSG2(_(e_invarg2), arg);
2551
2552 return arg_end;
2553}
2554
2555/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002556 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2557 */
2558 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002559check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002560{
2561 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2562 {
2563 EMSG2(_(e_readonlyvar), arg);
2564 return TRUE;
2565 }
2566 return FALSE;
2567}
2568
2569/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002570 * Get an lval: variable, Dict item or List item that can be assigned a value
2571 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2572 * "name.key", "name.key[expr]" etc.
2573 * Indexing only works if "name" is an existing List or Dictionary.
2574 * "name" points to the start of the name.
2575 * If "rettv" is not NULL it points to the value to be assigned.
2576 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2577 * wrong; must end in space or cmd separator.
2578 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002579 * flags:
2580 * GLV_QUIET: do not give error messages
2581 * GLV_NO_AUTOLOAD: do not use script autoloading
2582 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002583 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002584 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002585 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586 */
2587 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002588get_lval(
2589 char_u *name,
2590 typval_T *rettv,
2591 lval_T *lp,
2592 int unlet,
2593 int skip,
2594 int flags, /* GLV_ values */
2595 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002596{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002597 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002598 char_u *expr_start, *expr_end;
2599 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002600 dictitem_T *v;
2601 typval_T var1;
2602 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002603 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002604 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002605 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002606 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002607 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002608 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002609
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002610 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002611 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612
2613 if (skip)
2614 {
2615 /* When skipping just find the end of the name. */
2616 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002617 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002618 }
2619
2620 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002621 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002622 if (expr_start != NULL)
2623 {
2624 /* Don't expand the name when we already know there is an error. */
2625 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2626 && *p != '[' && *p != '.')
2627 {
2628 EMSG(_(e_trailing));
2629 return NULL;
2630 }
2631
2632 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2633 if (lp->ll_exp_name == NULL)
2634 {
2635 /* Report an invalid expression in braces, unless the
2636 * expression evaluation has been cancelled due to an
2637 * aborting error, an interrupt, or an exception. */
2638 if (!aborting() && !quiet)
2639 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002640 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002641 EMSG2(_(e_invarg2), name);
2642 return NULL;
2643 }
2644 }
2645 lp->ll_name = lp->ll_exp_name;
2646 }
2647 else
2648 lp->ll_name = name;
2649
2650 /* Without [idx] or .key we are done. */
2651 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2652 return p;
2653
2654 cc = *p;
2655 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002656 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 if (v == NULL && !quiet)
2658 EMSG2(_(e_undefvar), lp->ll_name);
2659 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002660 if (v == NULL)
2661 return NULL;
2662
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 /*
2664 * Loop until no more [idx] or .key is following.
2665 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002666 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2670 && !(lp->ll_tv->v_type == VAR_DICT
2671 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_("E689: Can only index a List or Dictionary"));
2675 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002677 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002678 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002679 if (!quiet)
2680 EMSG(_("E708: [:] must come last"));
2681 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002682 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002683
Bram Moolenaar8c711452005-01-14 21:53:12 +00002684 len = -1;
2685 if (*p == '.')
2686 {
2687 key = p + 1;
2688 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2689 ;
2690 if (len == 0)
2691 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002692 if (!quiet)
2693 EMSG(_(e_emptykey));
2694 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002695 }
2696 p = key + len;
2697 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 else
2699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002701 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002702 if (*p == ':')
2703 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002704 else
2705 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002706 empty1 = FALSE;
2707 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002708 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002709 if (get_tv_string_chk(&var1) == NULL)
2710 {
2711 /* not a number or string */
2712 clear_tv(&var1);
2713 return NULL;
2714 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 }
2716
2717 /* Optionally get the second index [ :expr]. */
2718 if (*p == ':')
2719 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002721 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002723 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002724 if (!empty1)
2725 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002726 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002727 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002728 if (rettv != NULL && (rettv->v_type != VAR_LIST
2729 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002731 if (!quiet)
2732 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (!empty1)
2734 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002735 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002736 }
2737 p = skipwhite(p + 1);
2738 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002739 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002740 else
2741 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002742 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002743 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2744 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002745 if (!empty1)
2746 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002747 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002748 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002749 if (get_tv_string_chk(&var2) == NULL)
2750 {
2751 /* not a number or string */
2752 if (!empty1)
2753 clear_tv(&var1);
2754 clear_tv(&var2);
2755 return NULL;
2756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002758 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002759 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002762
Bram Moolenaar8c711452005-01-14 21:53:12 +00002763 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002764 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 if (!quiet)
2766 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002767 if (!empty1)
2768 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002769 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002770 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002771 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002772 }
2773
2774 /* Skip to past ']'. */
2775 ++p;
2776 }
2777
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002778 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002779 {
2780 if (len == -1)
2781 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002782 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002783 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 if (*key == NUL)
2785 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002786 if (!quiet)
2787 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002788 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002789 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002790 }
2791 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002792 lp->ll_list = NULL;
2793 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002794 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002795
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002796 /* When assigning to a scope dictionary check that a function and
2797 * variable name is valid (only variable name unless it is l: or
2798 * g: dictionary). Disallow overwriting a builtin function. */
2799 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002800 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 int prevval;
2802 int wrong;
2803
2804 if (len != -1)
2805 {
2806 prevval = key[len];
2807 key[len] = NUL;
2808 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002809 else
2810 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002811 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2812 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002814 || !valid_varname(key);
2815 if (len != -1)
2816 key[len] = prevval;
2817 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002818 return NULL;
2819 }
2820
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002823 /* Can't add "v:" variable. */
2824 if (lp->ll_dict == &vimvardict)
2825 {
2826 EMSG2(_(e_illvar), name);
2827 return NULL;
2828 }
2829
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002830 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002833 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002834 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 if (len == -1)
2836 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002837 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002838 }
2839 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002840 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002841 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002842 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 p = NULL;
2847 break;
2848 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002849 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002850 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002851 return NULL;
2852
Bram Moolenaar8c711452005-01-14 21:53:12 +00002853 if (len == -1)
2854 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002855 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002856 }
2857 else
2858 {
2859 /*
2860 * Get the number and item for the only or first index of the List.
2861 */
2862 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002863 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002864 else
2865 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002866 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 clear_tv(&var1);
2868 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002869 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002870 lp->ll_list = lp->ll_tv->vval.v_list;
2871 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2872 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002874 if (lp->ll_n1 < 0)
2875 {
2876 lp->ll_n1 = 0;
2877 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2878 }
2879 }
2880 if (lp->ll_li == NULL)
2881 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002883 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002884 if (!quiet)
2885 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002887 }
2888
2889 /*
2890 * May need to find the item or absolute index for the second
2891 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002892 * When no index given: "lp->ll_empty2" is TRUE.
2893 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002897 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002898 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002899 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002900 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002902 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002903 {
2904 if (!quiet)
2905 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002906 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002909 }
2910
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002911 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2912 if (lp->ll_n1 < 0)
2913 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2914 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002915 {
2916 if (!quiet)
2917 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002918 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002919 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002920 }
2921
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002922 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002923 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002924 }
2925
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002926 return p;
2927}
2928
2929/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002930 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 */
2932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002933clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002934{
2935 vim_free(lp->ll_exp_name);
2936 vim_free(lp->ll_newkey);
2937}
2938
2939/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002940 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002942 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002943 */
2944 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002945set_var_lval(
2946 lval_T *lp,
2947 char_u *endp,
2948 typval_T *rettv,
2949 int copy,
2950 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002951{
2952 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002953 listitem_T *ri;
2954 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002955
2956 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002957 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002958 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002959 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002960 cc = *endp;
2961 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002962 if (op != NULL && *op != '=')
2963 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002964 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002965
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002966 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002967 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002968 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002969 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002971 if ((di == NULL
2972 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2973 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2974 FALSE)))
2975 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002976 set_var(lp->ll_name, &tv, FALSE);
2977 clear_tv(&tv);
2978 }
2979 }
2980 else
2981 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002982 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002983 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002984 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002985 else if (tv_check_lock(lp->ll_newkey == NULL
2986 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002987 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002988 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002989 else if (lp->ll_range)
2990 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002991 listitem_T *ll_li = lp->ll_li;
2992 int ll_n1 = lp->ll_n1;
2993
2994 /*
2995 * Check whether any of the list items is locked
2996 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002997 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002998 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002999 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003000 return;
3001 ri = ri->li_next;
3002 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
3003 break;
3004 ll_li = ll_li->li_next;
3005 ++ll_n1;
3006 }
3007
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003008 /*
3009 * Assign the List values to the list items.
3010 */
3011 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003012 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003013 if (op != NULL && *op != '=')
3014 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3015 else
3016 {
3017 clear_tv(&lp->ll_li->li_tv);
3018 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3019 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003020 ri = ri->li_next;
3021 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3022 break;
3023 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003025 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003026 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003027 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003028 ri = NULL;
3029 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003030 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003031 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003032 lp->ll_li = lp->ll_li->li_next;
3033 ++lp->ll_n1;
3034 }
3035 if (ri != NULL)
3036 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003037 else if (lp->ll_empty2
3038 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003039 : lp->ll_n1 != lp->ll_n2)
3040 EMSG(_("E711: List value has not enough items"));
3041 }
3042 else
3043 {
3044 /*
3045 * Assign to a List or Dictionary item.
3046 */
3047 if (lp->ll_newkey != NULL)
3048 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003049 if (op != NULL && *op != '=')
3050 {
3051 EMSG2(_(e_letwrong), op);
3052 return;
3053 }
3054
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003055 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003056 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003057 if (di == NULL)
3058 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003059 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3060 {
3061 vim_free(di);
3062 return;
3063 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003065 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003066 else if (op != NULL && *op != '=')
3067 {
3068 tv_op(lp->ll_tv, rettv, op);
3069 return;
3070 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003071 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003072 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003073
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003074 /*
3075 * Assign the value to the variable or list item.
3076 */
3077 if (copy)
3078 copy_tv(rettv, lp->ll_tv);
3079 else
3080 {
3081 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003082 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003083 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003084 }
3085 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003086}
3087
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003088/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003089 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3090 * Returns OK or FAIL.
3091 */
3092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003093tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003094{
3095 long n;
3096 char_u numbuf[NUMBUFLEN];
3097 char_u *s;
3098
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3100 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3101 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 {
3103 switch (tv1->v_type)
3104 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003105 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 case VAR_DICT:
3107 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003108 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003109 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003110 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003111 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003112 break;
3113
3114 case VAR_LIST:
3115 if (*op != '+' || tv2->v_type != VAR_LIST)
3116 break;
3117 /* List += List */
3118 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3119 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3120 return OK;
3121
3122 case VAR_NUMBER:
3123 case VAR_STRING:
3124 if (tv2->v_type == VAR_LIST)
3125 break;
3126 if (*op == '+' || *op == '-')
3127 {
3128 /* nr += nr or nr -= nr*/
3129 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003130#ifdef FEAT_FLOAT
3131 if (tv2->v_type == VAR_FLOAT)
3132 {
3133 float_T f = n;
3134
3135 if (*op == '+')
3136 f += tv2->vval.v_float;
3137 else
3138 f -= tv2->vval.v_float;
3139 clear_tv(tv1);
3140 tv1->v_type = VAR_FLOAT;
3141 tv1->vval.v_float = f;
3142 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003143 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003144#endif
3145 {
3146 if (*op == '+')
3147 n += get_tv_number(tv2);
3148 else
3149 n -= get_tv_number(tv2);
3150 clear_tv(tv1);
3151 tv1->v_type = VAR_NUMBER;
3152 tv1->vval.v_number = n;
3153 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 }
3155 else
3156 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003157 if (tv2->v_type == VAR_FLOAT)
3158 break;
3159
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003160 /* str .= str */
3161 s = get_tv_string(tv1);
3162 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3163 clear_tv(tv1);
3164 tv1->v_type = VAR_STRING;
3165 tv1->vval.v_string = s;
3166 }
3167 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003168
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003169 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003170#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003171 {
3172 float_T f;
3173
3174 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3175 && tv2->v_type != VAR_NUMBER
3176 && tv2->v_type != VAR_STRING))
3177 break;
3178 if (tv2->v_type == VAR_FLOAT)
3179 f = tv2->vval.v_float;
3180 else
3181 f = get_tv_number(tv2);
3182 if (*op == '+')
3183 tv1->vval.v_float += f;
3184 else
3185 tv1->vval.v_float -= f;
3186 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003187#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003188 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003189 }
3190 }
3191
3192 EMSG2(_(e_letwrong), op);
3193 return FAIL;
3194}
3195
3196/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003197 * Add a watcher to a list.
3198 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003199 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003200list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003201{
3202 lw->lw_next = l->lv_watch;
3203 l->lv_watch = lw;
3204}
3205
3206/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003207 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208 * No warning when it isn't found...
3209 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003210 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003211list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003212{
Bram Moolenaar33570922005-01-25 22:26:29 +00003213 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003214
3215 lwp = &l->lv_watch;
3216 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3217 {
3218 if (lw == lwrem)
3219 {
3220 *lwp = lw->lw_next;
3221 break;
3222 }
3223 lwp = &lw->lw_next;
3224 }
3225}
3226
3227/*
3228 * Just before removing an item from a list: advance watchers to the next
3229 * item.
3230 */
3231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003232list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003233{
Bram Moolenaar33570922005-01-25 22:26:29 +00003234 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003235
3236 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3237 if (lw->lw_item == item)
3238 lw->lw_item = item->li_next;
3239}
3240
3241/*
3242 * Evaluate the expression used in a ":for var in expr" command.
3243 * "arg" points to "var".
3244 * Set "*errp" to TRUE for an error, FALSE otherwise;
3245 * Return a pointer that holds the info. Null when there is an error.
3246 */
3247 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003248eval_for_line(
3249 char_u *arg,
3250 int *errp,
3251 char_u **nextcmdp,
3252 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003253{
Bram Moolenaar33570922005-01-25 22:26:29 +00003254 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003255 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003256 typval_T tv;
3257 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003258
3259 *errp = TRUE; /* default: there is an error */
3260
Bram Moolenaar33570922005-01-25 22:26:29 +00003261 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003262 if (fi == NULL)
3263 return NULL;
3264
3265 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3266 if (expr == NULL)
3267 return fi;
3268
3269 expr = skipwhite(expr);
3270 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3271 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003272 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003273 return fi;
3274 }
3275
3276 if (skip)
3277 ++emsg_skip;
3278 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3279 {
3280 *errp = FALSE;
3281 if (!skip)
3282 {
3283 l = tv.vval.v_list;
3284 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003285 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003286 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003287 clear_tv(&tv);
3288 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003289 else
3290 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003291 /* No need to increment the refcount, it's already set for the
3292 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003293 fi->fi_list = l;
3294 list_add_watch(l, &fi->fi_lw);
3295 fi->fi_lw.lw_item = l->lv_first;
3296 }
3297 }
3298 }
3299 if (skip)
3300 --emsg_skip;
3301
3302 return fi;
3303}
3304
3305/*
3306 * Use the first item in a ":for" list. Advance to the next.
3307 * Assign the values to the variable (list). "arg" points to the first one.
3308 * Return TRUE when a valid item was found, FALSE when at end of list or
3309 * something wrong.
3310 */
3311 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003312next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003313{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003314 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003315 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003316 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003317
3318 item = fi->fi_lw.lw_item;
3319 if (item == NULL)
3320 result = FALSE;
3321 else
3322 {
3323 fi->fi_lw.lw_item = item->li_next;
3324 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3325 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3326 }
3327 return result;
3328}
3329
3330/*
3331 * Free the structure used to store info used by ":for".
3332 */
3333 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003334free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003335{
Bram Moolenaar33570922005-01-25 22:26:29 +00003336 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003338 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003339 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003340 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003341 list_unref(fi->fi_list);
3342 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003343 vim_free(fi);
3344}
3345
Bram Moolenaar071d4272004-06-13 20:20:40 +00003346#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3347
3348 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003349set_context_for_expression(
3350 expand_T *xp,
3351 char_u *arg,
3352 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003353{
3354 int got_eq = FALSE;
3355 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003357
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (cmdidx == CMD_let)
3359 {
3360 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003361 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 {
3363 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003364 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003365 {
3366 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003367 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003368 if (vim_iswhite(*p))
3369 break;
3370 }
3371 return;
3372 }
3373 }
3374 else
3375 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3376 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003377 while ((xp->xp_pattern = vim_strpbrk(arg,
3378 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3379 {
3380 c = *xp->xp_pattern;
3381 if (c == '&')
3382 {
3383 c = xp->xp_pattern[1];
3384 if (c == '&')
3385 {
3386 ++xp->xp_pattern;
3387 xp->xp_context = cmdidx != CMD_let || got_eq
3388 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3389 }
3390 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003391 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003392 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003393 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3394 xp->xp_pattern += 2;
3395
3396 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003397 }
3398 else if (c == '$')
3399 {
3400 /* environment variable */
3401 xp->xp_context = EXPAND_ENV_VARS;
3402 }
3403 else if (c == '=')
3404 {
3405 got_eq = TRUE;
3406 xp->xp_context = EXPAND_EXPRESSION;
3407 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003408 else if (c == '#'
3409 && xp->xp_context == EXPAND_EXPRESSION)
3410 {
3411 /* Autoload function/variable contains '#'. */
3412 break;
3413 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003414 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003415 && xp->xp_context == EXPAND_FUNCTIONS
3416 && vim_strchr(xp->xp_pattern, '(') == NULL)
3417 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003418 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003419 break;
3420 }
3421 else if (cmdidx != CMD_let || got_eq)
3422 {
3423 if (c == '"') /* string */
3424 {
3425 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3426 if (c == '\\' && xp->xp_pattern[1] != NUL)
3427 ++xp->xp_pattern;
3428 xp->xp_context = EXPAND_NOTHING;
3429 }
3430 else if (c == '\'') /* literal string */
3431 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003432 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003433 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3434 /* skip */ ;
3435 xp->xp_context = EXPAND_NOTHING;
3436 }
3437 else if (c == '|')
3438 {
3439 if (xp->xp_pattern[1] == '|')
3440 {
3441 ++xp->xp_pattern;
3442 xp->xp_context = EXPAND_EXPRESSION;
3443 }
3444 else
3445 xp->xp_context = EXPAND_COMMANDS;
3446 }
3447 else
3448 xp->xp_context = EXPAND_EXPRESSION;
3449 }
3450 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003451 /* Doesn't look like something valid, expand as an expression
3452 * anyway. */
3453 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003454 arg = xp->xp_pattern;
3455 if (*arg != NUL)
3456 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3457 /* skip */ ;
3458 }
3459 xp->xp_pattern = arg;
3460}
3461
3462#endif /* FEAT_CMDL_COMPL */
3463
3464/*
3465 * ":1,25call func(arg1, arg2)" function call.
3466 */
3467 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003468ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003469{
3470 char_u *arg = eap->arg;
3471 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003472 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003473 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003475 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003476 linenr_T lnum;
3477 int doesrange;
3478 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003479 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003480 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003481
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003482 if (eap->skip)
3483 {
3484 /* trans_function_name() doesn't work well when skipping, use eval0()
3485 * instead to skip to any following command, e.g. for:
3486 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003487 ++emsg_skip;
3488 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3489 clear_tv(&rettv);
3490 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003491 return;
3492 }
3493
Bram Moolenaar65639032016-03-16 21:40:30 +01003494 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003495 if (fudi.fd_newkey != NULL)
3496 {
3497 /* Still need to give an error message for missing key. */
3498 EMSG2(_(e_dictkey), fudi.fd_newkey);
3499 vim_free(fudi.fd_newkey);
3500 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003501 if (tofree == NULL)
3502 return;
3503
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003504 /* Increase refcount on dictionary, it could get deleted when evaluating
3505 * the arguments. */
3506 if (fudi.fd_dict != NULL)
3507 ++fudi.fd_dict->dv_refcount;
3508
Bram Moolenaar65639032016-03-16 21:40:30 +01003509 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3510 * contents. For VAR_PARTIAL get its partial, unless we already have one
3511 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003512 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003513 name = deref_func_name(tofree, &len,
3514 partial != NULL ? NULL : &partial, FALSE);
3515
Bram Moolenaar532c7802005-01-27 14:44:31 +00003516 /* Skip white space to allow ":call func ()". Not good, but required for
3517 * backward compatibility. */
3518 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003519 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003520
3521 if (*startarg != '(')
3522 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003523 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003524 goto end;
3525 }
3526
3527 /*
3528 * When skipping, evaluate the function once, to find the end of the
3529 * arguments.
3530 * When the function takes a range, this is discovered after the first
3531 * call, and the loop is broken.
3532 */
3533 if (eap->skip)
3534 {
3535 ++emsg_skip;
3536 lnum = eap->line2; /* do it once, also with an invalid range */
3537 }
3538 else
3539 lnum = eap->line1;
3540 for ( ; lnum <= eap->line2; ++lnum)
3541 {
3542 if (!eap->skip && eap->addr_count > 0)
3543 {
3544 curwin->w_cursor.lnum = lnum;
3545 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003546#ifdef FEAT_VIRTUALEDIT
3547 curwin->w_cursor.coladd = 0;
3548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003549 }
3550 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003551 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003552 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003553 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003554 {
3555 failed = TRUE;
3556 break;
3557 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003558
3559 /* Handle a function returning a Funcref, Dictionary or List. */
3560 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3561 {
3562 failed = TRUE;
3563 break;
3564 }
3565
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003567 if (doesrange || eap->skip)
3568 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003569
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003571 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003572 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003573 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003574 if (aborting())
3575 break;
3576 }
3577 if (eap->skip)
3578 --emsg_skip;
3579
3580 if (!failed)
3581 {
3582 /* Check for trailing illegal characters and a following command. */
3583 if (!ends_excmd(*arg))
3584 {
3585 emsg_severe = TRUE;
3586 EMSG(_(e_trailing));
3587 }
3588 else
3589 eap->nextcmd = check_nextcmd(arg);
3590 }
3591
3592end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003593 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003594 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003595}
3596
3597/*
3598 * ":unlet[!] var1 ... " command.
3599 */
3600 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003601ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 ex_unletlock(eap, eap->arg, 0);
3604}
3605
3606/*
3607 * ":lockvar" and ":unlockvar" commands
3608 */
3609 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003610ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003611{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003612 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003613 int deep = 2;
3614
3615 if (eap->forceit)
3616 deep = -1;
3617 else if (vim_isdigit(*arg))
3618 {
3619 deep = getdigits(&arg);
3620 arg = skipwhite(arg);
3621 }
3622
3623 ex_unletlock(eap, arg, deep);
3624}
3625
3626/*
3627 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3628 */
3629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003630ex_unletlock(
3631 exarg_T *eap,
3632 char_u *argstart,
3633 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003634{
3635 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003636 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003637 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003638 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639
3640 do
3641 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003642 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003643 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003644 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003645 if (lv.ll_name == NULL)
3646 error = TRUE; /* error but continue parsing */
3647 if (name_end == NULL || (!vim_iswhite(*name_end)
3648 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003649 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003650 if (name_end != NULL)
3651 {
3652 emsg_severe = TRUE;
3653 EMSG(_(e_trailing));
3654 }
3655 if (!(eap->skip || error))
3656 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003657 break;
3658 }
3659
3660 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003661 {
3662 if (eap->cmdidx == CMD_unlet)
3663 {
3664 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3665 error = TRUE;
3666 }
3667 else
3668 {
3669 if (do_lock_var(&lv, name_end, deep,
3670 eap->cmdidx == CMD_lockvar) == FAIL)
3671 error = TRUE;
3672 }
3673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003674
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003675 if (!eap->skip)
3676 clear_lval(&lv);
3677
Bram Moolenaar071d4272004-06-13 20:20:40 +00003678 arg = skipwhite(name_end);
3679 } while (!ends_excmd(*arg));
3680
3681 eap->nextcmd = check_nextcmd(arg);
3682}
3683
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003685do_unlet_var(
3686 lval_T *lp,
3687 char_u *name_end,
3688 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003689{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003690 int ret = OK;
3691 int cc;
3692
3693 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003695 cc = *name_end;
3696 *name_end = NUL;
3697
3698 /* Normal name or expanded name. */
3699 if (check_changedtick(lp->ll_name))
3700 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003701 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003702 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003703 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003704 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003705 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003706 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003707 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003708 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003709 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003710 else if (lp->ll_range)
3711 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003712 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003713 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003714 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003715
3716 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3717 {
3718 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003719 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003720 return FAIL;
3721 ll_li = li;
3722 ++ll_n1;
3723 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003724
3725 /* Delete a range of List items. */
3726 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3727 {
3728 li = lp->ll_li->li_next;
3729 listitem_remove(lp->ll_list, lp->ll_li);
3730 lp->ll_li = li;
3731 ++lp->ll_n1;
3732 }
3733 }
3734 else
3735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003737 /* unlet a List item. */
3738 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003739 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003740 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003741 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003742 }
3743
3744 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003745}
3746
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747/*
3748 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003749 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003750 */
3751 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003752do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753{
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 hashtab_T *ht;
3755 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003756 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003757 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003758 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003759
Bram Moolenaar33570922005-01-25 22:26:29 +00003760 ht = find_var_ht(name, &varname);
3761 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003763 if (ht == &globvarht)
3764 d = &globvardict;
3765 else if (current_funccal != NULL
3766 && ht == &current_funccal->l_vars.dv_hashtab)
3767 d = &current_funccal->l_vars;
3768 else if (ht == &compat_hashtab)
3769 d = &vimvardict;
3770 else
3771 {
3772 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3773 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3774 }
3775 if (d == NULL)
3776 {
3777 EMSG2(_(e_intern2), "do_unlet()");
3778 return FAIL;
3779 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003780 hi = hash_find(ht, varname);
3781 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003782 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003783 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003784 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003785 || var_check_ro(di->di_flags, name, FALSE)
3786 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003787 return FAIL;
3788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789 delete_var(ht, hi);
3790 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003791 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003792 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793 if (forceit)
3794 return OK;
3795 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003796 return FAIL;
3797}
3798
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003799/*
3800 * Lock or unlock variable indicated by "lp".
3801 * "deep" is the levels to go (-1 for unlimited);
3802 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3803 */
3804 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003805do_lock_var(
3806 lval_T *lp,
3807 char_u *name_end,
3808 int deep,
3809 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003810{
3811 int ret = OK;
3812 int cc;
3813 dictitem_T *di;
3814
3815 if (deep == 0) /* nothing to do */
3816 return OK;
3817
3818 if (lp->ll_tv == NULL)
3819 {
3820 cc = *name_end;
3821 *name_end = NUL;
3822
3823 /* Normal name or expanded name. */
3824 if (check_changedtick(lp->ll_name))
3825 ret = FAIL;
3826 else
3827 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003828 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003829 if (di == NULL)
3830 ret = FAIL;
3831 else
3832 {
3833 if (lock)
3834 di->di_flags |= DI_FLAGS_LOCK;
3835 else
3836 di->di_flags &= ~DI_FLAGS_LOCK;
3837 item_lock(&di->di_tv, deep, lock);
3838 }
3839 }
3840 *name_end = cc;
3841 }
3842 else if (lp->ll_range)
3843 {
3844 listitem_T *li = lp->ll_li;
3845
3846 /* (un)lock a range of List items. */
3847 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3848 {
3849 item_lock(&li->li_tv, deep, lock);
3850 li = li->li_next;
3851 ++lp->ll_n1;
3852 }
3853 }
3854 else if (lp->ll_list != NULL)
3855 /* (un)lock a List item. */
3856 item_lock(&lp->ll_li->li_tv, deep, lock);
3857 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003858 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859 item_lock(&lp->ll_di->di_tv, deep, lock);
3860
3861 return ret;
3862}
3863
3864/*
3865 * Lock or unlock an item. "deep" is nr of levels to go.
3866 */
3867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003868item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003869{
3870 static int recurse = 0;
3871 list_T *l;
3872 listitem_T *li;
3873 dict_T *d;
3874 hashitem_T *hi;
3875 int todo;
3876
3877 if (recurse >= DICT_MAXNEST)
3878 {
3879 EMSG(_("E743: variable nested too deep for (un)lock"));
3880 return;
3881 }
3882 if (deep == 0)
3883 return;
3884 ++recurse;
3885
3886 /* lock/unlock the item itself */
3887 if (lock)
3888 tv->v_lock |= VAR_LOCKED;
3889 else
3890 tv->v_lock &= ~VAR_LOCKED;
3891
3892 switch (tv->v_type)
3893 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003894 case VAR_UNKNOWN:
3895 case VAR_NUMBER:
3896 case VAR_STRING:
3897 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003898 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003899 case VAR_FLOAT:
3900 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003901 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003902 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003903 break;
3904
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003905 case VAR_LIST:
3906 if ((l = tv->vval.v_list) != NULL)
3907 {
3908 if (lock)
3909 l->lv_lock |= VAR_LOCKED;
3910 else
3911 l->lv_lock &= ~VAR_LOCKED;
3912 if (deep < 0 || deep > 1)
3913 /* recursive: lock/unlock the items the List contains */
3914 for (li = l->lv_first; li != NULL; li = li->li_next)
3915 item_lock(&li->li_tv, deep - 1, lock);
3916 }
3917 break;
3918 case VAR_DICT:
3919 if ((d = tv->vval.v_dict) != NULL)
3920 {
3921 if (lock)
3922 d->dv_lock |= VAR_LOCKED;
3923 else
3924 d->dv_lock &= ~VAR_LOCKED;
3925 if (deep < 0 || deep > 1)
3926 {
3927 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003928 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003929 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3930 {
3931 if (!HASHITEM_EMPTY(hi))
3932 {
3933 --todo;
3934 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3935 }
3936 }
3937 }
3938 }
3939 }
3940 --recurse;
3941}
3942
Bram Moolenaara40058a2005-07-11 22:42:07 +00003943/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003944 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3945 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003946 */
3947 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003948tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003949{
3950 return (tv->v_lock & VAR_LOCKED)
3951 || (tv->v_type == VAR_LIST
3952 && tv->vval.v_list != NULL
3953 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3954 || (tv->v_type == VAR_DICT
3955 && tv->vval.v_dict != NULL
3956 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3957}
3958
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3960/*
3961 * Delete all "menutrans_" variables.
3962 */
3963 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003964del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003965{
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003967 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003968
Bram Moolenaar33570922005-01-25 22:26:29 +00003969 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003970 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003971 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 {
3973 if (!HASHITEM_EMPTY(hi))
3974 {
3975 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003976 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3977 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003978 }
3979 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003980 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003981}
3982#endif
3983
3984#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3985
3986/*
3987 * Local string buffer for the next two functions to store a variable name
3988 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3989 * get_user_var_name().
3990 */
3991
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003992static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003993
3994static char_u *varnamebuf = NULL;
3995static int varnamebuflen = 0;
3996
3997/*
3998 * Function to concatenate a prefix and a variable name.
3999 */
4000 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004001cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004002{
4003 int len;
4004
4005 len = (int)STRLEN(name) + 3;
4006 if (len > varnamebuflen)
4007 {
4008 vim_free(varnamebuf);
4009 len += 10; /* some additional space */
4010 varnamebuf = alloc(len);
4011 if (varnamebuf == NULL)
4012 {
4013 varnamebuflen = 0;
4014 return NULL;
4015 }
4016 varnamebuflen = len;
4017 }
4018 *varnamebuf = prefix;
4019 varnamebuf[1] = ':';
4020 STRCPY(varnamebuf + 2, name);
4021 return varnamebuf;
4022}
4023
4024/*
4025 * Function given to ExpandGeneric() to obtain the list of user defined
4026 * (global/buffer/window/built-in) variable names.
4027 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004028 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004029get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 static long_u gdone;
4032 static long_u bdone;
4033 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 static long_u tdone;
4036#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004037 static int vidx;
4038 static hashitem_T *hi;
4039 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004040
4041 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004042 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004043 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004044#ifdef FEAT_WINDOWS
4045 tdone = 0;
4046#endif
4047 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004048
4049 /* Global variables */
4050 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004052 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004053 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004054 else
4055 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004056 while (HASHITEM_EMPTY(hi))
4057 ++hi;
4058 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4059 return cat_prefix_varname('g', hi->hi_key);
4060 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004061 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004062
4063 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004064 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004068 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004069 else
4070 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 while (HASHITEM_EMPTY(hi))
4072 ++hi;
4073 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004075 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004076 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004077 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 return (char_u *)"b:changedtick";
4079 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004080
4081 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004082 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004083 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004084 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004085 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004086 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004087 else
4088 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004089 while (HASHITEM_EMPTY(hi))
4090 ++hi;
4091 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004092 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004093
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004094#ifdef FEAT_WINDOWS
4095 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004096 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004097 if (tdone < ht->ht_used)
4098 {
4099 if (tdone++ == 0)
4100 hi = ht->ht_array;
4101 else
4102 ++hi;
4103 while (HASHITEM_EMPTY(hi))
4104 ++hi;
4105 return cat_prefix_varname('t', hi->hi_key);
4106 }
4107#endif
4108
Bram Moolenaar33570922005-01-25 22:26:29 +00004109 /* v: variables */
4110 if (vidx < VV_LEN)
4111 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004112
4113 vim_free(varnamebuf);
4114 varnamebuf = NULL;
4115 varnamebuflen = 0;
4116 return NULL;
4117}
4118
4119#endif /* FEAT_CMDL_COMPL */
4120
4121/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004122 * Return TRUE if "pat" matches "text".
4123 * Does not use 'cpo' and always uses 'magic'.
4124 */
4125 static int
4126pattern_match(char_u *pat, char_u *text, int ic)
4127{
4128 int matches = FALSE;
4129 char_u *save_cpo;
4130 regmatch_T regmatch;
4131
4132 /* avoid 'l' flag in 'cpoptions' */
4133 save_cpo = p_cpo;
4134 p_cpo = (char_u *)"";
4135 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4136 if (regmatch.regprog != NULL)
4137 {
4138 regmatch.rm_ic = ic;
4139 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4140 vim_regfree(regmatch.regprog);
4141 }
4142 p_cpo = save_cpo;
4143 return matches;
4144}
4145
4146/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004147 * types for expressions.
4148 */
4149typedef enum
4150{
4151 TYPE_UNKNOWN = 0
4152 , TYPE_EQUAL /* == */
4153 , TYPE_NEQUAL /* != */
4154 , TYPE_GREATER /* > */
4155 , TYPE_GEQUAL /* >= */
4156 , TYPE_SMALLER /* < */
4157 , TYPE_SEQUAL /* <= */
4158 , TYPE_MATCH /* =~ */
4159 , TYPE_NOMATCH /* !~ */
4160} exptype_T;
4161
4162/*
4163 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004164 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004165 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4166 */
4167
4168/*
4169 * Handle zero level expression.
4170 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004171 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004172 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004173 * Return OK or FAIL.
4174 */
4175 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004176eval0(
4177 char_u *arg,
4178 typval_T *rettv,
4179 char_u **nextcmd,
4180 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181{
4182 int ret;
4183 char_u *p;
4184
4185 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004186 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187 if (ret == FAIL || !ends_excmd(*p))
4188 {
4189 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004190 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004191 /*
4192 * Report the invalid expression unless the expression evaluation has
4193 * been cancelled due to an aborting error, an interrupt, or an
4194 * exception.
4195 */
4196 if (!aborting())
4197 EMSG2(_(e_invexpr2), arg);
4198 ret = FAIL;
4199 }
4200 if (nextcmd != NULL)
4201 *nextcmd = check_nextcmd(p);
4202
4203 return ret;
4204}
4205
4206/*
4207 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004208 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 *
4210 * "arg" must point to the first non-white of the expression.
4211 * "arg" is advanced to the next non-white after the recognized expression.
4212 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004213 * Note: "rettv.v_lock" is not set.
4214 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004215 * Return OK or FAIL.
4216 */
4217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004218eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004219{
4220 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004221 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004222
4223 /*
4224 * Get the first variable.
4225 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004226 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004227 return FAIL;
4228
4229 if ((*arg)[0] == '?')
4230 {
4231 result = FALSE;
4232 if (evaluate)
4233 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004234 int error = FALSE;
4235
4236 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004237 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004238 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004239 if (error)
4240 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004241 }
4242
4243 /*
4244 * Get the second variable.
4245 */
4246 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004247 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004248 return FAIL;
4249
4250 /*
4251 * Check for the ":".
4252 */
4253 if ((*arg)[0] != ':')
4254 {
4255 EMSG(_("E109: Missing ':' after '?'"));
4256 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004257 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004258 return FAIL;
4259 }
4260
4261 /*
4262 * Get the third variable.
4263 */
4264 *arg = skipwhite(*arg + 1);
4265 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4266 {
4267 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004268 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004269 return FAIL;
4270 }
4271 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004272 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004273 }
4274
4275 return OK;
4276}
4277
4278/*
4279 * Handle first level expression:
4280 * expr2 || expr2 || expr2 logical OR
4281 *
4282 * "arg" must point to the first non-white of the expression.
4283 * "arg" is advanced to the next non-white after the recognized expression.
4284 *
4285 * Return OK or FAIL.
4286 */
4287 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004288eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004289{
Bram Moolenaar33570922005-01-25 22:26:29 +00004290 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004291 long result;
4292 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004293 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004294
4295 /*
4296 * Get the first variable.
4297 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004298 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004299 return FAIL;
4300
4301 /*
4302 * Repeat until there is no following "||".
4303 */
4304 first = TRUE;
4305 result = FALSE;
4306 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4307 {
4308 if (evaluate && first)
4309 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004310 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004311 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004312 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004313 if (error)
4314 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004315 first = FALSE;
4316 }
4317
4318 /*
4319 * Get the second variable.
4320 */
4321 *arg = skipwhite(*arg + 2);
4322 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4323 return FAIL;
4324
4325 /*
4326 * Compute the result.
4327 */
4328 if (evaluate && !result)
4329 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004330 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004331 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004333 if (error)
4334 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004335 }
4336 if (evaluate)
4337 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004338 rettv->v_type = VAR_NUMBER;
4339 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004340 }
4341 }
4342
4343 return OK;
4344}
4345
4346/*
4347 * Handle second level expression:
4348 * expr3 && expr3 && expr3 logical AND
4349 *
4350 * "arg" must point to the first non-white of the expression.
4351 * "arg" is advanced to the next non-white after the recognized expression.
4352 *
4353 * Return OK or FAIL.
4354 */
4355 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004356eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004357{
Bram Moolenaar33570922005-01-25 22:26:29 +00004358 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004359 long result;
4360 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004361 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004362
4363 /*
4364 * Get the first variable.
4365 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004366 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004367 return FAIL;
4368
4369 /*
4370 * Repeat until there is no following "&&".
4371 */
4372 first = TRUE;
4373 result = TRUE;
4374 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4375 {
4376 if (evaluate && first)
4377 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004378 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004379 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004380 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004381 if (error)
4382 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004383 first = FALSE;
4384 }
4385
4386 /*
4387 * Get the second variable.
4388 */
4389 *arg = skipwhite(*arg + 2);
4390 if (eval4(arg, &var2, evaluate && result) == FAIL)
4391 return FAIL;
4392
4393 /*
4394 * Compute the result.
4395 */
4396 if (evaluate && result)
4397 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004398 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004401 if (error)
4402 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004403 }
4404 if (evaluate)
4405 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004406 rettv->v_type = VAR_NUMBER;
4407 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004408 }
4409 }
4410
4411 return OK;
4412}
4413
4414/*
4415 * Handle third level expression:
4416 * var1 == var2
4417 * var1 =~ var2
4418 * var1 != var2
4419 * var1 !~ var2
4420 * var1 > var2
4421 * var1 >= var2
4422 * var1 < var2
4423 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004424 * var1 is var2
4425 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426 *
4427 * "arg" must point to the first non-white of the expression.
4428 * "arg" is advanced to the next non-white after the recognized expression.
4429 *
4430 * Return OK or FAIL.
4431 */
4432 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004433eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434{
Bram Moolenaar33570922005-01-25 22:26:29 +00004435 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436 char_u *p;
4437 int i;
4438 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004439 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440 int len = 2;
4441 long n1, n2;
4442 char_u *s1, *s2;
4443 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004445
4446 /*
4447 * Get the first variable.
4448 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004449 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004450 return FAIL;
4451
4452 p = *arg;
4453 switch (p[0])
4454 {
4455 case '=': if (p[1] == '=')
4456 type = TYPE_EQUAL;
4457 else if (p[1] == '~')
4458 type = TYPE_MATCH;
4459 break;
4460 case '!': if (p[1] == '=')
4461 type = TYPE_NEQUAL;
4462 else if (p[1] == '~')
4463 type = TYPE_NOMATCH;
4464 break;
4465 case '>': if (p[1] != '=')
4466 {
4467 type = TYPE_GREATER;
4468 len = 1;
4469 }
4470 else
4471 type = TYPE_GEQUAL;
4472 break;
4473 case '<': if (p[1] != '=')
4474 {
4475 type = TYPE_SMALLER;
4476 len = 1;
4477 }
4478 else
4479 type = TYPE_SEQUAL;
4480 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 case 'i': if (p[1] == 's')
4482 {
4483 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4484 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004485 i = p[len];
4486 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004487 {
4488 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4489 type_is = TRUE;
4490 }
4491 }
4492 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004493 }
4494
4495 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004496 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004497 */
4498 if (type != TYPE_UNKNOWN)
4499 {
4500 /* extra question mark appended: ignore case */
4501 if (p[len] == '?')
4502 {
4503 ic = TRUE;
4504 ++len;
4505 }
4506 /* extra '#' appended: match case */
4507 else if (p[len] == '#')
4508 {
4509 ic = FALSE;
4510 ++len;
4511 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004512 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004513 else
4514 ic = p_ic;
4515
4516 /*
4517 * Get the second variable.
4518 */
4519 *arg = skipwhite(p + len);
4520 if (eval5(arg, &var2, evaluate) == FAIL)
4521 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004522 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004523 return FAIL;
4524 }
4525
4526 if (evaluate)
4527 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004528 if (type_is && rettv->v_type != var2.v_type)
4529 {
4530 /* For "is" a different type always means FALSE, for "notis"
4531 * it means TRUE. */
4532 n1 = (type == TYPE_NEQUAL);
4533 }
4534 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4535 {
4536 if (type_is)
4537 {
4538 n1 = (rettv->v_type == var2.v_type
4539 && rettv->vval.v_list == var2.vval.v_list);
4540 if (type == TYPE_NEQUAL)
4541 n1 = !n1;
4542 }
4543 else if (rettv->v_type != var2.v_type
4544 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4545 {
4546 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004547 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004548 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004549 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004550 clear_tv(rettv);
4551 clear_tv(&var2);
4552 return FAIL;
4553 }
4554 else
4555 {
4556 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004557 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4558 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004559 if (type == TYPE_NEQUAL)
4560 n1 = !n1;
4561 }
4562 }
4563
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004564 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4565 {
4566 if (type_is)
4567 {
4568 n1 = (rettv->v_type == var2.v_type
4569 && rettv->vval.v_dict == var2.vval.v_dict);
4570 if (type == TYPE_NEQUAL)
4571 n1 = !n1;
4572 }
4573 else if (rettv->v_type != var2.v_type
4574 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4575 {
4576 if (rettv->v_type != var2.v_type)
4577 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4578 else
4579 EMSG(_("E736: Invalid operation for Dictionary"));
4580 clear_tv(rettv);
4581 clear_tv(&var2);
4582 return FAIL;
4583 }
4584 else
4585 {
4586 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004587 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4588 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004589 if (type == TYPE_NEQUAL)
4590 n1 = !n1;
4591 }
4592 }
4593
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004594 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4595 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004596 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004597 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004598 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004599 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004600 clear_tv(rettv);
4601 clear_tv(&var2);
4602 return FAIL;
4603 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004604 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004605 if (type == TYPE_NEQUAL)
4606 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004607 }
4608
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004609#ifdef FEAT_FLOAT
4610 /*
4611 * If one of the two variables is a float, compare as a float.
4612 * When using "=~" or "!~", always compare as string.
4613 */
4614 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4615 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4616 {
4617 float_T f1, f2;
4618
4619 if (rettv->v_type == VAR_FLOAT)
4620 f1 = rettv->vval.v_float;
4621 else
4622 f1 = get_tv_number(rettv);
4623 if (var2.v_type == VAR_FLOAT)
4624 f2 = var2.vval.v_float;
4625 else
4626 f2 = get_tv_number(&var2);
4627 n1 = FALSE;
4628 switch (type)
4629 {
4630 case TYPE_EQUAL: n1 = (f1 == f2); break;
4631 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4632 case TYPE_GREATER: n1 = (f1 > f2); break;
4633 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4634 case TYPE_SMALLER: n1 = (f1 < f2); break;
4635 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4636 case TYPE_UNKNOWN:
4637 case TYPE_MATCH:
4638 case TYPE_NOMATCH: break; /* avoid gcc warning */
4639 }
4640 }
4641#endif
4642
Bram Moolenaar071d4272004-06-13 20:20:40 +00004643 /*
4644 * If one of the two variables is a number, compare as a number.
4645 * When using "=~" or "!~", always compare as string.
4646 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004647 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004648 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4649 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004650 n1 = get_tv_number(rettv);
4651 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004652 switch (type)
4653 {
4654 case TYPE_EQUAL: n1 = (n1 == n2); break;
4655 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4656 case TYPE_GREATER: n1 = (n1 > n2); break;
4657 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4658 case TYPE_SMALLER: n1 = (n1 < n2); break;
4659 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4660 case TYPE_UNKNOWN:
4661 case TYPE_MATCH:
4662 case TYPE_NOMATCH: break; /* avoid gcc warning */
4663 }
4664 }
4665 else
4666 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004667 s1 = get_tv_string_buf(rettv, buf1);
4668 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004669 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4670 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4671 else
4672 i = 0;
4673 n1 = FALSE;
4674 switch (type)
4675 {
4676 case TYPE_EQUAL: n1 = (i == 0); break;
4677 case TYPE_NEQUAL: n1 = (i != 0); break;
4678 case TYPE_GREATER: n1 = (i > 0); break;
4679 case TYPE_GEQUAL: n1 = (i >= 0); break;
4680 case TYPE_SMALLER: n1 = (i < 0); break;
4681 case TYPE_SEQUAL: n1 = (i <= 0); break;
4682
4683 case TYPE_MATCH:
4684 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004685 n1 = pattern_match(s2, s1, ic);
4686 if (type == TYPE_NOMATCH)
4687 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004688 break;
4689
4690 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4691 }
4692 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004693 clear_tv(rettv);
4694 clear_tv(&var2);
4695 rettv->v_type = VAR_NUMBER;
4696 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004697 }
4698 }
4699
4700 return OK;
4701}
4702
4703/*
4704 * Handle fourth level expression:
4705 * + number addition
4706 * - number subtraction
4707 * . string concatenation
4708 *
4709 * "arg" must point to the first non-white of the expression.
4710 * "arg" is advanced to the next non-white after the recognized expression.
4711 *
4712 * Return OK or FAIL.
4713 */
4714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004715eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716{
Bram Moolenaar33570922005-01-25 22:26:29 +00004717 typval_T var2;
4718 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004719 int op;
4720 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004721#ifdef FEAT_FLOAT
4722 float_T f1 = 0, f2 = 0;
4723#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004724 char_u *s1, *s2;
4725 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4726 char_u *p;
4727
4728 /*
4729 * Get the first variable.
4730 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004731 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 return FAIL;
4733
4734 /*
4735 * Repeat computing, until no '+', '-' or '.' is following.
4736 */
4737 for (;;)
4738 {
4739 op = **arg;
4740 if (op != '+' && op != '-' && op != '.')
4741 break;
4742
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004743 if ((op != '+' || rettv->v_type != VAR_LIST)
4744#ifdef FEAT_FLOAT
4745 && (op == '.' || rettv->v_type != VAR_FLOAT)
4746#endif
4747 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004748 {
4749 /* For "list + ...", an illegal use of the first operand as
4750 * a number cannot be determined before evaluating the 2nd
4751 * operand: if this is also a list, all is ok.
4752 * For "something . ...", "something - ..." or "non-list + ...",
4753 * we know that the first operand needs to be a string or number
4754 * without evaluating the 2nd operand. So check before to avoid
4755 * side effects after an error. */
4756 if (evaluate && get_tv_string_chk(rettv) == NULL)
4757 {
4758 clear_tv(rettv);
4759 return FAIL;
4760 }
4761 }
4762
Bram Moolenaar071d4272004-06-13 20:20:40 +00004763 /*
4764 * Get the second variable.
4765 */
4766 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004767 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004768 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004769 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004770 return FAIL;
4771 }
4772
4773 if (evaluate)
4774 {
4775 /*
4776 * Compute the result.
4777 */
4778 if (op == '.')
4779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004780 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4781 s2 = get_tv_string_buf_chk(&var2, buf2);
4782 if (s2 == NULL) /* type error ? */
4783 {
4784 clear_tv(rettv);
4785 clear_tv(&var2);
4786 return FAIL;
4787 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004788 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004789 clear_tv(rettv);
4790 rettv->v_type = VAR_STRING;
4791 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004792 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004793 else if (op == '+' && rettv->v_type == VAR_LIST
4794 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004795 {
4796 /* concatenate Lists */
4797 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4798 &var3) == FAIL)
4799 {
4800 clear_tv(rettv);
4801 clear_tv(&var2);
4802 return FAIL;
4803 }
4804 clear_tv(rettv);
4805 *rettv = var3;
4806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004807 else
4808 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004809 int error = FALSE;
4810
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811#ifdef FEAT_FLOAT
4812 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 f1 = rettv->vval.v_float;
4815 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 else
4818#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004819 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004820 n1 = get_tv_number_chk(rettv, &error);
4821 if (error)
4822 {
4823 /* This can only happen for "list + non-list". For
4824 * "non-list + ..." or "something - ...", we returned
4825 * before evaluating the 2nd operand. */
4826 clear_tv(rettv);
4827 return FAIL;
4828 }
4829#ifdef FEAT_FLOAT
4830 if (var2.v_type == VAR_FLOAT)
4831 f1 = n1;
4832#endif
4833 }
4834#ifdef FEAT_FLOAT
4835 if (var2.v_type == VAR_FLOAT)
4836 {
4837 f2 = var2.vval.v_float;
4838 n2 = 0;
4839 }
4840 else
4841#endif
4842 {
4843 n2 = get_tv_number_chk(&var2, &error);
4844 if (error)
4845 {
4846 clear_tv(rettv);
4847 clear_tv(&var2);
4848 return FAIL;
4849 }
4850#ifdef FEAT_FLOAT
4851 if (rettv->v_type == VAR_FLOAT)
4852 f2 = n2;
4853#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004854 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004855 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004856
4857#ifdef FEAT_FLOAT
4858 /* If there is a float on either side the result is a float. */
4859 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4860 {
4861 if (op == '+')
4862 f1 = f1 + f2;
4863 else
4864 f1 = f1 - f2;
4865 rettv->v_type = VAR_FLOAT;
4866 rettv->vval.v_float = f1;
4867 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004868 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004869#endif
4870 {
4871 if (op == '+')
4872 n1 = n1 + n2;
4873 else
4874 n1 = n1 - n2;
4875 rettv->v_type = VAR_NUMBER;
4876 rettv->vval.v_number = n1;
4877 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004879 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004880 }
4881 }
4882 return OK;
4883}
4884
4885/*
4886 * Handle fifth level expression:
4887 * * number multiplication
4888 * / number division
4889 * % number modulo
4890 *
4891 * "arg" must point to the first non-white of the expression.
4892 * "arg" is advanced to the next non-white after the recognized expression.
4893 *
4894 * Return OK or FAIL.
4895 */
4896 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004897eval6(
4898 char_u **arg,
4899 typval_T *rettv,
4900 int evaluate,
4901 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004902{
Bram Moolenaar33570922005-01-25 22:26:29 +00004903 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004904 int op;
4905 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004906#ifdef FEAT_FLOAT
4907 int use_float = FALSE;
4908 float_T f1 = 0, f2;
4909#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004910 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911
4912 /*
4913 * Get the first variable.
4914 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004915 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004916 return FAIL;
4917
4918 /*
4919 * Repeat computing, until no '*', '/' or '%' is following.
4920 */
4921 for (;;)
4922 {
4923 op = **arg;
4924 if (op != '*' && op != '/' && op != '%')
4925 break;
4926
4927 if (evaluate)
4928 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004929#ifdef FEAT_FLOAT
4930 if (rettv->v_type == VAR_FLOAT)
4931 {
4932 f1 = rettv->vval.v_float;
4933 use_float = TRUE;
4934 n1 = 0;
4935 }
4936 else
4937#endif
4938 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004939 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004940 if (error)
4941 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004942 }
4943 else
4944 n1 = 0;
4945
4946 /*
4947 * Get the second variable.
4948 */
4949 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004950 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004951 return FAIL;
4952
4953 if (evaluate)
4954 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004955#ifdef FEAT_FLOAT
4956 if (var2.v_type == VAR_FLOAT)
4957 {
4958 if (!use_float)
4959 {
4960 f1 = n1;
4961 use_float = TRUE;
4962 }
4963 f2 = var2.vval.v_float;
4964 n2 = 0;
4965 }
4966 else
4967#endif
4968 {
4969 n2 = get_tv_number_chk(&var2, &error);
4970 clear_tv(&var2);
4971 if (error)
4972 return FAIL;
4973#ifdef FEAT_FLOAT
4974 if (use_float)
4975 f2 = n2;
4976#endif
4977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978
4979 /*
4980 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004981 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983#ifdef FEAT_FLOAT
4984 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004985 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004986 if (op == '*')
4987 f1 = f1 * f2;
4988 else if (op == '/')
4989 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990# ifdef VMS
4991 /* VMS crashes on divide by zero, work around it */
4992 if (f2 == 0.0)
4993 {
4994 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004995 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004996 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004997 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004998 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004999 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005000 }
5001 else
5002 f1 = f1 / f2;
5003# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 /* We rely on the floating point library to handle divide
5005 * by zero to result in "inf" and not a crash. */
5006 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005007# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005009 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005010 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005011 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012 return FAIL;
5013 }
5014 rettv->v_type = VAR_FLOAT;
5015 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 }
5017 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005018#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005019 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005020 if (op == '*')
5021 n1 = n1 * n2;
5022 else if (op == '/')
5023 {
5024 if (n2 == 0) /* give an error message? */
5025 {
5026 if (n1 == 0)
5027 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5028 else if (n1 < 0)
5029 n1 = -0x7fffffffL;
5030 else
5031 n1 = 0x7fffffffL;
5032 }
5033 else
5034 n1 = n1 / n2;
5035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005036 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005037 {
5038 if (n2 == 0) /* give an error message? */
5039 n1 = 0;
5040 else
5041 n1 = n1 % n2;
5042 }
5043 rettv->v_type = VAR_NUMBER;
5044 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005045 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046 }
5047 }
5048
5049 return OK;
5050}
5051
5052/*
5053 * Handle sixth level expression:
5054 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005055 * "string" string constant
5056 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005057 * &option-name option value
5058 * @r register contents
5059 * identifier variable value
5060 * function() function call
5061 * $VAR environment variable
5062 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005063 * [expr, expr] List
5064 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005065 *
5066 * Also handle:
5067 * ! in front logical NOT
5068 * - in front unary minus
5069 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005070 * trailing [] subscript in String or List
5071 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005072 *
5073 * "arg" must point to the first non-white of the expression.
5074 * "arg" is advanced to the next non-white after the recognized expression.
5075 *
5076 * Return OK or FAIL.
5077 */
5078 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005079eval7(
5080 char_u **arg,
5081 typval_T *rettv,
5082 int evaluate,
5083 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005084{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 long n;
5086 int len;
5087 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005088 char_u *start_leader, *end_leader;
5089 int ret = OK;
5090 char_u *alias;
5091
5092 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005094 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005095 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005096 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005097
5098 /*
5099 * Skip '!' and '-' characters. They are handled later.
5100 */
5101 start_leader = *arg;
5102 while (**arg == '!' || **arg == '-' || **arg == '+')
5103 *arg = skipwhite(*arg + 1);
5104 end_leader = *arg;
5105
5106 switch (**arg)
5107 {
5108 /*
5109 * Number constant.
5110 */
5111 case '0':
5112 case '1':
5113 case '2':
5114 case '3':
5115 case '4':
5116 case '5':
5117 case '6':
5118 case '7':
5119 case '8':
5120 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005121 {
5122#ifdef FEAT_FLOAT
5123 char_u *p = skipdigits(*arg + 1);
5124 int get_float = FALSE;
5125
5126 /* We accept a float when the format matches
5127 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005128 * strict to avoid backwards compatibility problems.
5129 * Don't look for a float after the "." operator, so that
5130 * ":let vers = 1.2.3" doesn't fail. */
5131 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005132 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005133 get_float = TRUE;
5134 p = skipdigits(p + 2);
5135 if (*p == 'e' || *p == 'E')
5136 {
5137 ++p;
5138 if (*p == '-' || *p == '+')
5139 ++p;
5140 if (!vim_isdigit(*p))
5141 get_float = FALSE;
5142 else
5143 p = skipdigits(p + 1);
5144 }
5145 if (ASCII_ISALPHA(*p) || *p == '.')
5146 get_float = FALSE;
5147 }
5148 if (get_float)
5149 {
5150 float_T f;
5151
5152 *arg += string2float(*arg, &f);
5153 if (evaluate)
5154 {
5155 rettv->v_type = VAR_FLOAT;
5156 rettv->vval.v_float = f;
5157 }
5158 }
5159 else
5160#endif
5161 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005162 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005163 *arg += len;
5164 if (evaluate)
5165 {
5166 rettv->v_type = VAR_NUMBER;
5167 rettv->vval.v_number = n;
5168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169 }
5170 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005171 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005172
5173 /*
5174 * String constant: "string".
5175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005177 break;
5178
5179 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005180 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005183 break;
5184
5185 /*
5186 * List: [expr, expr]
5187 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005188 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005189 break;
5190
5191 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005192 * Dictionary: {key: val, key: val}
5193 */
5194 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5195 break;
5196
5197 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005198 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005199 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005200 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202
5203 /*
5204 * Environment variable: $VAR.
5205 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005206 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005207 break;
5208
5209 /*
5210 * Register contents: @r.
5211 */
5212 case '@': ++*arg;
5213 if (evaluate)
5214 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005215 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005216 rettv->vval.v_string = get_reg_contents(**arg,
5217 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005218 }
5219 if (**arg != NUL)
5220 ++*arg;
5221 break;
5222
5223 /*
5224 * nested expression: (expression).
5225 */
5226 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 if (**arg == ')')
5229 ++*arg;
5230 else if (ret == OK)
5231 {
5232 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005234 ret = FAIL;
5235 }
5236 break;
5237
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005239 break;
5240 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005241
5242 if (ret == NOTDONE)
5243 {
5244 /*
5245 * Must be a variable or function name.
5246 * Can also be a curly-braces kind of name: {expr}.
5247 */
5248 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005249 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005250 if (alias != NULL)
5251 s = alias;
5252
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005253 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005254 ret = FAIL;
5255 else
5256 {
5257 if (**arg == '(') /* recursive! */
5258 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005259 partial_T *partial;
5260
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261 /* If "s" is the name of a variable of type VAR_FUNC
5262 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005263 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005264
5265 /* Invoke the function. */
5266 ret = get_func_tv(s, len, rettv, arg,
5267 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005268 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005269
5270 /* If evaluate is FALSE rettv->v_type was not set in
5271 * get_func_tv, but it's needed in handle_subscript() to parse
5272 * what follows. So set it here. */
5273 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5274 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005275 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005276 rettv->v_type = VAR_FUNC;
5277 }
5278
Bram Moolenaar8c711452005-01-14 21:53:12 +00005279 /* Stop the expression evaluation when immediately
5280 * aborting on error, or when an interrupt occurred or
5281 * an exception was thrown but not caught. */
5282 if (aborting())
5283 {
5284 if (ret == OK)
5285 clear_tv(rettv);
5286 ret = FAIL;
5287 }
5288 }
5289 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005290 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005291 else
5292 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005293 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005294 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005295 }
5296
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297 *arg = skipwhite(*arg);
5298
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005299 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5300 * expr(expr). */
5301 if (ret == OK)
5302 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005303
5304 /*
5305 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5306 */
5307 if (ret == OK && evaluate && end_leader > start_leader)
5308 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005309 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005310 int val = 0;
5311#ifdef FEAT_FLOAT
5312 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005314 if (rettv->v_type == VAR_FLOAT)
5315 f = rettv->vval.v_float;
5316 else
5317#endif
5318 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005319 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 clear_tv(rettv);
5322 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005323 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005324 else
5325 {
5326 while (end_leader > start_leader)
5327 {
5328 --end_leader;
5329 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005330 {
5331#ifdef FEAT_FLOAT
5332 if (rettv->v_type == VAR_FLOAT)
5333 f = !f;
5334 else
5335#endif
5336 val = !val;
5337 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005338 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005339 {
5340#ifdef FEAT_FLOAT
5341 if (rettv->v_type == VAR_FLOAT)
5342 f = -f;
5343 else
5344#endif
5345 val = -val;
5346 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005347 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005348#ifdef FEAT_FLOAT
5349 if (rettv->v_type == VAR_FLOAT)
5350 {
5351 clear_tv(rettv);
5352 rettv->vval.v_float = f;
5353 }
5354 else
5355#endif
5356 {
5357 clear_tv(rettv);
5358 rettv->v_type = VAR_NUMBER;
5359 rettv->vval.v_number = val;
5360 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005361 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005362 }
5363
5364 return ret;
5365}
5366
5367/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005368 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5369 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005370 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5371 */
5372 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005373eval_index(
5374 char_u **arg,
5375 typval_T *rettv,
5376 int evaluate,
5377 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378{
5379 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005380 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 long len = -1;
5383 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005384 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005385 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005386
Bram Moolenaara03f2332016-02-06 18:09:59 +01005387 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005388 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005389 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005390 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005391 if (verbose)
5392 EMSG(_("E695: Cannot index a Funcref"));
5393 return FAIL;
5394 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005395#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005396 if (verbose)
5397 EMSG(_(e_float_as_string));
5398 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005399#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005401 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005402 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005403 if (verbose)
5404 EMSG(_("E909: Cannot index a special variable"));
5405 return FAIL;
5406 case VAR_UNKNOWN:
5407 if (evaluate)
5408 return FAIL;
5409 /* FALLTHROUGH */
5410
5411 case VAR_STRING:
5412 case VAR_NUMBER:
5413 case VAR_LIST:
5414 case VAR_DICT:
5415 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005416 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005417
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005418 init_tv(&var1);
5419 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005420 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005421 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005422 /*
5423 * dict.name
5424 */
5425 key = *arg + 1;
5426 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5427 ;
5428 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005429 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005430 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005431 }
5432 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005433 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005434 /*
5435 * something[idx]
5436 *
5437 * Get the (first) variable from inside the [].
5438 */
5439 *arg = skipwhite(*arg + 1);
5440 if (**arg == ':')
5441 empty1 = TRUE;
5442 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5443 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005444 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5445 {
5446 /* not a number or string */
5447 clear_tv(&var1);
5448 return FAIL;
5449 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005450
5451 /*
5452 * Get the second variable from inside the [:].
5453 */
5454 if (**arg == ':')
5455 {
5456 range = TRUE;
5457 *arg = skipwhite(*arg + 1);
5458 if (**arg == ']')
5459 empty2 = TRUE;
5460 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5461 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005462 if (!empty1)
5463 clear_tv(&var1);
5464 return FAIL;
5465 }
5466 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5467 {
5468 /* not a number or string */
5469 if (!empty1)
5470 clear_tv(&var1);
5471 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005472 return FAIL;
5473 }
5474 }
5475
5476 /* Check for the ']'. */
5477 if (**arg != ']')
5478 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005479 if (verbose)
5480 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005481 clear_tv(&var1);
5482 if (range)
5483 clear_tv(&var2);
5484 return FAIL;
5485 }
5486 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 }
5488
5489 if (evaluate)
5490 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005491 n1 = 0;
5492 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005494 n1 = get_tv_number(&var1);
5495 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005496 }
5497 if (range)
5498 {
5499 if (empty2)
5500 n2 = -1;
5501 else
5502 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005503 n2 = get_tv_number(&var2);
5504 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005505 }
5506 }
5507
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005508 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005509 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005510 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005512 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005513 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005514 case VAR_SPECIAL:
5515 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005516 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005517 break; /* not evaluating, skipping over subscript */
5518
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 case VAR_NUMBER:
5520 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005521 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005522 len = (long)STRLEN(s);
5523 if (range)
5524 {
5525 /* The resulting variable is a substring. If the indexes
5526 * are out of range the result is empty. */
5527 if (n1 < 0)
5528 {
5529 n1 = len + n1;
5530 if (n1 < 0)
5531 n1 = 0;
5532 }
5533 if (n2 < 0)
5534 n2 = len + n2;
5535 else if (n2 >= len)
5536 n2 = len;
5537 if (n1 >= len || n2 < 0 || n1 > n2)
5538 s = NULL;
5539 else
5540 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5541 }
5542 else
5543 {
5544 /* The resulting variable is a string of a single
5545 * character. If the index is too big or negative the
5546 * result is empty. */
5547 if (n1 >= len || n1 < 0)
5548 s = NULL;
5549 else
5550 s = vim_strnsave(s + n1, 1);
5551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 clear_tv(rettv);
5553 rettv->v_type = VAR_STRING;
5554 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005555 break;
5556
5557 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005558 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005559 if (n1 < 0)
5560 n1 = len + n1;
5561 if (!empty1 && (n1 < 0 || n1 >= len))
5562 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005563 /* For a range we allow invalid values and return an empty
5564 * list. A list index out of range is an error. */
5565 if (!range)
5566 {
5567 if (verbose)
5568 EMSGN(_(e_listidx), n1);
5569 return FAIL;
5570 }
5571 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005572 }
5573 if (range)
5574 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005575 list_T *l;
5576 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005577
5578 if (n2 < 0)
5579 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005580 else if (n2 >= len)
5581 n2 = len - 1;
5582 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005583 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005584 l = list_alloc();
5585 if (l == NULL)
5586 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005587 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005588 n1 <= n2; ++n1)
5589 {
5590 if (list_append_tv(l, &item->li_tv) == FAIL)
5591 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005592 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005593 return FAIL;
5594 }
5595 item = item->li_next;
5596 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005597 clear_tv(rettv);
5598 rettv->v_type = VAR_LIST;
5599 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005600 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 else
5603 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005604 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005605 clear_tv(rettv);
5606 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005607 }
5608 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609
5610 case VAR_DICT:
5611 if (range)
5612 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005613 if (verbose)
5614 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615 if (len == -1)
5616 clear_tv(&var1);
5617 return FAIL;
5618 }
5619 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005620 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005621
5622 if (len == -1)
5623 {
5624 key = get_tv_string(&var1);
5625 if (*key == NUL)
5626 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005627 if (verbose)
5628 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629 clear_tv(&var1);
5630 return FAIL;
5631 }
5632 }
5633
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005634 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005636 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005637 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005638 if (len == -1)
5639 clear_tv(&var1);
5640 if (item == NULL)
5641 return FAIL;
5642
5643 copy_tv(&item->di_tv, &var1);
5644 clear_tv(rettv);
5645 *rettv = var1;
5646 }
5647 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 }
5649 }
5650
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005651 return OK;
5652}
5653
5654/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005655 * Get an option value.
5656 * "arg" points to the '&' or '+' before the option name.
5657 * "arg" is advanced to character after the option name.
5658 * Return OK or FAIL.
5659 */
5660 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005661get_option_tv(
5662 char_u **arg,
5663 typval_T *rettv, /* when NULL, only check if option exists */
5664 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005665{
5666 char_u *option_end;
5667 long numval;
5668 char_u *stringval;
5669 int opt_type;
5670 int c;
5671 int working = (**arg == '+'); /* has("+option") */
5672 int ret = OK;
5673 int opt_flags;
5674
5675 /*
5676 * Isolate the option name and find its value.
5677 */
5678 option_end = find_option_end(arg, &opt_flags);
5679 if (option_end == NULL)
5680 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005681 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005682 EMSG2(_("E112: Option name missing: %s"), *arg);
5683 return FAIL;
5684 }
5685
5686 if (!evaluate)
5687 {
5688 *arg = option_end;
5689 return OK;
5690 }
5691
5692 c = *option_end;
5693 *option_end = NUL;
5694 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005695 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005696
5697 if (opt_type == -3) /* invalid name */
5698 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005699 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005700 EMSG2(_("E113: Unknown option: %s"), *arg);
5701 ret = FAIL;
5702 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005703 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005704 {
5705 if (opt_type == -2) /* hidden string option */
5706 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005707 rettv->v_type = VAR_STRING;
5708 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005709 }
5710 else if (opt_type == -1) /* hidden number option */
5711 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005712 rettv->v_type = VAR_NUMBER;
5713 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005714 }
5715 else if (opt_type == 1) /* number option */
5716 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005717 rettv->v_type = VAR_NUMBER;
5718 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005719 }
5720 else /* string option */
5721 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005722 rettv->v_type = VAR_STRING;
5723 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005724 }
5725 }
5726 else if (working && (opt_type == -2 || opt_type == -1))
5727 ret = FAIL;
5728
5729 *option_end = c; /* put back for error messages */
5730 *arg = option_end;
5731
5732 return ret;
5733}
5734
5735/*
5736 * Allocate a variable for a string constant.
5737 * Return OK or FAIL.
5738 */
5739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005740get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741{
5742 char_u *p;
5743 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 int extra = 0;
5745
5746 /*
5747 * Find the end of the string, skipping backslashed characters.
5748 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005749 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005750 {
5751 if (*p == '\\' && p[1] != NUL)
5752 {
5753 ++p;
5754 /* A "\<x>" form occupies at least 4 characters, and produces up
5755 * to 6 characters: reserve space for 2 extra */
5756 if (*p == '<')
5757 extra += 2;
5758 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005759 }
5760
5761 if (*p != '"')
5762 {
5763 EMSG2(_("E114: Missing quote: %s"), *arg);
5764 return FAIL;
5765 }
5766
5767 /* If only parsing, set *arg and return here */
5768 if (!evaluate)
5769 {
5770 *arg = p + 1;
5771 return OK;
5772 }
5773
5774 /*
5775 * Copy the string into allocated memory, handling backslashed
5776 * characters.
5777 */
5778 name = alloc((unsigned)(p - *arg + extra));
5779 if (name == NULL)
5780 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 rettv->v_type = VAR_STRING;
5782 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005783
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005785 {
5786 if (*p == '\\')
5787 {
5788 switch (*++p)
5789 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005790 case 'b': *name++ = BS; ++p; break;
5791 case 'e': *name++ = ESC; ++p; break;
5792 case 'f': *name++ = FF; ++p; break;
5793 case 'n': *name++ = NL; ++p; break;
5794 case 'r': *name++ = CAR; ++p; break;
5795 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005796
5797 case 'X': /* hex: "\x1", "\x12" */
5798 case 'x':
5799 case 'u': /* Unicode: "\u0023" */
5800 case 'U':
5801 if (vim_isxdigit(p[1]))
5802 {
5803 int n, nr;
5804 int c = toupper(*p);
5805
5806 if (c == 'X')
5807 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005808 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005810 else
5811 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005812 nr = 0;
5813 while (--n >= 0 && vim_isxdigit(p[1]))
5814 {
5815 ++p;
5816 nr = (nr << 4) + hex2nr(*p);
5817 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005818 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005819#ifdef FEAT_MBYTE
5820 /* For "\u" store the number according to
5821 * 'encoding'. */
5822 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 else
5825#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005826 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005827 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005828 break;
5829
5830 /* octal: "\1", "\12", "\123" */
5831 case '0':
5832 case '1':
5833 case '2':
5834 case '3':
5835 case '4':
5836 case '5':
5837 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 case '7': *name = *p++ - '0';
5839 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005841 *name = (*name << 3) + *p++ - '0';
5842 if (*p >= '0' && *p <= '7')
5843 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005845 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005846 break;
5847
5848 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 if (extra != 0)
5851 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005852 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005853 break;
5854 }
5855 /* FALLTHROUGH */
5856
Bram Moolenaar8c711452005-01-14 21:53:12 +00005857 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 break;
5859 }
5860 }
5861 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863
Bram Moolenaar071d4272004-06-13 20:20:40 +00005864 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005865 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005866 *arg = p + 1;
5867
Bram Moolenaar071d4272004-06-13 20:20:40 +00005868 return OK;
5869}
5870
5871/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005872 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005873 * Return OK or FAIL.
5874 */
5875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005876get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005877{
5878 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005879 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005880 int reduce = 0;
5881
5882 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 */
5885 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5886 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005887 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005888 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005889 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005890 break;
5891 ++reduce;
5892 ++p;
5893 }
5894 }
5895
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005898 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005899 return FAIL;
5900 }
5901
Bram Moolenaar8c711452005-01-14 21:53:12 +00005902 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005903 if (!evaluate)
5904 {
5905 *arg = p + 1;
5906 return OK;
5907 }
5908
5909 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005910 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911 */
5912 str = alloc((unsigned)((p - *arg) - reduce));
5913 if (str == NULL)
5914 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 rettv->v_type = VAR_STRING;
5916 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917
Bram Moolenaar8c711452005-01-14 21:53:12 +00005918 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005919 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 break;
5924 ++p;
5925 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005926 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005927 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005928 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005929 *arg = p + 1;
5930
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005931 return OK;
5932}
5933
5934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005935 * Allocate a variable for a List and fill it from "*arg".
5936 * Return OK or FAIL.
5937 */
5938 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005939get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005940{
Bram Moolenaar33570922005-01-25 22:26:29 +00005941 list_T *l = NULL;
5942 typval_T tv;
5943 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005944
5945 if (evaluate)
5946 {
5947 l = list_alloc();
5948 if (l == NULL)
5949 return FAIL;
5950 }
5951
5952 *arg = skipwhite(*arg + 1);
5953 while (**arg != ']' && **arg != NUL)
5954 {
5955 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5956 goto failret;
5957 if (evaluate)
5958 {
5959 item = listitem_alloc();
5960 if (item != NULL)
5961 {
5962 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005963 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005964 list_append(l, item);
5965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005966 else
5967 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005968 }
5969
5970 if (**arg == ']')
5971 break;
5972 if (**arg != ',')
5973 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005974 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005975 goto failret;
5976 }
5977 *arg = skipwhite(*arg + 1);
5978 }
5979
5980 if (**arg != ']')
5981 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005982 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983failret:
5984 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005985 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005986 return FAIL;
5987 }
5988
5989 *arg = skipwhite(*arg + 1);
5990 if (evaluate)
5991 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005992 rettv->v_type = VAR_LIST;
5993 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005994 ++l->lv_refcount;
5995 }
5996
5997 return OK;
5998}
5999
6000/*
6001 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006002 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006004 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006005list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006006{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006007 list_T *l;
6008
6009 l = (list_T *)alloc_clear(sizeof(list_T));
6010 if (l != NULL)
6011 {
6012 /* Prepend the list to the list of lists for garbage collection. */
6013 if (first_list != NULL)
6014 first_list->lv_used_prev = l;
6015 l->lv_used_prev = NULL;
6016 l->lv_used_next = first_list;
6017 first_list = l;
6018 }
6019 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006020}
6021
6022/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006023 * Allocate an empty list for a return value.
6024 * Returns OK or FAIL.
6025 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006026 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006027rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006028{
6029 list_T *l = list_alloc();
6030
6031 if (l == NULL)
6032 return FAIL;
6033
6034 rettv->vval.v_list = l;
6035 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006036 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006037 ++l->lv_refcount;
6038 return OK;
6039}
6040
6041/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006042 * Unreference a list: decrement the reference count and free it when it
6043 * becomes zero.
6044 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006046list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006048 if (l != NULL && --l->lv_refcount <= 0)
6049 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050}
6051
6052/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006053 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054 * Ignores the reference count.
6055 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006056 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006057list_free(
6058 list_T *l,
6059 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006060{
Bram Moolenaar33570922005-01-25 22:26:29 +00006061 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006062
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006063 /* Remove the list from the list of lists for garbage collection. */
6064 if (l->lv_used_prev == NULL)
6065 first_list = l->lv_used_next;
6066 else
6067 l->lv_used_prev->lv_used_next = l->lv_used_next;
6068 if (l->lv_used_next != NULL)
6069 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6070
Bram Moolenaard9fba312005-06-26 22:34:35 +00006071 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006073 /* Remove the item before deleting it. */
6074 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006075 if (recurse || (item->li_tv.v_type != VAR_LIST
6076 && item->li_tv.v_type != VAR_DICT))
6077 clear_tv(&item->li_tv);
6078 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 }
6080 vim_free(l);
6081}
6082
6083/*
6084 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006085 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006086 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006087 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006088listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089{
Bram Moolenaar33570922005-01-25 22:26:29 +00006090 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091}
6092
6093/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006094 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006095 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006096 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006097listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006098{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006099 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006100 vim_free(item);
6101}
6102
6103/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006104 * Remove a list item from a List and free it. Also clears the value.
6105 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006106 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006107listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006108{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006109 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006110 listitem_free(item);
6111}
6112
6113/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114 * Get the number of items in a list.
6115 */
6116 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006117list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006119 if (l == NULL)
6120 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006121 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006122}
6123
6124/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006125 * Return TRUE when two lists have exactly the same values.
6126 */
6127 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006128list_equal(
6129 list_T *l1,
6130 list_T *l2,
6131 int ic, /* ignore case for strings */
6132 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006133{
Bram Moolenaar33570922005-01-25 22:26:29 +00006134 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006135
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006136 if (l1 == NULL || l2 == NULL)
6137 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006138 if (l1 == l2)
6139 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006140 if (list_len(l1) != list_len(l2))
6141 return FALSE;
6142
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 for (item1 = l1->lv_first, item2 = l2->lv_first;
6144 item1 != NULL && item2 != NULL;
6145 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006146 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006147 return FALSE;
6148 return item1 == NULL && item2 == NULL;
6149}
6150
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006151/*
6152 * Return the dictitem that an entry in a hashtable points to.
6153 */
6154 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006155dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006156{
6157 return HI2DI(hi);
6158}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006159
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006160/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006161 * Return TRUE when two dictionaries have exactly the same key/values.
6162 */
6163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006164dict_equal(
6165 dict_T *d1,
6166 dict_T *d2,
6167 int ic, /* ignore case for strings */
6168 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169{
Bram Moolenaar33570922005-01-25 22:26:29 +00006170 hashitem_T *hi;
6171 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006172 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006173
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006174 if (d1 == NULL || d2 == NULL)
6175 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006176 if (d1 == d2)
6177 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006178 if (dict_len(d1) != dict_len(d2))
6179 return FALSE;
6180
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006181 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006182 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006183 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184 if (!HASHITEM_EMPTY(hi))
6185 {
6186 item2 = dict_find(d2, hi->hi_key, -1);
6187 if (item2 == NULL)
6188 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006189 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006190 return FALSE;
6191 --todo;
6192 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006193 }
6194 return TRUE;
6195}
6196
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006197static int tv_equal_recurse_limit;
6198
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006199/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006200 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006201 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006202 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203 */
6204 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006205tv_equal(
6206 typval_T *tv1,
6207 typval_T *tv2,
6208 int ic, /* ignore case */
6209 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006210{
6211 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006212 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006213 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006214 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006215
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006216 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6217 if ((tv1->v_type == VAR_FUNC
6218 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6219 && (tv2->v_type == VAR_FUNC
6220 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6221 {
6222 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6223 : tv1->vval.v_partial->pt_name;
6224 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6225 : tv2->vval.v_partial->pt_name;
6226 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6227 }
6228
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006229 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006230 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006231
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006232 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006233 * recursiveness to a limit. We guess they are equal then.
6234 * A fixed limit has the problem of still taking an awful long time.
6235 * Reduce the limit every time running into it. That should work fine for
6236 * deeply linked structures that are not recursively linked and catch
6237 * recursiveness quickly. */
6238 if (!recursive)
6239 tv_equal_recurse_limit = 1000;
6240 if (recursive_cnt >= tv_equal_recurse_limit)
6241 {
6242 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006243 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006244 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006245
6246 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006247 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006248 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006249 ++recursive_cnt;
6250 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6251 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006252 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253
6254 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006255 ++recursive_cnt;
6256 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6257 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006258 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006259
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006260 case VAR_NUMBER:
6261 return tv1->vval.v_number == tv2->vval.v_number;
6262
6263 case VAR_STRING:
6264 s1 = get_tv_string_buf(tv1, buf1);
6265 s2 = get_tv_string_buf(tv2, buf2);
6266 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006267
6268 case VAR_SPECIAL:
6269 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006270
6271 case VAR_FLOAT:
6272#ifdef FEAT_FLOAT
6273 return tv1->vval.v_float == tv2->vval.v_float;
6274#endif
6275 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006276#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006277 return tv1->vval.v_job == tv2->vval.v_job;
6278#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006279 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006280#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006281 return tv1->vval.v_channel == tv2->vval.v_channel;
6282#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006283 case VAR_FUNC:
6284 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006285 case VAR_UNKNOWN:
6286 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006287 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006288
Bram Moolenaara03f2332016-02-06 18:09:59 +01006289 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6290 * does not equal anything, not even itself. */
6291 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006292}
6293
6294/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295 * Locate item with index "n" in list "l" and return it.
6296 * A negative index is counted from the end; -1 is the last item.
6297 * Returns NULL when "n" is out of range.
6298 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006299 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006300list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006301{
Bram Moolenaar33570922005-01-25 22:26:29 +00006302 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 long idx;
6304
6305 if (l == NULL)
6306 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006307
6308 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006309 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006310 n = l->lv_len + n;
6311
6312 /* Check for index out of range. */
6313 if (n < 0 || n >= l->lv_len)
6314 return NULL;
6315
6316 /* When there is a cached index may start search from there. */
6317 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006318 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006319 if (n < l->lv_idx / 2)
6320 {
6321 /* closest to the start of the list */
6322 item = l->lv_first;
6323 idx = 0;
6324 }
6325 else if (n > (l->lv_idx + l->lv_len) / 2)
6326 {
6327 /* closest to the end of the list */
6328 item = l->lv_last;
6329 idx = l->lv_len - 1;
6330 }
6331 else
6332 {
6333 /* closest to the cached index */
6334 item = l->lv_idx_item;
6335 idx = l->lv_idx;
6336 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006337 }
6338 else
6339 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006340 if (n < l->lv_len / 2)
6341 {
6342 /* closest to the start of the list */
6343 item = l->lv_first;
6344 idx = 0;
6345 }
6346 else
6347 {
6348 /* closest to the end of the list */
6349 item = l->lv_last;
6350 idx = l->lv_len - 1;
6351 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006352 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006353
6354 while (n > idx)
6355 {
6356 /* search forward */
6357 item = item->li_next;
6358 ++idx;
6359 }
6360 while (n < idx)
6361 {
6362 /* search backward */
6363 item = item->li_prev;
6364 --idx;
6365 }
6366
6367 /* cache the used index */
6368 l->lv_idx = idx;
6369 l->lv_idx_item = item;
6370
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006371 return item;
6372}
6373
6374/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006375 * Get list item "l[idx]" as a number.
6376 */
6377 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006378list_find_nr(
6379 list_T *l,
6380 long idx,
6381 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006382{
6383 listitem_T *li;
6384
6385 li = list_find(l, idx);
6386 if (li == NULL)
6387 {
6388 if (errorp != NULL)
6389 *errorp = TRUE;
6390 return -1L;
6391 }
6392 return get_tv_number_chk(&li->li_tv, errorp);
6393}
6394
6395/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006396 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6397 */
6398 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006399list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006400{
6401 listitem_T *li;
6402
6403 li = list_find(l, idx - 1);
6404 if (li == NULL)
6405 {
6406 EMSGN(_(e_listidx), idx);
6407 return NULL;
6408 }
6409 return get_tv_string(&li->li_tv);
6410}
6411
6412/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006413 * Locate "item" list "l" and return its index.
6414 * Returns -1 when "item" is not in the list.
6415 */
6416 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006417list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006418{
6419 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006420 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006421
6422 if (l == NULL)
6423 return -1;
6424 idx = 0;
6425 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6426 ++idx;
6427 if (li == NULL)
6428 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006429 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006430}
6431
6432/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006433 * Append item "item" to the end of list "l".
6434 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006435 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006436list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006437{
6438 if (l->lv_last == NULL)
6439 {
6440 /* empty list */
6441 l->lv_first = item;
6442 l->lv_last = item;
6443 item->li_prev = NULL;
6444 }
6445 else
6446 {
6447 l->lv_last->li_next = item;
6448 item->li_prev = l->lv_last;
6449 l->lv_last = item;
6450 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006451 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 item->li_next = NULL;
6453}
6454
6455/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006456 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006457 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006459 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006460list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006461{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006462 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006463
Bram Moolenaar05159a02005-02-26 23:04:13 +00006464 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006465 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006466 copy_tv(tv, &li->li_tv);
6467 list_append(l, li);
6468 return OK;
6469}
6470
6471/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006472 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006473 * Return FAIL when out of memory.
6474 */
6475 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006476list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006477{
6478 listitem_T *li = listitem_alloc();
6479
6480 if (li == NULL)
6481 return FAIL;
6482 li->li_tv.v_type = VAR_DICT;
6483 li->li_tv.v_lock = 0;
6484 li->li_tv.vval.v_dict = dict;
6485 list_append(list, li);
6486 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006487 return OK;
6488}
6489
6490/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006491 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006492 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006493 * Returns FAIL when out of memory.
6494 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006495 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006496list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006497{
6498 listitem_T *li = listitem_alloc();
6499
6500 if (li == NULL)
6501 return FAIL;
6502 list_append(l, li);
6503 li->li_tv.v_type = VAR_STRING;
6504 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006505 if (str == NULL)
6506 li->li_tv.vval.v_string = NULL;
6507 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006508 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006509 return FAIL;
6510 return OK;
6511}
6512
6513/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006514 * Append "n" to list "l".
6515 * Returns FAIL when out of memory.
6516 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006517 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006518list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006519{
6520 listitem_T *li;
6521
6522 li = listitem_alloc();
6523 if (li == NULL)
6524 return FAIL;
6525 li->li_tv.v_type = VAR_NUMBER;
6526 li->li_tv.v_lock = 0;
6527 li->li_tv.vval.v_number = n;
6528 list_append(l, li);
6529 return OK;
6530}
6531
6532/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534 * If "item" is NULL append at the end.
6535 * Return FAIL when out of memory.
6536 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006537 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006538list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006539{
Bram Moolenaar33570922005-01-25 22:26:29 +00006540 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006541
6542 if (ni == NULL)
6543 return FAIL;
6544 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006545 list_insert(l, ni, item);
6546 return OK;
6547}
6548
6549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006550list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006551{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 if (item == NULL)
6553 /* Append new item at end of list. */
6554 list_append(l, ni);
6555 else
6556 {
6557 /* Insert new item before existing item. */
6558 ni->li_prev = item->li_prev;
6559 ni->li_next = item;
6560 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006563 ++l->lv_idx;
6564 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006566 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006568 l->lv_idx_item = NULL;
6569 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006570 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006571 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006573}
6574
6575/*
6576 * Extend "l1" with "l2".
6577 * If "bef" is NULL append at the end, otherwise insert before this item.
6578 * Returns FAIL when out of memory.
6579 */
6580 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006581list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582{
Bram Moolenaar33570922005-01-25 22:26:29 +00006583 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006584 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006585
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006586 /* We also quit the loop when we have inserted the original item count of
6587 * the list, avoid a hang when we extend a list with itself. */
6588 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006589 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6590 return FAIL;
6591 return OK;
6592}
6593
6594/*
6595 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6596 * Return FAIL when out of memory.
6597 */
6598 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006599list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600{
Bram Moolenaar33570922005-01-25 22:26:29 +00006601 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006603 if (l1 == NULL || l2 == NULL)
6604 return FAIL;
6605
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006606 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006607 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006608 if (l == NULL)
6609 return FAIL;
6610 tv->v_type = VAR_LIST;
6611 tv->vval.v_list = l;
6612
6613 /* append all items from the second list */
6614 return list_extend(l, l2, NULL);
6615}
6616
6617/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006618 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006620 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006621 * Returns NULL when out of memory.
6622 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006623 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006624list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625{
Bram Moolenaar33570922005-01-25 22:26:29 +00006626 list_T *copy;
6627 listitem_T *item;
6628 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006629
6630 if (orig == NULL)
6631 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006632
6633 copy = list_alloc();
6634 if (copy != NULL)
6635 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006636 if (copyID != 0)
6637 {
6638 /* Do this before adding the items, because one of the items may
6639 * refer back to this list. */
6640 orig->lv_copyID = copyID;
6641 orig->lv_copylist = copy;
6642 }
6643 for (item = orig->lv_first; item != NULL && !got_int;
6644 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006645 {
6646 ni = listitem_alloc();
6647 if (ni == NULL)
6648 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006649 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006650 {
6651 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6652 {
6653 vim_free(ni);
6654 break;
6655 }
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006658 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 list_append(copy, ni);
6660 }
6661 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006662 if (item != NULL)
6663 {
6664 list_unref(copy);
6665 copy = NULL;
6666 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 }
6668
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006669 return copy;
6670}
6671
6672/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006674 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006675 * This used to be called list_remove, but that conflicts with a Sun header
6676 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006677 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006679vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680{
Bram Moolenaar33570922005-01-25 22:26:29 +00006681 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006682
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006683 /* notify watchers */
6684 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006685 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006686 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006687 list_fix_watch(l, ip);
6688 if (ip == item2)
6689 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006690 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006691
6692 if (item2->li_next == NULL)
6693 l->lv_last = item->li_prev;
6694 else
6695 item2->li_next->li_prev = item->li_prev;
6696 if (item->li_prev == NULL)
6697 l->lv_first = item2->li_next;
6698 else
6699 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006700 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701}
6702
6703/*
6704 * Return an allocated string with the string representation of a list.
6705 * May return NULL.
6706 */
6707 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006708list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006709{
6710 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711
6712 if (tv->vval.v_list == NULL)
6713 return NULL;
6714 ga_init2(&ga, (int)sizeof(char), 80);
6715 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006716 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006717 {
6718 vim_free(ga.ga_data);
6719 return NULL;
6720 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006721 ga_append(&ga, ']');
6722 ga_append(&ga, NUL);
6723 return (char_u *)ga.ga_data;
6724}
6725
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006726typedef struct join_S {
6727 char_u *s;
6728 char_u *tofree;
6729} join_T;
6730
6731 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006732list_join_inner(
6733 garray_T *gap, /* to store the result in */
6734 list_T *l,
6735 char_u *sep,
6736 int echo_style,
6737 int copyID,
6738 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006739{
6740 int i;
6741 join_T *p;
6742 int len;
6743 int sumlen = 0;
6744 int first = TRUE;
6745 char_u *tofree;
6746 char_u numbuf[NUMBUFLEN];
6747 listitem_T *item;
6748 char_u *s;
6749
6750 /* Stringify each item in the list. */
6751 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6752 {
6753 if (echo_style)
6754 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6755 else
6756 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6757 if (s == NULL)
6758 return FAIL;
6759
6760 len = (int)STRLEN(s);
6761 sumlen += len;
6762
Bram Moolenaarcde88542015-08-11 19:14:00 +02006763 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006764 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6765 if (tofree != NULL || s != numbuf)
6766 {
6767 p->s = s;
6768 p->tofree = tofree;
6769 }
6770 else
6771 {
6772 p->s = vim_strnsave(s, len);
6773 p->tofree = p->s;
6774 }
6775
6776 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006777 if (did_echo_string_emsg) /* recursion error, bail out */
6778 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006779 }
6780
6781 /* Allocate result buffer with its total size, avoid re-allocation and
6782 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6783 if (join_gap->ga_len >= 2)
6784 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6785 if (ga_grow(gap, sumlen + 2) == FAIL)
6786 return FAIL;
6787
6788 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6789 {
6790 if (first)
6791 first = FALSE;
6792 else
6793 ga_concat(gap, sep);
6794 p = ((join_T *)join_gap->ga_data) + i;
6795
6796 if (p->s != NULL)
6797 ga_concat(gap, p->s);
6798 line_breakcheck();
6799 }
6800
6801 return OK;
6802}
6803
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006804/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006805 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006806 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006807 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006808 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006810list_join(
6811 garray_T *gap,
6812 list_T *l,
6813 char_u *sep,
6814 int echo_style,
6815 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006816{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006817 garray_T join_ga;
6818 int retval;
6819 join_T *p;
6820 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006821
Bram Moolenaard39a7512015-04-16 22:51:22 +02006822 if (l->lv_len < 1)
6823 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006824 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6825 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6826
6827 /* Dispose each item in join_ga. */
6828 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006829 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006830 p = (join_T *)join_ga.ga_data;
6831 for (i = 0; i < join_ga.ga_len; ++i)
6832 {
6833 vim_free(p->tofree);
6834 ++p;
6835 }
6836 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006837 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006838
6839 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006840}
6841
6842/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006843 * Return the next (unique) copy ID.
6844 * Used for serializing nested structures.
6845 */
6846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006847get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006848{
6849 current_copyID += COPYID_INC;
6850 return current_copyID;
6851}
6852
6853/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006854 * Garbage collection for lists and dictionaries.
6855 *
6856 * We use reference counts to be able to free most items right away when they
6857 * are no longer used. But for composite items it's possible that it becomes
6858 * unused while the reference count is > 0: When there is a recursive
6859 * reference. Example:
6860 * :let l = [1, 2, 3]
6861 * :let d = {9: l}
6862 * :let l[1] = d
6863 *
6864 * Since this is quite unusual we handle this with garbage collection: every
6865 * once in a while find out which lists and dicts are not referenced from any
6866 * variable.
6867 *
6868 * Here is a good reference text about garbage collection (refers to Python
6869 * but it applies to all reference-counting mechanisms):
6870 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006871 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006872
6873/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006874 * Do garbage collection for lists and dicts.
6875 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006876 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006877 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006878garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006879{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006881 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006882 buf_T *buf;
6883 win_T *wp;
6884 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006885 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006886 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006888#ifdef FEAT_WINDOWS
6889 tabpage_T *tp;
6890#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006892 /* Only do this once. */
6893 want_garbage_collect = FALSE;
6894 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006895 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006896
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006897 /* We advance by two because we add one for items referenced through
6898 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006899 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006900
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006901 /*
6902 * 1. Go through all accessible variables and mark all lists and dicts
6903 * with copyID.
6904 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905
6906 /* Don't free variables in the previous_funccal list unless they are only
6907 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006908 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6910 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006911 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6912 NULL);
6913 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6914 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006915 }
6916
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006917 /* script-local variables */
6918 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006919 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006920
6921 /* buffer-local variables */
6922 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006923 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6924 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925
6926 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006927 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006928 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6929 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006930#ifdef FEAT_AUTOCMD
6931 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6933 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006934#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006936#ifdef FEAT_WINDOWS
6937 /* tabpage-local variables */
6938 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6940 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006941#endif
6942
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006943 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945
6946 /* function-local variables */
6947 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6948 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006949 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6950 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006951 }
6952
Bram Moolenaard812df62008-11-09 12:46:09 +00006953 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006955
Bram Moolenaar1dced572012-04-05 16:54:08 +02006956#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006957 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006958#endif
6959
Bram Moolenaardb913952012-06-29 12:54:53 +02006960#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006961 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006962#endif
6963
6964#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006966#endif
6967
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006968#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006969 abort = abort || set_ref_in_channel(copyID);
6970#endif
6971
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006972 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 /*
6975 * 2. Free lists and dictionaries that are not referenced.
6976 */
6977 did_free = free_unref_items(copyID);
6978
6979 /*
6980 * 3. Check if any funccal can be freed now.
6981 */
6982 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006983 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006984 if (can_free_funccal(*pfc, copyID))
6985 {
6986 fc = *pfc;
6987 *pfc = fc->caller;
6988 free_funccal(fc, TRUE);
6989 did_free = TRUE;
6990 did_free_funccal = TRUE;
6991 }
6992 else
6993 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006995 if (did_free_funccal)
6996 /* When a funccal was freed some more items might be garbage
6997 * collected, so run again. */
6998 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006999 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007000 else if (p_verbose > 0)
7001 {
7002 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
7003 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004
7005 return did_free;
7006}
7007
7008/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007009 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007010 */
7011 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007012free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007013{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007014 dict_T *dd, *dd_next;
7015 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007016 int did_free = FALSE;
7017
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007019 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 */
7021 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007022 {
7023 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007024 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007026 /* Free the Dictionary and ordinary items it contains, but don't
7027 * recurse into Lists and Dictionaries, they will be in the list
7028 * of dicts or list of lists. */
7029 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007030 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007031 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007032 dd = dd_next;
7033 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007034
7035 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007036 * Go through the list of lists and free items without the copyID.
7037 * But don't free a list that has a watcher (used in a for loop), these
7038 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 */
7040 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007041 {
7042 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007043 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7044 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007046 /* Free the List and ordinary items it contains, but don't recurse
7047 * into Lists and Dictionaries, they will be in the list of dicts
7048 * or list of lists. */
7049 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007050 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007051 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007052 ll = ll_next;
7053 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007054
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007055 return did_free;
7056}
7057
7058/*
7059 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 * "list_stack" is used to add lists to be marked. Can be NULL.
7061 *
7062 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007065set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066{
7067 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007069 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007070 hashtab_T *cur_ht;
7071 ht_stack_T *ht_stack = NULL;
7072 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007073
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007074 cur_ht = ht;
7075 for (;;)
7076 {
7077 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007078 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007079 /* Mark each item in the hashtab. If the item contains a hashtab
7080 * it is added to ht_stack, if it contains a list it is added to
7081 * list_stack. */
7082 todo = (int)cur_ht->ht_used;
7083 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7084 if (!HASHITEM_EMPTY(hi))
7085 {
7086 --todo;
7087 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7088 &ht_stack, list_stack);
7089 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007090 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007091
7092 if (ht_stack == NULL)
7093 break;
7094
7095 /* take an item from the stack */
7096 cur_ht = ht_stack->ht;
7097 tempitem = ht_stack;
7098 ht_stack = ht_stack->prev;
7099 free(tempitem);
7100 }
7101
7102 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103}
7104
7105/*
7106 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7108 *
7109 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007110 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007111 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007112set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 listitem_T *li;
7115 int abort = FALSE;
7116 list_T *cur_l;
7117 list_stack_T *list_stack = NULL;
7118 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007119
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007120 cur_l = l;
7121 for (;;)
7122 {
7123 if (!abort)
7124 /* Mark each item in the list. If the item contains a hashtab
7125 * it is added to ht_stack, if it contains a list it is added to
7126 * list_stack. */
7127 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7128 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7129 ht_stack, &list_stack);
7130 if (list_stack == NULL)
7131 break;
7132
7133 /* take an item from the stack */
7134 cur_l = list_stack->list;
7135 tempitem = list_stack;
7136 list_stack = list_stack->prev;
7137 free(tempitem);
7138 }
7139
7140 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007141}
7142
7143/*
7144 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007145 * "list_stack" is used to add lists to be marked. Can be NULL.
7146 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7147 *
7148 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007149 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007150 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007151set_ref_in_item(
7152 typval_T *tv,
7153 int copyID,
7154 ht_stack_T **ht_stack,
7155 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007156{
7157 dict_T *dd;
7158 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007159 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007160
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007161 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007162 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007163 if (tv->v_type == VAR_DICT)
7164 dd = tv->vval.v_dict;
7165 else if (tv->vval.v_partial != NULL)
7166 dd = tv->vval.v_partial->pt_dict;
7167 else
7168 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 if (dd != NULL && dd->dv_copyID != copyID)
7170 {
7171 /* Didn't see this dict yet. */
7172 dd->dv_copyID = copyID;
7173 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007174 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007175 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7176 }
7177 else
7178 {
7179 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7180 if (newitem == NULL)
7181 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007182 else
7183 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007184 newitem->ht = &dd->dv_hashtab;
7185 newitem->prev = *ht_stack;
7186 *ht_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 }
7190 }
7191 else if (tv->v_type == VAR_LIST)
7192 {
7193 ll = tv->vval.v_list;
7194 if (ll != NULL && ll->lv_copyID != copyID)
7195 {
7196 /* Didn't see this list yet. */
7197 ll->lv_copyID = copyID;
7198 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007200 abort = set_ref_in_list(ll, copyID, ht_stack);
7201 }
7202 else
7203 {
7204 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007205 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007206 if (newitem == NULL)
7207 abort = TRUE;
7208 else
7209 {
7210 newitem->list = ll;
7211 newitem->prev = *list_stack;
7212 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007213 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007214 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007215 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007216 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007217 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007218}
7219
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007220 static void
7221partial_free(partial_T *pt, int free_dict)
7222{
7223 int i;
7224
7225 for (i = 0; i < pt->pt_argc; ++i)
7226 clear_tv(&pt->pt_argv[i]);
7227 vim_free(pt->pt_argv);
7228 if (free_dict)
7229 dict_unref(pt->pt_dict);
7230 func_unref(pt->pt_name);
7231 vim_free(pt->pt_name);
7232 vim_free(pt);
7233}
7234
7235/*
7236 * Unreference a closure: decrement the reference count and free it when it
7237 * becomes zero.
7238 */
7239 void
7240partial_unref(partial_T *pt)
7241{
7242 if (pt != NULL && --pt->pt_refcount <= 0)
7243 partial_free(pt, TRUE);
7244}
7245
Bram Moolenaard9fba312005-06-26 22:34:35 +00007246/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007247 * Allocate an empty header for a dictionary.
7248 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007249 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007250dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007251{
Bram Moolenaar33570922005-01-25 22:26:29 +00007252 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007253
Bram Moolenaar33570922005-01-25 22:26:29 +00007254 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007255 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007256 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007257 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007258 if (first_dict != NULL)
7259 first_dict->dv_used_prev = d;
7260 d->dv_used_next = first_dict;
7261 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007262 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007263
Bram Moolenaar33570922005-01-25 22:26:29 +00007264 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007265 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007266 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007267 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007268 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007269 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007270 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007271}
7272
7273/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007274 * Allocate an empty dict for a return value.
7275 * Returns OK or FAIL.
7276 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007277 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007278rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007279{
7280 dict_T *d = dict_alloc();
7281
7282 if (d == NULL)
7283 return FAIL;
7284
7285 rettv->vval.v_dict = d;
7286 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007287 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007288 ++d->dv_refcount;
7289 return OK;
7290}
7291
7292
7293/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294 * Unreference a Dictionary: decrement the reference count and free it when it
7295 * becomes zero.
7296 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007297 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007298dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007299{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007300 if (d != NULL && --d->dv_refcount <= 0)
7301 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302}
7303
7304/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007305 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306 * Ignores the reference count.
7307 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007308 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007309dict_free(
7310 dict_T *d,
7311 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007312{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007313 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007315 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007316
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007317 /* Remove the dict from the list of dicts for garbage collection. */
7318 if (d->dv_used_prev == NULL)
7319 first_dict = d->dv_used_next;
7320 else
7321 d->dv_used_prev->dv_used_next = d->dv_used_next;
7322 if (d->dv_used_next != NULL)
7323 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7324
7325 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007326 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007327 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007329 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007330 if (!HASHITEM_EMPTY(hi))
7331 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007332 /* Remove the item before deleting it, just in case there is
7333 * something recursive causing trouble. */
7334 di = HI2DI(hi);
7335 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007336 if (recurse || (di->di_tv.v_type != VAR_LIST
7337 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007338 {
7339 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7340 {
7341 partial_T *pt = di->di_tv.vval.v_partial;
7342
7343 /* We unref the partial but not the dict it refers to. */
7344 if (pt != NULL && --pt->pt_refcount == 0)
7345 partial_free(pt, FALSE);
7346 }
7347 else
7348 clear_tv(&di->di_tv);
7349 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007350 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351 --todo;
7352 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007353 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007354 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 vim_free(d);
7356}
7357
7358/*
7359 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360 * The "key" is copied to the new item.
7361 * Note that the value of the item "di_tv" still needs to be initialized!
7362 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007363 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007364 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007365dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366{
Bram Moolenaar33570922005-01-25 22:26:29 +00007367 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007368
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007369 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007370 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007371 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007372 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007373 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007374 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007375 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007376}
7377
7378/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379 * Make a copy of a Dictionary item.
7380 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007381 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007382dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007383{
Bram Moolenaar33570922005-01-25 22:26:29 +00007384 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007385
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007386 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7387 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007388 if (di != NULL)
7389 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007391 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007392 copy_tv(&org->di_tv, &di->di_tv);
7393 }
7394 return di;
7395}
7396
7397/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 * Remove item "item" from Dictionary "dict" and free it.
7399 */
7400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007401dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402{
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404
Bram Moolenaar33570922005-01-25 22:26:29 +00007405 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007406 if (HASHITEM_EMPTY(hi))
7407 EMSG2(_(e_intern2), "dictitem_remove()");
7408 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007409 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007410 dictitem_free(item);
7411}
7412
7413/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414 * Free a dict item. Also clears the value.
7415 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007416 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007417dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007418{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007419 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007420 if (item->di_flags & DI_FLAGS_ALLOC)
7421 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007422}
7423
7424/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7426 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007427 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007428 * Returns NULL when out of memory.
7429 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007431dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007432{
Bram Moolenaar33570922005-01-25 22:26:29 +00007433 dict_T *copy;
7434 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007435 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007436 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007437
7438 if (orig == NULL)
7439 return NULL;
7440
7441 copy = dict_alloc();
7442 if (copy != NULL)
7443 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 if (copyID != 0)
7445 {
7446 orig->dv_copyID = copyID;
7447 orig->dv_copydict = copy;
7448 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007449 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007450 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007451 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007452 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007453 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007454 --todo;
7455
7456 di = dictitem_alloc(hi->hi_key);
7457 if (di == NULL)
7458 break;
7459 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007460 {
7461 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7462 copyID) == FAIL)
7463 {
7464 vim_free(di);
7465 break;
7466 }
7467 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007468 else
7469 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7470 if (dict_add(copy, di) == FAIL)
7471 {
7472 dictitem_free(di);
7473 break;
7474 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007477
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007479 if (todo > 0)
7480 {
7481 dict_unref(copy);
7482 copy = NULL;
7483 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007484 }
7485
7486 return copy;
7487}
7488
7489/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007490 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007491 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007492 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007493 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007494dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007495{
Bram Moolenaar33570922005-01-25 22:26:29 +00007496 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007497}
7498
Bram Moolenaar8c711452005-01-14 21:53:12 +00007499/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007500 * Add a number or string entry to dictionary "d".
7501 * When "str" is NULL use number "nr", otherwise use "str".
7502 * Returns FAIL when out of memory and when key already exists.
7503 */
7504 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007505dict_add_nr_str(
7506 dict_T *d,
7507 char *key,
7508 long nr,
7509 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007510{
7511 dictitem_T *item;
7512
7513 item = dictitem_alloc((char_u *)key);
7514 if (item == NULL)
7515 return FAIL;
7516 item->di_tv.v_lock = 0;
7517 if (str == NULL)
7518 {
7519 item->di_tv.v_type = VAR_NUMBER;
7520 item->di_tv.vval.v_number = nr;
7521 }
7522 else
7523 {
7524 item->di_tv.v_type = VAR_STRING;
7525 item->di_tv.vval.v_string = vim_strsave(str);
7526 }
7527 if (dict_add(d, item) == FAIL)
7528 {
7529 dictitem_free(item);
7530 return FAIL;
7531 }
7532 return OK;
7533}
7534
7535/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007536 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007537 * Returns FAIL when out of memory and when key already exists.
7538 */
7539 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007540dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007541{
7542 dictitem_T *item;
7543
7544 item = dictitem_alloc((char_u *)key);
7545 if (item == NULL)
7546 return FAIL;
7547 item->di_tv.v_lock = 0;
7548 item->di_tv.v_type = VAR_LIST;
7549 item->di_tv.vval.v_list = list;
7550 if (dict_add(d, item) == FAIL)
7551 {
7552 dictitem_free(item);
7553 return FAIL;
7554 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007555 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007556 return OK;
7557}
7558
7559/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560 * Get the number of items in a Dictionary.
7561 */
7562 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007563dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007564{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007565 if (d == NULL)
7566 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007567 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007568}
7569
7570/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571 * Find item "key[len]" in Dictionary "d".
7572 * If "len" is negative use strlen(key).
7573 * Returns NULL when not found.
7574 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007575 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007576dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578#define AKEYLEN 200
7579 char_u buf[AKEYLEN];
7580 char_u *akey;
7581 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007582 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007583
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 if (len < 0)
7585 akey = key;
7586 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007587 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 tofree = akey = vim_strnsave(key, len);
7589 if (akey == NULL)
7590 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007591 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 else
7593 {
7594 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007595 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007596 akey = buf;
7597 }
7598
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007600 vim_free(tofree);
7601 if (HASHITEM_EMPTY(hi))
7602 return NULL;
7603 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007604}
7605
7606/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007607 * Get a string item from a dictionary.
7608 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007609 * Returns NULL if the entry doesn't exist or out of memory.
7610 */
7611 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007612get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007613{
7614 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007615 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007616
7617 di = dict_find(d, key, -1);
7618 if (di == NULL)
7619 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007620 s = get_tv_string(&di->di_tv);
7621 if (save && s != NULL)
7622 s = vim_strsave(s);
7623 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007624}
7625
7626/*
7627 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007628 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007629 */
7630 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007631get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007632{
7633 dictitem_T *di;
7634
7635 di = dict_find(d, key, -1);
7636 if (di == NULL)
7637 return 0;
7638 return get_tv_number(&di->di_tv);
7639}
7640
7641/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007642 * Return an allocated string with the string representation of a Dictionary.
7643 * May return NULL.
7644 */
7645 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007646dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647{
7648 garray_T ga;
7649 int first = TRUE;
7650 char_u *tofree;
7651 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007652 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007653 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007654 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007655 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 return NULL;
7659 ga_init2(&ga, (int)sizeof(char), 80);
7660 ga_append(&ga, '{');
7661
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007662 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007663 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007664 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007665 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007666 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007667 --todo;
7668
7669 if (first)
7670 first = FALSE;
7671 else
7672 ga_concat(&ga, (char_u *)", ");
7673
7674 tofree = string_quote(hi->hi_key, FALSE);
7675 if (tofree != NULL)
7676 {
7677 ga_concat(&ga, tofree);
7678 vim_free(tofree);
7679 }
7680 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007681 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007682 if (s != NULL)
7683 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007685 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007686 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007687 line_breakcheck();
7688
Bram Moolenaar8c711452005-01-14 21:53:12 +00007689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007691 if (todo > 0)
7692 {
7693 vim_free(ga.ga_data);
7694 return NULL;
7695 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007696
7697 ga_append(&ga, '}');
7698 ga_append(&ga, NUL);
7699 return (char_u *)ga.ga_data;
7700}
7701
7702/*
7703 * Allocate a variable for a Dictionary and fill it from "*arg".
7704 * Return OK or FAIL. Returns NOTDONE for {expr}.
7705 */
7706 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007707get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708{
Bram Moolenaar33570922005-01-25 22:26:29 +00007709 dict_T *d = NULL;
7710 typval_T tvkey;
7711 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007712 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007713 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007714 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007715 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007716
7717 /*
7718 * First check if it's not a curly-braces thing: {expr}.
7719 * Must do this without evaluating, otherwise a function may be called
7720 * twice. Unfortunately this means we need to call eval1() twice for the
7721 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007722 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007724 if (*start != '}')
7725 {
7726 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7727 return FAIL;
7728 if (*start == '}')
7729 return NOTDONE;
7730 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007731
7732 if (evaluate)
7733 {
7734 d = dict_alloc();
7735 if (d == NULL)
7736 return FAIL;
7737 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 tvkey.v_type = VAR_UNKNOWN;
7739 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007740
7741 *arg = skipwhite(*arg + 1);
7742 while (**arg != '}' && **arg != NUL)
7743 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007744 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 if (**arg != ':')
7747 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007748 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007749 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007750 goto failret;
7751 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007752 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007754 key = get_tv_string_buf_chk(&tvkey, buf);
7755 if (key == NULL || *key == NUL)
7756 {
7757 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7758 if (key != NULL)
7759 EMSG(_(e_emptykey));
7760 clear_tv(&tvkey);
7761 goto failret;
7762 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007763 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764
7765 *arg = skipwhite(*arg + 1);
7766 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7767 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007768 if (evaluate)
7769 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007770 goto failret;
7771 }
7772 if (evaluate)
7773 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007775 if (item != NULL)
7776 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007777 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007778 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 clear_tv(&tv);
7780 goto failret;
7781 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 item = dictitem_alloc(key);
7783 clear_tv(&tvkey);
7784 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007785 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007786 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007787 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007788 if (dict_add(d, item) == FAIL)
7789 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 }
7791 }
7792
7793 if (**arg == '}')
7794 break;
7795 if (**arg != ',')
7796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007797 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007798 goto failret;
7799 }
7800 *arg = skipwhite(*arg + 1);
7801 }
7802
7803 if (**arg != '}')
7804 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007805 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007806failret:
7807 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007808 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007809 return FAIL;
7810 }
7811
7812 *arg = skipwhite(*arg + 1);
7813 if (evaluate)
7814 {
7815 rettv->v_type = VAR_DICT;
7816 rettv->vval.v_dict = d;
7817 ++d->dv_refcount;
7818 }
7819
7820 return OK;
7821}
7822
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007823#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007824#endif
7825
Bram Moolenaar17a13432016-01-24 14:22:10 +01007826 static char *
7827get_var_special_name(int nr)
7828{
7829 switch (nr)
7830 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007831 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007832 case VVAL_TRUE: return "v:true";
7833 case VVAL_NONE: return "v:none";
7834 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007835 }
7836 EMSG2(_(e_intern2), "get_var_special_name()");
7837 return "42";
7838}
7839
Bram Moolenaar8c711452005-01-14 21:53:12 +00007840/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 * Return a string with the string representation of a variable.
7842 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007843 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007844 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007845 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007846 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007847 */
7848 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007849echo_string(
7850 typval_T *tv,
7851 char_u **tofree,
7852 char_u *numbuf,
7853 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007854{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007855 static int recurse = 0;
7856 char_u *r = NULL;
7857
Bram Moolenaar33570922005-01-25 22:26:29 +00007858 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007859 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007860 if (!did_echo_string_emsg)
7861 {
7862 /* Only give this message once for a recursive call to avoid
7863 * flooding the user with errors. And stop iterating over lists
7864 * and dicts. */
7865 did_echo_string_emsg = TRUE;
7866 EMSG(_("E724: variable nested too deep for displaying"));
7867 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007868 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007869 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007870 }
7871 ++recurse;
7872
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007873 switch (tv->v_type)
7874 {
7875 case VAR_FUNC:
7876 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007877 r = tv->vval.v_string;
7878 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007880 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007881 {
7882 partial_T *pt = tv->vval.v_partial;
7883 char_u *fname = string_quote(pt == NULL ? NULL
7884 : pt->pt_name, FALSE);
7885 garray_T ga;
7886 int i;
7887 char_u *tf;
7888
7889 ga_init2(&ga, 1, 100);
7890 ga_concat(&ga, (char_u *)"function(");
7891 if (fname != NULL)
7892 {
7893 ga_concat(&ga, fname);
7894 vim_free(fname);
7895 }
7896 if (pt != NULL && pt->pt_argc > 0)
7897 {
7898 ga_concat(&ga, (char_u *)", [");
7899 for (i = 0; i < pt->pt_argc; ++i)
7900 {
7901 if (i > 0)
7902 ga_concat(&ga, (char_u *)", ");
7903 ga_concat(&ga,
7904 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7905 vim_free(tf);
7906 }
7907 ga_concat(&ga, (char_u *)"]");
7908 }
7909 if (pt != NULL && pt->pt_dict != NULL)
7910 {
7911 typval_T dtv;
7912
7913 ga_concat(&ga, (char_u *)", ");
7914 dtv.v_type = VAR_DICT;
7915 dtv.vval.v_dict = pt->pt_dict;
7916 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7917 vim_free(tf);
7918 }
7919 ga_concat(&ga, (char_u *)")");
7920
7921 *tofree = ga.ga_data;
7922 r = *tofree;
7923 break;
7924 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007925
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007926 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007927 if (tv->vval.v_list == NULL)
7928 {
7929 *tofree = NULL;
7930 r = NULL;
7931 }
7932 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7933 {
7934 *tofree = NULL;
7935 r = (char_u *)"[...]";
7936 }
7937 else
7938 {
7939 tv->vval.v_list->lv_copyID = copyID;
7940 *tofree = list2string(tv, copyID);
7941 r = *tofree;
7942 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007943 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007944
Bram Moolenaar8c711452005-01-14 21:53:12 +00007945 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007946 if (tv->vval.v_dict == NULL)
7947 {
7948 *tofree = NULL;
7949 r = NULL;
7950 }
7951 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7952 {
7953 *tofree = NULL;
7954 r = (char_u *)"{...}";
7955 }
7956 else
7957 {
7958 tv->vval.v_dict->dv_copyID = copyID;
7959 *tofree = dict2string(tv, copyID);
7960 r = *tofree;
7961 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007962 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007963
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007964 case VAR_STRING:
7965 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007966 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007967 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007968 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007969 *tofree = NULL;
7970 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007971 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007972
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007973 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007974#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007975 *tofree = NULL;
7976 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7977 r = numbuf;
7978 break;
7979#endif
7980
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007981 case VAR_SPECIAL:
7982 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007983 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007984 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007985 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007986
Bram Moolenaar8502c702014-06-17 12:51:16 +02007987 if (--recurse == 0)
7988 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007989 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990}
7991
7992/*
7993 * Return a string with the string representation of a variable.
7994 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7995 * "numbuf" is used for a number.
7996 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007997 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007998 */
7999 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008000tv2string(
8001 typval_T *tv,
8002 char_u **tofree,
8003 char_u *numbuf,
8004 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008005{
8006 switch (tv->v_type)
8007 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008 case VAR_FUNC:
8009 *tofree = string_quote(tv->vval.v_string, TRUE);
8010 return *tofree;
8011 case VAR_STRING:
8012 *tofree = string_quote(tv->vval.v_string, FALSE);
8013 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008014 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008015#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008016 *tofree = NULL;
8017 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8018 return numbuf;
8019#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008021 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008022 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008023 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008024 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008025 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008026 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008027 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008028 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008029 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008030 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008031}
8032
8033/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008034 * Return string "str" in ' quotes, doubling ' characters.
8035 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008036 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008037 */
8038 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008039string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008040{
Bram Moolenaar33570922005-01-25 22:26:29 +00008041 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008042 char_u *p, *r, *s;
8043
Bram Moolenaar33570922005-01-25 22:26:29 +00008044 len = (function ? 13 : 3);
8045 if (str != NULL)
8046 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008047 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008048 for (p = str; *p != NUL; mb_ptr_adv(p))
8049 if (*p == '\'')
8050 ++len;
8051 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 s = r = alloc(len);
8053 if (r != NULL)
8054 {
8055 if (function)
8056 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008057 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008058 r += 10;
8059 }
8060 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008062 if (str != NULL)
8063 for (p = str; *p != NUL; )
8064 {
8065 if (*p == '\'')
8066 *r++ = '\'';
8067 MB_COPY_CHAR(p, r);
8068 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008069 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008070 if (function)
8071 *r++ = ')';
8072 *r++ = NUL;
8073 }
8074 return s;
8075}
8076
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008077#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078/*
8079 * Convert the string "text" to a floating point number.
8080 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8081 * this always uses a decimal point.
8082 * Returns the length of the text that was consumed.
8083 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008084 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008085string2float(
8086 char_u *text,
8087 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008088{
8089 char *s = (char *)text;
8090 float_T f;
8091
8092 f = strtod(s, &s);
8093 *value = f;
8094 return (int)((char_u *)s - text);
8095}
8096#endif
8097
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008098/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 * Get the value of an environment variable.
8100 * "arg" is pointing to the '$'. It is advanced to after the name.
8101 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008102 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008103 */
8104 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008105get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106{
8107 char_u *string = NULL;
8108 int len;
8109 int cc;
8110 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008111 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008112
8113 ++*arg;
8114 name = *arg;
8115 len = get_env_len(arg);
8116 if (evaluate)
8117 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008118 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008119 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008120
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008121 cc = name[len];
8122 name[len] = NUL;
8123 /* first try vim_getenv(), fast for normal environment vars */
8124 string = vim_getenv(name, &mustfree);
8125 if (string != NULL && *string != NUL)
8126 {
8127 if (!mustfree)
8128 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008129 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008130 else
8131 {
8132 if (mustfree)
8133 vim_free(string);
8134
8135 /* next try expanding things like $VIM and ${HOME} */
8136 string = expand_env_save(name - 1);
8137 if (string != NULL && *string == '$')
8138 {
8139 vim_free(string);
8140 string = NULL;
8141 }
8142 }
8143 name[len] = cc;
8144
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008145 rettv->v_type = VAR_STRING;
8146 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008147 }
8148
8149 return OK;
8150}
8151
8152/*
8153 * Array with names and number of arguments of all internal functions
8154 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8155 */
8156static struct fst
8157{
8158 char *f_name; /* function name */
8159 char f_min_argc; /* minimal number of arguments */
8160 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008161 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008162 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008163} functions[] =
8164{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008165#ifdef FEAT_FLOAT
8166 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008167 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008168#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008169 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008170 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008171 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008172 {"append", 2, 2, f_append},
8173 {"argc", 0, 0, f_argc},
8174 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008175 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008176 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008177#ifdef FEAT_FLOAT
8178 {"asin", 1, 1, f_asin}, /* WJMc */
8179#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008180 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008181 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008182 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008183 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008184 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008185 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008186#ifdef FEAT_FLOAT
8187 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008188 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008189#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008190 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008191 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"bufexists", 1, 1, f_bufexists},
8193 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8194 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8195 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8196 {"buflisted", 1, 1, f_buflisted},
8197 {"bufloaded", 1, 1, f_bufloaded},
8198 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008199 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008200 {"bufwinnr", 1, 1, f_bufwinnr},
8201 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008202 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008203 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008204 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008205#ifdef FEAT_FLOAT
8206 {"ceil", 1, 1, f_ceil},
8207#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008208#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008209 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008210 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8211 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008212 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008213 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008214 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008215 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008216 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008217 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008218 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008219 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008220 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8221 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008222 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008223 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008224#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008225 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008226 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008228 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008230#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008231 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008232 {"complete_add", 1, 1, f_complete_add},
8233 {"complete_check", 0, 0, f_complete_check},
8234#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008235 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008236 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008237#ifdef FEAT_FLOAT
8238 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008239 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008240#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008241 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008243 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008244 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008245 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008246 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008247 {"diff_filler", 1, 1, f_diff_filler},
8248 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008249 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008250 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008251 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008252 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008253 {"eventhandler", 0, 0, f_eventhandler},
8254 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008255 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008256 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008257#ifdef FEAT_FLOAT
8258 {"exp", 1, 1, f_exp},
8259#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008260 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008261 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008262 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008263 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8264 {"filereadable", 1, 1, f_filereadable},
8265 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008266 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008267 {"finddir", 1, 3, f_finddir},
8268 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008269#ifdef FEAT_FLOAT
8270 {"float2nr", 1, 1, f_float2nr},
8271 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008272 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008273#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008274 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"fnamemodify", 2, 2, f_fnamemodify},
8276 {"foldclosed", 1, 1, f_foldclosed},
8277 {"foldclosedend", 1, 1, f_foldclosedend},
8278 {"foldlevel", 1, 1, f_foldlevel},
8279 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008280 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008282 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008283 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008284 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008285 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008286 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008287 {"getchar", 0, 1, f_getchar},
8288 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008289 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getcmdline", 0, 0, f_getcmdline},
8291 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008292 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008293 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008294 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008295 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008296 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008297 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008298 {"getfsize", 1, 1, f_getfsize},
8299 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008300 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008301 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008302 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008303 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008304 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008305 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008306 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008307 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008308 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008309 {"gettabvar", 2, 3, f_gettabvar},
8310 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"getwinposx", 0, 0, f_getwinposx},
8312 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008313 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008314 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008315 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008316 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008317 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008318 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008319 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008320 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008321 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8322 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8323 {"histadd", 2, 2, f_histadd},
8324 {"histdel", 1, 2, f_histdel},
8325 {"histget", 1, 2, f_histget},
8326 {"histnr", 1, 1, f_histnr},
8327 {"hlID", 1, 1, f_hlID},
8328 {"hlexists", 1, 1, f_hlexists},
8329 {"hostname", 0, 0, f_hostname},
8330 {"iconv", 3, 3, f_iconv},
8331 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008332 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008333 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008334 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008335 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"inputrestore", 0, 0, f_inputrestore},
8337 {"inputsave", 0, 0, f_inputsave},
8338 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008339 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008340 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008341 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008342 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008343#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8344 {"isnan", 1, 1, f_isnan},
8345#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008346 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008347#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008348 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008349 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008350 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008351 {"job_start", 1, 2, f_job_start},
8352 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008353 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008354#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008355 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008356 {"js_decode", 1, 1, f_js_decode},
8357 {"js_encode", 1, 1, f_js_encode},
8358 {"json_decode", 1, 1, f_json_decode},
8359 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008360 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008361 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008362 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"libcall", 3, 3, f_libcall},
8364 {"libcallnr", 3, 3, f_libcallnr},
8365 {"line", 1, 1, f_line},
8366 {"line2byte", 1, 1, f_line2byte},
8367 {"lispindent", 1, 1, f_lispindent},
8368 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008369#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008370 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008371 {"log10", 1, 1, f_log10},
8372#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008373#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008374 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008375#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008376 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008377 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008378 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008379 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008380 {"matchadd", 2, 5, f_matchadd},
8381 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008382 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008383 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008384 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008385 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008386 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar7fed5c12016-03-29 23:10:31 +02008387 {"matchstrpos", 2, 4, f_matchstrpos},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008388 {"max", 1, 1, f_max},
8389 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008390#ifdef vim_mkdir
8391 {"mkdir", 1, 3, f_mkdir},
8392#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008393 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008394#ifdef FEAT_MZSCHEME
8395 {"mzeval", 1, 1, f_mzeval},
8396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008398 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008399 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008400 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008401#ifdef FEAT_PERL
8402 {"perleval", 1, 1, f_perleval},
8403#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008404#ifdef FEAT_FLOAT
8405 {"pow", 2, 2, f_pow},
8406#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008407 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008408 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008409 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008410#ifdef FEAT_PYTHON3
8411 {"py3eval", 1, 1, f_py3eval},
8412#endif
8413#ifdef FEAT_PYTHON
8414 {"pyeval", 1, 1, f_pyeval},
8415#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008416 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008417 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008418 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008419#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008420 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008421#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008422 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"remote_expr", 2, 3, f_remote_expr},
8424 {"remote_foreground", 1, 1, f_remote_foreground},
8425 {"remote_peek", 1, 2, f_remote_peek},
8426 {"remote_read", 1, 1, f_remote_read},
8427 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008428 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008430 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008431 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008432 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008433#ifdef FEAT_FLOAT
8434 {"round", 1, 1, f_round},
8435#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008436 {"screenattr", 2, 2, f_screenattr},
8437 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008438 {"screencol", 0, 0, f_screencol},
8439 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008440 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008441 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008442 {"searchpair", 3, 7, f_searchpair},
8443 {"searchpairpos", 3, 7, f_searchpairpos},
8444 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008445 {"server2client", 2, 2, f_server2client},
8446 {"serverlist", 0, 0, f_serverlist},
8447 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008448 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008450 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008451 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008452 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008453 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008454 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008455 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008457 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008458 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008459 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008460#ifdef FEAT_CRYPT
8461 {"sha256", 1, 1, f_sha256},
8462#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008463 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008464 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008465 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#ifdef FEAT_FLOAT
8467 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008468 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008469#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008470 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008471 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008472 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008473 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008474 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008475#ifdef FEAT_FLOAT
8476 {"sqrt", 1, 1, f_sqrt},
8477 {"str2float", 1, 1, f_str2float},
8478#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008479 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008480 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008481 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008482#ifdef HAVE_STRFTIME
8483 {"strftime", 1, 2, f_strftime},
8484#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008485 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008486 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008487 {"strlen", 1, 1, f_strlen},
8488 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008489 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008490 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008491 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008492 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008493 {"substitute", 4, 4, f_substitute},
8494 {"synID", 3, 3, f_synID},
8495 {"synIDattr", 2, 3, f_synIDattr},
8496 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008497 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008498 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008499 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008500 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008501 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008502 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008503 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008504 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008505 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008506#ifdef FEAT_FLOAT
8507 {"tan", 1, 1, f_tan},
8508 {"tanh", 1, 1, f_tanh},
8509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008510 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008511 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008512#ifdef FEAT_TIMERS
8513 {"timer_start", 2, 3, f_timer_start},
8514 {"timer_stop", 1, 1, f_timer_stop},
8515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008516 {"tolower", 1, 1, f_tolower},
8517 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008518 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008519#ifdef FEAT_FLOAT
8520 {"trunc", 1, 1, f_trunc},
8521#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008522 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008523 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008524 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008525 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008526 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008527 {"virtcol", 1, 1, f_virtcol},
8528 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008529 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008530 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008531 {"win_getid", 0, 2, f_win_getid},
8532 {"win_gotoid", 1, 1, f_win_gotoid},
8533 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8534 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008535 {"winbufnr", 1, 1, f_winbufnr},
8536 {"wincol", 0, 0, f_wincol},
8537 {"winheight", 1, 1, f_winheight},
8538 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008539 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008541 {"winrestview", 1, 1, f_winrestview},
8542 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008543 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008544 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008545 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008546 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008547};
8548
8549#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8550
8551/*
8552 * Function given to ExpandGeneric() to obtain the list of internal
8553 * or user defined function names.
8554 */
8555 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008556get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008557{
8558 static int intidx = -1;
8559 char_u *name;
8560
8561 if (idx == 0)
8562 intidx = -1;
8563 if (intidx < 0)
8564 {
8565 name = get_user_func_name(xp, idx);
8566 if (name != NULL)
8567 return name;
8568 }
8569 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8570 {
8571 STRCPY(IObuff, functions[intidx].f_name);
8572 STRCAT(IObuff, "(");
8573 if (functions[intidx].f_max_argc == 0)
8574 STRCAT(IObuff, ")");
8575 return IObuff;
8576 }
8577
8578 return NULL;
8579}
8580
8581/*
8582 * Function given to ExpandGeneric() to obtain the list of internal or
8583 * user defined variable or function names.
8584 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008585 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008586get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008587{
8588 static int intidx = -1;
8589 char_u *name;
8590
8591 if (idx == 0)
8592 intidx = -1;
8593 if (intidx < 0)
8594 {
8595 name = get_function_name(xp, idx);
8596 if (name != NULL)
8597 return name;
8598 }
8599 return get_user_var_name(xp, ++intidx);
8600}
8601
8602#endif /* FEAT_CMDL_COMPL */
8603
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008604#if defined(EBCDIC) || defined(PROTO)
8605/*
8606 * Compare struct fst by function name.
8607 */
8608 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008609compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008610{
8611 struct fst *p1 = (struct fst *)s1;
8612 struct fst *p2 = (struct fst *)s2;
8613
8614 return STRCMP(p1->f_name, p2->f_name);
8615}
8616
8617/*
8618 * Sort the function table by function name.
8619 * The sorting of the table above is ASCII dependant.
8620 * On machines using EBCDIC we have to sort it.
8621 */
8622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008623sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008624{
8625 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8626
8627 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8628}
8629#endif
8630
8631
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632/*
8633 * Find internal function in table above.
8634 * Return index, or -1 if not found
8635 */
8636 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008637find_internal_func(
8638 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008639{
8640 int first = 0;
8641 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8642 int cmp;
8643 int x;
8644
8645 /*
8646 * Find the function name in the table. Binary search.
8647 */
8648 while (first <= last)
8649 {
8650 x = first + ((unsigned)(last - first) >> 1);
8651 cmp = STRCMP(name, functions[x].f_name);
8652 if (cmp < 0)
8653 last = x - 1;
8654 else if (cmp > 0)
8655 first = x + 1;
8656 else
8657 return x;
8658 }
8659 return -1;
8660}
8661
8662/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8664 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008665 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8666 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008667 */
8668 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008669deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670{
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008672 int cc;
8673
Bram Moolenaar65639032016-03-16 21:40:30 +01008674 if (partialp != NULL)
8675 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008676
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 cc = name[*lenp];
8678 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008679 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008680 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008681 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008682 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008683 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008684 {
8685 *lenp = 0;
8686 return (char_u *)""; /* just in case */
8687 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008688 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008689 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008690 }
8691
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008692 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8693 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008694 partial_T *pt = v->di_tv.vval.v_partial;
8695
8696 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008697 {
8698 *lenp = 0;
8699 return (char_u *)""; /* just in case */
8700 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008701 if (partialp != NULL)
8702 *partialp = pt;
8703 *lenp = (int)STRLEN(pt->pt_name);
8704 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008705 }
8706
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008707 return name;
8708}
8709
8710/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008711 * Allocate a variable for the result of a function.
8712 * Return OK or FAIL.
8713 */
8714 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008715get_func_tv(
8716 char_u *name, /* name of the function */
8717 int len, /* length of "name" */
8718 typval_T *rettv,
8719 char_u **arg, /* argument, pointing to the '(' */
8720 linenr_T firstline, /* first line of range */
8721 linenr_T lastline, /* last line of range */
8722 int *doesrange, /* return: function handled range */
8723 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008724 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008725 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008726{
8727 char_u *argp;
8728 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008729 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 int argcount = 0; /* number of arguments found */
8731
8732 /*
8733 * Get the arguments.
8734 */
8735 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008736 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008737 {
8738 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8739 if (*argp == ')' || *argp == ',' || *argp == NUL)
8740 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8742 {
8743 ret = FAIL;
8744 break;
8745 }
8746 ++argcount;
8747 if (*argp != ',')
8748 break;
8749 }
8750 if (*argp == ')')
8751 ++argp;
8752 else
8753 ret = FAIL;
8754
8755 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008756 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008757 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008759 {
8760 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008761 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008762 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008763 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008764 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765
8766 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008767 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768
8769 *arg = skipwhite(argp);
8770 return ret;
8771}
8772
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008773#define ERROR_UNKNOWN 0
8774#define ERROR_TOOMANY 1
8775#define ERROR_TOOFEW 2
8776#define ERROR_SCRIPT 3
8777#define ERROR_DICT 4
8778#define ERROR_NONE 5
8779#define ERROR_OTHER 6
8780#define FLEN_FIXED 40
8781
8782/*
8783 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8784 * Change <SNR>123_name() to K_SNR 123_name().
8785 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8786 * (slow).
8787 */
8788 static char_u *
8789fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8790{
8791 int llen;
8792 char_u *fname;
8793 int i;
8794
8795 llen = eval_fname_script(name);
8796 if (llen > 0)
8797 {
8798 fname_buf[0] = K_SPECIAL;
8799 fname_buf[1] = KS_EXTRA;
8800 fname_buf[2] = (int)KE_SNR;
8801 i = 3;
8802 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8803 {
8804 if (current_SID <= 0)
8805 *error = ERROR_SCRIPT;
8806 else
8807 {
8808 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8809 i = (int)STRLEN(fname_buf);
8810 }
8811 }
8812 if (i + STRLEN(name + llen) < FLEN_FIXED)
8813 {
8814 STRCPY(fname_buf + i, name + llen);
8815 fname = fname_buf;
8816 }
8817 else
8818 {
8819 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8820 if (fname == NULL)
8821 *error = ERROR_OTHER;
8822 else
8823 {
8824 *tofree = fname;
8825 mch_memmove(fname, fname_buf, (size_t)i);
8826 STRCPY(fname + i, name + llen);
8827 }
8828 }
8829 }
8830 else
8831 fname = name;
8832 return fname;
8833}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834
8835/*
8836 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008837 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008838 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008839 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008841call_func(
8842 char_u *funcname, /* name of the function */
8843 int len, /* length of "name" */
8844 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008845 int argcount_in, /* number of "argvars" */
8846 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008847 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008848 linenr_T firstline, /* first line of range */
8849 linenr_T lastline, /* last line of range */
8850 int *doesrange, /* return: function handled range */
8851 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008852 partial_T *partial, /* optional, can be NULL */
8853 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854{
8855 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 int error = ERROR_NONE;
8857 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008858 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008859 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008860 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008861 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008862 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008863 int argcount = argcount_in;
8864 typval_T *argvars = argvars_in;
8865 dict_T *selfdict = selfdict_in;
8866 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8867 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008868
8869 /* Make a copy of the name, if it comes from a funcref variable it could
8870 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008871 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008872 if (name == NULL)
8873 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008874
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008875 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008876
8877 *doesrange = FALSE;
8878
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008879 if (partial != NULL)
8880 {
8881 if (partial->pt_dict != NULL)
8882 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008883 /* When the function has a partial with a dict and there is a dict
8884 * argument, use the dict argument. That is backwards compatible.
8885 */
8886 if (selfdict_in == NULL)
8887 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008888 }
8889 if (error == ERROR_NONE && partial->pt_argc > 0)
8890 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008891 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8892 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8893 for (i = 0; i < argcount_in; ++i)
8894 argv[i + argv_clear] = argvars_in[i];
8895 argvars = argv;
8896 argcount = partial->pt_argc + argcount_in;
8897 }
8898 }
8899
Bram Moolenaar071d4272004-06-13 20:20:40 +00008900
8901 /* execute the function if no errors detected and executing */
8902 if (evaluate && error == ERROR_NONE)
8903 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008904 char_u *rfname = fname;
8905
8906 /* Ignore "g:" before a function name. */
8907 if (fname[0] == 'g' && fname[1] == ':')
8908 rfname = fname + 2;
8909
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008910 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8911 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912 error = ERROR_UNKNOWN;
8913
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008914 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008915 {
8916 /*
8917 * User defined function.
8918 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008919 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008920
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008922 /* Trigger FuncUndefined event, may load the function. */
8923 if (fp == NULL
8924 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008925 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008926 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008927 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008928 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008929 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 }
8931#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008932 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008933 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008934 {
8935 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008936 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008937 }
8938
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 if (fp != NULL)
8940 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008941 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008943 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008945 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008947 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008949 else
8950 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008951 int did_save_redo = FALSE;
8952
Bram Moolenaar071d4272004-06-13 20:20:40 +00008953 /*
8954 * Call the user function.
8955 * Save and restore search patterns, script variables and
8956 * redo buffer.
8957 */
8958 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008959#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008960 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008961#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008962 {
8963 saveRedobuff();
8964 did_save_redo = TRUE;
8965 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008966 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008967 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008968 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008969 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8970 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8971 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008972 /* Function was unreferenced while being used, free it
8973 * now. */
8974 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008975 if (did_save_redo)
8976 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008977 restore_search_patterns();
8978 error = ERROR_NONE;
8979 }
8980 }
8981 }
8982 else
8983 {
8984 /*
8985 * Find the function name in the table, call its implementation.
8986 */
8987 i = find_internal_func(fname);
8988 if (i >= 0)
8989 {
8990 if (argcount < functions[i].f_min_argc)
8991 error = ERROR_TOOFEW;
8992 else if (argcount > functions[i].f_max_argc)
8993 error = ERROR_TOOMANY;
8994 else
8995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008996 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008997 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008998 error = ERROR_NONE;
8999 }
9000 }
9001 }
9002 /*
9003 * The function call (or "FuncUndefined" autocommand sequence) might
9004 * have been aborted by an error, an interrupt, or an explicitly thrown
9005 * exception that has not been caught so far. This situation can be
9006 * tested for by calling aborting(). For an error in an internal
9007 * function or for the "E132" error in call_user_func(), however, the
9008 * throw point at which the "force_abort" flag (temporarily reset by
9009 * emsg()) is normally updated has not been reached yet. We need to
9010 * update that flag first to make aborting() reliable.
9011 */
9012 update_force_abort();
9013 }
9014 if (error == ERROR_NONE)
9015 ret = OK;
9016
9017 /*
9018 * Report an error unless the argument evaluation or function call has been
9019 * cancelled due to an aborting error, an interrupt, or an exception.
9020 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009021 if (!aborting())
9022 {
9023 switch (error)
9024 {
9025 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009026 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009027 break;
9028 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009029 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009030 break;
9031 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009032 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009033 name);
9034 break;
9035 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009036 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009037 name);
9038 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009039 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009040 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009041 name);
9042 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009043 }
9044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009045
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009046 while (argv_clear > 0)
9047 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009048 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009049 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009050
9051 return ret;
9052}
9053
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009054/*
9055 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009056 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009057 */
9058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009059emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009060{
9061 char_u *p;
9062
9063 if (*name == K_SPECIAL)
9064 p = concat_str((char_u *)"<SNR>", name + 3);
9065 else
9066 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009067 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009068 if (p != name)
9069 vim_free(p);
9070}
9071
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009072/*
9073 * Return TRUE for a non-zero Number and a non-empty String.
9074 */
9075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009076non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009077{
9078 return ((argvars[0].v_type == VAR_NUMBER
9079 && argvars[0].vval.v_number != 0)
9080 || (argvars[0].v_type == VAR_STRING
9081 && argvars[0].vval.v_string != NULL
9082 && *argvars[0].vval.v_string != NUL));
9083}
9084
Bram Moolenaar071d4272004-06-13 20:20:40 +00009085/*********************************************
9086 * Implementation of the built-in functions
9087 */
9088
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009089#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009090static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009091
9092/*
9093 * Get the float value of "argvars[0]" into "f".
9094 * Returns FAIL when the argument is not a Number or Float.
9095 */
9096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009097get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009098{
9099 if (argvars[0].v_type == VAR_FLOAT)
9100 {
9101 *f = argvars[0].vval.v_float;
9102 return OK;
9103 }
9104 if (argvars[0].v_type == VAR_NUMBER)
9105 {
9106 *f = (float_T)argvars[0].vval.v_number;
9107 return OK;
9108 }
9109 EMSG(_("E808: Number or Float required"));
9110 return FAIL;
9111}
9112
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009113/*
9114 * "abs(expr)" function
9115 */
9116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009117f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009118{
9119 if (argvars[0].v_type == VAR_FLOAT)
9120 {
9121 rettv->v_type = VAR_FLOAT;
9122 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9123 }
9124 else
9125 {
9126 varnumber_T n;
9127 int error = FALSE;
9128
9129 n = get_tv_number_chk(&argvars[0], &error);
9130 if (error)
9131 rettv->vval.v_number = -1;
9132 else if (n > 0)
9133 rettv->vval.v_number = n;
9134 else
9135 rettv->vval.v_number = -n;
9136 }
9137}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009138
9139/*
9140 * "acos()" function
9141 */
9142 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009143f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009144{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009145 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009146
9147 rettv->v_type = VAR_FLOAT;
9148 if (get_float_arg(argvars, &f) == OK)
9149 rettv->vval.v_float = acos(f);
9150 else
9151 rettv->vval.v_float = 0.0;
9152}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009153#endif
9154
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009156 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009157 */
9158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009159f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009160{
Bram Moolenaar33570922005-01-25 22:26:29 +00009161 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009162
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009165 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009166 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009167 && !tv_check_lock(l->lv_lock,
9168 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009169 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009170 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009171 }
9172 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009173 EMSG(_(e_listreq));
9174}
9175
9176/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009177 * "alloc_fail(id, countdown, repeat)" function
9178 */
9179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009180f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009181{
9182 if (argvars[0].v_type != VAR_NUMBER
9183 || argvars[0].vval.v_number <= 0
9184 || argvars[1].v_type != VAR_NUMBER
9185 || argvars[1].vval.v_number < 0
9186 || argvars[2].v_type != VAR_NUMBER)
9187 EMSG(_(e_invarg));
9188 else
9189 {
9190 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009191 if (alloc_fail_id >= aid_last)
9192 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009193 alloc_fail_countdown = argvars[1].vval.v_number;
9194 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009195 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009196 }
9197}
9198
9199/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009200 * "and(expr, expr)" function
9201 */
9202 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009203f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009204{
9205 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9206 & get_tv_number_chk(&argvars[1], NULL);
9207}
9208
9209/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009210 * "append(lnum, string/list)" function
9211 */
9212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009213f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009214{
9215 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009216 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009217 list_T *l = NULL;
9218 listitem_T *li = NULL;
9219 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009220 long added = 0;
9221
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009222 /* When coming here from Insert mode, sync undo, so that this can be
9223 * undone separately from what was previously inserted. */
9224 if (u_sync_once == 2)
9225 {
9226 u_sync_once = 1; /* notify that u_sync() was called */
9227 u_sync(TRUE);
9228 }
9229
Bram Moolenaar0d660222005-01-07 21:51:51 +00009230 lnum = get_tv_lnum(argvars);
9231 if (lnum >= 0
9232 && lnum <= curbuf->b_ml.ml_line_count
9233 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009235 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009236 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009237 l = argvars[1].vval.v_list;
9238 if (l == NULL)
9239 return;
9240 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009241 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009242 for (;;)
9243 {
9244 if (l == NULL)
9245 tv = &argvars[1]; /* append a string */
9246 else if (li == NULL)
9247 break; /* end of list */
9248 else
9249 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009250 line = get_tv_string_chk(tv);
9251 if (line == NULL) /* type error */
9252 {
9253 rettv->vval.v_number = 1; /* Failed */
9254 break;
9255 }
9256 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009257 ++added;
9258 if (l == NULL)
9259 break;
9260 li = li->li_next;
9261 }
9262
9263 appended_lines_mark(lnum, added);
9264 if (curwin->w_cursor.lnum > lnum)
9265 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009266 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009267 else
9268 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269}
9270
9271/*
9272 * "argc()" function
9273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
9281 * "argidx()" function
9282 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009283 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009284f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009285{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009286 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009287}
9288
9289/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009290 * "arglistid()" function
9291 */
9292 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009293f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009294{
9295 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009296
9297 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009298 wp = find_tabwin(&argvars[0], &argvars[1]);
9299 if (wp != NULL)
9300 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009301}
9302
9303/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009304 * "argv(nr)" function
9305 */
9306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009307f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009308{
9309 int idx;
9310
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009311 if (argvars[0].v_type != VAR_UNKNOWN)
9312 {
9313 idx = get_tv_number_chk(&argvars[0], NULL);
9314 if (idx >= 0 && idx < ARGCOUNT)
9315 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9316 else
9317 rettv->vval.v_string = NULL;
9318 rettv->v_type = VAR_STRING;
9319 }
9320 else if (rettv_list_alloc(rettv) == OK)
9321 for (idx = 0; idx < ARGCOUNT; ++idx)
9322 list_append_string(rettv->vval.v_list,
9323 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009324}
9325
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009326static void prepare_assert_error(garray_T*gap);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009327static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv, int is_match);
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009328static void assert_error(garray_T *gap);
9329static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009330
9331/*
9332 * Prepare "gap" for an assert error and add the sourcing position.
9333 */
9334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009335prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009336{
9337 char buf[NUMBUFLEN];
9338
9339 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009340 if (sourcing_name != NULL)
9341 {
9342 ga_concat(gap, sourcing_name);
9343 if (sourcing_lnum > 0)
9344 ga_concat(gap, (char_u *)" ");
9345 }
9346 if (sourcing_lnum > 0)
9347 {
9348 sprintf(buf, "line %ld", (long)sourcing_lnum);
9349 ga_concat(gap, (char_u *)buf);
9350 }
9351 if (sourcing_name != NULL || sourcing_lnum > 0)
9352 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009353}
9354
9355/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009356 * Append "str" to "gap", escaping unprintable characters.
9357 * Changes NL to \n, CR to \r, etc.
9358 */
9359 static void
9360ga_concat_esc(garray_T *gap, char_u *str)
9361{
9362 char_u *p;
9363 char_u buf[NUMBUFLEN];
9364
Bram Moolenaarf1551962016-03-15 12:55:58 +01009365 if (str == NULL)
9366 {
9367 ga_concat(gap, (char_u *)"NULL");
9368 return;
9369 }
9370
Bram Moolenaar23689172016-02-15 22:37:37 +01009371 for (p = str; *p != NUL; ++p)
9372 switch (*p)
9373 {
9374 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9375 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9376 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9377 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9378 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9379 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9380 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9381 default:
9382 if (*p < ' ')
9383 {
9384 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9385 ga_concat(gap, buf);
9386 }
9387 else
9388 ga_append(gap, *p);
9389 break;
9390 }
9391}
9392
9393/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009394 * Fill "gap" with information about an assert error.
9395 */
9396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009397fill_assert_error(
9398 garray_T *gap,
9399 typval_T *opt_msg_tv,
9400 char_u *exp_str,
9401 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009402 typval_T *got_tv,
9403 int is_match)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009404{
9405 char_u numbuf[NUMBUFLEN];
9406 char_u *tofree;
9407
9408 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9409 {
9410 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9411 vim_free(tofree);
9412 }
9413 else
9414 {
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009415 if (is_match)
9416 ga_concat(gap, (char_u *)"Pattern ");
9417 else
9418 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009419 if (exp_str == NULL)
9420 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009421 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009422 vim_free(tofree);
9423 }
9424 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009425 ga_concat_esc(gap, exp_str);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009426 if (is_match)
9427 ga_concat(gap, (char_u *)" does not match ");
9428 else
9429 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009430 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009431 vim_free(tofree);
9432 }
9433}
Bram Moolenaar43345542015-11-29 17:35:35 +01009434
9435/*
9436 * Add an assert error to v:errors.
9437 */
9438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009439assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009440{
9441 struct vimvar *vp = &vimvars[VV_ERRORS];
9442
9443 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9444 /* Make sure v:errors is a list. */
9445 set_vim_var_list(VV_ERRORS, list_alloc());
9446 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9447}
9448
Bram Moolenaar43345542015-11-29 17:35:35 +01009449/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009450 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009451 */
9452 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009453f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009454{
9455 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009456
9457 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9458 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009459 prepare_assert_error(&ga);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009460 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9461 FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009462 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009463 ga_clear(&ga);
9464 }
9465}
9466
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009467/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009468 * "assert_exception(string[, msg])" function
9469 */
9470 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009471f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009472{
9473 garray_T ga;
9474 char *error;
9475
9476 error = (char *)get_tv_string_chk(&argvars[0]);
9477 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9478 {
9479 prepare_assert_error(&ga);
9480 ga_concat(&ga, (char_u *)"v:exception is not set");
9481 assert_error(&ga);
9482 ga_clear(&ga);
9483 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009484 else if (error != NULL
9485 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009486 {
9487 prepare_assert_error(&ga);
9488 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009489 &vimvars[VV_EXCEPTION].vv_tv, FALSE);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009490 assert_error(&ga);
9491 ga_clear(&ga);
9492 }
9493}
9494
9495/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009496 * "assert_fails(cmd [, error])" function
9497 */
9498 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009499f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009500{
9501 char_u *cmd = get_tv_string_chk(&argvars[0]);
9502 garray_T ga;
9503
9504 called_emsg = FALSE;
9505 suppress_errthrow = TRUE;
9506 emsg_silent = TRUE;
9507 do_cmdline_cmd(cmd);
9508 if (!called_emsg)
9509 {
9510 prepare_assert_error(&ga);
9511 ga_concat(&ga, (char_u *)"command did not fail: ");
9512 ga_concat(&ga, cmd);
9513 assert_error(&ga);
9514 ga_clear(&ga);
9515 }
9516 else if (argvars[1].v_type != VAR_UNKNOWN)
9517 {
9518 char_u buf[NUMBUFLEN];
9519 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9520
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009521 if (error == NULL
9522 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009523 {
9524 prepare_assert_error(&ga);
9525 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009526 &vimvars[VV_ERRMSG].vv_tv, FALSE);
Bram Moolenaara260b872016-01-15 20:48:22 +01009527 assert_error(&ga);
9528 ga_clear(&ga);
9529 }
9530 }
9531
9532 called_emsg = FALSE;
9533 suppress_errthrow = FALSE;
9534 emsg_silent = FALSE;
9535 emsg_on_display = FALSE;
9536 set_vim_var_string(VV_ERRMSG, NULL, 0);
9537}
9538
9539/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009540 * Common for assert_true() and assert_false().
9541 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009543assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009544{
9545 int error = FALSE;
9546 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009547
Bram Moolenaar37127922016-02-06 20:29:28 +01009548 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009549 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009550 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009551 if (argvars[0].v_type != VAR_NUMBER
9552 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9553 || error)
9554 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009555 prepare_assert_error(&ga);
9556 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009557 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009558 NULL, &argvars[0], FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009559 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009560 ga_clear(&ga);
9561 }
9562}
9563
9564/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009565 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009566 */
9567 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009568f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009569{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009570 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009571}
9572
9573/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009574 * "assert_match(pattern, actual[, msg])" function
9575 */
9576 static void
9577f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9578{
9579 garray_T ga;
9580 char_u buf1[NUMBUFLEN];
9581 char_u buf2[NUMBUFLEN];
9582 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9583 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9584
Bram Moolenaar72188e92016-03-28 22:48:29 +02009585 if (pat == NULL || text == NULL)
9586 EMSG(_(e_invarg));
9587 else if (!pattern_match(pat, text, FALSE))
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009588 {
9589 prepare_assert_error(&ga);
9590 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9591 TRUE);
9592 assert_error(&ga);
9593 ga_clear(&ga);
9594 }
9595}
9596
9597/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009598 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009599 */
9600 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009601f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009602{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009603 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009604}
9605
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009606#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009607/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009608 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009609 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009610 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009611f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009612{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009613 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009614
9615 rettv->v_type = VAR_FLOAT;
9616 if (get_float_arg(argvars, &f) == OK)
9617 rettv->vval.v_float = asin(f);
9618 else
9619 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009620}
9621
9622/*
9623 * "atan()" function
9624 */
9625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009626f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009627{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009628 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009629
9630 rettv->v_type = VAR_FLOAT;
9631 if (get_float_arg(argvars, &f) == OK)
9632 rettv->vval.v_float = atan(f);
9633 else
9634 rettv->vval.v_float = 0.0;
9635}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009636
9637/*
9638 * "atan2()" function
9639 */
9640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009641f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009642{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009643 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009644
9645 rettv->v_type = VAR_FLOAT;
9646 if (get_float_arg(argvars, &fx) == OK
9647 && get_float_arg(&argvars[1], &fy) == OK)
9648 rettv->vval.v_float = atan2(fx, fy);
9649 else
9650 rettv->vval.v_float = 0.0;
9651}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009652#endif
9653
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654/*
9655 * "browse(save, title, initdir, default)" function
9656 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009658f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659{
9660#ifdef FEAT_BROWSE
9661 int save;
9662 char_u *title;
9663 char_u *initdir;
9664 char_u *defname;
9665 char_u buf[NUMBUFLEN];
9666 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009667 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009668
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009669 save = get_tv_number_chk(&argvars[0], &error);
9670 title = get_tv_string_chk(&argvars[1]);
9671 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9672 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009673
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009674 if (error || title == NULL || initdir == NULL || defname == NULL)
9675 rettv->vval.v_string = NULL;
9676 else
9677 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009678 do_browse(save ? BROWSE_SAVE : 0,
9679 title, defname, NULL, initdir, NULL, curbuf);
9680#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009681 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009682#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009683 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009684}
9685
9686/*
9687 * "browsedir(title, initdir)" function
9688 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009689 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009690f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009691{
9692#ifdef FEAT_BROWSE
9693 char_u *title;
9694 char_u *initdir;
9695 char_u buf[NUMBUFLEN];
9696
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009697 title = get_tv_string_chk(&argvars[0]);
9698 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009699
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009700 if (title == NULL || initdir == NULL)
9701 rettv->vval.v_string = NULL;
9702 else
9703 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009704 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009706 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009708 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709}
9710
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009711static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009712
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713/*
9714 * Find a buffer by number or exact name.
9715 */
9716 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009717find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009718{
9719 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009720
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009721 if (avar->v_type == VAR_NUMBER)
9722 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009723 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009725 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009726 if (buf == NULL)
9727 {
9728 /* No full path name match, try a match with a URL or a "nofile"
9729 * buffer, these don't use the full path. */
9730 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9731 if (buf->b_fname != NULL
9732 && (path_with_url(buf->b_fname)
9733#ifdef FEAT_QUICKFIX
9734 || bt_nofile(buf)
9735#endif
9736 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009737 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009738 break;
9739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740 }
9741 return buf;
9742}
9743
9744/*
9745 * "bufexists(expr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009750 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009751}
9752
9753/*
9754 * "buflisted(expr)" function
9755 */
9756 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009757f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009758{
9759 buf_T *buf;
9760
9761 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009762 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763}
9764
9765/*
9766 * "bufloaded(expr)" function
9767 */
9768 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009769f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
9771 buf_T *buf;
9772
9773 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775}
9776
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009777 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009778buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009779{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009780 int save_magic;
9781 char_u *save_cpo;
9782 buf_T *buf;
9783
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9785 save_magic = p_magic;
9786 p_magic = TRUE;
9787 save_cpo = p_cpo;
9788 p_cpo = (char_u *)"";
9789
9790 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009791 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009792
9793 p_magic = save_magic;
9794 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009795 return buf;
9796}
9797
9798/*
9799 * Get buffer by number or pattern.
9800 */
9801 static buf_T *
9802get_buf_tv(typval_T *tv, int curtab_only)
9803{
9804 char_u *name = tv->vval.v_string;
9805 buf_T *buf;
9806
9807 if (tv->v_type == VAR_NUMBER)
9808 return buflist_findnr((int)tv->vval.v_number);
9809 if (tv->v_type != VAR_STRING)
9810 return NULL;
9811 if (name == NULL || *name == NUL)
9812 return curbuf;
9813 if (name[0] == '$' && name[1] == NUL)
9814 return lastbuf;
9815
9816 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009817
9818 /* If not found, try expanding the name, like done for bufexists(). */
9819 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009820 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821
9822 return buf;
9823}
9824
9825/*
9826 * "bufname(expr)" function
9827 */
9828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009829f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830{
9831 buf_T *buf;
9832
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009833 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009834 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009835 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009836 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009837 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009838 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009839 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009840 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009841 --emsg_off;
9842}
9843
9844/*
9845 * "bufnr(expr)" function
9846 */
9847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009848f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009849{
9850 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009851 int error = FALSE;
9852 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009853
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009854 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009855 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009856 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009857 --emsg_off;
9858
9859 /* If the buffer isn't found and the second argument is not zero create a
9860 * new buffer. */
9861 if (buf == NULL
9862 && argvars[1].v_type != VAR_UNKNOWN
9863 && get_tv_number_chk(&argvars[1], &error) != 0
9864 && !error
9865 && (name = get_tv_string_chk(&argvars[0])) != NULL
9866 && !error)
9867 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9868
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009870 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009872 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009873}
9874
9875/*
9876 * "bufwinnr(nr)" function
9877 */
9878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009879f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009880{
9881#ifdef FEAT_WINDOWS
9882 win_T *wp;
9883 int winnr = 0;
9884#endif
9885 buf_T *buf;
9886
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009887 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009888 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009889 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009890#ifdef FEAT_WINDOWS
9891 for (wp = firstwin; wp; wp = wp->w_next)
9892 {
9893 ++winnr;
9894 if (wp->w_buffer == buf)
9895 break;
9896 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009897 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009899 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900#endif
9901 --emsg_off;
9902}
9903
9904/*
9905 * "byte2line(byte)" function
9906 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009908f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909{
9910#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009911 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009912#else
9913 long boff = 0;
9914
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009915 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009916 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009917 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009918 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009919 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009920 (linenr_T)0, &boff);
9921#endif
9922}
9923
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009925byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009926{
9927#ifdef FEAT_MBYTE
9928 char_u *t;
9929#endif
9930 char_u *str;
9931 long idx;
9932
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009933 str = get_tv_string_chk(&argvars[0]);
9934 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009935 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009936 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009937 return;
9938
9939#ifdef FEAT_MBYTE
9940 t = str;
9941 for ( ; idx > 0; idx--)
9942 {
9943 if (*t == NUL) /* EOL reached */
9944 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009945 if (enc_utf8 && comp)
9946 t += utf_ptr2len(t);
9947 else
9948 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009949 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009950 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009951#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009952 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009953 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009954#endif
9955}
9956
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009957/*
9958 * "byteidx()" function
9959 */
9960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009962{
9963 byteidx(argvars, rettv, FALSE);
9964}
9965
9966/*
9967 * "byteidxcomp()" function
9968 */
9969 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009970f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009971{
9972 byteidx(argvars, rettv, TRUE);
9973}
9974
Bram Moolenaardb913952012-06-29 12:54:53 +02009975 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009976func_call(
9977 char_u *name,
9978 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009979 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009980 dict_T *selfdict,
9981 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009982{
9983 listitem_T *item;
9984 typval_T argv[MAX_FUNC_ARGS + 1];
9985 int argc = 0;
9986 int dummy;
9987 int r = 0;
9988
9989 for (item = args->vval.v_list->lv_first; item != NULL;
9990 item = item->li_next)
9991 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009992 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009993 {
9994 EMSG(_("E699: Too many arguments"));
9995 break;
9996 }
9997 /* Make a copy of each argument. This is needed to be able to set
9998 * v_lock to VAR_FIXED in the copy without changing the original list.
9999 */
10000 copy_tv(&item->li_tv, &argv[argc++]);
10001 }
10002
10003 if (item == NULL)
10004 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
10005 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010006 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +020010007
10008 /* Free the arguments. */
10009 while (argc > 0)
10010 clear_tv(&argv[--argc]);
10011
10012 return r;
10013}
10014
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010015/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010016 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010017 */
10018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010019f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010020{
10021 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010022 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010023 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010024
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010025 if (argvars[1].v_type != VAR_LIST)
10026 {
10027 EMSG(_(e_listreq));
10028 return;
10029 }
10030 if (argvars[1].vval.v_list == NULL)
10031 return;
10032
10033 if (argvars[0].v_type == VAR_FUNC)
10034 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010035 else if (argvars[0].v_type == VAR_PARTIAL)
10036 {
10037 partial = argvars[0].vval.v_partial;
10038 func = partial->pt_name;
10039 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010040 else
10041 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010042 if (*func == NUL)
10043 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010044
Bram Moolenaare9a41262005-01-15 22:18:47 +000010045 if (argvars[2].v_type != VAR_UNKNOWN)
10046 {
10047 if (argvars[2].v_type != VAR_DICT)
10048 {
10049 EMSG(_(e_dictreq));
10050 return;
10051 }
10052 selfdict = argvars[2].vval.v_dict;
10053 }
10054
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010055 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010056}
10057
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010058#ifdef FEAT_FLOAT
10059/*
10060 * "ceil({float})" function
10061 */
10062 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010063f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010064{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010065 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010066
10067 rettv->v_type = VAR_FLOAT;
10068 if (get_float_arg(argvars, &f) == OK)
10069 rettv->vval.v_float = ceil(f);
10070 else
10071 rettv->vval.v_float = 0.0;
10072}
10073#endif
10074
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010075#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010076/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010077 * "ch_close()" function
10078 */
10079 static void
10080f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10081{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010082 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010083
10084 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010085 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010086 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010087 channel_clear(channel);
10088 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010089}
10090
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010091/*
10092 * "ch_getbufnr()" function
10093 */
10094 static void
10095f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10096{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010097 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010098
10099 rettv->vval.v_number = -1;
10100 if (channel != NULL)
10101 {
10102 char_u *what = get_tv_string(&argvars[1]);
10103 int part;
10104
10105 if (STRCMP(what, "err") == 0)
10106 part = PART_ERR;
10107 else if (STRCMP(what, "out") == 0)
10108 part = PART_OUT;
10109 else if (STRCMP(what, "in") == 0)
10110 part = PART_IN;
10111 else
10112 part = PART_SOCK;
10113 if (channel->ch_part[part].ch_buffer != NULL)
10114 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10115 }
10116}
10117
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010118/*
10119 * "ch_getjob()" function
10120 */
10121 static void
10122f_ch_getjob(typval_T *argvars, typval_T *rettv)
10123{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010124 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010125
10126 if (channel != NULL)
10127 {
10128 rettv->v_type = VAR_JOB;
10129 rettv->vval.v_job = channel->ch_job;
10130 if (channel->ch_job != NULL)
10131 ++channel->ch_job->jv_refcount;
10132 }
10133}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010134
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010135/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010136 * "ch_info()" function
10137 */
10138 static void
10139f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10140{
10141 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10142
10143 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10144 channel_info(channel, rettv->vval.v_dict);
10145}
10146
10147/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010148 * "ch_log()" function
10149 */
10150 static void
10151f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10152{
10153 char_u *msg = get_tv_string(&argvars[0]);
10154 channel_T *channel = NULL;
10155
10156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010157 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010158
10159 ch_log(channel, (char *)msg);
10160}
10161
10162/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010163 * "ch_logfile()" function
10164 */
10165 static void
10166f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10167{
10168 char_u *fname;
10169 char_u *opt = (char_u *)"";
10170 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010171
10172 fname = get_tv_string(&argvars[0]);
10173 if (argvars[1].v_type == VAR_STRING)
10174 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010175 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010176}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010177
10178/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010179 * "ch_open()" function
10180 */
10181 static void
10182f_ch_open(typval_T *argvars, typval_T *rettv)
10183{
Bram Moolenaar77073442016-02-13 23:23:53 +010010184 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010185 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010186}
10187
10188/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010189 * "ch_read()" function
10190 */
10191 static void
10192f_ch_read(typval_T *argvars, typval_T *rettv)
10193{
10194 common_channel_read(argvars, rettv, FALSE);
10195}
10196
10197/*
10198 * "ch_readraw()" function
10199 */
10200 static void
10201f_ch_readraw(typval_T *argvars, typval_T *rettv)
10202{
10203 common_channel_read(argvars, rettv, TRUE);
10204}
10205
10206/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010207 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010208 */
10209 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010210f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10211{
10212 ch_expr_common(argvars, rettv, TRUE);
10213}
10214
10215/*
10216 * "ch_sendexpr()" function
10217 */
10218 static void
10219f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10220{
10221 ch_expr_common(argvars, rettv, FALSE);
10222}
10223
10224/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010225 * "ch_evalraw()" function
10226 */
10227 static void
10228f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10229{
10230 ch_raw_common(argvars, rettv, TRUE);
10231}
10232
10233/*
10234 * "ch_sendraw()" function
10235 */
10236 static void
10237f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10238{
10239 ch_raw_common(argvars, rettv, FALSE);
10240}
10241
10242/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010243 * "ch_setoptions()" function
10244 */
10245 static void
10246f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10247{
10248 channel_T *channel;
10249 jobopt_T opt;
10250
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010251 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010252 if (channel == NULL)
10253 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010254 clear_job_options(&opt);
10255 if (get_job_options(&argvars[1], &opt,
10256 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010257 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010258 channel_set_options(channel, &opt);
10259}
10260
10261/*
10262 * "ch_status()" function
10263 */
10264 static void
10265f_ch_status(typval_T *argvars, typval_T *rettv)
10266{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010267 channel_T *channel;
10268
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010269 /* return an empty string by default */
10270 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010271 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010272
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010273 channel = get_channel_arg(&argvars[0], FALSE);
10274 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010275}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010276#endif
10277
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010278/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010279 * "changenr()" function
10280 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010282f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010283{
10284 rettv->vval.v_number = curbuf->b_u_seq_cur;
10285}
10286
10287/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010288 * "char2nr(string)" function
10289 */
10290 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010291f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010292{
10293#ifdef FEAT_MBYTE
10294 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010295 {
10296 int utf8 = 0;
10297
10298 if (argvars[1].v_type != VAR_UNKNOWN)
10299 utf8 = get_tv_number_chk(&argvars[1], NULL);
10300
10301 if (utf8)
10302 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10303 else
10304 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10305 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010306 else
10307#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010308 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010309}
10310
10311/*
10312 * "cindent(lnum)" function
10313 */
10314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010315f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316{
10317#ifdef FEAT_CINDENT
10318 pos_T pos;
10319 linenr_T lnum;
10320
10321 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10324 {
10325 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010326 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010327 curwin->w_cursor = pos;
10328 }
10329 else
10330#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010331 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010332}
10333
10334/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010335 * "clearmatches()" function
10336 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010338f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010339{
10340#ifdef FEAT_SEARCH_EXTRA
10341 clear_matches(curwin);
10342#endif
10343}
10344
10345/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 * "col(string)" function
10347 */
10348 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010349f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010350{
10351 colnr_T col = 0;
10352 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010353 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010355 fp = var2fpos(&argvars[0], FALSE, &fnum);
10356 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010357 {
10358 if (fp->col == MAXCOL)
10359 {
10360 /* '> can be MAXCOL, get the length of the line then */
10361 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010362 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010363 else
10364 col = MAXCOL;
10365 }
10366 else
10367 {
10368 col = fp->col + 1;
10369#ifdef FEAT_VIRTUALEDIT
10370 /* col(".") when the cursor is on the NUL at the end of the line
10371 * because of "coladd" can be seen as an extra column. */
10372 if (virtual_active() && fp == &curwin->w_cursor)
10373 {
10374 char_u *p = ml_get_cursor();
10375
10376 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10377 curwin->w_virtcol - curwin->w_cursor.coladd))
10378 {
10379# ifdef FEAT_MBYTE
10380 int l;
10381
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010382 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383 col += l;
10384# else
10385 if (*p != NUL && p[1] == NUL)
10386 ++col;
10387# endif
10388 }
10389 }
10390#endif
10391 }
10392 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010393 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010394}
10395
Bram Moolenaar572cb562005-08-05 21:35:02 +000010396#if defined(FEAT_INS_EXPAND)
10397/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010398 * "complete()" function
10399 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010401f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010402{
10403 int startcol;
10404
10405 if ((State & INSERT) == 0)
10406 {
10407 EMSG(_("E785: complete() can only be used in Insert mode"));
10408 return;
10409 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010410
10411 /* Check for undo allowed here, because if something was already inserted
10412 * the line was already saved for undo and this check isn't done. */
10413 if (!undo_allowed())
10414 return;
10415
Bram Moolenaarade00832006-03-10 21:46:58 +000010416 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10417 {
10418 EMSG(_(e_invarg));
10419 return;
10420 }
10421
10422 startcol = get_tv_number_chk(&argvars[0], NULL);
10423 if (startcol <= 0)
10424 return;
10425
10426 set_completion(startcol - 1, argvars[1].vval.v_list);
10427}
10428
10429/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010430 * "complete_add()" function
10431 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010434{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010435 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010436}
10437
10438/*
10439 * "complete_check()" function
10440 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010442f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010443{
10444 int saved = RedrawingDisabled;
10445
10446 RedrawingDisabled = 0;
10447 ins_compl_check_keys(0);
10448 rettv->vval.v_number = compl_interrupted;
10449 RedrawingDisabled = saved;
10450}
10451#endif
10452
Bram Moolenaar071d4272004-06-13 20:20:40 +000010453/*
10454 * "confirm(message, buttons[, default [, type]])" function
10455 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010457f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010458{
10459#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10460 char_u *message;
10461 char_u *buttons = NULL;
10462 char_u buf[NUMBUFLEN];
10463 char_u buf2[NUMBUFLEN];
10464 int def = 1;
10465 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010466 char_u *typestr;
10467 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010468
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010469 message = get_tv_string_chk(&argvars[0]);
10470 if (message == NULL)
10471 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010472 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010473 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010474 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10475 if (buttons == NULL)
10476 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010477 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010478 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010479 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010480 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010481 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010482 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10483 if (typestr == NULL)
10484 error = TRUE;
10485 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010487 switch (TOUPPER_ASC(*typestr))
10488 {
10489 case 'E': type = VIM_ERROR; break;
10490 case 'Q': type = VIM_QUESTION; break;
10491 case 'I': type = VIM_INFO; break;
10492 case 'W': type = VIM_WARNING; break;
10493 case 'G': type = VIM_GENERIC; break;
10494 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495 }
10496 }
10497 }
10498 }
10499
10500 if (buttons == NULL || *buttons == NUL)
10501 buttons = (char_u *)_("&Ok");
10502
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010503 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010504 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010505 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506#endif
10507}
10508
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010509/*
10510 * "copy()" function
10511 */
10512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010513f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010514{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010515 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010516}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010517
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010518#ifdef FEAT_FLOAT
10519/*
10520 * "cos()" function
10521 */
10522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010523f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010524{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010525 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010526
10527 rettv->v_type = VAR_FLOAT;
10528 if (get_float_arg(argvars, &f) == OK)
10529 rettv->vval.v_float = cos(f);
10530 else
10531 rettv->vval.v_float = 0.0;
10532}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010533
10534/*
10535 * "cosh()" function
10536 */
10537 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010538f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010539{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010540 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010541
10542 rettv->v_type = VAR_FLOAT;
10543 if (get_float_arg(argvars, &f) == OK)
10544 rettv->vval.v_float = cosh(f);
10545 else
10546 rettv->vval.v_float = 0.0;
10547}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010548#endif
10549
Bram Moolenaar071d4272004-06-13 20:20:40 +000010550/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010551 * "count()" function
10552 */
10553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010554f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010555{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010556 long n = 0;
10557 int ic = FALSE;
10558
Bram Moolenaare9a41262005-01-15 22:18:47 +000010559 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010560 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010561 listitem_T *li;
10562 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010563 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010564
Bram Moolenaare9a41262005-01-15 22:18:47 +000010565 if ((l = argvars[0].vval.v_list) != NULL)
10566 {
10567 li = l->lv_first;
10568 if (argvars[2].v_type != VAR_UNKNOWN)
10569 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010570 int error = FALSE;
10571
10572 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 if (argvars[3].v_type != VAR_UNKNOWN)
10574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010575 idx = get_tv_number_chk(&argvars[3], &error);
10576 if (!error)
10577 {
10578 li = list_find(l, idx);
10579 if (li == NULL)
10580 EMSGN(_(e_listidx), idx);
10581 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010582 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010583 if (error)
10584 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010585 }
10586
10587 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010588 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010589 ++n;
10590 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010591 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010592 else if (argvars[0].v_type == VAR_DICT)
10593 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010594 int todo;
10595 dict_T *d;
10596 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010597
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010598 if ((d = argvars[0].vval.v_dict) != NULL)
10599 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010600 int error = FALSE;
10601
Bram Moolenaare9a41262005-01-15 22:18:47 +000010602 if (argvars[2].v_type != VAR_UNKNOWN)
10603 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010604 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010605 if (argvars[3].v_type != VAR_UNKNOWN)
10606 EMSG(_(e_invarg));
10607 }
10608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010609 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010610 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010611 {
10612 if (!HASHITEM_EMPTY(hi))
10613 {
10614 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010615 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010616 ++n;
10617 }
10618 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010619 }
10620 }
10621 else
10622 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010623 rettv->vval.v_number = n;
10624}
10625
10626/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010627 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10628 *
10629 * Checks the existence of a cscope connection.
10630 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010632f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633{
10634#ifdef FEAT_CSCOPE
10635 int num = 0;
10636 char_u *dbpath = NULL;
10637 char_u *prepend = NULL;
10638 char_u buf[NUMBUFLEN];
10639
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010640 if (argvars[0].v_type != VAR_UNKNOWN
10641 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010642 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010643 num = (int)get_tv_number(&argvars[0]);
10644 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010645 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010646 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 }
10648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010649 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650#endif
10651}
10652
10653/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010654 * "cursor(lnum, col)" function, or
10655 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010656 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010657 * Moves the cursor to the specified line and column.
10658 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010660 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010661f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010662{
10663 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010664#ifdef FEAT_VIRTUALEDIT
10665 long coladd = 0;
10666#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010667 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010668
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010669 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010670 if (argvars[1].v_type == VAR_UNKNOWN)
10671 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010672 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010673 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010674
Bram Moolenaar493c1782014-05-28 14:34:46 +020010675 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010676 {
10677 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010678 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010679 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010680 line = pos.lnum;
10681 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010682#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010683 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010684#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010685 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010686 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010687 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010688 set_curswant = FALSE;
10689 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010690 }
10691 else
10692 {
10693 line = get_tv_lnum(argvars);
10694 col = get_tv_number_chk(&argvars[1], NULL);
10695#ifdef FEAT_VIRTUALEDIT
10696 if (argvars[2].v_type != VAR_UNKNOWN)
10697 coladd = get_tv_number_chk(&argvars[2], NULL);
10698#endif
10699 }
10700 if (line < 0 || col < 0
10701#ifdef FEAT_VIRTUALEDIT
10702 || coladd < 0
10703#endif
10704 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010705 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010706 if (line > 0)
10707 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010708 if (col > 0)
10709 curwin->w_cursor.col = col - 1;
10710#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010711 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010712#endif
10713
10714 /* Make sure the cursor is in a valid position. */
10715 check_cursor();
10716#ifdef FEAT_MBYTE
10717 /* Correct cursor for multi-byte character. */
10718 if (has_mbyte)
10719 mb_adjust_cursor();
10720#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010721
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010722 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010723 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010724}
10725
10726/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010727 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010728 */
10729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010730f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010731{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010732 int noref = 0;
10733
10734 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010735 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010736 if (noref < 0 || noref > 1)
10737 EMSG(_(e_invarg));
10738 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010739 {
10740 current_copyID += COPYID_INC;
10741 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10742 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010743}
10744
10745/*
10746 * "delete()" function
10747 */
10748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010749f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010750{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010751 char_u nbuf[NUMBUFLEN];
10752 char_u *name;
10753 char_u *flags;
10754
10755 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010756 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010757 return;
10758
10759 name = get_tv_string(&argvars[0]);
10760 if (name == NULL || *name == NUL)
10761 {
10762 EMSG(_(e_invarg));
10763 return;
10764 }
10765
10766 if (argvars[1].v_type != VAR_UNKNOWN)
10767 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010768 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010769 flags = (char_u *)"";
10770
10771 if (*flags == NUL)
10772 /* delete a file */
10773 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10774 else if (STRCMP(flags, "d") == 0)
10775 /* delete an empty directory */
10776 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10777 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010778 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010779 rettv->vval.v_number = delete_recursive(name);
10780 else
10781 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010782}
10783
10784/*
10785 * "did_filetype()" function
10786 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010789{
10790#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010792#endif
10793}
10794
10795/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010796 * "diff_filler()" function
10797 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800{
10801#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010803#endif
10804}
10805
10806/*
10807 * "diff_hlID()" function
10808 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010810f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010811{
10812#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010813 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010814 static linenr_T prev_lnum = 0;
10815 static int changedtick = 0;
10816 static int fnum = 0;
10817 static int change_start = 0;
10818 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010819 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010820 int filler_lines;
10821 int col;
10822
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010823 if (lnum < 0) /* ignore type error in {lnum} arg */
10824 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010825 if (lnum != prev_lnum
10826 || changedtick != curbuf->b_changedtick
10827 || fnum != curbuf->b_fnum)
10828 {
10829 /* New line, buffer, change: need to get the values. */
10830 filler_lines = diff_check(curwin, lnum);
10831 if (filler_lines < 0)
10832 {
10833 if (filler_lines == -1)
10834 {
10835 change_start = MAXCOL;
10836 change_end = -1;
10837 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10838 hlID = HLF_ADD; /* added line */
10839 else
10840 hlID = HLF_CHD; /* changed line */
10841 }
10842 else
10843 hlID = HLF_ADD; /* added line */
10844 }
10845 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010846 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010847 prev_lnum = lnum;
10848 changedtick = curbuf->b_changedtick;
10849 fnum = curbuf->b_fnum;
10850 }
10851
10852 if (hlID == HLF_CHD || hlID == HLF_TXD)
10853 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010854 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010855 if (col >= change_start && col <= change_end)
10856 hlID = HLF_TXD; /* changed text */
10857 else
10858 hlID = HLF_CHD; /* changed line */
10859 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010860 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010861#endif
10862}
10863
10864/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010865 * "disable_char_avail_for_testing({expr})" function
10866 */
10867 static void
10868f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10869{
10870 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10871}
10872
10873/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010874 * "empty({expr})" function
10875 */
10876 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010877f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010878{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010879 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010880
10881 switch (argvars[0].v_type)
10882 {
10883 case VAR_STRING:
10884 case VAR_FUNC:
10885 n = argvars[0].vval.v_string == NULL
10886 || *argvars[0].vval.v_string == NUL;
10887 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010888 case VAR_PARTIAL:
10889 n = FALSE;
10890 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010891 case VAR_NUMBER:
10892 n = argvars[0].vval.v_number == 0;
10893 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010894 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010895#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010896 n = argvars[0].vval.v_float == 0.0;
10897 break;
10898#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010899 case VAR_LIST:
10900 n = argvars[0].vval.v_list == NULL
10901 || argvars[0].vval.v_list->lv_first == NULL;
10902 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010903 case VAR_DICT:
10904 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010905 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010906 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010907 case VAR_SPECIAL:
10908 n = argvars[0].vval.v_number != VVAL_TRUE;
10909 break;
10910
Bram Moolenaar835dc632016-02-07 14:27:38 +010010911 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010912#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010913 n = argvars[0].vval.v_job == NULL
10914 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10915 break;
10916#endif
10917 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010918#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010919 n = argvars[0].vval.v_channel == NULL
10920 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010921 break;
10922#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010923 case VAR_UNKNOWN:
10924 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10925 n = TRUE;
10926 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010927 }
10928
10929 rettv->vval.v_number = n;
10930}
10931
10932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010933 * "escape({string}, {chars})" function
10934 */
10935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010936f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010937{
10938 char_u buf[NUMBUFLEN];
10939
Bram Moolenaar758711c2005-02-02 23:11:38 +000010940 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10941 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010942 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010943}
10944
10945/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010946 * "eval()" function
10947 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010949f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010950{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010951 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010952
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010953 s = get_tv_string_chk(&argvars[0]);
10954 if (s != NULL)
10955 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010956
Bram Moolenaar615b9972015-01-14 17:15:05 +010010957 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010958 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10959 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010960 if (p != NULL && !aborting())
10961 EMSG2(_(e_invexpr2), p);
10962 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010963 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010964 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010965 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010966 else if (*s != NUL)
10967 EMSG(_(e_trailing));
10968}
10969
10970/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010971 * "eventhandler()" function
10972 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010974f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010976 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977}
10978
10979/*
10980 * "executable()" function
10981 */
10982 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010983f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010984{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010985 char_u *name = get_tv_string(&argvars[0]);
10986
10987 /* Check in $PATH and also check directly if there is a directory name. */
10988 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10989 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010990}
10991
10992/*
10993 * "exepath()" function
10994 */
10995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010996f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010997{
10998 char_u *p = NULL;
10999
Bram Moolenaarb5971142015-03-21 17:32:19 +010011000 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020011001 rettv->v_type = VAR_STRING;
11002 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003}
11004
11005/*
11006 * "exists()" function
11007 */
11008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011009f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011010{
11011 char_u *p;
11012 char_u *name;
11013 int n = FALSE;
11014 int len = 0;
11015
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011016 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011017 if (*p == '$') /* environment variable */
11018 {
11019 /* first try "normal" environment variables (fast) */
11020 if (mch_getenv(p + 1) != NULL)
11021 n = TRUE;
11022 else
11023 {
11024 /* try expanding things like $VIM and ${HOME} */
11025 p = expand_env_save(p);
11026 if (p != NULL && *p != '$')
11027 n = TRUE;
11028 vim_free(p);
11029 }
11030 }
11031 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011032 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011033 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011034 if (*skipwhite(p) != NUL)
11035 n = FALSE; /* trailing garbage */
11036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011037 else if (*p == '*') /* internal or user defined function */
11038 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011039 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011040 }
11041 else if (*p == ':')
11042 {
11043 n = cmd_exists(p + 1);
11044 }
11045 else if (*p == '#')
11046 {
11047#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011048 if (p[1] == '#')
11049 n = autocmd_supported(p + 2);
11050 else
11051 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052#endif
11053 }
11054 else /* internal variable */
11055 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011056 char_u *tofree;
11057 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011058
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011059 /* get_name_len() takes care of expanding curly braces */
11060 name = p;
11061 len = get_name_len(&p, &tofree, TRUE, FALSE);
11062 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011063 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011064 if (tofree != NULL)
11065 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011066 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011067 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011069 /* handle d.key, l[idx], f(expr) */
11070 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11071 if (n)
11072 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073 }
11074 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011075 if (*p != NUL)
11076 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011077
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011078 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011079 }
11080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011081 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011082}
11083
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011084#ifdef FEAT_FLOAT
11085/*
11086 * "exp()" function
11087 */
11088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011089f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011090{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011091 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011092
11093 rettv->v_type = VAR_FLOAT;
11094 if (get_float_arg(argvars, &f) == OK)
11095 rettv->vval.v_float = exp(f);
11096 else
11097 rettv->vval.v_float = 0.0;
11098}
11099#endif
11100
Bram Moolenaar071d4272004-06-13 20:20:40 +000011101/*
11102 * "expand()" function
11103 */
11104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011105f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011106{
11107 char_u *s;
11108 int len;
11109 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011110 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011111 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011112 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011113 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011114
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011115 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011116 if (argvars[1].v_type != VAR_UNKNOWN
11117 && argvars[2].v_type != VAR_UNKNOWN
11118 && get_tv_number_chk(&argvars[2], &error)
11119 && !error)
11120 {
11121 rettv->v_type = VAR_LIST;
11122 rettv->vval.v_list = NULL;
11123 }
11124
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011125 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011126 if (*s == '%' || *s == '#' || *s == '<')
11127 {
11128 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011129 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011130 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011131 if (rettv->v_type == VAR_LIST)
11132 {
11133 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11134 list_append_string(rettv->vval.v_list, result, -1);
11135 else
11136 vim_free(result);
11137 }
11138 else
11139 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011140 }
11141 else
11142 {
11143 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011144 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011145 if (argvars[1].v_type != VAR_UNKNOWN
11146 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011147 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011148 if (!error)
11149 {
11150 ExpandInit(&xpc);
11151 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011152 if (p_wic)
11153 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011154 if (rettv->v_type == VAR_STRING)
11155 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11156 options, WILD_ALL);
11157 else if (rettv_list_alloc(rettv) != FAIL)
11158 {
11159 int i;
11160
11161 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11162 for (i = 0; i < xpc.xp_numfiles; i++)
11163 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11164 ExpandCleanup(&xpc);
11165 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011166 }
11167 else
11168 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011169 }
11170}
11171
11172/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011173 * Go over all entries in "d2" and add them to "d1".
11174 * When "action" is "error" then a duplicate key is an error.
11175 * When "action" is "force" then a duplicate key is overwritten.
11176 * Otherwise duplicate keys are ignored ("action" is "keep").
11177 */
11178 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011179dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011180{
11181 dictitem_T *di1;
11182 hashitem_T *hi2;
11183 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011184 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011185
11186 todo = (int)d2->dv_hashtab.ht_used;
11187 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11188 {
11189 if (!HASHITEM_EMPTY(hi2))
11190 {
11191 --todo;
11192 di1 = dict_find(d1, hi2->hi_key, -1);
11193 if (d1->dv_scope != 0)
11194 {
11195 /* Disallow replacing a builtin function in l: and g:.
11196 * Check the key to be valid when adding to any
11197 * scope. */
11198 if (d1->dv_scope == VAR_DEF_SCOPE
11199 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11200 && var_check_func_name(hi2->hi_key,
11201 di1 == NULL))
11202 break;
11203 if (!valid_varname(hi2->hi_key))
11204 break;
11205 }
11206 if (di1 == NULL)
11207 {
11208 di1 = dictitem_copy(HI2DI(hi2));
11209 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11210 dictitem_free(di1);
11211 }
11212 else if (*action == 'e')
11213 {
11214 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11215 break;
11216 }
11217 else if (*action == 'f' && HI2DI(hi2) != di1)
11218 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011219 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11220 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011221 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011222 clear_tv(&di1->di_tv);
11223 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11224 }
11225 }
11226 }
11227}
11228
11229/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011230 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011232 */
11233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011234f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011235{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011236 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011237
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011239 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011240 list_T *l1, *l2;
11241 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011242 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011244
Bram Moolenaare9a41262005-01-15 22:18:47 +000011245 l1 = argvars[0].vval.v_list;
11246 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011247 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011248 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011249 {
11250 if (argvars[2].v_type != VAR_UNKNOWN)
11251 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011252 before = get_tv_number_chk(&argvars[2], &error);
11253 if (error)
11254 return; /* type error; errmsg already given */
11255
Bram Moolenaar758711c2005-02-02 23:11:38 +000011256 if (before == l1->lv_len)
11257 item = NULL;
11258 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011259 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011260 item = list_find(l1, before);
11261 if (item == NULL)
11262 {
11263 EMSGN(_(e_listidx), before);
11264 return;
11265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011266 }
11267 }
11268 else
11269 item = NULL;
11270 list_extend(l1, l2, item);
11271
Bram Moolenaare9a41262005-01-15 22:18:47 +000011272 copy_tv(&argvars[0], rettv);
11273 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011274 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011275 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11276 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011277 dict_T *d1, *d2;
11278 char_u *action;
11279 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011280
11281 d1 = argvars[0].vval.v_dict;
11282 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011283 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011284 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011285 {
11286 /* Check the third argument. */
11287 if (argvars[2].v_type != VAR_UNKNOWN)
11288 {
11289 static char *(av[]) = {"keep", "force", "error"};
11290
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011291 action = get_tv_string_chk(&argvars[2]);
11292 if (action == NULL)
11293 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011294 for (i = 0; i < 3; ++i)
11295 if (STRCMP(action, av[i]) == 0)
11296 break;
11297 if (i == 3)
11298 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011299 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011300 return;
11301 }
11302 }
11303 else
11304 action = (char_u *)"force";
11305
Bram Moolenaara9922d62013-05-30 13:01:18 +020011306 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011307
Bram Moolenaare9a41262005-01-15 22:18:47 +000011308 copy_tv(&argvars[0], rettv);
11309 }
11310 }
11311 else
11312 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011313}
11314
11315/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011316 * "feedkeys()" function
11317 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011319f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011320{
11321 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011322 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011323 char_u *keys, *flags;
11324 char_u nbuf[NUMBUFLEN];
11325 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011326 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011327 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011328
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011329 /* This is not allowed in the sandbox. If the commands would still be
11330 * executed in the sandbox it would be OK, but it probably happens later,
11331 * when "sandbox" is no longer set. */
11332 if (check_secure())
11333 return;
11334
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011335 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011336
11337 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011338 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011339 flags = get_tv_string_buf(&argvars[1], nbuf);
11340 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011341 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011342 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011343 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011344 case 'n': remap = FALSE; break;
11345 case 'm': remap = TRUE; break;
11346 case 't': typed = TRUE; break;
11347 case 'i': insert = TRUE; break;
11348 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011349 }
11350 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011351 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011352
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011353 if (*keys != NUL || execute)
11354 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011355 /* Need to escape K_SPECIAL and CSI before putting the string in the
11356 * typeahead buffer. */
11357 keys_esc = vim_strsave_escape_csi(keys);
11358 if (keys_esc != NULL)
11359 {
11360 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011361 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011362 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011363 if (vgetc_busy)
11364 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011365 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011366 {
11367 int save_msg_scroll = msg_scroll;
11368
11369 /* Avoid a 1 second delay when the keys start Insert mode. */
11370 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011371 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011372 msg_scroll |= save_msg_scroll;
11373 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011374 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011375 }
11376}
11377
11378/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011379 * "filereadable()" function
11380 */
11381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011382f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011384 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 char_u *p;
11386 int n;
11387
Bram Moolenaarc236c162008-07-13 17:41:49 +000011388#ifndef O_NONBLOCK
11389# define O_NONBLOCK 0
11390#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011391 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011392 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11393 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011394 {
11395 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011396 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 }
11398 else
11399 n = FALSE;
11400
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011401 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011402}
11403
11404/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011405 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011406 * rights to write into.
11407 */
11408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011409f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011410{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011411 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011412}
11413
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011414 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011415findfilendir(
11416 typval_T *argvars UNUSED,
11417 typval_T *rettv,
11418 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011419{
11420#ifdef FEAT_SEARCHPATH
11421 char_u *fname;
11422 char_u *fresult = NULL;
11423 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11424 char_u *p;
11425 char_u pathbuf[NUMBUFLEN];
11426 int count = 1;
11427 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011428 int error = FALSE;
11429#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011430
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011431 rettv->vval.v_string = NULL;
11432 rettv->v_type = VAR_STRING;
11433
11434#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011435 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011436
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011437 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011438 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011439 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11440 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011441 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011442 else
11443 {
11444 if (*p != NUL)
11445 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011446
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011448 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011449 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011450 }
11451
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011452 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11453 error = TRUE;
11454
11455 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011456 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 do
11458 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011459 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011460 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011461 fresult = find_file_in_path_option(first ? fname : NULL,
11462 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011463 0, first, path,
11464 find_what,
11465 curbuf->b_ffname,
11466 find_what == FINDFILE_DIR
11467 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011468 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011469
11470 if (fresult != NULL && rettv->v_type == VAR_LIST)
11471 list_append_string(rettv->vval.v_list, fresult, -1);
11472
11473 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011474 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011475
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011476 if (rettv->v_type == VAR_STRING)
11477 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011478#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011479}
11480
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011481static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11482static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011483
11484/*
11485 * Implementation of map() and filter().
11486 */
11487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011488filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011489{
11490 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011492 listitem_T *li, *nli;
11493 list_T *l = NULL;
11494 dictitem_T *di;
11495 hashtab_T *ht;
11496 hashitem_T *hi;
11497 dict_T *d = NULL;
11498 typval_T save_val;
11499 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011500 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011501 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011502 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011503 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011504 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011505 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011506 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011507
Bram Moolenaare9a41262005-01-15 22:18:47 +000011508 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011509 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011510 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011511 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 return;
11513 }
11514 else if (argvars[0].v_type == VAR_DICT)
11515 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011516 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011517 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011518 return;
11519 }
11520 else
11521 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011522 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011523 return;
11524 }
11525
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011526 expr = get_tv_string_buf_chk(&argvars[1], buf);
11527 /* On type errors, the preceding call has already displayed an error
11528 * message. Avoid a misleading error message for an empty string that
11529 * was not passed as argument. */
11530 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 prepare_vimvar(VV_VAL, &save_val);
11533 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011534
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011535 /* We reset "did_emsg" to be able to detect whether an error
11536 * occurred during evaluation of the expression. */
11537 save_did_emsg = did_emsg;
11538 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011539
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011540 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011542 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 vimvars[VV_KEY].vv_type = VAR_STRING;
11544
11545 ht = &d->dv_hashtab;
11546 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011547 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011549 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 if (!HASHITEM_EMPTY(hi))
11551 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011552 int r;
11553
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011554 --todo;
11555 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011556 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011557 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11558 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 break;
11560 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011561 r = filter_map_one(&di->di_tv, expr, map, &rem);
11562 clear_tv(&vimvars[VV_KEY].vv_tv);
11563 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 break;
11565 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011566 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011567 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11568 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011569 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011570 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011571 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011572 }
11573 }
11574 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 }
11576 else
11577 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011578 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11579
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011580 for (li = l->lv_first; li != NULL; li = nli)
11581 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011582 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011583 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011584 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011585 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011586 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011587 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011588 break;
11589 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011590 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011591 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011592 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011593 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011594
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011595 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011596 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011597
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011598 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011599 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011600
11601 copy_tv(&argvars[0], rettv);
11602}
11603
11604 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011605filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606{
Bram Moolenaar33570922005-01-25 22:26:29 +000011607 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011608 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011609 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011610
Bram Moolenaar33570922005-01-25 22:26:29 +000011611 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011612 s = expr;
11613 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011614 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 if (*s != NUL) /* check for trailing chars after expr */
11616 {
11617 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011618 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011619 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011620 }
11621 if (map)
11622 {
11623 /* map(): replace the list item value */
11624 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011625 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011626 *tv = rettv;
11627 }
11628 else
11629 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011630 int error = FALSE;
11631
Bram Moolenaare9a41262005-01-15 22:18:47 +000011632 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011633 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011634 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011635 /* On type error, nothing has been removed; return FAIL to stop the
11636 * loop. The error message was given by get_tv_number_chk(). */
11637 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011638 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011639 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011640 retval = OK;
11641theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011642 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011643 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011644}
11645
11646/*
11647 * "filter()" function
11648 */
11649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011651{
11652 filter_map(argvars, rettv, FALSE);
11653}
11654
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011655/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011656 * "finddir({fname}[, {path}[, {count}]])" function
11657 */
11658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011659f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011661 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
11664/*
11665 * "findfile({fname}[, {path}[, {count}]])" function
11666 */
11667 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011668f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011669{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011670 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011671}
11672
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011673#ifdef FEAT_FLOAT
11674/*
11675 * "float2nr({float})" function
11676 */
11677 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011678f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011679{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011680 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011681
11682 if (get_float_arg(argvars, &f) == OK)
11683 {
11684 if (f < -0x7fffffff)
11685 rettv->vval.v_number = -0x7fffffff;
11686 else if (f > 0x7fffffff)
11687 rettv->vval.v_number = 0x7fffffff;
11688 else
11689 rettv->vval.v_number = (varnumber_T)f;
11690 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011691}
11692
11693/*
11694 * "floor({float})" function
11695 */
11696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011697f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011698{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011699 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011700
11701 rettv->v_type = VAR_FLOAT;
11702 if (get_float_arg(argvars, &f) == OK)
11703 rettv->vval.v_float = floor(f);
11704 else
11705 rettv->vval.v_float = 0.0;
11706}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011707
11708/*
11709 * "fmod()" function
11710 */
11711 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011712f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011713{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011714 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011715
11716 rettv->v_type = VAR_FLOAT;
11717 if (get_float_arg(argvars, &fx) == OK
11718 && get_float_arg(&argvars[1], &fy) == OK)
11719 rettv->vval.v_float = fmod(fx, fy);
11720 else
11721 rettv->vval.v_float = 0.0;
11722}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011723#endif
11724
Bram Moolenaar0d660222005-01-07 21:51:51 +000011725/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011726 * "fnameescape({string})" function
11727 */
11728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011729f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011730{
11731 rettv->vval.v_string = vim_strsave_fnameescape(
11732 get_tv_string(&argvars[0]), FALSE);
11733 rettv->v_type = VAR_STRING;
11734}
11735
11736/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011737 * "fnamemodify({fname}, {mods})" function
11738 */
11739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011740f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011741{
11742 char_u *fname;
11743 char_u *mods;
11744 int usedlen = 0;
11745 int len;
11746 char_u *fbuf = NULL;
11747 char_u buf[NUMBUFLEN];
11748
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011749 fname = get_tv_string_chk(&argvars[0]);
11750 mods = get_tv_string_buf_chk(&argvars[1], buf);
11751 if (fname == NULL || mods == NULL)
11752 fname = NULL;
11753 else
11754 {
11755 len = (int)STRLEN(fname);
11756 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011758
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011759 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011760 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011761 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011762 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011763 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011764 vim_free(fbuf);
11765}
11766
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011767static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768
11769/*
11770 * "foldclosed()" function
11771 */
11772 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011773foldclosed_both(
11774 typval_T *argvars UNUSED,
11775 typval_T *rettv,
11776 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011777{
11778#ifdef FEAT_FOLDING
11779 linenr_T lnum;
11780 linenr_T first, last;
11781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011782 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011783 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11784 {
11785 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11786 {
11787 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011788 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011789 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011790 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011791 return;
11792 }
11793 }
11794#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011795 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011796}
11797
11798/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011799 * "foldclosed()" function
11800 */
11801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011802f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011803{
11804 foldclosed_both(argvars, rettv, FALSE);
11805}
11806
11807/*
11808 * "foldclosedend()" function
11809 */
11810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011811f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011812{
11813 foldclosed_both(argvars, rettv, TRUE);
11814}
11815
11816/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 * "foldlevel()" function
11818 */
11819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011820f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011821{
11822#ifdef FEAT_FOLDING
11823 linenr_T lnum;
11824
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011825 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011826 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011827 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011829}
11830
11831/*
11832 * "foldtext()" function
11833 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011835f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836{
11837#ifdef FEAT_FOLDING
11838 linenr_T lnum;
11839 char_u *s;
11840 char_u *r;
11841 int len;
11842 char *txt;
11843#endif
11844
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011845 rettv->v_type = VAR_STRING;
11846 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011848 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11849 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11850 <= curbuf->b_ml.ml_line_count
11851 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011852 {
11853 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011854 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11855 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 {
11857 if (!linewhite(lnum))
11858 break;
11859 ++lnum;
11860 }
11861
11862 /* Find interesting text in this line. */
11863 s = skipwhite(ml_get(lnum));
11864 /* skip C comment-start */
11865 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011868 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011870 {
11871 s = skipwhite(ml_get(lnum + 1));
11872 if (*s == '*')
11873 s = skipwhite(s + 1);
11874 }
11875 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011876 txt = _("+-%s%3ld lines: ");
11877 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011878 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011879 + 20 /* for %3ld */
11880 + STRLEN(s))); /* concatenated */
11881 if (r != NULL)
11882 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011883 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11884 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11885 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011886 len = (int)STRLEN(r);
11887 STRCAT(r, s);
11888 /* remove 'foldmarker' and 'commentstring' */
11889 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011890 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011891 }
11892 }
11893#endif
11894}
11895
11896/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011897 * "foldtextresult(lnum)" function
11898 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011899 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011900f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011901{
11902#ifdef FEAT_FOLDING
11903 linenr_T lnum;
11904 char_u *text;
11905 char_u buf[51];
11906 foldinfo_T foldinfo;
11907 int fold_count;
11908#endif
11909
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011910 rettv->v_type = VAR_STRING;
11911 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011912#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011914 /* treat illegal types and illegal string values for {lnum} the same */
11915 if (lnum < 0)
11916 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011917 fold_count = foldedCount(curwin, lnum, &foldinfo);
11918 if (fold_count > 0)
11919 {
11920 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11921 &foldinfo, buf);
11922 if (text == buf)
11923 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011924 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011925 }
11926#endif
11927}
11928
11929/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011930 * "foreground()" function
11931 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011933f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011934{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011935#ifdef FEAT_GUI
11936 if (gui.in_use)
11937 gui_mch_set_foreground();
11938#else
11939# ifdef WIN32
11940 win32_set_foreground();
11941# endif
11942#endif
11943}
11944
11945/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011946 * "function()" function
11947 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011948 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011949f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011950{
11951 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011952 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011953 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011954 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011955
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011956 if (argvars[0].v_type == VAR_FUNC)
11957 {
11958 /* function(MyFunc, [arg], dict) */
11959 s = argvars[0].vval.v_string;
11960 }
11961 else if (argvars[0].v_type == VAR_PARTIAL
11962 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011963 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011964 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011965 arg_pt = argvars[0].vval.v_partial;
11966 s = arg_pt->pt_name;
11967 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011968 else
11969 {
11970 /* function('MyFunc', [arg], dict) */
11971 s = get_tv_string(&argvars[0]);
11972 use_string = TRUE;
11973 }
11974
11975 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011976 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011977 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011978 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11979 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011980 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011981 else
11982 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011983 int dict_idx = 0;
11984 int arg_idx = 0;
11985 list_T *list = NULL;
11986
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011987 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011988 {
11989 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011990 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011991
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011992 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11993 * also be called from another script. Using trans_function_name()
11994 * would also work, but some plugins depend on the name being
11995 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011996 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011997 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11998 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011999 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012000 STRCPY(name, sid_buf);
12001 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020012002 }
12003 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020012004 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012005 name = vim_strsave(s);
12006
12007 if (argvars[1].v_type != VAR_UNKNOWN)
12008 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012009 if (argvars[2].v_type != VAR_UNKNOWN)
12010 {
12011 /* function(name, [args], dict) */
12012 arg_idx = 1;
12013 dict_idx = 2;
12014 }
12015 else if (argvars[1].v_type == VAR_DICT)
12016 /* function(name, dict) */
12017 dict_idx = 1;
12018 else
12019 /* function(name, [args]) */
12020 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012021 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012022 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012023 if (argvars[dict_idx].v_type != VAR_DICT)
12024 {
12025 EMSG(_("E922: expected a dict"));
12026 vim_free(name);
12027 return;
12028 }
12029 if (argvars[dict_idx].vval.v_dict == NULL)
12030 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012031 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012032 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012033 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012034 if (argvars[arg_idx].v_type != VAR_LIST)
12035 {
12036 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12037 vim_free(name);
12038 return;
12039 }
12040 list = argvars[arg_idx].vval.v_list;
12041 if (list == NULL || list->lv_len == 0)
12042 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012043 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012044 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012045 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012046 {
12047 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012048
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012049 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012050 if (pt == NULL)
12051 vim_free(name);
12052 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012053 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012054 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012055 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012056 listitem_T *li;
12057 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012058 int arg_len = 0;
12059 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012060
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012061 if (arg_pt != NULL)
12062 arg_len = arg_pt->pt_argc;
12063 if (list != NULL)
12064 lv_len = list->lv_len;
12065 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012066 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012067 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012068 if (pt->pt_argv == NULL)
12069 {
12070 vim_free(pt);
12071 vim_free(name);
12072 return;
12073 }
12074 else
12075 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012076 for (i = 0; i < arg_len; i++)
12077 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12078 if (lv_len > 0)
12079 for (li = list->lv_first; li != NULL;
12080 li = li->li_next)
12081 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012082 }
12083 }
12084
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012085 /* For "function(dict.func, [], dict)" and "func" is a partial
12086 * use "dict". That is backwards compatible. */
12087 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012088 {
12089 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12090 ++pt->pt_dict->dv_refcount;
12091 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012092 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012093 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012094 pt->pt_dict = arg_pt->pt_dict;
12095 if (pt->pt_dict != NULL)
12096 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012097 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012098
12099 pt->pt_refcount = 1;
12100 pt->pt_name = name;
12101 func_ref(pt->pt_name);
12102 }
12103 rettv->v_type = VAR_PARTIAL;
12104 rettv->vval.v_partial = pt;
12105 }
12106 else
12107 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012108 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012109 rettv->v_type = VAR_FUNC;
12110 rettv->vval.v_string = name;
12111 func_ref(name);
12112 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012113 }
12114}
12115
12116/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012117 * "garbagecollect()" function
12118 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012120f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012121{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012122 /* This is postponed until we are back at the toplevel, because we may be
12123 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12124 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012125
12126 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12127 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012128}
12129
12130/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012131 * "get()" function
12132 */
12133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012134f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012135{
Bram Moolenaar33570922005-01-25 22:26:29 +000012136 listitem_T *li;
12137 list_T *l;
12138 dictitem_T *di;
12139 dict_T *d;
12140 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141
Bram Moolenaare9a41262005-01-15 22:18:47 +000012142 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012144 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012145 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012146 int error = FALSE;
12147
12148 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12149 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012150 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012151 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012153 else if (argvars[0].v_type == VAR_DICT)
12154 {
12155 if ((d = argvars[0].vval.v_dict) != NULL)
12156 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012157 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012158 if (di != NULL)
12159 tv = &di->di_tv;
12160 }
12161 }
12162 else
12163 EMSG2(_(e_listdictarg), "get()");
12164
12165 if (tv == NULL)
12166 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012167 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012168 copy_tv(&argvars[2], rettv);
12169 }
12170 else
12171 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012172}
12173
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012174static 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 +000012175
12176/*
12177 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012178 * Return a range (from start to end) of lines in rettv from the specified
12179 * buffer.
12180 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012181 */
12182 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012183get_buffer_lines(
12184 buf_T *buf,
12185 linenr_T start,
12186 linenr_T end,
12187 int retlist,
12188 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012189{
12190 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012191
Bram Moolenaar959a1432013-12-14 12:17:38 +010012192 rettv->v_type = VAR_STRING;
12193 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012194 if (retlist && rettv_list_alloc(rettv) == FAIL)
12195 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012196
12197 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12198 return;
12199
12200 if (!retlist)
12201 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012202 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12203 p = ml_get_buf(buf, start, FALSE);
12204 else
12205 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012206 rettv->vval.v_string = vim_strsave(p);
12207 }
12208 else
12209 {
12210 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012211 return;
12212
12213 if (start < 1)
12214 start = 1;
12215 if (end > buf->b_ml.ml_line_count)
12216 end = buf->b_ml.ml_line_count;
12217 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012218 if (list_append_string(rettv->vval.v_list,
12219 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012220 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012221 }
12222}
12223
12224/*
12225 * "getbufline()" function
12226 */
12227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012228f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012229{
12230 linenr_T lnum;
12231 linenr_T end;
12232 buf_T *buf;
12233
12234 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12235 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012236 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012237 --emsg_off;
12238
Bram Moolenaar661b1822005-07-28 22:36:45 +000012239 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012240 if (argvars[2].v_type == VAR_UNKNOWN)
12241 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012242 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012243 end = get_tv_lnum_buf(&argvars[2], buf);
12244
Bram Moolenaar342337a2005-07-21 21:11:17 +000012245 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012246}
12247
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248/*
12249 * "getbufvar()" function
12250 */
12251 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012252f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012253{
12254 buf_T *buf;
12255 buf_T *save_curbuf;
12256 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012257 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012258 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012259
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012260 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12261 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012262 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012263 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012264
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012265 rettv->v_type = VAR_STRING;
12266 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012267
12268 if (buf != NULL && varname != NULL)
12269 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012270 /* set curbuf to be our buf, temporarily */
12271 save_curbuf = curbuf;
12272 curbuf = buf;
12273
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012275 {
12276 if (get_option_tv(&varname, rettv, TRUE) == OK)
12277 done = TRUE;
12278 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012279 else if (STRCMP(varname, "changedtick") == 0)
12280 {
12281 rettv->v_type = VAR_NUMBER;
12282 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285 else
12286 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012287 /* Look up the variable. */
12288 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12289 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12290 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012291 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012292 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012293 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012294 done = TRUE;
12295 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012296 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012297
12298 /* restore previous notion of curbuf */
12299 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012300 }
12301
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012302 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12303 /* use the default value */
12304 copy_tv(&argvars[2], rettv);
12305
Bram Moolenaar0d660222005-01-07 21:51:51 +000012306 --emsg_off;
12307}
12308
12309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 * "getchar()" function
12311 */
12312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012313f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012314{
12315 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012316 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012317
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012318 /* Position the cursor. Needed after a message that ends in a space. */
12319 windgoto(msg_row, msg_col);
12320
Bram Moolenaar071d4272004-06-13 20:20:40 +000012321 ++no_mapping;
12322 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012323 for (;;)
12324 {
12325 if (argvars[0].v_type == VAR_UNKNOWN)
12326 /* getchar(): blocking wait. */
12327 n = safe_vgetc();
12328 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12329 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012330 n = vpeekc_any();
12331 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012332 /* illegal argument or getchar(0) and no char avail: return zero */
12333 n = 0;
12334 else
12335 /* getchar(0) and char avail: return char */
12336 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012337
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012338 if (n == K_IGNORE)
12339 continue;
12340 break;
12341 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012342 --no_mapping;
12343 --allow_keys;
12344
Bram Moolenaar219b8702006-11-01 14:32:36 +000012345 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12346 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12347 vimvars[VV_MOUSE_COL].vv_nr = 0;
12348
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012349 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012350 if (IS_SPECIAL(n) || mod_mask != 0)
12351 {
12352 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12353 int i = 0;
12354
12355 /* Turn a special key into three bytes, plus modifier. */
12356 if (mod_mask != 0)
12357 {
12358 temp[i++] = K_SPECIAL;
12359 temp[i++] = KS_MODIFIER;
12360 temp[i++] = mod_mask;
12361 }
12362 if (IS_SPECIAL(n))
12363 {
12364 temp[i++] = K_SPECIAL;
12365 temp[i++] = K_SECOND(n);
12366 temp[i++] = K_THIRD(n);
12367 }
12368#ifdef FEAT_MBYTE
12369 else if (has_mbyte)
12370 i += (*mb_char2bytes)(n, temp + i);
12371#endif
12372 else
12373 temp[i++] = n;
12374 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012375 rettv->v_type = VAR_STRING;
12376 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012377
12378#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012379 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012380 {
12381 int row = mouse_row;
12382 int col = mouse_col;
12383 win_T *win;
12384 linenr_T lnum;
12385# ifdef FEAT_WINDOWS
12386 win_T *wp;
12387# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012388 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012389
12390 if (row >= 0 && col >= 0)
12391 {
12392 /* Find the window at the mouse coordinates and compute the
12393 * text position. */
12394 win = mouse_find_win(&row, &col);
12395 (void)mouse_comp_pos(win, &row, &col, &lnum);
12396# ifdef FEAT_WINDOWS
12397 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012398 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012399# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012400 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012401 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12402 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12403 }
12404 }
12405#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012406 }
12407}
12408
12409/*
12410 * "getcharmod()" function
12411 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012413f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012414{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012415 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012416}
12417
12418/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012419 * "getcharsearch()" function
12420 */
12421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012422f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012423{
12424 if (rettv_dict_alloc(rettv) != FAIL)
12425 {
12426 dict_T *dict = rettv->vval.v_dict;
12427
12428 dict_add_nr_str(dict, "char", 0L, last_csearch());
12429 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12430 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12431 }
12432}
12433
12434/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012435 * "getcmdline()" function
12436 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012438f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012439{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012440 rettv->v_type = VAR_STRING;
12441 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
12445 * "getcmdpos()" function
12446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012448f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012449{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012450 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012451}
12452
12453/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012454 * "getcmdtype()" function
12455 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012457f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012458{
12459 rettv->v_type = VAR_STRING;
12460 rettv->vval.v_string = alloc(2);
12461 if (rettv->vval.v_string != NULL)
12462 {
12463 rettv->vval.v_string[0] = get_cmdline_type();
12464 rettv->vval.v_string[1] = NUL;
12465 }
12466}
12467
12468/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012469 * "getcmdwintype()" function
12470 */
12471 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012472f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012473{
12474 rettv->v_type = VAR_STRING;
12475 rettv->vval.v_string = NULL;
12476#ifdef FEAT_CMDWIN
12477 rettv->vval.v_string = alloc(2);
12478 if (rettv->vval.v_string != NULL)
12479 {
12480 rettv->vval.v_string[0] = cmdwin_type;
12481 rettv->vval.v_string[1] = NUL;
12482 }
12483#endif
12484}
12485
12486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012487 * "getcwd()" function
12488 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012490f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012492 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012493 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012494
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012495 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012496 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012497
12498 wp = find_tabwin(&argvars[0], &argvars[1]);
12499 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012500 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012501 if (wp->w_localdir != NULL)
12502 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12503 else if(globaldir != NULL)
12504 rettv->vval.v_string = vim_strsave(globaldir);
12505 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012506 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012507 cwd = alloc(MAXPATHL);
12508 if (cwd != NULL)
12509 {
12510 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12511 rettv->vval.v_string = vim_strsave(cwd);
12512 vim_free(cwd);
12513 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012514 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012515#ifdef BACKSLASH_IN_FILENAME
12516 if (rettv->vval.v_string != NULL)
12517 slash_adjust(rettv->vval.v_string);
12518#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519 }
12520}
12521
12522/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012523 * "getfontname()" function
12524 */
12525 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012526f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012527{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012528 rettv->v_type = VAR_STRING;
12529 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012530#ifdef FEAT_GUI
12531 if (gui.in_use)
12532 {
12533 GuiFont font;
12534 char_u *name = NULL;
12535
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012536 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012537 {
12538 /* Get the "Normal" font. Either the name saved by
12539 * hl_set_font_name() or from the font ID. */
12540 font = gui.norm_font;
12541 name = hl_get_font_name();
12542 }
12543 else
12544 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012545 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012546 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12547 return;
12548 font = gui_mch_get_font(name, FALSE);
12549 if (font == NOFONT)
12550 return; /* Invalid font name, return empty string. */
12551 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012552 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012553 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012554 gui_mch_free_font(font);
12555 }
12556#endif
12557}
12558
12559/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012560 * "getfperm({fname})" function
12561 */
12562 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012563f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012564{
12565 char_u *fname;
12566 struct stat st;
12567 char_u *perm = NULL;
12568 char_u flags[] = "rwx";
12569 int i;
12570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012571 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012573 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012574 if (mch_stat((char *)fname, &st) >= 0)
12575 {
12576 perm = vim_strsave((char_u *)"---------");
12577 if (perm != NULL)
12578 {
12579 for (i = 0; i < 9; i++)
12580 {
12581 if (st.st_mode & (1 << (8 - i)))
12582 perm[i] = flags[i % 3];
12583 }
12584 }
12585 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012586 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012587}
12588
12589/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590 * "getfsize({fname})" function
12591 */
12592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012593f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012594{
12595 char_u *fname;
12596 struct stat st;
12597
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012598 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012599
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012600 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012601
12602 if (mch_stat((char *)fname, &st) >= 0)
12603 {
12604 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012607 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012608 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012609
12610 /* non-perfect check for overflow */
12611 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12612 rettv->vval.v_number = -2;
12613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012614 }
12615 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012616 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012617}
12618
12619/*
12620 * "getftime({fname})" function
12621 */
12622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012623f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012624{
12625 char_u *fname;
12626 struct stat st;
12627
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012628 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012629
12630 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012631 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012632 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012633 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012634}
12635
12636/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012637 * "getftype({fname})" function
12638 */
12639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012640f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012641{
12642 char_u *fname;
12643 struct stat st;
12644 char_u *type = NULL;
12645 char *t;
12646
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012647 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012648
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012649 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012650 if (mch_lstat((char *)fname, &st) >= 0)
12651 {
12652#ifdef S_ISREG
12653 if (S_ISREG(st.st_mode))
12654 t = "file";
12655 else if (S_ISDIR(st.st_mode))
12656 t = "dir";
12657# ifdef S_ISLNK
12658 else if (S_ISLNK(st.st_mode))
12659 t = "link";
12660# endif
12661# ifdef S_ISBLK
12662 else if (S_ISBLK(st.st_mode))
12663 t = "bdev";
12664# endif
12665# ifdef S_ISCHR
12666 else if (S_ISCHR(st.st_mode))
12667 t = "cdev";
12668# endif
12669# ifdef S_ISFIFO
12670 else if (S_ISFIFO(st.st_mode))
12671 t = "fifo";
12672# endif
12673# ifdef S_ISSOCK
12674 else if (S_ISSOCK(st.st_mode))
12675 t = "fifo";
12676# endif
12677 else
12678 t = "other";
12679#else
12680# ifdef S_IFMT
12681 switch (st.st_mode & S_IFMT)
12682 {
12683 case S_IFREG: t = "file"; break;
12684 case S_IFDIR: t = "dir"; break;
12685# ifdef S_IFLNK
12686 case S_IFLNK: t = "link"; break;
12687# endif
12688# ifdef S_IFBLK
12689 case S_IFBLK: t = "bdev"; break;
12690# endif
12691# ifdef S_IFCHR
12692 case S_IFCHR: t = "cdev"; break;
12693# endif
12694# ifdef S_IFIFO
12695 case S_IFIFO: t = "fifo"; break;
12696# endif
12697# ifdef S_IFSOCK
12698 case S_IFSOCK: t = "socket"; break;
12699# endif
12700 default: t = "other";
12701 }
12702# else
12703 if (mch_isdir(fname))
12704 t = "dir";
12705 else
12706 t = "file";
12707# endif
12708#endif
12709 type = vim_strsave((char_u *)t);
12710 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012711 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012712}
12713
12714/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012715 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012716 */
12717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012718f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719{
12720 linenr_T lnum;
12721 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012722 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012723
12724 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012725 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012726 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012727 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012728 retlist = FALSE;
12729 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012730 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012731 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012732 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012733 retlist = TRUE;
12734 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012735
Bram Moolenaar342337a2005-07-21 21:11:17 +000012736 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012737}
12738
12739/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012740 * "getmatches()" function
12741 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012743f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012744{
12745#ifdef FEAT_SEARCH_EXTRA
12746 dict_T *dict;
12747 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012748 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012749
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012750 if (rettv_list_alloc(rettv) == OK)
12751 {
12752 while (cur != NULL)
12753 {
12754 dict = dict_alloc();
12755 if (dict == NULL)
12756 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012757 if (cur->match.regprog == NULL)
12758 {
12759 /* match added with matchaddpos() */
12760 for (i = 0; i < MAXPOSMATCH; ++i)
12761 {
12762 llpos_T *llpos;
12763 char buf[6];
12764 list_T *l;
12765
12766 llpos = &cur->pos.pos[i];
12767 if (llpos->lnum == 0)
12768 break;
12769 l = list_alloc();
12770 if (l == NULL)
12771 break;
12772 list_append_number(l, (varnumber_T)llpos->lnum);
12773 if (llpos->col > 0)
12774 {
12775 list_append_number(l, (varnumber_T)llpos->col);
12776 list_append_number(l, (varnumber_T)llpos->len);
12777 }
12778 sprintf(buf, "pos%d", i + 1);
12779 dict_add_list(dict, buf, l);
12780 }
12781 }
12782 else
12783 {
12784 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12785 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012786 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012787 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12788 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012789# ifdef FEAT_CONCEAL
12790 if (cur->conceal_char)
12791 {
12792 char_u buf[MB_MAXBYTES + 1];
12793
12794 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12795 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12796 }
12797# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012798 list_append_dict(rettv->vval.v_list, dict);
12799 cur = cur->next;
12800 }
12801 }
12802#endif
12803}
12804
12805/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012806 * "getpid()" function
12807 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012810{
12811 rettv->vval.v_number = mch_get_pid();
12812}
12813
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012814static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012815
12816/*
12817 * "getcurpos()" function
12818 */
12819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012820f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012821{
12822 getpos_both(argvars, rettv, TRUE);
12823}
12824
Bram Moolenaar18081e32008-02-20 19:11:07 +000012825/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012826 * "getpos(string)" function
12827 */
12828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012829f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012830{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012831 getpos_both(argvars, rettv, FALSE);
12832}
12833
12834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012835getpos_both(
12836 typval_T *argvars,
12837 typval_T *rettv,
12838 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012839{
Bram Moolenaara5525202006-03-02 22:52:09 +000012840 pos_T *fp;
12841 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012842 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012843
12844 if (rettv_list_alloc(rettv) == OK)
12845 {
12846 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012847 if (getcurpos)
12848 fp = &curwin->w_cursor;
12849 else
12850 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012851 if (fnum != -1)
12852 list_append_number(l, (varnumber_T)fnum);
12853 else
12854 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012855 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12856 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012857 list_append_number(l, (fp != NULL)
12858 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012859 : (varnumber_T)0);
12860 list_append_number(l,
12861#ifdef FEAT_VIRTUALEDIT
12862 (fp != NULL) ? (varnumber_T)fp->coladd :
12863#endif
12864 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012865 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012866 {
12867 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012868 list_append_number(l, curwin->w_curswant == MAXCOL ?
12869 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012870 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012871 }
12872 else
12873 rettv->vval.v_number = FALSE;
12874}
12875
12876/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012877 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012878 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012879 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012880f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012881{
12882#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012883 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012884#endif
12885
Bram Moolenaar2641f772005-03-25 21:58:17 +000012886#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012887 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012888 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012889 wp = NULL;
12890 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12891 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012892 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012893 if (wp == NULL)
12894 return;
12895 }
12896
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012897 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012898 }
12899#endif
12900}
12901
12902/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012903 * "getreg()" function
12904 */
12905 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012906f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012907{
12908 char_u *strregname;
12909 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012910 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012911 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012912 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012913
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012914 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012915 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012916 strregname = get_tv_string_chk(&argvars[0]);
12917 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012918 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012919 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012920 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012921 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12922 return_list = get_tv_number_chk(&argvars[2], &error);
12923 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012924 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012925 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012926 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012927
12928 if (error)
12929 return;
12930
Bram Moolenaar071d4272004-06-13 20:20:40 +000012931 regname = (strregname == NULL ? '"' : *strregname);
12932 if (regname == 0)
12933 regname = '"';
12934
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012935 if (return_list)
12936 {
12937 rettv->v_type = VAR_LIST;
12938 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12939 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012940 if (rettv->vval.v_list != NULL)
12941 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012942 }
12943 else
12944 {
12945 rettv->v_type = VAR_STRING;
12946 rettv->vval.v_string = get_reg_contents(regname,
12947 arg2 ? GREG_EXPR_SRC : 0);
12948 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012949}
12950
12951/*
12952 * "getregtype()" function
12953 */
12954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012955f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012956{
12957 char_u *strregname;
12958 int regname;
12959 char_u buf[NUMBUFLEN + 2];
12960 long reglen = 0;
12961
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012962 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012963 {
12964 strregname = get_tv_string_chk(&argvars[0]);
12965 if (strregname == NULL) /* type error; errmsg already given */
12966 {
12967 rettv->v_type = VAR_STRING;
12968 rettv->vval.v_string = NULL;
12969 return;
12970 }
12971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012972 else
12973 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012974 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975
12976 regname = (strregname == NULL ? '"' : *strregname);
12977 if (regname == 0)
12978 regname = '"';
12979
12980 buf[0] = NUL;
12981 buf[1] = NUL;
12982 switch (get_reg_type(regname, &reglen))
12983 {
12984 case MLINE: buf[0] = 'V'; break;
12985 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012986 case MBLOCK:
12987 buf[0] = Ctrl_V;
12988 sprintf((char *)buf + 1, "%ld", reglen + 1);
12989 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012990 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012991 rettv->v_type = VAR_STRING;
12992 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012993}
12994
12995/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012996 * "gettabvar()" function
12997 */
12998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012999f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013000{
Bram Moolenaar3089a102014-09-09 23:11:49 +020013001 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013002 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013003 dictitem_T *v;
13004 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013005 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013006
13007 rettv->v_type = VAR_STRING;
13008 rettv->vval.v_string = NULL;
13009
13010 varname = get_tv_string_chk(&argvars[1]);
13011 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13012 if (tp != NULL && varname != NULL)
13013 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013014 /* Set tp to be our tabpage, temporarily. Also set the window to the
13015 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013016 if (switch_win(&oldcurwin, &oldtabpage,
13017 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013018 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013019 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013020 /* look up the variable */
13021 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13022 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13023 if (v != NULL)
13024 {
13025 copy_tv(&v->di_tv, rettv);
13026 done = TRUE;
13027 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013028 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013029
13030 /* restore previous notion of curwin */
13031 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013032 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013033
13034 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013035 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013036}
13037
13038/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013039 * "gettabwinvar()" function
13040 */
13041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013042f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013043{
13044 getwinvar(argvars, rettv, 1);
13045}
13046
13047/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048 * "getwinposx()" function
13049 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013051f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013053 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013054#ifdef FEAT_GUI
13055 if (gui.in_use)
13056 {
13057 int x, y;
13058
13059 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013060 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013061 }
13062#endif
13063}
13064
13065/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013066 * "win_findbuf()" function
13067 */
13068 static void
13069f_win_findbuf(typval_T *argvars, typval_T *rettv)
13070{
13071 if (rettv_list_alloc(rettv) != FAIL)
13072 win_findbuf(argvars, rettv->vval.v_list);
13073}
13074
13075/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013076 * "win_getid()" function
13077 */
13078 static void
13079f_win_getid(typval_T *argvars, typval_T *rettv)
13080{
13081 rettv->vval.v_number = win_getid(argvars);
13082}
13083
13084/*
13085 * "win_gotoid()" function
13086 */
13087 static void
13088f_win_gotoid(typval_T *argvars, typval_T *rettv)
13089{
13090 rettv->vval.v_number = win_gotoid(argvars);
13091}
13092
13093/*
13094 * "win_id2tabwin()" function
13095 */
13096 static void
13097f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13098{
13099 if (rettv_list_alloc(rettv) != FAIL)
13100 win_id2tabwin(argvars, rettv->vval.v_list);
13101}
13102
13103/*
13104 * "win_id2win()" function
13105 */
13106 static void
13107f_win_id2win(typval_T *argvars, typval_T *rettv)
13108{
13109 rettv->vval.v_number = win_id2win(argvars);
13110}
13111
13112/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013113 * "getwinposy()" function
13114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013116f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013118 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013119#ifdef FEAT_GUI
13120 if (gui.in_use)
13121 {
13122 int x, y;
13123
13124 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013125 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013126 }
13127#endif
13128}
13129
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013130/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013131 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013132 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013133 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013134find_win_by_nr(
13135 typval_T *vp,
13136 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013137{
13138#ifdef FEAT_WINDOWS
13139 win_T *wp;
13140#endif
13141 int nr;
13142
13143 nr = get_tv_number_chk(vp, NULL);
13144
13145#ifdef FEAT_WINDOWS
13146 if (nr < 0)
13147 return NULL;
13148 if (nr == 0)
13149 return curwin;
13150
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013151 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13152 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013153 if (--nr <= 0)
13154 break;
13155 return wp;
13156#else
13157 if (nr == 0 || nr == 1)
13158 return curwin;
13159 return NULL;
13160#endif
13161}
13162
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013164 * Find window specified by "wvp" in tabpage "tvp".
13165 */
13166 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013167find_tabwin(
13168 typval_T *wvp, /* VAR_UNKNOWN for current window */
13169 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013170{
13171 win_T *wp = NULL;
13172 tabpage_T *tp = NULL;
13173 long n;
13174
13175 if (wvp->v_type != VAR_UNKNOWN)
13176 {
13177 if (tvp->v_type != VAR_UNKNOWN)
13178 {
13179 n = get_tv_number(tvp);
13180 if (n >= 0)
13181 tp = find_tabpage(n);
13182 }
13183 else
13184 tp = curtab;
13185
13186 if (tp != NULL)
13187 wp = find_win_by_nr(wvp, tp);
13188 }
13189 else
13190 wp = curwin;
13191
13192 return wp;
13193}
13194
13195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013196 * "getwinvar()" function
13197 */
13198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013199f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013200{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013201 getwinvar(argvars, rettv, 0);
13202}
13203
13204/*
13205 * getwinvar() and gettabwinvar()
13206 */
13207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013208getwinvar(
13209 typval_T *argvars,
13210 typval_T *rettv,
13211 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013212{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013213 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013215 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013216 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013217 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013218#ifdef FEAT_WINDOWS
13219 win_T *oldcurwin;
13220 tabpage_T *oldtabpage;
13221 int need_switch_win;
13222#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013223
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013224#ifdef FEAT_WINDOWS
13225 if (off == 1)
13226 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13227 else
13228 tp = curtab;
13229#endif
13230 win = find_win_by_nr(&argvars[off], tp);
13231 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013232 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013233
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013234 rettv->v_type = VAR_STRING;
13235 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013236
13237 if (win != NULL && varname != NULL)
13238 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013239#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013240 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013241 * otherwise the window is not valid. Only do this when needed,
13242 * autocommands get blocked. */
13243 need_switch_win = !(tp == curtab && win == curwin);
13244 if (!need_switch_win
13245 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13246#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013247 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013248 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013249 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013250 if (get_option_tv(&varname, rettv, 1) == OK)
13251 done = TRUE;
13252 }
13253 else
13254 {
13255 /* Look up the variable. */
13256 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13257 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13258 varname, FALSE);
13259 if (v != NULL)
13260 {
13261 copy_tv(&v->di_tv, rettv);
13262 done = TRUE;
13263 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013265 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013266
Bram Moolenaarba117c22015-09-29 16:53:22 +020013267#ifdef FEAT_WINDOWS
13268 if (need_switch_win)
13269 /* restore previous notion of curwin */
13270 restore_win(oldcurwin, oldtabpage, TRUE);
13271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013272 }
13273
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013274 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13275 /* use the default return value */
13276 copy_tv(&argvars[off + 2], rettv);
13277
Bram Moolenaar071d4272004-06-13 20:20:40 +000013278 --emsg_off;
13279}
13280
13281/*
13282 * "glob()" function
13283 */
13284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013287 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013288 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013289 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013290
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013291 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013292 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013293 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013294 if (argvars[1].v_type != VAR_UNKNOWN)
13295 {
13296 if (get_tv_number_chk(&argvars[1], &error))
13297 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013298 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013299 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013300 if (get_tv_number_chk(&argvars[2], &error))
13301 {
13302 rettv->v_type = VAR_LIST;
13303 rettv->vval.v_list = NULL;
13304 }
13305 if (argvars[3].v_type != VAR_UNKNOWN
13306 && get_tv_number_chk(&argvars[3], &error))
13307 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013308 }
13309 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013310 if (!error)
13311 {
13312 ExpandInit(&xpc);
13313 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013314 if (p_wic)
13315 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013316 if (rettv->v_type == VAR_STRING)
13317 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013318 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013319 else if (rettv_list_alloc(rettv) != FAIL)
13320 {
13321 int i;
13322
13323 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13324 NULL, options, WILD_ALL_KEEP);
13325 for (i = 0; i < xpc.xp_numfiles; i++)
13326 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13327
13328 ExpandCleanup(&xpc);
13329 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013330 }
13331 else
13332 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333}
13334
13335/*
13336 * "globpath()" function
13337 */
13338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013339f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013340{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013341 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013342 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013343 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013344 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013345 garray_T ga;
13346 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013347
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013348 /* When the optional second argument is non-zero, don't remove matches
13349 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013350 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013351 if (argvars[2].v_type != VAR_UNKNOWN)
13352 {
13353 if (get_tv_number_chk(&argvars[2], &error))
13354 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013355 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013356 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013357 if (get_tv_number_chk(&argvars[3], &error))
13358 {
13359 rettv->v_type = VAR_LIST;
13360 rettv->vval.v_list = NULL;
13361 }
13362 if (argvars[4].v_type != VAR_UNKNOWN
13363 && get_tv_number_chk(&argvars[4], &error))
13364 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013365 }
13366 }
13367 if (file != NULL && !error)
13368 {
13369 ga_init2(&ga, (int)sizeof(char_u *), 10);
13370 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13371 if (rettv->v_type == VAR_STRING)
13372 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13373 else if (rettv_list_alloc(rettv) != FAIL)
13374 for (i = 0; i < ga.ga_len; ++i)
13375 list_append_string(rettv->vval.v_list,
13376 ((char_u **)(ga.ga_data))[i], -1);
13377 ga_clear_strings(&ga);
13378 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013379 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013380 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013381}
13382
13383/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013384 * "glob2regpat()" function
13385 */
13386 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013387f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013388{
13389 char_u *pat = get_tv_string_chk(&argvars[0]);
13390
13391 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013392 rettv->vval.v_string = (pat == NULL)
13393 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013394}
13395
13396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013397 * "has()" function
13398 */
13399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013400f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401{
13402 int i;
13403 char_u *name;
13404 int n = FALSE;
13405 static char *(has_list[]) =
13406 {
13407#ifdef AMIGA
13408 "amiga",
13409# ifdef FEAT_ARP
13410 "arp",
13411# endif
13412#endif
13413#ifdef __BEOS__
13414 "beos",
13415#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013416#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417 "mac",
13418#endif
13419#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013420 "macunix", /* built with 'darwin' enabled */
13421#endif
13422#if defined(__APPLE__) && __APPLE__ == 1
13423 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425#ifdef __QNX__
13426 "qnx",
13427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013428#ifdef UNIX
13429 "unix",
13430#endif
13431#ifdef VMS
13432 "vms",
13433#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013434#ifdef WIN32
13435 "win32",
13436#endif
13437#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13438 "win32unix",
13439#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013440#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441 "win64",
13442#endif
13443#ifdef EBCDIC
13444 "ebcdic",
13445#endif
13446#ifndef CASE_INSENSITIVE_FILENAME
13447 "fname_case",
13448#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013449#ifdef HAVE_ACL
13450 "acl",
13451#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#ifdef FEAT_ARABIC
13453 "arabic",
13454#endif
13455#ifdef FEAT_AUTOCMD
13456 "autocmd",
13457#endif
13458#ifdef FEAT_BEVAL
13459 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013460# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13461 "balloon_multiline",
13462# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013463#endif
13464#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13465 "builtin_terms",
13466# ifdef ALL_BUILTIN_TCAPS
13467 "all_builtin_terms",
13468# endif
13469#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013470#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13471 || defined(FEAT_GUI_W32) \
13472 || defined(FEAT_GUI_MOTIF))
13473 "browsefilter",
13474#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013475#ifdef FEAT_BYTEOFF
13476 "byte_offset",
13477#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013478#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013479 "channel",
13480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013481#ifdef FEAT_CINDENT
13482 "cindent",
13483#endif
13484#ifdef FEAT_CLIENTSERVER
13485 "clientserver",
13486#endif
13487#ifdef FEAT_CLIPBOARD
13488 "clipboard",
13489#endif
13490#ifdef FEAT_CMDL_COMPL
13491 "cmdline_compl",
13492#endif
13493#ifdef FEAT_CMDHIST
13494 "cmdline_hist",
13495#endif
13496#ifdef FEAT_COMMENTS
13497 "comments",
13498#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013499#ifdef FEAT_CONCEAL
13500 "conceal",
13501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#ifdef FEAT_CRYPT
13503 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013504 "crypt-blowfish",
13505 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013506#endif
13507#ifdef FEAT_CSCOPE
13508 "cscope",
13509#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013510#ifdef FEAT_CURSORBIND
13511 "cursorbind",
13512#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013513#ifdef CURSOR_SHAPE
13514 "cursorshape",
13515#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013516#ifdef DEBUG
13517 "debug",
13518#endif
13519#ifdef FEAT_CON_DIALOG
13520 "dialog_con",
13521#endif
13522#ifdef FEAT_GUI_DIALOG
13523 "dialog_gui",
13524#endif
13525#ifdef FEAT_DIFF
13526 "diff",
13527#endif
13528#ifdef FEAT_DIGRAPHS
13529 "digraphs",
13530#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013531#ifdef FEAT_DIRECTX
13532 "directx",
13533#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013534#ifdef FEAT_DND
13535 "dnd",
13536#endif
13537#ifdef FEAT_EMACS_TAGS
13538 "emacs_tags",
13539#endif
13540 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013541 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013542#ifdef FEAT_SEARCH_EXTRA
13543 "extra_search",
13544#endif
13545#ifdef FEAT_FKMAP
13546 "farsi",
13547#endif
13548#ifdef FEAT_SEARCHPATH
13549 "file_in_path",
13550#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013551#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013552 "filterpipe",
13553#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013554#ifdef FEAT_FIND_ID
13555 "find_in_path",
13556#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013557#ifdef FEAT_FLOAT
13558 "float",
13559#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013560#ifdef FEAT_FOLDING
13561 "folding",
13562#endif
13563#ifdef FEAT_FOOTER
13564 "footer",
13565#endif
13566#if !defined(USE_SYSTEM) && defined(UNIX)
13567 "fork",
13568#endif
13569#ifdef FEAT_GETTEXT
13570 "gettext",
13571#endif
13572#ifdef FEAT_GUI
13573 "gui",
13574#endif
13575#ifdef FEAT_GUI_ATHENA
13576# ifdef FEAT_GUI_NEXTAW
13577 "gui_neXtaw",
13578# else
13579 "gui_athena",
13580# endif
13581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#ifdef FEAT_GUI_GTK
13583 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013584# ifdef USE_GTK3
13585 "gui_gtk3",
13586# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013587 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013588# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013590#ifdef FEAT_GUI_GNOME
13591 "gui_gnome",
13592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593#ifdef FEAT_GUI_MAC
13594 "gui_mac",
13595#endif
13596#ifdef FEAT_GUI_MOTIF
13597 "gui_motif",
13598#endif
13599#ifdef FEAT_GUI_PHOTON
13600 "gui_photon",
13601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013602#ifdef FEAT_GUI_W32
13603 "gui_win32",
13604#endif
13605#ifdef FEAT_HANGULIN
13606 "hangul_input",
13607#endif
13608#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13609 "iconv",
13610#endif
13611#ifdef FEAT_INS_EXPAND
13612 "insert_expand",
13613#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013614#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013615 "job",
13616#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013617#ifdef FEAT_JUMPLIST
13618 "jumplist",
13619#endif
13620#ifdef FEAT_KEYMAP
13621 "keymap",
13622#endif
13623#ifdef FEAT_LANGMAP
13624 "langmap",
13625#endif
13626#ifdef FEAT_LIBCALL
13627 "libcall",
13628#endif
13629#ifdef FEAT_LINEBREAK
13630 "linebreak",
13631#endif
13632#ifdef FEAT_LISP
13633 "lispindent",
13634#endif
13635#ifdef FEAT_LISTCMDS
13636 "listcmds",
13637#endif
13638#ifdef FEAT_LOCALMAP
13639 "localmap",
13640#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013641#ifdef FEAT_LUA
13642# ifndef DYNAMIC_LUA
13643 "lua",
13644# endif
13645#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013646#ifdef FEAT_MENU
13647 "menu",
13648#endif
13649#ifdef FEAT_SESSION
13650 "mksession",
13651#endif
13652#ifdef FEAT_MODIFY_FNAME
13653 "modify_fname",
13654#endif
13655#ifdef FEAT_MOUSE
13656 "mouse",
13657#endif
13658#ifdef FEAT_MOUSESHAPE
13659 "mouseshape",
13660#endif
13661#if defined(UNIX) || defined(VMS)
13662# ifdef FEAT_MOUSE_DEC
13663 "mouse_dec",
13664# endif
13665# ifdef FEAT_MOUSE_GPM
13666 "mouse_gpm",
13667# endif
13668# ifdef FEAT_MOUSE_JSB
13669 "mouse_jsbterm",
13670# endif
13671# ifdef FEAT_MOUSE_NET
13672 "mouse_netterm",
13673# endif
13674# ifdef FEAT_MOUSE_PTERM
13675 "mouse_pterm",
13676# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013677# ifdef FEAT_MOUSE_SGR
13678 "mouse_sgr",
13679# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013680# ifdef FEAT_SYSMOUSE
13681 "mouse_sysmouse",
13682# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013683# ifdef FEAT_MOUSE_URXVT
13684 "mouse_urxvt",
13685# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013686# ifdef FEAT_MOUSE_XTERM
13687 "mouse_xterm",
13688# endif
13689#endif
13690#ifdef FEAT_MBYTE
13691 "multi_byte",
13692#endif
13693#ifdef FEAT_MBYTE_IME
13694 "multi_byte_ime",
13695#endif
13696#ifdef FEAT_MULTI_LANG
13697 "multi_lang",
13698#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013699#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013700#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013701 "mzscheme",
13702#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013703#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013704#ifdef FEAT_OLE
13705 "ole",
13706#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013707 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#ifdef FEAT_PATH_EXTRA
13709 "path_extra",
13710#endif
13711#ifdef FEAT_PERL
13712#ifndef DYNAMIC_PERL
13713 "perl",
13714#endif
13715#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013716#ifdef FEAT_PERSISTENT_UNDO
13717 "persistent_undo",
13718#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013719#ifdef FEAT_PYTHON
13720#ifndef DYNAMIC_PYTHON
13721 "python",
13722#endif
13723#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013724#ifdef FEAT_PYTHON3
13725#ifndef DYNAMIC_PYTHON3
13726 "python3",
13727#endif
13728#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013729#ifdef FEAT_POSTSCRIPT
13730 "postscript",
13731#endif
13732#ifdef FEAT_PRINTER
13733 "printer",
13734#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013735#ifdef FEAT_PROFILE
13736 "profile",
13737#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013738#ifdef FEAT_RELTIME
13739 "reltime",
13740#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013741#ifdef FEAT_QUICKFIX
13742 "quickfix",
13743#endif
13744#ifdef FEAT_RIGHTLEFT
13745 "rightleft",
13746#endif
13747#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13748 "ruby",
13749#endif
13750#ifdef FEAT_SCROLLBIND
13751 "scrollbind",
13752#endif
13753#ifdef FEAT_CMDL_INFO
13754 "showcmd",
13755 "cmdline_info",
13756#endif
13757#ifdef FEAT_SIGNS
13758 "signs",
13759#endif
13760#ifdef FEAT_SMARTINDENT
13761 "smartindent",
13762#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013763#ifdef STARTUPTIME
13764 "startuptime",
13765#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013766#ifdef FEAT_STL_OPT
13767 "statusline",
13768#endif
13769#ifdef FEAT_SUN_WORKSHOP
13770 "sun_workshop",
13771#endif
13772#ifdef FEAT_NETBEANS_INTG
13773 "netbeans_intg",
13774#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013775#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013776 "spell",
13777#endif
13778#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013779 "syntax",
13780#endif
13781#if defined(USE_SYSTEM) || !defined(UNIX)
13782 "system",
13783#endif
13784#ifdef FEAT_TAG_BINS
13785 "tag_binary",
13786#endif
13787#ifdef FEAT_TAG_OLDSTATIC
13788 "tag_old_static",
13789#endif
13790#ifdef FEAT_TAG_ANYWHITE
13791 "tag_any_white",
13792#endif
13793#ifdef FEAT_TCL
13794# ifndef DYNAMIC_TCL
13795 "tcl",
13796# endif
13797#endif
13798#ifdef TERMINFO
13799 "terminfo",
13800#endif
13801#ifdef FEAT_TERMRESPONSE
13802 "termresponse",
13803#endif
13804#ifdef FEAT_TEXTOBJ
13805 "textobjects",
13806#endif
13807#ifdef HAVE_TGETENT
13808 "tgetent",
13809#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013810#ifdef FEAT_TIMERS
13811 "timers",
13812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813#ifdef FEAT_TITLE
13814 "title",
13815#endif
13816#ifdef FEAT_TOOLBAR
13817 "toolbar",
13818#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013819#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13820 "unnamedplus",
13821#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013822#ifdef FEAT_USR_CMDS
13823 "user-commands", /* was accidentally included in 5.4 */
13824 "user_commands",
13825#endif
13826#ifdef FEAT_VIMINFO
13827 "viminfo",
13828#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013829#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013830 "vertsplit",
13831#endif
13832#ifdef FEAT_VIRTUALEDIT
13833 "virtualedit",
13834#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013835 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef FEAT_VISUALEXTRA
13837 "visualextra",
13838#endif
13839#ifdef FEAT_VREPLACE
13840 "vreplace",
13841#endif
13842#ifdef FEAT_WILDIGN
13843 "wildignore",
13844#endif
13845#ifdef FEAT_WILDMENU
13846 "wildmenu",
13847#endif
13848#ifdef FEAT_WINDOWS
13849 "windows",
13850#endif
13851#ifdef FEAT_WAK
13852 "winaltkeys",
13853#endif
13854#ifdef FEAT_WRITEBACKUP
13855 "writebackup",
13856#endif
13857#ifdef FEAT_XIM
13858 "xim",
13859#endif
13860#ifdef FEAT_XFONTSET
13861 "xfontset",
13862#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013863#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013864 "xpm",
13865 "xpm_w32", /* for backward compatibility */
13866#else
13867# if defined(HAVE_XPM)
13868 "xpm",
13869# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013870#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013871#ifdef USE_XSMP
13872 "xsmp",
13873#endif
13874#ifdef USE_XSMP_INTERACT
13875 "xsmp_interact",
13876#endif
13877#ifdef FEAT_XCLIPBOARD
13878 "xterm_clipboard",
13879#endif
13880#ifdef FEAT_XTERM_SAVE
13881 "xterm_save",
13882#endif
13883#if defined(UNIX) && defined(FEAT_X11)
13884 "X11",
13885#endif
13886 NULL
13887 };
13888
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013889 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013890 for (i = 0; has_list[i] != NULL; ++i)
13891 if (STRICMP(name, has_list[i]) == 0)
13892 {
13893 n = TRUE;
13894 break;
13895 }
13896
13897 if (n == FALSE)
13898 {
13899 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013900 {
13901 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013902 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013903 && vim_isdigit(name[6])
13904 && vim_isdigit(name[8])
13905 && vim_isdigit(name[10]))
13906 {
13907 int major = atoi((char *)name + 6);
13908 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013909
13910 /* Expect "patch-9.9.01234". */
13911 n = (major < VIM_VERSION_MAJOR
13912 || (major == VIM_VERSION_MAJOR
13913 && (minor < VIM_VERSION_MINOR
13914 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013915 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013916 }
13917 else
13918 n = has_patch(atoi((char *)name + 5));
13919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920 else if (STRICMP(name, "vim_starting") == 0)
13921 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013922#ifdef FEAT_MBYTE
13923 else if (STRICMP(name, "multi_byte_encoding") == 0)
13924 n = has_mbyte;
13925#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013926#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13927 else if (STRICMP(name, "balloon_multiline") == 0)
13928 n = multiline_balloon_available();
13929#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930#ifdef DYNAMIC_TCL
13931 else if (STRICMP(name, "tcl") == 0)
13932 n = tcl_enabled(FALSE);
13933#endif
13934#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13935 else if (STRICMP(name, "iconv") == 0)
13936 n = iconv_enabled(FALSE);
13937#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013938#ifdef DYNAMIC_LUA
13939 else if (STRICMP(name, "lua") == 0)
13940 n = lua_enabled(FALSE);
13941#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013942#ifdef DYNAMIC_MZSCHEME
13943 else if (STRICMP(name, "mzscheme") == 0)
13944 n = mzscheme_enabled(FALSE);
13945#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013946#ifdef DYNAMIC_RUBY
13947 else if (STRICMP(name, "ruby") == 0)
13948 n = ruby_enabled(FALSE);
13949#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013950#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951#ifdef DYNAMIC_PYTHON
13952 else if (STRICMP(name, "python") == 0)
13953 n = python_enabled(FALSE);
13954#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013955#endif
13956#ifdef FEAT_PYTHON3
13957#ifdef DYNAMIC_PYTHON3
13958 else if (STRICMP(name, "python3") == 0)
13959 n = python3_enabled(FALSE);
13960#endif
13961#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013962#ifdef DYNAMIC_PERL
13963 else if (STRICMP(name, "perl") == 0)
13964 n = perl_enabled(FALSE);
13965#endif
13966#ifdef FEAT_GUI
13967 else if (STRICMP(name, "gui_running") == 0)
13968 n = (gui.in_use || gui.starting);
13969# ifdef FEAT_GUI_W32
13970 else if (STRICMP(name, "gui_win32s") == 0)
13971 n = gui_is_win32s();
13972# endif
13973# ifdef FEAT_BROWSE
13974 else if (STRICMP(name, "browse") == 0)
13975 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13976# endif
13977#endif
13978#ifdef FEAT_SYN_HL
13979 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013980 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981#endif
13982#if defined(WIN3264)
13983 else if (STRICMP(name, "win95") == 0)
13984 n = mch_windows95();
13985#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013986#ifdef FEAT_NETBEANS_INTG
13987 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013988 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013989#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 }
13991
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013992 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013993}
13994
13995/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013996 * "has_key()" function
13997 */
13998 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013999f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000014000{
Bram Moolenaare9a41262005-01-15 22:18:47 +000014001 if (argvars[0].v_type != VAR_DICT)
14002 {
14003 EMSG(_(e_dictreq));
14004 return;
14005 }
14006 if (argvars[0].vval.v_dict == NULL)
14007 return;
14008
14009 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014010 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014011}
14012
14013/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014014 * "haslocaldir()" function
14015 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014016 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014017f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014018{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014019 win_T *wp = NULL;
14020
14021 wp = find_tabwin(&argvars[0], &argvars[1]);
14022 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014023}
14024
14025/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026 * "hasmapto()" function
14027 */
14028 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014029f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014030{
14031 char_u *name;
14032 char_u *mode;
14033 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014034 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014035
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014037 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014038 mode = (char_u *)"nvo";
14039 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014040 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014041 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014042 if (argvars[2].v_type != VAR_UNKNOWN)
14043 abbr = get_tv_number(&argvars[2]);
14044 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014045
Bram Moolenaar2c932302006-03-18 21:42:09 +000014046 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014047 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014049 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014050}
14051
14052/*
14053 * "histadd()" function
14054 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014056f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014057{
14058#ifdef FEAT_CMDHIST
14059 int histype;
14060 char_u *str;
14061 char_u buf[NUMBUFLEN];
14062#endif
14063
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014064 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 if (check_restricted() || check_secure())
14066 return;
14067#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014068 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14069 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014070 if (histype >= 0)
14071 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014072 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014073 if (*str != NUL)
14074 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014075 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014077 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 return;
14079 }
14080 }
14081#endif
14082}
14083
14084/*
14085 * "histdel()" function
14086 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014088f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014089{
14090#ifdef FEAT_CMDHIST
14091 int n;
14092 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014093 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014095 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14096 if (str == NULL)
14097 n = 0;
14098 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014100 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014101 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014102 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014103 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 else
14106 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014107 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014108 get_tv_string_buf(&argvars[1], buf));
14109 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014110#endif
14111}
14112
14113/*
14114 * "histget()" function
14115 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014117f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118{
14119#ifdef FEAT_CMDHIST
14120 int type;
14121 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014122 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014124 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14125 if (str == NULL)
14126 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014128 {
14129 type = get_histtype(str);
14130 if (argvars[1].v_type == VAR_UNKNOWN)
14131 idx = get_history_idx(type);
14132 else
14133 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14134 /* -1 on type error */
14135 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14136 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014138 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014140 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014141}
14142
14143/*
14144 * "histnr()" function
14145 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014147f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014148{
14149 int i;
14150
14151#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 char_u *history = get_tv_string_chk(&argvars[0]);
14153
14154 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 if (i >= HIST_CMD && i < HIST_COUNT)
14156 i = get_history_idx(i);
14157 else
14158#endif
14159 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161}
14162
14163/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014164 * "highlightID(name)" function
14165 */
14166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014167f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014168{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014169 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014170}
14171
14172/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014173 * "highlight_exists()" function
14174 */
14175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014176f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014177{
14178 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14179}
14180
14181/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 * "hostname()" function
14183 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014184 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014185f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186{
14187 char_u hostname[256];
14188
14189 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014190 rettv->v_type = VAR_STRING;
14191 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014192}
14193
14194/*
14195 * iconv() function
14196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014198f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014199{
14200#ifdef FEAT_MBYTE
14201 char_u buf1[NUMBUFLEN];
14202 char_u buf2[NUMBUFLEN];
14203 char_u *from, *to, *str;
14204 vimconv_T vimconv;
14205#endif
14206
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014207 rettv->v_type = VAR_STRING;
14208 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209
14210#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014211 str = get_tv_string(&argvars[0]);
14212 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14213 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014214 vimconv.vc_type = CONV_NONE;
14215 convert_setup(&vimconv, from, to);
14216
14217 /* If the encodings are equal, no conversion needed. */
14218 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014219 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014221 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014222
14223 convert_setup(&vimconv, NULL, NULL);
14224 vim_free(from);
14225 vim_free(to);
14226#endif
14227}
14228
14229/*
14230 * "indent()" function
14231 */
14232 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014233f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234{
14235 linenr_T lnum;
14236
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014237 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014238 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014239 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014240 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014241 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014242}
14243
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014244/*
14245 * "index()" function
14246 */
14247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014248f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014249{
Bram Moolenaar33570922005-01-25 22:26:29 +000014250 list_T *l;
14251 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014252 long idx = 0;
14253 int ic = FALSE;
14254
14255 rettv->vval.v_number = -1;
14256 if (argvars[0].v_type != VAR_LIST)
14257 {
14258 EMSG(_(e_listreq));
14259 return;
14260 }
14261 l = argvars[0].vval.v_list;
14262 if (l != NULL)
14263 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014264 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014265 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014266 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014267 int error = FALSE;
14268
Bram Moolenaar758711c2005-02-02 23:11:38 +000014269 /* Start at specified item. Use the cached index that list_find()
14270 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014271 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014272 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014273 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014274 ic = get_tv_number_chk(&argvars[3], &error);
14275 if (error)
14276 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014277 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014278
Bram Moolenaar758711c2005-02-02 23:11:38 +000014279 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014280 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014281 {
14282 rettv->vval.v_number = idx;
14283 break;
14284 }
14285 }
14286}
14287
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288static int inputsecret_flag = 0;
14289
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014290static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014291
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014293 * This function is used by f_input() and f_inputdialog() functions. The third
14294 * argument to f_input() specifies the type of completion to use at the
14295 * prompt. The third argument to f_inputdialog() specifies the value to return
14296 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014297 */
14298 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014299get_user_input(
14300 typval_T *argvars,
14301 typval_T *rettv,
14302 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014305 char_u *p = NULL;
14306 int c;
14307 char_u buf[NUMBUFLEN];
14308 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014309 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014310 int xp_type = EXPAND_NOTHING;
14311 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014312
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014314 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014315
14316#ifdef NO_CONSOLE_INPUT
14317 /* While starting up, there is no place to enter text. */
14318 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014319 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014320#endif
14321
14322 cmd_silent = FALSE; /* Want to see the prompt. */
14323 if (prompt != NULL)
14324 {
14325 /* Only the part of the message after the last NL is considered as
14326 * prompt for the command line */
14327 p = vim_strrchr(prompt, '\n');
14328 if (p == NULL)
14329 p = prompt;
14330 else
14331 {
14332 ++p;
14333 c = *p;
14334 *p = NUL;
14335 msg_start();
14336 msg_clr_eos();
14337 msg_puts_attr(prompt, echo_attr);
14338 msg_didout = FALSE;
14339 msg_starthere();
14340 *p = c;
14341 }
14342 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014343
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014344 if (argvars[1].v_type != VAR_UNKNOWN)
14345 {
14346 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14347 if (defstr != NULL)
14348 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014349
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014350 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014351 {
14352 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014353 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014354 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014355
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014356 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014357 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014358
Bram Moolenaar4463f292005-09-25 22:20:24 +000014359 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14360 if (xp_name == NULL)
14361 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014362
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014363 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014364
Bram Moolenaar4463f292005-09-25 22:20:24 +000014365 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14366 &xp_arg) == FAIL)
14367 return;
14368 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014369 }
14370
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014371 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014372 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014373 int save_ex_normal_busy = ex_normal_busy;
14374 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014375 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014376 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14377 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014378 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014379 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014380 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014381 && argvars[1].v_type != VAR_UNKNOWN
14382 && argvars[2].v_type != VAR_UNKNOWN)
14383 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14384 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014385
14386 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014388 /* since the user typed this, no need to wait for return */
14389 need_wait_return = FALSE;
14390 msg_didout = FALSE;
14391 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 cmd_silent = cmd_silent_save;
14393}
14394
14395/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014396 * "input()" function
14397 * Also handles inputsecret() when inputsecret is set.
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014401{
14402 get_user_input(argvars, rettv, FALSE);
14403}
14404
14405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014406 * "inputdialog()" function
14407 */
14408 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014409f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014410{
14411#if defined(FEAT_GUI_TEXTDIALOG)
14412 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14413 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14414 {
14415 char_u *message;
14416 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014417 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014419 message = get_tv_string_chk(&argvars[0]);
14420 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014421 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014422 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014423 else
14424 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014425 if (message != NULL && defstr != NULL
14426 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014427 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 else
14430 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014431 if (message != NULL && defstr != NULL
14432 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014433 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014434 rettv->vval.v_string = vim_strsave(
14435 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014436 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014437 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014438 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014439 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014440 }
14441 else
14442#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014443 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014444}
14445
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014446/*
14447 * "inputlist()" function
14448 */
14449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014450f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014451{
14452 listitem_T *li;
14453 int selected;
14454 int mouse_used;
14455
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014456#ifdef NO_CONSOLE_INPUT
14457 /* While starting up, there is no place to enter text. */
14458 if (no_console_input())
14459 return;
14460#endif
14461 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14462 {
14463 EMSG2(_(e_listarg), "inputlist()");
14464 return;
14465 }
14466
14467 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014468 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014469 lines_left = Rows; /* avoid more prompt */
14470 msg_scroll = TRUE;
14471 msg_clr_eos();
14472
14473 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14474 {
14475 msg_puts(get_tv_string(&li->li_tv));
14476 msg_putchar('\n');
14477 }
14478
14479 /* Ask for choice. */
14480 selected = prompt_for_number(&mouse_used);
14481 if (mouse_used)
14482 selected -= lines_left;
14483
14484 rettv->vval.v_number = selected;
14485}
14486
14487
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14489
14490/*
14491 * "inputrestore()" function
14492 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014494f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014495{
14496 if (ga_userinput.ga_len > 0)
14497 {
14498 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014499 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14500 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014501 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 }
14503 else if (p_verbose > 1)
14504 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014505 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014506 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014507 }
14508}
14509
14510/*
14511 * "inputsave()" function
14512 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014514f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014516 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517 if (ga_grow(&ga_userinput, 1) == OK)
14518 {
14519 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14520 + ga_userinput.ga_len);
14521 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014522 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014523 }
14524 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526}
14527
14528/*
14529 * "inputsecret()" function
14530 */
14531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014532f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014533{
14534 ++cmdline_star;
14535 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014536 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014537 --cmdline_star;
14538 --inputsecret_flag;
14539}
14540
14541/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542 * "insert()" function
14543 */
14544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014545f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014546{
14547 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014548 listitem_T *item;
14549 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014550 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014551
14552 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014553 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014554 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014555 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014556 {
14557 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014558 before = get_tv_number_chk(&argvars[2], &error);
14559 if (error)
14560 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014561
Bram Moolenaar758711c2005-02-02 23:11:38 +000014562 if (before == l->lv_len)
14563 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014564 else
14565 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014566 item = list_find(l, before);
14567 if (item == NULL)
14568 {
14569 EMSGN(_(e_listidx), before);
14570 l = NULL;
14571 }
14572 }
14573 if (l != NULL)
14574 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014575 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014576 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014577 }
14578 }
14579}
14580
14581/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014582 * "invert(expr)" function
14583 */
14584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014585f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014586{
14587 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14588}
14589
14590/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014591 * "isdirectory()" function
14592 */
14593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014594f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014595{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014596 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014597}
14598
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014599/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014600 * "islocked()" function
14601 */
14602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014603f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014604{
14605 lval_T lv;
14606 char_u *end;
14607 dictitem_T *di;
14608
14609 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014610 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14611 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014612 if (end != NULL && lv.ll_name != NULL)
14613 {
14614 if (*end != NUL)
14615 EMSG(_(e_trailing));
14616 else
14617 {
14618 if (lv.ll_tv == NULL)
14619 {
14620 if (check_changedtick(lv.ll_name))
14621 rettv->vval.v_number = 1; /* always locked */
14622 else
14623 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014624 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014625 if (di != NULL)
14626 {
14627 /* Consider a variable locked when:
14628 * 1. the variable itself is locked
14629 * 2. the value of the variable is locked.
14630 * 3. the List or Dict value is locked.
14631 */
14632 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14633 || tv_islocked(&di->di_tv));
14634 }
14635 }
14636 }
14637 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014638 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014639 else if (lv.ll_newkey != NULL)
14640 EMSG2(_(e_dictkey), lv.ll_newkey);
14641 else if (lv.ll_list != NULL)
14642 /* List item. */
14643 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14644 else
14645 /* Dictionary item. */
14646 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14647 }
14648 }
14649
14650 clear_lval(&lv);
14651}
14652
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014653#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14654/*
14655 * "isnan()" function
14656 */
14657 static void
14658f_isnan(typval_T *argvars, typval_T *rettv)
14659{
14660 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14661 && isnan(argvars[0].vval.v_float);
14662}
14663#endif
14664
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014665static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014666
14667/*
14668 * Turn a dict into a list:
14669 * "what" == 0: list of keys
14670 * "what" == 1: list of values
14671 * "what" == 2: list of items
14672 */
14673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014674dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014675{
Bram Moolenaar33570922005-01-25 22:26:29 +000014676 list_T *l2;
14677 dictitem_T *di;
14678 hashitem_T *hi;
14679 listitem_T *li;
14680 listitem_T *li2;
14681 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014682 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683
Bram Moolenaar8c711452005-01-14 21:53:12 +000014684 if (argvars[0].v_type != VAR_DICT)
14685 {
14686 EMSG(_(e_dictreq));
14687 return;
14688 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014689 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014690 return;
14691
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014692 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014694
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014695 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014696 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014697 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014698 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014699 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014700 --todo;
14701 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014702
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014703 li = listitem_alloc();
14704 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014705 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014706 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014707
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014708 if (what == 0)
14709 {
14710 /* keys() */
14711 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014712 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014713 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14714 }
14715 else if (what == 1)
14716 {
14717 /* values() */
14718 copy_tv(&di->di_tv, &li->li_tv);
14719 }
14720 else
14721 {
14722 /* items() */
14723 l2 = list_alloc();
14724 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014725 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014726 li->li_tv.vval.v_list = l2;
14727 if (l2 == NULL)
14728 break;
14729 ++l2->lv_refcount;
14730
14731 li2 = listitem_alloc();
14732 if (li2 == NULL)
14733 break;
14734 list_append(l2, li2);
14735 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014736 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014737 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14738
14739 li2 = listitem_alloc();
14740 if (li2 == NULL)
14741 break;
14742 list_append(l2, li2);
14743 copy_tv(&di->di_tv, &li2->li_tv);
14744 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014745 }
14746 }
14747}
14748
14749/*
14750 * "items(dict)" function
14751 */
14752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014753f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014754{
14755 dict_list(argvars, rettv, 2);
14756}
14757
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014758#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014759/*
14760 * Get the job from the argument.
14761 * Returns NULL if the job is invalid.
14762 */
14763 static job_T *
14764get_job_arg(typval_T *tv)
14765{
14766 job_T *job;
14767
14768 if (tv->v_type != VAR_JOB)
14769 {
14770 EMSG2(_(e_invarg2), get_tv_string(tv));
14771 return NULL;
14772 }
14773 job = tv->vval.v_job;
14774
14775 if (job == NULL)
14776 EMSG(_("E916: not a valid job"));
14777 return job;
14778}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014779
Bram Moolenaar835dc632016-02-07 14:27:38 +010014780/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014781 * "job_getchannel()" function
14782 */
14783 static void
14784f_job_getchannel(typval_T *argvars, typval_T *rettv)
14785{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014786 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014787
Bram Moolenaar65edff82016-02-21 16:40:11 +010014788 if (job != NULL)
14789 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014790 rettv->v_type = VAR_CHANNEL;
14791 rettv->vval.v_channel = job->jv_channel;
14792 if (job->jv_channel != NULL)
14793 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014794 }
14795}
14796
14797/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014798 * "job_info()" function
14799 */
14800 static void
14801f_job_info(typval_T *argvars, typval_T *rettv)
14802{
14803 job_T *job = get_job_arg(&argvars[0]);
14804
14805 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14806 job_info(job, rettv->vval.v_dict);
14807}
14808
14809/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014810 * "job_setoptions()" function
14811 */
14812 static void
14813f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14814{
14815 job_T *job = get_job_arg(&argvars[0]);
14816 jobopt_T opt;
14817
14818 if (job == NULL)
14819 return;
14820 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014821 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014822 return;
14823 job_set_options(job, &opt);
14824}
14825
14826/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014827 * "job_start()" function
14828 */
14829 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014830f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014831{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014832 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014833 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014834}
14835
14836/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014837 * "job_status()" function
14838 */
14839 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014840f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014841{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014842 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014843
Bram Moolenaar65edff82016-02-21 16:40:11 +010014844 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014845 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014846 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014847 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014848 }
14849}
14850
14851/*
14852 * "job_stop()" function
14853 */
14854 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014855f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014856{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014857 job_T *job = get_job_arg(&argvars[0]);
14858
14859 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014860 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014861}
14862#endif
14863
Bram Moolenaar071d4272004-06-13 20:20:40 +000014864/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014865 * "join()" function
14866 */
14867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014868f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014869{
14870 garray_T ga;
14871 char_u *sep;
14872
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014873 if (argvars[0].v_type != VAR_LIST)
14874 {
14875 EMSG(_(e_listreq));
14876 return;
14877 }
14878 if (argvars[0].vval.v_list == NULL)
14879 return;
14880 if (argvars[1].v_type == VAR_UNKNOWN)
14881 sep = (char_u *)" ";
14882 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014883 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014884
14885 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014886
14887 if (sep != NULL)
14888 {
14889 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014890 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014891 ga_append(&ga, NUL);
14892 rettv->vval.v_string = (char_u *)ga.ga_data;
14893 }
14894 else
14895 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014896}
14897
14898/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014899 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014900 */
14901 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014902f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014903{
14904 js_read_T reader;
14905
14906 reader.js_buf = get_tv_string(&argvars[0]);
14907 reader.js_fill = NULL;
14908 reader.js_used = 0;
14909 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14910 EMSG(_(e_invarg));
14911}
14912
14913/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014914 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014915 */
14916 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014917f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014918{
14919 rettv->v_type = VAR_STRING;
14920 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14921}
14922
14923/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014924 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014925 */
14926 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014927f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014928{
14929 js_read_T reader;
14930
14931 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014932 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014933 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014934 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014935 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014936}
14937
14938/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014939 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014940 */
14941 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014942f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014943{
14944 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014945 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014946}
14947
14948/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014949 * "keys()" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014953{
14954 dict_list(argvars, rettv, 0);
14955}
14956
14957/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014958 * "last_buffer_nr()" function.
14959 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014961f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962{
14963 int n = 0;
14964 buf_T *buf;
14965
14966 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14967 if (n < buf->b_fnum)
14968 n = buf->b_fnum;
14969
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014970 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014971}
14972
14973/*
14974 * "len()" function
14975 */
14976 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014977f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014978{
14979 switch (argvars[0].v_type)
14980 {
14981 case VAR_STRING:
14982 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014983 rettv->vval.v_number = (varnumber_T)STRLEN(
14984 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014985 break;
14986 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014987 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014988 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014989 case VAR_DICT:
14990 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14991 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014992 case VAR_UNKNOWN:
14993 case VAR_SPECIAL:
14994 case VAR_FLOAT:
14995 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014996 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014997 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014998 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014999 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015000 break;
15001 }
15002}
15003
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015004static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015005
15006 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015007libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015008{
15009#ifdef FEAT_LIBCALL
15010 char_u *string_in;
15011 char_u **string_result;
15012 int nr_result;
15013#endif
15014
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015015 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015016 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015017 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015018
15019 if (check_restricted() || check_secure())
15020 return;
15021
15022#ifdef FEAT_LIBCALL
15023 /* The first two args must be strings, otherwise its meaningless */
15024 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15025 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015026 string_in = NULL;
15027 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015028 string_in = argvars[2].vval.v_string;
15029 if (type == VAR_NUMBER)
15030 string_result = NULL;
15031 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015032 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015033 if (mch_libcall(argvars[0].vval.v_string,
15034 argvars[1].vval.v_string,
15035 string_in,
15036 argvars[2].vval.v_number,
15037 string_result,
15038 &nr_result) == OK
15039 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015040 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015041 }
15042#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015043}
15044
15045/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015046 * "libcall()" function
15047 */
15048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015049f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050{
15051 libcall_common(argvars, rettv, VAR_STRING);
15052}
15053
15054/*
15055 * "libcallnr()" function
15056 */
15057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015058f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015059{
15060 libcall_common(argvars, rettv, VAR_NUMBER);
15061}
15062
15063/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015064 * "line(string)" function
15065 */
15066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015067f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068{
15069 linenr_T lnum = 0;
15070 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015071 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015072
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015073 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015074 if (fp != NULL)
15075 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015076 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015077}
15078
15079/*
15080 * "line2byte(lnum)" function
15081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015083f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084{
15085#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015086 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015087#else
15088 linenr_T lnum;
15089
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015090 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015091 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015092 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015093 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015094 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15095 if (rettv->vval.v_number >= 0)
15096 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015097#endif
15098}
15099
15100/*
15101 * "lispindent(lnum)" function
15102 */
15103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015104f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105{
15106#ifdef FEAT_LISP
15107 pos_T pos;
15108 linenr_T lnum;
15109
15110 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15113 {
15114 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015115 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015116 curwin->w_cursor = pos;
15117 }
15118 else
15119#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121}
15122
15123/*
15124 * "localtime()" function
15125 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015126 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015127f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015128{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015129 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015130}
15131
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015132static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015133
15134 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015135get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136{
15137 char_u *keys;
15138 char_u *which;
15139 char_u buf[NUMBUFLEN];
15140 char_u *keys_buf = NULL;
15141 char_u *rhs;
15142 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015143 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015144 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015145 mapblock_T *mp;
15146 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015147
15148 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015149 rettv->v_type = VAR_STRING;
15150 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015151
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015152 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015153 if (*keys == NUL)
15154 return;
15155
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015156 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015157 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015158 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015159 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015160 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015161 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015162 if (argvars[3].v_type != VAR_UNKNOWN)
15163 get_dict = get_tv_number(&argvars[3]);
15164 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 else
15167 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015168 if (which == NULL)
15169 return;
15170
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171 mode = get_map_mode(&which, 0);
15172
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015173 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015174 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015175 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015176
15177 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015178 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015179 /* Return a string. */
15180 if (rhs != NULL)
15181 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015182
Bram Moolenaarbd743252010-10-20 21:23:33 +020015183 }
15184 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15185 {
15186 /* Return a dictionary. */
15187 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15188 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15189 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015190
Bram Moolenaarbd743252010-10-20 21:23:33 +020015191 dict_add_nr_str(dict, "lhs", 0L, lhs);
15192 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15193 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15194 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15195 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15196 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15197 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015198 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015199 dict_add_nr_str(dict, "mode", 0L, mapmode);
15200
15201 vim_free(lhs);
15202 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203 }
15204}
15205
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015206#ifdef FEAT_FLOAT
15207/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015208 * "log()" function
15209 */
15210 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015211f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015212{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015213 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015214
15215 rettv->v_type = VAR_FLOAT;
15216 if (get_float_arg(argvars, &f) == OK)
15217 rettv->vval.v_float = log(f);
15218 else
15219 rettv->vval.v_float = 0.0;
15220}
15221
15222/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015223 * "log10()" function
15224 */
15225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015226f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015227{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015228 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015229
15230 rettv->v_type = VAR_FLOAT;
15231 if (get_float_arg(argvars, &f) == OK)
15232 rettv->vval.v_float = log10(f);
15233 else
15234 rettv->vval.v_float = 0.0;
15235}
15236#endif
15237
Bram Moolenaar1dced572012-04-05 16:54:08 +020015238#ifdef FEAT_LUA
15239/*
15240 * "luaeval()" function
15241 */
15242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015243f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015244{
15245 char_u *str;
15246 char_u buf[NUMBUFLEN];
15247
15248 str = get_tv_string_buf(&argvars[0], buf);
15249 do_luaeval(str, argvars + 1, rettv);
15250}
15251#endif
15252
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015254 * "map()" function
15255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015258{
15259 filter_map(argvars, rettv, TRUE);
15260}
15261
15262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 */
15265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015266f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015268 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269}
15270
15271/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015272 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273 */
15274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015275f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015276{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015277 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015278}
15279
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015280static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281
15282 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015283find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015284{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015285 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015286 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015287 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015288 char_u *pat;
15289 regmatch_T regmatch;
15290 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015291 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292 char_u *save_cpo;
15293 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015294 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015295 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015296 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015297 list_T *l = NULL;
15298 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015299 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015300 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015301
15302 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15303 save_cpo = p_cpo;
15304 p_cpo = (char_u *)"";
15305
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015306 rettv->vval.v_number = -1;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015307 if (type == 3 || type == 4)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015308 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015309 /* type 3: return empty list when there are no matches.
15310 * type 4: return ["", -1, -1, -1] */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015311 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312 goto theend;
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015313 if (type == 4
15314 && (list_append_string(rettv->vval.v_list,
15315 (char_u *)"", 0) == FAIL
15316 || list_append_number(rettv->vval.v_list,
15317 (varnumber_T)-1) == FAIL
15318 || list_append_number(rettv->vval.v_list,
15319 (varnumber_T)-1) == FAIL
15320 || list_append_number(rettv->vval.v_list,
15321 (varnumber_T)-1) == FAIL))
15322 {
15323 list_free(rettv->vval.v_list, TRUE);
15324 rettv->vval.v_list = NULL;
15325 goto theend;
15326 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015327 }
15328 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015329 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015330 rettv->v_type = VAR_STRING;
15331 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015333
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015334 if (argvars[0].v_type == VAR_LIST)
15335 {
15336 if ((l = argvars[0].vval.v_list) == NULL)
15337 goto theend;
15338 li = l->lv_first;
15339 }
15340 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015341 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015342 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015343 len = (long)STRLEN(str);
15344 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015345
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015346 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15347 if (pat == NULL)
15348 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015349
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015350 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015351 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015352 int error = FALSE;
15353
15354 start = get_tv_number_chk(&argvars[2], &error);
15355 if (error)
15356 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015357 if (l != NULL)
15358 {
15359 li = list_find(l, start);
15360 if (li == NULL)
15361 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015362 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015363 }
15364 else
15365 {
15366 if (start < 0)
15367 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015368 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015369 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015370 /* When "count" argument is there ignore matches before "start",
15371 * otherwise skip part of the string. Differs when pattern is "^"
15372 * or "\<". */
15373 if (argvars[3].v_type != VAR_UNKNOWN)
15374 startcol = start;
15375 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015376 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015377 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015378 len -= start;
15379 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015380 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015381
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015382 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015383 nth = get_tv_number_chk(&argvars[3], &error);
15384 if (error)
15385 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015386 }
15387
15388 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15389 if (regmatch.regprog != NULL)
15390 {
15391 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015392
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015393 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015394 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015395 if (l != NULL)
15396 {
15397 if (li == NULL)
15398 {
15399 match = FALSE;
15400 break;
15401 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015402 vim_free(tofree);
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015403 expr = str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015404 if (str == NULL)
15405 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015406 }
15407
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015408 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015409
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015410 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015411 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015412 if (l == NULL && !match)
15413 break;
15414
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015415 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015416 if (l != NULL)
15417 {
15418 li = li->li_next;
15419 ++idx;
15420 }
15421 else
15422 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015423#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015424 startcol = (colnr_T)(regmatch.startp[0]
15425 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015426#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015427 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015428#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015429 if (startcol > (colnr_T)len
15430 || str + startcol <= regmatch.startp[0])
15431 {
15432 match = FALSE;
15433 break;
15434 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015435 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015436 }
15437
15438 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015439 {
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015440 if (type == 4)
15441 {
15442 listitem_T *li1 = rettv->vval.v_list->lv_first;
15443 listitem_T *li2 = li1->li_next;
15444 listitem_T *li3 = li2->li_next;
15445 listitem_T *li4 = li3->li_next;
15446
15447 li1->li_tv.vval.v_string = vim_strnsave(regmatch.startp[0],
15448 (int)(regmatch.endp[0] - regmatch.startp[0]));
15449 li3->li_tv.vval.v_number =
15450 (varnumber_T)(regmatch.startp[0] - expr);
15451 li4->li_tv.vval.v_number =
15452 (varnumber_T)(regmatch.endp[0] - expr);
15453 if (l != NULL)
15454 li2->li_tv.vval.v_number = (varnumber_T)idx;
15455 }
15456 else if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015457 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015458 int i;
15459
15460 /* return list with matched string and submatches */
15461 for (i = 0; i < NSUBEXP; ++i)
15462 {
15463 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015464 {
15465 if (list_append_string(rettv->vval.v_list,
15466 (char_u *)"", 0) == FAIL)
15467 break;
15468 }
15469 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015470 regmatch.startp[i],
15471 (int)(regmatch.endp[i] - regmatch.startp[i]))
15472 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015473 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015474 }
15475 }
15476 else if (type == 2)
15477 {
15478 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015479 if (l != NULL)
15480 copy_tv(&li->li_tv, rettv);
15481 else
15482 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015483 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015484 }
15485 else if (l != NULL)
15486 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015487 else
15488 {
15489 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015490 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015491 (varnumber_T)(regmatch.startp[0] - str);
15492 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015493 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015494 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015495 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015496 }
15497 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015498 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015499 }
15500
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015501 if (type == 4 && l == NULL)
15502 /* matchstrpos() without a list: drop the second item. */
15503 listitem_remove(rettv->vval.v_list,
15504 rettv->vval.v_list->lv_first->li_next);
15505
Bram Moolenaar071d4272004-06-13 20:20:40 +000015506theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015507 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015508 p_cpo = save_cpo;
15509}
15510
15511/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015512 * "match()" function
15513 */
15514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015515f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015516{
15517 find_some_match(argvars, rettv, 1);
15518}
15519
15520/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015521 * "matchadd()" function
15522 */
15523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015524f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015525{
15526#ifdef FEAT_SEARCH_EXTRA
15527 char_u buf[NUMBUFLEN];
15528 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15529 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15530 int prio = 10; /* default priority */
15531 int id = -1;
15532 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015533 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015534
15535 rettv->vval.v_number = -1;
15536
15537 if (grp == NULL || pat == NULL)
15538 return;
15539 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015540 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015541 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015542 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015543 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015544 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015545 if (argvars[4].v_type != VAR_UNKNOWN)
15546 {
15547 if (argvars[4].v_type != VAR_DICT)
15548 {
15549 EMSG(_(e_dictreq));
15550 return;
15551 }
15552 if (dict_find(argvars[4].vval.v_dict,
15553 (char_u *)"conceal", -1) != NULL)
15554 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15555 (char_u *)"conceal", FALSE);
15556 }
15557 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015558 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015559 if (error == TRUE)
15560 return;
15561 if (id >= 1 && id <= 3)
15562 {
15563 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15564 return;
15565 }
15566
Bram Moolenaar6561d522015-07-21 15:48:27 +020015567 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15568 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015569#endif
15570}
15571
15572/*
15573 * "matchaddpos()" function
15574 */
15575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015576f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015577{
15578#ifdef FEAT_SEARCH_EXTRA
15579 char_u buf[NUMBUFLEN];
15580 char_u *group;
15581 int prio = 10;
15582 int id = -1;
15583 int error = FALSE;
15584 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015585 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015586
15587 rettv->vval.v_number = -1;
15588
15589 group = get_tv_string_buf_chk(&argvars[0], buf);
15590 if (group == NULL)
15591 return;
15592
15593 if (argvars[1].v_type != VAR_LIST)
15594 {
15595 EMSG2(_(e_listarg), "matchaddpos()");
15596 return;
15597 }
15598 l = argvars[1].vval.v_list;
15599 if (l == NULL)
15600 return;
15601
15602 if (argvars[2].v_type != VAR_UNKNOWN)
15603 {
15604 prio = get_tv_number_chk(&argvars[2], &error);
15605 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015606 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015607 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015608 if (argvars[4].v_type != VAR_UNKNOWN)
15609 {
15610 if (argvars[4].v_type != VAR_DICT)
15611 {
15612 EMSG(_(e_dictreq));
15613 return;
15614 }
15615 if (dict_find(argvars[4].vval.v_dict,
15616 (char_u *)"conceal", -1) != NULL)
15617 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15618 (char_u *)"conceal", FALSE);
15619 }
15620 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015621 }
15622 if (error == TRUE)
15623 return;
15624
15625 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15626 if (id == 1 || id == 2)
15627 {
15628 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15629 return;
15630 }
15631
Bram Moolenaar6561d522015-07-21 15:48:27 +020015632 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15633 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015634#endif
15635}
15636
15637/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015638 * "matcharg()" function
15639 */
15640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015641f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015642{
15643 if (rettv_list_alloc(rettv) == OK)
15644 {
15645#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015646 int id = get_tv_number(&argvars[0]);
15647 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015648
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015649 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015650 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015651 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15652 {
15653 list_append_string(rettv->vval.v_list,
15654 syn_id2name(m->hlg_id), -1);
15655 list_append_string(rettv->vval.v_list, m->pattern, -1);
15656 }
15657 else
15658 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015659 list_append_string(rettv->vval.v_list, NULL, -1);
15660 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015661 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015662 }
15663#endif
15664 }
15665}
15666
15667/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015668 * "matchdelete()" function
15669 */
15670 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015671f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015672{
15673#ifdef FEAT_SEARCH_EXTRA
15674 rettv->vval.v_number = match_delete(curwin,
15675 (int)get_tv_number(&argvars[0]), TRUE);
15676#endif
15677}
15678
15679/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015680 * "matchend()" function
15681 */
15682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015683f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015684{
15685 find_some_match(argvars, rettv, 0);
15686}
15687
15688/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015689 * "matchlist()" function
15690 */
15691 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015692f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015693{
15694 find_some_match(argvars, rettv, 3);
15695}
15696
15697/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015698 * "matchstr()" function
15699 */
15700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015701f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015702{
15703 find_some_match(argvars, rettv, 2);
15704}
15705
Bram Moolenaar7fed5c12016-03-29 23:10:31 +020015706/*
15707 * "matchstrpos()" function
15708 */
15709 static void
15710f_matchstrpos(typval_T *argvars, typval_T *rettv)
15711{
15712 find_some_match(argvars, rettv, 4);
15713}
15714
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015715static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015716
15717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015718max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015719{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015720 long n = 0;
15721 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015722 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015723
15724 if (argvars[0].v_type == VAR_LIST)
15725 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015726 list_T *l;
15727 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015728
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015729 l = argvars[0].vval.v_list;
15730 if (l != NULL)
15731 {
15732 li = l->lv_first;
15733 if (li != NULL)
15734 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015735 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015736 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015737 {
15738 li = li->li_next;
15739 if (li == NULL)
15740 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015741 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015742 if (domax ? i > n : i < n)
15743 n = i;
15744 }
15745 }
15746 }
15747 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015748 else if (argvars[0].v_type == VAR_DICT)
15749 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015750 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015751 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015752 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015753 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015754
15755 d = argvars[0].vval.v_dict;
15756 if (d != NULL)
15757 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015758 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015759 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015760 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015761 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015762 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015763 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015764 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015765 if (first)
15766 {
15767 n = i;
15768 first = FALSE;
15769 }
15770 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015771 n = i;
15772 }
15773 }
15774 }
15775 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015776 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015777 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015778 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015779}
15780
15781/*
15782 * "max()" function
15783 */
15784 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015785f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015786{
15787 max_min(argvars, rettv, TRUE);
15788}
15789
15790/*
15791 * "min()" function
15792 */
15793 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015794f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015795{
15796 max_min(argvars, rettv, FALSE);
15797}
15798
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015799static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015800
15801/*
15802 * Create the directory in which "dir" is located, and higher levels when
15803 * needed.
15804 */
15805 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015806mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015807{
15808 char_u *p;
15809 char_u *updir;
15810 int r = FAIL;
15811
15812 /* Get end of directory name in "dir".
15813 * We're done when it's "/" or "c:/". */
15814 p = gettail_sep(dir);
15815 if (p <= get_past_head(dir))
15816 return OK;
15817
15818 /* If the directory exists we're done. Otherwise: create it.*/
15819 updir = vim_strnsave(dir, (int)(p - dir));
15820 if (updir == NULL)
15821 return FAIL;
15822 if (mch_isdir(updir))
15823 r = OK;
15824 else if (mkdir_recurse(updir, prot) == OK)
15825 r = vim_mkdir_emsg(updir, prot);
15826 vim_free(updir);
15827 return r;
15828}
15829
15830#ifdef vim_mkdir
15831/*
15832 * "mkdir()" function
15833 */
15834 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015835f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015836{
15837 char_u *dir;
15838 char_u buf[NUMBUFLEN];
15839 int prot = 0755;
15840
15841 rettv->vval.v_number = FAIL;
15842 if (check_restricted() || check_secure())
15843 return;
15844
15845 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015846 if (*dir == NUL)
15847 rettv->vval.v_number = FAIL;
15848 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015849 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015850 if (*gettail(dir) == NUL)
15851 /* remove trailing slashes */
15852 *gettail_sep(dir) = NUL;
15853
15854 if (argvars[1].v_type != VAR_UNKNOWN)
15855 {
15856 if (argvars[2].v_type != VAR_UNKNOWN)
15857 prot = get_tv_number_chk(&argvars[2], NULL);
15858 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15859 mkdir_recurse(dir, prot);
15860 }
15861 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015862 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015863}
15864#endif
15865
Bram Moolenaar0d660222005-01-07 21:51:51 +000015866/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 * "mode()" function
15868 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015870f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015872 char_u buf[3];
15873
15874 buf[1] = NUL;
15875 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015876
Bram Moolenaar071d4272004-06-13 20:20:40 +000015877 if (VIsual_active)
15878 {
15879 if (VIsual_select)
15880 buf[0] = VIsual_mode + 's' - 'v';
15881 else
15882 buf[0] = VIsual_mode;
15883 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015884 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015885 || State == CONFIRM)
15886 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015887 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015888 if (State == ASKMORE)
15889 buf[1] = 'm';
15890 else if (State == CONFIRM)
15891 buf[1] = '?';
15892 }
15893 else if (State == EXTERNCMD)
15894 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015895 else if (State & INSERT)
15896 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015897#ifdef FEAT_VREPLACE
15898 if (State & VREPLACE_FLAG)
15899 {
15900 buf[0] = 'R';
15901 buf[1] = 'v';
15902 }
15903 else
15904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015905 if (State & REPLACE_FLAG)
15906 buf[0] = 'R';
15907 else
15908 buf[0] = 'i';
15909 }
15910 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015911 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015912 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015913 if (exmode_active)
15914 buf[1] = 'v';
15915 }
15916 else if (exmode_active)
15917 {
15918 buf[0] = 'c';
15919 buf[1] = 'e';
15920 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015921 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015922 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015923 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015924 if (finish_op)
15925 buf[1] = 'o';
15926 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015927
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015928 /* Clear out the minor mode when the argument is not a non-zero number or
15929 * non-empty string. */
15930 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015931 buf[1] = NUL;
15932
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015933 rettv->vval.v_string = vim_strsave(buf);
15934 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935}
15936
Bram Moolenaar429fa852013-04-15 12:27:36 +020015937#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015938/*
15939 * "mzeval()" function
15940 */
15941 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015942f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015943{
15944 char_u *str;
15945 char_u buf[NUMBUFLEN];
15946
15947 str = get_tv_string_buf(&argvars[0], buf);
15948 do_mzeval(str, rettv);
15949}
Bram Moolenaar75676462013-01-30 14:55:42 +010015950
15951 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015952mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015953{
15954 typval_T argvars[3];
15955
15956 argvars[0].v_type = VAR_STRING;
15957 argvars[0].vval.v_string = name;
15958 copy_tv(args, &argvars[1]);
15959 argvars[2].v_type = VAR_UNKNOWN;
15960 f_call(argvars, rettv);
15961 clear_tv(&argvars[1]);
15962}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015963#endif
15964
Bram Moolenaar071d4272004-06-13 20:20:40 +000015965/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015966 * "nextnonblank()" function
15967 */
15968 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015969f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015970{
15971 linenr_T lnum;
15972
15973 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015975 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015976 {
15977 lnum = 0;
15978 break;
15979 }
15980 if (*skipwhite(ml_get(lnum)) != NUL)
15981 break;
15982 }
15983 rettv->vval.v_number = lnum;
15984}
15985
15986/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015987 * "nr2char()" function
15988 */
15989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015990f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015991{
15992 char_u buf[NUMBUFLEN];
15993
15994#ifdef FEAT_MBYTE
15995 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015996 {
15997 int utf8 = 0;
15998
15999 if (argvars[1].v_type != VAR_UNKNOWN)
16000 utf8 = get_tv_number_chk(&argvars[1], NULL);
16001 if (utf8)
16002 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16003 else
16004 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
16005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016006 else
16007#endif
16008 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016009 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016010 buf[1] = NUL;
16011 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016012 rettv->v_type = VAR_STRING;
16013 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016014}
16015
16016/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016017 * "or(expr, expr)" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010016021{
16022 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
16023 | get_tv_number_chk(&argvars[1], NULL);
16024}
16025
16026/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016027 * "pathshorten()" function
16028 */
16029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016030f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016031{
16032 char_u *p;
16033
16034 rettv->v_type = VAR_STRING;
16035 p = get_tv_string_chk(&argvars[0]);
16036 if (p == NULL)
16037 rettv->vval.v_string = NULL;
16038 else
16039 {
16040 p = vim_strsave(p);
16041 rettv->vval.v_string = p;
16042 if (p != NULL)
16043 shorten_dir(p);
16044 }
16045}
16046
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016047#ifdef FEAT_PERL
16048/*
16049 * "perleval()" function
16050 */
16051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016052f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010016053{
16054 char_u *str;
16055 char_u buf[NUMBUFLEN];
16056
16057 str = get_tv_string_buf(&argvars[0], buf);
16058 do_perleval(str, rettv);
16059}
16060#endif
16061
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016062#ifdef FEAT_FLOAT
16063/*
16064 * "pow()" function
16065 */
16066 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016067f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016068{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016069 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016070
16071 rettv->v_type = VAR_FLOAT;
16072 if (get_float_arg(argvars, &fx) == OK
16073 && get_float_arg(&argvars[1], &fy) == OK)
16074 rettv->vval.v_float = pow(fx, fy);
16075 else
16076 rettv->vval.v_float = 0.0;
16077}
16078#endif
16079
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016080/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016081 * "prevnonblank()" function
16082 */
16083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016084f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016085{
16086 linenr_T lnum;
16087
16088 lnum = get_tv_lnum(argvars);
16089 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16090 lnum = 0;
16091 else
16092 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16093 --lnum;
16094 rettv->vval.v_number = lnum;
16095}
16096
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016097/* This dummy va_list is here because:
16098 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16099 * - locally in the function results in a "used before set" warning
16100 * - using va_start() to initialize it gives "function with fixed args" error */
16101static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016102
Bram Moolenaar8c711452005-01-14 21:53:12 +000016103/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016104 * "printf()" function
16105 */
16106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016107f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016108{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016109 char_u buf[NUMBUFLEN];
16110 int len;
16111 char_u *s;
16112 int saved_did_emsg = did_emsg;
16113 char *fmt;
16114
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016115 rettv->v_type = VAR_STRING;
16116 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016117
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016118 /* Get the required length, allocate the buffer and do it for real. */
16119 did_emsg = FALSE;
16120 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16121 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16122 if (!did_emsg)
16123 {
16124 s = alloc(len + 1);
16125 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016126 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016127 rettv->vval.v_string = s;
16128 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016129 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016130 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016131 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016132}
16133
16134/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016135 * "pumvisible()" function
16136 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016138f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016139{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016140#ifdef FEAT_INS_EXPAND
16141 if (pum_visible())
16142 rettv->vval.v_number = 1;
16143#endif
16144}
16145
Bram Moolenaardb913952012-06-29 12:54:53 +020016146#ifdef FEAT_PYTHON3
16147/*
16148 * "py3eval()" function
16149 */
16150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016151f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016152{
16153 char_u *str;
16154 char_u buf[NUMBUFLEN];
16155
16156 str = get_tv_string_buf(&argvars[0], buf);
16157 do_py3eval(str, rettv);
16158}
16159#endif
16160
16161#ifdef FEAT_PYTHON
16162/*
16163 * "pyeval()" function
16164 */
16165 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016166f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016167{
16168 char_u *str;
16169 char_u buf[NUMBUFLEN];
16170
16171 str = get_tv_string_buf(&argvars[0], buf);
16172 do_pyeval(str, rettv);
16173}
16174#endif
16175
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016176/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016177 * "range()" function
16178 */
16179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016180f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016181{
16182 long start;
16183 long end;
16184 long stride = 1;
16185 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016186 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016187
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016188 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016189 if (argvars[1].v_type == VAR_UNKNOWN)
16190 {
16191 end = start - 1;
16192 start = 0;
16193 }
16194 else
16195 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016196 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016197 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016198 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016199 }
16200
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016201 if (error)
16202 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016203 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016204 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016205 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016206 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016207 else
16208 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016209 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016210 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016211 if (list_append_number(rettv->vval.v_list,
16212 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016213 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016214 }
16215}
16216
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016217/*
16218 * "readfile()" function
16219 */
16220 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016221f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016222{
16223 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016224 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016225 char_u *fname;
16226 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016227 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16228 int io_size = sizeof(buf);
16229 int readlen; /* size of last fread() */
16230 char_u *prev = NULL; /* previously read bytes, if any */
16231 long prevlen = 0; /* length of data in prev */
16232 long prevsize = 0; /* size of prev buffer */
16233 long maxline = MAXLNUM;
16234 long cnt = 0;
16235 char_u *p; /* position in buf */
16236 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016237
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016238 if (argvars[1].v_type != VAR_UNKNOWN)
16239 {
16240 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16241 binary = TRUE;
16242 if (argvars[2].v_type != VAR_UNKNOWN)
16243 maxline = get_tv_number(&argvars[2]);
16244 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016245
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016246 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016247 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016248
16249 /* Always open the file in binary mode, library functions have a mind of
16250 * their own about CR-LF conversion. */
16251 fname = get_tv_string(&argvars[0]);
16252 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16253 {
16254 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16255 return;
16256 }
16257
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016258 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016259 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016260 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016261
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016262 /* This for loop processes what was read, but is also entered at end
16263 * of file so that either:
16264 * - an incomplete line gets written
16265 * - a "binary" file gets an empty line at the end if it ends in a
16266 * newline. */
16267 for (p = buf, start = buf;
16268 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16269 ++p)
16270 {
16271 if (*p == '\n' || readlen <= 0)
16272 {
16273 listitem_T *li;
16274 char_u *s = NULL;
16275 long_u len = p - start;
16276
16277 /* Finished a line. Remove CRs before NL. */
16278 if (readlen > 0 && !binary)
16279 {
16280 while (len > 0 && start[len - 1] == '\r')
16281 --len;
16282 /* removal may cross back to the "prev" string */
16283 if (len == 0)
16284 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16285 --prevlen;
16286 }
16287 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016288 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016289 else
16290 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016291 /* Change "prev" buffer to be the right size. This way
16292 * the bytes are only copied once, and very long lines are
16293 * allocated only once. */
16294 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016295 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016296 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016297 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016298 prev = NULL; /* the list will own the string */
16299 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016300 }
16301 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016302 if (s == NULL)
16303 {
16304 do_outofmem_msg((long_u) prevlen + len + 1);
16305 failed = TRUE;
16306 break;
16307 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016308
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016309 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016310 {
16311 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016312 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016313 break;
16314 }
16315 li->li_tv.v_type = VAR_STRING;
16316 li->li_tv.v_lock = 0;
16317 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016318 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016319
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016320 start = p + 1; /* step over newline */
16321 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016322 break;
16323 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016324 else if (*p == NUL)
16325 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016326#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016327 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16328 * when finding the BF and check the previous two bytes. */
16329 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016330 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016331 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16332 * + 1, these may be in the "prev" string. */
16333 char_u back1 = p >= buf + 1 ? p[-1]
16334 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16335 char_u back2 = p >= buf + 2 ? p[-2]
16336 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16337 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016338
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016339 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016340 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016341 char_u *dest = p - 2;
16342
16343 /* Usually a BOM is at the beginning of a file, and so at
16344 * the beginning of a line; then we can just step over it.
16345 */
16346 if (start == dest)
16347 start = p + 1;
16348 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016349 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016350 /* have to shuffle buf to close gap */
16351 int adjust_prevlen = 0;
16352
16353 if (dest < buf)
16354 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016355 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016356 dest = buf;
16357 }
16358 if (readlen > p - buf + 1)
16359 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16360 readlen -= 3 - adjust_prevlen;
16361 prevlen -= adjust_prevlen;
16362 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016363 }
16364 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016365 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016366#endif
16367 } /* for */
16368
16369 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16370 break;
16371 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016372 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016373 /* There's part of a line in buf, store it in "prev". */
16374 if (p - start + prevlen >= prevsize)
16375 {
16376 /* need bigger "prev" buffer */
16377 char_u *newprev;
16378
16379 /* A common use case is ordinary text files and "prev" gets a
16380 * fragment of a line, so the first allocation is made
16381 * small, to avoid repeatedly 'allocing' large and
16382 * 'reallocing' small. */
16383 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016384 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016385 else
16386 {
16387 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016388 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016389 prevsize = grow50pc > growmin ? grow50pc : growmin;
16390 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016391 newprev = prev == NULL ? alloc(prevsize)
16392 : vim_realloc(prev, prevsize);
16393 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016394 {
16395 do_outofmem_msg((long_u)prevsize);
16396 failed = TRUE;
16397 break;
16398 }
16399 prev = newprev;
16400 }
16401 /* Add the line part to end of "prev". */
16402 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016403 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016404 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016405 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016406
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016407 /*
16408 * For a negative line count use only the lines at the end of the file,
16409 * free the rest.
16410 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016411 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016412 while (cnt > -maxline)
16413 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016414 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016415 --cnt;
16416 }
16417
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016418 if (failed)
16419 {
16420 list_free(rettv->vval.v_list, TRUE);
16421 /* readfile doc says an empty list is returned on error */
16422 rettv->vval.v_list = list_alloc();
16423 }
16424
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016425 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016426 fclose(fd);
16427}
16428
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016429#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016430static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016431
16432/*
16433 * Convert a List to proftime_T.
16434 * Return FAIL when there is something wrong.
16435 */
16436 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016437list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016438{
16439 long n1, n2;
16440 int error = FALSE;
16441
16442 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16443 || arg->vval.v_list->lv_len != 2)
16444 return FAIL;
16445 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16446 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16447# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016448 tm->HighPart = n1;
16449 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016450# else
16451 tm->tv_sec = n1;
16452 tm->tv_usec = n2;
16453# endif
16454 return error ? FAIL : OK;
16455}
16456#endif /* FEAT_RELTIME */
16457
16458/*
16459 * "reltime()" function
16460 */
16461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016462f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016463{
16464#ifdef FEAT_RELTIME
16465 proftime_T res;
16466 proftime_T start;
16467
16468 if (argvars[0].v_type == VAR_UNKNOWN)
16469 {
16470 /* No arguments: get current time. */
16471 profile_start(&res);
16472 }
16473 else if (argvars[1].v_type == VAR_UNKNOWN)
16474 {
16475 if (list2proftime(&argvars[0], &res) == FAIL)
16476 return;
16477 profile_end(&res);
16478 }
16479 else
16480 {
16481 /* Two arguments: compute the difference. */
16482 if (list2proftime(&argvars[0], &start) == FAIL
16483 || list2proftime(&argvars[1], &res) == FAIL)
16484 return;
16485 profile_sub(&res, &start);
16486 }
16487
16488 if (rettv_list_alloc(rettv) == OK)
16489 {
16490 long n1, n2;
16491
16492# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016493 n1 = res.HighPart;
16494 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016495# else
16496 n1 = res.tv_sec;
16497 n2 = res.tv_usec;
16498# endif
16499 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16500 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16501 }
16502#endif
16503}
16504
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016505#ifdef FEAT_FLOAT
16506/*
16507 * "reltimefloat()" function
16508 */
16509 static void
16510f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16511{
16512# ifdef FEAT_RELTIME
16513 proftime_T tm;
16514# endif
16515
16516 rettv->v_type = VAR_FLOAT;
16517 rettv->vval.v_float = 0;
16518# ifdef FEAT_RELTIME
16519 if (list2proftime(&argvars[0], &tm) == OK)
16520 rettv->vval.v_float = profile_float(&tm);
16521# endif
16522}
16523#endif
16524
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016525/*
16526 * "reltimestr()" function
16527 */
16528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016529f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016530{
16531#ifdef FEAT_RELTIME
16532 proftime_T tm;
16533#endif
16534
16535 rettv->v_type = VAR_STRING;
16536 rettv->vval.v_string = NULL;
16537#ifdef FEAT_RELTIME
16538 if (list2proftime(&argvars[0], &tm) == OK)
16539 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16540#endif
16541}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016542
Bram Moolenaar0d660222005-01-07 21:51:51 +000016543#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016544static void make_connection(void);
16545static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546
16547 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016548make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016549{
16550 if (X_DISPLAY == NULL
16551# ifdef FEAT_GUI
16552 && !gui.in_use
16553# endif
16554 )
16555 {
16556 x_force_connect = TRUE;
16557 setup_term_clip();
16558 x_force_connect = FALSE;
16559 }
16560}
16561
16562 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016563check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564{
16565 make_connection();
16566 if (X_DISPLAY == NULL)
16567 {
16568 EMSG(_("E240: No connection to Vim server"));
16569 return FAIL;
16570 }
16571 return OK;
16572}
16573#endif
16574
16575#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016576static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016577
16578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016579remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016580{
16581 char_u *server_name;
16582 char_u *keys;
16583 char_u *r = NULL;
16584 char_u buf[NUMBUFLEN];
16585# ifdef WIN32
16586 HWND w;
16587# else
16588 Window w;
16589# endif
16590
16591 if (check_restricted() || check_secure())
16592 return;
16593
16594# ifdef FEAT_X11
16595 if (check_connection() == FAIL)
16596 return;
16597# endif
16598
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016599 server_name = get_tv_string_chk(&argvars[0]);
16600 if (server_name == NULL)
16601 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 keys = get_tv_string_buf(&argvars[1], buf);
16603# ifdef WIN32
16604 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16605# else
16606 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16607 < 0)
16608# endif
16609 {
16610 if (r != NULL)
16611 EMSG(r); /* sending worked but evaluation failed */
16612 else
16613 EMSG2(_("E241: Unable to send to %s"), server_name);
16614 return;
16615 }
16616
16617 rettv->vval.v_string = r;
16618
16619 if (argvars[2].v_type != VAR_UNKNOWN)
16620 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016621 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016622 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016623 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016625 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016626 v.di_tv.v_type = VAR_STRING;
16627 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 idvar = get_tv_string_chk(&argvars[2]);
16629 if (idvar != NULL)
16630 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016631 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016632 }
16633}
16634#endif
16635
16636/*
16637 * "remote_expr()" function
16638 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016639 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016640f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641{
16642 rettv->v_type = VAR_STRING;
16643 rettv->vval.v_string = NULL;
16644#ifdef FEAT_CLIENTSERVER
16645 remote_common(argvars, rettv, TRUE);
16646#endif
16647}
16648
16649/*
16650 * "remote_foreground()" function
16651 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016652 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016653f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016654{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016655#ifdef FEAT_CLIENTSERVER
16656# ifdef WIN32
16657 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016658 {
16659 char_u *server_name = get_tv_string_chk(&argvars[0]);
16660
16661 if (server_name != NULL)
16662 serverForeground(server_name);
16663 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016664# else
16665 /* Send a foreground() expression to the server. */
16666 argvars[1].v_type = VAR_STRING;
16667 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16668 argvars[2].v_type = VAR_UNKNOWN;
16669 remote_common(argvars, rettv, TRUE);
16670 vim_free(argvars[1].vval.v_string);
16671# endif
16672#endif
16673}
16674
Bram Moolenaar0d660222005-01-07 21:51:51 +000016675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016676f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016677{
16678#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016679 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016680 char_u *s = NULL;
16681# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016682 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016684 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016685
16686 if (check_restricted() || check_secure())
16687 {
16688 rettv->vval.v_number = -1;
16689 return;
16690 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016691 serverid = get_tv_string_chk(&argvars[0]);
16692 if (serverid == NULL)
16693 {
16694 rettv->vval.v_number = -1;
16695 return; /* type error; errmsg already given */
16696 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016698 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016699 if (n == 0)
16700 rettv->vval.v_number = -1;
16701 else
16702 {
16703 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16704 rettv->vval.v_number = (s != NULL);
16705 }
16706# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016707 if (check_connection() == FAIL)
16708 return;
16709
16710 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016711 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016712# endif
16713
16714 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16715 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016716 char_u *retvar;
16717
Bram Moolenaar33570922005-01-25 22:26:29 +000016718 v.di_tv.v_type = VAR_STRING;
16719 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016720 retvar = get_tv_string_chk(&argvars[1]);
16721 if (retvar != NULL)
16722 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016723 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016724 }
16725#else
16726 rettv->vval.v_number = -1;
16727#endif
16728}
16729
Bram Moolenaar0d660222005-01-07 21:51:51 +000016730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016731f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016732{
16733 char_u *r = NULL;
16734
16735#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016736 char_u *serverid = get_tv_string_chk(&argvars[0]);
16737
16738 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016739 {
16740# ifdef WIN32
16741 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016742 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016743
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016744 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016745 if (n != 0)
16746 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16747 if (r == NULL)
16748# else
16749 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016750 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016751# endif
16752 EMSG(_("E277: Unable to read a server reply"));
16753 }
16754#endif
16755 rettv->v_type = VAR_STRING;
16756 rettv->vval.v_string = r;
16757}
16758
16759/*
16760 * "remote_send()" function
16761 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016763f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016764{
16765 rettv->v_type = VAR_STRING;
16766 rettv->vval.v_string = NULL;
16767#ifdef FEAT_CLIENTSERVER
16768 remote_common(argvars, rettv, FALSE);
16769#endif
16770}
16771
16772/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016773 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016774 */
16775 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016776f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016777{
Bram Moolenaar33570922005-01-25 22:26:29 +000016778 list_T *l;
16779 listitem_T *item, *item2;
16780 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016781 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016782 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016783 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016784 dict_T *d;
16785 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016786 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016787
Bram Moolenaar8c711452005-01-14 21:53:12 +000016788 if (argvars[0].v_type == VAR_DICT)
16789 {
16790 if (argvars[2].v_type != VAR_UNKNOWN)
16791 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016792 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016793 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016794 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016795 key = get_tv_string_chk(&argvars[1]);
16796 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016797 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016798 di = dict_find(d, key, -1);
16799 if (di == NULL)
16800 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016801 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16802 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016803 {
16804 *rettv = di->di_tv;
16805 init_tv(&di->di_tv);
16806 dictitem_remove(d, di);
16807 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016808 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016809 }
16810 }
16811 else if (argvars[0].v_type != VAR_LIST)
16812 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016813 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016814 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016815 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016816 int error = FALSE;
16817
16818 idx = get_tv_number_chk(&argvars[1], &error);
16819 if (error)
16820 ; /* type error: do nothing, errmsg already given */
16821 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016822 EMSGN(_(e_listidx), idx);
16823 else
16824 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016825 if (argvars[2].v_type == VAR_UNKNOWN)
16826 {
16827 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016828 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016829 *rettv = item->li_tv;
16830 vim_free(item);
16831 }
16832 else
16833 {
16834 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016835 end = get_tv_number_chk(&argvars[2], &error);
16836 if (error)
16837 ; /* type error: do nothing */
16838 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016839 EMSGN(_(e_listidx), end);
16840 else
16841 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016842 int cnt = 0;
16843
16844 for (li = item; li != NULL; li = li->li_next)
16845 {
16846 ++cnt;
16847 if (li == item2)
16848 break;
16849 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016850 if (li == NULL) /* didn't find "item2" after "item" */
16851 EMSG(_(e_invrange));
16852 else
16853 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016854 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016855 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016856 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016857 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016858 l->lv_first = item;
16859 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016860 item->li_prev = NULL;
16861 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016862 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016863 }
16864 }
16865 }
16866 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016867 }
16868 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016869}
16870
16871/*
16872 * "rename({from}, {to})" function
16873 */
16874 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016875f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876{
16877 char_u buf[NUMBUFLEN];
16878
16879 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016880 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016881 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016882 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16883 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016884}
16885
16886/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016887 * "repeat()" function
16888 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016889 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016890f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016891{
16892 char_u *p;
16893 int n;
16894 int slen;
16895 int len;
16896 char_u *r;
16897 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016898
16899 n = get_tv_number(&argvars[1]);
16900 if (argvars[0].v_type == VAR_LIST)
16901 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016902 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016903 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016904 if (list_extend(rettv->vval.v_list,
16905 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016906 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016907 }
16908 else
16909 {
16910 p = get_tv_string(&argvars[0]);
16911 rettv->v_type = VAR_STRING;
16912 rettv->vval.v_string = NULL;
16913
16914 slen = (int)STRLEN(p);
16915 len = slen * n;
16916 if (len <= 0)
16917 return;
16918
16919 r = alloc(len + 1);
16920 if (r != NULL)
16921 {
16922 for (i = 0; i < n; i++)
16923 mch_memmove(r + i * slen, p, (size_t)slen);
16924 r[len] = NUL;
16925 }
16926
16927 rettv->vval.v_string = r;
16928 }
16929}
16930
16931/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016932 * "resolve()" function
16933 */
16934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016935f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936{
16937 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016938#ifdef HAVE_READLINK
16939 char_u *buf = NULL;
16940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016941
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016942 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016943#ifdef FEAT_SHORTCUT
16944 {
16945 char_u *v = NULL;
16946
16947 v = mch_resolve_shortcut(p);
16948 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016949 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016951 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016952 }
16953#else
16954# ifdef HAVE_READLINK
16955 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016956 char_u *cpy;
16957 int len;
16958 char_u *remain = NULL;
16959 char_u *q;
16960 int is_relative_to_current = FALSE;
16961 int has_trailing_pathsep = FALSE;
16962 int limit = 100;
16963
16964 p = vim_strsave(p);
16965
16966 if (p[0] == '.' && (vim_ispathsep(p[1])
16967 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16968 is_relative_to_current = TRUE;
16969
16970 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016971 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016972 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016973 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016974 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976
16977 q = getnextcomp(p);
16978 if (*q != NUL)
16979 {
16980 /* Separate the first path component in "p", and keep the
16981 * remainder (beginning with the path separator). */
16982 remain = vim_strsave(q - 1);
16983 q[-1] = NUL;
16984 }
16985
Bram Moolenaard9462e32011-04-11 21:35:11 +020016986 buf = alloc(MAXPATHL + 1);
16987 if (buf == NULL)
16988 goto fail;
16989
Bram Moolenaar071d4272004-06-13 20:20:40 +000016990 for (;;)
16991 {
16992 for (;;)
16993 {
16994 len = readlink((char *)p, (char *)buf, MAXPATHL);
16995 if (len <= 0)
16996 break;
16997 buf[len] = NUL;
16998
16999 if (limit-- == 0)
17000 {
17001 vim_free(p);
17002 vim_free(remain);
17003 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017004 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017005 goto fail;
17006 }
17007
17008 /* Ensure that the result will have a trailing path separator
17009 * if the argument has one. */
17010 if (remain == NULL && has_trailing_pathsep)
17011 add_pathsep(buf);
17012
17013 /* Separate the first path component in the link value and
17014 * concatenate the remainders. */
17015 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
17016 if (*q != NUL)
17017 {
17018 if (remain == NULL)
17019 remain = vim_strsave(q - 1);
17020 else
17021 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000017022 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017023 if (cpy != NULL)
17024 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017025 vim_free(remain);
17026 remain = cpy;
17027 }
17028 }
17029 q[-1] = NUL;
17030 }
17031
17032 q = gettail(p);
17033 if (q > p && *q == NUL)
17034 {
17035 /* Ignore trailing path separator. */
17036 q[-1] = NUL;
17037 q = gettail(p);
17038 }
17039 if (q > p && !mch_isFullName(buf))
17040 {
17041 /* symlink is relative to directory of argument */
17042 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
17043 if (cpy != NULL)
17044 {
17045 STRCPY(cpy, p);
17046 STRCPY(gettail(cpy), buf);
17047 vim_free(p);
17048 p = cpy;
17049 }
17050 }
17051 else
17052 {
17053 vim_free(p);
17054 p = vim_strsave(buf);
17055 }
17056 }
17057
17058 if (remain == NULL)
17059 break;
17060
17061 /* Append the first path component of "remain" to "p". */
17062 q = getnextcomp(remain + 1);
17063 len = q - remain - (*q != NUL);
17064 cpy = vim_strnsave(p, STRLEN(p) + len);
17065 if (cpy != NULL)
17066 {
17067 STRNCAT(cpy, remain, len);
17068 vim_free(p);
17069 p = cpy;
17070 }
17071 /* Shorten "remain". */
17072 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017073 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074 else
17075 {
17076 vim_free(remain);
17077 remain = NULL;
17078 }
17079 }
17080
17081 /* If the result is a relative path name, make it explicitly relative to
17082 * the current directory if and only if the argument had this form. */
17083 if (!vim_ispathsep(*p))
17084 {
17085 if (is_relative_to_current
17086 && *p != NUL
17087 && !(p[0] == '.'
17088 && (p[1] == NUL
17089 || vim_ispathsep(p[1])
17090 || (p[1] == '.'
17091 && (p[2] == NUL
17092 || vim_ispathsep(p[2]))))))
17093 {
17094 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017095 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017096 if (cpy != NULL)
17097 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017098 vim_free(p);
17099 p = cpy;
17100 }
17101 }
17102 else if (!is_relative_to_current)
17103 {
17104 /* Strip leading "./". */
17105 q = p;
17106 while (q[0] == '.' && vim_ispathsep(q[1]))
17107 q += 2;
17108 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017109 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017110 }
17111 }
17112
17113 /* Ensure that the result will have no trailing path separator
17114 * if the argument had none. But keep "/" or "//". */
17115 if (!has_trailing_pathsep)
17116 {
17117 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017118 if (after_pathsep(p, q))
17119 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017120 }
17121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017122 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017123 }
17124# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017125 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017126# endif
17127#endif
17128
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017129 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017130
17131#ifdef HAVE_READLINK
17132fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017133 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017134#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017135 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017136}
17137
17138/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017139 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140 */
17141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017142f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017143{
Bram Moolenaar33570922005-01-25 22:26:29 +000017144 list_T *l;
17145 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017146
Bram Moolenaar0d660222005-01-07 21:51:51 +000017147 if (argvars[0].v_type != VAR_LIST)
17148 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017149 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017150 && !tv_check_lock(l->lv_lock,
17151 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017152 {
17153 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017154 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017155 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017156 while (li != NULL)
17157 {
17158 ni = li->li_prev;
17159 list_append(l, li);
17160 li = ni;
17161 }
17162 rettv->vval.v_list = l;
17163 rettv->v_type = VAR_LIST;
17164 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017165 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017166 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017167}
17168
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017169#define SP_NOMOVE 0x01 /* don't move cursor */
17170#define SP_REPEAT 0x02 /* repeat to find outer pair */
17171#define SP_RETCOUNT 0x04 /* return matchcount */
17172#define SP_SETPCMARK 0x08 /* set previous context mark */
17173#define SP_START 0x10 /* accept match at start position */
17174#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17175#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017176#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017177
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017178static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017179
17180/*
17181 * Get flags for a search function.
17182 * Possibly sets "p_ws".
17183 * Returns BACKWARD, FORWARD or zero (for an error).
17184 */
17185 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017186get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017187{
17188 int dir = FORWARD;
17189 char_u *flags;
17190 char_u nbuf[NUMBUFLEN];
17191 int mask;
17192
17193 if (varp->v_type != VAR_UNKNOWN)
17194 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017195 flags = get_tv_string_buf_chk(varp, nbuf);
17196 if (flags == NULL)
17197 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017198 while (*flags != NUL)
17199 {
17200 switch (*flags)
17201 {
17202 case 'b': dir = BACKWARD; break;
17203 case 'w': p_ws = TRUE; break;
17204 case 'W': p_ws = FALSE; break;
17205 default: mask = 0;
17206 if (flagsp != NULL)
17207 switch (*flags)
17208 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017209 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017210 case 'e': mask = SP_END; break;
17211 case 'm': mask = SP_RETCOUNT; break;
17212 case 'n': mask = SP_NOMOVE; break;
17213 case 'p': mask = SP_SUBPAT; break;
17214 case 'r': mask = SP_REPEAT; break;
17215 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017216 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017217 }
17218 if (mask == 0)
17219 {
17220 EMSG2(_(e_invarg2), flags);
17221 dir = 0;
17222 }
17223 else
17224 *flagsp |= mask;
17225 }
17226 if (dir == 0)
17227 break;
17228 ++flags;
17229 }
17230 }
17231 return dir;
17232}
17233
Bram Moolenaar071d4272004-06-13 20:20:40 +000017234/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017235 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017236 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017237 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017238search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017239{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017240 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017241 char_u *pat;
17242 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017243 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244 int save_p_ws = p_ws;
17245 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017246 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017247 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017248 proftime_T tm;
17249#ifdef FEAT_RELTIME
17250 long time_limit = 0;
17251#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017252 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017253 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017254
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017255 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017256 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017257 if (dir == 0)
17258 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017259 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017260 if (flags & SP_START)
17261 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017262 if (flags & SP_END)
17263 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017264 if (flags & SP_COLUMN)
17265 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017266
Bram Moolenaar76929292008-01-06 19:07:36 +000017267 /* Optional arguments: line number to stop searching and timeout. */
17268 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017269 {
17270 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17271 if (lnum_stop < 0)
17272 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017273#ifdef FEAT_RELTIME
17274 if (argvars[3].v_type != VAR_UNKNOWN)
17275 {
17276 time_limit = get_tv_number_chk(&argvars[3], NULL);
17277 if (time_limit < 0)
17278 goto theend;
17279 }
17280#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017281 }
17282
Bram Moolenaar76929292008-01-06 19:07:36 +000017283#ifdef FEAT_RELTIME
17284 /* Set the time limit, if there is one. */
17285 profile_setlimit(time_limit, &tm);
17286#endif
17287
Bram Moolenaar231334e2005-07-25 20:46:57 +000017288 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017289 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017290 * Check to make sure only those flags are set.
17291 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17292 * flags cannot be set. Check for that condition also.
17293 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017294 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017295 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017296 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017297 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017298 goto theend;
17299 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017300
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017301 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017302 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017303 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017304 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017305 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017306 if (flags & SP_SUBPAT)
17307 retval = subpatnum;
17308 else
17309 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017310 if (flags & SP_SETPCMARK)
17311 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017313 if (match_pos != NULL)
17314 {
17315 /* Store the match cursor position */
17316 match_pos->lnum = pos.lnum;
17317 match_pos->col = pos.col + 1;
17318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017319 /* "/$" will put the cursor after the end of the line, may need to
17320 * correct that here */
17321 check_cursor();
17322 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017323
17324 /* If 'n' flag is used: restore cursor position. */
17325 if (flags & SP_NOMOVE)
17326 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017327 else
17328 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017329theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017330 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017331
17332 return retval;
17333}
17334
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017335#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017336
17337/*
17338 * round() is not in C90, use ceil() or floor() instead.
17339 */
17340 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017341vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017342{
17343 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17344}
17345
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017346/*
17347 * "round({float})" function
17348 */
17349 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017350f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017351{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017352 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017353
17354 rettv->v_type = VAR_FLOAT;
17355 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017356 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017357 else
17358 rettv->vval.v_float = 0.0;
17359}
17360#endif
17361
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017362/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017363 * "screenattr()" function
17364 */
17365 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017366f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017367{
17368 int row;
17369 int col;
17370 int c;
17371
17372 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17373 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17374 if (row < 0 || row >= screen_Rows
17375 || col < 0 || col >= screen_Columns)
17376 c = -1;
17377 else
17378 c = ScreenAttrs[LineOffset[row] + col];
17379 rettv->vval.v_number = c;
17380}
17381
17382/*
17383 * "screenchar()" function
17384 */
17385 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017386f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017387{
17388 int row;
17389 int col;
17390 int off;
17391 int c;
17392
17393 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17394 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17395 if (row < 0 || row >= screen_Rows
17396 || col < 0 || col >= screen_Columns)
17397 c = -1;
17398 else
17399 {
17400 off = LineOffset[row] + col;
17401#ifdef FEAT_MBYTE
17402 if (enc_utf8 && ScreenLinesUC[off] != 0)
17403 c = ScreenLinesUC[off];
17404 else
17405#endif
17406 c = ScreenLines[off];
17407 }
17408 rettv->vval.v_number = c;
17409}
17410
17411/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017412 * "screencol()" function
17413 *
17414 * First column is 1 to be consistent with virtcol().
17415 */
17416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017418{
17419 rettv->vval.v_number = screen_screencol() + 1;
17420}
17421
17422/*
17423 * "screenrow()" function
17424 */
17425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017426f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017427{
17428 rettv->vval.v_number = screen_screenrow() + 1;
17429}
17430
17431/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017432 * "search()" function
17433 */
17434 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017435f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017436{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017437 int flags = 0;
17438
17439 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017440}
17441
Bram Moolenaar071d4272004-06-13 20:20:40 +000017442/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017443 * "searchdecl()" function
17444 */
17445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017446f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017447{
17448 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017449 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017450 int error = FALSE;
17451 char_u *name;
17452
17453 rettv->vval.v_number = 1; /* default: FAIL */
17454
17455 name = get_tv_string_chk(&argvars[0]);
17456 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017457 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017458 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017459 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17460 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17461 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017462 if (!error && name != NULL)
17463 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017464 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017465}
17466
17467/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017468 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017469 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017470 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017471searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017472{
17473 char_u *spat, *mpat, *epat;
17474 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017475 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017476 int dir;
17477 int flags = 0;
17478 char_u nbuf1[NUMBUFLEN];
17479 char_u nbuf2[NUMBUFLEN];
17480 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017481 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017482 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017483 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017484
Bram Moolenaar071d4272004-06-13 20:20:40 +000017485 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017486 spat = get_tv_string_chk(&argvars[0]);
17487 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17488 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17489 if (spat == NULL || mpat == NULL || epat == NULL)
17490 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017491
Bram Moolenaar071d4272004-06-13 20:20:40 +000017492 /* Handle the optional fourth argument: flags */
17493 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017494 if (dir == 0)
17495 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017496
17497 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017498 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17499 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017500 if ((flags & (SP_END | SP_SUBPAT)) != 0
17501 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017502 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017503 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017504 goto theend;
17505 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017506
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017507 /* Using 'r' implies 'W', otherwise it doesn't work. */
17508 if (flags & SP_REPEAT)
17509 p_ws = FALSE;
17510
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017511 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017512 if (argvars[3].v_type == VAR_UNKNOWN
17513 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017514 skip = (char_u *)"";
17515 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017516 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017517 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017518 if (argvars[5].v_type != VAR_UNKNOWN)
17519 {
17520 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17521 if (lnum_stop < 0)
17522 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017523#ifdef FEAT_RELTIME
17524 if (argvars[6].v_type != VAR_UNKNOWN)
17525 {
17526 time_limit = get_tv_number_chk(&argvars[6], NULL);
17527 if (time_limit < 0)
17528 goto theend;
17529 }
17530#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017531 }
17532 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017533 if (skip == NULL)
17534 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017535
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017536 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017537 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017538
17539theend:
17540 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017541
17542 return retval;
17543}
17544
17545/*
17546 * "searchpair()" function
17547 */
17548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017549f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017550{
17551 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17552}
17553
17554/*
17555 * "searchpairpos()" function
17556 */
17557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017558f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017559{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017560 pos_T match_pos;
17561 int lnum = 0;
17562 int col = 0;
17563
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017564 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017565 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017566
17567 if (searchpair_cmn(argvars, &match_pos) > 0)
17568 {
17569 lnum = match_pos.lnum;
17570 col = match_pos.col;
17571 }
17572
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017573 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17574 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017575}
17576
17577/*
17578 * Search for a start/middle/end thing.
17579 * Used by searchpair(), see its documentation for the details.
17580 * Returns 0 or -1 for no match,
17581 */
17582 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017583do_searchpair(
17584 char_u *spat, /* start pattern */
17585 char_u *mpat, /* middle pattern */
17586 char_u *epat, /* end pattern */
17587 int dir, /* BACKWARD or FORWARD */
17588 char_u *skip, /* skip expression */
17589 int flags, /* SP_SETPCMARK and other SP_ values */
17590 pos_T *match_pos,
17591 linenr_T lnum_stop, /* stop at this line if not zero */
17592 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017593{
17594 char_u *save_cpo;
17595 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17596 long retval = 0;
17597 pos_T pos;
17598 pos_T firstpos;
17599 pos_T foundpos;
17600 pos_T save_cursor;
17601 pos_T save_pos;
17602 int n;
17603 int r;
17604 int nest = 1;
17605 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017606 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017607 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017608
17609 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17610 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017611 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017612
Bram Moolenaar76929292008-01-06 19:07:36 +000017613#ifdef FEAT_RELTIME
17614 /* Set the time limit, if there is one. */
17615 profile_setlimit(time_limit, &tm);
17616#endif
17617
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017618 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17619 * start/middle/end (pat3, for the top pair). */
17620 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17621 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17622 if (pat2 == NULL || pat3 == NULL)
17623 goto theend;
17624 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17625 if (*mpat == NUL)
17626 STRCPY(pat3, pat2);
17627 else
17628 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17629 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017630 if (flags & SP_START)
17631 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017632
Bram Moolenaar071d4272004-06-13 20:20:40 +000017633 save_cursor = curwin->w_cursor;
17634 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017635 clearpos(&firstpos);
17636 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017637 pat = pat3;
17638 for (;;)
17639 {
17640 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017641 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017642 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17643 /* didn't find it or found the first match again: FAIL */
17644 break;
17645
17646 if (firstpos.lnum == 0)
17647 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017648 if (equalpos(pos, foundpos))
17649 {
17650 /* Found the same position again. Can happen with a pattern that
17651 * has "\zs" at the end and searching backwards. Advance one
17652 * character and try again. */
17653 if (dir == BACKWARD)
17654 decl(&pos);
17655 else
17656 incl(&pos);
17657 }
17658 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017659
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017660 /* clear the start flag to avoid getting stuck here */
17661 options &= ~SEARCH_START;
17662
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 /* If the skip pattern matches, ignore this match. */
17664 if (*skip != NUL)
17665 {
17666 save_pos = curwin->w_cursor;
17667 curwin->w_cursor = pos;
17668 r = eval_to_bool(skip, &err, NULL, FALSE);
17669 curwin->w_cursor = save_pos;
17670 if (err)
17671 {
17672 /* Evaluating {skip} caused an error, break here. */
17673 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017674 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017675 break;
17676 }
17677 if (r)
17678 continue;
17679 }
17680
17681 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17682 {
17683 /* Found end when searching backwards or start when searching
17684 * forward: nested pair. */
17685 ++nest;
17686 pat = pat2; /* nested, don't search for middle */
17687 }
17688 else
17689 {
17690 /* Found end when searching forward or start when searching
17691 * backward: end of (nested) pair; or found middle in outer pair. */
17692 if (--nest == 1)
17693 pat = pat3; /* outer level, search for middle */
17694 }
17695
17696 if (nest == 0)
17697 {
17698 /* Found the match: return matchcount or line number. */
17699 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017700 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017702 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017703 if (flags & SP_SETPCMARK)
17704 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017705 curwin->w_cursor = pos;
17706 if (!(flags & SP_REPEAT))
17707 break;
17708 nest = 1; /* search for next unmatched */
17709 }
17710 }
17711
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017712 if (match_pos != NULL)
17713 {
17714 /* Store the match cursor position */
17715 match_pos->lnum = curwin->w_cursor.lnum;
17716 match_pos->col = curwin->w_cursor.col + 1;
17717 }
17718
Bram Moolenaar071d4272004-06-13 20:20:40 +000017719 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017720 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017721 curwin->w_cursor = save_cursor;
17722
17723theend:
17724 vim_free(pat2);
17725 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017726 if (p_cpo == empty_option)
17727 p_cpo = save_cpo;
17728 else
17729 /* Darn, evaluating the {skip} expression changed the value. */
17730 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017731
17732 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017733}
17734
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017735/*
17736 * "searchpos()" function
17737 */
17738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017740{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017741 pos_T match_pos;
17742 int lnum = 0;
17743 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017744 int n;
17745 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017746
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017747 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017748 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017749
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017750 n = search_cmn(argvars, &match_pos, &flags);
17751 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017752 {
17753 lnum = match_pos.lnum;
17754 col = match_pos.col;
17755 }
17756
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017757 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17758 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017759 if (flags & SP_SUBPAT)
17760 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017761}
17762
Bram Moolenaar0d660222005-01-07 21:51:51 +000017763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017764f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017765{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017766#ifdef FEAT_CLIENTSERVER
17767 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 char_u *server = get_tv_string_chk(&argvars[0]);
17769 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017770
Bram Moolenaar0d660222005-01-07 21:51:51 +000017771 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017772 if (server == NULL || reply == NULL)
17773 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017774 if (check_restricted() || check_secure())
17775 return;
17776# ifdef FEAT_X11
17777 if (check_connection() == FAIL)
17778 return;
17779# endif
17780
17781 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017782 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017783 EMSG(_("E258: Unable to send to client"));
17784 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017785 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017786 rettv->vval.v_number = 0;
17787#else
17788 rettv->vval.v_number = -1;
17789#endif
17790}
17791
Bram Moolenaar0d660222005-01-07 21:51:51 +000017792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017793f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017794{
17795 char_u *r = NULL;
17796
17797#ifdef FEAT_CLIENTSERVER
17798# ifdef WIN32
17799 r = serverGetVimNames();
17800# else
17801 make_connection();
17802 if (X_DISPLAY != NULL)
17803 r = serverGetVimNames(X_DISPLAY);
17804# endif
17805#endif
17806 rettv->v_type = VAR_STRING;
17807 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017808}
17809
17810/*
17811 * "setbufvar()" function
17812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017814f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017815{
17816 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017817 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017818 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017819 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017820 char_u nbuf[NUMBUFLEN];
17821
17822 if (check_restricted() || check_secure())
17823 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17825 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017826 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017827 varp = &argvars[2];
17828
17829 if (buf != NULL && varname != NULL && varp != NULL)
17830 {
17831 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017832 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017833
17834 if (*varname == '&')
17835 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017836 long numval;
17837 char_u *strval;
17838 int error = FALSE;
17839
Bram Moolenaar071d4272004-06-13 20:20:40 +000017840 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017841 numval = get_tv_number_chk(varp, &error);
17842 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017843 if (!error && strval != NULL)
17844 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017845 }
17846 else
17847 {
17848 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17849 if (bufvarname != NULL)
17850 {
17851 STRCPY(bufvarname, "b:");
17852 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017853 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017854 vim_free(bufvarname);
17855 }
17856 }
17857
17858 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017859 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017861}
17862
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017863 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017864f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017865{
17866 dict_T *d;
17867 dictitem_T *di;
17868 char_u *csearch;
17869
17870 if (argvars[0].v_type != VAR_DICT)
17871 {
17872 EMSG(_(e_dictreq));
17873 return;
17874 }
17875
17876 if ((d = argvars[0].vval.v_dict) != NULL)
17877 {
17878 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17879 if (csearch != NULL)
17880 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017881#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017882 if (enc_utf8)
17883 {
17884 int pcc[MAX_MCO];
17885 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017886
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017887 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17888 }
17889 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017890#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017891 set_last_csearch(PTR2CHAR(csearch),
17892 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017893 }
17894
17895 di = dict_find(d, (char_u *)"forward", -1);
17896 if (di != NULL)
17897 set_csearch_direction(get_tv_number(&di->di_tv)
17898 ? FORWARD : BACKWARD);
17899
17900 di = dict_find(d, (char_u *)"until", -1);
17901 if (di != NULL)
17902 set_csearch_until(!!get_tv_number(&di->di_tv));
17903 }
17904}
17905
Bram Moolenaar071d4272004-06-13 20:20:40 +000017906/*
17907 * "setcmdpos()" function
17908 */
17909 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017910f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017911{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017912 int pos = (int)get_tv_number(&argvars[0]) - 1;
17913
17914 if (pos >= 0)
17915 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916}
17917
17918/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017919 * "setfperm({fname}, {mode})" function
17920 */
17921 static void
17922f_setfperm(typval_T *argvars, typval_T *rettv)
17923{
17924 char_u *fname;
17925 char_u modebuf[NUMBUFLEN];
17926 char_u *mode_str;
17927 int i;
17928 int mask;
17929 int mode = 0;
17930
17931 rettv->vval.v_number = 0;
17932 fname = get_tv_string_chk(&argvars[0]);
17933 if (fname == NULL)
17934 return;
17935 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17936 if (mode_str == NULL)
17937 return;
17938 if (STRLEN(mode_str) != 9)
17939 {
17940 EMSG2(_(e_invarg2), mode_str);
17941 return;
17942 }
17943
17944 mask = 1;
17945 for (i = 8; i >= 0; --i)
17946 {
17947 if (mode_str[i] != '-')
17948 mode |= mask;
17949 mask = mask << 1;
17950 }
17951 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17952}
17953
17954/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017955 * "setline()" function
17956 */
17957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017958f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017959{
17960 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017961 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017962 list_T *l = NULL;
17963 listitem_T *li = NULL;
17964 long added = 0;
17965 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017966
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017967 lnum = get_tv_lnum(&argvars[0]);
17968 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017969 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017970 l = argvars[1].vval.v_list;
17971 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017973 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017974 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017975
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017976 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017977 for (;;)
17978 {
17979 if (l != NULL)
17980 {
17981 /* list argument, get next string */
17982 if (li == NULL)
17983 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017984 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017985 li = li->li_next;
17986 }
17987
17988 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017989 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017990 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017991
17992 /* When coming here from Insert mode, sync undo, so that this can be
17993 * undone separately from what was previously inserted. */
17994 if (u_sync_once == 2)
17995 {
17996 u_sync_once = 1; /* notify that u_sync() was called */
17997 u_sync(TRUE);
17998 }
17999
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018000 if (lnum <= curbuf->b_ml.ml_line_count)
18001 {
18002 /* existing line, replace it */
18003 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
18004 {
18005 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000018006 if (lnum == curwin->w_cursor.lnum)
18007 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018008 rettv->vval.v_number = 0; /* OK */
18009 }
18010 }
18011 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
18012 {
18013 /* lnum is one past the last line, append the line */
18014 ++added;
18015 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
18016 rettv->vval.v_number = 0; /* OK */
18017 }
18018
18019 if (l == NULL) /* only one string argument */
18020 break;
18021 ++lnum;
18022 }
18023
18024 if (added > 0)
18025 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018026}
18027
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018028static 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 +000018029
Bram Moolenaar071d4272004-06-13 20:20:40 +000018030/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018031 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000018032 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000018033 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018034set_qf_ll_list(
18035 win_T *wp UNUSED,
18036 typval_T *list_arg UNUSED,
18037 typval_T *action_arg UNUSED,
18038 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018039{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018040#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018041 char_u *act;
18042 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000018043#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018044
Bram Moolenaar2641f772005-03-25 21:58:17 +000018045 rettv->vval.v_number = -1;
18046
18047#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018048 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018049 EMSG(_(e_listreq));
18050 else
18051 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018052 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000018053
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018054 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018055 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018056 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018057 if (act == NULL)
18058 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018059 if (*act == 'a' || *act == 'r')
18060 action = *act;
18061 }
18062
Bram Moolenaar81484f42012-12-05 15:16:47 +010018063 if (l != NULL && set_errorlist(wp, l, action,
18064 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018065 rettv->vval.v_number = 0;
18066 }
18067#endif
18068}
18069
18070/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018071 * "setloclist()" function
18072 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018074f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018075{
18076 win_T *win;
18077
18078 rettv->vval.v_number = -1;
18079
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018080 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018081 if (win != NULL)
18082 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18083}
18084
18085/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018086 * "setmatches()" function
18087 */
18088 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018089f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018090{
18091#ifdef FEAT_SEARCH_EXTRA
18092 list_T *l;
18093 listitem_T *li;
18094 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018095 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018096
18097 rettv->vval.v_number = -1;
18098 if (argvars[0].v_type != VAR_LIST)
18099 {
18100 EMSG(_(e_listreq));
18101 return;
18102 }
18103 if ((l = argvars[0].vval.v_list) != NULL)
18104 {
18105
18106 /* To some extent make sure that we are dealing with a list from
18107 * "getmatches()". */
18108 li = l->lv_first;
18109 while (li != NULL)
18110 {
18111 if (li->li_tv.v_type != VAR_DICT
18112 || (d = li->li_tv.vval.v_dict) == NULL)
18113 {
18114 EMSG(_(e_invarg));
18115 return;
18116 }
18117 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018118 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18119 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018120 && dict_find(d, (char_u *)"priority", -1) != NULL
18121 && dict_find(d, (char_u *)"id", -1) != NULL))
18122 {
18123 EMSG(_(e_invarg));
18124 return;
18125 }
18126 li = li->li_next;
18127 }
18128
18129 clear_matches(curwin);
18130 li = l->lv_first;
18131 while (li != NULL)
18132 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018133 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018134 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018135 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018136 char_u *group;
18137 int priority;
18138 int id;
18139 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018140
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018141 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018142 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18143 {
18144 if (s == NULL)
18145 {
18146 s = list_alloc();
18147 if (s == NULL)
18148 return;
18149 }
18150
18151 /* match from matchaddpos() */
18152 for (i = 1; i < 9; i++)
18153 {
18154 sprintf((char *)buf, (char *)"pos%d", i);
18155 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18156 {
18157 if (di->di_tv.v_type != VAR_LIST)
18158 return;
18159
18160 list_append_tv(s, &di->di_tv);
18161 s->lv_refcount++;
18162 }
18163 else
18164 break;
18165 }
18166 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018167
18168 group = get_dict_string(d, (char_u *)"group", FALSE);
18169 priority = (int)get_dict_number(d, (char_u *)"priority");
18170 id = (int)get_dict_number(d, (char_u *)"id");
18171 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18172 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18173 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018174 if (i == 0)
18175 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018176 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018177 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018178 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018179 }
18180 else
18181 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018182 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018183 list_unref(s);
18184 s = NULL;
18185 }
18186
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018187 li = li->li_next;
18188 }
18189 rettv->vval.v_number = 0;
18190 }
18191#endif
18192}
18193
18194/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018195 * "setpos()" function
18196 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018198f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018199{
18200 pos_T pos;
18201 int fnum;
18202 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018203 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018204
Bram Moolenaar08250432008-02-13 11:42:46 +000018205 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018206 name = get_tv_string_chk(argvars);
18207 if (name != NULL)
18208 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018209 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018210 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018211 if (--pos.col < 0)
18212 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018213 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018214 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018215 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018216 if (fnum == curbuf->b_fnum)
18217 {
18218 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018219 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018220 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018221 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018222 curwin->w_set_curswant = FALSE;
18223 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018224 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018225 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018226 }
18227 else
18228 EMSG(_(e_invarg));
18229 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018230 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18231 {
18232 /* set mark */
18233 if (setmark_pos(name[1], &pos, fnum) == OK)
18234 rettv->vval.v_number = 0;
18235 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018236 else
18237 EMSG(_(e_invarg));
18238 }
18239 }
18240}
18241
18242/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018243 * "setqflist()" function
18244 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018246f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018247{
18248 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18249}
18250
18251/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018252 * "setreg()" function
18253 */
18254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018255f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018256{
18257 int regname;
18258 char_u *strregname;
18259 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018260 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018261 int append;
18262 char_u yank_type;
18263 long block_len;
18264
18265 block_len = -1;
18266 yank_type = MAUTO;
18267 append = FALSE;
18268
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018269 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018270 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018271
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018272 if (strregname == NULL)
18273 return; /* type error; errmsg already given */
18274 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018275 if (regname == 0 || regname == '@')
18276 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018277
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018278 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018279 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018280 stropt = get_tv_string_chk(&argvars[2]);
18281 if (stropt == NULL)
18282 return; /* type error */
18283 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018284 switch (*stropt)
18285 {
18286 case 'a': case 'A': /* append */
18287 append = TRUE;
18288 break;
18289 case 'v': case 'c': /* character-wise selection */
18290 yank_type = MCHAR;
18291 break;
18292 case 'V': case 'l': /* line-wise selection */
18293 yank_type = MLINE;
18294 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018295 case 'b': case Ctrl_V: /* block-wise selection */
18296 yank_type = MBLOCK;
18297 if (VIM_ISDIGIT(stropt[1]))
18298 {
18299 ++stropt;
18300 block_len = getdigits(&stropt) - 1;
18301 --stropt;
18302 }
18303 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 }
18305 }
18306
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018307 if (argvars[1].v_type == VAR_LIST)
18308 {
18309 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018310 char_u **allocval;
18311 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018312 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018313 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018314 int len = argvars[1].vval.v_list->lv_len;
18315 listitem_T *li;
18316
Bram Moolenaar7d647822014-04-05 21:28:56 +020018317 /* First half: use for pointers to result lines; second half: use for
18318 * pointers to allocated copies. */
18319 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018320 if (lstval == NULL)
18321 return;
18322 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018323 allocval = lstval + len + 2;
18324 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018325
18326 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18327 li = li->li_next)
18328 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018329 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018330 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018331 goto free_lstval;
18332 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018333 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018334 /* Need to make a copy, next get_tv_string_buf_chk() will
18335 * overwrite the string. */
18336 strval = vim_strsave(buf);
18337 if (strval == NULL)
18338 goto free_lstval;
18339 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018340 }
18341 *curval++ = strval;
18342 }
18343 *curval++ = NULL;
18344
18345 write_reg_contents_lst(regname, lstval, -1,
18346 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018347free_lstval:
18348 while (curallocval > allocval)
18349 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018350 vim_free(lstval);
18351 }
18352 else
18353 {
18354 strval = get_tv_string_chk(&argvars[1]);
18355 if (strval == NULL)
18356 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018357 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018358 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018359 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018360 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018361}
18362
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018363/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018364 * "settabvar()" function
18365 */
18366 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018367f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018368{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018369#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018370 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018371 tabpage_T *tp;
18372#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018373 char_u *varname, *tabvarname;
18374 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018375
18376 rettv->vval.v_number = 0;
18377
18378 if (check_restricted() || check_secure())
18379 return;
18380
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018381#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018382 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018383#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018384 varname = get_tv_string_chk(&argvars[1]);
18385 varp = &argvars[2];
18386
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018387 if (varname != NULL && varp != NULL
18388#ifdef FEAT_WINDOWS
18389 && tp != NULL
18390#endif
18391 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018392 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018393#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018394 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018395 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018396#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018397
18398 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18399 if (tabvarname != NULL)
18400 {
18401 STRCPY(tabvarname, "t:");
18402 STRCPY(tabvarname + 2, varname);
18403 set_var(tabvarname, varp, TRUE);
18404 vim_free(tabvarname);
18405 }
18406
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018407#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018408 /* Restore current tabpage */
18409 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018410 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018411#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018412 }
18413}
18414
18415/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018416 * "settabwinvar()" function
18417 */
18418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018419f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018420{
18421 setwinvar(argvars, rettv, 1);
18422}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018423
18424/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018425 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018426 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018427 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018428f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018429{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018430 setwinvar(argvars, rettv, 0);
18431}
18432
18433/*
18434 * "setwinvar()" and "settabwinvar()" functions
18435 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018436
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018437 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018438setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018439{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 win_T *win;
18441#ifdef FEAT_WINDOWS
18442 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018443 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018444 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445#endif
18446 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018447 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018448 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018449 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018450
18451 if (check_restricted() || check_secure())
18452 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018453
18454#ifdef FEAT_WINDOWS
18455 if (off == 1)
18456 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18457 else
18458 tp = curtab;
18459#endif
18460 win = find_win_by_nr(&argvars[off], tp);
18461 varname = get_tv_string_chk(&argvars[off + 1]);
18462 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463
18464 if (win != NULL && varname != NULL && varp != NULL)
18465 {
18466#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018467 need_switch_win = !(tp == curtab && win == curwin);
18468 if (!need_switch_win
18469 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018471 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018472 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018473 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018474 long numval;
18475 char_u *strval;
18476 int error = FALSE;
18477
18478 ++varname;
18479 numval = get_tv_number_chk(varp, &error);
18480 strval = get_tv_string_buf_chk(varp, nbuf);
18481 if (!error && strval != NULL)
18482 set_option_value(varname, numval, strval, OPT_LOCAL);
18483 }
18484 else
18485 {
18486 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18487 if (winvarname != NULL)
18488 {
18489 STRCPY(winvarname, "w:");
18490 STRCPY(winvarname + 2, varname);
18491 set_var(winvarname, varp, TRUE);
18492 vim_free(winvarname);
18493 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018494 }
18495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018496#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018497 if (need_switch_win)
18498 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018499#endif
18500 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018501}
18502
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018503#ifdef FEAT_CRYPT
18504/*
18505 * "sha256({string})" function
18506 */
18507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018508f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018509{
18510 char_u *p;
18511
18512 p = get_tv_string(&argvars[0]);
18513 rettv->vval.v_string = vim_strsave(
18514 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18515 rettv->v_type = VAR_STRING;
18516}
18517#endif /* FEAT_CRYPT */
18518
Bram Moolenaar071d4272004-06-13 20:20:40 +000018519/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018520 * "shellescape({string})" function
18521 */
18522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018523f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018524{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018525 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018526 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018527 rettv->v_type = VAR_STRING;
18528}
18529
18530/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018531 * shiftwidth() function
18532 */
18533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018534f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018535{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018536 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018537}
18538
18539/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018540 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018541 */
18542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018543f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018544{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018545 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018546
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547 p = get_tv_string(&argvars[0]);
18548 rettv->vval.v_string = vim_strsave(p);
18549 simplify_filename(rettv->vval.v_string); /* simplify in place */
18550 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018551}
18552
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018553#ifdef FEAT_FLOAT
18554/*
18555 * "sin()" function
18556 */
18557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018558f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018559{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018560 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018561
18562 rettv->v_type = VAR_FLOAT;
18563 if (get_float_arg(argvars, &f) == OK)
18564 rettv->vval.v_float = sin(f);
18565 else
18566 rettv->vval.v_float = 0.0;
18567}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018568
18569/*
18570 * "sinh()" function
18571 */
18572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018573f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018574{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018575 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018576
18577 rettv->v_type = VAR_FLOAT;
18578 if (get_float_arg(argvars, &f) == OK)
18579 rettv->vval.v_float = sinh(f);
18580 else
18581 rettv->vval.v_float = 0.0;
18582}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018583#endif
18584
Bram Moolenaar0d660222005-01-07 21:51:51 +000018585static int
18586#ifdef __BORLANDC__
18587 _RTLENTRYF
18588#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018589 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018590static int
18591#ifdef __BORLANDC__
18592 _RTLENTRYF
18593#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018594 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018595
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018596/* struct used in the array that's given to qsort() */
18597typedef struct
18598{
18599 listitem_T *item;
18600 int idx;
18601} sortItem_T;
18602
Bram Moolenaar0b962472016-02-22 22:51:33 +010018603/* struct storing information about current sort */
18604typedef struct
18605{
18606 int item_compare_ic;
18607 int item_compare_numeric;
18608 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018609#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018610 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018611#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018612 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018613 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018614 dict_T *item_compare_selfdict;
18615 int item_compare_func_err;
18616 int item_compare_keep_zero;
18617} sortinfo_T;
18618static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018619static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018620#define ITEM_COMPARE_FAIL 999
18621
Bram Moolenaar071d4272004-06-13 20:20:40 +000018622/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018623 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018624 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 static int
18626#ifdef __BORLANDC__
18627_RTLENTRYF
18628#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018629item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018630{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018631 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018632 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018633 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018634 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018635 int res;
18636 char_u numbuf1[NUMBUFLEN];
18637 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018639 si1 = (sortItem_T *)s1;
18640 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018641 tv1 = &si1->item->li_tv;
18642 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018643
Bram Moolenaar0b962472016-02-22 22:51:33 +010018644 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018645 {
18646 long v1 = get_tv_number(tv1);
18647 long v2 = get_tv_number(tv2);
18648
18649 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18650 }
18651
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018652#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018653 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018654 {
18655 float_T v1 = get_tv_float(tv1);
18656 float_T v2 = get_tv_float(tv2);
18657
18658 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18659 }
18660#endif
18661
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018662 /* tv2string() puts quotes around a string and allocates memory. Don't do
18663 * that for string variables. Use a single quote when comparing with a
18664 * non-string to do what the docs promise. */
18665 if (tv1->v_type == VAR_STRING)
18666 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018667 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018668 p1 = (char_u *)"'";
18669 else
18670 p1 = tv1->vval.v_string;
18671 }
18672 else
18673 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18674 if (tv2->v_type == VAR_STRING)
18675 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018676 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018677 p2 = (char_u *)"'";
18678 else
18679 p2 = tv2->vval.v_string;
18680 }
18681 else
18682 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018683 if (p1 == NULL)
18684 p1 = (char_u *)"";
18685 if (p2 == NULL)
18686 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018687 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018688 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018689 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018690 res = STRICMP(p1, p2);
18691 else
18692 res = STRCMP(p1, p2);
18693 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018694 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018695 {
18696 double n1, n2;
18697 n1 = strtod((char *)p1, (char **)&p1);
18698 n2 = strtod((char *)p2, (char **)&p2);
18699 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18700 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018701
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018702 /* When the result would be zero, compare the item indexes. Makes the
18703 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018705 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018706
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707 vim_free(tofree1);
18708 vim_free(tofree2);
18709 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018710}
18711
18712 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018713#ifdef __BORLANDC__
18714_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018716item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018717{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018718 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018719 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018720 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018721 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018722 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018723 char_u *func_name;
18724 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018726 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018727 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018728 return 0;
18729
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018730 si1 = (sortItem_T *)s1;
18731 si2 = (sortItem_T *)s2;
18732
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018733 if (partial == NULL)
18734 func_name = sortinfo->item_compare_func;
18735 else
18736 func_name = partial->pt_name;
18737
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018738 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018739 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018740 copy_tv(&si1->item->li_tv, &argv[0]);
18741 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018742
18743 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018744 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018745 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018746 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018747 clear_tv(&argv[0]);
18748 clear_tv(&argv[1]);
18749
18750 if (res == FAIL)
18751 res = ITEM_COMPARE_FAIL;
18752 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018753 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18754 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018755 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018756 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018757
18758 /* When the result would be zero, compare the pointers themselves. Makes
18759 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018760 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018761 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018762
Bram Moolenaar0d660222005-01-07 21:51:51 +000018763 return res;
18764}
18765
18766/*
18767 * "sort({list})" function
18768 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018769 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018770do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018771{
Bram Moolenaar33570922005-01-25 22:26:29 +000018772 list_T *l;
18773 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018774 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018775 sortinfo_T *old_sortinfo;
18776 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018777 long len;
18778 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018779
Bram Moolenaar0b962472016-02-22 22:51:33 +010018780 /* Pointer to current info struct used in compare function. Save and
18781 * restore the current one for nested calls. */
18782 old_sortinfo = sortinfo;
18783 sortinfo = &info;
18784
Bram Moolenaar0d660222005-01-07 21:51:51 +000018785 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018787 else
18788 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018789 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018790 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018791 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18792 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018793 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018794 rettv->vval.v_list = l;
18795 rettv->v_type = VAR_LIST;
18796 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018797
Bram Moolenaar0d660222005-01-07 21:51:51 +000018798 len = list_len(l);
18799 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018800 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018801
Bram Moolenaar0b962472016-02-22 22:51:33 +010018802 info.item_compare_ic = FALSE;
18803 info.item_compare_numeric = FALSE;
18804 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018805#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018806 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018807#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018808 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018809 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018810 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018811 if (argvars[1].v_type != VAR_UNKNOWN)
18812 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018813 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018814 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018815 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018816 else if (argvars[1].v_type == VAR_PARTIAL)
18817 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018818 else
18819 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018820 int error = FALSE;
18821
18822 i = get_tv_number_chk(&argvars[1], &error);
18823 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018824 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018825 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018826 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018827 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018828 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018829 else if (i != 0)
18830 {
18831 EMSG(_(e_invarg));
18832 goto theend;
18833 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018834 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018835 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018836 if (*info.item_compare_func == NUL)
18837 {
18838 /* empty string means default sort */
18839 info.item_compare_func = NULL;
18840 }
18841 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018842 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018843 info.item_compare_func = NULL;
18844 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018845 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018846 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018847 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018848 info.item_compare_func = NULL;
18849 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018850 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018851#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018852 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018853 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018854 info.item_compare_func = NULL;
18855 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018856 }
18857#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018858 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018859 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018860 info.item_compare_func = NULL;
18861 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018862 }
18863 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018864 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018865
18866 if (argvars[2].v_type != VAR_UNKNOWN)
18867 {
18868 /* optional third argument: {dict} */
18869 if (argvars[2].v_type != VAR_DICT)
18870 {
18871 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018872 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018873 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018874 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018875 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018876 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018877
Bram Moolenaar0d660222005-01-07 21:51:51 +000018878 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018879 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018880 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018881 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018882
Bram Moolenaar327aa022014-03-25 18:24:23 +010018883 i = 0;
18884 if (sort)
18885 {
18886 /* sort(): ptrs will be the list to sort */
18887 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018888 {
18889 ptrs[i].item = li;
18890 ptrs[i].idx = i;
18891 ++i;
18892 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018893
Bram Moolenaar0b962472016-02-22 22:51:33 +010018894 info.item_compare_func_err = FALSE;
18895 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018896 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018897 if ((info.item_compare_func != NULL
18898 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018899 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018900 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018901 EMSG(_("E702: Sort compare function failed"));
18902 else
18903 {
18904 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018905 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018906 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018907 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018908 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018909
Bram Moolenaar0b962472016-02-22 22:51:33 +010018910 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018911 {
18912 /* Clear the List and append the items in sorted order. */
18913 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18914 l->lv_len = 0;
18915 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018916 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018917 }
18918 }
18919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018920 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018921 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018922 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018923
18924 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018925 info.item_compare_func_err = FALSE;
18926 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018927 item_compare_func_ptr = info.item_compare_func != NULL
18928 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018929 ? item_compare2 : item_compare;
18930
18931 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18932 li = li->li_next)
18933 {
18934 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18935 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018936 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018937 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018938 {
18939 EMSG(_("E882: Uniq compare function failed"));
18940 break;
18941 }
18942 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018943
Bram Moolenaar0b962472016-02-22 22:51:33 +010018944 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018945 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018946 while (--i >= 0)
18947 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018948 li = ptrs[i].item->li_next;
18949 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018950 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018951 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018952 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018953 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018954 list_fix_watch(l, li);
18955 listitem_free(li);
18956 l->lv_len--;
18957 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018958 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018959 }
18960
18961 vim_free(ptrs);
18962 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018963theend:
18964 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018965}
18966
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018967/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018968 * "sort({list})" function
18969 */
18970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018971f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018972{
18973 do_sort_uniq(argvars, rettv, TRUE);
18974}
18975
18976/*
18977 * "uniq({list})" function
18978 */
18979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018980f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018981{
18982 do_sort_uniq(argvars, rettv, FALSE);
18983}
18984
18985/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018986 * "soundfold({word})" function
18987 */
18988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018989f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018990{
18991 char_u *s;
18992
18993 rettv->v_type = VAR_STRING;
18994 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018995#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018996 rettv->vval.v_string = eval_soundfold(s);
18997#else
18998 rettv->vval.v_string = vim_strsave(s);
18999#endif
19000}
19001
19002/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019003 * "spellbadword()" function
19004 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019006f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019007{
Bram Moolenaar4463f292005-09-25 22:20:24 +000019008 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019009 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000019010 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019011
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019012 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019013 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019014
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019015#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000019016 if (argvars[0].v_type == VAR_UNKNOWN)
19017 {
19018 /* Find the start and length of the badly spelled word. */
19019 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
19020 if (len != 0)
19021 word = ml_get_cursor();
19022 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020019023 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019024 {
19025 char_u *str = get_tv_string_chk(&argvars[0]);
19026 int capcol = -1;
19027
19028 if (str != NULL)
19029 {
19030 /* Check the argument for spelling. */
19031 while (*str != NUL)
19032 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000019033 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019034 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000019035 {
19036 word = str;
19037 break;
19038 }
19039 str += len;
19040 }
19041 }
19042 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019043#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000019044
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019045 list_append_string(rettv->vval.v_list, word, len);
19046 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000019047 attr == HLF_SPB ? "bad" :
19048 attr == HLF_SPR ? "rare" :
19049 attr == HLF_SPL ? "local" :
19050 attr == HLF_SPC ? "caps" :
19051 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019052}
19053
19054/*
19055 * "spellsuggest()" function
19056 */
19057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019058f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019059{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019060#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019061 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019062 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019063 int maxcount;
19064 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019065 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019066 listitem_T *li;
19067 int need_capital = FALSE;
19068#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019069
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019070 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019071 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019072
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019073#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019074 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019075 {
19076 str = get_tv_string(&argvars[0]);
19077 if (argvars[1].v_type != VAR_UNKNOWN)
19078 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019079 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019080 if (maxcount <= 0)
19081 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019082 if (argvars[2].v_type != VAR_UNKNOWN)
19083 {
19084 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19085 if (typeerr)
19086 return;
19087 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019088 }
19089 else
19090 maxcount = 25;
19091
Bram Moolenaar4770d092006-01-12 23:22:24 +000019092 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019093
19094 for (i = 0; i < ga.ga_len; ++i)
19095 {
19096 str = ((char_u **)ga.ga_data)[i];
19097
19098 li = listitem_alloc();
19099 if (li == NULL)
19100 vim_free(str);
19101 else
19102 {
19103 li->li_tv.v_type = VAR_STRING;
19104 li->li_tv.v_lock = 0;
19105 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019106 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019107 }
19108 }
19109 ga_clear(&ga);
19110 }
19111#endif
19112}
19113
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019115f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019116{
19117 char_u *str;
19118 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019119 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019120 regmatch_T regmatch;
19121 char_u patbuf[NUMBUFLEN];
19122 char_u *save_cpo;
19123 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019125 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019126 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019127
19128 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19129 save_cpo = p_cpo;
19130 p_cpo = (char_u *)"";
19131
19132 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019133 if (argvars[1].v_type != VAR_UNKNOWN)
19134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019135 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19136 if (pat == NULL)
19137 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019138 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019139 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019140 }
19141 if (pat == NULL || *pat == NUL)
19142 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019143
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019144 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019145 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019146 if (typeerr)
19147 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019148
Bram Moolenaar0d660222005-01-07 21:51:51 +000019149 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19150 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019151 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019152 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019153 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019154 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019155 if (*str == NUL)
19156 match = FALSE; /* empty item at the end */
19157 else
19158 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019159 if (match)
19160 end = regmatch.startp[0];
19161 else
19162 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019163 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19164 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019165 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019166 if (list_append_string(rettv->vval.v_list, str,
19167 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019168 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019169 }
19170 if (!match)
19171 break;
19172 /* Advance to just after the match. */
19173 if (regmatch.endp[0] > str)
19174 col = 0;
19175 else
19176 {
19177 /* Don't get stuck at the same match. */
19178#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019179 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019180#else
19181 col = 1;
19182#endif
19183 }
19184 str = regmatch.endp[0];
19185 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019186
Bram Moolenaar473de612013-06-08 18:19:48 +020019187 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019188 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019189
Bram Moolenaar0d660222005-01-07 21:51:51 +000019190 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019191}
19192
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019193#ifdef FEAT_FLOAT
19194/*
19195 * "sqrt()" function
19196 */
19197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019198f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019199{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019200 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019201
19202 rettv->v_type = VAR_FLOAT;
19203 if (get_float_arg(argvars, &f) == OK)
19204 rettv->vval.v_float = sqrt(f);
19205 else
19206 rettv->vval.v_float = 0.0;
19207}
19208
19209/*
19210 * "str2float()" function
19211 */
19212 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019213f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019214{
19215 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19216
19217 if (*p == '+')
19218 p = skipwhite(p + 1);
19219 (void)string2float(p, &rettv->vval.v_float);
19220 rettv->v_type = VAR_FLOAT;
19221}
19222#endif
19223
Bram Moolenaar2c932302006-03-18 21:42:09 +000019224/*
19225 * "str2nr()" function
19226 */
19227 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019228f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019229{
19230 int base = 10;
19231 char_u *p;
19232 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019233 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019234
19235 if (argvars[1].v_type != VAR_UNKNOWN)
19236 {
19237 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019238 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019239 {
19240 EMSG(_(e_invarg));
19241 return;
19242 }
19243 }
19244
19245 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019246 if (*p == '+')
19247 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019248 switch (base)
19249 {
19250 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19251 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19252 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19253 default: what = 0;
19254 }
19255 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019256 rettv->vval.v_number = n;
19257}
19258
Bram Moolenaar071d4272004-06-13 20:20:40 +000019259#ifdef HAVE_STRFTIME
19260/*
19261 * "strftime({format}[, {time}])" function
19262 */
19263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019264f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019265{
19266 char_u result_buf[256];
19267 struct tm *curtime;
19268 time_t seconds;
19269 char_u *p;
19270
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019271 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019273 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019274 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 seconds = time(NULL);
19276 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019277 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019278 curtime = localtime(&seconds);
19279 /* MSVC returns NULL for an invalid value of seconds. */
19280 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019281 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019282 else
19283 {
19284# ifdef FEAT_MBYTE
19285 vimconv_T conv;
19286 char_u *enc;
19287
19288 conv.vc_type = CONV_NONE;
19289 enc = enc_locale();
19290 convert_setup(&conv, p_enc, enc);
19291 if (conv.vc_type != CONV_NONE)
19292 p = string_convert(&conv, p, NULL);
19293# endif
19294 if (p != NULL)
19295 (void)strftime((char *)result_buf, sizeof(result_buf),
19296 (char *)p, curtime);
19297 else
19298 result_buf[0] = NUL;
19299
19300# ifdef FEAT_MBYTE
19301 if (conv.vc_type != CONV_NONE)
19302 vim_free(p);
19303 convert_setup(&conv, enc, p_enc);
19304 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019305 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019306 else
19307# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019308 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019309
19310# ifdef FEAT_MBYTE
19311 /* Release conversion descriptors */
19312 convert_setup(&conv, NULL, NULL);
19313 vim_free(enc);
19314# endif
19315 }
19316}
19317#endif
19318
19319/*
19320 * "stridx()" function
19321 */
19322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019323f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019324{
19325 char_u buf[NUMBUFLEN];
19326 char_u *needle;
19327 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019328 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019329 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019330 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019332 needle = get_tv_string_chk(&argvars[1]);
19333 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019334 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019335 if (needle == NULL || haystack == NULL)
19336 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019337
Bram Moolenaar33570922005-01-25 22:26:29 +000019338 if (argvars[2].v_type != VAR_UNKNOWN)
19339 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019340 int error = FALSE;
19341
19342 start_idx = get_tv_number_chk(&argvars[2], &error);
19343 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019344 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019345 if (start_idx >= 0)
19346 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019347 }
19348
19349 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19350 if (pos != NULL)
19351 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019352}
19353
19354/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019355 * "string()" function
19356 */
19357 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019358f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019359{
19360 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019361 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019362
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019363 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019364 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19365 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019366 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019367 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019368 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019369}
19370
19371/*
19372 * "strlen()" function
19373 */
19374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019375f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019376{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019377 rettv->vval.v_number = (varnumber_T)(STRLEN(
19378 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019379}
19380
19381/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019382 * "strchars()" function
19383 */
19384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019385f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019386{
19387 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019388 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019389#ifdef FEAT_MBYTE
19390 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019391 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019392#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019393
19394 if (argvars[1].v_type != VAR_UNKNOWN)
19395 skipcc = get_tv_number_chk(&argvars[1], NULL);
19396 if (skipcc < 0 || skipcc > 1)
19397 EMSG(_(e_invarg));
19398 else
19399 {
19400#ifdef FEAT_MBYTE
19401 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19402 while (*s != NUL)
19403 {
19404 func_mb_ptr2char_adv(&s);
19405 ++len;
19406 }
19407 rettv->vval.v_number = len;
19408#else
19409 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19410#endif
19411 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019412}
19413
19414/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019415 * "strdisplaywidth()" function
19416 */
19417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019418f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019419{
19420 char_u *s = get_tv_string(&argvars[0]);
19421 int col = 0;
19422
19423 if (argvars[1].v_type != VAR_UNKNOWN)
19424 col = get_tv_number(&argvars[1]);
19425
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019426 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019427}
19428
19429/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019430 * "strwidth()" function
19431 */
19432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019433f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019434{
19435 char_u *s = get_tv_string(&argvars[0]);
19436
19437 rettv->vval.v_number = (varnumber_T)(
19438#ifdef FEAT_MBYTE
19439 mb_string2cells(s, -1)
19440#else
19441 STRLEN(s)
19442#endif
19443 );
19444}
19445
19446/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 * "strpart()" function
19448 */
19449 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019450f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019451{
19452 char_u *p;
19453 int n;
19454 int len;
19455 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019456 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019458 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019459 slen = (int)STRLEN(p);
19460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 n = get_tv_number_chk(&argvars[1], &error);
19462 if (error)
19463 len = 0;
19464 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019465 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019466 else
19467 len = slen - n; /* default len: all bytes that are available. */
19468
19469 /*
19470 * Only return the overlap between the specified part and the actual
19471 * string.
19472 */
19473 if (n < 0)
19474 {
19475 len += n;
19476 n = 0;
19477 }
19478 else if (n > slen)
19479 n = slen;
19480 if (len < 0)
19481 len = 0;
19482 else if (n + len > slen)
19483 len = slen - n;
19484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019485 rettv->v_type = VAR_STRING;
19486 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487}
19488
19489/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019490 * "strridx()" function
19491 */
19492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019493f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019494{
19495 char_u buf[NUMBUFLEN];
19496 char_u *needle;
19497 char_u *haystack;
19498 char_u *rest;
19499 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019500 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019502 needle = get_tv_string_chk(&argvars[1]);
19503 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019504
19505 rettv->vval.v_number = -1;
19506 if (needle == NULL || haystack == NULL)
19507 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019508
19509 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019510 if (argvars[2].v_type != VAR_UNKNOWN)
19511 {
19512 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019513 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019514 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019515 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019516 }
19517 else
19518 end_idx = haystack_len;
19519
Bram Moolenaar0d660222005-01-07 21:51:51 +000019520 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019521 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019522 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019523 lastmatch = haystack + end_idx;
19524 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019525 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019526 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019527 for (rest = haystack; *rest != '\0'; ++rest)
19528 {
19529 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019530 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019531 break;
19532 lastmatch = rest;
19533 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019534 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019535
19536 if (lastmatch == NULL)
19537 rettv->vval.v_number = -1;
19538 else
19539 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19540}
19541
19542/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019543 * "strtrans()" function
19544 */
19545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019546f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019547{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019548 rettv->v_type = VAR_STRING;
19549 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019550}
19551
19552/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019553 * "submatch()" function
19554 */
19555 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019556f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019557{
Bram Moolenaar41571762014-04-02 19:00:58 +020019558 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019559 int no;
19560 int retList = 0;
19561
19562 no = (int)get_tv_number_chk(&argvars[0], &error);
19563 if (error)
19564 return;
19565 error = FALSE;
19566 if (argvars[1].v_type != VAR_UNKNOWN)
19567 retList = get_tv_number_chk(&argvars[1], &error);
19568 if (error)
19569 return;
19570
19571 if (retList == 0)
19572 {
19573 rettv->v_type = VAR_STRING;
19574 rettv->vval.v_string = reg_submatch(no);
19575 }
19576 else
19577 {
19578 rettv->v_type = VAR_LIST;
19579 rettv->vval.v_list = reg_submatch_list(no);
19580 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019581}
19582
19583/*
19584 * "substitute()" function
19585 */
19586 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019587f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019588{
19589 char_u patbuf[NUMBUFLEN];
19590 char_u subbuf[NUMBUFLEN];
19591 char_u flagsbuf[NUMBUFLEN];
19592
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019593 char_u *str = get_tv_string_chk(&argvars[0]);
19594 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19595 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19596 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19597
Bram Moolenaar0d660222005-01-07 21:51:51 +000019598 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019599 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19600 rettv->vval.v_string = NULL;
19601 else
19602 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019603}
19604
19605/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019606 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019608 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019609f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019610{
19611 int id = 0;
19612#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019613 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019614 long col;
19615 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019616 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019617
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019618 lnum = get_tv_lnum(argvars); /* -1 on type error */
19619 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19620 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019622 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019623 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019624 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019625#endif
19626
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019627 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019628}
19629
19630/*
19631 * "synIDattr(id, what [, mode])" function
19632 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019634f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019635{
19636 char_u *p = NULL;
19637#ifdef FEAT_SYN_HL
19638 int id;
19639 char_u *what;
19640 char_u *mode;
19641 char_u modebuf[NUMBUFLEN];
19642 int modec;
19643
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019644 id = get_tv_number(&argvars[0]);
19645 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019646 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019647 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019648 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019649 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019650 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019651 modec = 0; /* replace invalid with current */
19652 }
19653 else
19654 {
19655#ifdef FEAT_GUI
19656 if (gui.in_use)
19657 modec = 'g';
19658 else
19659#endif
19660 if (t_colors > 1)
19661 modec = 'c';
19662 else
19663 modec = 't';
19664 }
19665
19666
19667 switch (TOLOWER_ASC(what[0]))
19668 {
19669 case 'b':
19670 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19671 p = highlight_color(id, what, modec);
19672 else /* bold */
19673 p = highlight_has_attr(id, HL_BOLD, modec);
19674 break;
19675
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019676 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019677 p = highlight_color(id, what, modec);
19678 break;
19679
19680 case 'i':
19681 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19682 p = highlight_has_attr(id, HL_INVERSE, modec);
19683 else /* italic */
19684 p = highlight_has_attr(id, HL_ITALIC, modec);
19685 break;
19686
19687 case 'n': /* name */
19688 p = get_highlight_name(NULL, id - 1);
19689 break;
19690
19691 case 'r': /* reverse */
19692 p = highlight_has_attr(id, HL_INVERSE, modec);
19693 break;
19694
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019695 case 's':
19696 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19697 p = highlight_color(id, what, modec);
19698 else /* standout */
19699 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019700 break;
19701
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019702 case 'u':
19703 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19704 /* underline */
19705 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19706 else
19707 /* undercurl */
19708 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019709 break;
19710 }
19711
19712 if (p != NULL)
19713 p = vim_strsave(p);
19714#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019715 rettv->v_type = VAR_STRING;
19716 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019717}
19718
19719/*
19720 * "synIDtrans(id)" function
19721 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019723f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019724{
19725 int id;
19726
19727#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019728 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019729
19730 if (id > 0)
19731 id = syn_get_final_id(id);
19732 else
19733#endif
19734 id = 0;
19735
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019736 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019737}
19738
19739/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019740 * "synconcealed(lnum, col)" function
19741 */
19742 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019743f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019744{
19745#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19746 long lnum;
19747 long col;
19748 int syntax_flags = 0;
19749 int cchar;
19750 int matchid = 0;
19751 char_u str[NUMBUFLEN];
19752#endif
19753
19754 rettv->v_type = VAR_LIST;
19755 rettv->vval.v_list = NULL;
19756
19757#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19758 lnum = get_tv_lnum(argvars); /* -1 on type error */
19759 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19760
19761 vim_memset(str, NUL, sizeof(str));
19762
19763 if (rettv_list_alloc(rettv) != FAIL)
19764 {
19765 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19766 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19767 && curwin->w_p_cole > 0)
19768 {
19769 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19770 syntax_flags = get_syntax_info(&matchid);
19771
19772 /* get the conceal character */
19773 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19774 {
19775 cchar = syn_get_sub_char();
19776 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19777 cchar = lcs_conceal;
19778 if (cchar != NUL)
19779 {
19780# ifdef FEAT_MBYTE
19781 if (has_mbyte)
19782 (*mb_char2bytes)(cchar, str);
19783 else
19784# endif
19785 str[0] = cchar;
19786 }
19787 }
19788 }
19789
19790 list_append_number(rettv->vval.v_list,
19791 (syntax_flags & HL_CONCEAL) != 0);
19792 /* -1 to auto-determine strlen */
19793 list_append_string(rettv->vval.v_list, str, -1);
19794 list_append_number(rettv->vval.v_list, matchid);
19795 }
19796#endif
19797}
19798
19799/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019800 * "synstack(lnum, col)" function
19801 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019802 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019803f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019804{
19805#ifdef FEAT_SYN_HL
19806 long lnum;
19807 long col;
19808 int i;
19809 int id;
19810#endif
19811
19812 rettv->v_type = VAR_LIST;
19813 rettv->vval.v_list = NULL;
19814
19815#ifdef FEAT_SYN_HL
19816 lnum = get_tv_lnum(argvars); /* -1 on type error */
19817 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19818
19819 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019820 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019821 && rettv_list_alloc(rettv) != FAIL)
19822 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019823 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019824 for (i = 0; ; ++i)
19825 {
19826 id = syn_get_stack_item(i);
19827 if (id < 0)
19828 break;
19829 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19830 break;
19831 }
19832 }
19833#endif
19834}
19835
Bram Moolenaar071d4272004-06-13 20:20:40 +000019836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019837get_cmd_output_as_rettv(
19838 typval_T *argvars,
19839 typval_T *rettv,
19840 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019841{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019842 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019843 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019844 char_u *infile = NULL;
19845 char_u buf[NUMBUFLEN];
19846 int err = FALSE;
19847 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019848 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019849 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019850
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019851 rettv->v_type = VAR_STRING;
19852 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019853 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019854 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019855
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019856 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019857 {
19858 /*
19859 * Write the string to a temp file, to be used for input of the shell
19860 * command.
19861 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019862 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019863 {
19864 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019865 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019866 }
19867
19868 fd = mch_fopen((char *)infile, WRITEBIN);
19869 if (fd == NULL)
19870 {
19871 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019872 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019873 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019874 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019875 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019876 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19877 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019878 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019879 else
19880 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019881 size_t len;
19882
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019883 p = get_tv_string_buf_chk(&argvars[1], buf);
19884 if (p == NULL)
19885 {
19886 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019887 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019888 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019889 len = STRLEN(p);
19890 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019891 err = TRUE;
19892 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019893 if (fclose(fd) != 0)
19894 err = TRUE;
19895 if (err)
19896 {
19897 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019898 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019899 }
19900 }
19901
Bram Moolenaar52a72462014-08-29 15:53:52 +020019902 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19903 * echoes typeahead, that messes up the display. */
19904 if (!msg_silent)
19905 flags += SHELL_COOKED;
19906
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019907 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019908 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019909 int len;
19910 listitem_T *li;
19911 char_u *s = NULL;
19912 char_u *start;
19913 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019914 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915
Bram Moolenaar52a72462014-08-29 15:53:52 +020019916 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019917 if (res == NULL)
19918 goto errret;
19919
19920 list = list_alloc();
19921 if (list == NULL)
19922 goto errret;
19923
19924 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019925 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019926 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019927 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019928 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019929 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019930
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019931 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019932 if (s == NULL)
19933 goto errret;
19934
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019935 for (p = s; start < end; ++p, ++start)
19936 *p = *start == NUL ? NL : *start;
19937 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019938
19939 li = listitem_alloc();
19940 if (li == NULL)
19941 {
19942 vim_free(s);
19943 goto errret;
19944 }
19945 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019946 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019947 li->li_tv.vval.v_string = s;
19948 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019949 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019950
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019951 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019952 rettv->v_type = VAR_LIST;
19953 rettv->vval.v_list = list;
19954 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019955 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019956 else
19957 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019958 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019959#ifdef USE_CR
19960 /* translate <CR> into <NL> */
19961 if (res != NULL)
19962 {
19963 char_u *s;
19964
19965 for (s = res; *s; ++s)
19966 {
19967 if (*s == CAR)
19968 *s = NL;
19969 }
19970 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019971#else
19972# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019973 /* translate <CR><NL> into <NL> */
19974 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019975 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019976 char_u *s, *d;
19977
19978 d = res;
19979 for (s = res; *s; ++s)
19980 {
19981 if (s[0] == CAR && s[1] == NL)
19982 ++s;
19983 *d++ = *s;
19984 }
19985 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019986 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019987# endif
19988#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019989 rettv->vval.v_string = res;
19990 res = NULL;
19991 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019992
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019993errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019994 if (infile != NULL)
19995 {
19996 mch_remove(infile);
19997 vim_free(infile);
19998 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019999 if (res != NULL)
20000 vim_free(res);
20001 if (list != NULL)
20002 list_free(list, TRUE);
20003}
20004
20005/*
20006 * "system()" function
20007 */
20008 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020009f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020010{
20011 get_cmd_output_as_rettv(argvars, rettv, FALSE);
20012}
20013
20014/*
20015 * "systemlist()" function
20016 */
20017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020018f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020020019{
20020 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020021}
20022
20023/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020024 * "tabpagebuflist()" function
20025 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020026 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020027f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020028{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020029#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020030 tabpage_T *tp;
20031 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020032
20033 if (argvars[0].v_type == VAR_UNKNOWN)
20034 wp = firstwin;
20035 else
20036 {
20037 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20038 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000020039 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020040 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020041 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020042 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020043 for (; wp != NULL; wp = wp->w_next)
20044 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020045 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000020046 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020047 }
20048#endif
20049}
20050
20051
20052/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020053 * "tabpagenr()" function
20054 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020056f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020057{
20058 int nr = 1;
20059#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020060 char_u *arg;
20061
20062 if (argvars[0].v_type != VAR_UNKNOWN)
20063 {
20064 arg = get_tv_string_chk(&argvars[0]);
20065 nr = 0;
20066 if (arg != NULL)
20067 {
20068 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020069 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020070 else
20071 EMSG2(_(e_invexpr2), arg);
20072 }
20073 }
20074 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020075 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020076#endif
20077 rettv->vval.v_number = nr;
20078}
20079
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020080
20081#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020082static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020083
20084/*
20085 * Common code for tabpagewinnr() and winnr().
20086 */
20087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020088get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020089{
20090 win_T *twin;
20091 int nr = 1;
20092 win_T *wp;
20093 char_u *arg;
20094
20095 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20096 if (argvar->v_type != VAR_UNKNOWN)
20097 {
20098 arg = get_tv_string_chk(argvar);
20099 if (arg == NULL)
20100 nr = 0; /* type error; errmsg already given */
20101 else if (STRCMP(arg, "$") == 0)
20102 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20103 else if (STRCMP(arg, "#") == 0)
20104 {
20105 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20106 if (twin == NULL)
20107 nr = 0;
20108 }
20109 else
20110 {
20111 EMSG2(_(e_invexpr2), arg);
20112 nr = 0;
20113 }
20114 }
20115
20116 if (nr > 0)
20117 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20118 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020119 {
20120 if (wp == NULL)
20121 {
20122 /* didn't find it in this tabpage */
20123 nr = 0;
20124 break;
20125 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020126 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020127 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020128 return nr;
20129}
20130#endif
20131
20132/*
20133 * "tabpagewinnr()" function
20134 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020136f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020137{
20138 int nr = 1;
20139#ifdef FEAT_WINDOWS
20140 tabpage_T *tp;
20141
20142 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20143 if (tp == NULL)
20144 nr = 0;
20145 else
20146 nr = get_winnr(tp, &argvars[1]);
20147#endif
20148 rettv->vval.v_number = nr;
20149}
20150
20151
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020152/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020153 * "tagfiles()" function
20154 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020156f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020157{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020158 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020159 tagname_T tn;
20160 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020161
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020162 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020163 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020164 fname = alloc(MAXPATHL);
20165 if (fname == NULL)
20166 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020167
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020168 for (first = TRUE; ; first = FALSE)
20169 if (get_tagfname(&tn, first, fname) == FAIL
20170 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020171 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020172 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020173 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020174}
20175
20176/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020177 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020178 */
20179 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020180f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020181{
20182 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020183
20184 tag_pattern = get_tv_string(&argvars[0]);
20185
20186 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020187 if (*tag_pattern == NUL)
20188 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020189
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020190 if (rettv_list_alloc(rettv) == OK)
20191 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020192}
20193
20194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020195 * "tempname()" function
20196 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020198f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020199{
20200 static int x = 'A';
20201
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020202 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020203 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204
20205 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20206 * names. Skip 'I' and 'O', they are used for shell redirection. */
20207 do
20208 {
20209 if (x == 'Z')
20210 x = '0';
20211 else if (x == '9')
20212 x = 'A';
20213 else
20214 {
20215#ifdef EBCDIC
20216 if (x == 'I')
20217 x = 'J';
20218 else if (x == 'R')
20219 x = 'S';
20220 else
20221#endif
20222 ++x;
20223 }
20224 } while (x == 'I' || x == 'O');
20225}
20226
20227/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020228 * "test(list)" function: Just checking the walls...
20229 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020230 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020231f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020232{
20233 /* Used for unit testing. Change the code below to your liking. */
20234#if 0
20235 listitem_T *li;
20236 list_T *l;
20237 char_u *bad, *good;
20238
20239 if (argvars[0].v_type != VAR_LIST)
20240 return;
20241 l = argvars[0].vval.v_list;
20242 if (l == NULL)
20243 return;
20244 li = l->lv_first;
20245 if (li == NULL)
20246 return;
20247 bad = get_tv_string(&li->li_tv);
20248 li = li->li_next;
20249 if (li == NULL)
20250 return;
20251 good = get_tv_string(&li->li_tv);
20252 rettv->vval.v_number = test_edit_score(bad, good);
20253#endif
20254}
20255
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020256#ifdef FEAT_FLOAT
20257/*
20258 * "tan()" function
20259 */
20260 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020261f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020262{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020263 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020264
20265 rettv->v_type = VAR_FLOAT;
20266 if (get_float_arg(argvars, &f) == OK)
20267 rettv->vval.v_float = tan(f);
20268 else
20269 rettv->vval.v_float = 0.0;
20270}
20271
20272/*
20273 * "tanh()" function
20274 */
20275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020276f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020277{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020278 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020279
20280 rettv->v_type = VAR_FLOAT;
20281 if (get_float_arg(argvars, &f) == OK)
20282 rettv->vval.v_float = tanh(f);
20283 else
20284 rettv->vval.v_float = 0.0;
20285}
20286#endif
20287
Bram Moolenaar975b5272016-03-15 23:10:59 +010020288#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20289/*
20290 * Get a callback from "arg". It can be a Funcref or a function name.
20291 * When "arg" is zero return an empty string.
20292 * Return NULL for an invalid argument.
20293 */
20294 char_u *
20295get_callback(typval_T *arg, partial_T **pp)
20296{
20297 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20298 {
20299 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020300 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020301 return (*pp)->pt_name;
20302 }
20303 *pp = NULL;
20304 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20305 return arg->vval.v_string;
20306 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20307 return (char_u *)"";
20308 EMSG(_("E921: Invalid callback argument"));
20309 return NULL;
20310}
20311#endif
20312
20313#ifdef FEAT_TIMERS
20314/*
20315 * "timer_start(time, callback [, options])" function
20316 */
20317 static void
20318f_timer_start(typval_T *argvars, typval_T *rettv)
20319{
20320 long msec = get_tv_number(&argvars[0]);
20321 timer_T *timer;
20322 int repeat = 0;
20323 char_u *callback;
20324 dict_T *dict;
20325
20326 if (argvars[2].v_type != VAR_UNKNOWN)
20327 {
20328 if (argvars[2].v_type != VAR_DICT
20329 || (dict = argvars[2].vval.v_dict) == NULL)
20330 {
20331 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20332 return;
20333 }
20334 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20335 repeat = get_dict_number(dict, (char_u *)"repeat");
20336 }
20337
20338 timer = create_timer(msec, repeat);
20339 callback = get_callback(&argvars[1], &timer->tr_partial);
20340 if (callback == NULL)
20341 {
20342 stop_timer(timer);
20343 rettv->vval.v_number = -1;
20344 }
20345 else
20346 {
20347 timer->tr_callback = vim_strsave(callback);
20348 rettv->vval.v_number = timer->tr_id;
20349 }
20350}
20351
20352/*
20353 * "timer_stop(timer)" function
20354 */
20355 static void
20356f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20357{
20358 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20359
20360 if (timer != NULL)
20361 stop_timer(timer);
20362}
20363#endif
20364
Bram Moolenaard52d9742005-08-21 22:20:28 +000020365/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020366 * "tolower(string)" function
20367 */
20368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020369f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020370{
20371 char_u *p;
20372
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020373 p = vim_strsave(get_tv_string(&argvars[0]));
20374 rettv->v_type = VAR_STRING;
20375 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020376
20377 if (p != NULL)
20378 while (*p != NUL)
20379 {
20380#ifdef FEAT_MBYTE
20381 int l;
20382
20383 if (enc_utf8)
20384 {
20385 int c, lc;
20386
20387 c = utf_ptr2char(p);
20388 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020389 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020390 /* TODO: reallocate string when byte count changes. */
20391 if (utf_char2len(lc) == l)
20392 utf_char2bytes(lc, p);
20393 p += l;
20394 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020395 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020396 p += l; /* skip multi-byte character */
20397 else
20398#endif
20399 {
20400 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20401 ++p;
20402 }
20403 }
20404}
20405
20406/*
20407 * "toupper(string)" function
20408 */
20409 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020410f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020411{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020412 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020413 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020414}
20415
20416/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020417 * "tr(string, fromstr, tostr)" function
20418 */
20419 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020420f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020421{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020422 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020423 char_u *fromstr;
20424 char_u *tostr;
20425 char_u *p;
20426#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020427 int inlen;
20428 int fromlen;
20429 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020430 int idx;
20431 char_u *cpstr;
20432 int cplen;
20433 int first = TRUE;
20434#endif
20435 char_u buf[NUMBUFLEN];
20436 char_u buf2[NUMBUFLEN];
20437 garray_T ga;
20438
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020439 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020440 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20441 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020442
20443 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020444 rettv->v_type = VAR_STRING;
20445 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020446 if (fromstr == NULL || tostr == NULL)
20447 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020448 ga_init2(&ga, (int)sizeof(char), 80);
20449
20450#ifdef FEAT_MBYTE
20451 if (!has_mbyte)
20452#endif
20453 /* not multi-byte: fromstr and tostr must be the same length */
20454 if (STRLEN(fromstr) != STRLEN(tostr))
20455 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020456#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020457error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020458#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020459 EMSG2(_(e_invarg2), fromstr);
20460 ga_clear(&ga);
20461 return;
20462 }
20463
20464 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020465 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020466 {
20467#ifdef FEAT_MBYTE
20468 if (has_mbyte)
20469 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020470 inlen = (*mb_ptr2len)(in_str);
20471 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020472 cplen = inlen;
20473 idx = 0;
20474 for (p = fromstr; *p != NUL; p += fromlen)
20475 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020476 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020477 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020478 {
20479 for (p = tostr; *p != NUL; p += tolen)
20480 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020481 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020482 if (idx-- == 0)
20483 {
20484 cplen = tolen;
20485 cpstr = p;
20486 break;
20487 }
20488 }
20489 if (*p == NUL) /* tostr is shorter than fromstr */
20490 goto error;
20491 break;
20492 }
20493 ++idx;
20494 }
20495
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020496 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020497 {
20498 /* Check that fromstr and tostr have the same number of
20499 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020500 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020501 first = FALSE;
20502 for (p = tostr; *p != NUL; p += tolen)
20503 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020504 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020505 --idx;
20506 }
20507 if (idx != 0)
20508 goto error;
20509 }
20510
Bram Moolenaarcde88542015-08-11 19:14:00 +020020511 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020512 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020513 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020514
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020515 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020516 }
20517 else
20518#endif
20519 {
20520 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020521 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020522 if (p != NULL)
20523 ga_append(&ga, tostr[p - fromstr]);
20524 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020525 ga_append(&ga, *in_str);
20526 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020527 }
20528 }
20529
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020530 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020531 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020532 ga_append(&ga, NUL);
20533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020534 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020535}
20536
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020537#ifdef FEAT_FLOAT
20538/*
20539 * "trunc({float})" function
20540 */
20541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020542f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020543{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020544 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020545
20546 rettv->v_type = VAR_FLOAT;
20547 if (get_float_arg(argvars, &f) == OK)
20548 /* trunc() is not in C90, use floor() or ceil() instead. */
20549 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20550 else
20551 rettv->vval.v_float = 0.0;
20552}
20553#endif
20554
Bram Moolenaar8299df92004-07-10 09:47:34 +000020555/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 * "type(expr)" function
20557 */
20558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020559f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020560{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020561 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020562
20563 switch (argvars[0].v_type)
20564 {
20565 case VAR_NUMBER: n = 0; break;
20566 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020567 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020568 case VAR_FUNC: n = 2; break;
20569 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020570 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020571 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020572 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020573 if (argvars[0].vval.v_number == VVAL_FALSE
20574 || argvars[0].vval.v_number == VVAL_TRUE)
20575 n = 6;
20576 else
20577 n = 7;
20578 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020579 case VAR_JOB: n = 8; break;
20580 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020581 case VAR_UNKNOWN:
20582 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20583 n = -1;
20584 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020585 }
20586 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020587}
20588
20589/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020590 * "undofile(name)" function
20591 */
20592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020593f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020594{
20595 rettv->v_type = VAR_STRING;
20596#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020597 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020598 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020599
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020600 if (*fname == NUL)
20601 {
20602 /* If there is no file name there will be no undo file. */
20603 rettv->vval.v_string = NULL;
20604 }
20605 else
20606 {
20607 char_u *ffname = FullName_save(fname, FALSE);
20608
20609 if (ffname != NULL)
20610 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20611 vim_free(ffname);
20612 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020613 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020614#else
20615 rettv->vval.v_string = NULL;
20616#endif
20617}
20618
20619/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020620 * "undotree()" function
20621 */
20622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020623f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020624{
20625 if (rettv_dict_alloc(rettv) == OK)
20626 {
20627 dict_T *dict = rettv->vval.v_dict;
20628 list_T *list;
20629
Bram Moolenaar730cde92010-06-27 05:18:54 +020020630 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020631 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020632 dict_add_nr_str(dict, "save_last",
20633 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020634 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20635 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020636 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020637
20638 list = list_alloc();
20639 if (list != NULL)
20640 {
20641 u_eval_tree(curbuf->b_u_oldhead, list);
20642 dict_add_list(dict, "entries", list);
20643 }
20644 }
20645}
20646
20647/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020648 * "values(dict)" function
20649 */
20650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020651f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020652{
20653 dict_list(argvars, rettv, 1);
20654}
20655
20656/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020657 * "virtcol(string)" function
20658 */
20659 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020660f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020661{
20662 colnr_T vcol = 0;
20663 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020664 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020665
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020666 fp = var2fpos(&argvars[0], FALSE, &fnum);
20667 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20668 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 {
20670 getvvcol(curwin, fp, NULL, NULL, &vcol);
20671 ++vcol;
20672 }
20673
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020674 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020675}
20676
20677/*
20678 * "visualmode()" function
20679 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020680 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020681f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020682{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683 char_u str[2];
20684
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020685 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020686 str[0] = curbuf->b_visual_mode_eval;
20687 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689
20690 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020691 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020692 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020693}
20694
20695/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020696 * "wildmenumode()" function
20697 */
20698 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020699f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020700{
20701#ifdef FEAT_WILDMENU
20702 if (wild_menu_showing)
20703 rettv->vval.v_number = 1;
20704#endif
20705}
20706
20707/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708 * "winbufnr(nr)" function
20709 */
20710 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020711f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020712{
20713 win_T *wp;
20714
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020715 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020716 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020717 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020719 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720}
20721
20722/*
20723 * "wincol()" function
20724 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020726f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020727{
20728 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020729 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020730}
20731
20732/*
20733 * "winheight(nr)" function
20734 */
20735 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020736f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737{
20738 win_T *wp;
20739
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020740 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745}
20746
20747/*
20748 * "winline()" function
20749 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020751f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020752{
20753 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020754 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020755}
20756
20757/*
20758 * "winnr()" function
20759 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020760 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020761f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020762{
20763 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020764
Bram Moolenaar071d4272004-06-13 20:20:40 +000020765#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020766 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020767#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020768 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020769}
20770
20771/*
20772 * "winrestcmd()" function
20773 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020775f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020776{
20777#ifdef FEAT_WINDOWS
20778 win_T *wp;
20779 int winnr = 1;
20780 garray_T ga;
20781 char_u buf[50];
20782
20783 ga_init2(&ga, (int)sizeof(char), 70);
20784 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20785 {
20786 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20787 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020788 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20789 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020790 ++winnr;
20791 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020792 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020793
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020794 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020795#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020796 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020797#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020798 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020799}
20800
20801/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020802 * "winrestview()" function
20803 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020804 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020805f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020806{
20807 dict_T *dict;
20808
20809 if (argvars[0].v_type != VAR_DICT
20810 || (dict = argvars[0].vval.v_dict) == NULL)
20811 EMSG(_(e_invarg));
20812 else
20813 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020814 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20815 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20816 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20817 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020818#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020819 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20820 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020821#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020822 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20823 {
20824 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20825 curwin->w_set_curswant = FALSE;
20826 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020827
Bram Moolenaar82c25852014-05-28 16:47:16 +020020828 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20829 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020830#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020831 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20832 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020833#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020834 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20835 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20836 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20837 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020838
20839 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020840 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020841# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020842 win_new_width(curwin, W_WIDTH(curwin));
20843# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020844 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020845
Bram Moolenaarb851a962014-10-31 15:45:52 +010020846 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020847 curwin->w_topline = 1;
20848 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20849 curwin->w_topline = curbuf->b_ml.ml_line_count;
20850#ifdef FEAT_DIFF
20851 check_topfill(curwin, TRUE);
20852#endif
20853 }
20854}
20855
20856/*
20857 * "winsaveview()" function
20858 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020860f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020861{
20862 dict_T *dict;
20863
Bram Moolenaara800b422010-06-27 01:15:55 +020020864 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020865 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020866 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020867
20868 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20869 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20870#ifdef FEAT_VIRTUALEDIT
20871 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20872#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020873 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020874 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20875
20876 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20877#ifdef FEAT_DIFF
20878 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20879#endif
20880 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20881 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20882}
20883
20884/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020885 * "winwidth(nr)" function
20886 */
20887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020888f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020889{
20890 win_T *wp;
20891
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020892 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020893 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020895 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020896#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020897 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020898#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020899 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020900#endif
20901}
20902
Bram Moolenaar071d4272004-06-13 20:20:40 +000020903/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020904 * "wordcount()" function
20905 */
20906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020907f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020908{
20909 if (rettv_dict_alloc(rettv) == FAIL)
20910 return;
20911 cursor_pos_info(rettv->vval.v_dict);
20912}
20913
20914/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020915 * Write list of strings to file
20916 */
20917 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020918write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020919{
20920 listitem_T *li;
20921 int c;
20922 int ret = OK;
20923 char_u *s;
20924
20925 for (li = list->lv_first; li != NULL; li = li->li_next)
20926 {
20927 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20928 {
20929 if (*s == '\n')
20930 c = putc(NUL, fd);
20931 else
20932 c = putc(*s, fd);
20933 if (c == EOF)
20934 {
20935 ret = FAIL;
20936 break;
20937 }
20938 }
20939 if (!binary || li->li_next != NULL)
20940 if (putc('\n', fd) == EOF)
20941 {
20942 ret = FAIL;
20943 break;
20944 }
20945 if (ret == FAIL)
20946 {
20947 EMSG(_(e_write));
20948 break;
20949 }
20950 }
20951 return ret;
20952}
20953
20954/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020955 * "writefile()" function
20956 */
20957 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020958f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020959{
20960 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020961 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020962 char_u *fname;
20963 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020964 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020965
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020966 if (check_restricted() || check_secure())
20967 return;
20968
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020969 if (argvars[0].v_type != VAR_LIST)
20970 {
20971 EMSG2(_(e_listarg), "writefile()");
20972 return;
20973 }
20974 if (argvars[0].vval.v_list == NULL)
20975 return;
20976
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020977 if (argvars[2].v_type != VAR_UNKNOWN)
20978 {
20979 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20980 binary = TRUE;
20981 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20982 append = TRUE;
20983 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020984
20985 /* Always open the file in binary mode, library functions have a mind of
20986 * their own about CR-LF conversion. */
20987 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020988 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20989 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020990 {
20991 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20992 ret = -1;
20993 }
20994 else
20995 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020996 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20997 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020998 fclose(fd);
20999 }
21000
21001 rettv->vval.v_number = ret;
21002}
21003
21004/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021005 * "xor(expr, expr)" function
21006 */
21007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021008f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010021009{
21010 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
21011 ^ get_tv_number_chk(&argvars[1], NULL);
21012}
21013
21014
21015/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021016 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021017 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021018 */
21019 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021020var2fpos(
21021 typval_T *varp,
21022 int dollar_lnum, /* TRUE when $ is last line */
21023 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021025 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021026 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000021027 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021028
Bram Moolenaara5525202006-03-02 22:52:09 +000021029 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021030 if (varp->v_type == VAR_LIST)
21031 {
21032 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021033 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000021034 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000021035 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021036
21037 l = varp->vval.v_list;
21038 if (l == NULL)
21039 return NULL;
21040
21041 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021042 pos.lnum = list_find_nr(l, 0L, &error);
21043 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021044 return NULL; /* invalid line number */
21045
21046 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021047 pos.col = list_find_nr(l, 1L, &error);
21048 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021049 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021050 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000021051
21052 /* We accept "$" for the column number: last column. */
21053 li = list_find(l, 1L);
21054 if (li != NULL && li->li_tv.v_type == VAR_STRING
21055 && li->li_tv.vval.v_string != NULL
21056 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21057 pos.col = len + 1;
21058
Bram Moolenaara5525202006-03-02 22:52:09 +000021059 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021060 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021061 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021062 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021063
Bram Moolenaara5525202006-03-02 22:52:09 +000021064#ifdef FEAT_VIRTUALEDIT
21065 /* Get the virtual offset. Defaults to zero. */
21066 pos.coladd = list_find_nr(l, 2L, &error);
21067 if (error)
21068 pos.coladd = 0;
21069#endif
21070
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021071 return &pos;
21072 }
21073
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021074 name = get_tv_string_chk(varp);
21075 if (name == NULL)
21076 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021077 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021078 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021079 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21080 {
21081 if (VIsual_active)
21082 return &VIsual;
21083 return &curwin->w_cursor;
21084 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021085 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021086 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021087 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21089 return NULL;
21090 return pp;
21091 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021092
21093#ifdef FEAT_VIRTUALEDIT
21094 pos.coladd = 0;
21095#endif
21096
Bram Moolenaar477933c2007-07-17 14:32:23 +000021097 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021098 {
21099 pos.col = 0;
21100 if (name[1] == '0') /* "w0": first visible line */
21101 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021102 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021103 pos.lnum = curwin->w_topline;
21104 return &pos;
21105 }
21106 else if (name[1] == '$') /* "w$": last visible line */
21107 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021108 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021109 pos.lnum = curwin->w_botline - 1;
21110 return &pos;
21111 }
21112 }
21113 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021114 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021115 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021116 {
21117 pos.lnum = curbuf->b_ml.ml_line_count;
21118 pos.col = 0;
21119 }
21120 else
21121 {
21122 pos.lnum = curwin->w_cursor.lnum;
21123 pos.col = (colnr_T)STRLEN(ml_get_curline());
21124 }
21125 return &pos;
21126 }
21127 return NULL;
21128}
21129
21130/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021131 * Convert list in "arg" into a position and optional file number.
21132 * When "fnump" is NULL there is no file number, only 3 items.
21133 * Note that the column is passed on as-is, the caller may want to decrement
21134 * it to use 1 for the first column.
21135 * Return FAIL when conversion is not possible, doesn't check the position for
21136 * validity.
21137 */
21138 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021139list2fpos(
21140 typval_T *arg,
21141 pos_T *posp,
21142 int *fnump,
21143 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021144{
21145 list_T *l = arg->vval.v_list;
21146 long i = 0;
21147 long n;
21148
Bram Moolenaar493c1782014-05-28 14:34:46 +020021149 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21150 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021151 if (arg->v_type != VAR_LIST
21152 || l == NULL
21153 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021154 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021155 return FAIL;
21156
21157 if (fnump != NULL)
21158 {
21159 n = list_find_nr(l, i++, NULL); /* fnum */
21160 if (n < 0)
21161 return FAIL;
21162 if (n == 0)
21163 n = curbuf->b_fnum; /* current buffer */
21164 *fnump = n;
21165 }
21166
21167 n = list_find_nr(l, i++, NULL); /* lnum */
21168 if (n < 0)
21169 return FAIL;
21170 posp->lnum = n;
21171
21172 n = list_find_nr(l, i++, NULL); /* col */
21173 if (n < 0)
21174 return FAIL;
21175 posp->col = n;
21176
21177#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021178 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021179 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021180 posp->coladd = 0;
21181 else
21182 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021183#endif
21184
Bram Moolenaar493c1782014-05-28 14:34:46 +020021185 if (curswantp != NULL)
21186 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21187
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021188 return OK;
21189}
21190
21191/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 * Get the length of an environment variable name.
21193 * Advance "arg" to the first character after the name.
21194 * Return 0 for error.
21195 */
21196 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021197get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021198{
21199 char_u *p;
21200 int len;
21201
21202 for (p = *arg; vim_isIDc(*p); ++p)
21203 ;
21204 if (p == *arg) /* no name found */
21205 return 0;
21206
21207 len = (int)(p - *arg);
21208 *arg = p;
21209 return len;
21210}
21211
21212/*
21213 * Get the length of the name of a function or internal variable.
21214 * "arg" is advanced to the first non-white character after the name.
21215 * Return 0 if something is wrong.
21216 */
21217 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021218get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021219{
21220 char_u *p;
21221 int len;
21222
21223 /* Find the end of the name. */
21224 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021225 {
21226 if (*p == ':')
21227 {
21228 /* "s:" is start of "s:var", but "n:" is not and can be used in
21229 * slice "[n:]". Also "xx:" is not a namespace. */
21230 len = (int)(p - *arg);
21231 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21232 || len > 1)
21233 break;
21234 }
21235 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021236 if (p == *arg) /* no name found */
21237 return 0;
21238
21239 len = (int)(p - *arg);
21240 *arg = skipwhite(p);
21241
21242 return len;
21243}
21244
21245/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021246 * Get the length of the name of a variable or function.
21247 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021248 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021249 * Return -1 if curly braces expansion failed.
21250 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 * If the name contains 'magic' {}'s, expand them and return the
21252 * expanded name in an allocated string via 'alias' - caller must free.
21253 */
21254 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021255get_name_len(
21256 char_u **arg,
21257 char_u **alias,
21258 int evaluate,
21259 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021260{
21261 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021262 char_u *p;
21263 char_u *expr_start;
21264 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021265
21266 *alias = NULL; /* default to no alias */
21267
21268 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21269 && (*arg)[2] == (int)KE_SNR)
21270 {
21271 /* hard coded <SNR>, already translated */
21272 *arg += 3;
21273 return get_id_len(arg) + 3;
21274 }
21275 len = eval_fname_script(*arg);
21276 if (len > 0)
21277 {
21278 /* literal "<SID>", "s:" or "<SNR>" */
21279 *arg += len;
21280 }
21281
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021285 p = find_name_end(*arg, &expr_start, &expr_end,
21286 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021287 if (expr_start != NULL)
21288 {
21289 char_u *temp_string;
21290
21291 if (!evaluate)
21292 {
21293 len += (int)(p - *arg);
21294 *arg = skipwhite(p);
21295 return len;
21296 }
21297
21298 /*
21299 * Include any <SID> etc in the expanded string:
21300 * Thus the -len here.
21301 */
21302 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21303 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021304 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021305 *alias = temp_string;
21306 *arg = skipwhite(p);
21307 return (int)STRLEN(temp_string);
21308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021309
21310 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021311 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021312 EMSG2(_(e_invexpr2), *arg);
21313
21314 return len;
21315}
21316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021317/*
21318 * Find the end of a variable or function name, taking care of magic braces.
21319 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21320 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021321 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021322 * Return a pointer to just after the name. Equal to "arg" if there is no
21323 * valid name.
21324 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021325 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021326find_name_end(
21327 char_u *arg,
21328 char_u **expr_start,
21329 char_u **expr_end,
21330 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021332 int mb_nest = 0;
21333 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021335 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021337 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021338 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021339 *expr_start = NULL;
21340 *expr_end = NULL;
21341 }
21342
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021343 /* Quick check for valid starting character. */
21344 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21345 return arg;
21346
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021347 for (p = arg; *p != NUL
21348 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021349 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021350 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021351 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021352 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021353 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021354 if (*p == '\'')
21355 {
21356 /* skip over 'string' to avoid counting [ and ] inside it. */
21357 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21358 ;
21359 if (*p == NUL)
21360 break;
21361 }
21362 else if (*p == '"')
21363 {
21364 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21365 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21366 if (*p == '\\' && p[1] != NUL)
21367 ++p;
21368 if (*p == NUL)
21369 break;
21370 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021371 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21372 {
21373 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021374 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021375 len = (int)(p - arg);
21376 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021377 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021378 break;
21379 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021381 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021383 if (*p == '[')
21384 ++br_nest;
21385 else if (*p == ']')
21386 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021387 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021388
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021389 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021390 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021391 if (*p == '{')
21392 {
21393 mb_nest++;
21394 if (expr_start != NULL && *expr_start == NULL)
21395 *expr_start = p;
21396 }
21397 else if (*p == '}')
21398 {
21399 mb_nest--;
21400 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21401 *expr_end = p;
21402 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021403 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021404 }
21405
21406 return p;
21407}
21408
21409/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021410 * Expands out the 'magic' {}'s in a variable/function name.
21411 * Note that this can call itself recursively, to deal with
21412 * constructs like foo{bar}{baz}{bam}
21413 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21414 * "in_start" ^
21415 * "expr_start" ^
21416 * "expr_end" ^
21417 * "in_end" ^
21418 *
21419 * Returns a new allocated string, which the caller must free.
21420 * Returns NULL for failure.
21421 */
21422 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021423make_expanded_name(
21424 char_u *in_start,
21425 char_u *expr_start,
21426 char_u *expr_end,
21427 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021428{
21429 char_u c1;
21430 char_u *retval = NULL;
21431 char_u *temp_result;
21432 char_u *nextcmd = NULL;
21433
21434 if (expr_end == NULL || in_end == NULL)
21435 return NULL;
21436 *expr_start = NUL;
21437 *expr_end = NUL;
21438 c1 = *in_end;
21439 *in_end = NUL;
21440
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021441 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021442 if (temp_result != NULL && nextcmd == NULL)
21443 {
21444 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21445 + (in_end - expr_end) + 1));
21446 if (retval != NULL)
21447 {
21448 STRCPY(retval, in_start);
21449 STRCAT(retval, temp_result);
21450 STRCAT(retval, expr_end + 1);
21451 }
21452 }
21453 vim_free(temp_result);
21454
21455 *in_end = c1; /* put char back for error messages */
21456 *expr_start = '{';
21457 *expr_end = '}';
21458
21459 if (retval != NULL)
21460 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021461 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021462 if (expr_start != NULL)
21463 {
21464 /* Further expansion! */
21465 temp_result = make_expanded_name(retval, expr_start,
21466 expr_end, temp_result);
21467 vim_free(retval);
21468 retval = temp_result;
21469 }
21470 }
21471
21472 return retval;
21473}
21474
21475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021476 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021477 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021478 */
21479 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021480eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021481{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021482 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21483}
21484
21485/*
21486 * Return TRUE if character "c" can be used as the first character in a
21487 * variable or function name (excluding '{' and '}').
21488 */
21489 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021490eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021491{
21492 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493}
21494
21495/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496 * Set number v: variable to "val".
21497 */
21498 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021499set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021500{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021501 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021502}
21503
21504/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021505 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021506 */
21507 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021510 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021511}
21512
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021513/*
21514 * Get string v: variable value. Uses a static buffer, can only be used once.
21515 */
21516 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021517get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021518{
21519 return get_tv_string(&vimvars[idx].vv_tv);
21520}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021521
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021523 * Get List v: variable value. Caller must take care of reference count when
21524 * needed.
21525 */
21526 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021527get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021528{
21529 return vimvars[idx].vv_list;
21530}
21531
21532/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021533 * Set v:char to character "c".
21534 */
21535 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021536set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021537{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021538 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021539
21540#ifdef FEAT_MBYTE
21541 if (has_mbyte)
21542 buf[(*mb_char2bytes)(c, buf)] = NUL;
21543 else
21544#endif
21545 {
21546 buf[0] = c;
21547 buf[1] = NUL;
21548 }
21549 set_vim_var_string(VV_CHAR, buf, -1);
21550}
21551
21552/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021553 * Set v:count to "count" and v:count1 to "count1".
21554 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021555 */
21556 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021557set_vcount(
21558 long count,
21559 long count1,
21560 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021561{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021562 if (set_prevcount)
21563 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021564 vimvars[VV_COUNT].vv_nr = count;
21565 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021566}
21567
21568/*
21569 * Set string v: variable to a copy of "val".
21570 */
21571 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021572set_vim_var_string(
21573 int idx,
21574 char_u *val,
21575 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021576{
Bram Moolenaara542c682016-01-31 16:28:04 +010021577 clear_tv(&vimvars[idx].vv_di.di_tv);
21578 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021580 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021581 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021582 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021583 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021584 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021585}
21586
21587/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021588 * Set List v: variable to "val".
21589 */
21590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021591set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021592{
Bram Moolenaara542c682016-01-31 16:28:04 +010021593 clear_tv(&vimvars[idx].vv_di.di_tv);
21594 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021595 vimvars[idx].vv_list = val;
21596 if (val != NULL)
21597 ++val->lv_refcount;
21598}
21599
21600/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021601 * Set Dictionary v: variable to "val".
21602 */
21603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021604set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021605{
21606 int todo;
21607 hashitem_T *hi;
21608
Bram Moolenaara542c682016-01-31 16:28:04 +010021609 clear_tv(&vimvars[idx].vv_di.di_tv);
21610 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021611 vimvars[idx].vv_dict = val;
21612 if (val != NULL)
21613 {
21614 ++val->dv_refcount;
21615
21616 /* Set readonly */
21617 todo = (int)val->dv_hashtab.ht_used;
21618 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21619 {
21620 if (HASHITEM_EMPTY(hi))
21621 continue;
21622 --todo;
21623 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21624 }
21625 }
21626}
21627
21628/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021629 * Set v:register if needed.
21630 */
21631 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021632set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021633{
21634 char_u regname;
21635
21636 if (c == 0 || c == ' ')
21637 regname = '"';
21638 else
21639 regname = c;
21640 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021641 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 set_vim_var_string(VV_REG, &regname, 1);
21643}
21644
21645/*
21646 * Get or set v:exception. If "oldval" == NULL, return the current value.
21647 * Otherwise, restore the value to "oldval" and return NULL.
21648 * Must always be called in pairs to save and restore v:exception! Does not
21649 * take care of memory allocations.
21650 */
21651 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021652v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021653{
21654 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021655 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021656
Bram Moolenaare9a41262005-01-15 22:18:47 +000021657 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021658 return NULL;
21659}
21660
21661/*
21662 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21663 * Otherwise, restore the value to "oldval" and return NULL.
21664 * Must always be called in pairs to save and restore v:throwpoint! Does not
21665 * take care of memory allocations.
21666 */
21667 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021668v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021669{
21670 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021671 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021672
Bram Moolenaare9a41262005-01-15 22:18:47 +000021673 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021674 return NULL;
21675}
21676
21677#if defined(FEAT_AUTOCMD) || defined(PROTO)
21678/*
21679 * Set v:cmdarg.
21680 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21681 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21682 * Must always be called in pairs!
21683 */
21684 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021685set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021686{
21687 char_u *oldval;
21688 char_u *newval;
21689 unsigned len;
21690
Bram Moolenaare9a41262005-01-15 22:18:47 +000021691 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021692 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021693 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021694 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021695 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021696 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021697 }
21698
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021699 if (eap->force_bin == FORCE_BIN)
21700 len = 6;
21701 else if (eap->force_bin == FORCE_NOBIN)
21702 len = 8;
21703 else
21704 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021705
21706 if (eap->read_edit)
21707 len += 7;
21708
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021709 if (eap->force_ff != 0)
21710 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21711# ifdef FEAT_MBYTE
21712 if (eap->force_enc != 0)
21713 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021714 if (eap->bad_char != 0)
21715 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021716# endif
21717
21718 newval = alloc(len + 1);
21719 if (newval == NULL)
21720 return NULL;
21721
21722 if (eap->force_bin == FORCE_BIN)
21723 sprintf((char *)newval, " ++bin");
21724 else if (eap->force_bin == FORCE_NOBIN)
21725 sprintf((char *)newval, " ++nobin");
21726 else
21727 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021728
21729 if (eap->read_edit)
21730 STRCAT(newval, " ++edit");
21731
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021732 if (eap->force_ff != 0)
21733 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21734 eap->cmd + eap->force_ff);
21735# ifdef FEAT_MBYTE
21736 if (eap->force_enc != 0)
21737 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21738 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021739 if (eap->bad_char == BAD_KEEP)
21740 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21741 else if (eap->bad_char == BAD_DROP)
21742 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21743 else if (eap->bad_char != 0)
21744 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021745# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021746 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021747 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748}
21749#endif
21750
21751/*
21752 * Get the value of internal variable "name".
21753 * Return OK or FAIL.
21754 */
21755 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021756get_var_tv(
21757 char_u *name,
21758 int len, /* length of "name" */
21759 typval_T *rettv, /* NULL when only checking existence */
21760 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21761 int verbose, /* may give error message */
21762 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021763{
21764 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021765 typval_T *tv = NULL;
21766 typval_T atv;
21767 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021768 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021769
21770 /* truncate the name, so that we can use strcmp() */
21771 cc = name[len];
21772 name[len] = NUL;
21773
21774 /*
21775 * Check for "b:changedtick".
21776 */
21777 if (STRCMP(name, "b:changedtick") == 0)
21778 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021779 atv.v_type = VAR_NUMBER;
21780 atv.vval.v_number = curbuf->b_changedtick;
21781 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021782 }
21783
21784 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 * Check for user-defined variables.
21786 */
21787 else
21788 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021789 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021791 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021792 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021793 if (dip != NULL)
21794 *dip = v;
21795 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021796 }
21797
Bram Moolenaare9a41262005-01-15 22:18:47 +000021798 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021799 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021800 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021801 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802 ret = FAIL;
21803 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021804 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021805 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806
21807 name[len] = cc;
21808
21809 return ret;
21810}
21811
21812/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021813 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21814 * Also handle function call with Funcref variable: func(expr)
21815 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21816 */
21817 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021818handle_subscript(
21819 char_u **arg,
21820 typval_T *rettv,
21821 int evaluate, /* do more than finding the end */
21822 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021823{
21824 int ret = OK;
21825 dict_T *selfdict = NULL;
21826 char_u *s;
21827 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021828 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021829
21830 while (ret == OK
21831 && (**arg == '['
21832 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021833 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21834 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021835 && !vim_iswhite(*(*arg - 1)))
21836 {
21837 if (**arg == '(')
21838 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021839 partial_T *pt = NULL;
21840
Bram Moolenaard9fba312005-06-26 22:34:35 +000021841 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021842 if (evaluate)
21843 {
21844 functv = *rettv;
21845 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021846
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021847 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021848 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021849 {
21850 pt = functv.vval.v_partial;
21851 s = pt->pt_name;
21852 }
21853 else
21854 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021855 }
21856 else
21857 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021858 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021859 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021860 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021861
21862 /* Clear the funcref afterwards, so that deleting it while
21863 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021864 if (evaluate)
21865 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021866
21867 /* Stop the expression evaluation when immediately aborting on
21868 * error, or when an interrupt occurred or an exception was thrown
21869 * but not caught. */
21870 if (aborting())
21871 {
21872 if (ret == OK)
21873 clear_tv(rettv);
21874 ret = FAIL;
21875 }
21876 dict_unref(selfdict);
21877 selfdict = NULL;
21878 }
21879 else /* **arg == '[' || **arg == '.' */
21880 {
21881 dict_unref(selfdict);
21882 if (rettv->v_type == VAR_DICT)
21883 {
21884 selfdict = rettv->vval.v_dict;
21885 if (selfdict != NULL)
21886 ++selfdict->dv_refcount;
21887 }
21888 else
21889 selfdict = NULL;
21890 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21891 {
21892 clear_tv(rettv);
21893 ret = FAIL;
21894 }
21895 }
21896 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021897
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021898 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21899 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021900 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021901 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21902 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021903 char_u *tofree = NULL;
21904 ufunc_T *fp;
21905 char_u fname_buf[FLEN_FIXED + 1];
21906 int error;
21907
21908 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021909 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021910 fp = find_func(fname);
21911 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021912
21913 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021914 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021915 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021916 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21917
21918 if (pt != NULL)
21919 {
21920 pt->pt_refcount = 1;
21921 pt->pt_dict = selfdict;
21922 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021923 if (rettv->v_type == VAR_FUNC)
21924 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021925 /* Just a function: Take over the function name and use
21926 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021927 pt->pt_name = rettv->vval.v_string;
21928 }
21929 else
21930 {
21931 partial_T *ret_pt = rettv->vval.v_partial;
21932 int i;
21933
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021934 /* Partial: copy the function name, use selfdict and copy
21935 * args. Can't take over name or args, the partial might
21936 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021937 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021938 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021939 if (ret_pt->pt_argc > 0)
21940 {
21941 pt->pt_argv = (typval_T *)alloc(
21942 sizeof(typval_T) * ret_pt->pt_argc);
21943 if (pt->pt_argv == NULL)
21944 /* out of memory: drop the arguments */
21945 pt->pt_argc = 0;
21946 else
21947 {
21948 pt->pt_argc = ret_pt->pt_argc;
21949 for (i = 0; i < pt->pt_argc; i++)
21950 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21951 }
21952 }
21953 partial_unref(ret_pt);
21954 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021955 rettv->v_type = VAR_PARTIAL;
21956 rettv->vval.v_partial = pt;
21957 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021958 }
21959 }
21960
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021961 dict_unref(selfdict);
21962 return ret;
21963}
21964
21965/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021966 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021967 * value).
21968 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021969 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021970alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021971{
Bram Moolenaar33570922005-01-25 22:26:29 +000021972 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021973}
21974
21975/*
21976 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021977 * The string "s" must have been allocated, it is consumed.
21978 * Return NULL for out of memory, the variable otherwise.
21979 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021980 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021981alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982{
Bram Moolenaar33570922005-01-25 22:26:29 +000021983 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021984
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021985 rettv = alloc_tv();
21986 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021987 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021988 rettv->v_type = VAR_STRING;
21989 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990 }
21991 else
21992 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021993 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994}
21995
21996/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021998 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021999 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022000free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022001{
22002 if (varp != NULL)
22003 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022004 switch (varp->v_type)
22005 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022007 func_unref(varp->vval.v_string);
22008 /*FALLTHROUGH*/
22009 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022010 vim_free(varp->vval.v_string);
22011 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022012 case VAR_PARTIAL:
22013 partial_unref(varp->vval.v_partial);
22014 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022015 case VAR_LIST:
22016 list_unref(varp->vval.v_list);
22017 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022018 case VAR_DICT:
22019 dict_unref(varp->vval.v_dict);
22020 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022021 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022022#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022023 job_unref(varp->vval.v_job);
22024 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022025#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022026 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022027#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022028 channel_unref(varp->vval.v_channel);
22029 break;
22030#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022031 case VAR_NUMBER:
22032 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022033 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010022034 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000022035 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022036 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 vim_free(varp);
22038 }
22039}
22040
22041/*
22042 * Free the memory for a variable value and set the value to NULL or 0.
22043 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022044 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046{
22047 if (varp != NULL)
22048 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022049 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022050 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022051 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022052 func_unref(varp->vval.v_string);
22053 /*FALLTHROUGH*/
22054 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022055 vim_free(varp->vval.v_string);
22056 varp->vval.v_string = NULL;
22057 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022058 case VAR_PARTIAL:
22059 partial_unref(varp->vval.v_partial);
22060 varp->vval.v_partial = NULL;
22061 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022062 case VAR_LIST:
22063 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022064 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022065 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022066 case VAR_DICT:
22067 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022068 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022069 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022070 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022071 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 varp->vval.v_number = 0;
22073 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022074 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022075#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022076 varp->vval.v_float = 0.0;
22077 break;
22078#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022079 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022080#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022081 job_unref(varp->vval.v_job);
22082 varp->vval.v_job = NULL;
22083#endif
22084 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022085 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022086#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022087 channel_unref(varp->vval.v_channel);
22088 varp->vval.v_channel = NULL;
22089#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022090 case VAR_UNKNOWN:
22091 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022092 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022093 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022094 }
22095}
22096
22097/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022098 * Set the value of a variable to NULL without freeing items.
22099 */
22100 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022101init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022102{
22103 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022104 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022105}
22106
22107/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022108 * Get the number value of a variable.
22109 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022110 * For incompatible types, return 0.
22111 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22112 * caller of incompatible types: it sets *denote to TRUE if "denote"
22113 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022115 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022116get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022117{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022118 int error = FALSE;
22119
22120 return get_tv_number_chk(varp, &error); /* return 0L on error */
22121}
22122
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022123 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022125{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022127
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022128 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022129 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022130 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022131 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022132 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022133#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022134 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022135 break;
22136#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022137 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022138 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022139 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022140 break;
22141 case VAR_STRING:
22142 if (varp->vval.v_string != NULL)
22143 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022144 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022145 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022146 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022147 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022148 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022149 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022150 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022151 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022152 case VAR_SPECIAL:
22153 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22154 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022155 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022156#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022157 EMSG(_("E910: Using a Job as a Number"));
22158 break;
22159#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022160 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022161#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022162 EMSG(_("E913: Using a Channel as a Number"));
22163 break;
22164#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022165 case VAR_UNKNOWN:
22166 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022167 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022169 if (denote == NULL) /* useful for values that must be unsigned */
22170 n = -1;
22171 else
22172 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022173 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022174}
22175
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022176#ifdef FEAT_FLOAT
22177 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022178get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022179{
22180 switch (varp->v_type)
22181 {
22182 case VAR_NUMBER:
22183 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022184 case VAR_FLOAT:
22185 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022186 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022187 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022188 EMSG(_("E891: Using a Funcref as a Float"));
22189 break;
22190 case VAR_STRING:
22191 EMSG(_("E892: Using a String as a Float"));
22192 break;
22193 case VAR_LIST:
22194 EMSG(_("E893: Using a List as a Float"));
22195 break;
22196 case VAR_DICT:
22197 EMSG(_("E894: Using a Dictionary as a Float"));
22198 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022199 case VAR_SPECIAL:
22200 EMSG(_("E907: Using a special value as a Float"));
22201 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022202 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022203# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022204 EMSG(_("E911: Using a Job as a Float"));
22205 break;
22206# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022207 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022208# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022209 EMSG(_("E914: Using a Channel as a Float"));
22210 break;
22211# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022212 case VAR_UNKNOWN:
22213 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022214 break;
22215 }
22216 return 0;
22217}
22218#endif
22219
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022221 * Get the lnum from the first argument.
22222 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022223 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224 */
22225 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022226get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022227{
Bram Moolenaar33570922005-01-25 22:26:29 +000022228 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022229 linenr_T lnum;
22230
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022231 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022232 if (lnum == 0) /* no valid number, try using line() */
22233 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022234 rettv.v_type = VAR_NUMBER;
22235 f_line(argvars, &rettv);
22236 lnum = rettv.vval.v_number;
22237 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022238 }
22239 return lnum;
22240}
22241
22242/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022243 * Get the lnum from the first argument.
22244 * Also accepts "$", then "buf" is used.
22245 * Returns 0 on error.
22246 */
22247 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022248get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022249{
22250 if (argvars[0].v_type == VAR_STRING
22251 && argvars[0].vval.v_string != NULL
22252 && argvars[0].vval.v_string[0] == '$'
22253 && buf != NULL)
22254 return buf->b_ml.ml_line_count;
22255 return get_tv_number_chk(&argvars[0], NULL);
22256}
22257
22258/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022259 * Get the string value of a variable.
22260 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022261 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22262 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022263 * If the String variable has never been set, return an empty string.
22264 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022265 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22266 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022267 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022268 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022269get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270{
22271 static char_u mybuf[NUMBUFLEN];
22272
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022273 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274}
22275
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022276 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022277get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022278{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022279 char_u *res = get_tv_string_buf_chk(varp, buf);
22280
22281 return res != NULL ? res : (char_u *)"";
22282}
22283
Bram Moolenaar7d647822014-04-05 21:28:56 +020022284/*
22285 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22286 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022287 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022288get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022289{
22290 static char_u mybuf[NUMBUFLEN];
22291
22292 return get_tv_string_buf_chk(varp, mybuf);
22293}
22294
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022295 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022296get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022297{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022298 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022299 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022300 case VAR_NUMBER:
22301 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22302 return buf;
22303 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022304 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022305 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022306 break;
22307 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022308 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022309 break;
22310 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022311 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022312 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022313 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022314#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022315 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022316 break;
22317#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022318 case VAR_STRING:
22319 if (varp->vval.v_string != NULL)
22320 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022321 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022322 case VAR_SPECIAL:
22323 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22324 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022325 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022326#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022327 {
22328 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022329 char *status;
22330
22331 if (job == NULL)
22332 return (char_u *)"no process";
22333 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022334 : job->jv_status == JOB_ENDED ? "dead"
22335 : "run";
22336# ifdef UNIX
22337 vim_snprintf((char *)buf, NUMBUFLEN,
22338 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022339# elif defined(WIN32)
22340 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022341 "process %ld %s",
22342 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022343 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022344# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022345 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022346 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22347# endif
22348 return buf;
22349 }
22350#endif
22351 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022352 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022353#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022354 {
22355 channel_T *channel = varp->vval.v_channel;
22356 char *status = channel_status(channel);
22357
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022358 if (channel == NULL)
22359 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22360 else
22361 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022362 "channel %d %s", channel->ch_id, status);
22363 return buf;
22364 }
22365#endif
22366 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022367 case VAR_UNKNOWN:
22368 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022369 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022370 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022371 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022372}
22373
22374/*
22375 * Find variable "name" in the list of variables.
22376 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022377 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022378 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022379 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022380 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022381 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022382find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022383{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022384 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022385 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386
Bram Moolenaara7043832005-01-21 11:56:39 +000022387 ht = find_var_ht(name, &varname);
22388 if (htp != NULL)
22389 *htp = ht;
22390 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022391 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022392 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022393}
22394
22395/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022396 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022397 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022398 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022400find_var_in_ht(
22401 hashtab_T *ht,
22402 int htname,
22403 char_u *varname,
22404 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022405{
Bram Moolenaar33570922005-01-25 22:26:29 +000022406 hashitem_T *hi;
22407
22408 if (*varname == NUL)
22409 {
22410 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022411 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022412 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022413 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022414 case 'g': return &globvars_var;
22415 case 'v': return &vimvars_var;
22416 case 'b': return &curbuf->b_bufvar;
22417 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022418#ifdef FEAT_WINDOWS
22419 case 't': return &curtab->tp_winvar;
22420#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022421 case 'l': return current_funccal == NULL
22422 ? NULL : &current_funccal->l_vars_var;
22423 case 'a': return current_funccal == NULL
22424 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022425 }
22426 return NULL;
22427 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022428
22429 hi = hash_find(ht, varname);
22430 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 {
22432 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022433 * worked find the variable again. Don't auto-load a script if it was
22434 * loaded already, otherwise it would be loaded every time when
22435 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022436 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022437 {
22438 /* Note: script_autoload() may make "hi" invalid. It must either
22439 * be obtained again or not used. */
22440 if (!script_autoload(varname, FALSE) || aborting())
22441 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022442 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022443 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022444 if (HASHITEM_EMPTY(hi))
22445 return NULL;
22446 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022447 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022448}
22449
22450/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022452 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022453 * Set "varname" to the start of name without ':'.
22454 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022455 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022456find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022457{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022458 hashitem_T *hi;
22459
Bram Moolenaar73627d02015-08-11 15:46:09 +020022460 if (name[0] == NUL)
22461 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022462 if (name[1] != ':')
22463 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022464 /* The name must not start with a colon or #. */
22465 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022466 return NULL;
22467 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022468
22469 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022470 hi = hash_find(&compat_hashtab, name);
22471 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022472 return &compat_hashtab;
22473
Bram Moolenaar071d4272004-06-13 20:20:40 +000022474 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022476 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 }
22478 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022479 if (*name == 'g') /* global variable */
22480 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022481 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22482 */
22483 if (vim_strchr(name + 2, ':') != NULL
22484 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022485 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022486 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022487 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022488 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022489 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022490#ifdef FEAT_WINDOWS
22491 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022492 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022493#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022494 if (*name == 'v') /* v: variable */
22495 return &vimvarht;
22496 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022497 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022498 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022499 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500 if (*name == 's' /* script variable */
22501 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22502 return &SCRIPT_VARS(current_SID);
22503 return NULL;
22504}
22505
22506/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022507 * Get function call environment based on bactrace debug level
22508 */
22509 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022510get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022511{
22512 int i;
22513 funccall_T *funccal;
22514 funccall_T *temp_funccal;
22515
22516 funccal = current_funccal;
22517 if (debug_backtrace_level > 0)
22518 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022519 for (i = 0; i < debug_backtrace_level; i++)
22520 {
22521 temp_funccal = funccal->caller;
22522 if (temp_funccal)
22523 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022524 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022525 /* backtrace level overflow. reset to max */
22526 debug_backtrace_level = i;
22527 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022528 }
22529 return funccal;
22530}
22531
22532/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022534 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535 * Returns NULL when it doesn't exist.
22536 */
22537 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022538get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022539{
Bram Moolenaar33570922005-01-25 22:26:29 +000022540 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022541
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022542 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022543 if (v == NULL)
22544 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022545 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546}
22547
22548/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022549 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550 * sourcing this script and when executing functions defined in the script.
22551 */
22552 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022553new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554{
Bram Moolenaara7043832005-01-21 11:56:39 +000022555 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022556 hashtab_T *ht;
22557 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022558
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22560 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022561 /* Re-allocating ga_data means that an ht_array pointing to
22562 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022563 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022564 for (i = 1; i <= ga_scripts.ga_len; ++i)
22565 {
22566 ht = &SCRIPT_VARS(i);
22567 if (ht->ht_mask == HT_INIT_SIZE - 1)
22568 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022569 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022570 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022571 }
22572
Bram Moolenaar071d4272004-06-13 20:20:40 +000022573 while (ga_scripts.ga_len < id)
22574 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022575 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022576 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022577 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022579 }
22580 }
22581}
22582
22583/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022584 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22585 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022586 */
22587 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022588init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022589{
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022591 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022592 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022593 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022594 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022595 dict_var->di_tv.vval.v_dict = dict;
22596 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022597 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022598 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22599 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022600}
22601
22602/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022603 * Unreference a dictionary initialized by init_var_dict().
22604 */
22605 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022606unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022607{
22608 /* Now the dict needs to be freed if no one else is using it, go back to
22609 * normal reference counting. */
22610 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22611 dict_unref(dict);
22612}
22613
22614/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022616 * Frees all allocated variables and the value they contain.
22617 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022618 */
22619 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022620vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022621{
22622 vars_clear_ext(ht, TRUE);
22623}
22624
22625/*
22626 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22627 */
22628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022629vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022630{
Bram Moolenaara7043832005-01-21 11:56:39 +000022631 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022632 hashitem_T *hi;
22633 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634
Bram Moolenaar33570922005-01-25 22:26:29 +000022635 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022636 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022637 for (hi = ht->ht_array; todo > 0; ++hi)
22638 {
22639 if (!HASHITEM_EMPTY(hi))
22640 {
22641 --todo;
22642
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022644 * ht_array might change then. hash_clear() takes care of it
22645 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022646 v = HI2DI(hi);
22647 if (free_val)
22648 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022649 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022650 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022651 }
22652 }
22653 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022654 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022655}
22656
Bram Moolenaara7043832005-01-21 11:56:39 +000022657/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022658 * Delete a variable from hashtab "ht" at item "hi".
22659 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022660 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022662delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022663{
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022665
22666 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022667 clear_tv(&di->di_tv);
22668 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022669}
22670
22671/*
22672 * List the value of one internal variable.
22673 */
22674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022675list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022677 char_u *tofree;
22678 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022679 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022680
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022681 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022682 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022683 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022684 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022685}
22686
Bram Moolenaar071d4272004-06-13 20:20:40 +000022687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022688list_one_var_a(
22689 char_u *prefix,
22690 char_u *name,
22691 int type,
22692 char_u *string,
22693 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022694{
Bram Moolenaar31859182007-08-14 20:41:13 +000022695 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22696 msg_start();
22697 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022698 if (name != NULL) /* "a:" vars don't have a name stored */
22699 msg_puts(name);
22700 msg_putchar(' ');
22701 msg_advance(22);
22702 if (type == VAR_NUMBER)
22703 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022704 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022705 msg_putchar('*');
22706 else if (type == VAR_LIST)
22707 {
22708 msg_putchar('[');
22709 if (*string == '[')
22710 ++string;
22711 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022712 else if (type == VAR_DICT)
22713 {
22714 msg_putchar('{');
22715 if (*string == '{')
22716 ++string;
22717 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022718 else
22719 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022720
Bram Moolenaar071d4272004-06-13 20:20:40 +000022721 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022722
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022723 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022724 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022725 if (*first)
22726 {
22727 msg_clr_eos();
22728 *first = FALSE;
22729 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022730}
22731
22732/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022733 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022734 * If the variable already exists, the value is updated.
22735 * Otherwise the variable is created.
22736 */
22737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022738set_var(
22739 char_u *name,
22740 typval_T *tv,
22741 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022742{
Bram Moolenaar33570922005-01-25 22:26:29 +000022743 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022744 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022746
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022747 ht = find_var_ht(name, &varname);
22748 if (ht == NULL || *varname == NUL)
22749 {
22750 EMSG2(_(e_illvar), name);
22751 return;
22752 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022753 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022754
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022755 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22756 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022757 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022758
Bram Moolenaar33570922005-01-25 22:26:29 +000022759 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022760 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022761 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022762 if (var_check_ro(v->di_flags, name, FALSE)
22763 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022764 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022765
22766 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022767 * Handle setting internal v: variables separately where needed to
22768 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 */
22770 if (ht == &vimvarht)
22771 {
22772 if (v->di_tv.v_type == VAR_STRING)
22773 {
22774 vim_free(v->di_tv.vval.v_string);
22775 if (copy || tv->v_type != VAR_STRING)
22776 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22777 else
22778 {
22779 /* Take over the string to avoid an extra alloc/free. */
22780 v->di_tv.vval.v_string = tv->vval.v_string;
22781 tv->vval.v_string = NULL;
22782 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022783 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022784 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022785 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022786 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022788 if (STRCMP(varname, "searchforward") == 0)
22789 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022790#ifdef FEAT_SEARCH_EXTRA
22791 else if (STRCMP(varname, "hlsearch") == 0)
22792 {
22793 no_hlsearch = !v->di_tv.vval.v_number;
22794 redraw_all_later(SOME_VALID);
22795 }
22796#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022797 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022798 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022799 else if (v->di_tv.v_type != tv->v_type)
22800 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022801 }
22802
22803 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022804 }
22805 else /* add a new variable */
22806 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022807 /* Can't add "v:" variable. */
22808 if (ht == &vimvarht)
22809 {
22810 EMSG2(_(e_illvar), name);
22811 return;
22812 }
22813
Bram Moolenaar92124a32005-06-17 22:03:40 +000022814 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022815 if (!valid_varname(varname))
22816 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022817
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022818 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22819 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022820 if (v == NULL)
22821 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022822 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022823 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022824 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022825 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022826 return;
22827 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022828 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022829 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022830
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022831 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022832 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022833 else
22834 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022835 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022836 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022837 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022838 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022839}
22840
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022841/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022842 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022843 * Also give an error message.
22844 */
22845 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022846var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022847{
22848 if (flags & DI_FLAGS_RO)
22849 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022850 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022851 return TRUE;
22852 }
22853 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22854 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022855 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022856 return TRUE;
22857 }
22858 return FALSE;
22859}
22860
22861/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022862 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22863 * Also give an error message.
22864 */
22865 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022866var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022867{
22868 if (flags & DI_FLAGS_FIX)
22869 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022870 EMSG2(_("E795: Cannot delete variable %s"),
22871 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022872 return TRUE;
22873 }
22874 return FALSE;
22875}
22876
22877/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022878 * Check if a funcref is assigned to a valid variable name.
22879 * Return TRUE and give an error if not.
22880 */
22881 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022882var_check_func_name(
22883 char_u *name, /* points to start of variable name */
22884 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022885{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022886 /* Allow for w: b: s: and t:. */
22887 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022888 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22889 ? name[2] : name[0]))
22890 {
22891 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22892 name);
22893 return TRUE;
22894 }
22895 /* Don't allow hiding a function. When "v" is not NULL we might be
22896 * assigning another function to the same var, the type is checked
22897 * below. */
22898 if (new_var && function_exists(name))
22899 {
22900 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22901 name);
22902 return TRUE;
22903 }
22904 return FALSE;
22905}
22906
22907/*
22908 * Check if a variable name is valid.
22909 * Return FALSE and give an error if not.
22910 */
22911 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022912valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022913{
22914 char_u *p;
22915
22916 for (p = varname; *p != NUL; ++p)
22917 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22918 && *p != AUTOLOAD_CHAR)
22919 {
22920 EMSG2(_(e_illvar), varname);
22921 return FALSE;
22922 }
22923 return TRUE;
22924}
22925
22926/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022927 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022928 * Also give an error message, using "name" or _("name") when use_gettext is
22929 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022930 */
22931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022932tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022933{
22934 if (lock & VAR_LOCKED)
22935 {
22936 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022937 name == NULL ? (char_u *)_("Unknown")
22938 : use_gettext ? (char_u *)_(name)
22939 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022940 return TRUE;
22941 }
22942 if (lock & VAR_FIXED)
22943 {
22944 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022945 name == NULL ? (char_u *)_("Unknown")
22946 : use_gettext ? (char_u *)_(name)
22947 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022948 return TRUE;
22949 }
22950 return FALSE;
22951}
22952
22953/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022954 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022955 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022956 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022957 * It is OK for "from" and "to" to point to the same item. This is used to
22958 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022959 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022960 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022961copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022962{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022963 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022964 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022965 switch (from->v_type)
22966 {
22967 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022968 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022969 to->vval.v_number = from->vval.v_number;
22970 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022971 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022972#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022973 to->vval.v_float = from->vval.v_float;
22974 break;
22975#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022976 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022977#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022978 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022979 if (to->vval.v_job != NULL)
22980 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022981 break;
22982#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022983 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022984#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022985 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022986 if (to->vval.v_channel != NULL)
22987 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022988 break;
22989#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022990 case VAR_STRING:
22991 case VAR_FUNC:
22992 if (from->vval.v_string == NULL)
22993 to->vval.v_string = NULL;
22994 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022995 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022996 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022997 if (from->v_type == VAR_FUNC)
22998 func_ref(to->vval.v_string);
22999 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023000 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023001 case VAR_PARTIAL:
23002 if (from->vval.v_partial == NULL)
23003 to->vval.v_partial = NULL;
23004 else
23005 {
23006 to->vval.v_partial = from->vval.v_partial;
23007 ++to->vval.v_partial->pt_refcount;
23008 }
23009 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023010 case VAR_LIST:
23011 if (from->vval.v_list == NULL)
23012 to->vval.v_list = NULL;
23013 else
23014 {
23015 to->vval.v_list = from->vval.v_list;
23016 ++to->vval.v_list->lv_refcount;
23017 }
23018 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000023019 case VAR_DICT:
23020 if (from->vval.v_dict == NULL)
23021 to->vval.v_dict = NULL;
23022 else
23023 {
23024 to->vval.v_dict = from->vval.v_dict;
23025 ++to->vval.v_dict->dv_refcount;
23026 }
23027 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023028 case VAR_UNKNOWN:
23029 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023030 break;
23031 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023032}
23033
23034/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000023035 * Make a copy of an item.
23036 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023037 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
23038 * reference to an already copied list/dict can be used.
23039 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000023040 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023041 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010023042item_copy(
23043 typval_T *from,
23044 typval_T *to,
23045 int deep,
23046 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023047{
23048 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023049 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023050
Bram Moolenaar33570922005-01-25 22:26:29 +000023051 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000023052 {
23053 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023054 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023055 }
23056 ++recurse;
23057
23058 switch (from->v_type)
23059 {
23060 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023061 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023062 case VAR_STRING:
23063 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023064 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023065 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023066 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023067 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023068 copy_tv(from, to);
23069 break;
23070 case VAR_LIST:
23071 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023072 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023073 if (from->vval.v_list == NULL)
23074 to->vval.v_list = NULL;
23075 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23076 {
23077 /* use the copy made earlier */
23078 to->vval.v_list = from->vval.v_list->lv_copylist;
23079 ++to->vval.v_list->lv_refcount;
23080 }
23081 else
23082 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23083 if (to->vval.v_list == NULL)
23084 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023085 break;
23086 case VAR_DICT:
23087 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023088 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023089 if (from->vval.v_dict == NULL)
23090 to->vval.v_dict = NULL;
23091 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23092 {
23093 /* use the copy made earlier */
23094 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23095 ++to->vval.v_dict->dv_refcount;
23096 }
23097 else
23098 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23099 if (to->vval.v_dict == NULL)
23100 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023101 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023102 case VAR_UNKNOWN:
23103 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023104 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023105 }
23106 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023107 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023108}
23109
23110/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023111 * ":echo expr1 ..." print each argument separated with a space, add a
23112 * newline at the end.
23113 * ":echon expr1 ..." print each argument plain.
23114 */
23115 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023116ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023117{
23118 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023119 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023120 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023121 char_u *p;
23122 int needclr = TRUE;
23123 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023124 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023125
23126 if (eap->skip)
23127 ++emsg_skip;
23128 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023130 /* If eval1() causes an error message the text from the command may
23131 * still need to be cleared. E.g., "echo 22,44". */
23132 need_clr_eos = needclr;
23133
Bram Moolenaar071d4272004-06-13 20:20:40 +000023134 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023135 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136 {
23137 /*
23138 * Report the invalid expression unless the expression evaluation
23139 * has been cancelled due to an aborting error, an interrupt, or an
23140 * exception.
23141 */
23142 if (!aborting())
23143 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023144 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 break;
23146 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023147 need_clr_eos = FALSE;
23148
Bram Moolenaar071d4272004-06-13 20:20:40 +000023149 if (!eap->skip)
23150 {
23151 if (atstart)
23152 {
23153 atstart = FALSE;
23154 /* Call msg_start() after eval1(), evaluating the expression
23155 * may cause a message to appear. */
23156 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023157 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023158 /* Mark the saved text as finishing the line, so that what
23159 * follows is displayed on a new line when scrolling back
23160 * at the more prompt. */
23161 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023162 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023163 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164 }
23165 else if (eap->cmdidx == CMD_echo)
23166 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023167 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023168 if (p != NULL)
23169 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023170 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023171 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023172 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023173 if (*p != TAB && needclr)
23174 {
23175 /* remove any text still there from the command */
23176 msg_clr_eos();
23177 needclr = FALSE;
23178 }
23179 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023180 }
23181 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023182 {
23183#ifdef FEAT_MBYTE
23184 if (has_mbyte)
23185 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023186 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023187
23188 (void)msg_outtrans_len_attr(p, i, echo_attr);
23189 p += i - 1;
23190 }
23191 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023192#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023193 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23194 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023195 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023196 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023197 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023198 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023199 arg = skipwhite(arg);
23200 }
23201 eap->nextcmd = check_nextcmd(arg);
23202
23203 if (eap->skip)
23204 --emsg_skip;
23205 else
23206 {
23207 /* remove text that may still be there from the command */
23208 if (needclr)
23209 msg_clr_eos();
23210 if (eap->cmdidx == CMD_echo)
23211 msg_end();
23212 }
23213}
23214
23215/*
23216 * ":echohl {name}".
23217 */
23218 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023219ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023220{
23221 int id;
23222
23223 id = syn_name2id(eap->arg);
23224 if (id == 0)
23225 echo_attr = 0;
23226 else
23227 echo_attr = syn_id2attr(id);
23228}
23229
23230/*
23231 * ":execute expr1 ..." execute the result of an expression.
23232 * ":echomsg expr1 ..." Print a message
23233 * ":echoerr expr1 ..." Print an error
23234 * Each gets spaces around each argument and a newline at the end for
23235 * echo commands
23236 */
23237 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023238ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239{
23240 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023241 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023242 int ret = OK;
23243 char_u *p;
23244 garray_T ga;
23245 int len;
23246 int save_did_emsg;
23247
23248 ga_init2(&ga, 1, 80);
23249
23250 if (eap->skip)
23251 ++emsg_skip;
23252 while (*arg != NUL && *arg != '|' && *arg != '\n')
23253 {
23254 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023255 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023256 {
23257 /*
23258 * Report the invalid expression unless the expression evaluation
23259 * has been cancelled due to an aborting error, an interrupt, or an
23260 * exception.
23261 */
23262 if (!aborting())
23263 EMSG2(_(e_invexpr2), p);
23264 ret = FAIL;
23265 break;
23266 }
23267
23268 if (!eap->skip)
23269 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023270 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023271 len = (int)STRLEN(p);
23272 if (ga_grow(&ga, len + 2) == FAIL)
23273 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023274 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023275 ret = FAIL;
23276 break;
23277 }
23278 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023279 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023280 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023281 ga.ga_len += len;
23282 }
23283
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023284 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023285 arg = skipwhite(arg);
23286 }
23287
23288 if (ret != FAIL && ga.ga_data != NULL)
23289 {
23290 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023291 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023292 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023293 out_flush();
23294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023295 else if (eap->cmdidx == CMD_echoerr)
23296 {
23297 /* We don't want to abort following commands, restore did_emsg. */
23298 save_did_emsg = did_emsg;
23299 EMSG((char_u *)ga.ga_data);
23300 if (!force_abort)
23301 did_emsg = save_did_emsg;
23302 }
23303 else if (eap->cmdidx == CMD_execute)
23304 do_cmdline((char_u *)ga.ga_data,
23305 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23306 }
23307
23308 ga_clear(&ga);
23309
23310 if (eap->skip)
23311 --emsg_skip;
23312
23313 eap->nextcmd = check_nextcmd(arg);
23314}
23315
23316/*
23317 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23318 * "arg" points to the "&" or '+' when called, to "option" when returning.
23319 * Returns NULL when no option name found. Otherwise pointer to the char
23320 * after the option name.
23321 */
23322 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023323find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023324{
23325 char_u *p = *arg;
23326
23327 ++p;
23328 if (*p == 'g' && p[1] == ':')
23329 {
23330 *opt_flags = OPT_GLOBAL;
23331 p += 2;
23332 }
23333 else if (*p == 'l' && p[1] == ':')
23334 {
23335 *opt_flags = OPT_LOCAL;
23336 p += 2;
23337 }
23338 else
23339 *opt_flags = 0;
23340
23341 if (!ASCII_ISALPHA(*p))
23342 return NULL;
23343 *arg = p;
23344
23345 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23346 p += 4; /* termcap option */
23347 else
23348 while (ASCII_ISALPHA(*p))
23349 ++p;
23350 return p;
23351}
23352
23353/*
23354 * ":function"
23355 */
23356 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023357ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023358{
23359 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023360 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023361 int j;
23362 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023363 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023364 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023365 char_u *name = NULL;
23366 char_u *p;
23367 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023368 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023369 garray_T newargs;
23370 garray_T newlines;
23371 int varargs = FALSE;
23372 int mustend = FALSE;
23373 int flags = 0;
23374 ufunc_T *fp;
23375 int indent;
23376 int nesting;
23377 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023378 dictitem_T *v;
23379 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023380 static int func_nr = 0; /* number for nameless function */
23381 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023382 hashtab_T *ht;
23383 int todo;
23384 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023385 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023386
23387 /*
23388 * ":function" without argument: list functions.
23389 */
23390 if (ends_excmd(*eap->arg))
23391 {
23392 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023393 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023394 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023395 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023396 {
23397 if (!HASHITEM_EMPTY(hi))
23398 {
23399 --todo;
23400 fp = HI2UF(hi);
23401 if (!isdigit(*fp->uf_name))
23402 list_func_head(fp, FALSE);
23403 }
23404 }
23405 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023406 eap->nextcmd = check_nextcmd(eap->arg);
23407 return;
23408 }
23409
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023410 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023411 * ":function /pat": list functions matching pattern.
23412 */
23413 if (*eap->arg == '/')
23414 {
23415 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23416 if (!eap->skip)
23417 {
23418 regmatch_T regmatch;
23419
23420 c = *p;
23421 *p = NUL;
23422 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23423 *p = c;
23424 if (regmatch.regprog != NULL)
23425 {
23426 regmatch.rm_ic = p_ic;
23427
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023428 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023429 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23430 {
23431 if (!HASHITEM_EMPTY(hi))
23432 {
23433 --todo;
23434 fp = HI2UF(hi);
23435 if (!isdigit(*fp->uf_name)
23436 && vim_regexec(&regmatch, fp->uf_name, 0))
23437 list_func_head(fp, FALSE);
23438 }
23439 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023440 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023441 }
23442 }
23443 if (*p == '/')
23444 ++p;
23445 eap->nextcmd = check_nextcmd(p);
23446 return;
23447 }
23448
23449 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023450 * Get the function name. There are these situations:
23451 * func normal function name
23452 * "name" == func, "fudi.fd_dict" == NULL
23453 * dict.func new dictionary entry
23454 * "name" == NULL, "fudi.fd_dict" set,
23455 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23456 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023457 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023458 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23459 * dict.func existing dict entry that's not a Funcref
23460 * "name" == NULL, "fudi.fd_dict" set,
23461 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023462 * s:func script-local function name
23463 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023464 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023466 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023467 paren = (vim_strchr(p, '(') != NULL);
23468 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023469 {
23470 /*
23471 * Return on an invalid expression in braces, unless the expression
23472 * evaluation has been cancelled due to an aborting error, an
23473 * interrupt, or an exception.
23474 */
23475 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023476 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023477 if (!eap->skip && fudi.fd_newkey != NULL)
23478 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023481 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023482 else
23483 eap->skip = TRUE;
23484 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023485
Bram Moolenaar071d4272004-06-13 20:20:40 +000023486 /* An error in a function call during evaluation of an expression in magic
23487 * braces should not cause the function not to be defined. */
23488 saved_did_emsg = did_emsg;
23489 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490
23491 /*
23492 * ":function func" with only function name: list function.
23493 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023494 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023495 {
23496 if (!ends_excmd(*skipwhite(p)))
23497 {
23498 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023499 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 }
23501 eap->nextcmd = check_nextcmd(p);
23502 if (eap->nextcmd != NULL)
23503 *p = NUL;
23504 if (!eap->skip && !got_int)
23505 {
23506 fp = find_func(name);
23507 if (fp != NULL)
23508 {
23509 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023510 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023511 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023512 if (FUNCLINE(fp, j) == NULL)
23513 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023514 msg_putchar('\n');
23515 msg_outnum((long)(j + 1));
23516 if (j < 9)
23517 msg_putchar(' ');
23518 if (j < 99)
23519 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023520 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023521 out_flush(); /* show a line at a time */
23522 ui_breakcheck();
23523 }
23524 if (!got_int)
23525 {
23526 msg_putchar('\n');
23527 msg_puts((char_u *)" endfunction");
23528 }
23529 }
23530 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023531 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023532 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023533 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023534 }
23535
23536 /*
23537 * ":function name(arg1, arg2)" Define function.
23538 */
23539 p = skipwhite(p);
23540 if (*p != '(')
23541 {
23542 if (!eap->skip)
23543 {
23544 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023545 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023546 }
23547 /* attempt to continue by skipping some text */
23548 if (vim_strchr(p, '(') != NULL)
23549 p = vim_strchr(p, '(');
23550 }
23551 p = skipwhite(p + 1);
23552
23553 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23554 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23555
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023556 if (!eap->skip)
23557 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023558 /* Check the name of the function. Unless it's a dictionary function
23559 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023560 if (name != NULL)
23561 arg = name;
23562 else
23563 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023564 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023565 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23566 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023567 {
23568 if (*arg == K_SPECIAL)
23569 j = 3;
23570 else
23571 j = 0;
23572 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23573 : eval_isnamec(arg[j])))
23574 ++j;
23575 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023576 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023577 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023578 /* Disallow using the g: dict. */
23579 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23580 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023581 }
23582
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 /*
23584 * Isolate the arguments: "arg1, arg2, ...)"
23585 */
23586 while (*p != ')')
23587 {
23588 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23589 {
23590 varargs = TRUE;
23591 p += 3;
23592 mustend = TRUE;
23593 }
23594 else
23595 {
23596 arg = p;
23597 while (ASCII_ISALNUM(*p) || *p == '_')
23598 ++p;
23599 if (arg == p || isdigit(*arg)
23600 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23601 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23602 {
23603 if (!eap->skip)
23604 EMSG2(_("E125: Illegal argument: %s"), arg);
23605 break;
23606 }
23607 if (ga_grow(&newargs, 1) == FAIL)
23608 goto erret;
23609 c = *p;
23610 *p = NUL;
23611 arg = vim_strsave(arg);
23612 if (arg == NULL)
23613 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023614
23615 /* Check for duplicate argument name. */
23616 for (i = 0; i < newargs.ga_len; ++i)
23617 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23618 {
23619 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023620 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023621 goto erret;
23622 }
23623
Bram Moolenaar071d4272004-06-13 20:20:40 +000023624 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23625 *p = c;
23626 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023627 if (*p == ',')
23628 ++p;
23629 else
23630 mustend = TRUE;
23631 }
23632 p = skipwhite(p);
23633 if (mustend && *p != ')')
23634 {
23635 if (!eap->skip)
23636 EMSG2(_(e_invarg2), eap->arg);
23637 break;
23638 }
23639 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023640 if (*p != ')')
23641 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023642 ++p; /* skip the ')' */
23643
Bram Moolenaare9a41262005-01-15 22:18:47 +000023644 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023645 for (;;)
23646 {
23647 p = skipwhite(p);
23648 if (STRNCMP(p, "range", 5) == 0)
23649 {
23650 flags |= FC_RANGE;
23651 p += 5;
23652 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023653 else if (STRNCMP(p, "dict", 4) == 0)
23654 {
23655 flags |= FC_DICT;
23656 p += 4;
23657 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 else if (STRNCMP(p, "abort", 5) == 0)
23659 {
23660 flags |= FC_ABORT;
23661 p += 5;
23662 }
23663 else
23664 break;
23665 }
23666
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023667 /* When there is a line break use what follows for the function body.
23668 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23669 if (*p == '\n')
23670 line_arg = p + 1;
23671 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023672 EMSG(_(e_trailing));
23673
23674 /*
23675 * Read the body of the function, until ":endfunction" is found.
23676 */
23677 if (KeyTyped)
23678 {
23679 /* Check if the function already exists, don't let the user type the
23680 * whole function before telling him it doesn't work! For a script we
23681 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023682 if (!eap->skip && !eap->forceit)
23683 {
23684 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23685 EMSG(_(e_funcdict));
23686 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023687 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023688 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023689
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023690 if (!eap->skip && did_emsg)
23691 goto erret;
23692
Bram Moolenaar071d4272004-06-13 20:20:40 +000023693 msg_putchar('\n'); /* don't overwrite the function name */
23694 cmdline_row = msg_row;
23695 }
23696
23697 indent = 2;
23698 nesting = 0;
23699 for (;;)
23700 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023701 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023702 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023703 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023704 saved_wait_return = FALSE;
23705 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023706 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023707 sourcing_lnum_off = sourcing_lnum;
23708
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023709 if (line_arg != NULL)
23710 {
23711 /* Use eap->arg, split up in parts by line breaks. */
23712 theline = line_arg;
23713 p = vim_strchr(theline, '\n');
23714 if (p == NULL)
23715 line_arg += STRLEN(line_arg);
23716 else
23717 {
23718 *p = NUL;
23719 line_arg = p + 1;
23720 }
23721 }
23722 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 theline = getcmdline(':', 0L, indent);
23724 else
23725 theline = eap->getline(':', eap->cookie, indent);
23726 if (KeyTyped)
23727 lines_left = Rows - 1;
23728 if (theline == NULL)
23729 {
23730 EMSG(_("E126: Missing :endfunction"));
23731 goto erret;
23732 }
23733
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023734 /* Detect line continuation: sourcing_lnum increased more than one. */
23735 if (sourcing_lnum > sourcing_lnum_off + 1)
23736 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23737 else
23738 sourcing_lnum_off = 0;
23739
Bram Moolenaar071d4272004-06-13 20:20:40 +000023740 if (skip_until != NULL)
23741 {
23742 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23743 * don't check for ":endfunc". */
23744 if (STRCMP(theline, skip_until) == 0)
23745 {
23746 vim_free(skip_until);
23747 skip_until = NULL;
23748 }
23749 }
23750 else
23751 {
23752 /* skip ':' and blanks*/
23753 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23754 ;
23755
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023756 /* Check for "endfunction". */
23757 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023758 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023759 if (line_arg == NULL)
23760 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023761 break;
23762 }
23763
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023764 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023765 * at "end". */
23766 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23767 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023768 else if (STRNCMP(p, "if", 2) == 0
23769 || STRNCMP(p, "wh", 2) == 0
23770 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 || STRNCMP(p, "try", 3) == 0)
23772 indent += 2;
23773
23774 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023775 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023776 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023777 if (*p == '!')
23778 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023779 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023780 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023781 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023782 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023783 ++nesting;
23784 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023785 }
23786 }
23787
23788 /* Check for ":append" or ":insert". */
23789 p = skip_range(p, NULL);
23790 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23791 || (p[0] == 'i'
23792 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23793 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23794 skip_until = vim_strsave((char_u *)".");
23795
23796 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23797 arg = skipwhite(skiptowhite(p));
23798 if (arg[0] == '<' && arg[1] =='<'
23799 && ((p[0] == 'p' && p[1] == 'y'
23800 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23801 || (p[0] == 'p' && p[1] == 'e'
23802 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23803 || (p[0] == 't' && p[1] == 'c'
23804 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023805 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23806 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023807 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23808 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023809 || (p[0] == 'm' && p[1] == 'z'
23810 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023811 ))
23812 {
23813 /* ":python <<" continues until a dot, like ":append" */
23814 p = skipwhite(arg + 2);
23815 if (*p == NUL)
23816 skip_until = vim_strsave((char_u *)".");
23817 else
23818 skip_until = vim_strsave(p);
23819 }
23820 }
23821
23822 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023823 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023824 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023825 if (line_arg == NULL)
23826 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023827 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023828 }
23829
23830 /* Copy the line to newly allocated memory. get_one_sourceline()
23831 * allocates 250 bytes per line, this saves 80% on average. The cost
23832 * is an extra alloc/free. */
23833 p = vim_strsave(theline);
23834 if (p != NULL)
23835 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023836 if (line_arg == NULL)
23837 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023838 theline = p;
23839 }
23840
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023841 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23842
23843 /* Add NULL lines for continuation lines, so that the line count is
23844 * equal to the index in the growarray. */
23845 while (sourcing_lnum_off-- > 0)
23846 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023847
23848 /* Check for end of eap->arg. */
23849 if (line_arg != NULL && *line_arg == NUL)
23850 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023851 }
23852
23853 /* Don't define the function when skipping commands or when an error was
23854 * detected. */
23855 if (eap->skip || did_emsg)
23856 goto erret;
23857
23858 /*
23859 * If there are no errors, add the function
23860 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023861 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023862 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023863 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023864 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023865 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023866 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023867 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023868 goto erret;
23869 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023870
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023871 fp = find_func(name);
23872 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023873 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023874 if (!eap->forceit)
23875 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023876 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023877 goto erret;
23878 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023879 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023880 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023881 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023882 name);
23883 goto erret;
23884 }
23885 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023886 ga_clear_strings(&(fp->uf_args));
23887 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023888 vim_free(name);
23889 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023890 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023891 }
23892 else
23893 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023894 char numbuf[20];
23895
23896 fp = NULL;
23897 if (fudi.fd_newkey == NULL && !eap->forceit)
23898 {
23899 EMSG(_(e_funcdict));
23900 goto erret;
23901 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023902 if (fudi.fd_di == NULL)
23903 {
23904 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023905 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023906 goto erret;
23907 }
23908 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023909 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023910 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023911
23912 /* Give the function a sequential number. Can only be used with a
23913 * Funcref! */
23914 vim_free(name);
23915 sprintf(numbuf, "%d", ++func_nr);
23916 name = vim_strsave((char_u *)numbuf);
23917 if (name == NULL)
23918 goto erret;
23919 }
23920
23921 if (fp == NULL)
23922 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023923 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023924 {
23925 int slen, plen;
23926 char_u *scriptname;
23927
23928 /* Check that the autoload name matches the script name. */
23929 j = FAIL;
23930 if (sourcing_name != NULL)
23931 {
23932 scriptname = autoload_name(name);
23933 if (scriptname != NULL)
23934 {
23935 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023936 plen = (int)STRLEN(p);
23937 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023938 if (slen > plen && fnamecmp(p,
23939 sourcing_name + slen - plen) == 0)
23940 j = OK;
23941 vim_free(scriptname);
23942 }
23943 }
23944 if (j == FAIL)
23945 {
23946 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23947 goto erret;
23948 }
23949 }
23950
23951 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952 if (fp == NULL)
23953 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023954
23955 if (fudi.fd_dict != NULL)
23956 {
23957 if (fudi.fd_di == NULL)
23958 {
23959 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023960 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023961 if (fudi.fd_di == NULL)
23962 {
23963 vim_free(fp);
23964 goto erret;
23965 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023966 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23967 {
23968 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023969 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023970 goto erret;
23971 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023972 }
23973 else
23974 /* overwrite existing dict entry */
23975 clear_tv(&fudi.fd_di->di_tv);
23976 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023977 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023978 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023979 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023980
23981 /* behave like "dict" was used */
23982 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023983 }
23984
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023986 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023987 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23988 {
23989 vim_free(fp);
23990 goto erret;
23991 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023993 fp->uf_args = newargs;
23994 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023995#ifdef FEAT_PROFILE
23996 fp->uf_tml_count = NULL;
23997 fp->uf_tml_total = NULL;
23998 fp->uf_tml_self = NULL;
23999 fp->uf_profiling = FALSE;
24000 if (prof_def_func())
24001 func_do_profile(fp);
24002#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024003 fp->uf_varargs = varargs;
24004 fp->uf_flags = flags;
24005 fp->uf_calls = 0;
24006 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024007 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008
24009erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 ga_clear_strings(&newargs);
24011 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024012ret_free:
24013 vim_free(skip_until);
24014 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024015 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024016 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020024017 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024018}
24019
24020/*
24021 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000024022 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000024023 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024024 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010024025 * TFN_INT: internal function name OK
24026 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024027 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000024028 * Advances "pp" to just after the function name (if no error).
24029 */
24030 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024031trans_function_name(
24032 char_u **pp,
24033 int skip, /* only find the end, don't evaluate */
24034 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010024035 funcdict_T *fdp, /* return: info about dictionary used */
24036 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024037{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024038 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024039 char_u *start;
24040 char_u *end;
24041 int lead;
24042 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000024043 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024044 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024045
24046 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000024047 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024048 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000024049
24050 /* Check for hard coded <SNR>: already translated function ID (from a user
24051 * command). */
24052 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
24053 && (*pp)[2] == (int)KE_SNR)
24054 {
24055 *pp += 3;
24056 len = get_id_len(pp) + 3;
24057 return vim_strnsave(start, len);
24058 }
24059
24060 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24061 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024062 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024063 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024064 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024065
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024066 /* Note that TFN_ flags use the same values as GLV_ flags. */
24067 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024068 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024069 if (end == start)
24070 {
24071 if (!skip)
24072 EMSG(_("E129: Function name required"));
24073 goto theend;
24074 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024075 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024076 {
24077 /*
24078 * Report an invalid expression in braces, unless the expression
24079 * evaluation has been cancelled due to an aborting error, an
24080 * interrupt, or an exception.
24081 */
24082 if (!aborting())
24083 {
24084 if (end != NULL)
24085 EMSG2(_(e_invarg2), start);
24086 }
24087 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024088 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024089 goto theend;
24090 }
24091
24092 if (lv.ll_tv != NULL)
24093 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024094 if (fdp != NULL)
24095 {
24096 fdp->fd_dict = lv.ll_dict;
24097 fdp->fd_newkey = lv.ll_newkey;
24098 lv.ll_newkey = NULL;
24099 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024100 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024101 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24102 {
24103 name = vim_strsave(lv.ll_tv->vval.v_string);
24104 *pp = end;
24105 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024106 else if (lv.ll_tv->v_type == VAR_PARTIAL
24107 && lv.ll_tv->vval.v_partial != NULL)
24108 {
24109 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24110 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024111 if (partial != NULL)
24112 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024113 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024114 else
24115 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024116 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24117 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024118 EMSG(_(e_funcref));
24119 else
24120 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024121 name = NULL;
24122 }
24123 goto theend;
24124 }
24125
24126 if (lv.ll_name == NULL)
24127 {
24128 /* Error found, but continue after the function name. */
24129 *pp = end;
24130 goto theend;
24131 }
24132
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024133 /* Check if the name is a Funcref. If so, use the value. */
24134 if (lv.ll_exp_name != NULL)
24135 {
24136 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024137 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024138 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024139 if (name == lv.ll_exp_name)
24140 name = NULL;
24141 }
24142 else
24143 {
24144 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024145 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024146 if (name == *pp)
24147 name = NULL;
24148 }
24149 if (name != NULL)
24150 {
24151 name = vim_strsave(name);
24152 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024153 if (STRNCMP(name, "<SNR>", 5) == 0)
24154 {
24155 /* Change "<SNR>" to the byte sequence. */
24156 name[0] = K_SPECIAL;
24157 name[1] = KS_EXTRA;
24158 name[2] = (int)KE_SNR;
24159 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24160 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024161 goto theend;
24162 }
24163
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024164 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024165 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024166 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024167 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24168 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24169 {
24170 /* When there was "s:" already or the name expanded to get a
24171 * leading "s:" then remove it. */
24172 lv.ll_name += 2;
24173 len -= 2;
24174 lead = 2;
24175 }
24176 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024177 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024178 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024179 /* skip over "s:" and "g:" */
24180 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024181 lv.ll_name += 2;
24182 len = (int)(end - lv.ll_name);
24183 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024184
24185 /*
24186 * Copy the function name to allocated memory.
24187 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24188 * Accept <SNR>123_name() outside a script.
24189 */
24190 if (skip)
24191 lead = 0; /* do nothing */
24192 else if (lead > 0)
24193 {
24194 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024195 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24196 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024197 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024198 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024199 if (current_SID <= 0)
24200 {
24201 EMSG(_(e_usingsid));
24202 goto theend;
24203 }
24204 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24205 lead += (int)STRLEN(sid_buf);
24206 }
24207 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024208 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024209 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024210 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024211 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024212 goto theend;
24213 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024214 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024215 {
24216 char_u *cp = vim_strchr(lv.ll_name, ':');
24217
24218 if (cp != NULL && cp < end)
24219 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024220 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024221 goto theend;
24222 }
24223 }
24224
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024225 name = alloc((unsigned)(len + lead + 1));
24226 if (name != NULL)
24227 {
24228 if (lead > 0)
24229 {
24230 name[0] = K_SPECIAL;
24231 name[1] = KS_EXTRA;
24232 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024233 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024234 STRCPY(name + 3, sid_buf);
24235 }
24236 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024237 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024238 }
24239 *pp = end;
24240
24241theend:
24242 clear_lval(&lv);
24243 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024244}
24245
24246/*
24247 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24248 * Return 2 if "p" starts with "s:".
24249 * Return 0 otherwise.
24250 */
24251 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024252eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024253{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024254 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24255 * the standard library function. */
24256 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24257 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024258 return 5;
24259 if (p[0] == 's' && p[1] == ':')
24260 return 2;
24261 return 0;
24262}
24263
24264/*
24265 * Return TRUE if "p" starts with "<SID>" or "s:".
24266 * Only works if eval_fname_script() returned non-zero for "p"!
24267 */
24268 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024269eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270{
24271 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24272}
24273
24274/*
24275 * List the head of the function: "name(arg1, arg2)".
24276 */
24277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024278list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024279{
24280 int j;
24281
24282 msg_start();
24283 if (indent)
24284 MSG_PUTS(" ");
24285 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024286 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024287 {
24288 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024289 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024290 }
24291 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024292 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024293 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024294 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024295 {
24296 if (j)
24297 MSG_PUTS(", ");
24298 msg_puts(FUNCARG(fp, j));
24299 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024300 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024301 {
24302 if (j)
24303 MSG_PUTS(", ");
24304 MSG_PUTS("...");
24305 }
24306 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024307 if (fp->uf_flags & FC_ABORT)
24308 MSG_PUTS(" abort");
24309 if (fp->uf_flags & FC_RANGE)
24310 MSG_PUTS(" range");
24311 if (fp->uf_flags & FC_DICT)
24312 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024313 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024314 if (p_verbose > 0)
24315 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024316}
24317
24318/*
24319 * Find a function by name, return pointer to it in ufuncs.
24320 * Return NULL for unknown function.
24321 */
24322 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024323find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024324{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024325 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024326
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024327 hi = hash_find(&func_hashtab, name);
24328 if (!HASHITEM_EMPTY(hi))
24329 return HI2UF(hi);
24330 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024331}
24332
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024333#if defined(EXITFREE) || defined(PROTO)
24334 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024335free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024336{
24337 hashitem_T *hi;
24338
24339 /* Need to start all over every time, because func_free() may change the
24340 * hash table. */
24341 while (func_hashtab.ht_used > 0)
24342 for (hi = func_hashtab.ht_array; ; ++hi)
24343 if (!HASHITEM_EMPTY(hi))
24344 {
24345 func_free(HI2UF(hi));
24346 break;
24347 }
24348}
24349#endif
24350
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024351 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024352translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024353{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024354 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024355 return find_internal_func(name) >= 0;
24356 return find_func(name) != NULL;
24357}
24358
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024359/*
24360 * Return TRUE if a function "name" exists.
24361 */
24362 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024363function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024364{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024365 char_u *nm = name;
24366 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024367 int n = FALSE;
24368
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024369 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024370 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024371 nm = skipwhite(nm);
24372
24373 /* Only accept "funcname", "funcname ", "funcname (..." and
24374 * "funcname(...", not "funcname!...". */
24375 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024376 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024377 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024378 return n;
24379}
24380
Bram Moolenaara1544c02013-05-30 12:35:52 +020024381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024382get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024383{
24384 char_u *nm = name;
24385 char_u *p;
24386
Bram Moolenaar65639032016-03-16 21:40:30 +010024387 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024388
24389 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024390 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024391 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024392
Bram Moolenaara1544c02013-05-30 12:35:52 +020024393 vim_free(p);
24394 return NULL;
24395}
24396
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024397/*
24398 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024399 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24400 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024401 */
24402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024403builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024404{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024405 char_u *p;
24406
24407 if (!ASCII_ISLOWER(name[0]))
24408 return FALSE;
24409 p = vim_strchr(name, AUTOLOAD_CHAR);
24410 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024411}
24412
Bram Moolenaar05159a02005-02-26 23:04:13 +000024413#if defined(FEAT_PROFILE) || defined(PROTO)
24414/*
24415 * Start profiling function "fp".
24416 */
24417 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024418func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024419{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024420 int len = fp->uf_lines.ga_len;
24421
24422 if (len == 0)
24423 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024424 fp->uf_tm_count = 0;
24425 profile_zero(&fp->uf_tm_self);
24426 profile_zero(&fp->uf_tm_total);
24427 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024428 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024429 if (fp->uf_tml_total == NULL)
24430 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024431 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024432 if (fp->uf_tml_self == NULL)
24433 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024434 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024435 fp->uf_tml_idx = -1;
24436 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24437 || fp->uf_tml_self == NULL)
24438 return; /* out of memory */
24439
24440 fp->uf_profiling = TRUE;
24441}
24442
24443/*
24444 * Dump the profiling results for all functions in file "fd".
24445 */
24446 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024447func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024448{
24449 hashitem_T *hi;
24450 int todo;
24451 ufunc_T *fp;
24452 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024453 ufunc_T **sorttab;
24454 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024455
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024456 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024457 if (todo == 0)
24458 return; /* nothing to dump */
24459
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024460 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024461
Bram Moolenaar05159a02005-02-26 23:04:13 +000024462 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24463 {
24464 if (!HASHITEM_EMPTY(hi))
24465 {
24466 --todo;
24467 fp = HI2UF(hi);
24468 if (fp->uf_profiling)
24469 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024470 if (sorttab != NULL)
24471 sorttab[st_len++] = fp;
24472
Bram Moolenaar05159a02005-02-26 23:04:13 +000024473 if (fp->uf_name[0] == K_SPECIAL)
24474 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24475 else
24476 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24477 if (fp->uf_tm_count == 1)
24478 fprintf(fd, "Called 1 time\n");
24479 else
24480 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24481 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24482 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24483 fprintf(fd, "\n");
24484 fprintf(fd, "count total (s) self (s)\n");
24485
24486 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24487 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024488 if (FUNCLINE(fp, i) == NULL)
24489 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024490 prof_func_line(fd, fp->uf_tml_count[i],
24491 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024492 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24493 }
24494 fprintf(fd, "\n");
24495 }
24496 }
24497 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024498
24499 if (sorttab != NULL && st_len > 0)
24500 {
24501 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24502 prof_total_cmp);
24503 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24504 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24505 prof_self_cmp);
24506 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24507 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024508
24509 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024510}
Bram Moolenaar73830342005-02-28 22:48:19 +000024511
24512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024513prof_sort_list(
24514 FILE *fd,
24515 ufunc_T **sorttab,
24516 int st_len,
24517 char *title,
24518 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024519{
24520 int i;
24521 ufunc_T *fp;
24522
24523 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24524 fprintf(fd, "count total (s) self (s) function\n");
24525 for (i = 0; i < 20 && i < st_len; ++i)
24526 {
24527 fp = sorttab[i];
24528 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24529 prefer_self);
24530 if (fp->uf_name[0] == K_SPECIAL)
24531 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24532 else
24533 fprintf(fd, " %s()\n", fp->uf_name);
24534 }
24535 fprintf(fd, "\n");
24536}
24537
24538/*
24539 * Print the count and times for one function or function line.
24540 */
24541 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024542prof_func_line(
24543 FILE *fd,
24544 int count,
24545 proftime_T *total,
24546 proftime_T *self,
24547 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024548{
24549 if (count > 0)
24550 {
24551 fprintf(fd, "%5d ", count);
24552 if (prefer_self && profile_equal(total, self))
24553 fprintf(fd, " ");
24554 else
24555 fprintf(fd, "%s ", profile_msg(total));
24556 if (!prefer_self && profile_equal(total, self))
24557 fprintf(fd, " ");
24558 else
24559 fprintf(fd, "%s ", profile_msg(self));
24560 }
24561 else
24562 fprintf(fd, " ");
24563}
24564
24565/*
24566 * Compare function for total time sorting.
24567 */
24568 static int
24569#ifdef __BORLANDC__
24570_RTLENTRYF
24571#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024572prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024573{
24574 ufunc_T *p1, *p2;
24575
24576 p1 = *(ufunc_T **)s1;
24577 p2 = *(ufunc_T **)s2;
24578 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24579}
24580
24581/*
24582 * Compare function for self time sorting.
24583 */
24584 static int
24585#ifdef __BORLANDC__
24586_RTLENTRYF
24587#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024588prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024589{
24590 ufunc_T *p1, *p2;
24591
24592 p1 = *(ufunc_T **)s1;
24593 p2 = *(ufunc_T **)s2;
24594 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24595}
24596
Bram Moolenaar05159a02005-02-26 23:04:13 +000024597#endif
24598
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024599/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024600 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024601 * Return TRUE if a package was loaded.
24602 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024603 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024604script_autoload(
24605 char_u *name,
24606 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024607{
24608 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024609 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024610 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024611 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024612
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024613 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024614 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024615 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024616 return FALSE;
24617
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024618 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024619
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024620 /* Find the name in the list of previously loaded package names. Skip
24621 * "autoload/", it's always the same. */
24622 for (i = 0; i < ga_loaded.ga_len; ++i)
24623 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24624 break;
24625 if (!reload && i < ga_loaded.ga_len)
24626 ret = FALSE; /* was loaded already */
24627 else
24628 {
24629 /* Remember the name if it wasn't loaded already. */
24630 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24631 {
24632 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24633 tofree = NULL;
24634 }
24635
24636 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024637 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024638 ret = TRUE;
24639 }
24640
24641 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024642 return ret;
24643}
24644
24645/*
24646 * Return the autoload script name for a function or variable name.
24647 * Returns NULL when out of memory.
24648 */
24649 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024650autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024651{
24652 char_u *p;
24653 char_u *scriptname;
24654
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024655 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024656 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24657 if (scriptname == NULL)
24658 return FALSE;
24659 STRCPY(scriptname, "autoload/");
24660 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024661 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024662 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024663 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024664 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024665 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024666}
24667
Bram Moolenaar071d4272004-06-13 20:20:40 +000024668#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24669
24670/*
24671 * Function given to ExpandGeneric() to obtain the list of user defined
24672 * function names.
24673 */
24674 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024675get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024676{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024677 static long_u done;
24678 static hashitem_T *hi;
24679 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680
24681 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024683 done = 0;
24684 hi = func_hashtab.ht_array;
24685 }
24686 if (done < func_hashtab.ht_used)
24687 {
24688 if (done++ > 0)
24689 ++hi;
24690 while (HASHITEM_EMPTY(hi))
24691 ++hi;
24692 fp = HI2UF(hi);
24693
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024694 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024695 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024696
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024697 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24698 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024699
24700 cat_func_name(IObuff, fp);
24701 if (xp->xp_context != EXPAND_USER_FUNC)
24702 {
24703 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024704 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024705 STRCAT(IObuff, ")");
24706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024707 return IObuff;
24708 }
24709 return NULL;
24710}
24711
24712#endif /* FEAT_CMDL_COMPL */
24713
24714/*
24715 * Copy the function name of "fp" to buffer "buf".
24716 * "buf" must be able to hold the function name plus three bytes.
24717 * Takes care of script-local function names.
24718 */
24719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024720cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024721{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024722 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 {
24724 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024725 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024726 }
24727 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024728 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729}
24730
24731/*
24732 * ":delfunction {name}"
24733 */
24734 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024735ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024736{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024737 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024738 char_u *p;
24739 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024740 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024741
24742 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024743 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024744 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024745 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024746 {
24747 if (fudi.fd_dict != NULL && !eap->skip)
24748 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024749 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024750 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024751 if (!ends_excmd(*skipwhite(p)))
24752 {
24753 vim_free(name);
24754 EMSG(_(e_trailing));
24755 return;
24756 }
24757 eap->nextcmd = check_nextcmd(p);
24758 if (eap->nextcmd != NULL)
24759 *p = NUL;
24760
24761 if (!eap->skip)
24762 fp = find_func(name);
24763 vim_free(name);
24764
24765 if (!eap->skip)
24766 {
24767 if (fp == NULL)
24768 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024769 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024770 return;
24771 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024772 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024773 {
24774 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24775 return;
24776 }
24777
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024778 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024779 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024780 /* Delete the dict item that refers to the function, it will
24781 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024782 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024783 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024784 else
24785 func_free(fp);
24786 }
24787}
24788
24789/*
24790 * Free a function and remove it from the list of functions.
24791 */
24792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024793func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024794{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024795 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024796
24797 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024798 ga_clear_strings(&(fp->uf_args));
24799 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024800#ifdef FEAT_PROFILE
24801 vim_free(fp->uf_tml_count);
24802 vim_free(fp->uf_tml_total);
24803 vim_free(fp->uf_tml_self);
24804#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024805
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024806 /* remove the function from the function hashtable */
24807 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24808 if (HASHITEM_EMPTY(hi))
24809 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024810 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024811 hash_remove(&func_hashtab, hi);
24812
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024813 vim_free(fp);
24814}
24815
24816/*
24817 * Unreference a Function: decrement the reference count and free it when it
24818 * becomes zero. Only for numbered functions.
24819 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024820 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024821func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024822{
24823 ufunc_T *fp;
24824
24825 if (name != NULL && isdigit(*name))
24826 {
24827 fp = find_func(name);
24828 if (fp == NULL)
24829 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024830 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024831 {
24832 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024833 * when "uf_calls" becomes zero. */
24834 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024835 func_free(fp);
24836 }
24837 }
24838}
24839
24840/*
24841 * Count a reference to a Function.
24842 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024843 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024844func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024845{
24846 ufunc_T *fp;
24847
24848 if (name != NULL && isdigit(*name))
24849 {
24850 fp = find_func(name);
24851 if (fp == NULL)
24852 EMSG2(_(e_intern2), "func_ref()");
24853 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024854 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024855 }
24856}
24857
24858/*
24859 * Call a user function.
24860 */
24861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024862call_user_func(
24863 ufunc_T *fp, /* pointer to function */
24864 int argcount, /* nr of args */
24865 typval_T *argvars, /* arguments */
24866 typval_T *rettv, /* return value */
24867 linenr_T firstline, /* first line of range */
24868 linenr_T lastline, /* last line of range */
24869 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870{
Bram Moolenaar33570922005-01-25 22:26:29 +000024871 char_u *save_sourcing_name;
24872 linenr_T save_sourcing_lnum;
24873 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024874 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024875 int save_did_emsg;
24876 static int depth = 0;
24877 dictitem_T *v;
24878 int fixvar_idx = 0; /* index in fixvar[] */
24879 int i;
24880 int ai;
24881 char_u numbuf[NUMBUFLEN];
24882 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024883 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024884#ifdef FEAT_PROFILE
24885 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024886 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024887#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024888
24889 /* If depth of calling is getting too high, don't execute the function */
24890 if (depth >= p_mfd)
24891 {
24892 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024893 rettv->v_type = VAR_NUMBER;
24894 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024895 return;
24896 }
24897 ++depth;
24898
24899 line_breakcheck(); /* check for CTRL-C hit */
24900
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024901 fc = (funccall_T *)alloc(sizeof(funccall_T));
24902 fc->caller = current_funccal;
24903 current_funccal = fc;
24904 fc->func = fp;
24905 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024906 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024907 fc->linenr = 0;
24908 fc->returned = FALSE;
24909 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024910 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024911 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24912 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913
Bram Moolenaar33570922005-01-25 22:26:29 +000024914 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024915 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024916 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24917 * each argument variable and saves a lot of time.
24918 */
24919 /*
24920 * Init l: variables.
24921 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024922 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024923 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024924 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024925 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24926 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024927 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024928 name = v->di_key;
24929 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024930 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024931 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024932 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024933 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 v->di_tv.vval.v_dict = selfdict;
24935 ++selfdict->dv_refcount;
24936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024937
Bram Moolenaar33570922005-01-25 22:26:29 +000024938 /*
24939 * Init a: variables.
24940 * Set a:0 to "argcount".
24941 * Set a:000 to a list with room for the "..." arguments.
24942 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024943 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024944 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024945 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024946 /* Use "name" to avoid a warning from some compiler that checks the
24947 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024948 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024949 name = v->di_key;
24950 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024951 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024952 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024953 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024954 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024955 v->di_tv.vval.v_list = &fc->l_varlist;
24956 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24957 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24958 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024959
24960 /*
24961 * Set a:firstline to "firstline" and a:lastline to "lastline".
24962 * Set a:name to named arguments.
24963 * Set a:N to the "..." arguments.
24964 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024965 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024966 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024967 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024968 (varnumber_T)lastline);
24969 for (i = 0; i < argcount; ++i)
24970 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024971 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024972 if (ai < 0)
24973 /* named argument a:name */
24974 name = FUNCARG(fp, i);
24975 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024976 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024977 /* "..." argument a:1, a:2, etc. */
24978 sprintf((char *)numbuf, "%d", ai + 1);
24979 name = numbuf;
24980 }
24981 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24982 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024983 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024984 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24985 }
24986 else
24987 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024988 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24989 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024990 if (v == NULL)
24991 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024992 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024993 }
24994 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024995 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024996
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024997 /* Note: the values are copied directly to avoid alloc/free.
24998 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024999 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025000 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025001
25002 if (ai >= 0 && ai < MAX_FUNC_ARGS)
25003 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025004 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
25005 fc->l_listitems[ai].li_tv = argvars[i];
25006 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000025007 }
25008 }
25009
Bram Moolenaar071d4272004-06-13 20:20:40 +000025010 /* Don't redraw while executing the function. */
25011 ++RedrawingDisabled;
25012 save_sourcing_name = sourcing_name;
25013 save_sourcing_lnum = sourcing_lnum;
25014 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025015 /* need space for function name + ("function " + 3) or "[number]" */
25016 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
25017 + STRLEN(fp->uf_name) + 20;
25018 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025019 if (sourcing_name != NULL)
25020 {
25021 if (save_sourcing_name != NULL
25022 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020025023 sprintf((char *)sourcing_name, "%s[%d]..",
25024 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025025 else
25026 STRCPY(sourcing_name, "function ");
25027 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
25028
25029 if (p_verbose >= 12)
25030 {
25031 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025032 verbose_enter_scroll();
25033
Bram Moolenaar555b2802005-05-19 21:08:39 +000025034 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 if (p_verbose >= 14)
25036 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025038 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025039 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025040 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025041
25042 msg_puts((char_u *)"(");
25043 for (i = 0; i < argcount; ++i)
25044 {
25045 if (i > 0)
25046 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000025047 if (argvars[i].v_type == VAR_NUMBER)
25048 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025049 else
25050 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020025051 /* Do not want errors such as E724 here. */
25052 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025053 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025054 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025055 if (s != NULL)
25056 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025057 if (vim_strsize(s) > MSG_BUF_CLEN)
25058 {
25059 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25060 s = buf;
25061 }
25062 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025063 vim_free(tofree);
25064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025065 }
25066 }
25067 msg_puts((char_u *)")");
25068 }
25069 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025070
25071 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 --no_wait_return;
25073 }
25074 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025075#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025076 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025077 {
25078 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25079 func_do_profile(fp);
25080 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025081 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025082 {
25083 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025084 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025085 profile_zero(&fp->uf_tm_children);
25086 }
25087 script_prof_save(&wait_start);
25088 }
25089#endif
25090
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025092 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025093 save_did_emsg = did_emsg;
25094 did_emsg = FALSE;
25095
25096 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025097 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25099
25100 --RedrawingDisabled;
25101
25102 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025103 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025105 clear_tv(rettv);
25106 rettv->v_type = VAR_NUMBER;
25107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 }
25109
Bram Moolenaar05159a02005-02-26 23:04:13 +000025110#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025111 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025112 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025113 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025114 profile_end(&call_start);
25115 profile_sub_wait(&wait_start, &call_start);
25116 profile_add(&fp->uf_tm_total, &call_start);
25117 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025118 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025120 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25121 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025122 }
25123 }
25124#endif
25125
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 /* when being verbose, mention the return value */
25127 if (p_verbose >= 12)
25128 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025129 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025130 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025131
Bram Moolenaar071d4272004-06-13 20:20:40 +000025132 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025133 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025134 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025135 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025136 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025138 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025139 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025140 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025141 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025142 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025143
Bram Moolenaar555b2802005-05-19 21:08:39 +000025144 /* The value may be very long. Skip the middle part, so that we
25145 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025146 * truncate it at the end. Don't want errors such as E724 here. */
25147 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025148 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025149 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025150 if (s != NULL)
25151 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025152 if (vim_strsize(s) > MSG_BUF_CLEN)
25153 {
25154 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25155 s = buf;
25156 }
25157 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025158 vim_free(tofree);
25159 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025160 }
25161 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025162
25163 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025164 --no_wait_return;
25165 }
25166
25167 vim_free(sourcing_name);
25168 sourcing_name = save_sourcing_name;
25169 sourcing_lnum = save_sourcing_lnum;
25170 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025171#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025172 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025173 script_prof_restore(&wait_start);
25174#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025175
25176 if (p_verbose >= 12 && sourcing_name != NULL)
25177 {
25178 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025179 verbose_enter_scroll();
25180
Bram Moolenaar555b2802005-05-19 21:08:39 +000025181 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025182 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025183
25184 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185 --no_wait_return;
25186 }
25187
25188 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025189 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025190 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025191
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025192 /* If the a:000 list and the l: and a: dicts are not referenced we can
25193 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025194 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25195 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25196 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25197 {
25198 free_funccal(fc, FALSE);
25199 }
25200 else
25201 {
25202 hashitem_T *hi;
25203 listitem_T *li;
25204 int todo;
25205
25206 /* "fc" is still in use. This can happen when returning "a:000" or
25207 * assigning "l:" to a global variable.
25208 * Link "fc" in the list for garbage collection later. */
25209 fc->caller = previous_funccal;
25210 previous_funccal = fc;
25211
25212 /* Make a copy of the a: variables, since we didn't do that above. */
25213 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25214 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25215 {
25216 if (!HASHITEM_EMPTY(hi))
25217 {
25218 --todo;
25219 v = HI2DI(hi);
25220 copy_tv(&v->di_tv, &v->di_tv);
25221 }
25222 }
25223
25224 /* Make a copy of the a:000 items, since we didn't do that above. */
25225 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25226 copy_tv(&li->li_tv, &li->li_tv);
25227 }
25228}
25229
25230/*
25231 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025232 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025233 */
25234 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025235can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025236{
25237 return (fc->l_varlist.lv_copyID != copyID
25238 && fc->l_vars.dv_copyID != copyID
25239 && fc->l_avars.dv_copyID != copyID);
25240}
25241
25242/*
25243 * Free "fc" and what it contains.
25244 */
25245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025246free_funccal(
25247 funccall_T *fc,
25248 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025249{
25250 listitem_T *li;
25251
25252 /* The a: variables typevals may not have been allocated, only free the
25253 * allocated variables. */
25254 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25255
25256 /* free all l: variables */
25257 vars_clear(&fc->l_vars.dv_hashtab);
25258
25259 /* Free the a:000 variables if they were allocated. */
25260 if (free_val)
25261 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25262 clear_tv(&li->li_tv);
25263
25264 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265}
25266
25267/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025268 * Add a number variable "name" to dict "dp" with value "nr".
25269 */
25270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025271add_nr_var(
25272 dict_T *dp,
25273 dictitem_T *v,
25274 char *name,
25275 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025276{
25277 STRCPY(v->di_key, name);
25278 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25279 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25280 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025281 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025282 v->di_tv.vval.v_number = nr;
25283}
25284
25285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 * ":return [expr]"
25287 */
25288 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025289ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025290{
25291 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025292 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025293 int returning = FALSE;
25294
25295 if (current_funccal == NULL)
25296 {
25297 EMSG(_("E133: :return not inside a function"));
25298 return;
25299 }
25300
25301 if (eap->skip)
25302 ++emsg_skip;
25303
25304 eap->nextcmd = NULL;
25305 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025306 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025307 {
25308 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025309 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025310 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025311 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025312 }
25313 /* It's safer to return also on error. */
25314 else if (!eap->skip)
25315 {
25316 /*
25317 * Return unless the expression evaluation has been cancelled due to an
25318 * aborting error, an interrupt, or an exception.
25319 */
25320 if (!aborting())
25321 returning = do_return(eap, FALSE, TRUE, NULL);
25322 }
25323
25324 /* When skipping or the return gets pending, advance to the next command
25325 * in this line (!returning). Otherwise, ignore the rest of the line.
25326 * Following lines will be ignored by get_func_line(). */
25327 if (returning)
25328 eap->nextcmd = NULL;
25329 else if (eap->nextcmd == NULL) /* no argument */
25330 eap->nextcmd = check_nextcmd(arg);
25331
25332 if (eap->skip)
25333 --emsg_skip;
25334}
25335
25336/*
25337 * Return from a function. Possibly makes the return pending. Also called
25338 * for a pending return at the ":endtry" or after returning from an extra
25339 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025340 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025341 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025342 * FALSE when the return gets pending.
25343 */
25344 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025345do_return(
25346 exarg_T *eap,
25347 int reanimate,
25348 int is_cmd,
25349 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025350{
25351 int idx;
25352 struct condstack *cstack = eap->cstack;
25353
25354 if (reanimate)
25355 /* Undo the return. */
25356 current_funccal->returned = FALSE;
25357
25358 /*
25359 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25360 * not in its finally clause (which then is to be executed next) is found.
25361 * In this case, make the ":return" pending for execution at the ":endtry".
25362 * Otherwise, return normally.
25363 */
25364 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25365 if (idx >= 0)
25366 {
25367 cstack->cs_pending[idx] = CSTP_RETURN;
25368
25369 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025370 /* A pending return again gets pending. "rettv" points to an
25371 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025372 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025373 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025374 else
25375 {
25376 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025377 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025378 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025379 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025380
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025381 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025382 {
25383 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025384 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025385 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025386 else
25387 EMSG(_(e_outofmem));
25388 }
25389 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025390 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025391
25392 if (reanimate)
25393 {
25394 /* The pending return value could be overwritten by a ":return"
25395 * without argument in a finally clause; reset the default
25396 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025397 current_funccal->rettv->v_type = VAR_NUMBER;
25398 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399 }
25400 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025401 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025402 }
25403 else
25404 {
25405 current_funccal->returned = TRUE;
25406
25407 /* If the return is carried out now, store the return value. For
25408 * a return immediately after reanimation, the value is already
25409 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025410 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025412 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025413 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025415 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416 }
25417 }
25418
25419 return idx < 0;
25420}
25421
25422/*
25423 * Free the variable with a pending return value.
25424 */
25425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025426discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025427{
Bram Moolenaar33570922005-01-25 22:26:29 +000025428 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025429}
25430
25431/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025432 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025433 * is an allocated string. Used by report_pending() for verbose messages.
25434 */
25435 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025436get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025437{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025438 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025439 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025440 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025441
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025442 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025443 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025444 if (s == NULL)
25445 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025446
25447 STRCPY(IObuff, ":return ");
25448 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25449 if (STRLEN(s) + 8 >= IOSIZE)
25450 STRCPY(IObuff + IOSIZE - 4, "...");
25451 vim_free(tofree);
25452 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025453}
25454
25455/*
25456 * Get next function line.
25457 * Called by do_cmdline() to get the next line.
25458 * Returns allocated string, or NULL for end of function.
25459 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025461get_func_line(
25462 int c UNUSED,
25463 void *cookie,
25464 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025465{
Bram Moolenaar33570922005-01-25 22:26:29 +000025466 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025467 ufunc_T *fp = fcp->func;
25468 char_u *retval;
25469 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025470
25471 /* If breakpoints have been added/deleted need to check for it. */
25472 if (fcp->dbg_tick != debug_tick)
25473 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025474 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025475 sourcing_lnum);
25476 fcp->dbg_tick = debug_tick;
25477 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025478#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025479 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025480 func_line_end(cookie);
25481#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025482
Bram Moolenaar05159a02005-02-26 23:04:13 +000025483 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025484 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25485 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025486 retval = NULL;
25487 else
25488 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025489 /* Skip NULL lines (continuation lines). */
25490 while (fcp->linenr < gap->ga_len
25491 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25492 ++fcp->linenr;
25493 if (fcp->linenr >= gap->ga_len)
25494 retval = NULL;
25495 else
25496 {
25497 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25498 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025499#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025500 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025501 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025502#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025503 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025504 }
25505
25506 /* Did we encounter a breakpoint? */
25507 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25508 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025509 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025510 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025511 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025512 sourcing_lnum);
25513 fcp->dbg_tick = debug_tick;
25514 }
25515
25516 return retval;
25517}
25518
Bram Moolenaar05159a02005-02-26 23:04:13 +000025519#if defined(FEAT_PROFILE) || defined(PROTO)
25520/*
25521 * Called when starting to read a function line.
25522 * "sourcing_lnum" must be correct!
25523 * When skipping lines it may not actually be executed, but we won't find out
25524 * until later and we need to store the time now.
25525 */
25526 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025527func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025528{
25529 funccall_T *fcp = (funccall_T *)cookie;
25530 ufunc_T *fp = fcp->func;
25531
25532 if (fp->uf_profiling && sourcing_lnum >= 1
25533 && sourcing_lnum <= fp->uf_lines.ga_len)
25534 {
25535 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025536 /* Skip continuation lines. */
25537 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25538 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025539 fp->uf_tml_execed = FALSE;
25540 profile_start(&fp->uf_tml_start);
25541 profile_zero(&fp->uf_tml_children);
25542 profile_get_wait(&fp->uf_tml_wait);
25543 }
25544}
25545
25546/*
25547 * Called when actually executing a function line.
25548 */
25549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025550func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025551{
25552 funccall_T *fcp = (funccall_T *)cookie;
25553 ufunc_T *fp = fcp->func;
25554
25555 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25556 fp->uf_tml_execed = TRUE;
25557}
25558
25559/*
25560 * Called when done with a function line.
25561 */
25562 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025563func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025564{
25565 funccall_T *fcp = (funccall_T *)cookie;
25566 ufunc_T *fp = fcp->func;
25567
25568 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25569 {
25570 if (fp->uf_tml_execed)
25571 {
25572 ++fp->uf_tml_count[fp->uf_tml_idx];
25573 profile_end(&fp->uf_tml_start);
25574 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025575 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025576 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25577 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025578 }
25579 fp->uf_tml_idx = -1;
25580 }
25581}
25582#endif
25583
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584/*
25585 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025586 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025587 */
25588 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025589func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025590{
Bram Moolenaar33570922005-01-25 22:26:29 +000025591 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025592
25593 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25594 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025595 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025596 || fcp->returned);
25597}
25598
25599/*
25600 * return TRUE if cookie indicates a function which "abort"s on errors.
25601 */
25602 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025603func_has_abort(
25604 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025605{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025606 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025607}
25608
25609#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25610typedef enum
25611{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025612 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25613 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25614 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615} var_flavour_T;
25616
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025617static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025618
25619 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025620var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025621{
25622 char_u *p = varname;
25623
25624 if (ASCII_ISUPPER(*p))
25625 {
25626 while (*(++p))
25627 if (ASCII_ISLOWER(*p))
25628 return VAR_FLAVOUR_SESSION;
25629 return VAR_FLAVOUR_VIMINFO;
25630 }
25631 else
25632 return VAR_FLAVOUR_DEFAULT;
25633}
25634#endif
25635
25636#if defined(FEAT_VIMINFO) || defined(PROTO)
25637/*
25638 * Restore global vars that start with a capital from the viminfo file
25639 */
25640 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025641read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025642{
25643 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025644 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025645 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025646 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025647
25648 if (!writing && (find_viminfo_parameter('!') != NULL))
25649 {
25650 tab = vim_strchr(virp->vir_line + 1, '\t');
25651 if (tab != NULL)
25652 {
25653 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025654 switch (*tab)
25655 {
25656 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025657#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025658 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025659#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025660 case 'D': type = VAR_DICT; break;
25661 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025662 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025664
25665 tab = vim_strchr(tab, '\t');
25666 if (tab != NULL)
25667 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025668 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025669 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025670 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025671 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025672#ifdef FEAT_FLOAT
25673 else if (type == VAR_FLOAT)
25674 (void)string2float(tab + 1, &tv.vval.v_float);
25675#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025677 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025678 if (type == VAR_DICT || type == VAR_LIST)
25679 {
25680 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25681
25682 if (etv == NULL)
25683 /* Failed to parse back the dict or list, use it as a
25684 * string. */
25685 tv.v_type = VAR_STRING;
25686 else
25687 {
25688 vim_free(tv.vval.v_string);
25689 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025690 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025691 }
25692 }
25693
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025694 /* when in a function use global variables */
25695 save_funccal = current_funccal;
25696 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025697 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025698 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025699
25700 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025701 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025702 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25703 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025704 }
25705 }
25706 }
25707
25708 return viminfo_readline(virp);
25709}
25710
25711/*
25712 * Write global vars that start with a capital to the viminfo file
25713 */
25714 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025715write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025716{
Bram Moolenaar33570922005-01-25 22:26:29 +000025717 hashitem_T *hi;
25718 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025719 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025720 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025721 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025722 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025723 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025724
25725 if (find_viminfo_parameter('!') == NULL)
25726 return;
25727
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025728 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025729
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025730 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025731 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025732 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025733 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025734 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025735 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025736 this_var = HI2DI(hi);
25737 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025738 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025739 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025740 {
25741 case VAR_STRING: s = "STR"; break;
25742 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025743 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025744 case VAR_DICT: s = "DIC"; break;
25745 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025746 case VAR_SPECIAL: s = "XPL"; break;
25747
25748 case VAR_UNKNOWN:
25749 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025750 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025751 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025752 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025753 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025754 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025755 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025756 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025757 if (p != NULL)
25758 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025759 vim_free(tofree);
25760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025761 }
25762 }
25763}
25764#endif
25765
25766#if defined(FEAT_SESSION) || defined(PROTO)
25767 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025768store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769{
Bram Moolenaar33570922005-01-25 22:26:29 +000025770 hashitem_T *hi;
25771 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025772 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 char_u *p, *t;
25774
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025775 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025776 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025777 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025778 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025779 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025780 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025781 this_var = HI2DI(hi);
25782 if ((this_var->di_tv.v_type == VAR_NUMBER
25783 || this_var->di_tv.v_type == VAR_STRING)
25784 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025785 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025786 /* Escape special characters with a backslash. Turn a LF and
25787 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025788 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025789 (char_u *)"\\\"\n\r");
25790 if (p == NULL) /* out of memory */
25791 break;
25792 for (t = p; *t != NUL; ++t)
25793 if (*t == '\n')
25794 *t = 'n';
25795 else if (*t == '\r')
25796 *t = 'r';
25797 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025798 this_var->di_key,
25799 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25800 : ' ',
25801 p,
25802 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25803 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025804 || put_eol(fd) == FAIL)
25805 {
25806 vim_free(p);
25807 return FAIL;
25808 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025809 vim_free(p);
25810 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025811#ifdef FEAT_FLOAT
25812 else if (this_var->di_tv.v_type == VAR_FLOAT
25813 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25814 {
25815 float_T f = this_var->di_tv.vval.v_float;
25816 int sign = ' ';
25817
25818 if (f < 0)
25819 {
25820 f = -f;
25821 sign = '-';
25822 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025823 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025824 this_var->di_key, sign, f) < 0)
25825 || put_eol(fd) == FAIL)
25826 return FAIL;
25827 }
25828#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025829 }
25830 }
25831 return OK;
25832}
25833#endif
25834
Bram Moolenaar661b1822005-07-28 22:36:45 +000025835/*
25836 * Display script name where an item was last set.
25837 * Should only be invoked when 'verbose' is non-zero.
25838 */
25839 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025840last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025841{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025842 char_u *p;
25843
Bram Moolenaar661b1822005-07-28 22:36:45 +000025844 if (scriptID != 0)
25845 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025846 p = home_replace_save(NULL, get_scriptname(scriptID));
25847 if (p != NULL)
25848 {
25849 verbose_enter();
25850 MSG_PUTS(_("\n\tLast set from "));
25851 MSG_PUTS(p);
25852 vim_free(p);
25853 verbose_leave();
25854 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025855 }
25856}
25857
Bram Moolenaard812df62008-11-09 12:46:09 +000025858/*
25859 * List v:oldfiles in a nice way.
25860 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025861 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025862ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025863{
25864 list_T *l = vimvars[VV_OLDFILES].vv_list;
25865 listitem_T *li;
25866 int nr = 0;
25867
25868 if (l == NULL)
25869 msg((char_u *)_("No old files"));
25870 else
25871 {
25872 msg_start();
25873 msg_scroll = TRUE;
25874 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25875 {
25876 msg_outnum((long)++nr);
25877 MSG_PUTS(": ");
25878 msg_outtrans(get_tv_string(&li->li_tv));
25879 msg_putchar('\n');
25880 out_flush(); /* output one line at a time */
25881 ui_breakcheck();
25882 }
25883 /* Assume "got_int" was set to truncate the listing. */
25884 got_int = FALSE;
25885
25886#ifdef FEAT_BROWSE_CMD
25887 if (cmdmod.browse)
25888 {
25889 quit_more = FALSE;
25890 nr = prompt_for_number(FALSE);
25891 msg_starthere();
25892 if (nr > 0)
25893 {
25894 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25895 (long)nr);
25896
25897 if (p != NULL)
25898 {
25899 p = expand_env_save(p);
25900 eap->arg = p;
25901 eap->cmdidx = CMD_edit;
25902 cmdmod.browse = FALSE;
25903 do_exedit(eap, NULL);
25904 vim_free(p);
25905 }
25906 }
25907 }
25908#endif
25909 }
25910}
25911
Bram Moolenaar53744302015-07-17 17:38:22 +020025912/* reset v:option_new, v:option_old and v:option_type */
25913 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025914reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025915{
25916 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25917 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25918 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25919}
25920
25921
Bram Moolenaar071d4272004-06-13 20:20:40 +000025922#endif /* FEAT_EVAL */
25923
Bram Moolenaar071d4272004-06-13 20:20:40 +000025924
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025925#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926
25927#ifdef WIN3264
25928/*
25929 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25930 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025931static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25932static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25933static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934
25935/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025936 * Get the short path (8.3) for the filename in "fnamep".
25937 * Only works for a valid file name.
25938 * When the path gets longer "fnamep" is changed and the allocated buffer
25939 * is put in "bufp".
25940 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25941 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025942 */
25943 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025944get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025946 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947 char_u *newbuf;
25948
25949 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025950 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025951 if (l > len - 1)
25952 {
25953 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025954 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025955 newbuf = vim_strnsave(*fnamep, l);
25956 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025957 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025958
25959 vim_free(*bufp);
25960 *fnamep = *bufp = newbuf;
25961
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025962 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025963 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025964 }
25965
25966 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025967 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025968}
25969
25970/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025971 * Get the short path (8.3) for the filename in "fname". The converted
25972 * path is returned in "bufp".
25973 *
25974 * Some of the directories specified in "fname" may not exist. This function
25975 * will shorten the existing directories at the beginning of the path and then
25976 * append the remaining non-existing path.
25977 *
25978 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025979 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025980 * bufp - Pointer to an allocated buffer for the filename.
25981 * fnamelen - Length of the filename pointed to by fname
25982 *
25983 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 */
25985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025986shortpath_for_invalid_fname(
25987 char_u **fname,
25988 char_u **bufp,
25989 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025990{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025991 char_u *short_fname, *save_fname, *pbuf_unused;
25992 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025993 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025994 int old_len, len;
25995 int new_len, sfx_len;
25996 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997
25998 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025999 old_len = *fnamelen;
26000 save_fname = vim_strnsave(*fname, old_len);
26001 pbuf_unused = NULL;
26002 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026003
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026004 endp = save_fname + old_len - 1; /* Find the end of the copy */
26005 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026007 /*
26008 * Try shortening the supplied path till it succeeds by removing one
26009 * directory at a time from the tail of the path.
26010 */
26011 len = 0;
26012 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026013 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026014 /* go back one path-separator */
26015 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
26016 --endp;
26017 if (endp <= save_fname)
26018 break; /* processed the complete path */
26019
26020 /*
26021 * Replace the path separator with a NUL and try to shorten the
26022 * resulting path.
26023 */
26024 ch = *endp;
26025 *endp = 0;
26026 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000026027 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026028 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
26029 {
26030 retval = FAIL;
26031 goto theend;
26032 }
26033 *endp = ch; /* preserve the string */
26034
26035 if (len > 0)
26036 break; /* successfully shortened the path */
26037
26038 /* failed to shorten the path. Skip the path separator */
26039 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026040 }
26041
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026042 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026044 /*
26045 * Succeeded in shortening the path. Now concatenate the shortened
26046 * path with the remaining path at the tail.
26047 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026048
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026049 /* Compute the length of the new path. */
26050 sfx_len = (int)(save_endp - endp) + 1;
26051 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026052
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026053 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026054 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026055 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026056 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026057 /* There is not enough space in the currently allocated string,
26058 * copy it to a buffer big enough. */
26059 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026060 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026061 {
26062 retval = FAIL;
26063 goto theend;
26064 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065 }
26066 else
26067 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026068 /* Transfer short_fname to the main buffer (it's big enough),
26069 * unless get_short_pathname() did its work in-place. */
26070 *fname = *bufp = save_fname;
26071 if (short_fname != save_fname)
26072 vim_strncpy(save_fname, short_fname, len);
26073 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026074 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026075
26076 /* concat the not-shortened part of the path */
26077 vim_strncpy(*fname + len, endp, sfx_len);
26078 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026079 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026080
26081theend:
26082 vim_free(pbuf_unused);
26083 vim_free(save_fname);
26084
26085 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086}
26087
26088/*
26089 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026090 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026091 */
26092 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026093shortpath_for_partial(
26094 char_u **fnamep,
26095 char_u **bufp,
26096 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026097{
26098 int sepcount, len, tflen;
26099 char_u *p;
26100 char_u *pbuf, *tfname;
26101 int hasTilde;
26102
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026103 /* Count up the path separators from the RHS.. so we know which part
26104 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026105 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026106 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026107 if (vim_ispathsep(*p))
26108 ++sepcount;
26109
26110 /* Need full path first (use expand_env() to remove a "~/") */
26111 hasTilde = (**fnamep == '~');
26112 if (hasTilde)
26113 pbuf = tfname = expand_env_save(*fnamep);
26114 else
26115 pbuf = tfname = FullName_save(*fnamep, FALSE);
26116
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026117 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026119 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26120 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026121
26122 if (len == 0)
26123 {
26124 /* Don't have a valid filename, so shorten the rest of the
26125 * path if we can. This CAN give us invalid 8.3 filenames, but
26126 * there's not a lot of point in guessing what it might be.
26127 */
26128 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026129 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26130 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026131 }
26132
26133 /* Count the paths backward to find the beginning of the desired string. */
26134 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026135 {
26136#ifdef FEAT_MBYTE
26137 if (has_mbyte)
26138 p -= mb_head_off(tfname, p);
26139#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026140 if (vim_ispathsep(*p))
26141 {
26142 if (sepcount == 0 || (hasTilde && sepcount == 1))
26143 break;
26144 else
26145 sepcount --;
26146 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026147 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026148 if (hasTilde)
26149 {
26150 --p;
26151 if (p >= tfname)
26152 *p = '~';
26153 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026154 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026155 }
26156 else
26157 ++p;
26158
26159 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26160 vim_free(*bufp);
26161 *fnamelen = (int)STRLEN(p);
26162 *bufp = pbuf;
26163 *fnamep = p;
26164
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026165 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026166}
26167#endif /* WIN3264 */
26168
26169/*
26170 * Adjust a filename, according to a string of modifiers.
26171 * *fnamep must be NUL terminated when called. When returning, the length is
26172 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026173 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026174 * When there is an error, *fnamep is set to NULL.
26175 */
26176 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026177modify_fname(
26178 char_u *src, /* string with modifiers */
26179 int *usedlen, /* characters after src that are used */
26180 char_u **fnamep, /* file name so far */
26181 char_u **bufp, /* buffer for allocated file name or NULL */
26182 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026183{
26184 int valid = 0;
26185 char_u *tail;
26186 char_u *s, *p, *pbuf;
26187 char_u dirname[MAXPATHL];
26188 int c;
26189 int has_fullname = 0;
26190#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026191 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026192 int has_shortname = 0;
26193#endif
26194
26195repeat:
26196 /* ":p" - full path/file_name */
26197 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26198 {
26199 has_fullname = 1;
26200
26201 valid |= VALID_PATH;
26202 *usedlen += 2;
26203
26204 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26205 if ((*fnamep)[0] == '~'
26206#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26207 && ((*fnamep)[1] == '/'
26208# ifdef BACKSLASH_IN_FILENAME
26209 || (*fnamep)[1] == '\\'
26210# endif
26211 || (*fnamep)[1] == NUL)
26212
26213#endif
26214 )
26215 {
26216 *fnamep = expand_env_save(*fnamep);
26217 vim_free(*bufp); /* free any allocated file name */
26218 *bufp = *fnamep;
26219 if (*fnamep == NULL)
26220 return -1;
26221 }
26222
26223 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026224 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026225 {
26226 if (vim_ispathsep(*p)
26227 && p[1] == '.'
26228 && (p[2] == NUL
26229 || vim_ispathsep(p[2])
26230 || (p[2] == '.'
26231 && (p[3] == NUL || vim_ispathsep(p[3])))))
26232 break;
26233 }
26234
26235 /* FullName_save() is slow, don't use it when not needed. */
26236 if (*p != NUL || !vim_isAbsName(*fnamep))
26237 {
26238 *fnamep = FullName_save(*fnamep, *p != NUL);
26239 vim_free(*bufp); /* free any allocated file name */
26240 *bufp = *fnamep;
26241 if (*fnamep == NULL)
26242 return -1;
26243 }
26244
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026245#ifdef WIN3264
26246# if _WIN32_WINNT >= 0x0500
26247 if (vim_strchr(*fnamep, '~') != NULL)
26248 {
26249 /* Expand 8.3 filename to full path. Needed to make sure the same
26250 * file does not have two different names.
26251 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26252 p = alloc(_MAX_PATH + 1);
26253 if (p != NULL)
26254 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026255 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026256 {
26257 vim_free(*bufp);
26258 *bufp = *fnamep = p;
26259 }
26260 else
26261 vim_free(p);
26262 }
26263 }
26264# endif
26265#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026266 /* Append a path separator to a directory. */
26267 if (mch_isdir(*fnamep))
26268 {
26269 /* Make room for one or two extra characters. */
26270 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26271 vim_free(*bufp); /* free any allocated file name */
26272 *bufp = *fnamep;
26273 if (*fnamep == NULL)
26274 return -1;
26275 add_pathsep(*fnamep);
26276 }
26277 }
26278
26279 /* ":." - path relative to the current directory */
26280 /* ":~" - path relative to the home directory */
26281 /* ":8" - shortname path - postponed till after */
26282 while (src[*usedlen] == ':'
26283 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26284 {
26285 *usedlen += 2;
26286 if (c == '8')
26287 {
26288#ifdef WIN3264
26289 has_shortname = 1; /* Postpone this. */
26290#endif
26291 continue;
26292 }
26293 pbuf = NULL;
26294 /* Need full path first (use expand_env() to remove a "~/") */
26295 if (!has_fullname)
26296 {
26297 if (c == '.' && **fnamep == '~')
26298 p = pbuf = expand_env_save(*fnamep);
26299 else
26300 p = pbuf = FullName_save(*fnamep, FALSE);
26301 }
26302 else
26303 p = *fnamep;
26304
26305 has_fullname = 0;
26306
26307 if (p != NULL)
26308 {
26309 if (c == '.')
26310 {
26311 mch_dirname(dirname, MAXPATHL);
26312 s = shorten_fname(p, dirname);
26313 if (s != NULL)
26314 {
26315 *fnamep = s;
26316 if (pbuf != NULL)
26317 {
26318 vim_free(*bufp); /* free any allocated file name */
26319 *bufp = pbuf;
26320 pbuf = NULL;
26321 }
26322 }
26323 }
26324 else
26325 {
26326 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26327 /* Only replace it when it starts with '~' */
26328 if (*dirname == '~')
26329 {
26330 s = vim_strsave(dirname);
26331 if (s != NULL)
26332 {
26333 *fnamep = s;
26334 vim_free(*bufp);
26335 *bufp = s;
26336 }
26337 }
26338 }
26339 vim_free(pbuf);
26340 }
26341 }
26342
26343 tail = gettail(*fnamep);
26344 *fnamelen = (int)STRLEN(*fnamep);
26345
26346 /* ":h" - head, remove "/file_name", can be repeated */
26347 /* Don't remove the first "/" or "c:\" */
26348 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26349 {
26350 valid |= VALID_HEAD;
26351 *usedlen += 2;
26352 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026353 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026354 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 *fnamelen = (int)(tail - *fnamep);
26356#ifdef VMS
26357 if (*fnamelen > 0)
26358 *fnamelen += 1; /* the path separator is part of the path */
26359#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026360 if (*fnamelen == 0)
26361 {
26362 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26363 p = vim_strsave((char_u *)".");
26364 if (p == NULL)
26365 return -1;
26366 vim_free(*bufp);
26367 *bufp = *fnamep = tail = p;
26368 *fnamelen = 1;
26369 }
26370 else
26371 {
26372 while (tail > s && !after_pathsep(s, tail))
26373 mb_ptr_back(*fnamep, tail);
26374 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026375 }
26376
26377 /* ":8" - shortname */
26378 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26379 {
26380 *usedlen += 2;
26381#ifdef WIN3264
26382 has_shortname = 1;
26383#endif
26384 }
26385
26386#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026387 /*
26388 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026389 */
26390 if (has_shortname)
26391 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026392 /* Copy the string if it is shortened by :h and when it wasn't copied
26393 * yet, because we are going to change it in place. Avoids changing
26394 * the buffer name for "%:8". */
26395 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026396 {
26397 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026398 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026399 return -1;
26400 vim_free(*bufp);
26401 *bufp = *fnamep = p;
26402 }
26403
26404 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026405 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026406 if (!has_fullname && !vim_isAbsName(*fnamep))
26407 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026408 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026409 return -1;
26410 }
26411 else
26412 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026413 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026414
Bram Moolenaardc935552011-08-17 15:23:23 +020026415 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026416 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026417 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026418 return -1;
26419
26420 if (l == 0)
26421 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026422 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026423 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026424 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026425 return -1;
26426 }
26427 *fnamelen = l;
26428 }
26429 }
26430#endif /* WIN3264 */
26431
26432 /* ":t" - tail, just the basename */
26433 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26434 {
26435 *usedlen += 2;
26436 *fnamelen -= (int)(tail - *fnamep);
26437 *fnamep = tail;
26438 }
26439
26440 /* ":e" - extension, can be repeated */
26441 /* ":r" - root, without extension, can be repeated */
26442 while (src[*usedlen] == ':'
26443 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26444 {
26445 /* find a '.' in the tail:
26446 * - for second :e: before the current fname
26447 * - otherwise: The last '.'
26448 */
26449 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26450 s = *fnamep - 2;
26451 else
26452 s = *fnamep + *fnamelen - 1;
26453 for ( ; s > tail; --s)
26454 if (s[0] == '.')
26455 break;
26456 if (src[*usedlen + 1] == 'e') /* :e */
26457 {
26458 if (s > tail)
26459 {
26460 *fnamelen += (int)(*fnamep - (s + 1));
26461 *fnamep = s + 1;
26462#ifdef VMS
26463 /* cut version from the extension */
26464 s = *fnamep + *fnamelen - 1;
26465 for ( ; s > *fnamep; --s)
26466 if (s[0] == ';')
26467 break;
26468 if (s > *fnamep)
26469 *fnamelen = s - *fnamep;
26470#endif
26471 }
26472 else if (*fnamep <= tail)
26473 *fnamelen = 0;
26474 }
26475 else /* :r */
26476 {
26477 if (s > tail) /* remove one extension */
26478 *fnamelen = (int)(s - *fnamep);
26479 }
26480 *usedlen += 2;
26481 }
26482
26483 /* ":s?pat?foo?" - substitute */
26484 /* ":gs?pat?foo?" - global substitute */
26485 if (src[*usedlen] == ':'
26486 && (src[*usedlen + 1] == 's'
26487 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26488 {
26489 char_u *str;
26490 char_u *pat;
26491 char_u *sub;
26492 int sep;
26493 char_u *flags;
26494 int didit = FALSE;
26495
26496 flags = (char_u *)"";
26497 s = src + *usedlen + 2;
26498 if (src[*usedlen + 1] == 'g')
26499 {
26500 flags = (char_u *)"g";
26501 ++s;
26502 }
26503
26504 sep = *s++;
26505 if (sep)
26506 {
26507 /* find end of pattern */
26508 p = vim_strchr(s, sep);
26509 if (p != NULL)
26510 {
26511 pat = vim_strnsave(s, (int)(p - s));
26512 if (pat != NULL)
26513 {
26514 s = p + 1;
26515 /* find end of substitution */
26516 p = vim_strchr(s, sep);
26517 if (p != NULL)
26518 {
26519 sub = vim_strnsave(s, (int)(p - s));
26520 str = vim_strnsave(*fnamep, *fnamelen);
26521 if (sub != NULL && str != NULL)
26522 {
26523 *usedlen = (int)(p + 1 - src);
26524 s = do_string_sub(str, pat, sub, flags);
26525 if (s != NULL)
26526 {
26527 *fnamep = s;
26528 *fnamelen = (int)STRLEN(s);
26529 vim_free(*bufp);
26530 *bufp = s;
26531 didit = TRUE;
26532 }
26533 }
26534 vim_free(sub);
26535 vim_free(str);
26536 }
26537 vim_free(pat);
26538 }
26539 }
26540 /* after using ":s", repeat all the modifiers */
26541 if (didit)
26542 goto repeat;
26543 }
26544 }
26545
Bram Moolenaar26df0922014-02-23 23:39:13 +010026546 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26547 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026548 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026549 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026550 if (c != NUL)
26551 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026552 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026553 if (c != NUL)
26554 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026555 if (p == NULL)
26556 return -1;
26557 vim_free(*bufp);
26558 *bufp = *fnamep = p;
26559 *fnamelen = (int)STRLEN(p);
26560 *usedlen += 2;
26561 }
26562
Bram Moolenaar071d4272004-06-13 20:20:40 +000026563 return valid;
26564}
26565
26566/*
26567 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26568 * "flags" can be "g" to do a global substitute.
26569 * Returns an allocated string, NULL for error.
26570 */
26571 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026572do_string_sub(
26573 char_u *str,
26574 char_u *pat,
26575 char_u *sub,
26576 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026577{
26578 int sublen;
26579 regmatch_T regmatch;
26580 int i;
26581 int do_all;
26582 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026583 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026584 garray_T ga;
26585 char_u *ret;
26586 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026587 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026588
26589 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26590 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026591 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592
26593 ga_init2(&ga, 1, 200);
26594
26595 do_all = (flags[0] == 'g');
26596
26597 regmatch.rm_ic = p_ic;
26598 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26599 if (regmatch.regprog != NULL)
26600 {
26601 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026602 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026603 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26604 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026605 /* Skip empty match except for first match. */
26606 if (regmatch.startp[0] == regmatch.endp[0])
26607 {
26608 if (zero_width == regmatch.startp[0])
26609 {
26610 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026611 i = MB_PTR2LEN(tail);
26612 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26613 (size_t)i);
26614 ga.ga_len += i;
26615 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026616 continue;
26617 }
26618 zero_width = regmatch.startp[0];
26619 }
26620
Bram Moolenaar071d4272004-06-13 20:20:40 +000026621 /*
26622 * Get some space for a temporary buffer to do the substitution
26623 * into. It will contain:
26624 * - The text up to where the match is.
26625 * - The substituted text.
26626 * - The text after the match.
26627 */
26628 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026629 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026630 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26631 {
26632 ga_clear(&ga);
26633 break;
26634 }
26635
26636 /* copy the text up to where the match is */
26637 i = (int)(regmatch.startp[0] - tail);
26638 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26639 /* add the substituted text */
26640 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26641 + ga.ga_len + i, TRUE, TRUE, FALSE);
26642 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026643 tail = regmatch.endp[0];
26644 if (*tail == NUL)
26645 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026646 if (!do_all)
26647 break;
26648 }
26649
26650 if (ga.ga_data != NULL)
26651 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26652
Bram Moolenaar473de612013-06-08 18:19:48 +020026653 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026654 }
26655
26656 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26657 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026658 if (p_cpo == empty_option)
26659 p_cpo = save_cpo;
26660 else
26661 /* Darn, evaluating {sub} expression changed the value. */
26662 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026663
26664 return ret;
26665}
26666
26667#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */