blob: cd7ce25cd99e0a11494355ece25903b052adca5b [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);
676static void f_max(typval_T *argvars, typval_T *rettv);
677static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000678#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100682#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100683static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100684#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100685static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
686static void f_nr2char(typval_T *argvars, typval_T *rettv);
687static void f_or(typval_T *argvars, typval_T *rettv);
688static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100689#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100690static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100691#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000692#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100693static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000694#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
696static void f_printf(typval_T *argvars, typval_T *rettv);
697static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200698#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100699static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200700#endif
701#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100702static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200703#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100704static void f_range(typval_T *argvars, typval_T *rettv);
705static void f_readfile(typval_T *argvars, typval_T *rettv);
706static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100707#ifdef FEAT_FLOAT
708static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
709#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100710static void f_reltimestr(typval_T *argvars, typval_T *rettv);
711static void f_remote_expr(typval_T *argvars, typval_T *rettv);
712static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
713static void f_remote_peek(typval_T *argvars, typval_T *rettv);
714static void f_remote_read(typval_T *argvars, typval_T *rettv);
715static void f_remote_send(typval_T *argvars, typval_T *rettv);
716static void f_remove(typval_T *argvars, typval_T *rettv);
717static void f_rename(typval_T *argvars, typval_T *rettv);
718static void f_repeat(typval_T *argvars, typval_T *rettv);
719static void f_resolve(typval_T *argvars, typval_T *rettv);
720static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000721#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100722static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000723#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100724static void f_screenattr(typval_T *argvars, typval_T *rettv);
725static void f_screenchar(typval_T *argvars, typval_T *rettv);
726static void f_screencol(typval_T *argvars, typval_T *rettv);
727static void f_screenrow(typval_T *argvars, typval_T *rettv);
728static void f_search(typval_T *argvars, typval_T *rettv);
729static void f_searchdecl(typval_T *argvars, typval_T *rettv);
730static void f_searchpair(typval_T *argvars, typval_T *rettv);
731static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
732static void f_searchpos(typval_T *argvars, typval_T *rettv);
733static void f_server2client(typval_T *argvars, typval_T *rettv);
734static void f_serverlist(typval_T *argvars, typval_T *rettv);
735static void f_setbufvar(typval_T *argvars, typval_T *rettv);
736static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
737static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100738static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100739static void f_setline(typval_T *argvars, typval_T *rettv);
740static void f_setloclist(typval_T *argvars, typval_T *rettv);
741static void f_setmatches(typval_T *argvars, typval_T *rettv);
742static void f_setpos(typval_T *argvars, typval_T *rettv);
743static void f_setqflist(typval_T *argvars, typval_T *rettv);
744static void f_setreg(typval_T *argvars, typval_T *rettv);
745static void f_settabvar(typval_T *argvars, typval_T *rettv);
746static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
747static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100748#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100749static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100750#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_shellescape(typval_T *argvars, typval_T *rettv);
752static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
753static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000754#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100755static void f_sin(typval_T *argvars, typval_T *rettv);
756static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000757#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100758static void f_sort(typval_T *argvars, typval_T *rettv);
759static void f_soundfold(typval_T *argvars, typval_T *rettv);
760static void f_spellbadword(typval_T *argvars, typval_T *rettv);
761static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
762static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000763#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100764static void f_sqrt(typval_T *argvars, typval_T *rettv);
765static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000766#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100767static void f_str2nr(typval_T *argvars, typval_T *rettv);
768static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000769#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100770static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000771#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100772static void f_stridx(typval_T *argvars, typval_T *rettv);
773static void f_string(typval_T *argvars, typval_T *rettv);
774static void f_strlen(typval_T *argvars, typval_T *rettv);
775static void f_strpart(typval_T *argvars, typval_T *rettv);
776static void f_strridx(typval_T *argvars, typval_T *rettv);
777static void f_strtrans(typval_T *argvars, typval_T *rettv);
778static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
779static void f_strwidth(typval_T *argvars, typval_T *rettv);
780static void f_submatch(typval_T *argvars, typval_T *rettv);
781static void f_substitute(typval_T *argvars, typval_T *rettv);
782static void f_synID(typval_T *argvars, typval_T *rettv);
783static void f_synIDattr(typval_T *argvars, typval_T *rettv);
784static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
785static void f_synstack(typval_T *argvars, typval_T *rettv);
786static void f_synconcealed(typval_T *argvars, typval_T *rettv);
787static void f_system(typval_T *argvars, typval_T *rettv);
788static void f_systemlist(typval_T *argvars, typval_T *rettv);
789static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
790static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
791static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
792static void f_taglist(typval_T *argvars, typval_T *rettv);
793static void f_tagfiles(typval_T *argvars, typval_T *rettv);
794static void f_tempname(typval_T *argvars, typval_T *rettv);
795static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200796#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100797static void f_tan(typval_T *argvars, typval_T *rettv);
798static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200799#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100800#ifdef FEAT_TIMERS
801static void f_timer_start(typval_T *argvars, typval_T *rettv);
802static void f_timer_stop(typval_T *argvars, typval_T *rettv);
803#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_tolower(typval_T *argvars, typval_T *rettv);
805static void f_toupper(typval_T *argvars, typval_T *rettv);
806static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000807#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100808static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000809#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100810static void f_type(typval_T *argvars, typval_T *rettv);
811static void f_undofile(typval_T *argvars, typval_T *rettv);
812static void f_undotree(typval_T *argvars, typval_T *rettv);
813static void f_uniq(typval_T *argvars, typval_T *rettv);
814static void f_values(typval_T *argvars, typval_T *rettv);
815static void f_virtcol(typval_T *argvars, typval_T *rettv);
816static void f_visualmode(typval_T *argvars, typval_T *rettv);
817static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100818static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100819static void f_win_getid(typval_T *argvars, typval_T *rettv);
820static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
821static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
822static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100823static void f_winbufnr(typval_T *argvars, typval_T *rettv);
824static void f_wincol(typval_T *argvars, typval_T *rettv);
825static void f_winheight(typval_T *argvars, typval_T *rettv);
826static void f_winline(typval_T *argvars, typval_T *rettv);
827static void f_winnr(typval_T *argvars, typval_T *rettv);
828static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
829static void f_winrestview(typval_T *argvars, typval_T *rettv);
830static void f_winsaveview(typval_T *argvars, typval_T *rettv);
831static void f_winwidth(typval_T *argvars, typval_T *rettv);
832static void f_writefile(typval_T *argvars, typval_T *rettv);
833static void f_wordcount(typval_T *argvars, typval_T *rettv);
834static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000835
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100836static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
837static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
838static int get_env_len(char_u **arg);
839static int get_id_len(char_u **arg);
840static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
841static 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 +0000842#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
843#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
844 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100845static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
846static int eval_isnamec(int c);
847static int eval_isnamec1(int c);
848static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
849static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100850static typval_T *alloc_string_tv(char_u *string);
851static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100852#ifdef FEAT_FLOAT
853static float_T get_tv_float(typval_T *varp);
854#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100855static linenr_T get_tv_lnum(typval_T *argvars);
856static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100857static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
858static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
859static hashtab_T *find_var_ht(char_u *name, char_u **varname);
860static funccall_T *get_funccal(void);
861static void vars_clear_ext(hashtab_T *ht, int free_val);
862static void delete_var(hashtab_T *ht, hashitem_T *hi);
863static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
864static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
865static void set_var(char_u *name, typval_T *varp, int copy);
866static int var_check_ro(int flags, char_u *name, int use_gettext);
867static int var_check_fixed(int flags, char_u *name, int use_gettext);
868static int var_check_func_name(char_u *name, int new_var);
869static int valid_varname(char_u *varname);
870static int tv_check_lock(int lock, char_u *name, int use_gettext);
871static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
872static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100873static 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 +0100874static int eval_fname_script(char_u *p);
875static int eval_fname_sid(char_u *p);
876static void list_func_head(ufunc_T *fp, int indent);
877static ufunc_T *find_func(char_u *name);
878static int function_exists(char_u *name);
879static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000880#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100881static void func_do_profile(ufunc_T *fp);
882static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
883static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000884static int
885# ifdef __BORLANDC__
886 _RTLENTRYF
887# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100888 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000889static int
890# ifdef __BORLANDC__
891 _RTLENTRYF
892# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100893 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000894#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100895static int script_autoload(char_u *name, int reload);
896static char_u *autoload_name(char_u *name);
897static void cat_func_name(char_u *buf, ufunc_T *fp);
898static void func_free(ufunc_T *fp);
899static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
900static int can_free_funccal(funccall_T *fc, int copyID) ;
901static void free_funccal(funccall_T *fc, int free_val);
902static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
903static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
904static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
905static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
906static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
907static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
908static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
909static int write_list(FILE *fd, list_T *list, int binary);
910static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000911
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912
913#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100914static int compare_func_name(const void *s1, const void *s2);
915static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200916#endif
917
Bram Moolenaar33570922005-01-25 22:26:29 +0000918/*
919 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000920 */
921 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100922eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000923{
Bram Moolenaar33570922005-01-25 22:26:29 +0000924 int i;
925 struct vimvar *p;
926
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200927 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
928 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200929 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000930 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000931 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000932
933 for (i = 0; i < VV_LEN; ++i)
934 {
935 p = &vimvars[i];
936 STRCPY(p->vv_di.di_key, p->vv_name);
937 if (p->vv_flags & VV_RO)
938 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
939 else if (p->vv_flags & VV_RO_SBX)
940 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
941 else
942 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000943
944 /* add to v: scope dict, unless the value is not always available */
945 if (p->vv_type != VAR_UNKNOWN)
946 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000947 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000948 /* add to compat scope dict */
949 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000950 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100951 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
952
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000953 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100954 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200955 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100956 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100957
958 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
959 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
960 set_vim_var_nr(VV_NONE, VVAL_NONE);
961 set_vim_var_nr(VV_NULL, VVAL_NULL);
962
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200963 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200964
965#ifdef EBCDIC
966 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100967 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200968 */
969 sortFunctions();
970#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000971}
972
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000973#if defined(EXITFREE) || defined(PROTO)
974 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100975eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000976{
977 int i;
978 struct vimvar *p;
979
980 for (i = 0; i < VV_LEN; ++i)
981 {
982 p = &vimvars[i];
983 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000984 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000985 vim_free(p->vv_str);
986 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000987 }
988 else if (p->vv_di.di_tv.v_type == VAR_LIST)
989 {
990 list_unref(p->vv_list);
991 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000992 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000993 }
994 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000995 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000996 hash_clear(&compat_hashtab);
997
Bram Moolenaard9fba312005-06-26 22:34:35 +0000998 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100999# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +02001000 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001001# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001002
1003 /* global variables */
1004 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001005
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001006 /* autoloaded script names */
1007 ga_clear_strings(&ga_loaded);
1008
Bram Moolenaarcca74132013-09-25 21:00:28 +02001009 /* Script-local variables. First clear all the variables and in a second
1010 * loop free the scriptvar_T, because a variable in one script might hold
1011 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001013 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001014 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001015 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001016 ga_clear(&ga_scripts);
1017
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001018 /* unreferenced lists and dicts */
1019 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001020
1021 /* functions */
1022 free_all_functions();
1023 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001024}
1025#endif
1026
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001027/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028 * Return the name of the executed function.
1029 */
1030 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001031func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001032{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001033 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001034}
1035
1036/*
1037 * Return the address holding the next breakpoint line for a funccall cookie.
1038 */
1039 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001040func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001041{
Bram Moolenaar33570922005-01-25 22:26:29 +00001042 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001043}
1044
1045/*
1046 * Return the address holding the debug tick for a funccall cookie.
1047 */
1048 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001049func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001050{
Bram Moolenaar33570922005-01-25 22:26:29 +00001051 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001052}
1053
1054/*
1055 * Return the nesting level for a funccall cookie.
1056 */
1057 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001058func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001059{
Bram Moolenaar33570922005-01-25 22:26:29 +00001060 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061}
1062
1063/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001064funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001065
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001066/* pointer to list of previously used funccal, still around because some
1067 * item in it is still being used. */
1068funccall_T *previous_funccal = NULL;
1069
Bram Moolenaar071d4272004-06-13 20:20:40 +00001070/*
1071 * Return TRUE when a function was ended by a ":return" command.
1072 */
1073 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001074current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001075{
1076 return current_funccal->returned;
1077}
1078
1079
1080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001081 * Set an internal variable to a string value. Creates the variable if it does
1082 * not already exist.
1083 */
1084 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001085set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001086{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001087 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001088 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001089
1090 val = vim_strsave(value);
1091 if (val != NULL)
1092 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001094 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001095 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001096 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001097 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001098 }
1099 }
1100}
1101
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001102static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001103static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001104static char_u *redir_endp = NULL;
1105static char_u *redir_varname = NULL;
1106
1107/*
1108 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001109 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110 * Returns OK if successfully completed the setup. FAIL otherwise.
1111 */
1112 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001113var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001114{
1115 int save_emsg;
1116 int err;
1117 typval_T tv;
1118
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001119 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001120 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001121 {
1122 EMSG(_(e_invarg));
1123 return FAIL;
1124 }
1125
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001126 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001127 redir_varname = vim_strsave(name);
1128 if (redir_varname == NULL)
1129 return FAIL;
1130
1131 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1132 if (redir_lval == NULL)
1133 {
1134 var_redir_stop();
1135 return FAIL;
1136 }
1137
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001138 /* The output is stored in growarray "redir_ga" until redirection ends. */
1139 ga_init2(&redir_ga, (int)sizeof(char), 500);
1140
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001141 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001142 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001143 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001144 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1145 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001146 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001147 if (redir_endp != NULL && *redir_endp != NUL)
1148 /* Trailing characters are present after the variable name */
1149 EMSG(_(e_trailing));
1150 else
1151 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001152 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001153 var_redir_stop();
1154 return FAIL;
1155 }
1156
1157 /* check if we can write to the variable: set it to or append an empty
1158 * string */
1159 save_emsg = did_emsg;
1160 did_emsg = FALSE;
1161 tv.v_type = VAR_STRING;
1162 tv.vval.v_string = (char_u *)"";
1163 if (append)
1164 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1165 else
1166 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001167 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001168 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001169 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001170 if (err)
1171 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001172 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001173 var_redir_stop();
1174 return FAIL;
1175 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001176
1177 return OK;
1178}
1179
1180/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001181 * Append "value[value_len]" to the variable set by var_redir_start().
1182 * The actual appending is postponed until redirection ends, because the value
1183 * appended may in fact be the string we write to, changing it may cause freed
1184 * memory to be used:
1185 * :redir => foo
1186 * :let foo
1187 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001188 */
1189 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001190var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001191{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001192 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001193
1194 if (redir_lval == NULL)
1195 return;
1196
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001197 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001199 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001200 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001201
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001202 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001203 {
1204 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001205 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001206 }
1207 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001208 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001209}
1210
1211/*
1212 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001213 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001214 */
1215 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001216var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001217{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001218 typval_T tv;
1219
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001220 if (redir_lval != NULL)
1221 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001222 /* If there was no error: assign the text to the variable. */
1223 if (redir_endp != NULL)
1224 {
1225 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1226 tv.v_type = VAR_STRING;
1227 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001228 /* Call get_lval() again, if it's inside a Dict or List it may
1229 * have changed. */
1230 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001231 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001232 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1233 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1234 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001235 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001237 /* free the collected output */
1238 vim_free(redir_ga.ga_data);
1239 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001240
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001241 vim_free(redir_lval);
1242 redir_lval = NULL;
1243 }
1244 vim_free(redir_varname);
1245 redir_varname = NULL;
1246}
1247
Bram Moolenaar071d4272004-06-13 20:20:40 +00001248# if defined(FEAT_MBYTE) || defined(PROTO)
1249 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001250eval_charconvert(
1251 char_u *enc_from,
1252 char_u *enc_to,
1253 char_u *fname_from,
1254 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001255{
1256 int err = FALSE;
1257
1258 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1259 set_vim_var_string(VV_CC_TO, enc_to, -1);
1260 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1261 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1262 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1263 err = TRUE;
1264 set_vim_var_string(VV_CC_FROM, NULL, -1);
1265 set_vim_var_string(VV_CC_TO, NULL, -1);
1266 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1267 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1268
1269 if (err)
1270 return FAIL;
1271 return OK;
1272}
1273# endif
1274
1275# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1276 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001277eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001278{
1279 int err = FALSE;
1280
1281 set_vim_var_string(VV_FNAME_IN, fname, -1);
1282 set_vim_var_string(VV_CMDARG, args, -1);
1283 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1284 err = TRUE;
1285 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1286 set_vim_var_string(VV_CMDARG, NULL, -1);
1287
1288 if (err)
1289 {
1290 mch_remove(fname);
1291 return FAIL;
1292 }
1293 return OK;
1294}
1295# endif
1296
1297# if defined(FEAT_DIFF) || defined(PROTO)
1298 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001299eval_diff(
1300 char_u *origfile,
1301 char_u *newfile,
1302 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001303{
1304 int err = FALSE;
1305
1306 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1307 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1308 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1309 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1310 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1311 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1312 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1313}
1314
1315 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001316eval_patch(
1317 char_u *origfile,
1318 char_u *difffile,
1319 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001320{
1321 int err;
1322
1323 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1324 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1325 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1326 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1327 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1328 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1329 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1330}
1331# endif
1332
1333/*
1334 * Top level evaluation function, returning a boolean.
1335 * Sets "error" to TRUE if there was an error.
1336 * Return TRUE or FALSE.
1337 */
1338 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001339eval_to_bool(
1340 char_u *arg,
1341 int *error,
1342 char_u **nextcmd,
1343 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001344{
Bram Moolenaar33570922005-01-25 22:26:29 +00001345 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001346 int retval = FALSE;
1347
1348 if (skip)
1349 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001350 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001351 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001352 else
1353 {
1354 *error = FALSE;
1355 if (!skip)
1356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001357 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001358 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001359 }
1360 }
1361 if (skip)
1362 --emsg_skip;
1363
1364 return retval;
1365}
1366
1367/*
1368 * Top level evaluation function, returning a string. If "skip" is TRUE,
1369 * only parsing to "nextcmd" is done, without reporting errors. Return
1370 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1371 */
1372 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001373eval_to_string_skip(
1374 char_u *arg,
1375 char_u **nextcmd,
1376 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001377{
Bram Moolenaar33570922005-01-25 22:26:29 +00001378 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001379 char_u *retval;
1380
1381 if (skip)
1382 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 retval = NULL;
1385 else
1386 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001387 retval = vim_strsave(get_tv_string(&tv));
1388 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001389 }
1390 if (skip)
1391 --emsg_skip;
1392
1393 return retval;
1394}
1395
1396/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001397 * Skip over an expression at "*pp".
1398 * Return FAIL for an error, OK otherwise.
1399 */
1400 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001401skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001402{
Bram Moolenaar33570922005-01-25 22:26:29 +00001403 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001404
1405 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001406 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001407}
1408
1409/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001410 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001411 * When "convert" is TRUE convert a List into a sequence of lines and convert
1412 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 * Return pointer to allocated memory, or NULL for failure.
1414 */
1415 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001416eval_to_string(
1417 char_u *arg,
1418 char_u **nextcmd,
1419 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001420{
Bram Moolenaar33570922005-01-25 22:26:29 +00001421 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001422 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001423 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001424#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001425 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001426#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001427
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001428 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001429 retval = NULL;
1430 else
1431 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001432 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001433 {
1434 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001435 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001436 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001437 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001438 if (tv.vval.v_list->lv_len > 0)
1439 ga_append(&ga, NL);
1440 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001441 ga_append(&ga, NUL);
1442 retval = (char_u *)ga.ga_data;
1443 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001444#ifdef FEAT_FLOAT
1445 else if (convert && tv.v_type == VAR_FLOAT)
1446 {
1447 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1448 retval = vim_strsave(numbuf);
1449 }
1450#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001451 else
1452 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001453 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001454 }
1455
1456 return retval;
1457}
1458
1459/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001460 * Call eval_to_string() without using current local variables and using
1461 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001462 */
1463 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001464eval_to_string_safe(
1465 char_u *arg,
1466 char_u **nextcmd,
1467 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001468{
1469 char_u *retval;
1470 void *save_funccalp;
1471
1472 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001473 if (use_sandbox)
1474 ++sandbox;
1475 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001476 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001477 if (use_sandbox)
1478 --sandbox;
1479 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480 restore_funccal(save_funccalp);
1481 return retval;
1482}
1483
Bram Moolenaar071d4272004-06-13 20:20:40 +00001484/*
1485 * Top level evaluation function, returning a number.
1486 * Evaluates "expr" silently.
1487 * Returns -1 for an error.
1488 */
1489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001490eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491{
Bram Moolenaar33570922005-01-25 22:26:29 +00001492 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001494 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495
1496 ++emsg_off;
1497
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001498 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001499 retval = -1;
1500 else
1501 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001502 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001503 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001504 }
1505 --emsg_off;
1506
1507 return retval;
1508}
1509
Bram Moolenaara40058a2005-07-11 22:42:07 +00001510/*
1511 * Prepare v: variable "idx" to be used.
1512 * Save the current typeval in "save_tv".
1513 * When not used yet add the variable to the v: hashtable.
1514 */
1515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001516prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001517{
1518 *save_tv = vimvars[idx].vv_tv;
1519 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1520 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1521}
1522
1523/*
1524 * Restore v: variable "idx" to typeval "save_tv".
1525 * When no longer defined, remove the variable from the v: hashtable.
1526 */
1527 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001528restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001529{
1530 hashitem_T *hi;
1531
Bram Moolenaara40058a2005-07-11 22:42:07 +00001532 vimvars[idx].vv_tv = *save_tv;
1533 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1534 {
1535 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1536 if (HASHITEM_EMPTY(hi))
1537 EMSG2(_(e_intern2), "restore_vimvar()");
1538 else
1539 hash_remove(&vimvarht, hi);
1540 }
1541}
1542
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001543#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544/*
1545 * Evaluate an expression to a list with suggestions.
1546 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001547 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001548 */
1549 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001550eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001551{
1552 typval_T save_val;
1553 typval_T rettv;
1554 list_T *list = NULL;
1555 char_u *p = skipwhite(expr);
1556
1557 /* Set "v:val" to the bad word. */
1558 prepare_vimvar(VV_VAL, &save_val);
1559 vimvars[VV_VAL].vv_type = VAR_STRING;
1560 vimvars[VV_VAL].vv_str = badword;
1561 if (p_verbose == 0)
1562 ++emsg_off;
1563
1564 if (eval1(&p, &rettv, TRUE) == OK)
1565 {
1566 if (rettv.v_type != VAR_LIST)
1567 clear_tv(&rettv);
1568 else
1569 list = rettv.vval.v_list;
1570 }
1571
1572 if (p_verbose == 0)
1573 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001574 restore_vimvar(VV_VAL, &save_val);
1575
1576 return list;
1577}
1578
1579/*
1580 * "list" is supposed to contain two items: a word and a number. Return the
1581 * word in "pp" and the number as the return value.
1582 * Return -1 if anything isn't right.
1583 * Used to get the good word and score from the eval_spell_expr() result.
1584 */
1585 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001586get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001587{
1588 listitem_T *li;
1589
1590 li = list->lv_first;
1591 if (li == NULL)
1592 return -1;
1593 *pp = get_tv_string(&li->li_tv);
1594
1595 li = li->li_next;
1596 if (li == NULL)
1597 return -1;
1598 return get_tv_number(&li->li_tv);
1599}
1600#endif
1601
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001603 * Top level evaluation function.
1604 * Returns an allocated typval_T with the result.
1605 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001606 */
1607 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001608eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001609{
1610 typval_T *tv;
1611
1612 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001613 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001614 {
1615 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001616 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001617 }
1618
1619 return tv;
1620}
1621
1622
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001624 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001625 * Uses argv[argc] for the function arguments. Only Number and String
1626 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001627 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001628 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001629 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001630call_vim_function(
1631 char_u *func,
1632 int argc,
1633 char_u **argv,
1634 int safe, /* use the sandbox */
1635 int str_arg_only, /* all arguments are strings */
1636 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637{
Bram Moolenaar33570922005-01-25 22:26:29 +00001638 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001639 long n;
1640 int len;
1641 int i;
1642 int doesrange;
1643 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001646 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001647 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001648 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001649
1650 for (i = 0; i < argc; i++)
1651 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001652 /* Pass a NULL or empty argument as an empty string */
1653 if (argv[i] == NULL || *argv[i] == NUL)
1654 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001655 argvars[i].v_type = VAR_STRING;
1656 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001657 continue;
1658 }
1659
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001660 if (str_arg_only)
1661 len = 0;
1662 else
1663 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001664 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 if (len != 0 && len == (int)STRLEN(argv[i]))
1666 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001667 argvars[i].v_type = VAR_NUMBER;
1668 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001669 }
1670 else
1671 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001672 argvars[i].v_type = VAR_STRING;
1673 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001674 }
1675 }
1676
1677 if (safe)
1678 {
1679 save_funccalp = save_funccal();
1680 ++sandbox;
1681 }
1682
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001683 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1684 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001685 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001686 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001687 if (safe)
1688 {
1689 --sandbox;
1690 restore_funccal(save_funccalp);
1691 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001692 vim_free(argvars);
1693
1694 if (ret == FAIL)
1695 clear_tv(rettv);
1696
1697 return ret;
1698}
1699
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001700/*
1701 * Call vimL function "func" and return the result as a number.
1702 * Returns -1 when calling the function fails.
1703 * Uses argv[argc] for the function arguments.
1704 */
1705 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001706call_func_retnr(
1707 char_u *func,
1708 int argc,
1709 char_u **argv,
1710 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001711{
1712 typval_T rettv;
1713 long retval;
1714
1715 /* All arguments are passed as strings, no conversion to number. */
1716 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1717 return -1;
1718
1719 retval = get_tv_number_chk(&rettv, NULL);
1720 clear_tv(&rettv);
1721 return retval;
1722}
1723
1724#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1725 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1726
Bram Moolenaar4f688582007-07-24 12:34:30 +00001727# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001728/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001729 * Call vimL function "func" and return the result as a string.
1730 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001731 * Uses argv[argc] for the function arguments.
1732 */
1733 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001734call_func_retstr(
1735 char_u *func,
1736 int argc,
1737 char_u **argv,
1738 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001739{
1740 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001741 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001742
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001743 /* All arguments are passed as strings, no conversion to number. */
1744 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001745 return NULL;
1746
1747 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001749 return retval;
1750}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001751# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001752
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001753/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001754 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001755 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001756 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001757 */
1758 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001759call_func_retlist(
1760 char_u *func,
1761 int argc,
1762 char_u **argv,
1763 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001764{
1765 typval_T rettv;
1766
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001767 /* All arguments are passed as strings, no conversion to number. */
1768 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001769 return NULL;
1770
1771 if (rettv.v_type != VAR_LIST)
1772 {
1773 clear_tv(&rettv);
1774 return NULL;
1775 }
1776
1777 return rettv.vval.v_list;
1778}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001779#endif
1780
1781/*
1782 * Save the current function call pointer, and set it to NULL.
1783 * Used when executing autocommands and for ":source".
1784 */
1785 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001786save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001787{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001788 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001789
Bram Moolenaar071d4272004-06-13 20:20:40 +00001790 current_funccal = NULL;
1791 return (void *)fc;
1792}
1793
1794 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001795restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001797 funccall_T *fc = (funccall_T *)vfc;
1798
1799 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001800}
1801
Bram Moolenaar05159a02005-02-26 23:04:13 +00001802#if defined(FEAT_PROFILE) || defined(PROTO)
1803/*
1804 * Prepare profiling for entering a child or something else that is not
1805 * counted for the script/function itself.
1806 * Should always be called in pair with prof_child_exit().
1807 */
1808 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001809prof_child_enter(
1810 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001811{
1812 funccall_T *fc = current_funccal;
1813
1814 if (fc != NULL && fc->func->uf_profiling)
1815 profile_start(&fc->prof_child);
1816 script_prof_save(tm);
1817}
1818
1819/*
1820 * Take care of time spent in a child.
1821 * Should always be called after prof_child_enter().
1822 */
1823 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001824prof_child_exit(
1825 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001826{
1827 funccall_T *fc = current_funccal;
1828
1829 if (fc != NULL && fc->func->uf_profiling)
1830 {
1831 profile_end(&fc->prof_child);
1832 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1833 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1834 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1835 }
1836 script_prof_restore(tm);
1837}
1838#endif
1839
1840
Bram Moolenaar071d4272004-06-13 20:20:40 +00001841#ifdef FEAT_FOLDING
1842/*
1843 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1844 * it in "*cp". Doesn't give error messages.
1845 */
1846 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001847eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001848{
Bram Moolenaar33570922005-01-25 22:26:29 +00001849 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850 int retval;
1851 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001852 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1853 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001854
1855 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001856 if (use_sandbox)
1857 ++sandbox;
1858 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001859 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001860 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 retval = 0;
1862 else
1863 {
1864 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001865 if (tv.v_type == VAR_NUMBER)
1866 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001867 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001868 retval = 0;
1869 else
1870 {
1871 /* If the result is a string, check if there is a non-digit before
1872 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001873 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001874 if (!VIM_ISDIGIT(*s) && *s != '-')
1875 *cp = *s++;
1876 retval = atol((char *)s);
1877 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001878 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 }
1880 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001881 if (use_sandbox)
1882 --sandbox;
1883 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001884
1885 return retval;
1886}
1887#endif
1888
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001890 * ":let" list all variable values
1891 * ":let var1 var2" list variable values
1892 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001893 * ":let var += expr" assignment command.
1894 * ":let var -= expr" assignment command.
1895 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001896 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 */
1898 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001899ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900{
1901 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001902 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001903 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001904 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001905 int var_count = 0;
1906 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001907 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001908 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001909 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910
Bram Moolenaardb552d602006-03-23 22:59:57 +00001911 argend = skip_var_list(arg, &var_count, &semicolon);
1912 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001913 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001914 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1915 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001916 expr = skipwhite(argend);
1917 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1918 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001919 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001920 /*
1921 * ":let" without "=": list variables
1922 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001923 if (*arg == '[')
1924 EMSG(_(e_invarg));
1925 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001928 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001929 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001930 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_glob_vars(&first);
1932 list_buf_vars(&first);
1933 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001934#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001935 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001936#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001937 list_script_vars(&first);
1938 list_func_vars(&first);
1939 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001940 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 eap->nextcmd = check_nextcmd(arg);
1942 }
1943 else
1944 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001945 op[0] = '=';
1946 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001947 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1950 op[0] = *expr; /* +=, -= or .= */
1951 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001952 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001953 else
1954 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001955
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 if (eap->skip)
1957 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001958 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 if (eap->skip)
1960 {
1961 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001962 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 --emsg_skip;
1964 }
1965 else if (i != FAIL)
1966 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001967 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001968 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001969 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001970 }
1971 }
1972}
1973
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001974/*
1975 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1976 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001977 * When "nextchars" is not NULL it points to a string with characters that
1978 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1979 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001980 * Returns OK or FAIL;
1981 */
1982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001983ex_let_vars(
1984 char_u *arg_start,
1985 typval_T *tv,
1986 int copy, /* copy values from "tv", don't move */
1987 int semicolon, /* from skip_var_list() */
1988 int var_count, /* from skip_var_list() */
1989 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001990{
1991 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001992 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001993 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001994 listitem_T *item;
1995 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001996
1997 if (*arg != '[')
1998 {
1999 /*
2000 * ":let var = expr" or ":for var in list"
2001 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002002 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002003 return FAIL;
2004 return OK;
2005 }
2006
2007 /*
2008 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2009 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002010 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002011 {
2012 EMSG(_(e_listreq));
2013 return FAIL;
2014 }
2015
2016 i = list_len(l);
2017 if (semicolon == 0 && var_count < i)
2018 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002019 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002020 return FAIL;
2021 }
2022 if (var_count - semicolon > i)
2023 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002024 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002025 return FAIL;
2026 }
2027
2028 item = l->lv_first;
2029 while (*arg != ']')
2030 {
2031 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002032 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002033 item = item->li_next;
2034 if (arg == NULL)
2035 return FAIL;
2036
2037 arg = skipwhite(arg);
2038 if (*arg == ';')
2039 {
2040 /* Put the rest of the list (may be empty) in the var after ';'.
2041 * Create a new list for this. */
2042 l = list_alloc();
2043 if (l == NULL)
2044 return FAIL;
2045 while (item != NULL)
2046 {
2047 list_append_tv(l, &item->li_tv);
2048 item = item->li_next;
2049 }
2050
2051 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002052 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002053 ltv.vval.v_list = l;
2054 l->lv_refcount = 1;
2055
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002056 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2057 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002058 clear_tv(&ltv);
2059 if (arg == NULL)
2060 return FAIL;
2061 break;
2062 }
2063 else if (*arg != ',' && *arg != ']')
2064 {
2065 EMSG2(_(e_intern2), "ex_let_vars()");
2066 return FAIL;
2067 }
2068 }
2069
2070 return OK;
2071}
2072
2073/*
2074 * Skip over assignable variable "var" or list of variables "[var, var]".
2075 * Used for ":let varvar = expr" and ":for varvar in expr".
2076 * For "[var, var]" increment "*var_count" for each variable.
2077 * for "[var, var; var]" set "semicolon".
2078 * Return NULL for an error.
2079 */
2080 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002081skip_var_list(
2082 char_u *arg,
2083 int *var_count,
2084 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002085{
2086 char_u *p, *s;
2087
2088 if (*arg == '[')
2089 {
2090 /* "[var, var]": find the matching ']'. */
2091 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002092 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002093 {
2094 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2095 s = skip_var_one(p);
2096 if (s == p)
2097 {
2098 EMSG2(_(e_invarg2), p);
2099 return NULL;
2100 }
2101 ++*var_count;
2102
2103 p = skipwhite(s);
2104 if (*p == ']')
2105 break;
2106 else if (*p == ';')
2107 {
2108 if (*semicolon == 1)
2109 {
2110 EMSG(_("Double ; in list of variables"));
2111 return NULL;
2112 }
2113 *semicolon = 1;
2114 }
2115 else if (*p != ',')
2116 {
2117 EMSG2(_(e_invarg2), p);
2118 return NULL;
2119 }
2120 }
2121 return p + 1;
2122 }
2123 else
2124 return skip_var_one(arg);
2125}
2126
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002127/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002128 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002129 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002130 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002131 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002132skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002133{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002134 if (*arg == '@' && arg[1] != NUL)
2135 return arg + 2;
2136 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2137 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002138}
2139
Bram Moolenaara7043832005-01-21 11:56:39 +00002140/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002141 * List variables for hashtab "ht" with prefix "prefix".
2142 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002143 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002145list_hashtable_vars(
2146 hashtab_T *ht,
2147 char_u *prefix,
2148 int empty,
2149 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002150{
Bram Moolenaar33570922005-01-25 22:26:29 +00002151 hashitem_T *hi;
2152 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002153 int todo;
2154
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002155 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002156 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2157 {
2158 if (!HASHITEM_EMPTY(hi))
2159 {
2160 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002161 di = HI2DI(hi);
2162 if (empty || di->di_tv.v_type != VAR_STRING
2163 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002164 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002165 }
2166 }
2167}
2168
2169/*
2170 * List global variables.
2171 */
2172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002173list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002174{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002175 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002176}
2177
2178/*
2179 * List buffer variables.
2180 */
2181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002182list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002183{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184 char_u numbuf[NUMBUFLEN];
2185
Bram Moolenaar429fa852013-04-15 12:27:36 +02002186 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002187 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002188
2189 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002190 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2191 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002192}
2193
2194/*
2195 * List window variables.
2196 */
2197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002198list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002199{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002200 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002201 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002202}
2203
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002204#ifdef FEAT_WINDOWS
2205/*
2206 * List tab page variables.
2207 */
2208 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002209list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002210{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002211 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002212 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002213}
2214#endif
2215
Bram Moolenaara7043832005-01-21 11:56:39 +00002216/*
2217 * List Vim variables.
2218 */
2219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002220list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002221{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002222 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002223}
2224
2225/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226 * List script-local variables, if there is a script.
2227 */
2228 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002229list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002230{
2231 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002232 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2233 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002234}
2235
2236/*
2237 * List function variables, if there is a function.
2238 */
2239 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002240list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241{
2242 if (current_funccal != NULL)
2243 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002244 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002245}
2246
2247/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248 * List variables in "arg".
2249 */
2250 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002251list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002252{
2253 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002254 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002255 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002256 char_u *name_start;
2257 char_u *arg_subsc;
2258 char_u *tofree;
2259 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260
2261 while (!ends_excmd(*arg) && !got_int)
2262 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002263 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002264 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002265 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002266 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2267 {
2268 emsg_severe = TRUE;
2269 EMSG(_(e_trailing));
2270 break;
2271 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002272 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002273 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002274 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002275 /* get_name_len() takes care of expanding curly braces */
2276 name_start = name = arg;
2277 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2278 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 /* This is mainly to keep test 49 working: when expanding
2281 * curly braces fails overrule the exception error message. */
2282 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002283 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002284 emsg_severe = TRUE;
2285 EMSG2(_(e_invarg2), arg);
2286 break;
2287 }
2288 error = TRUE;
2289 }
2290 else
2291 {
2292 if (tofree != NULL)
2293 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002294 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002295 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002296 else
2297 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002298 /* handle d.key, l[idx], f(expr) */
2299 arg_subsc = arg;
2300 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002302 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002304 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002305 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002306 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002307 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 'g': list_glob_vars(first); break;
2309 case 'b': list_buf_vars(first); break;
2310 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002311#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002312 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002313#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002314 case 'v': list_vim_vars(first); break;
2315 case 's': list_script_vars(first); break;
2316 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002317 default:
2318 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002319 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002320 }
2321 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002322 {
2323 char_u numbuf[NUMBUFLEN];
2324 char_u *tf;
2325 int c;
2326 char_u *s;
2327
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002328 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002329 c = *arg;
2330 *arg = NUL;
2331 list_one_var_a((char_u *)"",
2332 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002333 tv.v_type,
2334 s == NULL ? (char_u *)"" : s,
2335 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002336 *arg = c;
2337 vim_free(tf);
2338 }
2339 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002340 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
2342 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002343
2344 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002345 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002346
2347 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002348 }
2349
2350 return arg;
2351}
2352
2353/*
2354 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2355 * Returns a pointer to the char just after the var name.
2356 * Returns NULL if there is an error.
2357 */
2358 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002359ex_let_one(
2360 char_u *arg, /* points to variable name */
2361 typval_T *tv, /* value to assign to variable */
2362 int copy, /* copy value from "tv" */
2363 char_u *endchars, /* valid chars after variable name or NULL */
2364 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002365{
2366 int c1;
2367 char_u *name;
2368 char_u *p;
2369 char_u *arg_end = NULL;
2370 int len;
2371 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002372 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002373
2374 /*
2375 * ":let $VAR = expr": Set environment variable.
2376 */
2377 if (*arg == '$')
2378 {
2379 /* Find the end of the name. */
2380 ++arg;
2381 name = arg;
2382 len = get_env_len(&arg);
2383 if (len == 0)
2384 EMSG2(_(e_invarg2), name - 1);
2385 else
2386 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002387 if (op != NULL && (*op == '+' || *op == '-'))
2388 EMSG2(_(e_letwrong), op);
2389 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002390 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002391 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002392 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002393 {
2394 c1 = name[len];
2395 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002396 p = get_tv_string_chk(tv);
2397 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002398 {
2399 int mustfree = FALSE;
2400 char_u *s = vim_getenv(name, &mustfree);
2401
2402 if (s != NULL)
2403 {
2404 p = tofree = concat_str(s, p);
2405 if (mustfree)
2406 vim_free(s);
2407 }
2408 }
2409 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002410 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002411 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002412 if (STRICMP(name, "HOME") == 0)
2413 init_homedir();
2414 else if (didset_vim && STRICMP(name, "VIM") == 0)
2415 didset_vim = FALSE;
2416 else if (didset_vimruntime
2417 && STRICMP(name, "VIMRUNTIME") == 0)
2418 didset_vimruntime = FALSE;
2419 arg_end = arg;
2420 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002421 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002422 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002423 }
2424 }
2425 }
2426
2427 /*
2428 * ":let &option = expr": Set option value.
2429 * ":let &l:option = expr": Set local option value.
2430 * ":let &g:option = expr": Set global option value.
2431 */
2432 else if (*arg == '&')
2433 {
2434 /* Find the end of the name. */
2435 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002436 if (p == NULL || (endchars != NULL
2437 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002438 EMSG(_(e_letunexp));
2439 else
2440 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002441 long n;
2442 int opt_type;
2443 long numval;
2444 char_u *stringval = NULL;
2445 char_u *s;
2446
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002447 c1 = *p;
2448 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449
2450 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002451 s = get_tv_string_chk(tv); /* != NULL if number or string */
2452 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002453 {
2454 opt_type = get_option_value(arg, &numval,
2455 &stringval, opt_flags);
2456 if ((opt_type == 1 && *op == '.')
2457 || (opt_type == 0 && *op != '.'))
2458 EMSG2(_(e_letwrong), op);
2459 else
2460 {
2461 if (opt_type == 1) /* number */
2462 {
2463 if (*op == '+')
2464 n = numval + n;
2465 else
2466 n = numval - n;
2467 }
2468 else if (opt_type == 0 && stringval != NULL) /* string */
2469 {
2470 s = concat_str(stringval, s);
2471 vim_free(stringval);
2472 stringval = s;
2473 }
2474 }
2475 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002476 if (s != NULL)
2477 {
2478 set_option_value(arg, n, s, opt_flags);
2479 arg_end = p;
2480 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002481 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002482 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002483 }
2484 }
2485
2486 /*
2487 * ":let @r = expr": Set register contents.
2488 */
2489 else if (*arg == '@')
2490 {
2491 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002492 if (op != NULL && (*op == '+' || *op == '-'))
2493 EMSG2(_(e_letwrong), op);
2494 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002495 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002496 EMSG(_(e_letunexp));
2497 else
2498 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002499 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 char_u *s;
2501
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002502 p = get_tv_string_chk(tv);
2503 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002504 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002505 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002506 if (s != NULL)
2507 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002508 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002509 vim_free(s);
2510 }
2511 }
2512 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002513 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002514 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002515 arg_end = arg + 1;
2516 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002517 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002518 }
2519 }
2520
2521 /*
2522 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002523 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002525 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002526 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002527 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002528
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002529 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002530 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002531 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002532 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2533 EMSG(_(e_letunexp));
2534 else
2535 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002536 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002537 arg_end = p;
2538 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002539 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002540 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002541 }
2542
2543 else
2544 EMSG2(_(e_invarg2), arg);
2545
2546 return arg_end;
2547}
2548
2549/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2551 */
2552 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002553check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002554{
2555 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2556 {
2557 EMSG2(_(e_readonlyvar), arg);
2558 return TRUE;
2559 }
2560 return FALSE;
2561}
2562
2563/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002564 * Get an lval: variable, Dict item or List item that can be assigned a value
2565 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2566 * "name.key", "name.key[expr]" etc.
2567 * Indexing only works if "name" is an existing List or Dictionary.
2568 * "name" points to the start of the name.
2569 * If "rettv" is not NULL it points to the value to be assigned.
2570 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2571 * wrong; must end in space or cmd separator.
2572 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002573 * flags:
2574 * GLV_QUIET: do not give error messages
2575 * GLV_NO_AUTOLOAD: do not use script autoloading
2576 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002577 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002578 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002579 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002580 */
2581 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002582get_lval(
2583 char_u *name,
2584 typval_T *rettv,
2585 lval_T *lp,
2586 int unlet,
2587 int skip,
2588 int flags, /* GLV_ values */
2589 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002590{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002591 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002592 char_u *expr_start, *expr_end;
2593 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 dictitem_T *v;
2595 typval_T var1;
2596 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002597 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002598 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002599 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002600 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002601 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002602 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002603
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002604 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002605 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002606
2607 if (skip)
2608 {
2609 /* When skipping just find the end of the name. */
2610 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002611 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 }
2613
2614 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002615 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002616 if (expr_start != NULL)
2617 {
2618 /* Don't expand the name when we already know there is an error. */
2619 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2620 && *p != '[' && *p != '.')
2621 {
2622 EMSG(_(e_trailing));
2623 return NULL;
2624 }
2625
2626 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2627 if (lp->ll_exp_name == NULL)
2628 {
2629 /* Report an invalid expression in braces, unless the
2630 * expression evaluation has been cancelled due to an
2631 * aborting error, an interrupt, or an exception. */
2632 if (!aborting() && !quiet)
2633 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002634 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002635 EMSG2(_(e_invarg2), name);
2636 return NULL;
2637 }
2638 }
2639 lp->ll_name = lp->ll_exp_name;
2640 }
2641 else
2642 lp->ll_name = name;
2643
2644 /* Without [idx] or .key we are done. */
2645 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2646 return p;
2647
2648 cc = *p;
2649 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002650 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002651 if (v == NULL && !quiet)
2652 EMSG2(_(e_undefvar), lp->ll_name);
2653 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002654 if (v == NULL)
2655 return NULL;
2656
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 /*
2658 * Loop until no more [idx] or .key is following.
2659 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002660 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002661 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2664 && !(lp->ll_tv->v_type == VAR_DICT
2665 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (!quiet)
2668 EMSG(_("E689: Can only index a List or Dictionary"));
2669 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002670 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002671 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002673 if (!quiet)
2674 EMSG(_("E708: [:] must come last"));
2675 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002676 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002677
Bram Moolenaar8c711452005-01-14 21:53:12 +00002678 len = -1;
2679 if (*p == '.')
2680 {
2681 key = p + 1;
2682 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2683 ;
2684 if (len == 0)
2685 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002686 if (!quiet)
2687 EMSG(_(e_emptykey));
2688 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002689 }
2690 p = key + len;
2691 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002692 else
2693 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002694 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002695 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 if (*p == ':')
2697 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002698 else
2699 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002700 empty1 = FALSE;
2701 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002702 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002703 if (get_tv_string_chk(&var1) == NULL)
2704 {
2705 /* not a number or string */
2706 clear_tv(&var1);
2707 return NULL;
2708 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002709 }
2710
2711 /* Optionally get the second index [ :expr]. */
2712 if (*p == ':')
2713 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002714 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002715 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002717 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002718 if (!empty1)
2719 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002720 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002721 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002722 if (rettv != NULL && (rettv->v_type != VAR_LIST
2723 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002724 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 if (!quiet)
2726 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002727 if (!empty1)
2728 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 }
2731 p = skipwhite(p + 1);
2732 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002733 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002734 else
2735 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002736 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002737 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2738 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002739 if (!empty1)
2740 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002741 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002742 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002743 if (get_tv_string_chk(&var2) == NULL)
2744 {
2745 /* not a number or string */
2746 if (!empty1)
2747 clear_tv(&var1);
2748 clear_tv(&var2);
2749 return NULL;
2750 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002751 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002752 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002753 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002754 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002756
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002758 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (!quiet)
2760 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002761 if (!empty1)
2762 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002763 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002764 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002765 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002766 }
2767
2768 /* Skip to past ']'. */
2769 ++p;
2770 }
2771
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002773 {
2774 if (len == -1)
2775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002777 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 if (*key == NUL)
2779 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002780 if (!quiet)
2781 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002782 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002783 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002784 }
2785 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002786 lp->ll_list = NULL;
2787 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002788 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002789
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002790 /* When assigning to a scope dictionary check that a function and
2791 * variable name is valid (only variable name unless it is l: or
2792 * g: dictionary). Disallow overwriting a builtin function. */
2793 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002794 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002795 int prevval;
2796 int wrong;
2797
2798 if (len != -1)
2799 {
2800 prevval = key[len];
2801 key[len] = NUL;
2802 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002803 else
2804 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002805 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2806 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002807 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002808 || !valid_varname(key);
2809 if (len != -1)
2810 key[len] = prevval;
2811 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002812 return NULL;
2813 }
2814
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002815 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002816 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002817 /* Can't add "v:" variable. */
2818 if (lp->ll_dict == &vimvardict)
2819 {
2820 EMSG2(_(e_illvar), name);
2821 return NULL;
2822 }
2823
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002824 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002825 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002826 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002828 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002829 if (len == -1)
2830 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002831 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002832 }
2833 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002834 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002835 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002836 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002837 if (len == -1)
2838 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002839 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002840 p = NULL;
2841 break;
2842 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002843 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002844 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002845 return NULL;
2846
Bram Moolenaar8c711452005-01-14 21:53:12 +00002847 if (len == -1)
2848 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002849 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002850 }
2851 else
2852 {
2853 /*
2854 * Get the number and item for the only or first index of the List.
2855 */
2856 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002857 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002858 else
2859 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002860 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002861 clear_tv(&var1);
2862 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002863 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002864 lp->ll_list = lp->ll_tv->vval.v_list;
2865 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2866 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002867 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002868 if (lp->ll_n1 < 0)
2869 {
2870 lp->ll_n1 = 0;
2871 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2872 }
2873 }
2874 if (lp->ll_li == NULL)
2875 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002878 if (!quiet)
2879 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002880 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002881 }
2882
2883 /*
2884 * May need to find the item or absolute index for the second
2885 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002886 * When no index given: "lp->ll_empty2" is TRUE.
2887 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002891 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002893 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002894 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002895 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002896 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 {
2898 if (!quiet)
2899 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002900 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002901 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002902 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002903 }
2904
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002905 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2906 if (lp->ll_n1 < 0)
2907 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2908 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 {
2910 if (!quiet)
2911 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002913 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002917 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002918 }
2919
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002920 return p;
2921}
2922
2923/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002924 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002925 */
2926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002927clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002928{
2929 vim_free(lp->ll_exp_name);
2930 vim_free(lp->ll_newkey);
2931}
2932
2933/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002934 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002935 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002936 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002937 */
2938 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002939set_var_lval(
2940 lval_T *lp,
2941 char_u *endp,
2942 typval_T *rettv,
2943 int copy,
2944 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945{
2946 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002947 listitem_T *ri;
2948 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002949
2950 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002951 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002952 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002953 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002954 cc = *endp;
2955 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002956 if (op != NULL && *op != '=')
2957 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002958 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002959
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002960 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002961 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002962 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002963 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002964 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002965 if ((di == NULL
2966 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2967 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2968 FALSE)))
2969 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002970 set_var(lp->ll_name, &tv, FALSE);
2971 clear_tv(&tv);
2972 }
2973 }
2974 else
2975 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002976 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002977 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002978 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002979 else if (tv_check_lock(lp->ll_newkey == NULL
2980 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002981 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002982 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002983 else if (lp->ll_range)
2984 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002985 listitem_T *ll_li = lp->ll_li;
2986 int ll_n1 = lp->ll_n1;
2987
2988 /*
2989 * Check whether any of the list items is locked
2990 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002991 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002992 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002993 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002994 return;
2995 ri = ri->li_next;
2996 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2997 break;
2998 ll_li = ll_li->li_next;
2999 ++ll_n1;
3000 }
3001
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003002 /*
3003 * Assign the List values to the list items.
3004 */
3005 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003006 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003007 if (op != NULL && *op != '=')
3008 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3009 else
3010 {
3011 clear_tv(&lp->ll_li->li_tv);
3012 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3013 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003014 ri = ri->li_next;
3015 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3016 break;
3017 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003018 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003019 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003020 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 ri = NULL;
3023 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003024 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003025 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003026 lp->ll_li = lp->ll_li->li_next;
3027 ++lp->ll_n1;
3028 }
3029 if (ri != NULL)
3030 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003031 else if (lp->ll_empty2
3032 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003033 : lp->ll_n1 != lp->ll_n2)
3034 EMSG(_("E711: List value has not enough items"));
3035 }
3036 else
3037 {
3038 /*
3039 * Assign to a List or Dictionary item.
3040 */
3041 if (lp->ll_newkey != NULL)
3042 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003043 if (op != NULL && *op != '=')
3044 {
3045 EMSG2(_(e_letwrong), op);
3046 return;
3047 }
3048
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003049 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003050 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003051 if (di == NULL)
3052 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003053 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3054 {
3055 vim_free(di);
3056 return;
3057 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003058 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003059 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003060 else if (op != NULL && *op != '=')
3061 {
3062 tv_op(lp->ll_tv, rettv, op);
3063 return;
3064 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003065 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003066 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003067
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003068 /*
3069 * Assign the value to the variable or list item.
3070 */
3071 if (copy)
3072 copy_tv(rettv, lp->ll_tv);
3073 else
3074 {
3075 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003076 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003077 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003078 }
3079 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003080}
3081
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003082/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003083 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3084 * Returns OK or FAIL.
3085 */
3086 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003087tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003088{
3089 long n;
3090 char_u numbuf[NUMBUFLEN];
3091 char_u *s;
3092
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003093 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3094 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3095 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 {
3097 switch (tv1->v_type)
3098 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003099 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003100 case VAR_DICT:
3101 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003102 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003103 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003104 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003105 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003106 break;
3107
3108 case VAR_LIST:
3109 if (*op != '+' || tv2->v_type != VAR_LIST)
3110 break;
3111 /* List += List */
3112 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3113 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3114 return OK;
3115
3116 case VAR_NUMBER:
3117 case VAR_STRING:
3118 if (tv2->v_type == VAR_LIST)
3119 break;
3120 if (*op == '+' || *op == '-')
3121 {
3122 /* nr += nr or nr -= nr*/
3123 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003124#ifdef FEAT_FLOAT
3125 if (tv2->v_type == VAR_FLOAT)
3126 {
3127 float_T f = n;
3128
3129 if (*op == '+')
3130 f += tv2->vval.v_float;
3131 else
3132 f -= tv2->vval.v_float;
3133 clear_tv(tv1);
3134 tv1->v_type = VAR_FLOAT;
3135 tv1->vval.v_float = f;
3136 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003137 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003138#endif
3139 {
3140 if (*op == '+')
3141 n += get_tv_number(tv2);
3142 else
3143 n -= get_tv_number(tv2);
3144 clear_tv(tv1);
3145 tv1->v_type = VAR_NUMBER;
3146 tv1->vval.v_number = n;
3147 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003148 }
3149 else
3150 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003151 if (tv2->v_type == VAR_FLOAT)
3152 break;
3153
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003154 /* str .= str */
3155 s = get_tv_string(tv1);
3156 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3157 clear_tv(tv1);
3158 tv1->v_type = VAR_STRING;
3159 tv1->vval.v_string = s;
3160 }
3161 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003162
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003163 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003164#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003165 {
3166 float_T f;
3167
3168 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3169 && tv2->v_type != VAR_NUMBER
3170 && tv2->v_type != VAR_STRING))
3171 break;
3172 if (tv2->v_type == VAR_FLOAT)
3173 f = tv2->vval.v_float;
3174 else
3175 f = get_tv_number(tv2);
3176 if (*op == '+')
3177 tv1->vval.v_float += f;
3178 else
3179 tv1->vval.v_float -= f;
3180 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003181#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003182 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003183 }
3184 }
3185
3186 EMSG2(_(e_letwrong), op);
3187 return FAIL;
3188}
3189
3190/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191 * Add a watcher to a list.
3192 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003193 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003194list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003195{
3196 lw->lw_next = l->lv_watch;
3197 l->lv_watch = lw;
3198}
3199
3200/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003201 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202 * No warning when it isn't found...
3203 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003204 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003205list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003206{
Bram Moolenaar33570922005-01-25 22:26:29 +00003207 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003208
3209 lwp = &l->lv_watch;
3210 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3211 {
3212 if (lw == lwrem)
3213 {
3214 *lwp = lw->lw_next;
3215 break;
3216 }
3217 lwp = &lw->lw_next;
3218 }
3219}
3220
3221/*
3222 * Just before removing an item from a list: advance watchers to the next
3223 * item.
3224 */
3225 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003226list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003227{
Bram Moolenaar33570922005-01-25 22:26:29 +00003228 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003229
3230 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3231 if (lw->lw_item == item)
3232 lw->lw_item = item->li_next;
3233}
3234
3235/*
3236 * Evaluate the expression used in a ":for var in expr" command.
3237 * "arg" points to "var".
3238 * Set "*errp" to TRUE for an error, FALSE otherwise;
3239 * Return a pointer that holds the info. Null when there is an error.
3240 */
3241 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003242eval_for_line(
3243 char_u *arg,
3244 int *errp,
3245 char_u **nextcmdp,
3246 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003247{
Bram Moolenaar33570922005-01-25 22:26:29 +00003248 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003249 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003250 typval_T tv;
3251 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252
3253 *errp = TRUE; /* default: there is an error */
3254
Bram Moolenaar33570922005-01-25 22:26:29 +00003255 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003256 if (fi == NULL)
3257 return NULL;
3258
3259 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3260 if (expr == NULL)
3261 return fi;
3262
3263 expr = skipwhite(expr);
3264 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3265 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003266 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003267 return fi;
3268 }
3269
3270 if (skip)
3271 ++emsg_skip;
3272 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3273 {
3274 *errp = FALSE;
3275 if (!skip)
3276 {
3277 l = tv.vval.v_list;
3278 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003279 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003280 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003281 clear_tv(&tv);
3282 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 else
3284 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003285 /* No need to increment the refcount, it's already set for the
3286 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003287 fi->fi_list = l;
3288 list_add_watch(l, &fi->fi_lw);
3289 fi->fi_lw.lw_item = l->lv_first;
3290 }
3291 }
3292 }
3293 if (skip)
3294 --emsg_skip;
3295
3296 return fi;
3297}
3298
3299/*
3300 * Use the first item in a ":for" list. Advance to the next.
3301 * Assign the values to the variable (list). "arg" points to the first one.
3302 * Return TRUE when a valid item was found, FALSE when at end of list or
3303 * something wrong.
3304 */
3305 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003306next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003308 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003309 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003310 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003311
3312 item = fi->fi_lw.lw_item;
3313 if (item == NULL)
3314 result = FALSE;
3315 else
3316 {
3317 fi->fi_lw.lw_item = item->li_next;
3318 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3319 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3320 }
3321 return result;
3322}
3323
3324/*
3325 * Free the structure used to store info used by ":for".
3326 */
3327 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003328free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003329{
Bram Moolenaar33570922005-01-25 22:26:29 +00003330 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003331
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003332 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003333 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003334 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003335 list_unref(fi->fi_list);
3336 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003337 vim_free(fi);
3338}
3339
Bram Moolenaar071d4272004-06-13 20:20:40 +00003340#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3341
3342 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003343set_context_for_expression(
3344 expand_T *xp,
3345 char_u *arg,
3346 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347{
3348 int got_eq = FALSE;
3349 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003350 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003351
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 if (cmdidx == CMD_let)
3353 {
3354 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003355 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003356 {
3357 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003358 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003359 {
3360 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003361 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003362 if (vim_iswhite(*p))
3363 break;
3364 }
3365 return;
3366 }
3367 }
3368 else
3369 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3370 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003371 while ((xp->xp_pattern = vim_strpbrk(arg,
3372 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3373 {
3374 c = *xp->xp_pattern;
3375 if (c == '&')
3376 {
3377 c = xp->xp_pattern[1];
3378 if (c == '&')
3379 {
3380 ++xp->xp_pattern;
3381 xp->xp_context = cmdidx != CMD_let || got_eq
3382 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3383 }
3384 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003385 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003386 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003387 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3388 xp->xp_pattern += 2;
3389
3390 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003391 }
3392 else if (c == '$')
3393 {
3394 /* environment variable */
3395 xp->xp_context = EXPAND_ENV_VARS;
3396 }
3397 else if (c == '=')
3398 {
3399 got_eq = TRUE;
3400 xp->xp_context = EXPAND_EXPRESSION;
3401 }
Bram Moolenaara32095f2016-03-28 19:27:13 +02003402 else if (c == '#'
3403 && xp->xp_context == EXPAND_EXPRESSION)
3404 {
3405 /* Autoload function/variable contains '#'. */
3406 break;
3407 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003408 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003409 && xp->xp_context == EXPAND_FUNCTIONS
3410 && vim_strchr(xp->xp_pattern, '(') == NULL)
3411 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003412 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003413 break;
3414 }
3415 else if (cmdidx != CMD_let || got_eq)
3416 {
3417 if (c == '"') /* string */
3418 {
3419 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3420 if (c == '\\' && xp->xp_pattern[1] != NUL)
3421 ++xp->xp_pattern;
3422 xp->xp_context = EXPAND_NOTHING;
3423 }
3424 else if (c == '\'') /* literal string */
3425 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003426 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003427 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3428 /* skip */ ;
3429 xp->xp_context = EXPAND_NOTHING;
3430 }
3431 else if (c == '|')
3432 {
3433 if (xp->xp_pattern[1] == '|')
3434 {
3435 ++xp->xp_pattern;
3436 xp->xp_context = EXPAND_EXPRESSION;
3437 }
3438 else
3439 xp->xp_context = EXPAND_COMMANDS;
3440 }
3441 else
3442 xp->xp_context = EXPAND_EXPRESSION;
3443 }
3444 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003445 /* Doesn't look like something valid, expand as an expression
3446 * anyway. */
3447 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003448 arg = xp->xp_pattern;
3449 if (*arg != NUL)
3450 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3451 /* skip */ ;
3452 }
3453 xp->xp_pattern = arg;
3454}
3455
3456#endif /* FEAT_CMDL_COMPL */
3457
3458/*
3459 * ":1,25call func(arg1, arg2)" function call.
3460 */
3461 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003462ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463{
3464 char_u *arg = eap->arg;
3465 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003466 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003467 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003469 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003470 linenr_T lnum;
3471 int doesrange;
3472 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003473 funcdict_T fudi;
Bram Moolenaar9e63f612016-03-17 23:13:28 +01003474 partial_T *partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003475
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003476 if (eap->skip)
3477 {
3478 /* trans_function_name() doesn't work well when skipping, use eval0()
3479 * instead to skip to any following command, e.g. for:
3480 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003481 ++emsg_skip;
3482 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3483 clear_tv(&rettv);
3484 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003485 return;
3486 }
3487
Bram Moolenaar65639032016-03-16 21:40:30 +01003488 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003489 if (fudi.fd_newkey != NULL)
3490 {
3491 /* Still need to give an error message for missing key. */
3492 EMSG2(_(e_dictkey), fudi.fd_newkey);
3493 vim_free(fudi.fd_newkey);
3494 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003495 if (tofree == NULL)
3496 return;
3497
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003498 /* Increase refcount on dictionary, it could get deleted when evaluating
3499 * the arguments. */
3500 if (fudi.fd_dict != NULL)
3501 ++fudi.fd_dict->dv_refcount;
3502
Bram Moolenaar65639032016-03-16 21:40:30 +01003503 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3504 * contents. For VAR_PARTIAL get its partial, unless we already have one
3505 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003506 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003507 name = deref_func_name(tofree, &len,
3508 partial != NULL ? NULL : &partial, FALSE);
3509
Bram Moolenaar532c7802005-01-27 14:44:31 +00003510 /* Skip white space to allow ":call func ()". Not good, but required for
3511 * backward compatibility. */
3512 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003513 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514
3515 if (*startarg != '(')
3516 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003517 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003518 goto end;
3519 }
3520
3521 /*
3522 * When skipping, evaluate the function once, to find the end of the
3523 * arguments.
3524 * When the function takes a range, this is discovered after the first
3525 * call, and the loop is broken.
3526 */
3527 if (eap->skip)
3528 {
3529 ++emsg_skip;
3530 lnum = eap->line2; /* do it once, also with an invalid range */
3531 }
3532 else
3533 lnum = eap->line1;
3534 for ( ; lnum <= eap->line2; ++lnum)
3535 {
3536 if (!eap->skip && eap->addr_count > 0)
3537 {
3538 curwin->w_cursor.lnum = lnum;
3539 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003540#ifdef FEAT_VIRTUALEDIT
3541 curwin->w_cursor.coladd = 0;
3542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003543 }
3544 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003545 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003546 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003547 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003548 {
3549 failed = TRUE;
3550 break;
3551 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003552
3553 /* Handle a function returning a Funcref, Dictionary or List. */
3554 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3555 {
3556 failed = TRUE;
3557 break;
3558 }
3559
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003560 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003561 if (doesrange || eap->skip)
3562 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003563
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003565 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003566 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003567 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003568 if (aborting())
3569 break;
3570 }
3571 if (eap->skip)
3572 --emsg_skip;
3573
3574 if (!failed)
3575 {
3576 /* Check for trailing illegal characters and a following command. */
3577 if (!ends_excmd(*arg))
3578 {
3579 emsg_severe = TRUE;
3580 EMSG(_(e_trailing));
3581 }
3582 else
3583 eap->nextcmd = check_nextcmd(arg);
3584 }
3585
3586end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003587 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003588 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003589}
3590
3591/*
3592 * ":unlet[!] var1 ... " command.
3593 */
3594 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003595ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003596{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003597 ex_unletlock(eap, eap->arg, 0);
3598}
3599
3600/*
3601 * ":lockvar" and ":unlockvar" commands
3602 */
3603 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003604ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003605{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003606 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003607 int deep = 2;
3608
3609 if (eap->forceit)
3610 deep = -1;
3611 else if (vim_isdigit(*arg))
3612 {
3613 deep = getdigits(&arg);
3614 arg = skipwhite(arg);
3615 }
3616
3617 ex_unletlock(eap, arg, deep);
3618}
3619
3620/*
3621 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3622 */
3623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003624ex_unletlock(
3625 exarg_T *eap,
3626 char_u *argstart,
3627 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628{
3629 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003630 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003631 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003632 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003633
3634 do
3635 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003636 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003637 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003638 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003639 if (lv.ll_name == NULL)
3640 error = TRUE; /* error but continue parsing */
3641 if (name_end == NULL || (!vim_iswhite(*name_end)
3642 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003643 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003644 if (name_end != NULL)
3645 {
3646 emsg_severe = TRUE;
3647 EMSG(_(e_trailing));
3648 }
3649 if (!(eap->skip || error))
3650 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003651 break;
3652 }
3653
3654 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003655 {
3656 if (eap->cmdidx == CMD_unlet)
3657 {
3658 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3659 error = TRUE;
3660 }
3661 else
3662 {
3663 if (do_lock_var(&lv, name_end, deep,
3664 eap->cmdidx == CMD_lockvar) == FAIL)
3665 error = TRUE;
3666 }
3667 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003669 if (!eap->skip)
3670 clear_lval(&lv);
3671
Bram Moolenaar071d4272004-06-13 20:20:40 +00003672 arg = skipwhite(name_end);
3673 } while (!ends_excmd(*arg));
3674
3675 eap->nextcmd = check_nextcmd(arg);
3676}
3677
Bram Moolenaar8c711452005-01-14 21:53:12 +00003678 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003679do_unlet_var(
3680 lval_T *lp,
3681 char_u *name_end,
3682 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003683{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003684 int ret = OK;
3685 int cc;
3686
3687 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003688 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003689 cc = *name_end;
3690 *name_end = NUL;
3691
3692 /* Normal name or expanded name. */
3693 if (check_changedtick(lp->ll_name))
3694 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003695 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003696 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003697 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003698 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003699 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003700 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003701 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003702 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003703 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003704 else if (lp->ll_range)
3705 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003706 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003707 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003708 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003709
3710 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3711 {
3712 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003713 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003714 return FAIL;
3715 ll_li = li;
3716 ++ll_n1;
3717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003718
3719 /* Delete a range of List items. */
3720 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3721 {
3722 li = lp->ll_li->li_next;
3723 listitem_remove(lp->ll_list, lp->ll_li);
3724 lp->ll_li = li;
3725 ++lp->ll_n1;
3726 }
3727 }
3728 else
3729 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003731 /* unlet a List item. */
3732 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003733 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003734 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003735 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003736 }
3737
3738 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003739}
3740
Bram Moolenaar071d4272004-06-13 20:20:40 +00003741/*
3742 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003743 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003744 */
3745 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003746do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003747{
Bram Moolenaar33570922005-01-25 22:26:29 +00003748 hashtab_T *ht;
3749 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003750 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003751 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003752 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003753
Bram Moolenaar33570922005-01-25 22:26:29 +00003754 ht = find_var_ht(name, &varname);
3755 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003756 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003757 if (ht == &globvarht)
3758 d = &globvardict;
3759 else if (current_funccal != NULL
3760 && ht == &current_funccal->l_vars.dv_hashtab)
3761 d = &current_funccal->l_vars;
3762 else if (ht == &compat_hashtab)
3763 d = &vimvardict;
3764 else
3765 {
3766 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3767 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3768 }
3769 if (d == NULL)
3770 {
3771 EMSG2(_(e_intern2), "do_unlet()");
3772 return FAIL;
3773 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003774 hi = hash_find(ht, varname);
3775 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003776 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003777 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003778 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003779 || var_check_ro(di->di_flags, name, FALSE)
3780 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003781 return FAIL;
3782
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783 delete_var(ht, hi);
3784 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003785 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003787 if (forceit)
3788 return OK;
3789 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003790 return FAIL;
3791}
3792
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003793/*
3794 * Lock or unlock variable indicated by "lp".
3795 * "deep" is the levels to go (-1 for unlimited);
3796 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3797 */
3798 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003799do_lock_var(
3800 lval_T *lp,
3801 char_u *name_end,
3802 int deep,
3803 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003804{
3805 int ret = OK;
3806 int cc;
3807 dictitem_T *di;
3808
3809 if (deep == 0) /* nothing to do */
3810 return OK;
3811
3812 if (lp->ll_tv == NULL)
3813 {
3814 cc = *name_end;
3815 *name_end = NUL;
3816
3817 /* Normal name or expanded name. */
3818 if (check_changedtick(lp->ll_name))
3819 ret = FAIL;
3820 else
3821 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003822 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003823 if (di == NULL)
3824 ret = FAIL;
3825 else
3826 {
3827 if (lock)
3828 di->di_flags |= DI_FLAGS_LOCK;
3829 else
3830 di->di_flags &= ~DI_FLAGS_LOCK;
3831 item_lock(&di->di_tv, deep, lock);
3832 }
3833 }
3834 *name_end = cc;
3835 }
3836 else if (lp->ll_range)
3837 {
3838 listitem_T *li = lp->ll_li;
3839
3840 /* (un)lock a range of List items. */
3841 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3842 {
3843 item_lock(&li->li_tv, deep, lock);
3844 li = li->li_next;
3845 ++lp->ll_n1;
3846 }
3847 }
3848 else if (lp->ll_list != NULL)
3849 /* (un)lock a List item. */
3850 item_lock(&lp->ll_li->li_tv, deep, lock);
3851 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003852 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003853 item_lock(&lp->ll_di->di_tv, deep, lock);
3854
3855 return ret;
3856}
3857
3858/*
3859 * Lock or unlock an item. "deep" is nr of levels to go.
3860 */
3861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003862item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003863{
3864 static int recurse = 0;
3865 list_T *l;
3866 listitem_T *li;
3867 dict_T *d;
3868 hashitem_T *hi;
3869 int todo;
3870
3871 if (recurse >= DICT_MAXNEST)
3872 {
3873 EMSG(_("E743: variable nested too deep for (un)lock"));
3874 return;
3875 }
3876 if (deep == 0)
3877 return;
3878 ++recurse;
3879
3880 /* lock/unlock the item itself */
3881 if (lock)
3882 tv->v_lock |= VAR_LOCKED;
3883 else
3884 tv->v_lock &= ~VAR_LOCKED;
3885
3886 switch (tv->v_type)
3887 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003888 case VAR_UNKNOWN:
3889 case VAR_NUMBER:
3890 case VAR_STRING:
3891 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003892 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003893 case VAR_FLOAT:
3894 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003895 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003896 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003897 break;
3898
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003899 case VAR_LIST:
3900 if ((l = tv->vval.v_list) != NULL)
3901 {
3902 if (lock)
3903 l->lv_lock |= VAR_LOCKED;
3904 else
3905 l->lv_lock &= ~VAR_LOCKED;
3906 if (deep < 0 || deep > 1)
3907 /* recursive: lock/unlock the items the List contains */
3908 for (li = l->lv_first; li != NULL; li = li->li_next)
3909 item_lock(&li->li_tv, deep - 1, lock);
3910 }
3911 break;
3912 case VAR_DICT:
3913 if ((d = tv->vval.v_dict) != NULL)
3914 {
3915 if (lock)
3916 d->dv_lock |= VAR_LOCKED;
3917 else
3918 d->dv_lock &= ~VAR_LOCKED;
3919 if (deep < 0 || deep > 1)
3920 {
3921 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003922 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003923 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3924 {
3925 if (!HASHITEM_EMPTY(hi))
3926 {
3927 --todo;
3928 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3929 }
3930 }
3931 }
3932 }
3933 }
3934 --recurse;
3935}
3936
Bram Moolenaara40058a2005-07-11 22:42:07 +00003937/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003938 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3939 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003940 */
3941 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003942tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003943{
3944 return (tv->v_lock & VAR_LOCKED)
3945 || (tv->v_type == VAR_LIST
3946 && tv->vval.v_list != NULL
3947 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3948 || (tv->v_type == VAR_DICT
3949 && tv->vval.v_dict != NULL
3950 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3951}
3952
Bram Moolenaar071d4272004-06-13 20:20:40 +00003953#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3954/*
3955 * Delete all "menutrans_" variables.
3956 */
3957 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003958del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003959{
Bram Moolenaar33570922005-01-25 22:26:29 +00003960 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003961 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003962
Bram Moolenaar33570922005-01-25 22:26:29 +00003963 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003964 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003965 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003966 {
3967 if (!HASHITEM_EMPTY(hi))
3968 {
3969 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3971 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003972 }
3973 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003974 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003975}
3976#endif
3977
3978#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3979
3980/*
3981 * Local string buffer for the next two functions to store a variable name
3982 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3983 * get_user_var_name().
3984 */
3985
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003986static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003987
3988static char_u *varnamebuf = NULL;
3989static int varnamebuflen = 0;
3990
3991/*
3992 * Function to concatenate a prefix and a variable name.
3993 */
3994 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003995cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003996{
3997 int len;
3998
3999 len = (int)STRLEN(name) + 3;
4000 if (len > varnamebuflen)
4001 {
4002 vim_free(varnamebuf);
4003 len += 10; /* some additional space */
4004 varnamebuf = alloc(len);
4005 if (varnamebuf == NULL)
4006 {
4007 varnamebuflen = 0;
4008 return NULL;
4009 }
4010 varnamebuflen = len;
4011 }
4012 *varnamebuf = prefix;
4013 varnamebuf[1] = ':';
4014 STRCPY(varnamebuf + 2, name);
4015 return varnamebuf;
4016}
4017
4018/*
4019 * Function given to ExpandGeneric() to obtain the list of user defined
4020 * (global/buffer/window/built-in) variable names.
4021 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004022 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004023get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004024{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004025 static long_u gdone;
4026 static long_u bdone;
4027 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004028#ifdef FEAT_WINDOWS
4029 static long_u tdone;
4030#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004031 static int vidx;
4032 static hashitem_T *hi;
4033 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004034
4035 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004036 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004037 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004038#ifdef FEAT_WINDOWS
4039 tdone = 0;
4040#endif
4041 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004042
4043 /* Global variables */
4044 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004045 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004047 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004048 else
4049 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004050 while (HASHITEM_EMPTY(hi))
4051 ++hi;
4052 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4053 return cat_prefix_varname('g', hi->hi_key);
4054 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004055 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004056
4057 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004058 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004059 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004060 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004062 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004063 else
4064 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004065 while (HASHITEM_EMPTY(hi))
4066 ++hi;
4067 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004069 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004070 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004071 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004072 return (char_u *)"b:changedtick";
4073 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004074
4075 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004076 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004077 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004079 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004080 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004081 else
4082 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004083 while (HASHITEM_EMPTY(hi))
4084 ++hi;
4085 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004086 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004087
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004088#ifdef FEAT_WINDOWS
4089 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004090 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004091 if (tdone < ht->ht_used)
4092 {
4093 if (tdone++ == 0)
4094 hi = ht->ht_array;
4095 else
4096 ++hi;
4097 while (HASHITEM_EMPTY(hi))
4098 ++hi;
4099 return cat_prefix_varname('t', hi->hi_key);
4100 }
4101#endif
4102
Bram Moolenaar33570922005-01-25 22:26:29 +00004103 /* v: variables */
4104 if (vidx < VV_LEN)
4105 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004106
4107 vim_free(varnamebuf);
4108 varnamebuf = NULL;
4109 varnamebuflen = 0;
4110 return NULL;
4111}
4112
4113#endif /* FEAT_CMDL_COMPL */
4114
4115/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004116 * Return TRUE if "pat" matches "text".
4117 * Does not use 'cpo' and always uses 'magic'.
4118 */
4119 static int
4120pattern_match(char_u *pat, char_u *text, int ic)
4121{
4122 int matches = FALSE;
4123 char_u *save_cpo;
4124 regmatch_T regmatch;
4125
4126 /* avoid 'l' flag in 'cpoptions' */
4127 save_cpo = p_cpo;
4128 p_cpo = (char_u *)"";
4129 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
4130 if (regmatch.regprog != NULL)
4131 {
4132 regmatch.rm_ic = ic;
4133 matches = vim_regexec_nl(&regmatch, text, (colnr_T)0);
4134 vim_regfree(regmatch.regprog);
4135 }
4136 p_cpo = save_cpo;
4137 return matches;
4138}
4139
4140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00004141 * types for expressions.
4142 */
4143typedef enum
4144{
4145 TYPE_UNKNOWN = 0
4146 , TYPE_EQUAL /* == */
4147 , TYPE_NEQUAL /* != */
4148 , TYPE_GREATER /* > */
4149 , TYPE_GEQUAL /* >= */
4150 , TYPE_SMALLER /* < */
4151 , TYPE_SEQUAL /* <= */
4152 , TYPE_MATCH /* =~ */
4153 , TYPE_NOMATCH /* !~ */
4154} exptype_T;
4155
4156/*
4157 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004158 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004159 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4160 */
4161
4162/*
4163 * Handle zero level expression.
4164 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004165 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004166 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004167 * Return OK or FAIL.
4168 */
4169 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004170eval0(
4171 char_u *arg,
4172 typval_T *rettv,
4173 char_u **nextcmd,
4174 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004175{
4176 int ret;
4177 char_u *p;
4178
4179 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004180 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004181 if (ret == FAIL || !ends_excmd(*p))
4182 {
4183 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004184 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004185 /*
4186 * Report the invalid expression unless the expression evaluation has
4187 * been cancelled due to an aborting error, an interrupt, or an
4188 * exception.
4189 */
4190 if (!aborting())
4191 EMSG2(_(e_invexpr2), arg);
4192 ret = FAIL;
4193 }
4194 if (nextcmd != NULL)
4195 *nextcmd = check_nextcmd(p);
4196
4197 return ret;
4198}
4199
4200/*
4201 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004202 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004203 *
4204 * "arg" must point to the first non-white of the expression.
4205 * "arg" is advanced to the next non-white after the recognized expression.
4206 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004207 * Note: "rettv.v_lock" is not set.
4208 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004209 * Return OK or FAIL.
4210 */
4211 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004212eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213{
4214 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004215 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004216
4217 /*
4218 * Get the first variable.
4219 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004220 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004221 return FAIL;
4222
4223 if ((*arg)[0] == '?')
4224 {
4225 result = FALSE;
4226 if (evaluate)
4227 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004228 int error = FALSE;
4229
4230 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004231 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004232 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004233 if (error)
4234 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004235 }
4236
4237 /*
4238 * Get the second variable.
4239 */
4240 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004241 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004242 return FAIL;
4243
4244 /*
4245 * Check for the ":".
4246 */
4247 if ((*arg)[0] != ':')
4248 {
4249 EMSG(_("E109: Missing ':' after '?'"));
4250 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004251 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004252 return FAIL;
4253 }
4254
4255 /*
4256 * Get the third variable.
4257 */
4258 *arg = skipwhite(*arg + 1);
4259 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4260 {
4261 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004262 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004263 return FAIL;
4264 }
4265 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004266 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004267 }
4268
4269 return OK;
4270}
4271
4272/*
4273 * Handle first level expression:
4274 * expr2 || expr2 || expr2 logical OR
4275 *
4276 * "arg" must point to the first non-white of the expression.
4277 * "arg" is advanced to the next non-white after the recognized expression.
4278 *
4279 * Return OK or FAIL.
4280 */
4281 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004282eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004283{
Bram Moolenaar33570922005-01-25 22:26:29 +00004284 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004285 long result;
4286 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004287 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004288
4289 /*
4290 * Get the first variable.
4291 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004292 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004293 return FAIL;
4294
4295 /*
4296 * Repeat until there is no following "||".
4297 */
4298 first = TRUE;
4299 result = FALSE;
4300 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4301 {
4302 if (evaluate && first)
4303 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004304 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004306 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004307 if (error)
4308 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004309 first = FALSE;
4310 }
4311
4312 /*
4313 * Get the second variable.
4314 */
4315 *arg = skipwhite(*arg + 2);
4316 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4317 return FAIL;
4318
4319 /*
4320 * Compute the result.
4321 */
4322 if (evaluate && !result)
4323 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004324 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004325 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004326 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004327 if (error)
4328 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004329 }
4330 if (evaluate)
4331 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004332 rettv->v_type = VAR_NUMBER;
4333 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004334 }
4335 }
4336
4337 return OK;
4338}
4339
4340/*
4341 * Handle second level expression:
4342 * expr3 && expr3 && expr3 logical AND
4343 *
4344 * "arg" must point to the first non-white of the expression.
4345 * "arg" is advanced to the next non-white after the recognized expression.
4346 *
4347 * Return OK or FAIL.
4348 */
4349 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004350eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004351{
Bram Moolenaar33570922005-01-25 22:26:29 +00004352 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004353 long result;
4354 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004355 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004356
4357 /*
4358 * Get the first variable.
4359 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004360 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004361 return FAIL;
4362
4363 /*
4364 * Repeat until there is no following "&&".
4365 */
4366 first = TRUE;
4367 result = TRUE;
4368 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4369 {
4370 if (evaluate && first)
4371 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004372 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004374 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004375 if (error)
4376 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004377 first = FALSE;
4378 }
4379
4380 /*
4381 * Get the second variable.
4382 */
4383 *arg = skipwhite(*arg + 2);
4384 if (eval4(arg, &var2, evaluate && result) == FAIL)
4385 return FAIL;
4386
4387 /*
4388 * Compute the result.
4389 */
4390 if (evaluate && result)
4391 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004392 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004393 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004394 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004395 if (error)
4396 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004397 }
4398 if (evaluate)
4399 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004400 rettv->v_type = VAR_NUMBER;
4401 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004402 }
4403 }
4404
4405 return OK;
4406}
4407
4408/*
4409 * Handle third level expression:
4410 * var1 == var2
4411 * var1 =~ var2
4412 * var1 != var2
4413 * var1 !~ var2
4414 * var1 > var2
4415 * var1 >= var2
4416 * var1 < var2
4417 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004418 * var1 is var2
4419 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004420 *
4421 * "arg" must point to the first non-white of the expression.
4422 * "arg" is advanced to the next non-white after the recognized expression.
4423 *
4424 * Return OK or FAIL.
4425 */
4426 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004427eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004428{
Bram Moolenaar33570922005-01-25 22:26:29 +00004429 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430 char_u *p;
4431 int i;
4432 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004433 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004434 int len = 2;
4435 long n1, n2;
4436 char_u *s1, *s2;
4437 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 int ic;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004439
4440 /*
4441 * Get the first variable.
4442 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004443 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004444 return FAIL;
4445
4446 p = *arg;
4447 switch (p[0])
4448 {
4449 case '=': if (p[1] == '=')
4450 type = TYPE_EQUAL;
4451 else if (p[1] == '~')
4452 type = TYPE_MATCH;
4453 break;
4454 case '!': if (p[1] == '=')
4455 type = TYPE_NEQUAL;
4456 else if (p[1] == '~')
4457 type = TYPE_NOMATCH;
4458 break;
4459 case '>': if (p[1] != '=')
4460 {
4461 type = TYPE_GREATER;
4462 len = 1;
4463 }
4464 else
4465 type = TYPE_GEQUAL;
4466 break;
4467 case '<': if (p[1] != '=')
4468 {
4469 type = TYPE_SMALLER;
4470 len = 1;
4471 }
4472 else
4473 type = TYPE_SEQUAL;
4474 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004475 case 'i': if (p[1] == 's')
4476 {
4477 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4478 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004479 i = p[len];
4480 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004481 {
4482 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4483 type_is = TRUE;
4484 }
4485 }
4486 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004487 }
4488
4489 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004490 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004491 */
4492 if (type != TYPE_UNKNOWN)
4493 {
4494 /* extra question mark appended: ignore case */
4495 if (p[len] == '?')
4496 {
4497 ic = TRUE;
4498 ++len;
4499 }
4500 /* extra '#' appended: match case */
4501 else if (p[len] == '#')
4502 {
4503 ic = FALSE;
4504 ++len;
4505 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004506 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004507 else
4508 ic = p_ic;
4509
4510 /*
4511 * Get the second variable.
4512 */
4513 *arg = skipwhite(p + len);
4514 if (eval5(arg, &var2, evaluate) == FAIL)
4515 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004516 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004517 return FAIL;
4518 }
4519
4520 if (evaluate)
4521 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004522 if (type_is && rettv->v_type != var2.v_type)
4523 {
4524 /* For "is" a different type always means FALSE, for "notis"
4525 * it means TRUE. */
4526 n1 = (type == TYPE_NEQUAL);
4527 }
4528 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4529 {
4530 if (type_is)
4531 {
4532 n1 = (rettv->v_type == var2.v_type
4533 && rettv->vval.v_list == var2.vval.v_list);
4534 if (type == TYPE_NEQUAL)
4535 n1 = !n1;
4536 }
4537 else if (rettv->v_type != var2.v_type
4538 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4539 {
4540 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004541 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004542 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004543 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004544 clear_tv(rettv);
4545 clear_tv(&var2);
4546 return FAIL;
4547 }
4548 else
4549 {
4550 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004551 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4552 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004553 if (type == TYPE_NEQUAL)
4554 n1 = !n1;
4555 }
4556 }
4557
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004558 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4559 {
4560 if (type_is)
4561 {
4562 n1 = (rettv->v_type == var2.v_type
4563 && rettv->vval.v_dict == var2.vval.v_dict);
4564 if (type == TYPE_NEQUAL)
4565 n1 = !n1;
4566 }
4567 else if (rettv->v_type != var2.v_type
4568 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4569 {
4570 if (rettv->v_type != var2.v_type)
4571 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4572 else
4573 EMSG(_("E736: Invalid operation for Dictionary"));
4574 clear_tv(rettv);
4575 clear_tv(&var2);
4576 return FAIL;
4577 }
4578 else
4579 {
4580 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004581 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4582 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004583 if (type == TYPE_NEQUAL)
4584 n1 = !n1;
4585 }
4586 }
4587
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004588 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4589 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004591 if (type != TYPE_EQUAL && type != TYPE_NEQUAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004592 {
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004593 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004594 clear_tv(rettv);
4595 clear_tv(&var2);
4596 return FAIL;
4597 }
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01004598 n1 = tv_equal(rettv, &var2, FALSE, FALSE);
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004599 if (type == TYPE_NEQUAL)
4600 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004601 }
4602
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004603#ifdef FEAT_FLOAT
4604 /*
4605 * If one of the two variables is a float, compare as a float.
4606 * When using "=~" or "!~", always compare as string.
4607 */
4608 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4609 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4610 {
4611 float_T f1, f2;
4612
4613 if (rettv->v_type == VAR_FLOAT)
4614 f1 = rettv->vval.v_float;
4615 else
4616 f1 = get_tv_number(rettv);
4617 if (var2.v_type == VAR_FLOAT)
4618 f2 = var2.vval.v_float;
4619 else
4620 f2 = get_tv_number(&var2);
4621 n1 = FALSE;
4622 switch (type)
4623 {
4624 case TYPE_EQUAL: n1 = (f1 == f2); break;
4625 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4626 case TYPE_GREATER: n1 = (f1 > f2); break;
4627 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4628 case TYPE_SMALLER: n1 = (f1 < f2); break;
4629 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4630 case TYPE_UNKNOWN:
4631 case TYPE_MATCH:
4632 case TYPE_NOMATCH: break; /* avoid gcc warning */
4633 }
4634 }
4635#endif
4636
Bram Moolenaar071d4272004-06-13 20:20:40 +00004637 /*
4638 * If one of the two variables is a number, compare as a number.
4639 * When using "=~" or "!~", always compare as string.
4640 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004641 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004642 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4643 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004644 n1 = get_tv_number(rettv);
4645 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004646 switch (type)
4647 {
4648 case TYPE_EQUAL: n1 = (n1 == n2); break;
4649 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4650 case TYPE_GREATER: n1 = (n1 > n2); break;
4651 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4652 case TYPE_SMALLER: n1 = (n1 < n2); break;
4653 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4654 case TYPE_UNKNOWN:
4655 case TYPE_MATCH:
4656 case TYPE_NOMATCH: break; /* avoid gcc warning */
4657 }
4658 }
4659 else
4660 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004661 s1 = get_tv_string_buf(rettv, buf1);
4662 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004663 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4664 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4665 else
4666 i = 0;
4667 n1 = FALSE;
4668 switch (type)
4669 {
4670 case TYPE_EQUAL: n1 = (i == 0); break;
4671 case TYPE_NEQUAL: n1 = (i != 0); break;
4672 case TYPE_GREATER: n1 = (i > 0); break;
4673 case TYPE_GEQUAL: n1 = (i >= 0); break;
4674 case TYPE_SMALLER: n1 = (i < 0); break;
4675 case TYPE_SEQUAL: n1 = (i <= 0); break;
4676
4677 case TYPE_MATCH:
4678 case TYPE_NOMATCH:
Bram Moolenaarea6553b2016-03-27 15:13:38 +02004679 n1 = pattern_match(s2, s1, ic);
4680 if (type == TYPE_NOMATCH)
4681 n1 = !n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004682 break;
4683
4684 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4685 }
4686 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004687 clear_tv(rettv);
4688 clear_tv(&var2);
4689 rettv->v_type = VAR_NUMBER;
4690 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004691 }
4692 }
4693
4694 return OK;
4695}
4696
4697/*
4698 * Handle fourth level expression:
4699 * + number addition
4700 * - number subtraction
4701 * . string concatenation
4702 *
4703 * "arg" must point to the first non-white of the expression.
4704 * "arg" is advanced to the next non-white after the recognized expression.
4705 *
4706 * Return OK or FAIL.
4707 */
4708 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004709eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004710{
Bram Moolenaar33570922005-01-25 22:26:29 +00004711 typval_T var2;
4712 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713 int op;
4714 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004715#ifdef FEAT_FLOAT
4716 float_T f1 = 0, f2 = 0;
4717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004718 char_u *s1, *s2;
4719 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4720 char_u *p;
4721
4722 /*
4723 * Get the first variable.
4724 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004725 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004726 return FAIL;
4727
4728 /*
4729 * Repeat computing, until no '+', '-' or '.' is following.
4730 */
4731 for (;;)
4732 {
4733 op = **arg;
4734 if (op != '+' && op != '-' && op != '.')
4735 break;
4736
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004737 if ((op != '+' || rettv->v_type != VAR_LIST)
4738#ifdef FEAT_FLOAT
4739 && (op == '.' || rettv->v_type != VAR_FLOAT)
4740#endif
4741 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004742 {
4743 /* For "list + ...", an illegal use of the first operand as
4744 * a number cannot be determined before evaluating the 2nd
4745 * operand: if this is also a list, all is ok.
4746 * For "something . ...", "something - ..." or "non-list + ...",
4747 * we know that the first operand needs to be a string or number
4748 * without evaluating the 2nd operand. So check before to avoid
4749 * side effects after an error. */
4750 if (evaluate && get_tv_string_chk(rettv) == NULL)
4751 {
4752 clear_tv(rettv);
4753 return FAIL;
4754 }
4755 }
4756
Bram Moolenaar071d4272004-06-13 20:20:40 +00004757 /*
4758 * Get the second variable.
4759 */
4760 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004761 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004762 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004763 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004764 return FAIL;
4765 }
4766
4767 if (evaluate)
4768 {
4769 /*
4770 * Compute the result.
4771 */
4772 if (op == '.')
4773 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004774 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4775 s2 = get_tv_string_buf_chk(&var2, buf2);
4776 if (s2 == NULL) /* type error ? */
4777 {
4778 clear_tv(rettv);
4779 clear_tv(&var2);
4780 return FAIL;
4781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004782 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004783 clear_tv(rettv);
4784 rettv->v_type = VAR_STRING;
4785 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004786 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004787 else if (op == '+' && rettv->v_type == VAR_LIST
4788 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004789 {
4790 /* concatenate Lists */
4791 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4792 &var3) == FAIL)
4793 {
4794 clear_tv(rettv);
4795 clear_tv(&var2);
4796 return FAIL;
4797 }
4798 clear_tv(rettv);
4799 *rettv = var3;
4800 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 else
4802 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004803 int error = FALSE;
4804
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004805#ifdef FEAT_FLOAT
4806 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004807 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808 f1 = rettv->vval.v_float;
4809 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 else
4812#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 n1 = get_tv_number_chk(rettv, &error);
4815 if (error)
4816 {
4817 /* This can only happen for "list + non-list". For
4818 * "non-list + ..." or "something - ...", we returned
4819 * before evaluating the 2nd operand. */
4820 clear_tv(rettv);
4821 return FAIL;
4822 }
4823#ifdef FEAT_FLOAT
4824 if (var2.v_type == VAR_FLOAT)
4825 f1 = n1;
4826#endif
4827 }
4828#ifdef FEAT_FLOAT
4829 if (var2.v_type == VAR_FLOAT)
4830 {
4831 f2 = var2.vval.v_float;
4832 n2 = 0;
4833 }
4834 else
4835#endif
4836 {
4837 n2 = get_tv_number_chk(&var2, &error);
4838 if (error)
4839 {
4840 clear_tv(rettv);
4841 clear_tv(&var2);
4842 return FAIL;
4843 }
4844#ifdef FEAT_FLOAT
4845 if (rettv->v_type == VAR_FLOAT)
4846 f2 = n2;
4847#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004848 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004849 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004850
4851#ifdef FEAT_FLOAT
4852 /* If there is a float on either side the result is a float. */
4853 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4854 {
4855 if (op == '+')
4856 f1 = f1 + f2;
4857 else
4858 f1 = f1 - f2;
4859 rettv->v_type = VAR_FLOAT;
4860 rettv->vval.v_float = f1;
4861 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004863#endif
4864 {
4865 if (op == '+')
4866 n1 = n1 + n2;
4867 else
4868 n1 = n1 - n2;
4869 rettv->v_type = VAR_NUMBER;
4870 rettv->vval.v_number = n1;
4871 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004873 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 }
4875 }
4876 return OK;
4877}
4878
4879/*
4880 * Handle fifth level expression:
4881 * * number multiplication
4882 * / number division
4883 * % number modulo
4884 *
4885 * "arg" must point to the first non-white of the expression.
4886 * "arg" is advanced to the next non-white after the recognized expression.
4887 *
4888 * Return OK or FAIL.
4889 */
4890 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004891eval6(
4892 char_u **arg,
4893 typval_T *rettv,
4894 int evaluate,
4895 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004896{
Bram Moolenaar33570922005-01-25 22:26:29 +00004897 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004898 int op;
4899 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004900#ifdef FEAT_FLOAT
4901 int use_float = FALSE;
4902 float_T f1 = 0, f2;
4903#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004904 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004905
4906 /*
4907 * Get the first variable.
4908 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004909 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004910 return FAIL;
4911
4912 /*
4913 * Repeat computing, until no '*', '/' or '%' is following.
4914 */
4915 for (;;)
4916 {
4917 op = **arg;
4918 if (op != '*' && op != '/' && op != '%')
4919 break;
4920
4921 if (evaluate)
4922 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004923#ifdef FEAT_FLOAT
4924 if (rettv->v_type == VAR_FLOAT)
4925 {
4926 f1 = rettv->vval.v_float;
4927 use_float = TRUE;
4928 n1 = 0;
4929 }
4930 else
4931#endif
4932 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004933 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004934 if (error)
4935 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004936 }
4937 else
4938 n1 = 0;
4939
4940 /*
4941 * Get the second variable.
4942 */
4943 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004944 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004945 return FAIL;
4946
4947 if (evaluate)
4948 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004949#ifdef FEAT_FLOAT
4950 if (var2.v_type == VAR_FLOAT)
4951 {
4952 if (!use_float)
4953 {
4954 f1 = n1;
4955 use_float = TRUE;
4956 }
4957 f2 = var2.vval.v_float;
4958 n2 = 0;
4959 }
4960 else
4961#endif
4962 {
4963 n2 = get_tv_number_chk(&var2, &error);
4964 clear_tv(&var2);
4965 if (error)
4966 return FAIL;
4967#ifdef FEAT_FLOAT
4968 if (use_float)
4969 f2 = n2;
4970#endif
4971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004972
4973 /*
4974 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004975 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004976 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004977#ifdef FEAT_FLOAT
4978 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980 if (op == '*')
4981 f1 = f1 * f2;
4982 else if (op == '/')
4983 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004984# ifdef VMS
4985 /* VMS crashes on divide by zero, work around it */
4986 if (f2 == 0.0)
4987 {
4988 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004989 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004990 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004991 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004992 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004993 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004994 }
4995 else
4996 f1 = f1 / f2;
4997# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004998 /* We rely on the floating point library to handle divide
4999 * by zero to result in "inf" and not a crash. */
5000 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005001# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005003 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005004 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005005 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005006 return FAIL;
5007 }
5008 rettv->v_type = VAR_FLOAT;
5009 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005010 }
5011 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005012#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005014 if (op == '*')
5015 n1 = n1 * n2;
5016 else if (op == '/')
5017 {
5018 if (n2 == 0) /* give an error message? */
5019 {
5020 if (n1 == 0)
5021 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5022 else if (n1 < 0)
5023 n1 = -0x7fffffffL;
5024 else
5025 n1 = 0x7fffffffL;
5026 }
5027 else
5028 n1 = n1 / n2;
5029 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005030 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005031 {
5032 if (n2 == 0) /* give an error message? */
5033 n1 = 0;
5034 else
5035 n1 = n1 % n2;
5036 }
5037 rettv->v_type = VAR_NUMBER;
5038 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005039 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005040 }
5041 }
5042
5043 return OK;
5044}
5045
5046/*
5047 * Handle sixth level expression:
5048 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005049 * "string" string constant
5050 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005051 * &option-name option value
5052 * @r register contents
5053 * identifier variable value
5054 * function() function call
5055 * $VAR environment variable
5056 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005057 * [expr, expr] List
5058 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005059 *
5060 * Also handle:
5061 * ! in front logical NOT
5062 * - in front unary minus
5063 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005064 * trailing [] subscript in String or List
5065 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005066 *
5067 * "arg" must point to the first non-white of the expression.
5068 * "arg" is advanced to the next non-white after the recognized expression.
5069 *
5070 * Return OK or FAIL.
5071 */
5072 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005073eval7(
5074 char_u **arg,
5075 typval_T *rettv,
5076 int evaluate,
5077 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005078{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005079 long n;
5080 int len;
5081 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 char_u *start_leader, *end_leader;
5083 int ret = OK;
5084 char_u *alias;
5085
5086 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005087 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005088 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005089 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005091
5092 /*
5093 * Skip '!' and '-' characters. They are handled later.
5094 */
5095 start_leader = *arg;
5096 while (**arg == '!' || **arg == '-' || **arg == '+')
5097 *arg = skipwhite(*arg + 1);
5098 end_leader = *arg;
5099
5100 switch (**arg)
5101 {
5102 /*
5103 * Number constant.
5104 */
5105 case '0':
5106 case '1':
5107 case '2':
5108 case '3':
5109 case '4':
5110 case '5':
5111 case '6':
5112 case '7':
5113 case '8':
5114 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005115 {
5116#ifdef FEAT_FLOAT
5117 char_u *p = skipdigits(*arg + 1);
5118 int get_float = FALSE;
5119
5120 /* We accept a float when the format matches
5121 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005122 * strict to avoid backwards compatibility problems.
5123 * Don't look for a float after the "." operator, so that
5124 * ":let vers = 1.2.3" doesn't fail. */
5125 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005126 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005127 get_float = TRUE;
5128 p = skipdigits(p + 2);
5129 if (*p == 'e' || *p == 'E')
5130 {
5131 ++p;
5132 if (*p == '-' || *p == '+')
5133 ++p;
5134 if (!vim_isdigit(*p))
5135 get_float = FALSE;
5136 else
5137 p = skipdigits(p + 1);
5138 }
5139 if (ASCII_ISALPHA(*p) || *p == '.')
5140 get_float = FALSE;
5141 }
5142 if (get_float)
5143 {
5144 float_T f;
5145
5146 *arg += string2float(*arg, &f);
5147 if (evaluate)
5148 {
5149 rettv->v_type = VAR_FLOAT;
5150 rettv->vval.v_float = f;
5151 }
5152 }
5153 else
5154#endif
5155 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005156 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005157 *arg += len;
5158 if (evaluate)
5159 {
5160 rettv->v_type = VAR_NUMBER;
5161 rettv->vval.v_number = n;
5162 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005163 }
5164 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166
5167 /*
5168 * String constant: "string".
5169 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005170 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005171 break;
5172
5173 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005174 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005175 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005176 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005177 break;
5178
5179 /*
5180 * List: [expr, expr]
5181 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005182 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005183 break;
5184
5185 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005186 * Dictionary: {key: val, key: val}
5187 */
5188 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5189 break;
5190
5191 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005192 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005193 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005194 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005195 break;
5196
5197 /*
5198 * Environment variable: $VAR.
5199 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005200 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005201 break;
5202
5203 /*
5204 * Register contents: @r.
5205 */
5206 case '@': ++*arg;
5207 if (evaluate)
5208 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005209 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005210 rettv->vval.v_string = get_reg_contents(**arg,
5211 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005212 }
5213 if (**arg != NUL)
5214 ++*arg;
5215 break;
5216
5217 /*
5218 * nested expression: (expression).
5219 */
5220 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005221 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005222 if (**arg == ')')
5223 ++*arg;
5224 else if (ret == OK)
5225 {
5226 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005227 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005228 ret = FAIL;
5229 }
5230 break;
5231
Bram Moolenaar8c711452005-01-14 21:53:12 +00005232 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 break;
5234 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235
5236 if (ret == NOTDONE)
5237 {
5238 /*
5239 * Must be a variable or function name.
5240 * Can also be a curly-braces kind of name: {expr}.
5241 */
5242 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005243 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005244 if (alias != NULL)
5245 s = alias;
5246
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005247 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005248 ret = FAIL;
5249 else
5250 {
5251 if (**arg == '(') /* recursive! */
5252 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005253 partial_T *partial;
5254
Bram Moolenaar8c711452005-01-14 21:53:12 +00005255 /* If "s" is the name of a variable of type VAR_FUNC
5256 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005257 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258
5259 /* Invoke the function. */
5260 ret = get_func_tv(s, len, rettv, arg,
5261 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005262 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005263
5264 /* If evaluate is FALSE rettv->v_type was not set in
5265 * get_func_tv, but it's needed in handle_subscript() to parse
5266 * what follows. So set it here. */
5267 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5268 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005269 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005270 rettv->v_type = VAR_FUNC;
5271 }
5272
Bram Moolenaar8c711452005-01-14 21:53:12 +00005273 /* Stop the expression evaluation when immediately
5274 * aborting on error, or when an interrupt occurred or
5275 * an exception was thrown but not caught. */
5276 if (aborting())
5277 {
5278 if (ret == OK)
5279 clear_tv(rettv);
5280 ret = FAIL;
5281 }
5282 }
5283 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005284 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005285 else
5286 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005287 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005288 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005289 }
5290
Bram Moolenaar071d4272004-06-13 20:20:40 +00005291 *arg = skipwhite(*arg);
5292
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005293 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5294 * expr(expr). */
5295 if (ret == OK)
5296 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005297
5298 /*
5299 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5300 */
5301 if (ret == OK && evaluate && end_leader > start_leader)
5302 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005303 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005304 int val = 0;
5305#ifdef FEAT_FLOAT
5306 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005307
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005308 if (rettv->v_type == VAR_FLOAT)
5309 f = rettv->vval.v_float;
5310 else
5311#endif
5312 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005313 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005315 clear_tv(rettv);
5316 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 else
5319 {
5320 while (end_leader > start_leader)
5321 {
5322 --end_leader;
5323 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005324 {
5325#ifdef FEAT_FLOAT
5326 if (rettv->v_type == VAR_FLOAT)
5327 f = !f;
5328 else
5329#endif
5330 val = !val;
5331 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005332 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005333 {
5334#ifdef FEAT_FLOAT
5335 if (rettv->v_type == VAR_FLOAT)
5336 f = -f;
5337 else
5338#endif
5339 val = -val;
5340 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005341 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005342#ifdef FEAT_FLOAT
5343 if (rettv->v_type == VAR_FLOAT)
5344 {
5345 clear_tv(rettv);
5346 rettv->vval.v_float = f;
5347 }
5348 else
5349#endif
5350 {
5351 clear_tv(rettv);
5352 rettv->v_type = VAR_NUMBER;
5353 rettv->vval.v_number = val;
5354 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005355 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005356 }
5357
5358 return ret;
5359}
5360
5361/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005362 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5363 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005364 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5365 */
5366 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005367eval_index(
5368 char_u **arg,
5369 typval_T *rettv,
5370 int evaluate,
5371 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005372{
5373 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005374 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005376 long len = -1;
5377 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005380
Bram Moolenaara03f2332016-02-06 18:09:59 +01005381 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005382 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005383 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005384 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005385 if (verbose)
5386 EMSG(_("E695: Cannot index a Funcref"));
5387 return FAIL;
5388 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005389#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005390 if (verbose)
5391 EMSG(_(e_float_as_string));
5392 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005393#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005394 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005395 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005396 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005397 if (verbose)
5398 EMSG(_("E909: Cannot index a special variable"));
5399 return FAIL;
5400 case VAR_UNKNOWN:
5401 if (evaluate)
5402 return FAIL;
5403 /* FALLTHROUGH */
5404
5405 case VAR_STRING:
5406 case VAR_NUMBER:
5407 case VAR_LIST:
5408 case VAR_DICT:
5409 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005410 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005411
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005412 init_tv(&var1);
5413 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005414 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005416 /*
5417 * dict.name
5418 */
5419 key = *arg + 1;
5420 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5421 ;
5422 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005423 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005424 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005425 }
5426 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005427 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005428 /*
5429 * something[idx]
5430 *
5431 * Get the (first) variable from inside the [].
5432 */
5433 *arg = skipwhite(*arg + 1);
5434 if (**arg == ':')
5435 empty1 = TRUE;
5436 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5437 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005438 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5439 {
5440 /* not a number or string */
5441 clear_tv(&var1);
5442 return FAIL;
5443 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005444
5445 /*
5446 * Get the second variable from inside the [:].
5447 */
5448 if (**arg == ':')
5449 {
5450 range = TRUE;
5451 *arg = skipwhite(*arg + 1);
5452 if (**arg == ']')
5453 empty2 = TRUE;
5454 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5455 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005456 if (!empty1)
5457 clear_tv(&var1);
5458 return FAIL;
5459 }
5460 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5461 {
5462 /* not a number or string */
5463 if (!empty1)
5464 clear_tv(&var1);
5465 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005466 return FAIL;
5467 }
5468 }
5469
5470 /* Check for the ']'. */
5471 if (**arg != ']')
5472 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005473 if (verbose)
5474 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005475 clear_tv(&var1);
5476 if (range)
5477 clear_tv(&var2);
5478 return FAIL;
5479 }
5480 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005481 }
5482
5483 if (evaluate)
5484 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005485 n1 = 0;
5486 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005487 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005488 n1 = get_tv_number(&var1);
5489 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 }
5491 if (range)
5492 {
5493 if (empty2)
5494 n2 = -1;
5495 else
5496 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005497 n2 = get_tv_number(&var2);
5498 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005499 }
5500 }
5501
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005502 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005503 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005504 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005505 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005506 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005507 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005508 case VAR_SPECIAL:
5509 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005510 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005511 break; /* not evaluating, skipping over subscript */
5512
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005513 case VAR_NUMBER:
5514 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005515 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 len = (long)STRLEN(s);
5517 if (range)
5518 {
5519 /* The resulting variable is a substring. If the indexes
5520 * are out of range the result is empty. */
5521 if (n1 < 0)
5522 {
5523 n1 = len + n1;
5524 if (n1 < 0)
5525 n1 = 0;
5526 }
5527 if (n2 < 0)
5528 n2 = len + n2;
5529 else if (n2 >= len)
5530 n2 = len;
5531 if (n1 >= len || n2 < 0 || n1 > n2)
5532 s = NULL;
5533 else
5534 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5535 }
5536 else
5537 {
5538 /* The resulting variable is a string of a single
5539 * character. If the index is too big or negative the
5540 * result is empty. */
5541 if (n1 >= len || n1 < 0)
5542 s = NULL;
5543 else
5544 s = vim_strnsave(s + n1, 1);
5545 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005546 clear_tv(rettv);
5547 rettv->v_type = VAR_STRING;
5548 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005549 break;
5550
5551 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005552 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005553 if (n1 < 0)
5554 n1 = len + n1;
5555 if (!empty1 && (n1 < 0 || n1 >= len))
5556 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005557 /* For a range we allow invalid values and return an empty
5558 * list. A list index out of range is an error. */
5559 if (!range)
5560 {
5561 if (verbose)
5562 EMSGN(_(e_listidx), n1);
5563 return FAIL;
5564 }
5565 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005566 }
5567 if (range)
5568 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005569 list_T *l;
5570 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005571
5572 if (n2 < 0)
5573 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005574 else if (n2 >= len)
5575 n2 = len - 1;
5576 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005577 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005578 l = list_alloc();
5579 if (l == NULL)
5580 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005581 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005582 n1 <= n2; ++n1)
5583 {
5584 if (list_append_tv(l, &item->li_tv) == FAIL)
5585 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005586 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005587 return FAIL;
5588 }
5589 item = item->li_next;
5590 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005591 clear_tv(rettv);
5592 rettv->v_type = VAR_LIST;
5593 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005594 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005595 }
5596 else
5597 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005598 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005599 clear_tv(rettv);
5600 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005601 }
5602 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005603
5604 case VAR_DICT:
5605 if (range)
5606 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005607 if (verbose)
5608 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005609 if (len == -1)
5610 clear_tv(&var1);
5611 return FAIL;
5612 }
5613 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005614 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005615
5616 if (len == -1)
5617 {
5618 key = get_tv_string(&var1);
5619 if (*key == NUL)
5620 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005621 if (verbose)
5622 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005623 clear_tv(&var1);
5624 return FAIL;
5625 }
5626 }
5627
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005628 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005629
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005630 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005631 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632 if (len == -1)
5633 clear_tv(&var1);
5634 if (item == NULL)
5635 return FAIL;
5636
5637 copy_tv(&item->di_tv, &var1);
5638 clear_tv(rettv);
5639 *rettv = var1;
5640 }
5641 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005642 }
5643 }
5644
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 return OK;
5646}
5647
5648/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005649 * Get an option value.
5650 * "arg" points to the '&' or '+' before the option name.
5651 * "arg" is advanced to character after the option name.
5652 * Return OK or FAIL.
5653 */
5654 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005655get_option_tv(
5656 char_u **arg,
5657 typval_T *rettv, /* when NULL, only check if option exists */
5658 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005659{
5660 char_u *option_end;
5661 long numval;
5662 char_u *stringval;
5663 int opt_type;
5664 int c;
5665 int working = (**arg == '+'); /* has("+option") */
5666 int ret = OK;
5667 int opt_flags;
5668
5669 /*
5670 * Isolate the option name and find its value.
5671 */
5672 option_end = find_option_end(arg, &opt_flags);
5673 if (option_end == NULL)
5674 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005675 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005676 EMSG2(_("E112: Option name missing: %s"), *arg);
5677 return FAIL;
5678 }
5679
5680 if (!evaluate)
5681 {
5682 *arg = option_end;
5683 return OK;
5684 }
5685
5686 c = *option_end;
5687 *option_end = NUL;
5688 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005689 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005690
5691 if (opt_type == -3) /* invalid name */
5692 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005693 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005694 EMSG2(_("E113: Unknown option: %s"), *arg);
5695 ret = FAIL;
5696 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005697 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005698 {
5699 if (opt_type == -2) /* hidden string option */
5700 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005701 rettv->v_type = VAR_STRING;
5702 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005703 }
5704 else if (opt_type == -1) /* hidden number option */
5705 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005706 rettv->v_type = VAR_NUMBER;
5707 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005708 }
5709 else if (opt_type == 1) /* number option */
5710 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005711 rettv->v_type = VAR_NUMBER;
5712 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005713 }
5714 else /* string option */
5715 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005716 rettv->v_type = VAR_STRING;
5717 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005718 }
5719 }
5720 else if (working && (opt_type == -2 || opt_type == -1))
5721 ret = FAIL;
5722
5723 *option_end = c; /* put back for error messages */
5724 *arg = option_end;
5725
5726 return ret;
5727}
5728
5729/*
5730 * Allocate a variable for a string constant.
5731 * Return OK or FAIL.
5732 */
5733 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005734get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005735{
5736 char_u *p;
5737 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738 int extra = 0;
5739
5740 /*
5741 * Find the end of the string, skipping backslashed characters.
5742 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005743 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005744 {
5745 if (*p == '\\' && p[1] != NUL)
5746 {
5747 ++p;
5748 /* A "\<x>" form occupies at least 4 characters, and produces up
5749 * to 6 characters: reserve space for 2 extra */
5750 if (*p == '<')
5751 extra += 2;
5752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005753 }
5754
5755 if (*p != '"')
5756 {
5757 EMSG2(_("E114: Missing quote: %s"), *arg);
5758 return FAIL;
5759 }
5760
5761 /* If only parsing, set *arg and return here */
5762 if (!evaluate)
5763 {
5764 *arg = p + 1;
5765 return OK;
5766 }
5767
5768 /*
5769 * Copy the string into allocated memory, handling backslashed
5770 * characters.
5771 */
5772 name = alloc((unsigned)(p - *arg + extra));
5773 if (name == NULL)
5774 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005775 rettv->v_type = VAR_STRING;
5776 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005777
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005779 {
5780 if (*p == '\\')
5781 {
5782 switch (*++p)
5783 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005784 case 'b': *name++ = BS; ++p; break;
5785 case 'e': *name++ = ESC; ++p; break;
5786 case 'f': *name++ = FF; ++p; break;
5787 case 'n': *name++ = NL; ++p; break;
5788 case 'r': *name++ = CAR; ++p; break;
5789 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005790
5791 case 'X': /* hex: "\x1", "\x12" */
5792 case 'x':
5793 case 'u': /* Unicode: "\u0023" */
5794 case 'U':
5795 if (vim_isxdigit(p[1]))
5796 {
5797 int n, nr;
5798 int c = toupper(*p);
5799
5800 if (c == 'X')
5801 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005802 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005803 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005804 else
5805 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 nr = 0;
5807 while (--n >= 0 && vim_isxdigit(p[1]))
5808 {
5809 ++p;
5810 nr = (nr << 4) + hex2nr(*p);
5811 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005812 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005813#ifdef FEAT_MBYTE
5814 /* For "\u" store the number according to
5815 * 'encoding'. */
5816 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005817 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005818 else
5819#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005822 break;
5823
5824 /* octal: "\1", "\12", "\123" */
5825 case '0':
5826 case '1':
5827 case '2':
5828 case '3':
5829 case '4':
5830 case '5':
5831 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005832 case '7': *name = *p++ - '0';
5833 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005834 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 *name = (*name << 3) + *p++ - '0';
5836 if (*p >= '0' && *p <= '7')
5837 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005838 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005839 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005840 break;
5841
5842 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005843 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005844 if (extra != 0)
5845 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 break;
5848 }
5849 /* FALLTHROUGH */
5850
Bram Moolenaar8c711452005-01-14 21:53:12 +00005851 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005852 break;
5853 }
5854 }
5855 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005856 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005857
Bram Moolenaar071d4272004-06-13 20:20:40 +00005858 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860 *arg = p + 1;
5861
Bram Moolenaar071d4272004-06-13 20:20:40 +00005862 return OK;
5863}
5864
5865/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005866 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005867 * Return OK or FAIL.
5868 */
5869 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005870get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005871{
5872 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005873 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005874 int reduce = 0;
5875
5876 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005877 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005878 */
5879 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5880 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005881 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005882 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005883 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005884 break;
5885 ++reduce;
5886 ++p;
5887 }
5888 }
5889
Bram Moolenaar8c711452005-01-14 21:53:12 +00005890 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005891 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005892 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005893 return FAIL;
5894 }
5895
Bram Moolenaar8c711452005-01-14 21:53:12 +00005896 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005897 if (!evaluate)
5898 {
5899 *arg = p + 1;
5900 return OK;
5901 }
5902
5903 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005904 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005905 */
5906 str = alloc((unsigned)((p - *arg) - reduce));
5907 if (str == NULL)
5908 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005909 rettv->v_type = VAR_STRING;
5910 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005911
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005913 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005914 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005915 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005916 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005917 break;
5918 ++p;
5919 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005920 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005921 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005922 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005923 *arg = p + 1;
5924
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005925 return OK;
5926}
5927
5928/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005929 * Allocate a variable for a List and fill it from "*arg".
5930 * Return OK or FAIL.
5931 */
5932 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005933get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005934{
Bram Moolenaar33570922005-01-25 22:26:29 +00005935 list_T *l = NULL;
5936 typval_T tv;
5937 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005938
5939 if (evaluate)
5940 {
5941 l = list_alloc();
5942 if (l == NULL)
5943 return FAIL;
5944 }
5945
5946 *arg = skipwhite(*arg + 1);
5947 while (**arg != ']' && **arg != NUL)
5948 {
5949 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5950 goto failret;
5951 if (evaluate)
5952 {
5953 item = listitem_alloc();
5954 if (item != NULL)
5955 {
5956 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005957 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005958 list_append(l, item);
5959 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005960 else
5961 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005962 }
5963
5964 if (**arg == ']')
5965 break;
5966 if (**arg != ',')
5967 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005968 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005969 goto failret;
5970 }
5971 *arg = skipwhite(*arg + 1);
5972 }
5973
5974 if (**arg != ']')
5975 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005976 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005977failret:
5978 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005979 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980 return FAIL;
5981 }
5982
5983 *arg = skipwhite(*arg + 1);
5984 if (evaluate)
5985 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005986 rettv->v_type = VAR_LIST;
5987 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005988 ++l->lv_refcount;
5989 }
5990
5991 return OK;
5992}
5993
5994/*
5995 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005996 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005997 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00005998 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01005999list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006001 list_T *l;
6002
6003 l = (list_T *)alloc_clear(sizeof(list_T));
6004 if (l != NULL)
6005 {
6006 /* Prepend the list to the list of lists for garbage collection. */
6007 if (first_list != NULL)
6008 first_list->lv_used_prev = l;
6009 l->lv_used_prev = NULL;
6010 l->lv_used_next = first_list;
6011 first_list = l;
6012 }
6013 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006014}
6015
6016/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006017 * Allocate an empty list for a return value.
6018 * Returns OK or FAIL.
6019 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006020 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006021rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006022{
6023 list_T *l = list_alloc();
6024
6025 if (l == NULL)
6026 return FAIL;
6027
6028 rettv->vval.v_list = l;
6029 rettv->v_type = VAR_LIST;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02006030 rettv->v_lock = 0;
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006031 ++l->lv_refcount;
6032 return OK;
6033}
6034
6035/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006036 * Unreference a list: decrement the reference count and free it when it
6037 * becomes zero.
6038 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006039 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006040list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006041{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006042 if (l != NULL && --l->lv_refcount <= 0)
6043 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006044}
6045
6046/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006047 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006048 * Ignores the reference count.
6049 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006050 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006051list_free(
6052 list_T *l,
6053 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006054{
Bram Moolenaar33570922005-01-25 22:26:29 +00006055 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006057 /* Remove the list from the list of lists for garbage collection. */
6058 if (l->lv_used_prev == NULL)
6059 first_list = l->lv_used_next;
6060 else
6061 l->lv_used_prev->lv_used_next = l->lv_used_next;
6062 if (l->lv_used_next != NULL)
6063 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6064
Bram Moolenaard9fba312005-06-26 22:34:35 +00006065 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006066 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 /* Remove the item before deleting it. */
6068 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006069 if (recurse || (item->li_tv.v_type != VAR_LIST
6070 && item->li_tv.v_type != VAR_DICT))
6071 clear_tv(&item->li_tv);
6072 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006073 }
6074 vim_free(l);
6075}
6076
6077/*
6078 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006079 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006080 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006081 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006082listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006083{
Bram Moolenaar33570922005-01-25 22:26:29 +00006084 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085}
6086
6087/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006088 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006089 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006090 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006091listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006092{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006093 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094 vim_free(item);
6095}
6096
6097/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006098 * Remove a list item from a List and free it. Also clears the value.
6099 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006100 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006101listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006102{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006103 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006104 listitem_free(item);
6105}
6106
6107/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006108 * Get the number of items in a list.
6109 */
6110 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006111list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006113 if (l == NULL)
6114 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006115 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006116}
6117
6118/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006119 * Return TRUE when two lists have exactly the same values.
6120 */
6121 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006122list_equal(
6123 list_T *l1,
6124 list_T *l2,
6125 int ic, /* ignore case for strings */
6126 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006127{
Bram Moolenaar33570922005-01-25 22:26:29 +00006128 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006130 if (l1 == NULL || l2 == NULL)
6131 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006132 if (l1 == l2)
6133 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006134 if (list_len(l1) != list_len(l2))
6135 return FALSE;
6136
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006137 for (item1 = l1->lv_first, item2 = l2->lv_first;
6138 item1 != NULL && item2 != NULL;
6139 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006140 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006141 return FALSE;
6142 return item1 == NULL && item2 == NULL;
6143}
6144
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006145/*
6146 * Return the dictitem that an entry in a hashtable points to.
6147 */
6148 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006149dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006150{
6151 return HI2DI(hi);
6152}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006153
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006154/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006155 * Return TRUE when two dictionaries have exactly the same key/values.
6156 */
6157 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006158dict_equal(
6159 dict_T *d1,
6160 dict_T *d2,
6161 int ic, /* ignore case for strings */
6162 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006163{
Bram Moolenaar33570922005-01-25 22:26:29 +00006164 hashitem_T *hi;
6165 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006166 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006167
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006168 if (d1 == NULL || d2 == NULL)
6169 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006170 if (d1 == d2)
6171 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006172 if (dict_len(d1) != dict_len(d2))
6173 return FALSE;
6174
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006175 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006176 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006177 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006178 if (!HASHITEM_EMPTY(hi))
6179 {
6180 item2 = dict_find(d2, hi->hi_key, -1);
6181 if (item2 == NULL)
6182 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006183 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006184 return FALSE;
6185 --todo;
6186 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006187 }
6188 return TRUE;
6189}
6190
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006191static int tv_equal_recurse_limit;
6192
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006193/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006194 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006195 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006196 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006197 */
6198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006199tv_equal(
6200 typval_T *tv1,
6201 typval_T *tv2,
6202 int ic, /* ignore case */
6203 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006204{
6205 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006206 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006207 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006208 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006209
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006210 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6211 if ((tv1->v_type == VAR_FUNC
6212 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6213 && (tv2->v_type == VAR_FUNC
6214 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6215 {
6216 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6217 : tv1->vval.v_partial->pt_name;
6218 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6219 : tv2->vval.v_partial->pt_name;
6220 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6221 }
6222
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006223 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006224 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006225
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 * recursiveness to a limit. We guess they are equal then.
6228 * A fixed limit has the problem of still taking an awful long time.
6229 * Reduce the limit every time running into it. That should work fine for
6230 * deeply linked structures that are not recursively linked and catch
6231 * recursiveness quickly. */
6232 if (!recursive)
6233 tv_equal_recurse_limit = 1000;
6234 if (recursive_cnt >= tv_equal_recurse_limit)
6235 {
6236 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006237 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006239
6240 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006241 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006243 ++recursive_cnt;
6244 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6245 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006246 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006247
6248 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006249 ++recursive_cnt;
6250 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6251 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006252 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006254 case VAR_NUMBER:
6255 return tv1->vval.v_number == tv2->vval.v_number;
6256
6257 case VAR_STRING:
6258 s1 = get_tv_string_buf(tv1, buf1);
6259 s2 = get_tv_string_buf(tv2, buf2);
6260 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006261
6262 case VAR_SPECIAL:
6263 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006264
6265 case VAR_FLOAT:
6266#ifdef FEAT_FLOAT
6267 return tv1->vval.v_float == tv2->vval.v_float;
6268#endif
6269 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006270#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006271 return tv1->vval.v_job == tv2->vval.v_job;
6272#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006273 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006274#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006275 return tv1->vval.v_channel == tv2->vval.v_channel;
6276#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006277 case VAR_FUNC:
6278 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006279 case VAR_UNKNOWN:
6280 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006281 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006282
Bram Moolenaara03f2332016-02-06 18:09:59 +01006283 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6284 * does not equal anything, not even itself. */
6285 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006286}
6287
6288/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006289 * Locate item with index "n" in list "l" and return it.
6290 * A negative index is counted from the end; -1 is the last item.
6291 * Returns NULL when "n" is out of range.
6292 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006293 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006294list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006295{
Bram Moolenaar33570922005-01-25 22:26:29 +00006296 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006297 long idx;
6298
6299 if (l == NULL)
6300 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006301
6302 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006303 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006304 n = l->lv_len + n;
6305
6306 /* Check for index out of range. */
6307 if (n < 0 || n >= l->lv_len)
6308 return NULL;
6309
6310 /* When there is a cached index may start search from there. */
6311 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006312 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006313 if (n < l->lv_idx / 2)
6314 {
6315 /* closest to the start of the list */
6316 item = l->lv_first;
6317 idx = 0;
6318 }
6319 else if (n > (l->lv_idx + l->lv_len) / 2)
6320 {
6321 /* closest to the end of the list */
6322 item = l->lv_last;
6323 idx = l->lv_len - 1;
6324 }
6325 else
6326 {
6327 /* closest to the cached index */
6328 item = l->lv_idx_item;
6329 idx = l->lv_idx;
6330 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006331 }
6332 else
6333 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006334 if (n < l->lv_len / 2)
6335 {
6336 /* closest to the start of the list */
6337 item = l->lv_first;
6338 idx = 0;
6339 }
6340 else
6341 {
6342 /* closest to the end of the list */
6343 item = l->lv_last;
6344 idx = l->lv_len - 1;
6345 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006346 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006347
6348 while (n > idx)
6349 {
6350 /* search forward */
6351 item = item->li_next;
6352 ++idx;
6353 }
6354 while (n < idx)
6355 {
6356 /* search backward */
6357 item = item->li_prev;
6358 --idx;
6359 }
6360
6361 /* cache the used index */
6362 l->lv_idx = idx;
6363 l->lv_idx_item = item;
6364
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006365 return item;
6366}
6367
6368/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006369 * Get list item "l[idx]" as a number.
6370 */
6371 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006372list_find_nr(
6373 list_T *l,
6374 long idx,
6375 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006376{
6377 listitem_T *li;
6378
6379 li = list_find(l, idx);
6380 if (li == NULL)
6381 {
6382 if (errorp != NULL)
6383 *errorp = TRUE;
6384 return -1L;
6385 }
6386 return get_tv_number_chk(&li->li_tv, errorp);
6387}
6388
6389/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006390 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6391 */
6392 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006393list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006394{
6395 listitem_T *li;
6396
6397 li = list_find(l, idx - 1);
6398 if (li == NULL)
6399 {
6400 EMSGN(_(e_listidx), idx);
6401 return NULL;
6402 }
6403 return get_tv_string(&li->li_tv);
6404}
6405
6406/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006407 * Locate "item" list "l" and return its index.
6408 * Returns -1 when "item" is not in the list.
6409 */
6410 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006411list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006412{
6413 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006414 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006415
6416 if (l == NULL)
6417 return -1;
6418 idx = 0;
6419 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6420 ++idx;
6421 if (li == NULL)
6422 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006423 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006424}
6425
6426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427 * Append item "item" to the end of list "l".
6428 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006429 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006430list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006431{
6432 if (l->lv_last == NULL)
6433 {
6434 /* empty list */
6435 l->lv_first = item;
6436 l->lv_last = item;
6437 item->li_prev = NULL;
6438 }
6439 else
6440 {
6441 l->lv_last->li_next = item;
6442 item->li_prev = l->lv_last;
6443 l->lv_last = item;
6444 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006445 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006446 item->li_next = NULL;
6447}
6448
6449/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006450 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006451 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006452 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006453 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006454list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006456 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006457
Bram Moolenaar05159a02005-02-26 23:04:13 +00006458 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006459 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006460 copy_tv(tv, &li->li_tv);
6461 list_append(l, li);
6462 return OK;
6463}
6464
6465/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006466 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006467 * Return FAIL when out of memory.
6468 */
6469 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006470list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006471{
6472 listitem_T *li = listitem_alloc();
6473
6474 if (li == NULL)
6475 return FAIL;
6476 li->li_tv.v_type = VAR_DICT;
6477 li->li_tv.v_lock = 0;
6478 li->li_tv.vval.v_dict = dict;
6479 list_append(list, li);
6480 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006481 return OK;
6482}
6483
6484/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006485 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006486 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006487 * Returns FAIL when out of memory.
6488 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006489 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006490list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006491{
6492 listitem_T *li = listitem_alloc();
6493
6494 if (li == NULL)
6495 return FAIL;
6496 list_append(l, li);
6497 li->li_tv.v_type = VAR_STRING;
6498 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006499 if (str == NULL)
6500 li->li_tv.vval.v_string = NULL;
6501 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006502 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006503 return FAIL;
6504 return OK;
6505}
6506
6507/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006508 * Append "n" to list "l".
6509 * Returns FAIL when out of memory.
6510 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006511 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006512list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006513{
6514 listitem_T *li;
6515
6516 li = listitem_alloc();
6517 if (li == NULL)
6518 return FAIL;
6519 li->li_tv.v_type = VAR_NUMBER;
6520 li->li_tv.v_lock = 0;
6521 li->li_tv.vval.v_number = n;
6522 list_append(l, li);
6523 return OK;
6524}
6525
6526/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006527 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006528 * If "item" is NULL append at the end.
6529 * Return FAIL when out of memory.
6530 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006532list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006533{
Bram Moolenaar33570922005-01-25 22:26:29 +00006534 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006535
6536 if (ni == NULL)
6537 return FAIL;
6538 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006539 list_insert(l, ni, item);
6540 return OK;
6541}
6542
6543 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006544list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006545{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006546 if (item == NULL)
6547 /* Append new item at end of list. */
6548 list_append(l, ni);
6549 else
6550 {
6551 /* Insert new item before existing item. */
6552 ni->li_prev = item->li_prev;
6553 ni->li_next = item;
6554 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006555 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006556 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006557 ++l->lv_idx;
6558 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006559 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006560 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006561 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006562 l->lv_idx_item = NULL;
6563 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006564 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006565 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006567}
6568
6569/*
6570 * Extend "l1" with "l2".
6571 * If "bef" is NULL append at the end, otherwise insert before this item.
6572 * Returns FAIL when out of memory.
6573 */
6574 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006575list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006576{
Bram Moolenaar33570922005-01-25 22:26:29 +00006577 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006578 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006580 /* We also quit the loop when we have inserted the original item count of
6581 * the list, avoid a hang when we extend a list with itself. */
6582 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006583 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6584 return FAIL;
6585 return OK;
6586}
6587
6588/*
6589 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6590 * Return FAIL when out of memory.
6591 */
6592 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006593list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006594{
Bram Moolenaar33570922005-01-25 22:26:29 +00006595 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006597 if (l1 == NULL || l2 == NULL)
6598 return FAIL;
6599
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006600 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006601 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006602 if (l == NULL)
6603 return FAIL;
6604 tv->v_type = VAR_LIST;
6605 tv->vval.v_list = l;
6606
6607 /* append all items from the second list */
6608 return list_extend(l, l2, NULL);
6609}
6610
6611/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006612 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006613 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006614 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615 * Returns NULL when out of memory.
6616 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006617 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006618list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619{
Bram Moolenaar33570922005-01-25 22:26:29 +00006620 list_T *copy;
6621 listitem_T *item;
6622 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006623
6624 if (orig == NULL)
6625 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006626
6627 copy = list_alloc();
6628 if (copy != NULL)
6629 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006630 if (copyID != 0)
6631 {
6632 /* Do this before adding the items, because one of the items may
6633 * refer back to this list. */
6634 orig->lv_copyID = copyID;
6635 orig->lv_copylist = copy;
6636 }
6637 for (item = orig->lv_first; item != NULL && !got_int;
6638 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006639 {
6640 ni = listitem_alloc();
6641 if (ni == NULL)
6642 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006643 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006644 {
6645 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6646 {
6647 vim_free(ni);
6648 break;
6649 }
6650 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006651 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006652 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006653 list_append(copy, ni);
6654 }
6655 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006656 if (item != NULL)
6657 {
6658 list_unref(copy);
6659 copy = NULL;
6660 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006661 }
6662
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006663 return copy;
6664}
6665
6666/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006667 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006668 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006669 * This used to be called list_remove, but that conflicts with a Sun header
6670 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006671 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006672 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006673vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006674{
Bram Moolenaar33570922005-01-25 22:26:29 +00006675 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006676
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677 /* notify watchers */
6678 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006679 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006680 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681 list_fix_watch(l, ip);
6682 if (ip == item2)
6683 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006684 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006685
6686 if (item2->li_next == NULL)
6687 l->lv_last = item->li_prev;
6688 else
6689 item2->li_next->li_prev = item->li_prev;
6690 if (item->li_prev == NULL)
6691 l->lv_first = item2->li_next;
6692 else
6693 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006694 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006695}
6696
6697/*
6698 * Return an allocated string with the string representation of a list.
6699 * May return NULL.
6700 */
6701 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006702list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006703{
6704 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006705
6706 if (tv->vval.v_list == NULL)
6707 return NULL;
6708 ga_init2(&ga, (int)sizeof(char), 80);
6709 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006710 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006711 {
6712 vim_free(ga.ga_data);
6713 return NULL;
6714 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006715 ga_append(&ga, ']');
6716 ga_append(&ga, NUL);
6717 return (char_u *)ga.ga_data;
6718}
6719
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006720typedef struct join_S {
6721 char_u *s;
6722 char_u *tofree;
6723} join_T;
6724
6725 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006726list_join_inner(
6727 garray_T *gap, /* to store the result in */
6728 list_T *l,
6729 char_u *sep,
6730 int echo_style,
6731 int copyID,
6732 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006733{
6734 int i;
6735 join_T *p;
6736 int len;
6737 int sumlen = 0;
6738 int first = TRUE;
6739 char_u *tofree;
6740 char_u numbuf[NUMBUFLEN];
6741 listitem_T *item;
6742 char_u *s;
6743
6744 /* Stringify each item in the list. */
6745 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6746 {
6747 if (echo_style)
6748 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6749 else
6750 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6751 if (s == NULL)
6752 return FAIL;
6753
6754 len = (int)STRLEN(s);
6755 sumlen += len;
6756
Bram Moolenaarcde88542015-08-11 19:14:00 +02006757 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006758 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6759 if (tofree != NULL || s != numbuf)
6760 {
6761 p->s = s;
6762 p->tofree = tofree;
6763 }
6764 else
6765 {
6766 p->s = vim_strnsave(s, len);
6767 p->tofree = p->s;
6768 }
6769
6770 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006771 if (did_echo_string_emsg) /* recursion error, bail out */
6772 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006773 }
6774
6775 /* Allocate result buffer with its total size, avoid re-allocation and
6776 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6777 if (join_gap->ga_len >= 2)
6778 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6779 if (ga_grow(gap, sumlen + 2) == FAIL)
6780 return FAIL;
6781
6782 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6783 {
6784 if (first)
6785 first = FALSE;
6786 else
6787 ga_concat(gap, sep);
6788 p = ((join_T *)join_gap->ga_data) + i;
6789
6790 if (p->s != NULL)
6791 ga_concat(gap, p->s);
6792 line_breakcheck();
6793 }
6794
6795 return OK;
6796}
6797
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006798/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006799 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006800 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006801 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006802 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006803 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006804list_join(
6805 garray_T *gap,
6806 list_T *l,
6807 char_u *sep,
6808 int echo_style,
6809 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006810{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006811 garray_T join_ga;
6812 int retval;
6813 join_T *p;
6814 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006815
Bram Moolenaard39a7512015-04-16 22:51:22 +02006816 if (l->lv_len < 1)
6817 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006818 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6819 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6820
6821 /* Dispose each item in join_ga. */
6822 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006823 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006824 p = (join_T *)join_ga.ga_data;
6825 for (i = 0; i < join_ga.ga_len; ++i)
6826 {
6827 vim_free(p->tofree);
6828 ++p;
6829 }
6830 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006831 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006832
6833 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006834}
6835
6836/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006837 * Return the next (unique) copy ID.
6838 * Used for serializing nested structures.
6839 */
6840 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006841get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006842{
6843 current_copyID += COPYID_INC;
6844 return current_copyID;
6845}
6846
6847/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006848 * Garbage collection for lists and dictionaries.
6849 *
6850 * We use reference counts to be able to free most items right away when they
6851 * are no longer used. But for composite items it's possible that it becomes
6852 * unused while the reference count is > 0: When there is a recursive
6853 * reference. Example:
6854 * :let l = [1, 2, 3]
6855 * :let d = {9: l}
6856 * :let l[1] = d
6857 *
6858 * Since this is quite unusual we handle this with garbage collection: every
6859 * once in a while find out which lists and dicts are not referenced from any
6860 * variable.
6861 *
6862 * Here is a good reference text about garbage collection (refers to Python
6863 * but it applies to all reference-counting mechanisms):
6864 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006865 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006866
6867/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006868 * Do garbage collection for lists and dicts.
6869 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006870 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006871 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006872garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006873{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006874 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006875 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006876 buf_T *buf;
6877 win_T *wp;
6878 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006879 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006880 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006881 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006882#ifdef FEAT_WINDOWS
6883 tabpage_T *tp;
6884#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006885
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006886 /* Only do this once. */
6887 want_garbage_collect = FALSE;
6888 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006889 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006890
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006891 /* We advance by two because we add one for items referenced through
6892 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006893 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006894
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006895 /*
6896 * 1. Go through all accessible variables and mark all lists and dicts
6897 * with copyID.
6898 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899
6900 /* Don't free variables in the previous_funccal list unless they are only
6901 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006902 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006903 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6904 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006905 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6906 NULL);
6907 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6908 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006909 }
6910
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006911 /* script-local variables */
6912 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006914
6915 /* buffer-local variables */
6916 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006917 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6918 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006919
6920 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006921 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6923 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006924#ifdef FEAT_AUTOCMD
6925 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006926 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6927 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006928#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006929
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006930#ifdef FEAT_WINDOWS
6931 /* tabpage-local variables */
6932 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006933 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6934 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006935#endif
6936
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006937 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006938 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006939
6940 /* function-local variables */
6941 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6942 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006943 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6944 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006945 }
6946
Bram Moolenaard812df62008-11-09 12:46:09 +00006947 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006948 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006949
Bram Moolenaar1dced572012-04-05 16:54:08 +02006950#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006952#endif
6953
Bram Moolenaardb913952012-06-29 12:54:53 +02006954#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006956#endif
6957
6958#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006959 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006960#endif
6961
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006962#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006963 abort = abort || set_ref_in_channel(copyID);
6964#endif
6965
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006966 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006967 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006968 /*
6969 * 2. Free lists and dictionaries that are not referenced.
6970 */
6971 did_free = free_unref_items(copyID);
6972
6973 /*
6974 * 3. Check if any funccal can be freed now.
6975 */
6976 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006977 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006978 if (can_free_funccal(*pfc, copyID))
6979 {
6980 fc = *pfc;
6981 *pfc = fc->caller;
6982 free_funccal(fc, TRUE);
6983 did_free = TRUE;
6984 did_free_funccal = TRUE;
6985 }
6986 else
6987 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006988 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006989 if (did_free_funccal)
6990 /* When a funccal was freed some more items might be garbage
6991 * collected, so run again. */
6992 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006993 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006994 else if (p_verbose > 0)
6995 {
6996 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6997 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006998
6999 return did_free;
7000}
7001
7002/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007003 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007004 */
7005 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007006free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007007{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007008 dict_T *dd, *dd_next;
7009 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007010 int did_free = FALSE;
7011
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007012 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007013 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007014 */
7015 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007016 {
7017 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007018 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007019 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007020 /* Free the Dictionary and ordinary items it contains, but don't
7021 * recurse into Lists and Dictionaries, they will be in the list
7022 * of dicts or list of lists. */
7023 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007025 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007026 dd = dd_next;
7027 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007028
7029 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007030 * Go through the list of lists and free items without the copyID.
7031 * But don't free a list that has a watcher (used in a for loop), these
7032 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007033 */
7034 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007035 {
7036 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007037 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7038 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007039 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007040 /* Free the List and ordinary items it contains, but don't recurse
7041 * into Lists and Dictionaries, they will be in the list of dicts
7042 * or list of lists. */
7043 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007046 ll = ll_next;
7047 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007048
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007049 return did_free;
7050}
7051
7052/*
7053 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 * "list_stack" is used to add lists to be marked. Can be NULL.
7055 *
7056 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007057 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007059set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007060{
7061 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007062 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 hashtab_T *cur_ht;
7065 ht_stack_T *ht_stack = NULL;
7066 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007067
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007068 cur_ht = ht;
7069 for (;;)
7070 {
7071 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007072 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007073 /* Mark each item in the hashtab. If the item contains a hashtab
7074 * it is added to ht_stack, if it contains a list it is added to
7075 * list_stack. */
7076 todo = (int)cur_ht->ht_used;
7077 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7078 if (!HASHITEM_EMPTY(hi))
7079 {
7080 --todo;
7081 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7082 &ht_stack, list_stack);
7083 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007084 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007085
7086 if (ht_stack == NULL)
7087 break;
7088
7089 /* take an item from the stack */
7090 cur_ht = ht_stack->ht;
7091 tempitem = ht_stack;
7092 ht_stack = ht_stack->prev;
7093 free(tempitem);
7094 }
7095
7096 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007097}
7098
7099/*
7100 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7102 *
7103 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007104 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007105 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007106set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007107{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007108 listitem_T *li;
7109 int abort = FALSE;
7110 list_T *cur_l;
7111 list_stack_T *list_stack = NULL;
7112 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007113
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007114 cur_l = l;
7115 for (;;)
7116 {
7117 if (!abort)
7118 /* Mark each item in the list. If the item contains a hashtab
7119 * it is added to ht_stack, if it contains a list it is added to
7120 * list_stack. */
7121 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7122 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7123 ht_stack, &list_stack);
7124 if (list_stack == NULL)
7125 break;
7126
7127 /* take an item from the stack */
7128 cur_l = list_stack->list;
7129 tempitem = list_stack;
7130 list_stack = list_stack->prev;
7131 free(tempitem);
7132 }
7133
7134 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007135}
7136
7137/*
7138 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007139 * "list_stack" is used to add lists to be marked. Can be NULL.
7140 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7141 *
7142 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007143 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007144 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007145set_ref_in_item(
7146 typval_T *tv,
7147 int copyID,
7148 ht_stack_T **ht_stack,
7149 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007150{
7151 dict_T *dd;
7152 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007153 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007154
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007155 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007156 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007157 if (tv->v_type == VAR_DICT)
7158 dd = tv->vval.v_dict;
7159 else if (tv->vval.v_partial != NULL)
7160 dd = tv->vval.v_partial->pt_dict;
7161 else
7162 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007163 if (dd != NULL && dd->dv_copyID != copyID)
7164 {
7165 /* Didn't see this dict yet. */
7166 dd->dv_copyID = copyID;
7167 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007168 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7170 }
7171 else
7172 {
7173 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7174 if (newitem == NULL)
7175 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007176 else
7177 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007178 newitem->ht = &dd->dv_hashtab;
7179 newitem->prev = *ht_stack;
7180 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007181 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007182 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007183 }
7184 }
7185 else if (tv->v_type == VAR_LIST)
7186 {
7187 ll = tv->vval.v_list;
7188 if (ll != NULL && ll->lv_copyID != copyID)
7189 {
7190 /* Didn't see this list yet. */
7191 ll->lv_copyID = copyID;
7192 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007193 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007194 abort = set_ref_in_list(ll, copyID, ht_stack);
7195 }
7196 else
7197 {
7198 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007199 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007200 if (newitem == NULL)
7201 abort = TRUE;
7202 else
7203 {
7204 newitem->list = ll;
7205 newitem->prev = *list_stack;
7206 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007207 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007208 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007209 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007210 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007211 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007212}
7213
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007214 static void
7215partial_free(partial_T *pt, int free_dict)
7216{
7217 int i;
7218
7219 for (i = 0; i < pt->pt_argc; ++i)
7220 clear_tv(&pt->pt_argv[i]);
7221 vim_free(pt->pt_argv);
7222 if (free_dict)
7223 dict_unref(pt->pt_dict);
7224 func_unref(pt->pt_name);
7225 vim_free(pt->pt_name);
7226 vim_free(pt);
7227}
7228
7229/*
7230 * Unreference a closure: decrement the reference count and free it when it
7231 * becomes zero.
7232 */
7233 void
7234partial_unref(partial_T *pt)
7235{
7236 if (pt != NULL && --pt->pt_refcount <= 0)
7237 partial_free(pt, TRUE);
7238}
7239
Bram Moolenaard9fba312005-06-26 22:34:35 +00007240/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007241 * Allocate an empty header for a dictionary.
7242 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007243 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007244dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007245{
Bram Moolenaar33570922005-01-25 22:26:29 +00007246 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007247
Bram Moolenaar33570922005-01-25 22:26:29 +00007248 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007249 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007250 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007251 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007252 if (first_dict != NULL)
7253 first_dict->dv_used_prev = d;
7254 d->dv_used_next = first_dict;
7255 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007256 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007257
Bram Moolenaar33570922005-01-25 22:26:29 +00007258 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007259 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007260 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007261 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007262 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007263 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007264 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007265}
7266
7267/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007268 * Allocate an empty dict for a return value.
7269 * Returns OK or FAIL.
7270 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007271 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007272rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007273{
7274 dict_T *d = dict_alloc();
7275
7276 if (d == NULL)
7277 return FAIL;
7278
7279 rettv->vval.v_dict = d;
7280 rettv->v_type = VAR_DICT;
Bram Moolenaar7d2a5792016-03-28 22:30:50 +02007281 rettv->v_lock = 0;
Bram Moolenaara800b422010-06-27 01:15:55 +02007282 ++d->dv_refcount;
7283 return OK;
7284}
7285
7286
7287/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007288 * Unreference a Dictionary: decrement the reference count and free it when it
7289 * becomes zero.
7290 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007291 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007292dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007293{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007294 if (d != NULL && --d->dv_refcount <= 0)
7295 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007296}
7297
7298/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007299 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 * Ignores the reference count.
7301 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007303dict_free(
7304 dict_T *d,
7305 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007306{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007308 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007309 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007311 /* Remove the dict from the list of dicts for garbage collection. */
7312 if (d->dv_used_prev == NULL)
7313 first_dict = d->dv_used_next;
7314 else
7315 d->dv_used_prev->dv_used_next = d->dv_used_next;
7316 if (d->dv_used_next != NULL)
7317 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7318
7319 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007320 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007321 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007322 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007324 if (!HASHITEM_EMPTY(hi))
7325 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007326 /* Remove the item before deleting it, just in case there is
7327 * something recursive causing trouble. */
7328 di = HI2DI(hi);
7329 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007330 if (recurse || (di->di_tv.v_type != VAR_LIST
7331 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007332 {
7333 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7334 {
7335 partial_T *pt = di->di_tv.vval.v_partial;
7336
7337 /* We unref the partial but not the dict it refers to. */
7338 if (pt != NULL && --pt->pt_refcount == 0)
7339 partial_free(pt, FALSE);
7340 }
7341 else
7342 clear_tv(&di->di_tv);
7343 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007344 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 --todo;
7346 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007348 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007349 vim_free(d);
7350}
7351
7352/*
7353 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007354 * The "key" is copied to the new item.
7355 * Note that the value of the item "di_tv" still needs to be initialized!
7356 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007357 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007358 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007359dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007360{
Bram Moolenaar33570922005-01-25 22:26:29 +00007361 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007363 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007365 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007366 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007367 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007368 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007369 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007370}
7371
7372/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007373 * Make a copy of a Dictionary item.
7374 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007375 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007376dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377{
Bram Moolenaar33570922005-01-25 22:26:29 +00007378 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007380 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7381 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007382 if (di != NULL)
7383 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007384 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007385 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007386 copy_tv(&org->di_tv, &di->di_tv);
7387 }
7388 return di;
7389}
7390
7391/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007392 * Remove item "item" from Dictionary "dict" and free it.
7393 */
7394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007395dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396{
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398
Bram Moolenaar33570922005-01-25 22:26:29 +00007399 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007400 if (HASHITEM_EMPTY(hi))
7401 EMSG2(_(e_intern2), "dictitem_remove()");
7402 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007403 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007404 dictitem_free(item);
7405}
7406
7407/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007408 * Free a dict item. Also clears the value.
7409 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007410 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007411dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007412{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007413 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007414 if (item->di_flags & DI_FLAGS_ALLOC)
7415 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007416}
7417
7418/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007419 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7420 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007421 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 * Returns NULL when out of memory.
7423 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007424 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007425dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007426{
Bram Moolenaar33570922005-01-25 22:26:29 +00007427 dict_T *copy;
7428 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007429 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007430 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431
7432 if (orig == NULL)
7433 return NULL;
7434
7435 copy = dict_alloc();
7436 if (copy != NULL)
7437 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007438 if (copyID != 0)
7439 {
7440 orig->dv_copyID = copyID;
7441 orig->dv_copydict = copy;
7442 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007443 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007444 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007445 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007447 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007448 --todo;
7449
7450 di = dictitem_alloc(hi->hi_key);
7451 if (di == NULL)
7452 break;
7453 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007454 {
7455 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7456 copyID) == FAIL)
7457 {
7458 vim_free(di);
7459 break;
7460 }
7461 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007462 else
7463 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7464 if (dict_add(copy, di) == FAIL)
7465 {
7466 dictitem_free(di);
7467 break;
7468 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007469 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007471
Bram Moolenaare9a41262005-01-15 22:18:47 +00007472 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007473 if (todo > 0)
7474 {
7475 dict_unref(copy);
7476 copy = NULL;
7477 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007478 }
7479
7480 return copy;
7481}
7482
7483/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007485 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007486 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007487 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007488dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489{
Bram Moolenaar33570922005-01-25 22:26:29 +00007490 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491}
7492
Bram Moolenaar8c711452005-01-14 21:53:12 +00007493/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007494 * Add a number or string entry to dictionary "d".
7495 * When "str" is NULL use number "nr", otherwise use "str".
7496 * Returns FAIL when out of memory and when key already exists.
7497 */
7498 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007499dict_add_nr_str(
7500 dict_T *d,
7501 char *key,
7502 long nr,
7503 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007504{
7505 dictitem_T *item;
7506
7507 item = dictitem_alloc((char_u *)key);
7508 if (item == NULL)
7509 return FAIL;
7510 item->di_tv.v_lock = 0;
7511 if (str == NULL)
7512 {
7513 item->di_tv.v_type = VAR_NUMBER;
7514 item->di_tv.vval.v_number = nr;
7515 }
7516 else
7517 {
7518 item->di_tv.v_type = VAR_STRING;
7519 item->di_tv.vval.v_string = vim_strsave(str);
7520 }
7521 if (dict_add(d, item) == FAIL)
7522 {
7523 dictitem_free(item);
7524 return FAIL;
7525 }
7526 return OK;
7527}
7528
7529/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007530 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007531 * Returns FAIL when out of memory and when key already exists.
7532 */
7533 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007534dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007535{
7536 dictitem_T *item;
7537
7538 item = dictitem_alloc((char_u *)key);
7539 if (item == NULL)
7540 return FAIL;
7541 item->di_tv.v_lock = 0;
7542 item->di_tv.v_type = VAR_LIST;
7543 item->di_tv.vval.v_list = list;
7544 if (dict_add(d, item) == FAIL)
7545 {
7546 dictitem_free(item);
7547 return FAIL;
7548 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007549 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007550 return OK;
7551}
7552
7553/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007554 * Get the number of items in a Dictionary.
7555 */
7556 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007557dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007558{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007559 if (d == NULL)
7560 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007561 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007562}
7563
7564/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007565 * Find item "key[len]" in Dictionary "d".
7566 * If "len" is negative use strlen(key).
7567 * Returns NULL when not found.
7568 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007569 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007570dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007571{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007572#define AKEYLEN 200
7573 char_u buf[AKEYLEN];
7574 char_u *akey;
7575 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007576 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007577
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007578 if (len < 0)
7579 akey = key;
7580 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007581 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007582 tofree = akey = vim_strnsave(key, len);
7583 if (akey == NULL)
7584 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007585 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007586 else
7587 {
7588 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007589 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007590 akey = buf;
7591 }
7592
Bram Moolenaar33570922005-01-25 22:26:29 +00007593 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007594 vim_free(tofree);
7595 if (HASHITEM_EMPTY(hi))
7596 return NULL;
7597 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007598}
7599
7600/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007601 * Get a string item from a dictionary.
7602 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007603 * Returns NULL if the entry doesn't exist or out of memory.
7604 */
7605 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007606get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007607{
7608 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007609 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007610
7611 di = dict_find(d, key, -1);
7612 if (di == NULL)
7613 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007614 s = get_tv_string(&di->di_tv);
7615 if (save && s != NULL)
7616 s = vim_strsave(s);
7617 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007618}
7619
7620/*
7621 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007622 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007623 */
7624 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007625get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007626{
7627 dictitem_T *di;
7628
7629 di = dict_find(d, key, -1);
7630 if (di == NULL)
7631 return 0;
7632 return get_tv_number(&di->di_tv);
7633}
7634
7635/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 * Return an allocated string with the string representation of a Dictionary.
7637 * May return NULL.
7638 */
7639 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007640dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007641{
7642 garray_T ga;
7643 int first = TRUE;
7644 char_u *tofree;
7645 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007647 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007648 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007651 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007652 return NULL;
7653 ga_init2(&ga, (int)sizeof(char), 80);
7654 ga_append(&ga, '{');
7655
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007656 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007657 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007660 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007661 --todo;
7662
7663 if (first)
7664 first = FALSE;
7665 else
7666 ga_concat(&ga, (char_u *)", ");
7667
7668 tofree = string_quote(hi->hi_key, FALSE);
7669 if (tofree != NULL)
7670 {
7671 ga_concat(&ga, tofree);
7672 vim_free(tofree);
7673 }
7674 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007675 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007676 if (s != NULL)
7677 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007679 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007680 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007681 line_breakcheck();
7682
Bram Moolenaar8c711452005-01-14 21:53:12 +00007683 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007684 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007685 if (todo > 0)
7686 {
7687 vim_free(ga.ga_data);
7688 return NULL;
7689 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007690
7691 ga_append(&ga, '}');
7692 ga_append(&ga, NUL);
7693 return (char_u *)ga.ga_data;
7694}
7695
7696/*
7697 * Allocate a variable for a Dictionary and fill it from "*arg".
7698 * Return OK or FAIL. Returns NOTDONE for {expr}.
7699 */
7700 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007701get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007702{
Bram Moolenaar33570922005-01-25 22:26:29 +00007703 dict_T *d = NULL;
7704 typval_T tvkey;
7705 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007706 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007707 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007709 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710
7711 /*
7712 * First check if it's not a curly-braces thing: {expr}.
7713 * Must do this without evaluating, otherwise a function may be called
7714 * twice. Unfortunately this means we need to call eval1() twice for the
7715 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007718 if (*start != '}')
7719 {
7720 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7721 return FAIL;
7722 if (*start == '}')
7723 return NOTDONE;
7724 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007725
7726 if (evaluate)
7727 {
7728 d = dict_alloc();
7729 if (d == NULL)
7730 return FAIL;
7731 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007732 tvkey.v_type = VAR_UNKNOWN;
7733 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007734
7735 *arg = skipwhite(*arg + 1);
7736 while (**arg != '}' && **arg != NUL)
7737 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007738 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007739 goto failret;
7740 if (**arg != ':')
7741 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007742 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007743 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007744 goto failret;
7745 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007746 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007747 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007748 key = get_tv_string_buf_chk(&tvkey, buf);
7749 if (key == NULL || *key == NUL)
7750 {
7751 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7752 if (key != NULL)
7753 EMSG(_(e_emptykey));
7754 clear_tv(&tvkey);
7755 goto failret;
7756 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007757 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007758
7759 *arg = skipwhite(*arg + 1);
7760 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7761 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007762 if (evaluate)
7763 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007764 goto failret;
7765 }
7766 if (evaluate)
7767 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007768 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007769 if (item != NULL)
7770 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007771 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007772 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007773 clear_tv(&tv);
7774 goto failret;
7775 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007776 item = dictitem_alloc(key);
7777 clear_tv(&tvkey);
7778 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007779 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007780 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007781 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007782 if (dict_add(d, item) == FAIL)
7783 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007784 }
7785 }
7786
7787 if (**arg == '}')
7788 break;
7789 if (**arg != ',')
7790 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007791 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007792 goto failret;
7793 }
7794 *arg = skipwhite(*arg + 1);
7795 }
7796
7797 if (**arg != '}')
7798 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007799 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007800failret:
7801 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007802 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007803 return FAIL;
7804 }
7805
7806 *arg = skipwhite(*arg + 1);
7807 if (evaluate)
7808 {
7809 rettv->v_type = VAR_DICT;
7810 rettv->vval.v_dict = d;
7811 ++d->dv_refcount;
7812 }
7813
7814 return OK;
7815}
7816
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007817#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007818#endif
7819
Bram Moolenaar17a13432016-01-24 14:22:10 +01007820 static char *
7821get_var_special_name(int nr)
7822{
7823 switch (nr)
7824 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007825 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007826 case VVAL_TRUE: return "v:true";
7827 case VVAL_NONE: return "v:none";
7828 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007829 }
7830 EMSG2(_(e_intern2), "get_var_special_name()");
7831 return "42";
7832}
7833
Bram Moolenaar8c711452005-01-14 21:53:12 +00007834/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007835 * Return a string with the string representation of a variable.
7836 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007837 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007838 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007839 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007840 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007841 */
7842 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007843echo_string(
7844 typval_T *tv,
7845 char_u **tofree,
7846 char_u *numbuf,
7847 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007848{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007849 static int recurse = 0;
7850 char_u *r = NULL;
7851
Bram Moolenaar33570922005-01-25 22:26:29 +00007852 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007853 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007854 if (!did_echo_string_emsg)
7855 {
7856 /* Only give this message once for a recursive call to avoid
7857 * flooding the user with errors. And stop iterating over lists
7858 * and dicts. */
7859 did_echo_string_emsg = TRUE;
7860 EMSG(_("E724: variable nested too deep for displaying"));
7861 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007863 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007864 }
7865 ++recurse;
7866
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007867 switch (tv->v_type)
7868 {
7869 case VAR_FUNC:
7870 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007871 r = tv->vval.v_string;
7872 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007873
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007874 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007875 {
7876 partial_T *pt = tv->vval.v_partial;
7877 char_u *fname = string_quote(pt == NULL ? NULL
7878 : pt->pt_name, FALSE);
7879 garray_T ga;
7880 int i;
7881 char_u *tf;
7882
7883 ga_init2(&ga, 1, 100);
7884 ga_concat(&ga, (char_u *)"function(");
7885 if (fname != NULL)
7886 {
7887 ga_concat(&ga, fname);
7888 vim_free(fname);
7889 }
7890 if (pt != NULL && pt->pt_argc > 0)
7891 {
7892 ga_concat(&ga, (char_u *)", [");
7893 for (i = 0; i < pt->pt_argc; ++i)
7894 {
7895 if (i > 0)
7896 ga_concat(&ga, (char_u *)", ");
7897 ga_concat(&ga,
7898 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7899 vim_free(tf);
7900 }
7901 ga_concat(&ga, (char_u *)"]");
7902 }
7903 if (pt != NULL && pt->pt_dict != NULL)
7904 {
7905 typval_T dtv;
7906
7907 ga_concat(&ga, (char_u *)", ");
7908 dtv.v_type = VAR_DICT;
7909 dtv.vval.v_dict = pt->pt_dict;
7910 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7911 vim_free(tf);
7912 }
7913 ga_concat(&ga, (char_u *)")");
7914
7915 *tofree = ga.ga_data;
7916 r = *tofree;
7917 break;
7918 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007919
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007920 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007921 if (tv->vval.v_list == NULL)
7922 {
7923 *tofree = NULL;
7924 r = NULL;
7925 }
7926 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7927 {
7928 *tofree = NULL;
7929 r = (char_u *)"[...]";
7930 }
7931 else
7932 {
7933 tv->vval.v_list->lv_copyID = copyID;
7934 *tofree = list2string(tv, copyID);
7935 r = *tofree;
7936 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007937 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007938
Bram Moolenaar8c711452005-01-14 21:53:12 +00007939 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007940 if (tv->vval.v_dict == NULL)
7941 {
7942 *tofree = NULL;
7943 r = NULL;
7944 }
7945 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7946 {
7947 *tofree = NULL;
7948 r = (char_u *)"{...}";
7949 }
7950 else
7951 {
7952 tv->vval.v_dict->dv_copyID = copyID;
7953 *tofree = dict2string(tv, copyID);
7954 r = *tofree;
7955 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007956 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007957
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007958 case VAR_STRING:
7959 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007960 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007961 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007962 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007963 *tofree = NULL;
7964 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007965 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007966
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007968#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007969 *tofree = NULL;
7970 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7971 r = numbuf;
7972 break;
7973#endif
7974
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007975 case VAR_SPECIAL:
7976 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007977 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007978 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007979 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007980
Bram Moolenaar8502c702014-06-17 12:51:16 +02007981 if (--recurse == 0)
7982 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007983 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007984}
7985
7986/*
7987 * Return a string with the string representation of a variable.
7988 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7989 * "numbuf" is used for a number.
7990 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007991 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007992 */
7993 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007994tv2string(
7995 typval_T *tv,
7996 char_u **tofree,
7997 char_u *numbuf,
7998 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007999{
8000 switch (tv->v_type)
8001 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008002 case VAR_FUNC:
8003 *tofree = string_quote(tv->vval.v_string, TRUE);
8004 return *tofree;
8005 case VAR_STRING:
8006 *tofree = string_quote(tv->vval.v_string, FALSE);
8007 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008009#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008010 *tofree = NULL;
8011 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8012 return numbuf;
8013#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008014 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008015 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008016 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008017 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008018 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008019 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008020 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008021 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008022 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008024 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008025}
8026
8027/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008028 * Return string "str" in ' quotes, doubling ' characters.
8029 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008030 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008031 */
8032 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008033string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034{
Bram Moolenaar33570922005-01-25 22:26:29 +00008035 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008036 char_u *p, *r, *s;
8037
Bram Moolenaar33570922005-01-25 22:26:29 +00008038 len = (function ? 13 : 3);
8039 if (str != NULL)
8040 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008041 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008042 for (p = str; *p != NUL; mb_ptr_adv(p))
8043 if (*p == '\'')
8044 ++len;
8045 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008046 s = r = alloc(len);
8047 if (r != NULL)
8048 {
8049 if (function)
8050 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008051 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008052 r += 10;
8053 }
8054 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008055 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008056 if (str != NULL)
8057 for (p = str; *p != NUL; )
8058 {
8059 if (*p == '\'')
8060 *r++ = '\'';
8061 MB_COPY_CHAR(p, r);
8062 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008063 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008064 if (function)
8065 *r++ = ')';
8066 *r++ = NUL;
8067 }
8068 return s;
8069}
8070
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008071#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008072/*
8073 * Convert the string "text" to a floating point number.
8074 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8075 * this always uses a decimal point.
8076 * Returns the length of the text that was consumed.
8077 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008078 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008079string2float(
8080 char_u *text,
8081 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008082{
8083 char *s = (char *)text;
8084 float_T f;
8085
8086 f = strtod(s, &s);
8087 *value = f;
8088 return (int)((char_u *)s - text);
8089}
8090#endif
8091
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008092/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008093 * Get the value of an environment variable.
8094 * "arg" is pointing to the '$'. It is advanced to after the name.
8095 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008096 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008097 */
8098 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008099get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008100{
8101 char_u *string = NULL;
8102 int len;
8103 int cc;
8104 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008105 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008106
8107 ++*arg;
8108 name = *arg;
8109 len = get_env_len(arg);
8110 if (evaluate)
8111 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008112 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008113 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008114
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008115 cc = name[len];
8116 name[len] = NUL;
8117 /* first try vim_getenv(), fast for normal environment vars */
8118 string = vim_getenv(name, &mustfree);
8119 if (string != NULL && *string != NUL)
8120 {
8121 if (!mustfree)
8122 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008123 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008124 else
8125 {
8126 if (mustfree)
8127 vim_free(string);
8128
8129 /* next try expanding things like $VIM and ${HOME} */
8130 string = expand_env_save(name - 1);
8131 if (string != NULL && *string == '$')
8132 {
8133 vim_free(string);
8134 string = NULL;
8135 }
8136 }
8137 name[len] = cc;
8138
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008139 rettv->v_type = VAR_STRING;
8140 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008141 }
8142
8143 return OK;
8144}
8145
8146/*
8147 * Array with names and number of arguments of all internal functions
8148 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8149 */
8150static struct fst
8151{
8152 char *f_name; /* function name */
8153 char f_min_argc; /* minimal number of arguments */
8154 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008155 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008156 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008157} functions[] =
8158{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008159#ifdef FEAT_FLOAT
8160 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008161 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008162#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008163 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008164 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008165 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008166 {"append", 2, 2, f_append},
8167 {"argc", 0, 0, f_argc},
8168 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008169 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008170 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008171#ifdef FEAT_FLOAT
8172 {"asin", 1, 1, f_asin}, /* WJMc */
8173#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008174 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008175 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008176 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008177 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008178 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008179 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008180#ifdef FEAT_FLOAT
8181 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008182 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008183#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008185 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008186 {"bufexists", 1, 1, f_bufexists},
8187 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8188 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8189 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8190 {"buflisted", 1, 1, f_buflisted},
8191 {"bufloaded", 1, 1, f_bufloaded},
8192 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008193 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008194 {"bufwinnr", 1, 1, f_bufwinnr},
8195 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008196 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008197 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008198 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008199#ifdef FEAT_FLOAT
8200 {"ceil", 1, 1, f_ceil},
8201#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008202#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008203 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008204 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8205 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008206 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008207 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008208 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008209 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008210 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008211 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008212 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008213 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008214 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8215 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008216 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008217 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008218#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008219 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008220 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008222 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008223 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008224#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008225 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008226 {"complete_add", 1, 1, f_complete_add},
8227 {"complete_check", 0, 0, f_complete_check},
8228#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008230 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008231#ifdef FEAT_FLOAT
8232 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008233 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008234#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008235 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008236 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008237 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008238 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008239 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008240 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008241 {"diff_filler", 1, 1, f_diff_filler},
8242 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008243 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008244 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008246 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008247 {"eventhandler", 0, 0, f_eventhandler},
8248 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008249 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008250 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008251#ifdef FEAT_FLOAT
8252 {"exp", 1, 1, f_exp},
8253#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008254 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008255 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008256 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008257 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8258 {"filereadable", 1, 1, f_filereadable},
8259 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008260 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008261 {"finddir", 1, 3, f_finddir},
8262 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008263#ifdef FEAT_FLOAT
8264 {"float2nr", 1, 1, f_float2nr},
8265 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008266 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008267#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008268 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"fnamemodify", 2, 2, f_fnamemodify},
8270 {"foldclosed", 1, 1, f_foldclosed},
8271 {"foldclosedend", 1, 1, f_foldclosedend},
8272 {"foldlevel", 1, 1, f_foldlevel},
8273 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008274 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008275 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008276 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008277 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008278 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008279 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008280 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008281 {"getchar", 0, 1, f_getchar},
8282 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008283 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008284 {"getcmdline", 0, 0, f_getcmdline},
8285 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008286 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008287 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008288 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008289 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008290 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008291 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008292 {"getfsize", 1, 1, f_getfsize},
8293 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008294 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008295 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008296 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008297 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008298 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008299 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008300 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008301 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008302 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008303 {"gettabvar", 2, 3, f_gettabvar},
8304 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008305 {"getwinposx", 0, 0, f_getwinposx},
8306 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008307 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008308 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008309 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008310 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008311 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008312 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008313 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008314 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008315 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8316 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8317 {"histadd", 2, 2, f_histadd},
8318 {"histdel", 1, 2, f_histdel},
8319 {"histget", 1, 2, f_histget},
8320 {"histnr", 1, 1, f_histnr},
8321 {"hlID", 1, 1, f_hlID},
8322 {"hlexists", 1, 1, f_hlexists},
8323 {"hostname", 0, 0, f_hostname},
8324 {"iconv", 3, 3, f_iconv},
8325 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008326 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008327 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008329 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"inputrestore", 0, 0, f_inputrestore},
8331 {"inputsave", 0, 0, f_inputsave},
8332 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008333 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008334 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008335 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008336 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008337#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8338 {"isnan", 1, 1, f_isnan},
8339#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008340 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008341#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008342 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008343 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008344 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008345 {"job_start", 1, 2, f_job_start},
8346 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008347 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008348#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008349 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008350 {"js_decode", 1, 1, f_js_decode},
8351 {"js_encode", 1, 1, f_js_encode},
8352 {"json_decode", 1, 1, f_json_decode},
8353 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008354 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008356 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008357 {"libcall", 3, 3, f_libcall},
8358 {"libcallnr", 3, 3, f_libcallnr},
8359 {"line", 1, 1, f_line},
8360 {"line2byte", 1, 1, f_line2byte},
8361 {"lispindent", 1, 1, f_lispindent},
8362 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008364 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008365 {"log10", 1, 1, f_log10},
8366#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008367#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008368 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008369#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008370 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008371 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008372 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008373 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008374 {"matchadd", 2, 5, f_matchadd},
8375 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008376 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008377 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008378 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008379 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008380 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008381 {"max", 1, 1, f_max},
8382 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008383#ifdef vim_mkdir
8384 {"mkdir", 1, 3, f_mkdir},
8385#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008386 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008387#ifdef FEAT_MZSCHEME
8388 {"mzeval", 1, 1, f_mzeval},
8389#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008390 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008391 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008392 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008393 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008394#ifdef FEAT_PERL
8395 {"perleval", 1, 1, f_perleval},
8396#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008397#ifdef FEAT_FLOAT
8398 {"pow", 2, 2, f_pow},
8399#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008401 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008402 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008403#ifdef FEAT_PYTHON3
8404 {"py3eval", 1, 1, f_py3eval},
8405#endif
8406#ifdef FEAT_PYTHON
8407 {"pyeval", 1, 1, f_pyeval},
8408#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008409 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008410 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008411 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008412#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008413 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008414#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008415 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008416 {"remote_expr", 2, 3, f_remote_expr},
8417 {"remote_foreground", 1, 1, f_remote_foreground},
8418 {"remote_peek", 1, 2, f_remote_peek},
8419 {"remote_read", 1, 1, f_remote_read},
8420 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008421 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008423 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008424 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008425 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426#ifdef FEAT_FLOAT
8427 {"round", 1, 1, f_round},
8428#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008429 {"screenattr", 2, 2, f_screenattr},
8430 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008431 {"screencol", 0, 0, f_screencol},
8432 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008433 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008434 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008435 {"searchpair", 3, 7, f_searchpair},
8436 {"searchpairpos", 3, 7, f_searchpairpos},
8437 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008438 {"server2client", 2, 2, f_server2client},
8439 {"serverlist", 0, 0, f_serverlist},
8440 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008441 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008443 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008444 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008445 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008446 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008447 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008448 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008449 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008450 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008451 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008452 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008453#ifdef FEAT_CRYPT
8454 {"sha256", 1, 1, f_sha256},
8455#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008456 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008457 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008458 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008459#ifdef FEAT_FLOAT
8460 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008461 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008462#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008463 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008464 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008465 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008466 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008467 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008468#ifdef FEAT_FLOAT
8469 {"sqrt", 1, 1, f_sqrt},
8470 {"str2float", 1, 1, f_str2float},
8471#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008472 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008473 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008474 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008475#ifdef HAVE_STRFTIME
8476 {"strftime", 1, 2, f_strftime},
8477#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008478 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008479 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008480 {"strlen", 1, 1, f_strlen},
8481 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008482 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008483 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008484 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008485 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008486 {"substitute", 4, 4, f_substitute},
8487 {"synID", 3, 3, f_synID},
8488 {"synIDattr", 2, 3, f_synIDattr},
8489 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008490 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008491 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008492 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008493 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008494 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008495 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008496 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008497 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008498 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008499#ifdef FEAT_FLOAT
8500 {"tan", 1, 1, f_tan},
8501 {"tanh", 1, 1, f_tanh},
8502#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008503 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008504 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008505#ifdef FEAT_TIMERS
8506 {"timer_start", 2, 3, f_timer_start},
8507 {"timer_stop", 1, 1, f_timer_stop},
8508#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008509 {"tolower", 1, 1, f_tolower},
8510 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008511 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008512#ifdef FEAT_FLOAT
8513 {"trunc", 1, 1, f_trunc},
8514#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008515 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008516 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008517 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008518 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008519 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008520 {"virtcol", 1, 1, f_virtcol},
8521 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008522 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008523 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008524 {"win_getid", 0, 2, f_win_getid},
8525 {"win_gotoid", 1, 1, f_win_gotoid},
8526 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8527 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008528 {"winbufnr", 1, 1, f_winbufnr},
8529 {"wincol", 0, 0, f_wincol},
8530 {"winheight", 1, 1, f_winheight},
8531 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008532 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008533 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008534 {"winrestview", 1, 1, f_winrestview},
8535 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008536 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008537 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008538 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008539 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008540};
8541
8542#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8543
8544/*
8545 * Function given to ExpandGeneric() to obtain the list of internal
8546 * or user defined function names.
8547 */
8548 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008549get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008550{
8551 static int intidx = -1;
8552 char_u *name;
8553
8554 if (idx == 0)
8555 intidx = -1;
8556 if (intidx < 0)
8557 {
8558 name = get_user_func_name(xp, idx);
8559 if (name != NULL)
8560 return name;
8561 }
8562 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8563 {
8564 STRCPY(IObuff, functions[intidx].f_name);
8565 STRCAT(IObuff, "(");
8566 if (functions[intidx].f_max_argc == 0)
8567 STRCAT(IObuff, ")");
8568 return IObuff;
8569 }
8570
8571 return NULL;
8572}
8573
8574/*
8575 * Function given to ExpandGeneric() to obtain the list of internal or
8576 * user defined variable or function names.
8577 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008579get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008580{
8581 static int intidx = -1;
8582 char_u *name;
8583
8584 if (idx == 0)
8585 intidx = -1;
8586 if (intidx < 0)
8587 {
8588 name = get_function_name(xp, idx);
8589 if (name != NULL)
8590 return name;
8591 }
8592 return get_user_var_name(xp, ++intidx);
8593}
8594
8595#endif /* FEAT_CMDL_COMPL */
8596
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008597#if defined(EBCDIC) || defined(PROTO)
8598/*
8599 * Compare struct fst by function name.
8600 */
8601 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008602compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008603{
8604 struct fst *p1 = (struct fst *)s1;
8605 struct fst *p2 = (struct fst *)s2;
8606
8607 return STRCMP(p1->f_name, p2->f_name);
8608}
8609
8610/*
8611 * Sort the function table by function name.
8612 * The sorting of the table above is ASCII dependant.
8613 * On machines using EBCDIC we have to sort it.
8614 */
8615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008616sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008617{
8618 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8619
8620 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8621}
8622#endif
8623
8624
Bram Moolenaar071d4272004-06-13 20:20:40 +00008625/*
8626 * Find internal function in table above.
8627 * Return index, or -1 if not found
8628 */
8629 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008630find_internal_func(
8631 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008632{
8633 int first = 0;
8634 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8635 int cmp;
8636 int x;
8637
8638 /*
8639 * Find the function name in the table. Binary search.
8640 */
8641 while (first <= last)
8642 {
8643 x = first + ((unsigned)(last - first) >> 1);
8644 cmp = STRCMP(name, functions[x].f_name);
8645 if (cmp < 0)
8646 last = x - 1;
8647 else if (cmp > 0)
8648 first = x + 1;
8649 else
8650 return x;
8651 }
8652 return -1;
8653}
8654
8655/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008656 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8657 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008658 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8659 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008660 */
8661 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008662deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663{
Bram Moolenaar33570922005-01-25 22:26:29 +00008664 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008665 int cc;
8666
Bram Moolenaar65639032016-03-16 21:40:30 +01008667 if (partialp != NULL)
8668 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008669
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008670 cc = name[*lenp];
8671 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008672 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008675 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008676 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008677 {
8678 *lenp = 0;
8679 return (char_u *)""; /* just in case */
8680 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008681 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008682 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008683 }
8684
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008685 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8686 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008687 partial_T *pt = v->di_tv.vval.v_partial;
8688
8689 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008690 {
8691 *lenp = 0;
8692 return (char_u *)""; /* just in case */
8693 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008694 if (partialp != NULL)
8695 *partialp = pt;
8696 *lenp = (int)STRLEN(pt->pt_name);
8697 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008698 }
8699
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008700 return name;
8701}
8702
8703/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008704 * Allocate a variable for the result of a function.
8705 * Return OK or FAIL.
8706 */
8707 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008708get_func_tv(
8709 char_u *name, /* name of the function */
8710 int len, /* length of "name" */
8711 typval_T *rettv,
8712 char_u **arg, /* argument, pointing to the '(' */
8713 linenr_T firstline, /* first line of range */
8714 linenr_T lastline, /* last line of range */
8715 int *doesrange, /* return: function handled range */
8716 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008717 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008718 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008719{
8720 char_u *argp;
8721 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008722 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008723 int argcount = 0; /* number of arguments found */
8724
8725 /*
8726 * Get the arguments.
8727 */
8728 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008729 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008730 {
8731 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8732 if (*argp == ')' || *argp == ',' || *argp == NUL)
8733 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008734 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8735 {
8736 ret = FAIL;
8737 break;
8738 }
8739 ++argcount;
8740 if (*argp != ',')
8741 break;
8742 }
8743 if (*argp == ')')
8744 ++argp;
8745 else
8746 ret = FAIL;
8747
8748 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008749 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008750 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008751 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008752 {
8753 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008754 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008756 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008757 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008758
8759 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008760 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761
8762 *arg = skipwhite(argp);
8763 return ret;
8764}
8765
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008766#define ERROR_UNKNOWN 0
8767#define ERROR_TOOMANY 1
8768#define ERROR_TOOFEW 2
8769#define ERROR_SCRIPT 3
8770#define ERROR_DICT 4
8771#define ERROR_NONE 5
8772#define ERROR_OTHER 6
8773#define FLEN_FIXED 40
8774
8775/*
8776 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8777 * Change <SNR>123_name() to K_SNR 123_name().
8778 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8779 * (slow).
8780 */
8781 static char_u *
8782fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8783{
8784 int llen;
8785 char_u *fname;
8786 int i;
8787
8788 llen = eval_fname_script(name);
8789 if (llen > 0)
8790 {
8791 fname_buf[0] = K_SPECIAL;
8792 fname_buf[1] = KS_EXTRA;
8793 fname_buf[2] = (int)KE_SNR;
8794 i = 3;
8795 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8796 {
8797 if (current_SID <= 0)
8798 *error = ERROR_SCRIPT;
8799 else
8800 {
8801 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8802 i = (int)STRLEN(fname_buf);
8803 }
8804 }
8805 if (i + STRLEN(name + llen) < FLEN_FIXED)
8806 {
8807 STRCPY(fname_buf + i, name + llen);
8808 fname = fname_buf;
8809 }
8810 else
8811 {
8812 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8813 if (fname == NULL)
8814 *error = ERROR_OTHER;
8815 else
8816 {
8817 *tofree = fname;
8818 mch_memmove(fname, fname_buf, (size_t)i);
8819 STRCPY(fname + i, name + llen);
8820 }
8821 }
8822 }
8823 else
8824 fname = name;
8825 return fname;
8826}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008827
8828/*
8829 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008830 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008831 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008832 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008833 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008834call_func(
8835 char_u *funcname, /* name of the function */
8836 int len, /* length of "name" */
8837 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008838 int argcount_in, /* number of "argvars" */
8839 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008840 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008841 linenr_T firstline, /* first line of range */
8842 linenr_T lastline, /* last line of range */
8843 int *doesrange, /* return: function handled range */
8844 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008845 partial_T *partial, /* optional, can be NULL */
8846 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847{
8848 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 int error = ERROR_NONE;
8850 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008853 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008854 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008855 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008856 int argcount = argcount_in;
8857 typval_T *argvars = argvars_in;
8858 dict_T *selfdict = selfdict_in;
8859 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8860 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008861
8862 /* Make a copy of the name, if it comes from a funcref variable it could
8863 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008864 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008865 if (name == NULL)
8866 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008868 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008869
8870 *doesrange = FALSE;
8871
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008872 if (partial != NULL)
8873 {
8874 if (partial->pt_dict != NULL)
8875 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008876 /* When the function has a partial with a dict and there is a dict
8877 * argument, use the dict argument. That is backwards compatible.
8878 */
8879 if (selfdict_in == NULL)
8880 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008881 }
8882 if (error == ERROR_NONE && partial->pt_argc > 0)
8883 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008884 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8885 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8886 for (i = 0; i < argcount_in; ++i)
8887 argv[i + argv_clear] = argvars_in[i];
8888 argvars = argv;
8889 argcount = partial->pt_argc + argcount_in;
8890 }
8891 }
8892
Bram Moolenaar071d4272004-06-13 20:20:40 +00008893
8894 /* execute the function if no errors detected and executing */
8895 if (evaluate && error == ERROR_NONE)
8896 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008897 char_u *rfname = fname;
8898
8899 /* Ignore "g:" before a function name. */
8900 if (fname[0] == 'g' && fname[1] == ':')
8901 rfname = fname + 2;
8902
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008903 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8904 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 error = ERROR_UNKNOWN;
8906
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008907 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008908 {
8909 /*
8910 * User defined function.
8911 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008912 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008913
Bram Moolenaar071d4272004-06-13 20:20:40 +00008914#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008915 /* Trigger FuncUndefined event, may load the function. */
8916 if (fp == NULL
8917 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008918 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008919 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008920 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008921 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008922 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008923 }
8924#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008925 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008926 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008927 {
8928 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008929 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008930 }
8931
Bram Moolenaar071d4272004-06-13 20:20:40 +00008932 if (fp != NULL)
8933 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008934 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008936 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008938 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008939 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008940 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008941 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008942 else
8943 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008944 int did_save_redo = FALSE;
8945
Bram Moolenaar071d4272004-06-13 20:20:40 +00008946 /*
8947 * Call the user function.
8948 * Save and restore search patterns, script variables and
8949 * redo buffer.
8950 */
8951 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008952#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008953 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008954#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008955 {
8956 saveRedobuff();
8957 did_save_redo = TRUE;
8958 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008959 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008960 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008961 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008962 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8963 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8964 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008965 /* Function was unreferenced while being used, free it
8966 * now. */
8967 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008968 if (did_save_redo)
8969 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008970 restore_search_patterns();
8971 error = ERROR_NONE;
8972 }
8973 }
8974 }
8975 else
8976 {
8977 /*
8978 * Find the function name in the table, call its implementation.
8979 */
8980 i = find_internal_func(fname);
8981 if (i >= 0)
8982 {
8983 if (argcount < functions[i].f_min_argc)
8984 error = ERROR_TOOFEW;
8985 else if (argcount > functions[i].f_max_argc)
8986 error = ERROR_TOOMANY;
8987 else
8988 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008989 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008990 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008991 error = ERROR_NONE;
8992 }
8993 }
8994 }
8995 /*
8996 * The function call (or "FuncUndefined" autocommand sequence) might
8997 * have been aborted by an error, an interrupt, or an explicitly thrown
8998 * exception that has not been caught so far. This situation can be
8999 * tested for by calling aborting(). For an error in an internal
9000 * function or for the "E132" error in call_user_func(), however, the
9001 * throw point at which the "force_abort" flag (temporarily reset by
9002 * emsg()) is normally updated has not been reached yet. We need to
9003 * update that flag first to make aborting() reliable.
9004 */
9005 update_force_abort();
9006 }
9007 if (error == ERROR_NONE)
9008 ret = OK;
9009
9010 /*
9011 * Report an error unless the argument evaluation or function call has been
9012 * cancelled due to an aborting error, an interrupt, or an exception.
9013 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009014 if (!aborting())
9015 {
9016 switch (error)
9017 {
9018 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009019 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009020 break;
9021 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009022 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009023 break;
9024 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009025 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009026 name);
9027 break;
9028 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009029 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009030 name);
9031 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009033 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009034 name);
9035 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009036 }
9037 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009038
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009039 while (argv_clear > 0)
9040 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009041 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009042 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009043
9044 return ret;
9045}
9046
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009047/*
9048 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009049 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009050 */
9051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009052emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009053{
9054 char_u *p;
9055
9056 if (*name == K_SPECIAL)
9057 p = concat_str((char_u *)"<SNR>", name + 3);
9058 else
9059 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009060 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009061 if (p != name)
9062 vim_free(p);
9063}
9064
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009065/*
9066 * Return TRUE for a non-zero Number and a non-empty String.
9067 */
9068 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009069non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009070{
9071 return ((argvars[0].v_type == VAR_NUMBER
9072 && argvars[0].vval.v_number != 0)
9073 || (argvars[0].v_type == VAR_STRING
9074 && argvars[0].vval.v_string != NULL
9075 && *argvars[0].vval.v_string != NUL));
9076}
9077
Bram Moolenaar071d4272004-06-13 20:20:40 +00009078/*********************************************
9079 * Implementation of the built-in functions
9080 */
9081
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009082#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009083static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009084
9085/*
9086 * Get the float value of "argvars[0]" into "f".
9087 * Returns FAIL when the argument is not a Number or Float.
9088 */
9089 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009090get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009091{
9092 if (argvars[0].v_type == VAR_FLOAT)
9093 {
9094 *f = argvars[0].vval.v_float;
9095 return OK;
9096 }
9097 if (argvars[0].v_type == VAR_NUMBER)
9098 {
9099 *f = (float_T)argvars[0].vval.v_number;
9100 return OK;
9101 }
9102 EMSG(_("E808: Number or Float required"));
9103 return FAIL;
9104}
9105
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009106/*
9107 * "abs(expr)" function
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009111{
9112 if (argvars[0].v_type == VAR_FLOAT)
9113 {
9114 rettv->v_type = VAR_FLOAT;
9115 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9116 }
9117 else
9118 {
9119 varnumber_T n;
9120 int error = FALSE;
9121
9122 n = get_tv_number_chk(&argvars[0], &error);
9123 if (error)
9124 rettv->vval.v_number = -1;
9125 else if (n > 0)
9126 rettv->vval.v_number = n;
9127 else
9128 rettv->vval.v_number = -n;
9129 }
9130}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009131
9132/*
9133 * "acos()" function
9134 */
9135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009136f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009137{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009138 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009139
9140 rettv->v_type = VAR_FLOAT;
9141 if (get_float_arg(argvars, &f) == OK)
9142 rettv->vval.v_float = acos(f);
9143 else
9144 rettv->vval.v_float = 0.0;
9145}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009146#endif
9147
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009150 */
9151 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009152f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153{
Bram Moolenaar33570922005-01-25 22:26:29 +00009154 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009155
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009156 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009157 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009158 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009159 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009160 && !tv_check_lock(l->lv_lock,
9161 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009162 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009163 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009164 }
9165 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009166 EMSG(_(e_listreq));
9167}
9168
9169/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009170 * "alloc_fail(id, countdown, repeat)" function
9171 */
9172 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009173f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009174{
9175 if (argvars[0].v_type != VAR_NUMBER
9176 || argvars[0].vval.v_number <= 0
9177 || argvars[1].v_type != VAR_NUMBER
9178 || argvars[1].vval.v_number < 0
9179 || argvars[2].v_type != VAR_NUMBER)
9180 EMSG(_(e_invarg));
9181 else
9182 {
9183 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009184 if (alloc_fail_id >= aid_last)
9185 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009186 alloc_fail_countdown = argvars[1].vval.v_number;
9187 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009188 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009189 }
9190}
9191
9192/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009193 * "and(expr, expr)" function
9194 */
9195 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009196f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009197{
9198 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9199 & get_tv_number_chk(&argvars[1], NULL);
9200}
9201
9202/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009203 * "append(lnum, string/list)" function
9204 */
9205 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009206f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009207{
9208 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009209 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009210 list_T *l = NULL;
9211 listitem_T *li = NULL;
9212 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009213 long added = 0;
9214
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009215 /* When coming here from Insert mode, sync undo, so that this can be
9216 * undone separately from what was previously inserted. */
9217 if (u_sync_once == 2)
9218 {
9219 u_sync_once = 1; /* notify that u_sync() was called */
9220 u_sync(TRUE);
9221 }
9222
Bram Moolenaar0d660222005-01-07 21:51:51 +00009223 lnum = get_tv_lnum(argvars);
9224 if (lnum >= 0
9225 && lnum <= curbuf->b_ml.ml_line_count
9226 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009227 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009229 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009230 l = argvars[1].vval.v_list;
9231 if (l == NULL)
9232 return;
9233 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009234 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009235 for (;;)
9236 {
9237 if (l == NULL)
9238 tv = &argvars[1]; /* append a string */
9239 else if (li == NULL)
9240 break; /* end of list */
9241 else
9242 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009243 line = get_tv_string_chk(tv);
9244 if (line == NULL) /* type error */
9245 {
9246 rettv->vval.v_number = 1; /* Failed */
9247 break;
9248 }
9249 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009250 ++added;
9251 if (l == NULL)
9252 break;
9253 li = li->li_next;
9254 }
9255
9256 appended_lines_mark(lnum, added);
9257 if (curwin->w_cursor.lnum > lnum)
9258 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009259 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009260 else
9261 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009262}
9263
9264/*
9265 * "argc()" function
9266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009268f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009270 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009271}
9272
9273/*
9274 * "argidx()" function
9275 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009277f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009279 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009280}
9281
9282/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009283 * "arglistid()" function
9284 */
9285 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009286f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009287{
9288 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009289
9290 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009291 wp = find_tabwin(&argvars[0], &argvars[1]);
9292 if (wp != NULL)
9293 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009294}
9295
9296/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009297 * "argv(nr)" function
9298 */
9299 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009300f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009301{
9302 int idx;
9303
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009304 if (argvars[0].v_type != VAR_UNKNOWN)
9305 {
9306 idx = get_tv_number_chk(&argvars[0], NULL);
9307 if (idx >= 0 && idx < ARGCOUNT)
9308 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9309 else
9310 rettv->vval.v_string = NULL;
9311 rettv->v_type = VAR_STRING;
9312 }
9313 else if (rettv_list_alloc(rettv) == OK)
9314 for (idx = 0; idx < ARGCOUNT; ++idx)
9315 list_append_string(rettv->vval.v_list,
9316 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009317}
9318
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009319static void prepare_assert_error(garray_T*gap);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009320static 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 +01009321static void assert_error(garray_T *gap);
9322static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009323
9324/*
9325 * Prepare "gap" for an assert error and add the sourcing position.
9326 */
9327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009328prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009329{
9330 char buf[NUMBUFLEN];
9331
9332 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009333 if (sourcing_name != NULL)
9334 {
9335 ga_concat(gap, sourcing_name);
9336 if (sourcing_lnum > 0)
9337 ga_concat(gap, (char_u *)" ");
9338 }
9339 if (sourcing_lnum > 0)
9340 {
9341 sprintf(buf, "line %ld", (long)sourcing_lnum);
9342 ga_concat(gap, (char_u *)buf);
9343 }
9344 if (sourcing_name != NULL || sourcing_lnum > 0)
9345 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009346}
9347
9348/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009349 * Append "str" to "gap", escaping unprintable characters.
9350 * Changes NL to \n, CR to \r, etc.
9351 */
9352 static void
9353ga_concat_esc(garray_T *gap, char_u *str)
9354{
9355 char_u *p;
9356 char_u buf[NUMBUFLEN];
9357
Bram Moolenaarf1551962016-03-15 12:55:58 +01009358 if (str == NULL)
9359 {
9360 ga_concat(gap, (char_u *)"NULL");
9361 return;
9362 }
9363
Bram Moolenaar23689172016-02-15 22:37:37 +01009364 for (p = str; *p != NUL; ++p)
9365 switch (*p)
9366 {
9367 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9368 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9369 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9370 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9371 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9372 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9373 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9374 default:
9375 if (*p < ' ')
9376 {
9377 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9378 ga_concat(gap, buf);
9379 }
9380 else
9381 ga_append(gap, *p);
9382 break;
9383 }
9384}
9385
9386/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009387 * Fill "gap" with information about an assert error.
9388 */
9389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009390fill_assert_error(
9391 garray_T *gap,
9392 typval_T *opt_msg_tv,
9393 char_u *exp_str,
9394 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009395 typval_T *got_tv,
9396 int is_match)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009397{
9398 char_u numbuf[NUMBUFLEN];
9399 char_u *tofree;
9400
9401 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9402 {
9403 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9404 vim_free(tofree);
9405 }
9406 else
9407 {
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009408 if (is_match)
9409 ga_concat(gap, (char_u *)"Pattern ");
9410 else
9411 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009412 if (exp_str == NULL)
9413 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009414 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009415 vim_free(tofree);
9416 }
9417 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009418 ga_concat_esc(gap, exp_str);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009419 if (is_match)
9420 ga_concat(gap, (char_u *)" does not match ");
9421 else
9422 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009423 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009424 vim_free(tofree);
9425 }
9426}
Bram Moolenaar43345542015-11-29 17:35:35 +01009427
9428/*
9429 * Add an assert error to v:errors.
9430 */
9431 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009432assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009433{
9434 struct vimvar *vp = &vimvars[VV_ERRORS];
9435
9436 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9437 /* Make sure v:errors is a list. */
9438 set_vim_var_list(VV_ERRORS, list_alloc());
9439 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9440}
9441
Bram Moolenaar43345542015-11-29 17:35:35 +01009442/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009443 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009444 */
9445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009446f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009447{
9448 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009449
9450 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9451 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009452 prepare_assert_error(&ga);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009453 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9454 FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009455 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009456 ga_clear(&ga);
9457 }
9458}
9459
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009460/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009461 * "assert_exception(string[, msg])" function
9462 */
9463 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009464f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009465{
9466 garray_T ga;
9467 char *error;
9468
9469 error = (char *)get_tv_string_chk(&argvars[0]);
9470 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9471 {
9472 prepare_assert_error(&ga);
9473 ga_concat(&ga, (char_u *)"v:exception is not set");
9474 assert_error(&ga);
9475 ga_clear(&ga);
9476 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009477 else if (error != NULL
9478 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009479 {
9480 prepare_assert_error(&ga);
9481 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009482 &vimvars[VV_EXCEPTION].vv_tv, FALSE);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009483 assert_error(&ga);
9484 ga_clear(&ga);
9485 }
9486}
9487
9488/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009489 * "assert_fails(cmd [, error])" function
9490 */
9491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009492f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009493{
9494 char_u *cmd = get_tv_string_chk(&argvars[0]);
9495 garray_T ga;
9496
9497 called_emsg = FALSE;
9498 suppress_errthrow = TRUE;
9499 emsg_silent = TRUE;
9500 do_cmdline_cmd(cmd);
9501 if (!called_emsg)
9502 {
9503 prepare_assert_error(&ga);
9504 ga_concat(&ga, (char_u *)"command did not fail: ");
9505 ga_concat(&ga, cmd);
9506 assert_error(&ga);
9507 ga_clear(&ga);
9508 }
9509 else if (argvars[1].v_type != VAR_UNKNOWN)
9510 {
9511 char_u buf[NUMBUFLEN];
9512 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9513
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009514 if (error == NULL
9515 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009516 {
9517 prepare_assert_error(&ga);
9518 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009519 &vimvars[VV_ERRMSG].vv_tv, FALSE);
Bram Moolenaara260b872016-01-15 20:48:22 +01009520 assert_error(&ga);
9521 ga_clear(&ga);
9522 }
9523 }
9524
9525 called_emsg = FALSE;
9526 suppress_errthrow = FALSE;
9527 emsg_silent = FALSE;
9528 emsg_on_display = FALSE;
9529 set_vim_var_string(VV_ERRMSG, NULL, 0);
9530}
9531
9532/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009533 * Common for assert_true() and assert_false().
9534 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009536assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009537{
9538 int error = FALSE;
9539 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009540
Bram Moolenaar37127922016-02-06 20:29:28 +01009541 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009542 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009543 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009544 if (argvars[0].v_type != VAR_NUMBER
9545 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9546 || error)
9547 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009548 prepare_assert_error(&ga);
9549 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009550 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009551 NULL, &argvars[0], FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009552 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009553 ga_clear(&ga);
9554 }
9555}
9556
9557/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009558 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009559 */
9560 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009561f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009562{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009563 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009564}
9565
9566/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009567 * "assert_match(pattern, actual[, msg])" function
9568 */
9569 static void
9570f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9571{
9572 garray_T ga;
9573 char_u buf1[NUMBUFLEN];
9574 char_u buf2[NUMBUFLEN];
9575 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9576 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9577
9578 if (!pattern_match(pat, text, FALSE))
9579 {
9580 prepare_assert_error(&ga);
9581 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9582 TRUE);
9583 assert_error(&ga);
9584 ga_clear(&ga);
9585 }
9586}
9587
9588/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009589 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009590 */
9591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009593{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009594 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009595}
9596
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009597#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009599 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009600 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009601 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009602f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009603{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009604 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009605
9606 rettv->v_type = VAR_FLOAT;
9607 if (get_float_arg(argvars, &f) == OK)
9608 rettv->vval.v_float = asin(f);
9609 else
9610 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009611}
9612
9613/*
9614 * "atan()" function
9615 */
9616 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009617f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009618{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009619 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009620
9621 rettv->v_type = VAR_FLOAT;
9622 if (get_float_arg(argvars, &f) == OK)
9623 rettv->vval.v_float = atan(f);
9624 else
9625 rettv->vval.v_float = 0.0;
9626}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009627
9628/*
9629 * "atan2()" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009633{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009634 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009635
9636 rettv->v_type = VAR_FLOAT;
9637 if (get_float_arg(argvars, &fx) == OK
9638 && get_float_arg(&argvars[1], &fy) == OK)
9639 rettv->vval.v_float = atan2(fx, fy);
9640 else
9641 rettv->vval.v_float = 0.0;
9642}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009643#endif
9644
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645/*
9646 * "browse(save, title, initdir, default)" function
9647 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009649f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650{
9651#ifdef FEAT_BROWSE
9652 int save;
9653 char_u *title;
9654 char_u *initdir;
9655 char_u *defname;
9656 char_u buf[NUMBUFLEN];
9657 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009660 save = get_tv_number_chk(&argvars[0], &error);
9661 title = get_tv_string_chk(&argvars[1]);
9662 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9663 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009664
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009665 if (error || title == NULL || initdir == NULL || defname == NULL)
9666 rettv->vval.v_string = NULL;
9667 else
9668 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009669 do_browse(save ? BROWSE_SAVE : 0,
9670 title, defname, NULL, initdir, NULL, curbuf);
9671#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009673#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009674 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009675}
9676
9677/*
9678 * "browsedir(title, initdir)" function
9679 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009680 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009681f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009682{
9683#ifdef FEAT_BROWSE
9684 char_u *title;
9685 char_u *initdir;
9686 char_u buf[NUMBUFLEN];
9687
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009688 title = get_tv_string_chk(&argvars[0]);
9689 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009690
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009691 if (title == NULL || initdir == NULL)
9692 rettv->vval.v_string = NULL;
9693 else
9694 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009695 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009699 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009700}
9701
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009702static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009703
Bram Moolenaar071d4272004-06-13 20:20:40 +00009704/*
9705 * Find a buffer by number or exact name.
9706 */
9707 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009708find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709{
9710 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009711
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009712 if (avar->v_type == VAR_NUMBER)
9713 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009714 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009715 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009716 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009717 if (buf == NULL)
9718 {
9719 /* No full path name match, try a match with a URL or a "nofile"
9720 * buffer, these don't use the full path. */
9721 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9722 if (buf->b_fname != NULL
9723 && (path_with_url(buf->b_fname)
9724#ifdef FEAT_QUICKFIX
9725 || bt_nofile(buf)
9726#endif
9727 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009728 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009729 break;
9730 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009731 }
9732 return buf;
9733}
9734
9735/*
9736 * "bufexists(expr)" function
9737 */
9738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009739f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009741 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009742}
9743
9744/*
9745 * "buflisted(expr)" function
9746 */
9747 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009748f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009749{
9750 buf_T *buf;
9751
9752 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009753 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009754}
9755
9756/*
9757 * "bufloaded(expr)" function
9758 */
9759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009760f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009761{
9762 buf_T *buf;
9763
9764 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009765 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009766}
9767
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009768 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009769buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009770{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009771 int save_magic;
9772 char_u *save_cpo;
9773 buf_T *buf;
9774
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9776 save_magic = p_magic;
9777 p_magic = TRUE;
9778 save_cpo = p_cpo;
9779 p_cpo = (char_u *)"";
9780
9781 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009782 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009783
9784 p_magic = save_magic;
9785 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009786 return buf;
9787}
9788
9789/*
9790 * Get buffer by number or pattern.
9791 */
9792 static buf_T *
9793get_buf_tv(typval_T *tv, int curtab_only)
9794{
9795 char_u *name = tv->vval.v_string;
9796 buf_T *buf;
9797
9798 if (tv->v_type == VAR_NUMBER)
9799 return buflist_findnr((int)tv->vval.v_number);
9800 if (tv->v_type != VAR_STRING)
9801 return NULL;
9802 if (name == NULL || *name == NUL)
9803 return curbuf;
9804 if (name[0] == '$' && name[1] == NUL)
9805 return lastbuf;
9806
9807 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009808
9809 /* If not found, try expanding the name, like done for bufexists(). */
9810 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009811 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009812
9813 return buf;
9814}
9815
9816/*
9817 * "bufname(expr)" function
9818 */
9819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009820f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009821{
9822 buf_T *buf;
9823
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009824 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009825 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009826 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009831 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009832 --emsg_off;
9833}
9834
9835/*
9836 * "bufnr(expr)" function
9837 */
9838 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009839f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009840{
9841 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009842 int error = FALSE;
9843 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009845 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009846 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009847 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009848 --emsg_off;
9849
9850 /* If the buffer isn't found and the second argument is not zero create a
9851 * new buffer. */
9852 if (buf == NULL
9853 && argvars[1].v_type != VAR_UNKNOWN
9854 && get_tv_number_chk(&argvars[1], &error) != 0
9855 && !error
9856 && (name = get_tv_string_chk(&argvars[0])) != NULL
9857 && !error)
9858 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9859
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009863 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009864}
9865
9866/*
9867 * "bufwinnr(nr)" function
9868 */
9869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009870f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009871{
9872#ifdef FEAT_WINDOWS
9873 win_T *wp;
9874 int winnr = 0;
9875#endif
9876 buf_T *buf;
9877
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009878 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009880 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009881#ifdef FEAT_WINDOWS
9882 for (wp = firstwin; wp; wp = wp->w_next)
9883 {
9884 ++winnr;
9885 if (wp->w_buffer == buf)
9886 break;
9887 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009890 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009891#endif
9892 --emsg_off;
9893}
9894
9895/*
9896 * "byte2line(byte)" function
9897 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009899f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009900{
9901#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009902 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009903#else
9904 long boff = 0;
9905
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009906 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009910 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009911 (linenr_T)0, &boff);
9912#endif
9913}
9914
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009915 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009916byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009917{
9918#ifdef FEAT_MBYTE
9919 char_u *t;
9920#endif
9921 char_u *str;
9922 long idx;
9923
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009924 str = get_tv_string_chk(&argvars[0]);
9925 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009926 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009927 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009928 return;
9929
9930#ifdef FEAT_MBYTE
9931 t = str;
9932 for ( ; idx > 0; idx--)
9933 {
9934 if (*t == NUL) /* EOL reached */
9935 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009936 if (enc_utf8 && comp)
9937 t += utf_ptr2len(t);
9938 else
9939 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009940 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009941 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009942#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009943 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009944 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009945#endif
9946}
9947
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009948/*
9949 * "byteidx()" function
9950 */
9951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009952f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009953{
9954 byteidx(argvars, rettv, FALSE);
9955}
9956
9957/*
9958 * "byteidxcomp()" function
9959 */
9960 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009961f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009962{
9963 byteidx(argvars, rettv, TRUE);
9964}
9965
Bram Moolenaardb913952012-06-29 12:54:53 +02009966 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009967func_call(
9968 char_u *name,
9969 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009970 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009971 dict_T *selfdict,
9972 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009973{
9974 listitem_T *item;
9975 typval_T argv[MAX_FUNC_ARGS + 1];
9976 int argc = 0;
9977 int dummy;
9978 int r = 0;
9979
9980 for (item = args->vval.v_list->lv_first; item != NULL;
9981 item = item->li_next)
9982 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009983 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009984 {
9985 EMSG(_("E699: Too many arguments"));
9986 break;
9987 }
9988 /* Make a copy of each argument. This is needed to be able to set
9989 * v_lock to VAR_FIXED in the copy without changing the original list.
9990 */
9991 copy_tv(&item->li_tv, &argv[argc++]);
9992 }
9993
9994 if (item == NULL)
9995 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9996 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009997 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009998
9999 /* Free the arguments. */
10000 while (argc > 0)
10001 clear_tv(&argv[--argc]);
10002
10003 return r;
10004}
10005
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010006/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010007 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010008 */
10009 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010010f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010011{
10012 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010013 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010014 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010015
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010016 if (argvars[1].v_type != VAR_LIST)
10017 {
10018 EMSG(_(e_listreq));
10019 return;
10020 }
10021 if (argvars[1].vval.v_list == NULL)
10022 return;
10023
10024 if (argvars[0].v_type == VAR_FUNC)
10025 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010026 else if (argvars[0].v_type == VAR_PARTIAL)
10027 {
10028 partial = argvars[0].vval.v_partial;
10029 func = partial->pt_name;
10030 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010031 else
10032 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010033 if (*func == NUL)
10034 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010035
Bram Moolenaare9a41262005-01-15 22:18:47 +000010036 if (argvars[2].v_type != VAR_UNKNOWN)
10037 {
10038 if (argvars[2].v_type != VAR_DICT)
10039 {
10040 EMSG(_(e_dictreq));
10041 return;
10042 }
10043 selfdict = argvars[2].vval.v_dict;
10044 }
10045
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010046 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010047}
10048
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010049#ifdef FEAT_FLOAT
10050/*
10051 * "ceil({float})" function
10052 */
10053 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010054f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010055{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010056 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010057
10058 rettv->v_type = VAR_FLOAT;
10059 if (get_float_arg(argvars, &f) == OK)
10060 rettv->vval.v_float = ceil(f);
10061 else
10062 rettv->vval.v_float = 0.0;
10063}
10064#endif
10065
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010066#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010067/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010068 * "ch_close()" function
10069 */
10070 static void
10071f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10072{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010073 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010074
10075 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010076 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010077 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010078 channel_clear(channel);
10079 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010080}
10081
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010082/*
10083 * "ch_getbufnr()" function
10084 */
10085 static void
10086f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10087{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010088 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010089
10090 rettv->vval.v_number = -1;
10091 if (channel != NULL)
10092 {
10093 char_u *what = get_tv_string(&argvars[1]);
10094 int part;
10095
10096 if (STRCMP(what, "err") == 0)
10097 part = PART_ERR;
10098 else if (STRCMP(what, "out") == 0)
10099 part = PART_OUT;
10100 else if (STRCMP(what, "in") == 0)
10101 part = PART_IN;
10102 else
10103 part = PART_SOCK;
10104 if (channel->ch_part[part].ch_buffer != NULL)
10105 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10106 }
10107}
10108
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010109/*
10110 * "ch_getjob()" function
10111 */
10112 static void
10113f_ch_getjob(typval_T *argvars, typval_T *rettv)
10114{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010115 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010116
10117 if (channel != NULL)
10118 {
10119 rettv->v_type = VAR_JOB;
10120 rettv->vval.v_job = channel->ch_job;
10121 if (channel->ch_job != NULL)
10122 ++channel->ch_job->jv_refcount;
10123 }
10124}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010125
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010126/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010127 * "ch_info()" function
10128 */
10129 static void
10130f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10131{
10132 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10133
10134 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10135 channel_info(channel, rettv->vval.v_dict);
10136}
10137
10138/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010139 * "ch_log()" function
10140 */
10141 static void
10142f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10143{
10144 char_u *msg = get_tv_string(&argvars[0]);
10145 channel_T *channel = NULL;
10146
10147 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010148 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010149
10150 ch_log(channel, (char *)msg);
10151}
10152
10153/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010154 * "ch_logfile()" function
10155 */
10156 static void
10157f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10158{
10159 char_u *fname;
10160 char_u *opt = (char_u *)"";
10161 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010162
10163 fname = get_tv_string(&argvars[0]);
10164 if (argvars[1].v_type == VAR_STRING)
10165 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010166 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010167}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010168
10169/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010170 * "ch_open()" function
10171 */
10172 static void
10173f_ch_open(typval_T *argvars, typval_T *rettv)
10174{
Bram Moolenaar77073442016-02-13 23:23:53 +010010175 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010176 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010177}
10178
10179/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010180 * "ch_read()" function
10181 */
10182 static void
10183f_ch_read(typval_T *argvars, typval_T *rettv)
10184{
10185 common_channel_read(argvars, rettv, FALSE);
10186}
10187
10188/*
10189 * "ch_readraw()" function
10190 */
10191 static void
10192f_ch_readraw(typval_T *argvars, typval_T *rettv)
10193{
10194 common_channel_read(argvars, rettv, TRUE);
10195}
10196
10197/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010198 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010199 */
10200 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010201f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10202{
10203 ch_expr_common(argvars, rettv, TRUE);
10204}
10205
10206/*
10207 * "ch_sendexpr()" function
10208 */
10209 static void
10210f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10211{
10212 ch_expr_common(argvars, rettv, FALSE);
10213}
10214
10215/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010216 * "ch_evalraw()" function
10217 */
10218 static void
10219f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10220{
10221 ch_raw_common(argvars, rettv, TRUE);
10222}
10223
10224/*
10225 * "ch_sendraw()" function
10226 */
10227 static void
10228f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10229{
10230 ch_raw_common(argvars, rettv, FALSE);
10231}
10232
10233/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010234 * "ch_setoptions()" function
10235 */
10236 static void
10237f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10238{
10239 channel_T *channel;
10240 jobopt_T opt;
10241
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010242 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010243 if (channel == NULL)
10244 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010245 clear_job_options(&opt);
10246 if (get_job_options(&argvars[1], &opt,
10247 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010248 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010249 channel_set_options(channel, &opt);
10250}
10251
10252/*
10253 * "ch_status()" function
10254 */
10255 static void
10256f_ch_status(typval_T *argvars, typval_T *rettv)
10257{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010258 channel_T *channel;
10259
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010260 /* return an empty string by default */
10261 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010262 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010263
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010264 channel = get_channel_arg(&argvars[0], FALSE);
10265 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010266}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010267#endif
10268
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010269/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010270 * "changenr()" function
10271 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010272 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010273f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010274{
10275 rettv->vval.v_number = curbuf->b_u_seq_cur;
10276}
10277
10278/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010279 * "char2nr(string)" function
10280 */
10281 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010282f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010283{
10284#ifdef FEAT_MBYTE
10285 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010286 {
10287 int utf8 = 0;
10288
10289 if (argvars[1].v_type != VAR_UNKNOWN)
10290 utf8 = get_tv_number_chk(&argvars[1], NULL);
10291
10292 if (utf8)
10293 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10294 else
10295 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10296 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010297 else
10298#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010299 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010300}
10301
10302/*
10303 * "cindent(lnum)" function
10304 */
10305 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010306f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010307{
10308#ifdef FEAT_CINDENT
10309 pos_T pos;
10310 linenr_T lnum;
10311
10312 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010313 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010314 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10315 {
10316 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010317 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010318 curwin->w_cursor = pos;
10319 }
10320 else
10321#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010322 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010323}
10324
10325/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010326 * "clearmatches()" function
10327 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010329f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010330{
10331#ifdef FEAT_SEARCH_EXTRA
10332 clear_matches(curwin);
10333#endif
10334}
10335
10336/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010337 * "col(string)" function
10338 */
10339 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010340f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341{
10342 colnr_T col = 0;
10343 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010344 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010345
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010346 fp = var2fpos(&argvars[0], FALSE, &fnum);
10347 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010348 {
10349 if (fp->col == MAXCOL)
10350 {
10351 /* '> can be MAXCOL, get the length of the line then */
10352 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010353 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010354 else
10355 col = MAXCOL;
10356 }
10357 else
10358 {
10359 col = fp->col + 1;
10360#ifdef FEAT_VIRTUALEDIT
10361 /* col(".") when the cursor is on the NUL at the end of the line
10362 * because of "coladd" can be seen as an extra column. */
10363 if (virtual_active() && fp == &curwin->w_cursor)
10364 {
10365 char_u *p = ml_get_cursor();
10366
10367 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10368 curwin->w_virtcol - curwin->w_cursor.coladd))
10369 {
10370# ifdef FEAT_MBYTE
10371 int l;
10372
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010373 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010374 col += l;
10375# else
10376 if (*p != NUL && p[1] == NUL)
10377 ++col;
10378# endif
10379 }
10380 }
10381#endif
10382 }
10383 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010384 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010385}
10386
Bram Moolenaar572cb562005-08-05 21:35:02 +000010387#if defined(FEAT_INS_EXPAND)
10388/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010389 * "complete()" function
10390 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010392f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010393{
10394 int startcol;
10395
10396 if ((State & INSERT) == 0)
10397 {
10398 EMSG(_("E785: complete() can only be used in Insert mode"));
10399 return;
10400 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010401
10402 /* Check for undo allowed here, because if something was already inserted
10403 * the line was already saved for undo and this check isn't done. */
10404 if (!undo_allowed())
10405 return;
10406
Bram Moolenaarade00832006-03-10 21:46:58 +000010407 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10408 {
10409 EMSG(_(e_invarg));
10410 return;
10411 }
10412
10413 startcol = get_tv_number_chk(&argvars[0], NULL);
10414 if (startcol <= 0)
10415 return;
10416
10417 set_completion(startcol - 1, argvars[1].vval.v_list);
10418}
10419
10420/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010421 * "complete_add()" function
10422 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010423 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010424f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010425{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010426 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010427}
10428
10429/*
10430 * "complete_check()" function
10431 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010432 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010433f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010434{
10435 int saved = RedrawingDisabled;
10436
10437 RedrawingDisabled = 0;
10438 ins_compl_check_keys(0);
10439 rettv->vval.v_number = compl_interrupted;
10440 RedrawingDisabled = saved;
10441}
10442#endif
10443
Bram Moolenaar071d4272004-06-13 20:20:40 +000010444/*
10445 * "confirm(message, buttons[, default [, type]])" function
10446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010448f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010449{
10450#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10451 char_u *message;
10452 char_u *buttons = NULL;
10453 char_u buf[NUMBUFLEN];
10454 char_u buf2[NUMBUFLEN];
10455 int def = 1;
10456 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010457 char_u *typestr;
10458 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010459
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010460 message = get_tv_string_chk(&argvars[0]);
10461 if (message == NULL)
10462 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010463 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010464 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010465 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10466 if (buttons == NULL)
10467 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010468 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010469 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010470 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010471 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010472 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010473 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10474 if (typestr == NULL)
10475 error = TRUE;
10476 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010477 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010478 switch (TOUPPER_ASC(*typestr))
10479 {
10480 case 'E': type = VIM_ERROR; break;
10481 case 'Q': type = VIM_QUESTION; break;
10482 case 'I': type = VIM_INFO; break;
10483 case 'W': type = VIM_WARNING; break;
10484 case 'G': type = VIM_GENERIC; break;
10485 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010486 }
10487 }
10488 }
10489 }
10490
10491 if (buttons == NULL || *buttons == NUL)
10492 buttons = (char_u *)_("&Ok");
10493
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010494 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010495 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010496 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010497#endif
10498}
10499
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010500/*
10501 * "copy()" function
10502 */
10503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010504f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010505{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010506 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010507}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010508
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010509#ifdef FEAT_FLOAT
10510/*
10511 * "cos()" function
10512 */
10513 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010514f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010515{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010516 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010517
10518 rettv->v_type = VAR_FLOAT;
10519 if (get_float_arg(argvars, &f) == OK)
10520 rettv->vval.v_float = cos(f);
10521 else
10522 rettv->vval.v_float = 0.0;
10523}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010524
10525/*
10526 * "cosh()" function
10527 */
10528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010529f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010530{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010531 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010532
10533 rettv->v_type = VAR_FLOAT;
10534 if (get_float_arg(argvars, &f) == OK)
10535 rettv->vval.v_float = cosh(f);
10536 else
10537 rettv->vval.v_float = 0.0;
10538}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010539#endif
10540
Bram Moolenaar071d4272004-06-13 20:20:40 +000010541/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010542 * "count()" function
10543 */
10544 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010545f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010546{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010547 long n = 0;
10548 int ic = FALSE;
10549
Bram Moolenaare9a41262005-01-15 22:18:47 +000010550 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010551 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010552 listitem_T *li;
10553 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010554 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010555
Bram Moolenaare9a41262005-01-15 22:18:47 +000010556 if ((l = argvars[0].vval.v_list) != NULL)
10557 {
10558 li = l->lv_first;
10559 if (argvars[2].v_type != VAR_UNKNOWN)
10560 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010561 int error = FALSE;
10562
10563 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010564 if (argvars[3].v_type != VAR_UNKNOWN)
10565 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010566 idx = get_tv_number_chk(&argvars[3], &error);
10567 if (!error)
10568 {
10569 li = list_find(l, idx);
10570 if (li == NULL)
10571 EMSGN(_(e_listidx), idx);
10572 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010573 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010574 if (error)
10575 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010576 }
10577
10578 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010579 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010580 ++n;
10581 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010582 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010583 else if (argvars[0].v_type == VAR_DICT)
10584 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010585 int todo;
10586 dict_T *d;
10587 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010588
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010589 if ((d = argvars[0].vval.v_dict) != NULL)
10590 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010591 int error = FALSE;
10592
Bram Moolenaare9a41262005-01-15 22:18:47 +000010593 if (argvars[2].v_type != VAR_UNKNOWN)
10594 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010595 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010596 if (argvars[3].v_type != VAR_UNKNOWN)
10597 EMSG(_(e_invarg));
10598 }
10599
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010600 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010601 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010602 {
10603 if (!HASHITEM_EMPTY(hi))
10604 {
10605 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010606 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010607 ++n;
10608 }
10609 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010610 }
10611 }
10612 else
10613 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010614 rettv->vval.v_number = n;
10615}
10616
10617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010618 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10619 *
10620 * Checks the existence of a cscope connection.
10621 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010623f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010624{
10625#ifdef FEAT_CSCOPE
10626 int num = 0;
10627 char_u *dbpath = NULL;
10628 char_u *prepend = NULL;
10629 char_u buf[NUMBUFLEN];
10630
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010631 if (argvars[0].v_type != VAR_UNKNOWN
10632 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010633 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010634 num = (int)get_tv_number(&argvars[0]);
10635 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010636 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010637 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010638 }
10639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010640 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010641#endif
10642}
10643
10644/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010645 * "cursor(lnum, col)" function, or
10646 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010647 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010648 * Moves the cursor to the specified line and column.
10649 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010652f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010653{
10654 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010655#ifdef FEAT_VIRTUALEDIT
10656 long coladd = 0;
10657#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010658 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010659
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010660 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010661 if (argvars[1].v_type == VAR_UNKNOWN)
10662 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010663 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010664 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010665
Bram Moolenaar493c1782014-05-28 14:34:46 +020010666 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010667 {
10668 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010669 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010670 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010671 line = pos.lnum;
10672 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010673#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010674 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010675#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010676 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010677 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010678 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010679 set_curswant = FALSE;
10680 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010681 }
10682 else
10683 {
10684 line = get_tv_lnum(argvars);
10685 col = get_tv_number_chk(&argvars[1], NULL);
10686#ifdef FEAT_VIRTUALEDIT
10687 if (argvars[2].v_type != VAR_UNKNOWN)
10688 coladd = get_tv_number_chk(&argvars[2], NULL);
10689#endif
10690 }
10691 if (line < 0 || col < 0
10692#ifdef FEAT_VIRTUALEDIT
10693 || coladd < 0
10694#endif
10695 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010696 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 if (line > 0)
10698 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010699 if (col > 0)
10700 curwin->w_cursor.col = col - 1;
10701#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010702 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010703#endif
10704
10705 /* Make sure the cursor is in a valid position. */
10706 check_cursor();
10707#ifdef FEAT_MBYTE
10708 /* Correct cursor for multi-byte character. */
10709 if (has_mbyte)
10710 mb_adjust_cursor();
10711#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010712
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010713 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010714 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010715}
10716
10717/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010718 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010719 */
10720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010721f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010722{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010723 int noref = 0;
10724
10725 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010726 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010727 if (noref < 0 || noref > 1)
10728 EMSG(_(e_invarg));
10729 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010730 {
10731 current_copyID += COPYID_INC;
10732 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10733 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010734}
10735
10736/*
10737 * "delete()" function
10738 */
10739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010740f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010741{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010742 char_u nbuf[NUMBUFLEN];
10743 char_u *name;
10744 char_u *flags;
10745
10746 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010747 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010748 return;
10749
10750 name = get_tv_string(&argvars[0]);
10751 if (name == NULL || *name == NUL)
10752 {
10753 EMSG(_(e_invarg));
10754 return;
10755 }
10756
10757 if (argvars[1].v_type != VAR_UNKNOWN)
10758 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010759 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010760 flags = (char_u *)"";
10761
10762 if (*flags == NUL)
10763 /* delete a file */
10764 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10765 else if (STRCMP(flags, "d") == 0)
10766 /* delete an empty directory */
10767 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10768 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010769 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010770 rettv->vval.v_number = delete_recursive(name);
10771 else
10772 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010773}
10774
10775/*
10776 * "did_filetype()" function
10777 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010779f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010780{
10781#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010782 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010783#endif
10784}
10785
10786/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010787 * "diff_filler()" function
10788 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010789 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010790f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010791{
10792#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010793 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010794#endif
10795}
10796
10797/*
10798 * "diff_hlID()" function
10799 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010801f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010802{
10803#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010804 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010805 static linenr_T prev_lnum = 0;
10806 static int changedtick = 0;
10807 static int fnum = 0;
10808 static int change_start = 0;
10809 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010810 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010811 int filler_lines;
10812 int col;
10813
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010814 if (lnum < 0) /* ignore type error in {lnum} arg */
10815 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010816 if (lnum != prev_lnum
10817 || changedtick != curbuf->b_changedtick
10818 || fnum != curbuf->b_fnum)
10819 {
10820 /* New line, buffer, change: need to get the values. */
10821 filler_lines = diff_check(curwin, lnum);
10822 if (filler_lines < 0)
10823 {
10824 if (filler_lines == -1)
10825 {
10826 change_start = MAXCOL;
10827 change_end = -1;
10828 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10829 hlID = HLF_ADD; /* added line */
10830 else
10831 hlID = HLF_CHD; /* changed line */
10832 }
10833 else
10834 hlID = HLF_ADD; /* added line */
10835 }
10836 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010837 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010838 prev_lnum = lnum;
10839 changedtick = curbuf->b_changedtick;
10840 fnum = curbuf->b_fnum;
10841 }
10842
10843 if (hlID == HLF_CHD || hlID == HLF_TXD)
10844 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010845 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010846 if (col >= change_start && col <= change_end)
10847 hlID = HLF_TXD; /* changed text */
10848 else
10849 hlID = HLF_CHD; /* changed line */
10850 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010851 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010852#endif
10853}
10854
10855/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010856 * "disable_char_avail_for_testing({expr})" function
10857 */
10858 static void
10859f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10860{
10861 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10862}
10863
10864/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010865 * "empty({expr})" function
10866 */
10867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010868f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010869{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010870 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010871
10872 switch (argvars[0].v_type)
10873 {
10874 case VAR_STRING:
10875 case VAR_FUNC:
10876 n = argvars[0].vval.v_string == NULL
10877 || *argvars[0].vval.v_string == NUL;
10878 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010879 case VAR_PARTIAL:
10880 n = FALSE;
10881 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010882 case VAR_NUMBER:
10883 n = argvars[0].vval.v_number == 0;
10884 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010885 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010886#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010887 n = argvars[0].vval.v_float == 0.0;
10888 break;
10889#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010890 case VAR_LIST:
10891 n = argvars[0].vval.v_list == NULL
10892 || argvars[0].vval.v_list->lv_first == NULL;
10893 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010894 case VAR_DICT:
10895 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010896 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010897 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010898 case VAR_SPECIAL:
10899 n = argvars[0].vval.v_number != VVAL_TRUE;
10900 break;
10901
Bram Moolenaar835dc632016-02-07 14:27:38 +010010902 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010903#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010904 n = argvars[0].vval.v_job == NULL
10905 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10906 break;
10907#endif
10908 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010909#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010910 n = argvars[0].vval.v_channel == NULL
10911 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010912 break;
10913#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010914 case VAR_UNKNOWN:
10915 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10916 n = TRUE;
10917 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010918 }
10919
10920 rettv->vval.v_number = n;
10921}
10922
10923/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010924 * "escape({string}, {chars})" function
10925 */
10926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010927f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010928{
10929 char_u buf[NUMBUFLEN];
10930
Bram Moolenaar758711c2005-02-02 23:11:38 +000010931 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10932 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010933 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010934}
10935
10936/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010937 * "eval()" function
10938 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010940f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010941{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010942 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010943
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010944 s = get_tv_string_chk(&argvars[0]);
10945 if (s != NULL)
10946 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010947
Bram Moolenaar615b9972015-01-14 17:15:05 +010010948 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010949 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10950 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010951 if (p != NULL && !aborting())
10952 EMSG2(_(e_invexpr2), p);
10953 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010955 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010956 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010957 else if (*s != NUL)
10958 EMSG(_(e_trailing));
10959}
10960
10961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 * "eventhandler()" function
10963 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010965f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010967 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010968}
10969
10970/*
10971 * "executable()" function
10972 */
10973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010974f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010975{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010976 char_u *name = get_tv_string(&argvars[0]);
10977
10978 /* Check in $PATH and also check directly if there is a directory name. */
10979 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10980 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010981}
10982
10983/*
10984 * "exepath()" function
10985 */
10986 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010987f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010988{
10989 char_u *p = NULL;
10990
Bram Moolenaarb5971142015-03-21 17:32:19 +010010991 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010992 rettv->v_type = VAR_STRING;
10993 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010994}
10995
10996/*
10997 * "exists()" function
10998 */
10999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011000f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011001{
11002 char_u *p;
11003 char_u *name;
11004 int n = FALSE;
11005 int len = 0;
11006
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011007 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011008 if (*p == '$') /* environment variable */
11009 {
11010 /* first try "normal" environment variables (fast) */
11011 if (mch_getenv(p + 1) != NULL)
11012 n = TRUE;
11013 else
11014 {
11015 /* try expanding things like $VIM and ${HOME} */
11016 p = expand_env_save(p);
11017 if (p != NULL && *p != '$')
11018 n = TRUE;
11019 vim_free(p);
11020 }
11021 }
11022 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011023 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011024 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011025 if (*skipwhite(p) != NUL)
11026 n = FALSE; /* trailing garbage */
11027 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011028 else if (*p == '*') /* internal or user defined function */
11029 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011030 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011031 }
11032 else if (*p == ':')
11033 {
11034 n = cmd_exists(p + 1);
11035 }
11036 else if (*p == '#')
11037 {
11038#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011039 if (p[1] == '#')
11040 n = autocmd_supported(p + 2);
11041 else
11042 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011043#endif
11044 }
11045 else /* internal variable */
11046 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011047 char_u *tofree;
11048 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011049
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011050 /* get_name_len() takes care of expanding curly braces */
11051 name = p;
11052 len = get_name_len(&p, &tofree, TRUE, FALSE);
11053 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011054 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011055 if (tofree != NULL)
11056 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011057 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011058 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011059 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011060 /* handle d.key, l[idx], f(expr) */
11061 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11062 if (n)
11063 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011064 }
11065 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011066 if (*p != NUL)
11067 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011069 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011070 }
11071
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011072 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011073}
11074
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011075#ifdef FEAT_FLOAT
11076/*
11077 * "exp()" function
11078 */
11079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011080f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011081{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011082 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011083
11084 rettv->v_type = VAR_FLOAT;
11085 if (get_float_arg(argvars, &f) == OK)
11086 rettv->vval.v_float = exp(f);
11087 else
11088 rettv->vval.v_float = 0.0;
11089}
11090#endif
11091
Bram Moolenaar071d4272004-06-13 20:20:40 +000011092/*
11093 * "expand()" function
11094 */
11095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011096f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011097{
11098 char_u *s;
11099 int len;
11100 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011101 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011102 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011103 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011104 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011105
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011106 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011107 if (argvars[1].v_type != VAR_UNKNOWN
11108 && argvars[2].v_type != VAR_UNKNOWN
11109 && get_tv_number_chk(&argvars[2], &error)
11110 && !error)
11111 {
11112 rettv->v_type = VAR_LIST;
11113 rettv->vval.v_list = NULL;
11114 }
11115
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011116 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011117 if (*s == '%' || *s == '#' || *s == '<')
11118 {
11119 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011120 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011121 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011122 if (rettv->v_type == VAR_LIST)
11123 {
11124 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11125 list_append_string(rettv->vval.v_list, result, -1);
11126 else
11127 vim_free(result);
11128 }
11129 else
11130 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011131 }
11132 else
11133 {
11134 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011135 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011136 if (argvars[1].v_type != VAR_UNKNOWN
11137 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011138 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011139 if (!error)
11140 {
11141 ExpandInit(&xpc);
11142 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011143 if (p_wic)
11144 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011145 if (rettv->v_type == VAR_STRING)
11146 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11147 options, WILD_ALL);
11148 else if (rettv_list_alloc(rettv) != FAIL)
11149 {
11150 int i;
11151
11152 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11153 for (i = 0; i < xpc.xp_numfiles; i++)
11154 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11155 ExpandCleanup(&xpc);
11156 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011157 }
11158 else
11159 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011160 }
11161}
11162
11163/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011164 * Go over all entries in "d2" and add them to "d1".
11165 * When "action" is "error" then a duplicate key is an error.
11166 * When "action" is "force" then a duplicate key is overwritten.
11167 * Otherwise duplicate keys are ignored ("action" is "keep").
11168 */
11169 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011170dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011171{
11172 dictitem_T *di1;
11173 hashitem_T *hi2;
11174 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011175 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011176
11177 todo = (int)d2->dv_hashtab.ht_used;
11178 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11179 {
11180 if (!HASHITEM_EMPTY(hi2))
11181 {
11182 --todo;
11183 di1 = dict_find(d1, hi2->hi_key, -1);
11184 if (d1->dv_scope != 0)
11185 {
11186 /* Disallow replacing a builtin function in l: and g:.
11187 * Check the key to be valid when adding to any
11188 * scope. */
11189 if (d1->dv_scope == VAR_DEF_SCOPE
11190 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11191 && var_check_func_name(hi2->hi_key,
11192 di1 == NULL))
11193 break;
11194 if (!valid_varname(hi2->hi_key))
11195 break;
11196 }
11197 if (di1 == NULL)
11198 {
11199 di1 = dictitem_copy(HI2DI(hi2));
11200 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11201 dictitem_free(di1);
11202 }
11203 else if (*action == 'e')
11204 {
11205 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11206 break;
11207 }
11208 else if (*action == 'f' && HI2DI(hi2) != di1)
11209 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011210 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11211 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011212 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011213 clear_tv(&di1->di_tv);
11214 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11215 }
11216 }
11217 }
11218}
11219
11220/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011221 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011222 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011223 */
11224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011225f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011226{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011227 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011228
Bram Moolenaare9a41262005-01-15 22:18:47 +000011229 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011230 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011231 list_T *l1, *l2;
11232 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011233 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011234 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011235
Bram Moolenaare9a41262005-01-15 22:18:47 +000011236 l1 = argvars[0].vval.v_list;
11237 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011238 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011239 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011240 {
11241 if (argvars[2].v_type != VAR_UNKNOWN)
11242 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011243 before = get_tv_number_chk(&argvars[2], &error);
11244 if (error)
11245 return; /* type error; errmsg already given */
11246
Bram Moolenaar758711c2005-02-02 23:11:38 +000011247 if (before == l1->lv_len)
11248 item = NULL;
11249 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011250 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011251 item = list_find(l1, before);
11252 if (item == NULL)
11253 {
11254 EMSGN(_(e_listidx), before);
11255 return;
11256 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011257 }
11258 }
11259 else
11260 item = NULL;
11261 list_extend(l1, l2, item);
11262
Bram Moolenaare9a41262005-01-15 22:18:47 +000011263 copy_tv(&argvars[0], rettv);
11264 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011265 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011266 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11267 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011268 dict_T *d1, *d2;
11269 char_u *action;
11270 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011271
11272 d1 = argvars[0].vval.v_dict;
11273 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011274 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011275 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011276 {
11277 /* Check the third argument. */
11278 if (argvars[2].v_type != VAR_UNKNOWN)
11279 {
11280 static char *(av[]) = {"keep", "force", "error"};
11281
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011282 action = get_tv_string_chk(&argvars[2]);
11283 if (action == NULL)
11284 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011285 for (i = 0; i < 3; ++i)
11286 if (STRCMP(action, av[i]) == 0)
11287 break;
11288 if (i == 3)
11289 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011290 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011291 return;
11292 }
11293 }
11294 else
11295 action = (char_u *)"force";
11296
Bram Moolenaara9922d62013-05-30 13:01:18 +020011297 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011298
Bram Moolenaare9a41262005-01-15 22:18:47 +000011299 copy_tv(&argvars[0], rettv);
11300 }
11301 }
11302 else
11303 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011304}
11305
11306/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011307 * "feedkeys()" function
11308 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011309 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011310f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011311{
11312 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011313 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011314 char_u *keys, *flags;
11315 char_u nbuf[NUMBUFLEN];
11316 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011317 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011318 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011319
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011320 /* This is not allowed in the sandbox. If the commands would still be
11321 * executed in the sandbox it would be OK, but it probably happens later,
11322 * when "sandbox" is no longer set. */
11323 if (check_secure())
11324 return;
11325
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011326 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011327
11328 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011329 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011330 flags = get_tv_string_buf(&argvars[1], nbuf);
11331 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011332 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011333 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011334 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011335 case 'n': remap = FALSE; break;
11336 case 'm': remap = TRUE; break;
11337 case 't': typed = TRUE; break;
11338 case 'i': insert = TRUE; break;
11339 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011340 }
11341 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011342 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011343
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011344 if (*keys != NUL || execute)
11345 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011346 /* Need to escape K_SPECIAL and CSI before putting the string in the
11347 * typeahead buffer. */
11348 keys_esc = vim_strsave_escape_csi(keys);
11349 if (keys_esc != NULL)
11350 {
11351 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011352 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011353 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011354 if (vgetc_busy)
11355 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011356 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011357 {
11358 int save_msg_scroll = msg_scroll;
11359
11360 /* Avoid a 1 second delay when the keys start Insert mode. */
11361 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011362 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011363 msg_scroll |= save_msg_scroll;
11364 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011365 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011366 }
11367}
11368
11369/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011370 * "filereadable()" function
11371 */
11372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011373f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011375 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011376 char_u *p;
11377 int n;
11378
Bram Moolenaarc236c162008-07-13 17:41:49 +000011379#ifndef O_NONBLOCK
11380# define O_NONBLOCK 0
11381#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011382 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011383 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11384 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011385 {
11386 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011387 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011388 }
11389 else
11390 n = FALSE;
11391
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011392 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011393}
11394
11395/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011396 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011397 * rights to write into.
11398 */
11399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011400f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011402 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011403}
11404
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011405 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011406findfilendir(
11407 typval_T *argvars UNUSED,
11408 typval_T *rettv,
11409 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011410{
11411#ifdef FEAT_SEARCHPATH
11412 char_u *fname;
11413 char_u *fresult = NULL;
11414 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11415 char_u *p;
11416 char_u pathbuf[NUMBUFLEN];
11417 int count = 1;
11418 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011419 int error = FALSE;
11420#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011421
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011422 rettv->vval.v_string = NULL;
11423 rettv->v_type = VAR_STRING;
11424
11425#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011426 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011427
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011428 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011429 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011430 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11431 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011432 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 else
11434 {
11435 if (*p != NUL)
11436 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011437
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011439 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011440 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011441 }
11442
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011443 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11444 error = TRUE;
11445
11446 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011447 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011448 do
11449 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011450 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011451 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011452 fresult = find_file_in_path_option(first ? fname : NULL,
11453 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011454 0, first, path,
11455 find_what,
11456 curbuf->b_ffname,
11457 find_what == FINDFILE_DIR
11458 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011460
11461 if (fresult != NULL && rettv->v_type == VAR_LIST)
11462 list_append_string(rettv->vval.v_list, fresult, -1);
11463
11464 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011465 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011466
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011467 if (rettv->v_type == VAR_STRING)
11468 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011469#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011470}
11471
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011472static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11473static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011474
11475/*
11476 * Implementation of map() and filter().
11477 */
11478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011479filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011480{
11481 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011482 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011483 listitem_T *li, *nli;
11484 list_T *l = NULL;
11485 dictitem_T *di;
11486 hashtab_T *ht;
11487 hashitem_T *hi;
11488 dict_T *d = NULL;
11489 typval_T save_val;
11490 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011491 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011492 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011493 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011494 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011495 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011496 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011497 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011498
Bram Moolenaare9a41262005-01-15 22:18:47 +000011499 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011500 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011501 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011502 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011503 return;
11504 }
11505 else if (argvars[0].v_type == VAR_DICT)
11506 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011507 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011508 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011509 return;
11510 }
11511 else
11512 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011513 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011514 return;
11515 }
11516
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011517 expr = get_tv_string_buf_chk(&argvars[1], buf);
11518 /* On type errors, the preceding call has already displayed an error
11519 * message. Avoid a misleading error message for an empty string that
11520 * was not passed as argument. */
11521 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011522 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011523 prepare_vimvar(VV_VAL, &save_val);
11524 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011525
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011526 /* We reset "did_emsg" to be able to detect whether an error
11527 * occurred during evaluation of the expression. */
11528 save_did_emsg = did_emsg;
11529 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011530
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011531 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011533 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011534 vimvars[VV_KEY].vv_type = VAR_STRING;
11535
11536 ht = &d->dv_hashtab;
11537 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011538 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011540 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011541 if (!HASHITEM_EMPTY(hi))
11542 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011543 int r;
11544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011545 --todo;
11546 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011547 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011548 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11549 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011550 break;
11551 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011552 r = filter_map_one(&di->di_tv, expr, map, &rem);
11553 clear_tv(&vimvars[VV_KEY].vv_tv);
11554 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011555 break;
11556 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011557 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011558 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11559 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011560 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011562 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011563 }
11564 }
11565 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011566 }
11567 else
11568 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011569 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11570
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011571 for (li = l->lv_first; li != NULL; li = nli)
11572 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011573 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011574 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011575 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011576 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011577 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011578 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011579 break;
11580 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011581 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011582 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011583 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011584 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011585
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011586 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011587 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011588
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011589 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011590 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011591
11592 copy_tv(&argvars[0], rettv);
11593}
11594
11595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011596filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597{
Bram Moolenaar33570922005-01-25 22:26:29 +000011598 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011599 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011600 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601
Bram Moolenaar33570922005-01-25 22:26:29 +000011602 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011603 s = expr;
11604 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011605 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011606 if (*s != NUL) /* check for trailing chars after expr */
11607 {
11608 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011609 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011610 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011611 }
11612 if (map)
11613 {
11614 /* map(): replace the list item value */
11615 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011616 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011617 *tv = rettv;
11618 }
11619 else
11620 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011621 int error = FALSE;
11622
Bram Moolenaare9a41262005-01-15 22:18:47 +000011623 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011625 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011626 /* On type error, nothing has been removed; return FAIL to stop the
11627 * loop. The error message was given by get_tv_number_chk(). */
11628 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011629 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011630 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011631 retval = OK;
11632theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011633 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011634 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011635}
11636
11637/*
11638 * "filter()" function
11639 */
11640 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011641f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011642{
11643 filter_map(argvars, rettv, FALSE);
11644}
11645
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011646/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011647 * "finddir({fname}[, {path}[, {count}]])" function
11648 */
11649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011650f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011652 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011653}
11654
11655/*
11656 * "findfile({fname}[, {path}[, {count}]])" function
11657 */
11658 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011659f_findfile(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_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662}
11663
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011664#ifdef FEAT_FLOAT
11665/*
11666 * "float2nr({float})" function
11667 */
11668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011669f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011670{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011671 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011672
11673 if (get_float_arg(argvars, &f) == OK)
11674 {
11675 if (f < -0x7fffffff)
11676 rettv->vval.v_number = -0x7fffffff;
11677 else if (f > 0x7fffffff)
11678 rettv->vval.v_number = 0x7fffffff;
11679 else
11680 rettv->vval.v_number = (varnumber_T)f;
11681 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011682}
11683
11684/*
11685 * "floor({float})" function
11686 */
11687 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011688f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011689{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011690 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011691
11692 rettv->v_type = VAR_FLOAT;
11693 if (get_float_arg(argvars, &f) == OK)
11694 rettv->vval.v_float = floor(f);
11695 else
11696 rettv->vval.v_float = 0.0;
11697}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011698
11699/*
11700 * "fmod()" function
11701 */
11702 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011703f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011704{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011705 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011706
11707 rettv->v_type = VAR_FLOAT;
11708 if (get_float_arg(argvars, &fx) == OK
11709 && get_float_arg(&argvars[1], &fy) == OK)
11710 rettv->vval.v_float = fmod(fx, fy);
11711 else
11712 rettv->vval.v_float = 0.0;
11713}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011714#endif
11715
Bram Moolenaar0d660222005-01-07 21:51:51 +000011716/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011717 * "fnameescape({string})" function
11718 */
11719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011720f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011721{
11722 rettv->vval.v_string = vim_strsave_fnameescape(
11723 get_tv_string(&argvars[0]), FALSE);
11724 rettv->v_type = VAR_STRING;
11725}
11726
11727/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011728 * "fnamemodify({fname}, {mods})" function
11729 */
11730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011731f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011732{
11733 char_u *fname;
11734 char_u *mods;
11735 int usedlen = 0;
11736 int len;
11737 char_u *fbuf = NULL;
11738 char_u buf[NUMBUFLEN];
11739
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011740 fname = get_tv_string_chk(&argvars[0]);
11741 mods = get_tv_string_buf_chk(&argvars[1], buf);
11742 if (fname == NULL || mods == NULL)
11743 fname = NULL;
11744 else
11745 {
11746 len = (int)STRLEN(fname);
11747 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11748 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011754 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011755 vim_free(fbuf);
11756}
11757
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011758static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011759
11760/*
11761 * "foldclosed()" function
11762 */
11763 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011764foldclosed_both(
11765 typval_T *argvars UNUSED,
11766 typval_T *rettv,
11767 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011768{
11769#ifdef FEAT_FOLDING
11770 linenr_T lnum;
11771 linenr_T first, last;
11772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011774 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11775 {
11776 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11777 {
11778 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011781 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011782 return;
11783 }
11784 }
11785#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011787}
11788
11789/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011790 * "foldclosed()" function
11791 */
11792 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011793f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011794{
11795 foldclosed_both(argvars, rettv, FALSE);
11796}
11797
11798/*
11799 * "foldclosedend()" function
11800 */
11801 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011802f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011803{
11804 foldclosed_both(argvars, rettv, TRUE);
11805}
11806
11807/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011808 * "foldlevel()" function
11809 */
11810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011811f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011812{
11813#ifdef FEAT_FOLDING
11814 linenr_T lnum;
11815
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011818 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011819#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011820}
11821
11822/*
11823 * "foldtext()" function
11824 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011826f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011827{
11828#ifdef FEAT_FOLDING
11829 linenr_T lnum;
11830 char_u *s;
11831 char_u *r;
11832 int len;
11833 char *txt;
11834#endif
11835
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011836 rettv->v_type = VAR_STRING;
11837 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011838#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011839 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11840 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11841 <= curbuf->b_ml.ml_line_count
11842 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011843 {
11844 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011845 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11846 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011847 {
11848 if (!linewhite(lnum))
11849 break;
11850 ++lnum;
11851 }
11852
11853 /* Find interesting text in this line. */
11854 s = skipwhite(ml_get(lnum));
11855 /* skip C comment-start */
11856 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011858 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011859 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011860 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011861 {
11862 s = skipwhite(ml_get(lnum + 1));
11863 if (*s == '*')
11864 s = skipwhite(s + 1);
11865 }
11866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011867 txt = _("+-%s%3ld lines: ");
11868 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011869 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011870 + 20 /* for %3ld */
11871 + STRLEN(s))); /* concatenated */
11872 if (r != NULL)
11873 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011874 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11875 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11876 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011877 len = (int)STRLEN(r);
11878 STRCAT(r, s);
11879 /* remove 'foldmarker' and 'commentstring' */
11880 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011881 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011882 }
11883 }
11884#endif
11885}
11886
11887/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011888 * "foldtextresult(lnum)" function
11889 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011890 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011891f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011892{
11893#ifdef FEAT_FOLDING
11894 linenr_T lnum;
11895 char_u *text;
11896 char_u buf[51];
11897 foldinfo_T foldinfo;
11898 int fold_count;
11899#endif
11900
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011901 rettv->v_type = VAR_STRING;
11902 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011903#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011904 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011905 /* treat illegal types and illegal string values for {lnum} the same */
11906 if (lnum < 0)
11907 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011908 fold_count = foldedCount(curwin, lnum, &foldinfo);
11909 if (fold_count > 0)
11910 {
11911 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11912 &foldinfo, buf);
11913 if (text == buf)
11914 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011915 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011916 }
11917#endif
11918}
11919
11920/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 * "foreground()" function
11922 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011924f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011925{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011926#ifdef FEAT_GUI
11927 if (gui.in_use)
11928 gui_mch_set_foreground();
11929#else
11930# ifdef WIN32
11931 win32_set_foreground();
11932# endif
11933#endif
11934}
11935
11936/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 * "function()" function
11938 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011939 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011940f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011941{
11942 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011943 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011944 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011945 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011946
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011947 if (argvars[0].v_type == VAR_FUNC)
11948 {
11949 /* function(MyFunc, [arg], dict) */
11950 s = argvars[0].vval.v_string;
11951 }
11952 else if (argvars[0].v_type == VAR_PARTIAL
11953 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011954 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011955 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011956 arg_pt = argvars[0].vval.v_partial;
11957 s = arg_pt->pt_name;
11958 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011959 else
11960 {
11961 /* function('MyFunc', [arg], dict) */
11962 s = get_tv_string(&argvars[0]);
11963 use_string = TRUE;
11964 }
11965
11966 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011967 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011968 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011969 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11970 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011971 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011972 else
11973 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011974 int dict_idx = 0;
11975 int arg_idx = 0;
11976 list_T *list = NULL;
11977
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011978 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011979 {
11980 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011981 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011982
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011983 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11984 * also be called from another script. Using trans_function_name()
11985 * would also work, but some plugins depend on the name being
11986 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011987 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011988 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11989 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011990 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011991 STRCPY(name, sid_buf);
11992 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011993 }
11994 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011995 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011996 name = vim_strsave(s);
11997
11998 if (argvars[1].v_type != VAR_UNKNOWN)
11999 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012000 if (argvars[2].v_type != VAR_UNKNOWN)
12001 {
12002 /* function(name, [args], dict) */
12003 arg_idx = 1;
12004 dict_idx = 2;
12005 }
12006 else if (argvars[1].v_type == VAR_DICT)
12007 /* function(name, dict) */
12008 dict_idx = 1;
12009 else
12010 /* function(name, [args]) */
12011 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012012 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012013 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012014 if (argvars[dict_idx].v_type != VAR_DICT)
12015 {
12016 EMSG(_("E922: expected a dict"));
12017 vim_free(name);
12018 return;
12019 }
12020 if (argvars[dict_idx].vval.v_dict == NULL)
12021 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012022 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012023 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012024 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012025 if (argvars[arg_idx].v_type != VAR_LIST)
12026 {
12027 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12028 vim_free(name);
12029 return;
12030 }
12031 list = argvars[arg_idx].vval.v_list;
12032 if (list == NULL || list->lv_len == 0)
12033 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012034 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012035 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012036 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012037 {
12038 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012039
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012040 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012041 if (pt == NULL)
12042 vim_free(name);
12043 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012044 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012045 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012046 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012047 listitem_T *li;
12048 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012049 int arg_len = 0;
12050 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012051
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012052 if (arg_pt != NULL)
12053 arg_len = arg_pt->pt_argc;
12054 if (list != NULL)
12055 lv_len = list->lv_len;
12056 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012057 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012058 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012059 if (pt->pt_argv == NULL)
12060 {
12061 vim_free(pt);
12062 vim_free(name);
12063 return;
12064 }
12065 else
12066 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012067 for (i = 0; i < arg_len; i++)
12068 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12069 if (lv_len > 0)
12070 for (li = list->lv_first; li != NULL;
12071 li = li->li_next)
12072 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012073 }
12074 }
12075
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012076 /* For "function(dict.func, [], dict)" and "func" is a partial
12077 * use "dict". That is backwards compatible. */
12078 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012079 {
12080 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12081 ++pt->pt_dict->dv_refcount;
12082 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012083 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012084 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012085 pt->pt_dict = arg_pt->pt_dict;
12086 if (pt->pt_dict != NULL)
12087 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012088 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012089
12090 pt->pt_refcount = 1;
12091 pt->pt_name = name;
12092 func_ref(pt->pt_name);
12093 }
12094 rettv->v_type = VAR_PARTIAL;
12095 rettv->vval.v_partial = pt;
12096 }
12097 else
12098 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012099 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012100 rettv->v_type = VAR_FUNC;
12101 rettv->vval.v_string = name;
12102 func_ref(name);
12103 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012104 }
12105}
12106
12107/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012108 * "garbagecollect()" function
12109 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012111f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012112{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012113 /* This is postponed until we are back at the toplevel, because we may be
12114 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12115 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012116
12117 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12118 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012119}
12120
12121/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012122 * "get()" function
12123 */
12124 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012125f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012126{
Bram Moolenaar33570922005-01-25 22:26:29 +000012127 listitem_T *li;
12128 list_T *l;
12129 dictitem_T *di;
12130 dict_T *d;
12131 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132
Bram Moolenaare9a41262005-01-15 22:18:47 +000012133 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012135 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012136 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012137 int error = FALSE;
12138
12139 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12140 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012141 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012142 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012143 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012144 else if (argvars[0].v_type == VAR_DICT)
12145 {
12146 if ((d = argvars[0].vval.v_dict) != NULL)
12147 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012148 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012149 if (di != NULL)
12150 tv = &di->di_tv;
12151 }
12152 }
12153 else
12154 EMSG2(_(e_listdictarg), "get()");
12155
12156 if (tv == NULL)
12157 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012158 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012159 copy_tv(&argvars[2], rettv);
12160 }
12161 else
12162 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012163}
12164
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012165static 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 +000012166
12167/*
12168 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012169 * Return a range (from start to end) of lines in rettv from the specified
12170 * buffer.
12171 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012172 */
12173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012174get_buffer_lines(
12175 buf_T *buf,
12176 linenr_T start,
12177 linenr_T end,
12178 int retlist,
12179 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012180{
12181 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012182
Bram Moolenaar959a1432013-12-14 12:17:38 +010012183 rettv->v_type = VAR_STRING;
12184 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012185 if (retlist && rettv_list_alloc(rettv) == FAIL)
12186 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012187
12188 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12189 return;
12190
12191 if (!retlist)
12192 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012193 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12194 p = ml_get_buf(buf, start, FALSE);
12195 else
12196 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012197 rettv->vval.v_string = vim_strsave(p);
12198 }
12199 else
12200 {
12201 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012202 return;
12203
12204 if (start < 1)
12205 start = 1;
12206 if (end > buf->b_ml.ml_line_count)
12207 end = buf->b_ml.ml_line_count;
12208 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012209 if (list_append_string(rettv->vval.v_list,
12210 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012211 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012212 }
12213}
12214
12215/*
12216 * "getbufline()" function
12217 */
12218 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012219f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012220{
12221 linenr_T lnum;
12222 linenr_T end;
12223 buf_T *buf;
12224
12225 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12226 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012227 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012228 --emsg_off;
12229
Bram Moolenaar661b1822005-07-28 22:36:45 +000012230 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012231 if (argvars[2].v_type == VAR_UNKNOWN)
12232 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012233 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012234 end = get_tv_lnum_buf(&argvars[2], buf);
12235
Bram Moolenaar342337a2005-07-21 21:11:17 +000012236 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012237}
12238
Bram Moolenaar0d660222005-01-07 21:51:51 +000012239/*
12240 * "getbufvar()" function
12241 */
12242 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012243f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012244{
12245 buf_T *buf;
12246 buf_T *save_curbuf;
12247 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012248 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012249 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012250
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012251 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12252 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012253 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012254 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012255
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012256 rettv->v_type = VAR_STRING;
12257 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012258
12259 if (buf != NULL && varname != NULL)
12260 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012261 /* set curbuf to be our buf, temporarily */
12262 save_curbuf = curbuf;
12263 curbuf = buf;
12264
Bram Moolenaar0d660222005-01-07 21:51:51 +000012265 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012266 {
12267 if (get_option_tv(&varname, rettv, TRUE) == OK)
12268 done = TRUE;
12269 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012270 else if (STRCMP(varname, "changedtick") == 0)
12271 {
12272 rettv->v_type = VAR_NUMBER;
12273 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012274 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012275 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012276 else
12277 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012278 /* Look up the variable. */
12279 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12280 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12281 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012282 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012284 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012285 done = TRUE;
12286 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012287 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012288
12289 /* restore previous notion of curbuf */
12290 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012291 }
12292
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012293 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12294 /* use the default value */
12295 copy_tv(&argvars[2], rettv);
12296
Bram Moolenaar0d660222005-01-07 21:51:51 +000012297 --emsg_off;
12298}
12299
12300/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301 * "getchar()" function
12302 */
12303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012304f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012305{
12306 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012307 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012308
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012309 /* Position the cursor. Needed after a message that ends in a space. */
12310 windgoto(msg_row, msg_col);
12311
Bram Moolenaar071d4272004-06-13 20:20:40 +000012312 ++no_mapping;
12313 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012314 for (;;)
12315 {
12316 if (argvars[0].v_type == VAR_UNKNOWN)
12317 /* getchar(): blocking wait. */
12318 n = safe_vgetc();
12319 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12320 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012321 n = vpeekc_any();
12322 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012323 /* illegal argument or getchar(0) and no char avail: return zero */
12324 n = 0;
12325 else
12326 /* getchar(0) and char avail: return char */
12327 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012328
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012329 if (n == K_IGNORE)
12330 continue;
12331 break;
12332 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012333 --no_mapping;
12334 --allow_keys;
12335
Bram Moolenaar219b8702006-11-01 14:32:36 +000012336 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12337 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12338 vimvars[VV_MOUSE_COL].vv_nr = 0;
12339
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012340 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012341 if (IS_SPECIAL(n) || mod_mask != 0)
12342 {
12343 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12344 int i = 0;
12345
12346 /* Turn a special key into three bytes, plus modifier. */
12347 if (mod_mask != 0)
12348 {
12349 temp[i++] = K_SPECIAL;
12350 temp[i++] = KS_MODIFIER;
12351 temp[i++] = mod_mask;
12352 }
12353 if (IS_SPECIAL(n))
12354 {
12355 temp[i++] = K_SPECIAL;
12356 temp[i++] = K_SECOND(n);
12357 temp[i++] = K_THIRD(n);
12358 }
12359#ifdef FEAT_MBYTE
12360 else if (has_mbyte)
12361 i += (*mb_char2bytes)(n, temp + i);
12362#endif
12363 else
12364 temp[i++] = n;
12365 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012366 rettv->v_type = VAR_STRING;
12367 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012368
12369#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012370 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012371 {
12372 int row = mouse_row;
12373 int col = mouse_col;
12374 win_T *win;
12375 linenr_T lnum;
12376# ifdef FEAT_WINDOWS
12377 win_T *wp;
12378# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012379 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012380
12381 if (row >= 0 && col >= 0)
12382 {
12383 /* Find the window at the mouse coordinates and compute the
12384 * text position. */
12385 win = mouse_find_win(&row, &col);
12386 (void)mouse_comp_pos(win, &row, &col, &lnum);
12387# ifdef FEAT_WINDOWS
12388 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012389 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012390# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012391 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012392 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12393 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12394 }
12395 }
12396#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012397 }
12398}
12399
12400/*
12401 * "getcharmod()" function
12402 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012404f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012406 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012407}
12408
12409/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012410 * "getcharsearch()" function
12411 */
12412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012413f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012414{
12415 if (rettv_dict_alloc(rettv) != FAIL)
12416 {
12417 dict_T *dict = rettv->vval.v_dict;
12418
12419 dict_add_nr_str(dict, "char", 0L, last_csearch());
12420 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12421 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12422 }
12423}
12424
12425/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 * "getcmdline()" function
12427 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012429f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012430{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012431 rettv->v_type = VAR_STRING;
12432 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012433}
12434
12435/*
12436 * "getcmdpos()" function
12437 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012439f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012441 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012442}
12443
12444/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012445 * "getcmdtype()" function
12446 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012448f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012449{
12450 rettv->v_type = VAR_STRING;
12451 rettv->vval.v_string = alloc(2);
12452 if (rettv->vval.v_string != NULL)
12453 {
12454 rettv->vval.v_string[0] = get_cmdline_type();
12455 rettv->vval.v_string[1] = NUL;
12456 }
12457}
12458
12459/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012460 * "getcmdwintype()" function
12461 */
12462 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012463f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012464{
12465 rettv->v_type = VAR_STRING;
12466 rettv->vval.v_string = NULL;
12467#ifdef FEAT_CMDWIN
12468 rettv->vval.v_string = alloc(2);
12469 if (rettv->vval.v_string != NULL)
12470 {
12471 rettv->vval.v_string[0] = cmdwin_type;
12472 rettv->vval.v_string[1] = NUL;
12473 }
12474#endif
12475}
12476
12477/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 * "getcwd()" function
12479 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012481f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012482{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012483 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012484 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012485
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012486 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012487 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012488
12489 wp = find_tabwin(&argvars[0], &argvars[1]);
12490 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012492 if (wp->w_localdir != NULL)
12493 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12494 else if(globaldir != NULL)
12495 rettv->vval.v_string = vim_strsave(globaldir);
12496 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012497 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012498 cwd = alloc(MAXPATHL);
12499 if (cwd != NULL)
12500 {
12501 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12502 rettv->vval.v_string = vim_strsave(cwd);
12503 vim_free(cwd);
12504 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012505 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012506#ifdef BACKSLASH_IN_FILENAME
12507 if (rettv->vval.v_string != NULL)
12508 slash_adjust(rettv->vval.v_string);
12509#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012510 }
12511}
12512
12513/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012514 * "getfontname()" function
12515 */
12516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012517f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012518{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012519 rettv->v_type = VAR_STRING;
12520 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012521#ifdef FEAT_GUI
12522 if (gui.in_use)
12523 {
12524 GuiFont font;
12525 char_u *name = NULL;
12526
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012527 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012528 {
12529 /* Get the "Normal" font. Either the name saved by
12530 * hl_set_font_name() or from the font ID. */
12531 font = gui.norm_font;
12532 name = hl_get_font_name();
12533 }
12534 else
12535 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012536 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012537 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12538 return;
12539 font = gui_mch_get_font(name, FALSE);
12540 if (font == NOFONT)
12541 return; /* Invalid font name, return empty string. */
12542 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012543 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012544 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012545 gui_mch_free_font(font);
12546 }
12547#endif
12548}
12549
12550/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012551 * "getfperm({fname})" function
12552 */
12553 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012554f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012555{
12556 char_u *fname;
12557 struct stat st;
12558 char_u *perm = NULL;
12559 char_u flags[] = "rwx";
12560 int i;
12561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012563
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012564 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012565 if (mch_stat((char *)fname, &st) >= 0)
12566 {
12567 perm = vim_strsave((char_u *)"---------");
12568 if (perm != NULL)
12569 {
12570 for (i = 0; i < 9; i++)
12571 {
12572 if (st.st_mode & (1 << (8 - i)))
12573 perm[i] = flags[i % 3];
12574 }
12575 }
12576 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012577 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012578}
12579
12580/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012581 * "getfsize({fname})" function
12582 */
12583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012584f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012585{
12586 char_u *fname;
12587 struct stat st;
12588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012591 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012592
12593 if (mch_stat((char *)fname, &st) >= 0)
12594 {
12595 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012597 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012598 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012599 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012600
12601 /* non-perfect check for overflow */
12602 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12603 rettv->vval.v_number = -2;
12604 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012605 }
12606 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012607 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012608}
12609
12610/*
12611 * "getftime({fname})" function
12612 */
12613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012614f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012615{
12616 char_u *fname;
12617 struct stat st;
12618
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012619 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012620
12621 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012624 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012625}
12626
12627/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012628 * "getftype({fname})" function
12629 */
12630 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012631f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012632{
12633 char_u *fname;
12634 struct stat st;
12635 char_u *type = NULL;
12636 char *t;
12637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012639
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012640 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012641 if (mch_lstat((char *)fname, &st) >= 0)
12642 {
12643#ifdef S_ISREG
12644 if (S_ISREG(st.st_mode))
12645 t = "file";
12646 else if (S_ISDIR(st.st_mode))
12647 t = "dir";
12648# ifdef S_ISLNK
12649 else if (S_ISLNK(st.st_mode))
12650 t = "link";
12651# endif
12652# ifdef S_ISBLK
12653 else if (S_ISBLK(st.st_mode))
12654 t = "bdev";
12655# endif
12656# ifdef S_ISCHR
12657 else if (S_ISCHR(st.st_mode))
12658 t = "cdev";
12659# endif
12660# ifdef S_ISFIFO
12661 else if (S_ISFIFO(st.st_mode))
12662 t = "fifo";
12663# endif
12664# ifdef S_ISSOCK
12665 else if (S_ISSOCK(st.st_mode))
12666 t = "fifo";
12667# endif
12668 else
12669 t = "other";
12670#else
12671# ifdef S_IFMT
12672 switch (st.st_mode & S_IFMT)
12673 {
12674 case S_IFREG: t = "file"; break;
12675 case S_IFDIR: t = "dir"; break;
12676# ifdef S_IFLNK
12677 case S_IFLNK: t = "link"; break;
12678# endif
12679# ifdef S_IFBLK
12680 case S_IFBLK: t = "bdev"; break;
12681# endif
12682# ifdef S_IFCHR
12683 case S_IFCHR: t = "cdev"; break;
12684# endif
12685# ifdef S_IFIFO
12686 case S_IFIFO: t = "fifo"; break;
12687# endif
12688# ifdef S_IFSOCK
12689 case S_IFSOCK: t = "socket"; break;
12690# endif
12691 default: t = "other";
12692 }
12693# else
12694 if (mch_isdir(fname))
12695 t = "dir";
12696 else
12697 t = "file";
12698# endif
12699#endif
12700 type = vim_strsave((char_u *)t);
12701 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012702 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012703}
12704
12705/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012706 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012707 */
12708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012709f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012710{
12711 linenr_T lnum;
12712 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012713 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012714
12715 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012716 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012717 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012718 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012719 retlist = FALSE;
12720 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012722 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012723 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012724 retlist = TRUE;
12725 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012726
Bram Moolenaar342337a2005-07-21 21:11:17 +000012727 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012728}
12729
12730/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012731 * "getmatches()" function
12732 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012733 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012734f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012735{
12736#ifdef FEAT_SEARCH_EXTRA
12737 dict_T *dict;
12738 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012739 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012740
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012741 if (rettv_list_alloc(rettv) == OK)
12742 {
12743 while (cur != NULL)
12744 {
12745 dict = dict_alloc();
12746 if (dict == NULL)
12747 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012748 if (cur->match.regprog == NULL)
12749 {
12750 /* match added with matchaddpos() */
12751 for (i = 0; i < MAXPOSMATCH; ++i)
12752 {
12753 llpos_T *llpos;
12754 char buf[6];
12755 list_T *l;
12756
12757 llpos = &cur->pos.pos[i];
12758 if (llpos->lnum == 0)
12759 break;
12760 l = list_alloc();
12761 if (l == NULL)
12762 break;
12763 list_append_number(l, (varnumber_T)llpos->lnum);
12764 if (llpos->col > 0)
12765 {
12766 list_append_number(l, (varnumber_T)llpos->col);
12767 list_append_number(l, (varnumber_T)llpos->len);
12768 }
12769 sprintf(buf, "pos%d", i + 1);
12770 dict_add_list(dict, buf, l);
12771 }
12772 }
12773 else
12774 {
12775 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12776 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012777 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012778 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12779 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012780# ifdef FEAT_CONCEAL
12781 if (cur->conceal_char)
12782 {
12783 char_u buf[MB_MAXBYTES + 1];
12784
12785 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12786 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12787 }
12788# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012789 list_append_dict(rettv->vval.v_list, dict);
12790 cur = cur->next;
12791 }
12792 }
12793#endif
12794}
12795
12796/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012797 * "getpid()" function
12798 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012800f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012801{
12802 rettv->vval.v_number = mch_get_pid();
12803}
12804
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012805static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012806
12807/*
12808 * "getcurpos()" function
12809 */
12810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012811f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012812{
12813 getpos_both(argvars, rettv, TRUE);
12814}
12815
Bram Moolenaar18081e32008-02-20 19:11:07 +000012816/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012817 * "getpos(string)" function
12818 */
12819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012820f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012821{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012822 getpos_both(argvars, rettv, FALSE);
12823}
12824
12825 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012826getpos_both(
12827 typval_T *argvars,
12828 typval_T *rettv,
12829 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012830{
Bram Moolenaara5525202006-03-02 22:52:09 +000012831 pos_T *fp;
12832 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012833 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012834
12835 if (rettv_list_alloc(rettv) == OK)
12836 {
12837 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012838 if (getcurpos)
12839 fp = &curwin->w_cursor;
12840 else
12841 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012842 if (fnum != -1)
12843 list_append_number(l, (varnumber_T)fnum);
12844 else
12845 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012846 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12847 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012848 list_append_number(l, (fp != NULL)
12849 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012850 : (varnumber_T)0);
12851 list_append_number(l,
12852#ifdef FEAT_VIRTUALEDIT
12853 (fp != NULL) ? (varnumber_T)fp->coladd :
12854#endif
12855 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012856 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012857 {
12858 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012859 list_append_number(l, curwin->w_curswant == MAXCOL ?
12860 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012861 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012862 }
12863 else
12864 rettv->vval.v_number = FALSE;
12865}
12866
12867/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012868 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012869 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012870 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012871f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012872{
12873#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012874 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012875#endif
12876
Bram Moolenaar2641f772005-03-25 21:58:17 +000012877#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012878 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012879 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012880 wp = NULL;
12881 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12882 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012883 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012884 if (wp == NULL)
12885 return;
12886 }
12887
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012888 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012889 }
12890#endif
12891}
12892
12893/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012894 * "getreg()" function
12895 */
12896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012897f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012898{
12899 char_u *strregname;
12900 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012901 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012902 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012903 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012904
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012905 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012906 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012907 strregname = get_tv_string_chk(&argvars[0]);
12908 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012909 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012910 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012911 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012912 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12913 return_list = get_tv_number_chk(&argvars[2], &error);
12914 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012915 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012916 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012917 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012918
12919 if (error)
12920 return;
12921
Bram Moolenaar071d4272004-06-13 20:20:40 +000012922 regname = (strregname == NULL ? '"' : *strregname);
12923 if (regname == 0)
12924 regname = '"';
12925
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012926 if (return_list)
12927 {
12928 rettv->v_type = VAR_LIST;
12929 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12930 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012931 if (rettv->vval.v_list != NULL)
12932 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012933 }
12934 else
12935 {
12936 rettv->v_type = VAR_STRING;
12937 rettv->vval.v_string = get_reg_contents(regname,
12938 arg2 ? GREG_EXPR_SRC : 0);
12939 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012940}
12941
12942/*
12943 * "getregtype()" function
12944 */
12945 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012946f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012947{
12948 char_u *strregname;
12949 int regname;
12950 char_u buf[NUMBUFLEN + 2];
12951 long reglen = 0;
12952
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012953 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012954 {
12955 strregname = get_tv_string_chk(&argvars[0]);
12956 if (strregname == NULL) /* type error; errmsg already given */
12957 {
12958 rettv->v_type = VAR_STRING;
12959 rettv->vval.v_string = NULL;
12960 return;
12961 }
12962 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012963 else
12964 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012965 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012966
12967 regname = (strregname == NULL ? '"' : *strregname);
12968 if (regname == 0)
12969 regname = '"';
12970
12971 buf[0] = NUL;
12972 buf[1] = NUL;
12973 switch (get_reg_type(regname, &reglen))
12974 {
12975 case MLINE: buf[0] = 'V'; break;
12976 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012977 case MBLOCK:
12978 buf[0] = Ctrl_V;
12979 sprintf((char *)buf + 1, "%ld", reglen + 1);
12980 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012981 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012982 rettv->v_type = VAR_STRING;
12983 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012984}
12985
12986/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012987 * "gettabvar()" function
12988 */
12989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012990f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012991{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012992 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012993 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012994 dictitem_T *v;
12995 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012996 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012997
12998 rettv->v_type = VAR_STRING;
12999 rettv->vval.v_string = NULL;
13000
13001 varname = get_tv_string_chk(&argvars[1]);
13002 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13003 if (tp != NULL && varname != NULL)
13004 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013005 /* Set tp to be our tabpage, temporarily. Also set the window to the
13006 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013007 if (switch_win(&oldcurwin, &oldtabpage,
13008 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013009 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013010 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013011 /* look up the variable */
13012 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13013 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13014 if (v != NULL)
13015 {
13016 copy_tv(&v->di_tv, rettv);
13017 done = TRUE;
13018 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013019 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013020
13021 /* restore previous notion of curwin */
13022 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013023 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013024
13025 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013026 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013027}
13028
13029/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013030 * "gettabwinvar()" function
13031 */
13032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013033f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013034{
13035 getwinvar(argvars, rettv, 1);
13036}
13037
13038/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 * "getwinposx()" function
13040 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013042f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013044 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013045#ifdef FEAT_GUI
13046 if (gui.in_use)
13047 {
13048 int x, y;
13049
13050 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013051 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013052 }
13053#endif
13054}
13055
13056/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013057 * "win_findbuf()" function
13058 */
13059 static void
13060f_win_findbuf(typval_T *argvars, typval_T *rettv)
13061{
13062 if (rettv_list_alloc(rettv) != FAIL)
13063 win_findbuf(argvars, rettv->vval.v_list);
13064}
13065
13066/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013067 * "win_getid()" function
13068 */
13069 static void
13070f_win_getid(typval_T *argvars, typval_T *rettv)
13071{
13072 rettv->vval.v_number = win_getid(argvars);
13073}
13074
13075/*
13076 * "win_gotoid()" function
13077 */
13078 static void
13079f_win_gotoid(typval_T *argvars, typval_T *rettv)
13080{
13081 rettv->vval.v_number = win_gotoid(argvars);
13082}
13083
13084/*
13085 * "win_id2tabwin()" function
13086 */
13087 static void
13088f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13089{
13090 if (rettv_list_alloc(rettv) != FAIL)
13091 win_id2tabwin(argvars, rettv->vval.v_list);
13092}
13093
13094/*
13095 * "win_id2win()" function
13096 */
13097 static void
13098f_win_id2win(typval_T *argvars, typval_T *rettv)
13099{
13100 rettv->vval.v_number = win_id2win(argvars);
13101}
13102
13103/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 * "getwinposy()" function
13105 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013107f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013110#ifdef FEAT_GUI
13111 if (gui.in_use)
13112 {
13113 int x, y;
13114
13115 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013116 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013117 }
13118#endif
13119}
13120
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013121/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013122 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013123 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013124 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013125find_win_by_nr(
13126 typval_T *vp,
13127 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013128{
13129#ifdef FEAT_WINDOWS
13130 win_T *wp;
13131#endif
13132 int nr;
13133
13134 nr = get_tv_number_chk(vp, NULL);
13135
13136#ifdef FEAT_WINDOWS
13137 if (nr < 0)
13138 return NULL;
13139 if (nr == 0)
13140 return curwin;
13141
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013142 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13143 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013144 if (--nr <= 0)
13145 break;
13146 return wp;
13147#else
13148 if (nr == 0 || nr == 1)
13149 return curwin;
13150 return NULL;
13151#endif
13152}
13153
Bram Moolenaar071d4272004-06-13 20:20:40 +000013154/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013155 * Find window specified by "wvp" in tabpage "tvp".
13156 */
13157 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013158find_tabwin(
13159 typval_T *wvp, /* VAR_UNKNOWN for current window */
13160 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013161{
13162 win_T *wp = NULL;
13163 tabpage_T *tp = NULL;
13164 long n;
13165
13166 if (wvp->v_type != VAR_UNKNOWN)
13167 {
13168 if (tvp->v_type != VAR_UNKNOWN)
13169 {
13170 n = get_tv_number(tvp);
13171 if (n >= 0)
13172 tp = find_tabpage(n);
13173 }
13174 else
13175 tp = curtab;
13176
13177 if (tp != NULL)
13178 wp = find_win_by_nr(wvp, tp);
13179 }
13180 else
13181 wp = curwin;
13182
13183 return wp;
13184}
13185
13186/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013187 * "getwinvar()" function
13188 */
13189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013190f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013191{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013192 getwinvar(argvars, rettv, 0);
13193}
13194
13195/*
13196 * getwinvar() and gettabwinvar()
13197 */
13198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013199getwinvar(
13200 typval_T *argvars,
13201 typval_T *rettv,
13202 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013203{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013204 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013205 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013206 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013207 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013208 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013209#ifdef FEAT_WINDOWS
13210 win_T *oldcurwin;
13211 tabpage_T *oldtabpage;
13212 int need_switch_win;
13213#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013214
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013215#ifdef FEAT_WINDOWS
13216 if (off == 1)
13217 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13218 else
13219 tp = curtab;
13220#endif
13221 win = find_win_by_nr(&argvars[off], tp);
13222 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013223 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013224
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013225 rettv->v_type = VAR_STRING;
13226 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227
13228 if (win != NULL && varname != NULL)
13229 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013230#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013231 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013232 * otherwise the window is not valid. Only do this when needed,
13233 * autocommands get blocked. */
13234 need_switch_win = !(tp == curtab && win == curwin);
13235 if (!need_switch_win
13236 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13237#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013238 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013239 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013240 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013241 if (get_option_tv(&varname, rettv, 1) == OK)
13242 done = TRUE;
13243 }
13244 else
13245 {
13246 /* Look up the variable. */
13247 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13248 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13249 varname, FALSE);
13250 if (v != NULL)
13251 {
13252 copy_tv(&v->di_tv, rettv);
13253 done = TRUE;
13254 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013255 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013256 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013257
Bram Moolenaarba117c22015-09-29 16:53:22 +020013258#ifdef FEAT_WINDOWS
13259 if (need_switch_win)
13260 /* restore previous notion of curwin */
13261 restore_win(oldcurwin, oldtabpage, TRUE);
13262#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013263 }
13264
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013265 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13266 /* use the default return value */
13267 copy_tv(&argvars[off + 2], rettv);
13268
Bram Moolenaar071d4272004-06-13 20:20:40 +000013269 --emsg_off;
13270}
13271
13272/*
13273 * "glob()" function
13274 */
13275 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013276f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013278 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013280 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013281
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013282 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013283 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013284 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013285 if (argvars[1].v_type != VAR_UNKNOWN)
13286 {
13287 if (get_tv_number_chk(&argvars[1], &error))
13288 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013289 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013290 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013291 if (get_tv_number_chk(&argvars[2], &error))
13292 {
13293 rettv->v_type = VAR_LIST;
13294 rettv->vval.v_list = NULL;
13295 }
13296 if (argvars[3].v_type != VAR_UNKNOWN
13297 && get_tv_number_chk(&argvars[3], &error))
13298 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013299 }
13300 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013301 if (!error)
13302 {
13303 ExpandInit(&xpc);
13304 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013305 if (p_wic)
13306 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013307 if (rettv->v_type == VAR_STRING)
13308 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013309 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013310 else if (rettv_list_alloc(rettv) != FAIL)
13311 {
13312 int i;
13313
13314 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13315 NULL, options, WILD_ALL_KEEP);
13316 for (i = 0; i < xpc.xp_numfiles; i++)
13317 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13318
13319 ExpandCleanup(&xpc);
13320 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013321 }
13322 else
13323 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013324}
13325
13326/*
13327 * "globpath()" function
13328 */
13329 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013330f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013332 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013333 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013334 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013335 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013336 garray_T ga;
13337 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013338
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013339 /* When the optional second argument is non-zero, don't remove matches
13340 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013341 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013342 if (argvars[2].v_type != VAR_UNKNOWN)
13343 {
13344 if (get_tv_number_chk(&argvars[2], &error))
13345 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013346 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013347 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013348 if (get_tv_number_chk(&argvars[3], &error))
13349 {
13350 rettv->v_type = VAR_LIST;
13351 rettv->vval.v_list = NULL;
13352 }
13353 if (argvars[4].v_type != VAR_UNKNOWN
13354 && get_tv_number_chk(&argvars[4], &error))
13355 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013356 }
13357 }
13358 if (file != NULL && !error)
13359 {
13360 ga_init2(&ga, (int)sizeof(char_u *), 10);
13361 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13362 if (rettv->v_type == VAR_STRING)
13363 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13364 else if (rettv_list_alloc(rettv) != FAIL)
13365 for (i = 0; i < ga.ga_len; ++i)
13366 list_append_string(rettv->vval.v_list,
13367 ((char_u **)(ga.ga_data))[i], -1);
13368 ga_clear_strings(&ga);
13369 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013370 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013371 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013372}
13373
13374/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013375 * "glob2regpat()" function
13376 */
13377 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013378f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013379{
13380 char_u *pat = get_tv_string_chk(&argvars[0]);
13381
13382 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013383 rettv->vval.v_string = (pat == NULL)
13384 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013385}
13386
13387/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013388 * "has()" function
13389 */
13390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013391f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013392{
13393 int i;
13394 char_u *name;
13395 int n = FALSE;
13396 static char *(has_list[]) =
13397 {
13398#ifdef AMIGA
13399 "amiga",
13400# ifdef FEAT_ARP
13401 "arp",
13402# endif
13403#endif
13404#ifdef __BEOS__
13405 "beos",
13406#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013407#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013408 "mac",
13409#endif
13410#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013411 "macunix", /* built with 'darwin' enabled */
13412#endif
13413#if defined(__APPLE__) && __APPLE__ == 1
13414 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013415#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013416#ifdef __QNX__
13417 "qnx",
13418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419#ifdef UNIX
13420 "unix",
13421#endif
13422#ifdef VMS
13423 "vms",
13424#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013425#ifdef WIN32
13426 "win32",
13427#endif
13428#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13429 "win32unix",
13430#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013431#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013432 "win64",
13433#endif
13434#ifdef EBCDIC
13435 "ebcdic",
13436#endif
13437#ifndef CASE_INSENSITIVE_FILENAME
13438 "fname_case",
13439#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013440#ifdef HAVE_ACL
13441 "acl",
13442#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013443#ifdef FEAT_ARABIC
13444 "arabic",
13445#endif
13446#ifdef FEAT_AUTOCMD
13447 "autocmd",
13448#endif
13449#ifdef FEAT_BEVAL
13450 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013451# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13452 "balloon_multiline",
13453# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013454#endif
13455#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13456 "builtin_terms",
13457# ifdef ALL_BUILTIN_TCAPS
13458 "all_builtin_terms",
13459# endif
13460#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013461#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13462 || defined(FEAT_GUI_W32) \
13463 || defined(FEAT_GUI_MOTIF))
13464 "browsefilter",
13465#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013466#ifdef FEAT_BYTEOFF
13467 "byte_offset",
13468#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013469#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013470 "channel",
13471#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472#ifdef FEAT_CINDENT
13473 "cindent",
13474#endif
13475#ifdef FEAT_CLIENTSERVER
13476 "clientserver",
13477#endif
13478#ifdef FEAT_CLIPBOARD
13479 "clipboard",
13480#endif
13481#ifdef FEAT_CMDL_COMPL
13482 "cmdline_compl",
13483#endif
13484#ifdef FEAT_CMDHIST
13485 "cmdline_hist",
13486#endif
13487#ifdef FEAT_COMMENTS
13488 "comments",
13489#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013490#ifdef FEAT_CONCEAL
13491 "conceal",
13492#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013493#ifdef FEAT_CRYPT
13494 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013495 "crypt-blowfish",
13496 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013497#endif
13498#ifdef FEAT_CSCOPE
13499 "cscope",
13500#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013501#ifdef FEAT_CURSORBIND
13502 "cursorbind",
13503#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013504#ifdef CURSOR_SHAPE
13505 "cursorshape",
13506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013507#ifdef DEBUG
13508 "debug",
13509#endif
13510#ifdef FEAT_CON_DIALOG
13511 "dialog_con",
13512#endif
13513#ifdef FEAT_GUI_DIALOG
13514 "dialog_gui",
13515#endif
13516#ifdef FEAT_DIFF
13517 "diff",
13518#endif
13519#ifdef FEAT_DIGRAPHS
13520 "digraphs",
13521#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013522#ifdef FEAT_DIRECTX
13523 "directx",
13524#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013525#ifdef FEAT_DND
13526 "dnd",
13527#endif
13528#ifdef FEAT_EMACS_TAGS
13529 "emacs_tags",
13530#endif
13531 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013532 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013533#ifdef FEAT_SEARCH_EXTRA
13534 "extra_search",
13535#endif
13536#ifdef FEAT_FKMAP
13537 "farsi",
13538#endif
13539#ifdef FEAT_SEARCHPATH
13540 "file_in_path",
13541#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013542#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013543 "filterpipe",
13544#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013545#ifdef FEAT_FIND_ID
13546 "find_in_path",
13547#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013548#ifdef FEAT_FLOAT
13549 "float",
13550#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013551#ifdef FEAT_FOLDING
13552 "folding",
13553#endif
13554#ifdef FEAT_FOOTER
13555 "footer",
13556#endif
13557#if !defined(USE_SYSTEM) && defined(UNIX)
13558 "fork",
13559#endif
13560#ifdef FEAT_GETTEXT
13561 "gettext",
13562#endif
13563#ifdef FEAT_GUI
13564 "gui",
13565#endif
13566#ifdef FEAT_GUI_ATHENA
13567# ifdef FEAT_GUI_NEXTAW
13568 "gui_neXtaw",
13569# else
13570 "gui_athena",
13571# endif
13572#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013573#ifdef FEAT_GUI_GTK
13574 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013575# ifdef USE_GTK3
13576 "gui_gtk3",
13577# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013579# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013580#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013581#ifdef FEAT_GUI_GNOME
13582 "gui_gnome",
13583#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013584#ifdef FEAT_GUI_MAC
13585 "gui_mac",
13586#endif
13587#ifdef FEAT_GUI_MOTIF
13588 "gui_motif",
13589#endif
13590#ifdef FEAT_GUI_PHOTON
13591 "gui_photon",
13592#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593#ifdef FEAT_GUI_W32
13594 "gui_win32",
13595#endif
13596#ifdef FEAT_HANGULIN
13597 "hangul_input",
13598#endif
13599#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13600 "iconv",
13601#endif
13602#ifdef FEAT_INS_EXPAND
13603 "insert_expand",
13604#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013605#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013606 "job",
13607#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013608#ifdef FEAT_JUMPLIST
13609 "jumplist",
13610#endif
13611#ifdef FEAT_KEYMAP
13612 "keymap",
13613#endif
13614#ifdef FEAT_LANGMAP
13615 "langmap",
13616#endif
13617#ifdef FEAT_LIBCALL
13618 "libcall",
13619#endif
13620#ifdef FEAT_LINEBREAK
13621 "linebreak",
13622#endif
13623#ifdef FEAT_LISP
13624 "lispindent",
13625#endif
13626#ifdef FEAT_LISTCMDS
13627 "listcmds",
13628#endif
13629#ifdef FEAT_LOCALMAP
13630 "localmap",
13631#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013632#ifdef FEAT_LUA
13633# ifndef DYNAMIC_LUA
13634 "lua",
13635# endif
13636#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013637#ifdef FEAT_MENU
13638 "menu",
13639#endif
13640#ifdef FEAT_SESSION
13641 "mksession",
13642#endif
13643#ifdef FEAT_MODIFY_FNAME
13644 "modify_fname",
13645#endif
13646#ifdef FEAT_MOUSE
13647 "mouse",
13648#endif
13649#ifdef FEAT_MOUSESHAPE
13650 "mouseshape",
13651#endif
13652#if defined(UNIX) || defined(VMS)
13653# ifdef FEAT_MOUSE_DEC
13654 "mouse_dec",
13655# endif
13656# ifdef FEAT_MOUSE_GPM
13657 "mouse_gpm",
13658# endif
13659# ifdef FEAT_MOUSE_JSB
13660 "mouse_jsbterm",
13661# endif
13662# ifdef FEAT_MOUSE_NET
13663 "mouse_netterm",
13664# endif
13665# ifdef FEAT_MOUSE_PTERM
13666 "mouse_pterm",
13667# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013668# ifdef FEAT_MOUSE_SGR
13669 "mouse_sgr",
13670# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013671# ifdef FEAT_SYSMOUSE
13672 "mouse_sysmouse",
13673# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013674# ifdef FEAT_MOUSE_URXVT
13675 "mouse_urxvt",
13676# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013677# ifdef FEAT_MOUSE_XTERM
13678 "mouse_xterm",
13679# endif
13680#endif
13681#ifdef FEAT_MBYTE
13682 "multi_byte",
13683#endif
13684#ifdef FEAT_MBYTE_IME
13685 "multi_byte_ime",
13686#endif
13687#ifdef FEAT_MULTI_LANG
13688 "multi_lang",
13689#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013690#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013691#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013692 "mzscheme",
13693#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013694#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013695#ifdef FEAT_OLE
13696 "ole",
13697#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013698 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013699#ifdef FEAT_PATH_EXTRA
13700 "path_extra",
13701#endif
13702#ifdef FEAT_PERL
13703#ifndef DYNAMIC_PERL
13704 "perl",
13705#endif
13706#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013707#ifdef FEAT_PERSISTENT_UNDO
13708 "persistent_undo",
13709#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013710#ifdef FEAT_PYTHON
13711#ifndef DYNAMIC_PYTHON
13712 "python",
13713#endif
13714#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013715#ifdef FEAT_PYTHON3
13716#ifndef DYNAMIC_PYTHON3
13717 "python3",
13718#endif
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720#ifdef FEAT_POSTSCRIPT
13721 "postscript",
13722#endif
13723#ifdef FEAT_PRINTER
13724 "printer",
13725#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013726#ifdef FEAT_PROFILE
13727 "profile",
13728#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013729#ifdef FEAT_RELTIME
13730 "reltime",
13731#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013732#ifdef FEAT_QUICKFIX
13733 "quickfix",
13734#endif
13735#ifdef FEAT_RIGHTLEFT
13736 "rightleft",
13737#endif
13738#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13739 "ruby",
13740#endif
13741#ifdef FEAT_SCROLLBIND
13742 "scrollbind",
13743#endif
13744#ifdef FEAT_CMDL_INFO
13745 "showcmd",
13746 "cmdline_info",
13747#endif
13748#ifdef FEAT_SIGNS
13749 "signs",
13750#endif
13751#ifdef FEAT_SMARTINDENT
13752 "smartindent",
13753#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013754#ifdef STARTUPTIME
13755 "startuptime",
13756#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013757#ifdef FEAT_STL_OPT
13758 "statusline",
13759#endif
13760#ifdef FEAT_SUN_WORKSHOP
13761 "sun_workshop",
13762#endif
13763#ifdef FEAT_NETBEANS_INTG
13764 "netbeans_intg",
13765#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013766#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013767 "spell",
13768#endif
13769#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013770 "syntax",
13771#endif
13772#if defined(USE_SYSTEM) || !defined(UNIX)
13773 "system",
13774#endif
13775#ifdef FEAT_TAG_BINS
13776 "tag_binary",
13777#endif
13778#ifdef FEAT_TAG_OLDSTATIC
13779 "tag_old_static",
13780#endif
13781#ifdef FEAT_TAG_ANYWHITE
13782 "tag_any_white",
13783#endif
13784#ifdef FEAT_TCL
13785# ifndef DYNAMIC_TCL
13786 "tcl",
13787# endif
13788#endif
13789#ifdef TERMINFO
13790 "terminfo",
13791#endif
13792#ifdef FEAT_TERMRESPONSE
13793 "termresponse",
13794#endif
13795#ifdef FEAT_TEXTOBJ
13796 "textobjects",
13797#endif
13798#ifdef HAVE_TGETENT
13799 "tgetent",
13800#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013801#ifdef FEAT_TIMERS
13802 "timers",
13803#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013804#ifdef FEAT_TITLE
13805 "title",
13806#endif
13807#ifdef FEAT_TOOLBAR
13808 "toolbar",
13809#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013810#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13811 "unnamedplus",
13812#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013813#ifdef FEAT_USR_CMDS
13814 "user-commands", /* was accidentally included in 5.4 */
13815 "user_commands",
13816#endif
13817#ifdef FEAT_VIMINFO
13818 "viminfo",
13819#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013820#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013821 "vertsplit",
13822#endif
13823#ifdef FEAT_VIRTUALEDIT
13824 "virtualedit",
13825#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013826 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013827#ifdef FEAT_VISUALEXTRA
13828 "visualextra",
13829#endif
13830#ifdef FEAT_VREPLACE
13831 "vreplace",
13832#endif
13833#ifdef FEAT_WILDIGN
13834 "wildignore",
13835#endif
13836#ifdef FEAT_WILDMENU
13837 "wildmenu",
13838#endif
13839#ifdef FEAT_WINDOWS
13840 "windows",
13841#endif
13842#ifdef FEAT_WAK
13843 "winaltkeys",
13844#endif
13845#ifdef FEAT_WRITEBACKUP
13846 "writebackup",
13847#endif
13848#ifdef FEAT_XIM
13849 "xim",
13850#endif
13851#ifdef FEAT_XFONTSET
13852 "xfontset",
13853#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013854#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013855 "xpm",
13856 "xpm_w32", /* for backward compatibility */
13857#else
13858# if defined(HAVE_XPM)
13859 "xpm",
13860# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013861#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013862#ifdef USE_XSMP
13863 "xsmp",
13864#endif
13865#ifdef USE_XSMP_INTERACT
13866 "xsmp_interact",
13867#endif
13868#ifdef FEAT_XCLIPBOARD
13869 "xterm_clipboard",
13870#endif
13871#ifdef FEAT_XTERM_SAVE
13872 "xterm_save",
13873#endif
13874#if defined(UNIX) && defined(FEAT_X11)
13875 "X11",
13876#endif
13877 NULL
13878 };
13879
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013880 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013881 for (i = 0; has_list[i] != NULL; ++i)
13882 if (STRICMP(name, has_list[i]) == 0)
13883 {
13884 n = TRUE;
13885 break;
13886 }
13887
13888 if (n == FALSE)
13889 {
13890 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013891 {
13892 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013893 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013894 && vim_isdigit(name[6])
13895 && vim_isdigit(name[8])
13896 && vim_isdigit(name[10]))
13897 {
13898 int major = atoi((char *)name + 6);
13899 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013900
13901 /* Expect "patch-9.9.01234". */
13902 n = (major < VIM_VERSION_MAJOR
13903 || (major == VIM_VERSION_MAJOR
13904 && (minor < VIM_VERSION_MINOR
13905 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013906 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013907 }
13908 else
13909 n = has_patch(atoi((char *)name + 5));
13910 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 else if (STRICMP(name, "vim_starting") == 0)
13912 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013913#ifdef FEAT_MBYTE
13914 else if (STRICMP(name, "multi_byte_encoding") == 0)
13915 n = has_mbyte;
13916#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013917#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13918 else if (STRICMP(name, "balloon_multiline") == 0)
13919 n = multiline_balloon_available();
13920#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013921#ifdef DYNAMIC_TCL
13922 else if (STRICMP(name, "tcl") == 0)
13923 n = tcl_enabled(FALSE);
13924#endif
13925#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13926 else if (STRICMP(name, "iconv") == 0)
13927 n = iconv_enabled(FALSE);
13928#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013929#ifdef DYNAMIC_LUA
13930 else if (STRICMP(name, "lua") == 0)
13931 n = lua_enabled(FALSE);
13932#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013933#ifdef DYNAMIC_MZSCHEME
13934 else if (STRICMP(name, "mzscheme") == 0)
13935 n = mzscheme_enabled(FALSE);
13936#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013937#ifdef DYNAMIC_RUBY
13938 else if (STRICMP(name, "ruby") == 0)
13939 n = ruby_enabled(FALSE);
13940#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013941#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942#ifdef DYNAMIC_PYTHON
13943 else if (STRICMP(name, "python") == 0)
13944 n = python_enabled(FALSE);
13945#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013946#endif
13947#ifdef FEAT_PYTHON3
13948#ifdef DYNAMIC_PYTHON3
13949 else if (STRICMP(name, "python3") == 0)
13950 n = python3_enabled(FALSE);
13951#endif
13952#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013953#ifdef DYNAMIC_PERL
13954 else if (STRICMP(name, "perl") == 0)
13955 n = perl_enabled(FALSE);
13956#endif
13957#ifdef FEAT_GUI
13958 else if (STRICMP(name, "gui_running") == 0)
13959 n = (gui.in_use || gui.starting);
13960# ifdef FEAT_GUI_W32
13961 else if (STRICMP(name, "gui_win32s") == 0)
13962 n = gui_is_win32s();
13963# endif
13964# ifdef FEAT_BROWSE
13965 else if (STRICMP(name, "browse") == 0)
13966 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13967# endif
13968#endif
13969#ifdef FEAT_SYN_HL
13970 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013971 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972#endif
13973#if defined(WIN3264)
13974 else if (STRICMP(name, "win95") == 0)
13975 n = mch_windows95();
13976#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013977#ifdef FEAT_NETBEANS_INTG
13978 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013979 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013980#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013981 }
13982
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013983 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984}
13985
13986/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013987 * "has_key()" function
13988 */
13989 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013990f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013991{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013992 if (argvars[0].v_type != VAR_DICT)
13993 {
13994 EMSG(_(e_dictreq));
13995 return;
13996 }
13997 if (argvars[0].vval.v_dict == NULL)
13998 return;
13999
14000 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014001 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014002}
14003
14004/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014005 * "haslocaldir()" function
14006 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014008f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014009{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014010 win_T *wp = NULL;
14011
14012 wp = find_tabwin(&argvars[0], &argvars[1]);
14013 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014014}
14015
14016/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014017 * "hasmapto()" function
14018 */
14019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014020f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014021{
14022 char_u *name;
14023 char_u *mode;
14024 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014025 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014027 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014028 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014029 mode = (char_u *)"nvo";
14030 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014031 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014032 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014033 if (argvars[2].v_type != VAR_UNKNOWN)
14034 abbr = get_tv_number(&argvars[2]);
14035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014036
Bram Moolenaar2c932302006-03-18 21:42:09 +000014037 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014040 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014041}
14042
14043/*
14044 * "histadd()" function
14045 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014047f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014048{
14049#ifdef FEAT_CMDHIST
14050 int histype;
14051 char_u *str;
14052 char_u buf[NUMBUFLEN];
14053#endif
14054
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014055 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014056 if (check_restricted() || check_secure())
14057 return;
14058#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014059 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14060 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014061 if (histype >= 0)
14062 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014063 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014064 if (*str != NUL)
14065 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014066 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014068 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 return;
14070 }
14071 }
14072#endif
14073}
14074
14075/*
14076 * "histdel()" function
14077 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014079f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014080{
14081#ifdef FEAT_CMDHIST
14082 int n;
14083 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014085
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014086 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14087 if (str == NULL)
14088 n = 0;
14089 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014090 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014091 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014092 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014093 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014094 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014095 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014096 else
14097 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014098 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014099 get_tv_string_buf(&argvars[1], buf));
14100 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014101#endif
14102}
14103
14104/*
14105 * "histget()" function
14106 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014108f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014109{
14110#ifdef FEAT_CMDHIST
14111 int type;
14112 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014114
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014115 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14116 if (str == NULL)
14117 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014118 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014119 {
14120 type = get_histtype(str);
14121 if (argvars[1].v_type == VAR_UNKNOWN)
14122 idx = get_history_idx(type);
14123 else
14124 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14125 /* -1 on type error */
14126 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14127 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014131 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014132}
14133
14134/*
14135 * "histnr()" function
14136 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014138f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014139{
14140 int i;
14141
14142#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014143 char_u *history = get_tv_string_chk(&argvars[0]);
14144
14145 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014146 if (i >= HIST_CMD && i < HIST_COUNT)
14147 i = get_history_idx(i);
14148 else
14149#endif
14150 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014151 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014152}
14153
14154/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014155 * "highlightID(name)" function
14156 */
14157 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014158f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014160 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014161}
14162
14163/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014164 * "highlight_exists()" function
14165 */
14166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014167f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014168{
14169 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14170}
14171
14172/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 * "hostname()" function
14174 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014176f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177{
14178 char_u hostname[256];
14179
14180 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014181 rettv->v_type = VAR_STRING;
14182 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014183}
14184
14185/*
14186 * iconv() function
14187 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014189f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190{
14191#ifdef FEAT_MBYTE
14192 char_u buf1[NUMBUFLEN];
14193 char_u buf2[NUMBUFLEN];
14194 char_u *from, *to, *str;
14195 vimconv_T vimconv;
14196#endif
14197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 rettv->v_type = VAR_STRING;
14199 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200
14201#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014202 str = get_tv_string(&argvars[0]);
14203 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14204 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205 vimconv.vc_type = CONV_NONE;
14206 convert_setup(&vimconv, from, to);
14207
14208 /* If the encodings are equal, no conversion needed. */
14209 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014212 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014213
14214 convert_setup(&vimconv, NULL, NULL);
14215 vim_free(from);
14216 vim_free(to);
14217#endif
14218}
14219
14220/*
14221 * "indent()" function
14222 */
14223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014224f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014225{
14226 linenr_T lnum;
14227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014232 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014233}
14234
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014235/*
14236 * "index()" function
14237 */
14238 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014239f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014240{
Bram Moolenaar33570922005-01-25 22:26:29 +000014241 list_T *l;
14242 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014243 long idx = 0;
14244 int ic = FALSE;
14245
14246 rettv->vval.v_number = -1;
14247 if (argvars[0].v_type != VAR_LIST)
14248 {
14249 EMSG(_(e_listreq));
14250 return;
14251 }
14252 l = argvars[0].vval.v_list;
14253 if (l != NULL)
14254 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014255 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014256 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014257 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014258 int error = FALSE;
14259
Bram Moolenaar758711c2005-02-02 23:11:38 +000014260 /* Start at specified item. Use the cached index that list_find()
14261 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014262 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014263 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014264 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014265 ic = get_tv_number_chk(&argvars[3], &error);
14266 if (error)
14267 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014268 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014269
Bram Moolenaar758711c2005-02-02 23:11:38 +000014270 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014271 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014272 {
14273 rettv->vval.v_number = idx;
14274 break;
14275 }
14276 }
14277}
14278
Bram Moolenaar071d4272004-06-13 20:20:40 +000014279static int inputsecret_flag = 0;
14280
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014281static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014282
Bram Moolenaar071d4272004-06-13 20:20:40 +000014283/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014284 * This function is used by f_input() and f_inputdialog() functions. The third
14285 * argument to f_input() specifies the type of completion to use at the
14286 * prompt. The third argument to f_inputdialog() specifies the value to return
14287 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014288 */
14289 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014290get_user_input(
14291 typval_T *argvars,
14292 typval_T *rettv,
14293 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014295 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014296 char_u *p = NULL;
14297 int c;
14298 char_u buf[NUMBUFLEN];
14299 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014300 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014301 int xp_type = EXPAND_NOTHING;
14302 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014304 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014306
14307#ifdef NO_CONSOLE_INPUT
14308 /* While starting up, there is no place to enter text. */
14309 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014310 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014311#endif
14312
14313 cmd_silent = FALSE; /* Want to see the prompt. */
14314 if (prompt != NULL)
14315 {
14316 /* Only the part of the message after the last NL is considered as
14317 * prompt for the command line */
14318 p = vim_strrchr(prompt, '\n');
14319 if (p == NULL)
14320 p = prompt;
14321 else
14322 {
14323 ++p;
14324 c = *p;
14325 *p = NUL;
14326 msg_start();
14327 msg_clr_eos();
14328 msg_puts_attr(prompt, echo_attr);
14329 msg_didout = FALSE;
14330 msg_starthere();
14331 *p = c;
14332 }
14333 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014334
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014335 if (argvars[1].v_type != VAR_UNKNOWN)
14336 {
14337 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14338 if (defstr != NULL)
14339 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014340
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014341 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014342 {
14343 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014344 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014345 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014346
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014347 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014348 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014349
Bram Moolenaar4463f292005-09-25 22:20:24 +000014350 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14351 if (xp_name == NULL)
14352 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014353
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014354 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014355
Bram Moolenaar4463f292005-09-25 22:20:24 +000014356 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14357 &xp_arg) == FAIL)
14358 return;
14359 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014360 }
14361
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014362 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014363 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014364 int save_ex_normal_busy = ex_normal_busy;
14365 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014366 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014367 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14368 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014369 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014370 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014371 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014372 && argvars[1].v_type != VAR_UNKNOWN
14373 && argvars[2].v_type != VAR_UNKNOWN)
14374 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14375 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014376
14377 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014379 /* since the user typed this, no need to wait for return */
14380 need_wait_return = FALSE;
14381 msg_didout = FALSE;
14382 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014383 cmd_silent = cmd_silent_save;
14384}
14385
14386/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014387 * "input()" function
14388 * Also handles inputsecret() when inputsecret is set.
14389 */
14390 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014391f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014392{
14393 get_user_input(argvars, rettv, FALSE);
14394}
14395
14396/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014397 * "inputdialog()" function
14398 */
14399 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014400f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014401{
14402#if defined(FEAT_GUI_TEXTDIALOG)
14403 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14404 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14405 {
14406 char_u *message;
14407 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014409
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014410 message = get_tv_string_chk(&argvars[0]);
14411 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014412 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014413 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014414 else
14415 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014416 if (message != NULL && defstr != NULL
14417 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014418 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014419 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014420 else
14421 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014422 if (message != NULL && defstr != NULL
14423 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014424 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014425 rettv->vval.v_string = vim_strsave(
14426 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014430 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014431 }
14432 else
14433#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014434 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014435}
14436
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014437/*
14438 * "inputlist()" function
14439 */
14440 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014441f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014442{
14443 listitem_T *li;
14444 int selected;
14445 int mouse_used;
14446
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014447#ifdef NO_CONSOLE_INPUT
14448 /* While starting up, there is no place to enter text. */
14449 if (no_console_input())
14450 return;
14451#endif
14452 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14453 {
14454 EMSG2(_(e_listarg), "inputlist()");
14455 return;
14456 }
14457
14458 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014459 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014460 lines_left = Rows; /* avoid more prompt */
14461 msg_scroll = TRUE;
14462 msg_clr_eos();
14463
14464 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14465 {
14466 msg_puts(get_tv_string(&li->li_tv));
14467 msg_putchar('\n');
14468 }
14469
14470 /* Ask for choice. */
14471 selected = prompt_for_number(&mouse_used);
14472 if (mouse_used)
14473 selected -= lines_left;
14474
14475 rettv->vval.v_number = selected;
14476}
14477
14478
Bram Moolenaar071d4272004-06-13 20:20:40 +000014479static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14480
14481/*
14482 * "inputrestore()" function
14483 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014485f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014486{
14487 if (ga_userinput.ga_len > 0)
14488 {
14489 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014490 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14491 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014492 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014493 }
14494 else if (p_verbose > 1)
14495 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014496 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014497 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014498 }
14499}
14500
14501/*
14502 * "inputsave()" function
14503 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014505f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014507 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014508 if (ga_grow(&ga_userinput, 1) == OK)
14509 {
14510 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14511 + ga_userinput.ga_len);
14512 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014513 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014514 }
14515 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014516 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014517}
14518
14519/*
14520 * "inputsecret()" function
14521 */
14522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014523f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014524{
14525 ++cmdline_star;
14526 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014527 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014528 --cmdline_star;
14529 --inputsecret_flag;
14530}
14531
14532/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014533 * "insert()" function
14534 */
14535 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014536f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014537{
14538 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014539 listitem_T *item;
14540 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014541 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014542
14543 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014544 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014545 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014546 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014547 {
14548 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014549 before = get_tv_number_chk(&argvars[2], &error);
14550 if (error)
14551 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014552
Bram Moolenaar758711c2005-02-02 23:11:38 +000014553 if (before == l->lv_len)
14554 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014555 else
14556 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014557 item = list_find(l, before);
14558 if (item == NULL)
14559 {
14560 EMSGN(_(e_listidx), before);
14561 l = NULL;
14562 }
14563 }
14564 if (l != NULL)
14565 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014566 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014567 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014568 }
14569 }
14570}
14571
14572/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014573 * "invert(expr)" function
14574 */
14575 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014576f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014577{
14578 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14579}
14580
14581/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014582 * "isdirectory()" function
14583 */
14584 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014585f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014587 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014588}
14589
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014590/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014591 * "islocked()" function
14592 */
14593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014594f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014595{
14596 lval_T lv;
14597 char_u *end;
14598 dictitem_T *di;
14599
14600 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014601 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14602 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014603 if (end != NULL && lv.ll_name != NULL)
14604 {
14605 if (*end != NUL)
14606 EMSG(_(e_trailing));
14607 else
14608 {
14609 if (lv.ll_tv == NULL)
14610 {
14611 if (check_changedtick(lv.ll_name))
14612 rettv->vval.v_number = 1; /* always locked */
14613 else
14614 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014615 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014616 if (di != NULL)
14617 {
14618 /* Consider a variable locked when:
14619 * 1. the variable itself is locked
14620 * 2. the value of the variable is locked.
14621 * 3. the List or Dict value is locked.
14622 */
14623 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14624 || tv_islocked(&di->di_tv));
14625 }
14626 }
14627 }
14628 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014629 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014630 else if (lv.ll_newkey != NULL)
14631 EMSG2(_(e_dictkey), lv.ll_newkey);
14632 else if (lv.ll_list != NULL)
14633 /* List item. */
14634 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14635 else
14636 /* Dictionary item. */
14637 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14638 }
14639 }
14640
14641 clear_lval(&lv);
14642}
14643
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014644#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14645/*
14646 * "isnan()" function
14647 */
14648 static void
14649f_isnan(typval_T *argvars, typval_T *rettv)
14650{
14651 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14652 && isnan(argvars[0].vval.v_float);
14653}
14654#endif
14655
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014656static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014657
14658/*
14659 * Turn a dict into a list:
14660 * "what" == 0: list of keys
14661 * "what" == 1: list of values
14662 * "what" == 2: list of items
14663 */
14664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014665dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014666{
Bram Moolenaar33570922005-01-25 22:26:29 +000014667 list_T *l2;
14668 dictitem_T *di;
14669 hashitem_T *hi;
14670 listitem_T *li;
14671 listitem_T *li2;
14672 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014673 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014674
Bram Moolenaar8c711452005-01-14 21:53:12 +000014675 if (argvars[0].v_type != VAR_DICT)
14676 {
14677 EMSG(_(e_dictreq));
14678 return;
14679 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014680 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014681 return;
14682
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014683 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014684 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014685
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014686 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014687 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014689 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014691 --todo;
14692 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014693
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014694 li = listitem_alloc();
14695 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014697 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014698
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014699 if (what == 0)
14700 {
14701 /* keys() */
14702 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014703 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014704 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14705 }
14706 else if (what == 1)
14707 {
14708 /* values() */
14709 copy_tv(&di->di_tv, &li->li_tv);
14710 }
14711 else
14712 {
14713 /* items() */
14714 l2 = list_alloc();
14715 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014716 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014717 li->li_tv.vval.v_list = l2;
14718 if (l2 == NULL)
14719 break;
14720 ++l2->lv_refcount;
14721
14722 li2 = listitem_alloc();
14723 if (li2 == NULL)
14724 break;
14725 list_append(l2, li2);
14726 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014727 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014728 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14729
14730 li2 = listitem_alloc();
14731 if (li2 == NULL)
14732 break;
14733 list_append(l2, li2);
14734 copy_tv(&di->di_tv, &li2->li_tv);
14735 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014736 }
14737 }
14738}
14739
14740/*
14741 * "items(dict)" function
14742 */
14743 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014744f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014745{
14746 dict_list(argvars, rettv, 2);
14747}
14748
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014749#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014750/*
14751 * Get the job from the argument.
14752 * Returns NULL if the job is invalid.
14753 */
14754 static job_T *
14755get_job_arg(typval_T *tv)
14756{
14757 job_T *job;
14758
14759 if (tv->v_type != VAR_JOB)
14760 {
14761 EMSG2(_(e_invarg2), get_tv_string(tv));
14762 return NULL;
14763 }
14764 job = tv->vval.v_job;
14765
14766 if (job == NULL)
14767 EMSG(_("E916: not a valid job"));
14768 return job;
14769}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014770
Bram Moolenaar835dc632016-02-07 14:27:38 +010014771/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014772 * "job_getchannel()" function
14773 */
14774 static void
14775f_job_getchannel(typval_T *argvars, typval_T *rettv)
14776{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014777 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014778
Bram Moolenaar65edff82016-02-21 16:40:11 +010014779 if (job != NULL)
14780 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014781 rettv->v_type = VAR_CHANNEL;
14782 rettv->vval.v_channel = job->jv_channel;
14783 if (job->jv_channel != NULL)
14784 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014785 }
14786}
14787
14788/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014789 * "job_info()" function
14790 */
14791 static void
14792f_job_info(typval_T *argvars, typval_T *rettv)
14793{
14794 job_T *job = get_job_arg(&argvars[0]);
14795
14796 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14797 job_info(job, rettv->vval.v_dict);
14798}
14799
14800/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014801 * "job_setoptions()" function
14802 */
14803 static void
14804f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14805{
14806 job_T *job = get_job_arg(&argvars[0]);
14807 jobopt_T opt;
14808
14809 if (job == NULL)
14810 return;
14811 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014812 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014813 return;
14814 job_set_options(job, &opt);
14815}
14816
14817/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014818 * "job_start()" function
14819 */
14820 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014821f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014822{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014823 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014824 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014825}
14826
14827/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014828 * "job_status()" function
14829 */
14830 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014831f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014832{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014833 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014834
Bram Moolenaar65edff82016-02-21 16:40:11 +010014835 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014836 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014837 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014838 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014839 }
14840}
14841
14842/*
14843 * "job_stop()" function
14844 */
14845 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014846f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014847{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014848 job_T *job = get_job_arg(&argvars[0]);
14849
14850 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014851 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014852}
14853#endif
14854
Bram Moolenaar071d4272004-06-13 20:20:40 +000014855/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014856 * "join()" function
14857 */
14858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014859f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014860{
14861 garray_T ga;
14862 char_u *sep;
14863
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014864 if (argvars[0].v_type != VAR_LIST)
14865 {
14866 EMSG(_(e_listreq));
14867 return;
14868 }
14869 if (argvars[0].vval.v_list == NULL)
14870 return;
14871 if (argvars[1].v_type == VAR_UNKNOWN)
14872 sep = (char_u *)" ";
14873 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014874 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014875
14876 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014877
14878 if (sep != NULL)
14879 {
14880 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014881 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014882 ga_append(&ga, NUL);
14883 rettv->vval.v_string = (char_u *)ga.ga_data;
14884 }
14885 else
14886 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014887}
14888
14889/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014890 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014891 */
14892 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014893f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014894{
14895 js_read_T reader;
14896
14897 reader.js_buf = get_tv_string(&argvars[0]);
14898 reader.js_fill = NULL;
14899 reader.js_used = 0;
14900 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14901 EMSG(_(e_invarg));
14902}
14903
14904/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014905 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014906 */
14907 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014908f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014909{
14910 rettv->v_type = VAR_STRING;
14911 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14912}
14913
14914/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014915 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014916 */
14917 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014918f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014919{
14920 js_read_T reader;
14921
14922 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014923 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014924 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014925 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014926 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014927}
14928
14929/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014930 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014931 */
14932 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014933f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014934{
14935 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014936 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014937}
14938
14939/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014940 * "keys()" function
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014944{
14945 dict_list(argvars, rettv, 0);
14946}
14947
14948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "last_buffer_nr()" function.
14950 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953{
14954 int n = 0;
14955 buf_T *buf;
14956
14957 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14958 if (n < buf->b_fnum)
14959 n = buf->b_fnum;
14960
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014962}
14963
14964/*
14965 * "len()" function
14966 */
14967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014968f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014969{
14970 switch (argvars[0].v_type)
14971 {
14972 case VAR_STRING:
14973 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014974 rettv->vval.v_number = (varnumber_T)STRLEN(
14975 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014976 break;
14977 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014978 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014979 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014980 case VAR_DICT:
14981 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14982 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014983 case VAR_UNKNOWN:
14984 case VAR_SPECIAL:
14985 case VAR_FLOAT:
14986 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014987 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014988 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014989 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014990 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014991 break;
14992 }
14993}
14994
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014995static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014996
14997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014998libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014999{
15000#ifdef FEAT_LIBCALL
15001 char_u *string_in;
15002 char_u **string_result;
15003 int nr_result;
15004#endif
15005
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015007 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015008 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015009
15010 if (check_restricted() || check_secure())
15011 return;
15012
15013#ifdef FEAT_LIBCALL
15014 /* The first two args must be strings, otherwise its meaningless */
15015 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15016 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015017 string_in = NULL;
15018 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015019 string_in = argvars[2].vval.v_string;
15020 if (type == VAR_NUMBER)
15021 string_result = NULL;
15022 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015023 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015024 if (mch_libcall(argvars[0].vval.v_string,
15025 argvars[1].vval.v_string,
15026 string_in,
15027 argvars[2].vval.v_number,
15028 string_result,
15029 &nr_result) == OK
15030 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015031 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015032 }
15033#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015034}
15035
15036/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015037 * "libcall()" function
15038 */
15039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015040f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015041{
15042 libcall_common(argvars, rettv, VAR_STRING);
15043}
15044
15045/*
15046 * "libcallnr()" function
15047 */
15048 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015049f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015050{
15051 libcall_common(argvars, rettv, VAR_NUMBER);
15052}
15053
15054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015055 * "line(string)" function
15056 */
15057 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015058f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015059{
15060 linenr_T lnum = 0;
15061 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015062 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015064 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015065 if (fp != NULL)
15066 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015067 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015068}
15069
15070/*
15071 * "line2byte(lnum)" function
15072 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015074f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075{
15076#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015077 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015078#else
15079 linenr_T lnum;
15080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015084 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015085 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15086 if (rettv->vval.v_number >= 0)
15087 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088#endif
15089}
15090
15091/*
15092 * "lispindent(lnum)" function
15093 */
15094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015095f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015096{
15097#ifdef FEAT_LISP
15098 pos_T pos;
15099 linenr_T lnum;
15100
15101 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015102 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015103 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15104 {
15105 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015106 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015107 curwin->w_cursor = pos;
15108 }
15109 else
15110#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015111 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015112}
15113
15114/*
15115 * "localtime()" function
15116 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015118f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015120 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015121}
15122
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015123static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015124
15125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015126get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015127{
15128 char_u *keys;
15129 char_u *which;
15130 char_u buf[NUMBUFLEN];
15131 char_u *keys_buf = NULL;
15132 char_u *rhs;
15133 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015134 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015135 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015136 mapblock_T *mp;
15137 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138
15139 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015140 rettv->v_type = VAR_STRING;
15141 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015143 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015144 if (*keys == NUL)
15145 return;
15146
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015147 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015148 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015149 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015151 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015152 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015153 if (argvars[3].v_type != VAR_UNKNOWN)
15154 get_dict = get_tv_number(&argvars[3]);
15155 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015156 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015157 else
15158 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015159 if (which == NULL)
15160 return;
15161
Bram Moolenaar071d4272004-06-13 20:20:40 +000015162 mode = get_map_mode(&which, 0);
15163
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015164 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015165 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015167
15168 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015170 /* Return a string. */
15171 if (rhs != NULL)
15172 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173
Bram Moolenaarbd743252010-10-20 21:23:33 +020015174 }
15175 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15176 {
15177 /* Return a dictionary. */
15178 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15179 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15180 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015181
Bram Moolenaarbd743252010-10-20 21:23:33 +020015182 dict_add_nr_str(dict, "lhs", 0L, lhs);
15183 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15184 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15185 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15186 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15187 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15188 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015189 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015190 dict_add_nr_str(dict, "mode", 0L, mapmode);
15191
15192 vim_free(lhs);
15193 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015194 }
15195}
15196
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015197#ifdef FEAT_FLOAT
15198/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015199 * "log()" function
15200 */
15201 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015202f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015203{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015204 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015205
15206 rettv->v_type = VAR_FLOAT;
15207 if (get_float_arg(argvars, &f) == OK)
15208 rettv->vval.v_float = log(f);
15209 else
15210 rettv->vval.v_float = 0.0;
15211}
15212
15213/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015214 * "log10()" function
15215 */
15216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015217f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015218{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015219 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015220
15221 rettv->v_type = VAR_FLOAT;
15222 if (get_float_arg(argvars, &f) == OK)
15223 rettv->vval.v_float = log10(f);
15224 else
15225 rettv->vval.v_float = 0.0;
15226}
15227#endif
15228
Bram Moolenaar1dced572012-04-05 16:54:08 +020015229#ifdef FEAT_LUA
15230/*
15231 * "luaeval()" function
15232 */
15233 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015234f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015235{
15236 char_u *str;
15237 char_u buf[NUMBUFLEN];
15238
15239 str = get_tv_string_buf(&argvars[0], buf);
15240 do_luaeval(str, argvars + 1, rettv);
15241}
15242#endif
15243
Bram Moolenaar071d4272004-06-13 20:20:40 +000015244/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015245 * "map()" function
15246 */
15247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015248f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015249{
15250 filter_map(argvars, rettv, TRUE);
15251}
15252
15253/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015254 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015255 */
15256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015257f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015259 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015260}
15261
15262/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015263 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015264 */
15265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015266f_mapcheck(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, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015269}
15270
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015271static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015272
15273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015274find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015275{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015277 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015278 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015279 char_u *pat;
15280 regmatch_T regmatch;
15281 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015282 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015283 char_u *save_cpo;
15284 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015285 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015286 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015287 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015288 list_T *l = NULL;
15289 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015290 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015291 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015292
15293 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15294 save_cpo = p_cpo;
15295 p_cpo = (char_u *)"";
15296
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015297 rettv->vval.v_number = -1;
15298 if (type == 3)
15299 {
15300 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015301 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015302 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015303 }
15304 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015305 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015306 rettv->v_type = VAR_STRING;
15307 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015310 if (argvars[0].v_type == VAR_LIST)
15311 {
15312 if ((l = argvars[0].vval.v_list) == NULL)
15313 goto theend;
15314 li = l->lv_first;
15315 }
15316 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015317 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015318 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015319 len = (long)STRLEN(str);
15320 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015321
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015322 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15323 if (pat == NULL)
15324 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015325
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015326 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015327 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015328 int error = FALSE;
15329
15330 start = get_tv_number_chk(&argvars[2], &error);
15331 if (error)
15332 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015333 if (l != NULL)
15334 {
15335 li = list_find(l, start);
15336 if (li == NULL)
15337 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015338 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015339 }
15340 else
15341 {
15342 if (start < 0)
15343 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015344 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015345 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015346 /* When "count" argument is there ignore matches before "start",
15347 * otherwise skip part of the string. Differs when pattern is "^"
15348 * or "\<". */
15349 if (argvars[3].v_type != VAR_UNKNOWN)
15350 startcol = start;
15351 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015352 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015353 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015354 len -= start;
15355 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015356 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015357
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015358 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015359 nth = get_tv_number_chk(&argvars[3], &error);
15360 if (error)
15361 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015362 }
15363
15364 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15365 if (regmatch.regprog != NULL)
15366 {
15367 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015368
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015369 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015370 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015371 if (l != NULL)
15372 {
15373 if (li == NULL)
15374 {
15375 match = FALSE;
15376 break;
15377 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015378 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015379 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015380 if (str == NULL)
15381 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015382 }
15383
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015384 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015385
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015386 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015387 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015388 if (l == NULL && !match)
15389 break;
15390
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015391 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015392 if (l != NULL)
15393 {
15394 li = li->li_next;
15395 ++idx;
15396 }
15397 else
15398 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015399#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015400 startcol = (colnr_T)(regmatch.startp[0]
15401 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015402#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015403 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015404#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015405 if (startcol > (colnr_T)len
15406 || str + startcol <= regmatch.startp[0])
15407 {
15408 match = FALSE;
15409 break;
15410 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015411 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015412 }
15413
15414 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015415 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015417 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015418 int i;
15419
15420 /* return list with matched string and submatches */
15421 for (i = 0; i < NSUBEXP; ++i)
15422 {
15423 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015424 {
15425 if (list_append_string(rettv->vval.v_list,
15426 (char_u *)"", 0) == FAIL)
15427 break;
15428 }
15429 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015430 regmatch.startp[i],
15431 (int)(regmatch.endp[i] - regmatch.startp[i]))
15432 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015433 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015434 }
15435 }
15436 else if (type == 2)
15437 {
15438 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015439 if (l != NULL)
15440 copy_tv(&li->li_tv, rettv);
15441 else
15442 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015443 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015444 }
15445 else if (l != NULL)
15446 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015447 else
15448 {
15449 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015450 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015451 (varnumber_T)(regmatch.startp[0] - str);
15452 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015453 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015455 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015456 }
15457 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015458 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015459 }
15460
15461theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015462 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015463 p_cpo = save_cpo;
15464}
15465
15466/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015467 * "match()" function
15468 */
15469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015470f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015471{
15472 find_some_match(argvars, rettv, 1);
15473}
15474
15475/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015476 * "matchadd()" function
15477 */
15478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015479f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015480{
15481#ifdef FEAT_SEARCH_EXTRA
15482 char_u buf[NUMBUFLEN];
15483 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15484 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15485 int prio = 10; /* default priority */
15486 int id = -1;
15487 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015488 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015489
15490 rettv->vval.v_number = -1;
15491
15492 if (grp == NULL || pat == NULL)
15493 return;
15494 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015495 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015496 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015497 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015498 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015499 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015500 if (argvars[4].v_type != VAR_UNKNOWN)
15501 {
15502 if (argvars[4].v_type != VAR_DICT)
15503 {
15504 EMSG(_(e_dictreq));
15505 return;
15506 }
15507 if (dict_find(argvars[4].vval.v_dict,
15508 (char_u *)"conceal", -1) != NULL)
15509 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15510 (char_u *)"conceal", FALSE);
15511 }
15512 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015513 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015514 if (error == TRUE)
15515 return;
15516 if (id >= 1 && id <= 3)
15517 {
15518 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15519 return;
15520 }
15521
Bram Moolenaar6561d522015-07-21 15:48:27 +020015522 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15523 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015524#endif
15525}
15526
15527/*
15528 * "matchaddpos()" function
15529 */
15530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015531f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015532{
15533#ifdef FEAT_SEARCH_EXTRA
15534 char_u buf[NUMBUFLEN];
15535 char_u *group;
15536 int prio = 10;
15537 int id = -1;
15538 int error = FALSE;
15539 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015540 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015541
15542 rettv->vval.v_number = -1;
15543
15544 group = get_tv_string_buf_chk(&argvars[0], buf);
15545 if (group == NULL)
15546 return;
15547
15548 if (argvars[1].v_type != VAR_LIST)
15549 {
15550 EMSG2(_(e_listarg), "matchaddpos()");
15551 return;
15552 }
15553 l = argvars[1].vval.v_list;
15554 if (l == NULL)
15555 return;
15556
15557 if (argvars[2].v_type != VAR_UNKNOWN)
15558 {
15559 prio = get_tv_number_chk(&argvars[2], &error);
15560 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015561 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015562 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015563 if (argvars[4].v_type != VAR_UNKNOWN)
15564 {
15565 if (argvars[4].v_type != VAR_DICT)
15566 {
15567 EMSG(_(e_dictreq));
15568 return;
15569 }
15570 if (dict_find(argvars[4].vval.v_dict,
15571 (char_u *)"conceal", -1) != NULL)
15572 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15573 (char_u *)"conceal", FALSE);
15574 }
15575 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015576 }
15577 if (error == TRUE)
15578 return;
15579
15580 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15581 if (id == 1 || id == 2)
15582 {
15583 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15584 return;
15585 }
15586
Bram Moolenaar6561d522015-07-21 15:48:27 +020015587 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15588 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015589#endif
15590}
15591
15592/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015593 * "matcharg()" function
15594 */
15595 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015596f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015597{
15598 if (rettv_list_alloc(rettv) == OK)
15599 {
15600#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015601 int id = get_tv_number(&argvars[0]);
15602 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015603
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015604 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015605 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015606 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15607 {
15608 list_append_string(rettv->vval.v_list,
15609 syn_id2name(m->hlg_id), -1);
15610 list_append_string(rettv->vval.v_list, m->pattern, -1);
15611 }
15612 else
15613 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015614 list_append_string(rettv->vval.v_list, NULL, -1);
15615 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015616 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015617 }
15618#endif
15619 }
15620}
15621
15622/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015623 * "matchdelete()" function
15624 */
15625 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015626f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015627{
15628#ifdef FEAT_SEARCH_EXTRA
15629 rettv->vval.v_number = match_delete(curwin,
15630 (int)get_tv_number(&argvars[0]), TRUE);
15631#endif
15632}
15633
15634/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015635 * "matchend()" function
15636 */
15637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015638f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015639{
15640 find_some_match(argvars, rettv, 0);
15641}
15642
15643/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015644 * "matchlist()" function
15645 */
15646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015647f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015648{
15649 find_some_match(argvars, rettv, 3);
15650}
15651
15652/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015653 * "matchstr()" function
15654 */
15655 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015656f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015657{
15658 find_some_match(argvars, rettv, 2);
15659}
15660
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015661static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015662
15663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015664max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015665{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015666 long n = 0;
15667 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015668 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015669
15670 if (argvars[0].v_type == VAR_LIST)
15671 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015672 list_T *l;
15673 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015674
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015675 l = argvars[0].vval.v_list;
15676 if (l != NULL)
15677 {
15678 li = l->lv_first;
15679 if (li != NULL)
15680 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015681 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015682 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015683 {
15684 li = li->li_next;
15685 if (li == NULL)
15686 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015687 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015688 if (domax ? i > n : i < n)
15689 n = i;
15690 }
15691 }
15692 }
15693 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015694 else if (argvars[0].v_type == VAR_DICT)
15695 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015697 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015698 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015699 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015700
15701 d = argvars[0].vval.v_dict;
15702 if (d != NULL)
15703 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015704 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015705 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015706 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015707 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015708 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015709 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015710 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015711 if (first)
15712 {
15713 n = i;
15714 first = FALSE;
15715 }
15716 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015717 n = i;
15718 }
15719 }
15720 }
15721 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015722 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015723 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015724 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015725}
15726
15727/*
15728 * "max()" function
15729 */
15730 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015731f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015732{
15733 max_min(argvars, rettv, TRUE);
15734}
15735
15736/*
15737 * "min()" function
15738 */
15739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015740f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015741{
15742 max_min(argvars, rettv, FALSE);
15743}
15744
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015745static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015746
15747/*
15748 * Create the directory in which "dir" is located, and higher levels when
15749 * needed.
15750 */
15751 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015752mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015753{
15754 char_u *p;
15755 char_u *updir;
15756 int r = FAIL;
15757
15758 /* Get end of directory name in "dir".
15759 * We're done when it's "/" or "c:/". */
15760 p = gettail_sep(dir);
15761 if (p <= get_past_head(dir))
15762 return OK;
15763
15764 /* If the directory exists we're done. Otherwise: create it.*/
15765 updir = vim_strnsave(dir, (int)(p - dir));
15766 if (updir == NULL)
15767 return FAIL;
15768 if (mch_isdir(updir))
15769 r = OK;
15770 else if (mkdir_recurse(updir, prot) == OK)
15771 r = vim_mkdir_emsg(updir, prot);
15772 vim_free(updir);
15773 return r;
15774}
15775
15776#ifdef vim_mkdir
15777/*
15778 * "mkdir()" function
15779 */
15780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015781f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015782{
15783 char_u *dir;
15784 char_u buf[NUMBUFLEN];
15785 int prot = 0755;
15786
15787 rettv->vval.v_number = FAIL;
15788 if (check_restricted() || check_secure())
15789 return;
15790
15791 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015792 if (*dir == NUL)
15793 rettv->vval.v_number = FAIL;
15794 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015795 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015796 if (*gettail(dir) == NUL)
15797 /* remove trailing slashes */
15798 *gettail_sep(dir) = NUL;
15799
15800 if (argvars[1].v_type != VAR_UNKNOWN)
15801 {
15802 if (argvars[2].v_type != VAR_UNKNOWN)
15803 prot = get_tv_number_chk(&argvars[2], NULL);
15804 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15805 mkdir_recurse(dir, prot);
15806 }
15807 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015808 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015809}
15810#endif
15811
Bram Moolenaar0d660222005-01-07 21:51:51 +000015812/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 * "mode()" function
15814 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015816f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015817{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015818 char_u buf[3];
15819
15820 buf[1] = NUL;
15821 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015822
Bram Moolenaar071d4272004-06-13 20:20:40 +000015823 if (VIsual_active)
15824 {
15825 if (VIsual_select)
15826 buf[0] = VIsual_mode + 's' - 'v';
15827 else
15828 buf[0] = VIsual_mode;
15829 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015830 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015831 || State == CONFIRM)
15832 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015833 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015834 if (State == ASKMORE)
15835 buf[1] = 'm';
15836 else if (State == CONFIRM)
15837 buf[1] = '?';
15838 }
15839 else if (State == EXTERNCMD)
15840 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015841 else if (State & INSERT)
15842 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015843#ifdef FEAT_VREPLACE
15844 if (State & VREPLACE_FLAG)
15845 {
15846 buf[0] = 'R';
15847 buf[1] = 'v';
15848 }
15849 else
15850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015851 if (State & REPLACE_FLAG)
15852 buf[0] = 'R';
15853 else
15854 buf[0] = 'i';
15855 }
15856 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015857 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015858 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015859 if (exmode_active)
15860 buf[1] = 'v';
15861 }
15862 else if (exmode_active)
15863 {
15864 buf[0] = 'c';
15865 buf[1] = 'e';
15866 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015868 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015869 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015870 if (finish_op)
15871 buf[1] = 'o';
15872 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015873
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015874 /* Clear out the minor mode when the argument is not a non-zero number or
15875 * non-empty string. */
15876 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015877 buf[1] = NUL;
15878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015879 rettv->vval.v_string = vim_strsave(buf);
15880 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015881}
15882
Bram Moolenaar429fa852013-04-15 12:27:36 +020015883#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015884/*
15885 * "mzeval()" function
15886 */
15887 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015888f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015889{
15890 char_u *str;
15891 char_u buf[NUMBUFLEN];
15892
15893 str = get_tv_string_buf(&argvars[0], buf);
15894 do_mzeval(str, rettv);
15895}
Bram Moolenaar75676462013-01-30 14:55:42 +010015896
15897 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015898mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015899{
15900 typval_T argvars[3];
15901
15902 argvars[0].v_type = VAR_STRING;
15903 argvars[0].vval.v_string = name;
15904 copy_tv(args, &argvars[1]);
15905 argvars[2].v_type = VAR_UNKNOWN;
15906 f_call(argvars, rettv);
15907 clear_tv(&argvars[1]);
15908}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015909#endif
15910
Bram Moolenaar071d4272004-06-13 20:20:40 +000015911/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015912 * "nextnonblank()" function
15913 */
15914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015915f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015916{
15917 linenr_T lnum;
15918
15919 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15920 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015921 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015922 {
15923 lnum = 0;
15924 break;
15925 }
15926 if (*skipwhite(ml_get(lnum)) != NUL)
15927 break;
15928 }
15929 rettv->vval.v_number = lnum;
15930}
15931
15932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015933 * "nr2char()" function
15934 */
15935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015936f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015937{
15938 char_u buf[NUMBUFLEN];
15939
15940#ifdef FEAT_MBYTE
15941 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015942 {
15943 int utf8 = 0;
15944
15945 if (argvars[1].v_type != VAR_UNKNOWN)
15946 utf8 = get_tv_number_chk(&argvars[1], NULL);
15947 if (utf8)
15948 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15949 else
15950 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015952 else
15953#endif
15954 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015955 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015956 buf[1] = NUL;
15957 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015958 rettv->v_type = VAR_STRING;
15959 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015960}
15961
15962/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015963 * "or(expr, expr)" function
15964 */
15965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015966f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015967{
15968 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15969 | get_tv_number_chk(&argvars[1], NULL);
15970}
15971
15972/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015973 * "pathshorten()" function
15974 */
15975 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015976f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015977{
15978 char_u *p;
15979
15980 rettv->v_type = VAR_STRING;
15981 p = get_tv_string_chk(&argvars[0]);
15982 if (p == NULL)
15983 rettv->vval.v_string = NULL;
15984 else
15985 {
15986 p = vim_strsave(p);
15987 rettv->vval.v_string = p;
15988 if (p != NULL)
15989 shorten_dir(p);
15990 }
15991}
15992
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015993#ifdef FEAT_PERL
15994/*
15995 * "perleval()" function
15996 */
15997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015998f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015999{
16000 char_u *str;
16001 char_u buf[NUMBUFLEN];
16002
16003 str = get_tv_string_buf(&argvars[0], buf);
16004 do_perleval(str, rettv);
16005}
16006#endif
16007
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016008#ifdef FEAT_FLOAT
16009/*
16010 * "pow()" function
16011 */
16012 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016013f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016014{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016015 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016016
16017 rettv->v_type = VAR_FLOAT;
16018 if (get_float_arg(argvars, &fx) == OK
16019 && get_float_arg(&argvars[1], &fy) == OK)
16020 rettv->vval.v_float = pow(fx, fy);
16021 else
16022 rettv->vval.v_float = 0.0;
16023}
16024#endif
16025
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016026/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016027 * "prevnonblank()" function
16028 */
16029 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016030f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016031{
16032 linenr_T lnum;
16033
16034 lnum = get_tv_lnum(argvars);
16035 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16036 lnum = 0;
16037 else
16038 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16039 --lnum;
16040 rettv->vval.v_number = lnum;
16041}
16042
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016043/* This dummy va_list is here because:
16044 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16045 * - locally in the function results in a "used before set" warning
16046 * - using va_start() to initialize it gives "function with fixed args" error */
16047static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016048
Bram Moolenaar8c711452005-01-14 21:53:12 +000016049/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016050 * "printf()" function
16051 */
16052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016053f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016054{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016055 char_u buf[NUMBUFLEN];
16056 int len;
16057 char_u *s;
16058 int saved_did_emsg = did_emsg;
16059 char *fmt;
16060
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016061 rettv->v_type = VAR_STRING;
16062 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016063
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016064 /* Get the required length, allocate the buffer and do it for real. */
16065 did_emsg = FALSE;
16066 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16067 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16068 if (!did_emsg)
16069 {
16070 s = alloc(len + 1);
16071 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016072 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016073 rettv->vval.v_string = s;
16074 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016075 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016076 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016077 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016078}
16079
16080/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016081 * "pumvisible()" function
16082 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016084f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016085{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016086#ifdef FEAT_INS_EXPAND
16087 if (pum_visible())
16088 rettv->vval.v_number = 1;
16089#endif
16090}
16091
Bram Moolenaardb913952012-06-29 12:54:53 +020016092#ifdef FEAT_PYTHON3
16093/*
16094 * "py3eval()" function
16095 */
16096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016097f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016098{
16099 char_u *str;
16100 char_u buf[NUMBUFLEN];
16101
16102 str = get_tv_string_buf(&argvars[0], buf);
16103 do_py3eval(str, rettv);
16104}
16105#endif
16106
16107#ifdef FEAT_PYTHON
16108/*
16109 * "pyeval()" function
16110 */
16111 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016112f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016113{
16114 char_u *str;
16115 char_u buf[NUMBUFLEN];
16116
16117 str = get_tv_string_buf(&argvars[0], buf);
16118 do_pyeval(str, rettv);
16119}
16120#endif
16121
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016122/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016123 * "range()" function
16124 */
16125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016126f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016127{
16128 long start;
16129 long end;
16130 long stride = 1;
16131 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016133
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016134 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016135 if (argvars[1].v_type == VAR_UNKNOWN)
16136 {
16137 end = start - 1;
16138 start = 0;
16139 }
16140 else
16141 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016143 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016144 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016145 }
16146
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016147 if (error)
16148 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016149 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016150 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016151 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016152 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016153 else
16154 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016155 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016156 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016157 if (list_append_number(rettv->vval.v_list,
16158 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016159 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016160 }
16161}
16162
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016163/*
16164 * "readfile()" function
16165 */
16166 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016167f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016168{
16169 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016170 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016171 char_u *fname;
16172 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016173 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16174 int io_size = sizeof(buf);
16175 int readlen; /* size of last fread() */
16176 char_u *prev = NULL; /* previously read bytes, if any */
16177 long prevlen = 0; /* length of data in prev */
16178 long prevsize = 0; /* size of prev buffer */
16179 long maxline = MAXLNUM;
16180 long cnt = 0;
16181 char_u *p; /* position in buf */
16182 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016183
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016184 if (argvars[1].v_type != VAR_UNKNOWN)
16185 {
16186 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16187 binary = TRUE;
16188 if (argvars[2].v_type != VAR_UNKNOWN)
16189 maxline = get_tv_number(&argvars[2]);
16190 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016191
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016192 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016193 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016194
16195 /* Always open the file in binary mode, library functions have a mind of
16196 * their own about CR-LF conversion. */
16197 fname = get_tv_string(&argvars[0]);
16198 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16199 {
16200 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16201 return;
16202 }
16203
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016204 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016205 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016206 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016207
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016208 /* This for loop processes what was read, but is also entered at end
16209 * of file so that either:
16210 * - an incomplete line gets written
16211 * - a "binary" file gets an empty line at the end if it ends in a
16212 * newline. */
16213 for (p = buf, start = buf;
16214 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16215 ++p)
16216 {
16217 if (*p == '\n' || readlen <= 0)
16218 {
16219 listitem_T *li;
16220 char_u *s = NULL;
16221 long_u len = p - start;
16222
16223 /* Finished a line. Remove CRs before NL. */
16224 if (readlen > 0 && !binary)
16225 {
16226 while (len > 0 && start[len - 1] == '\r')
16227 --len;
16228 /* removal may cross back to the "prev" string */
16229 if (len == 0)
16230 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16231 --prevlen;
16232 }
16233 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016234 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016235 else
16236 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016237 /* Change "prev" buffer to be the right size. This way
16238 * the bytes are only copied once, and very long lines are
16239 * allocated only once. */
16240 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016241 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016242 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016243 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016244 prev = NULL; /* the list will own the string */
16245 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016246 }
16247 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016248 if (s == NULL)
16249 {
16250 do_outofmem_msg((long_u) prevlen + len + 1);
16251 failed = TRUE;
16252 break;
16253 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016254
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016255 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016256 {
16257 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016258 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016259 break;
16260 }
16261 li->li_tv.v_type = VAR_STRING;
16262 li->li_tv.v_lock = 0;
16263 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016264 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016265
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016266 start = p + 1; /* step over newline */
16267 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016268 break;
16269 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016270 else if (*p == NUL)
16271 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016272#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016273 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16274 * when finding the BF and check the previous two bytes. */
16275 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016276 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016277 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16278 * + 1, these may be in the "prev" string. */
16279 char_u back1 = p >= buf + 1 ? p[-1]
16280 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16281 char_u back2 = p >= buf + 2 ? p[-2]
16282 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16283 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016284
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016285 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016286 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016287 char_u *dest = p - 2;
16288
16289 /* Usually a BOM is at the beginning of a file, and so at
16290 * the beginning of a line; then we can just step over it.
16291 */
16292 if (start == dest)
16293 start = p + 1;
16294 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016295 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016296 /* have to shuffle buf to close gap */
16297 int adjust_prevlen = 0;
16298
16299 if (dest < buf)
16300 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016301 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016302 dest = buf;
16303 }
16304 if (readlen > p - buf + 1)
16305 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16306 readlen -= 3 - adjust_prevlen;
16307 prevlen -= adjust_prevlen;
16308 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016309 }
16310 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016311 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016312#endif
16313 } /* for */
16314
16315 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16316 break;
16317 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016318 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016319 /* There's part of a line in buf, store it in "prev". */
16320 if (p - start + prevlen >= prevsize)
16321 {
16322 /* need bigger "prev" buffer */
16323 char_u *newprev;
16324
16325 /* A common use case is ordinary text files and "prev" gets a
16326 * fragment of a line, so the first allocation is made
16327 * small, to avoid repeatedly 'allocing' large and
16328 * 'reallocing' small. */
16329 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016330 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016331 else
16332 {
16333 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016334 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016335 prevsize = grow50pc > growmin ? grow50pc : growmin;
16336 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016337 newprev = prev == NULL ? alloc(prevsize)
16338 : vim_realloc(prev, prevsize);
16339 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016340 {
16341 do_outofmem_msg((long_u)prevsize);
16342 failed = TRUE;
16343 break;
16344 }
16345 prev = newprev;
16346 }
16347 /* Add the line part to end of "prev". */
16348 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016349 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016350 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016351 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016352
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016353 /*
16354 * For a negative line count use only the lines at the end of the file,
16355 * free the rest.
16356 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016357 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016358 while (cnt > -maxline)
16359 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016360 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016361 --cnt;
16362 }
16363
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016364 if (failed)
16365 {
16366 list_free(rettv->vval.v_list, TRUE);
16367 /* readfile doc says an empty list is returned on error */
16368 rettv->vval.v_list = list_alloc();
16369 }
16370
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016371 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016372 fclose(fd);
16373}
16374
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016375#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016376static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016377
16378/*
16379 * Convert a List to proftime_T.
16380 * Return FAIL when there is something wrong.
16381 */
16382 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016383list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016384{
16385 long n1, n2;
16386 int error = FALSE;
16387
16388 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16389 || arg->vval.v_list->lv_len != 2)
16390 return FAIL;
16391 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16392 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16393# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016394 tm->HighPart = n1;
16395 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016396# else
16397 tm->tv_sec = n1;
16398 tm->tv_usec = n2;
16399# endif
16400 return error ? FAIL : OK;
16401}
16402#endif /* FEAT_RELTIME */
16403
16404/*
16405 * "reltime()" function
16406 */
16407 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016408f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016409{
16410#ifdef FEAT_RELTIME
16411 proftime_T res;
16412 proftime_T start;
16413
16414 if (argvars[0].v_type == VAR_UNKNOWN)
16415 {
16416 /* No arguments: get current time. */
16417 profile_start(&res);
16418 }
16419 else if (argvars[1].v_type == VAR_UNKNOWN)
16420 {
16421 if (list2proftime(&argvars[0], &res) == FAIL)
16422 return;
16423 profile_end(&res);
16424 }
16425 else
16426 {
16427 /* Two arguments: compute the difference. */
16428 if (list2proftime(&argvars[0], &start) == FAIL
16429 || list2proftime(&argvars[1], &res) == FAIL)
16430 return;
16431 profile_sub(&res, &start);
16432 }
16433
16434 if (rettv_list_alloc(rettv) == OK)
16435 {
16436 long n1, n2;
16437
16438# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016439 n1 = res.HighPart;
16440 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016441# else
16442 n1 = res.tv_sec;
16443 n2 = res.tv_usec;
16444# endif
16445 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16446 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16447 }
16448#endif
16449}
16450
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016451#ifdef FEAT_FLOAT
16452/*
16453 * "reltimefloat()" function
16454 */
16455 static void
16456f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16457{
16458# ifdef FEAT_RELTIME
16459 proftime_T tm;
16460# endif
16461
16462 rettv->v_type = VAR_FLOAT;
16463 rettv->vval.v_float = 0;
16464# ifdef FEAT_RELTIME
16465 if (list2proftime(&argvars[0], &tm) == OK)
16466 rettv->vval.v_float = profile_float(&tm);
16467# endif
16468}
16469#endif
16470
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016471/*
16472 * "reltimestr()" function
16473 */
16474 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016475f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016476{
16477#ifdef FEAT_RELTIME
16478 proftime_T tm;
16479#endif
16480
16481 rettv->v_type = VAR_STRING;
16482 rettv->vval.v_string = NULL;
16483#ifdef FEAT_RELTIME
16484 if (list2proftime(&argvars[0], &tm) == OK)
16485 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16486#endif
16487}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016488
Bram Moolenaar0d660222005-01-07 21:51:51 +000016489#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016490static void make_connection(void);
16491static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492
16493 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016494make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495{
16496 if (X_DISPLAY == NULL
16497# ifdef FEAT_GUI
16498 && !gui.in_use
16499# endif
16500 )
16501 {
16502 x_force_connect = TRUE;
16503 setup_term_clip();
16504 x_force_connect = FALSE;
16505 }
16506}
16507
16508 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016509check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016510{
16511 make_connection();
16512 if (X_DISPLAY == NULL)
16513 {
16514 EMSG(_("E240: No connection to Vim server"));
16515 return FAIL;
16516 }
16517 return OK;
16518}
16519#endif
16520
16521#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016522static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523
16524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016525remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016526{
16527 char_u *server_name;
16528 char_u *keys;
16529 char_u *r = NULL;
16530 char_u buf[NUMBUFLEN];
16531# ifdef WIN32
16532 HWND w;
16533# else
16534 Window w;
16535# endif
16536
16537 if (check_restricted() || check_secure())
16538 return;
16539
16540# ifdef FEAT_X11
16541 if (check_connection() == FAIL)
16542 return;
16543# endif
16544
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016545 server_name = get_tv_string_chk(&argvars[0]);
16546 if (server_name == NULL)
16547 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016548 keys = get_tv_string_buf(&argvars[1], buf);
16549# ifdef WIN32
16550 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16551# else
16552 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16553 < 0)
16554# endif
16555 {
16556 if (r != NULL)
16557 EMSG(r); /* sending worked but evaluation failed */
16558 else
16559 EMSG2(_("E241: Unable to send to %s"), server_name);
16560 return;
16561 }
16562
16563 rettv->vval.v_string = r;
16564
16565 if (argvars[2].v_type != VAR_UNKNOWN)
16566 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016567 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016568 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016569 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016571 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016572 v.di_tv.v_type = VAR_STRING;
16573 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016574 idvar = get_tv_string_chk(&argvars[2]);
16575 if (idvar != NULL)
16576 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016577 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016578 }
16579}
16580#endif
16581
16582/*
16583 * "remote_expr()" function
16584 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016586f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016587{
16588 rettv->v_type = VAR_STRING;
16589 rettv->vval.v_string = NULL;
16590#ifdef FEAT_CLIENTSERVER
16591 remote_common(argvars, rettv, TRUE);
16592#endif
16593}
16594
16595/*
16596 * "remote_foreground()" function
16597 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016599f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016600{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016601#ifdef FEAT_CLIENTSERVER
16602# ifdef WIN32
16603 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016604 {
16605 char_u *server_name = get_tv_string_chk(&argvars[0]);
16606
16607 if (server_name != NULL)
16608 serverForeground(server_name);
16609 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016610# else
16611 /* Send a foreground() expression to the server. */
16612 argvars[1].v_type = VAR_STRING;
16613 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16614 argvars[2].v_type = VAR_UNKNOWN;
16615 remote_common(argvars, rettv, TRUE);
16616 vim_free(argvars[1].vval.v_string);
16617# endif
16618#endif
16619}
16620
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016622f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016623{
16624#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016625 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016626 char_u *s = NULL;
16627# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016628 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016630 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016631
16632 if (check_restricted() || check_secure())
16633 {
16634 rettv->vval.v_number = -1;
16635 return;
16636 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016637 serverid = get_tv_string_chk(&argvars[0]);
16638 if (serverid == NULL)
16639 {
16640 rettv->vval.v_number = -1;
16641 return; /* type error; errmsg already given */
16642 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016644 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016645 if (n == 0)
16646 rettv->vval.v_number = -1;
16647 else
16648 {
16649 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16650 rettv->vval.v_number = (s != NULL);
16651 }
16652# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016653 if (check_connection() == FAIL)
16654 return;
16655
16656 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016657 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016658# endif
16659
16660 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16661 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016662 char_u *retvar;
16663
Bram Moolenaar33570922005-01-25 22:26:29 +000016664 v.di_tv.v_type = VAR_STRING;
16665 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016666 retvar = get_tv_string_chk(&argvars[1]);
16667 if (retvar != NULL)
16668 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016669 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016670 }
16671#else
16672 rettv->vval.v_number = -1;
16673#endif
16674}
16675
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016677f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016678{
16679 char_u *r = NULL;
16680
16681#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016682 char_u *serverid = get_tv_string_chk(&argvars[0]);
16683
16684 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016685 {
16686# ifdef WIN32
16687 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016688 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016690 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016691 if (n != 0)
16692 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16693 if (r == NULL)
16694# else
16695 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016696 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016697# endif
16698 EMSG(_("E277: Unable to read a server reply"));
16699 }
16700#endif
16701 rettv->v_type = VAR_STRING;
16702 rettv->vval.v_string = r;
16703}
16704
16705/*
16706 * "remote_send()" function
16707 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016709f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016710{
16711 rettv->v_type = VAR_STRING;
16712 rettv->vval.v_string = NULL;
16713#ifdef FEAT_CLIENTSERVER
16714 remote_common(argvars, rettv, FALSE);
16715#endif
16716}
16717
16718/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016719 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016720 */
16721 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016722f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016723{
Bram Moolenaar33570922005-01-25 22:26:29 +000016724 list_T *l;
16725 listitem_T *item, *item2;
16726 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016727 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016728 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016729 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016730 dict_T *d;
16731 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016732 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016733
Bram Moolenaar8c711452005-01-14 21:53:12 +000016734 if (argvars[0].v_type == VAR_DICT)
16735 {
16736 if (argvars[2].v_type != VAR_UNKNOWN)
16737 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016738 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016739 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016740 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016741 key = get_tv_string_chk(&argvars[1]);
16742 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016743 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016744 di = dict_find(d, key, -1);
16745 if (di == NULL)
16746 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016747 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16748 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016749 {
16750 *rettv = di->di_tv;
16751 init_tv(&di->di_tv);
16752 dictitem_remove(d, di);
16753 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016755 }
16756 }
16757 else if (argvars[0].v_type != VAR_LIST)
16758 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016759 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016760 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016761 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016762 int error = FALSE;
16763
16764 idx = get_tv_number_chk(&argvars[1], &error);
16765 if (error)
16766 ; /* type error: do nothing, errmsg already given */
16767 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016768 EMSGN(_(e_listidx), idx);
16769 else
16770 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016771 if (argvars[2].v_type == VAR_UNKNOWN)
16772 {
16773 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016774 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016775 *rettv = item->li_tv;
16776 vim_free(item);
16777 }
16778 else
16779 {
16780 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016781 end = get_tv_number_chk(&argvars[2], &error);
16782 if (error)
16783 ; /* type error: do nothing */
16784 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016785 EMSGN(_(e_listidx), end);
16786 else
16787 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016788 int cnt = 0;
16789
16790 for (li = item; li != NULL; li = li->li_next)
16791 {
16792 ++cnt;
16793 if (li == item2)
16794 break;
16795 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016796 if (li == NULL) /* didn't find "item2" after "item" */
16797 EMSG(_(e_invrange));
16798 else
16799 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016800 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016802 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016803 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016804 l->lv_first = item;
16805 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016806 item->li_prev = NULL;
16807 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016808 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016809 }
16810 }
16811 }
16812 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016813 }
16814 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016815}
16816
16817/*
16818 * "rename({from}, {to})" function
16819 */
16820 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016821f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016822{
16823 char_u buf[NUMBUFLEN];
16824
16825 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016826 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016827 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016828 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16829 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830}
16831
16832/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016833 * "repeat()" function
16834 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016836f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016837{
16838 char_u *p;
16839 int n;
16840 int slen;
16841 int len;
16842 char_u *r;
16843 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016844
16845 n = get_tv_number(&argvars[1]);
16846 if (argvars[0].v_type == VAR_LIST)
16847 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016848 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016849 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016850 if (list_extend(rettv->vval.v_list,
16851 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016852 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016853 }
16854 else
16855 {
16856 p = get_tv_string(&argvars[0]);
16857 rettv->v_type = VAR_STRING;
16858 rettv->vval.v_string = NULL;
16859
16860 slen = (int)STRLEN(p);
16861 len = slen * n;
16862 if (len <= 0)
16863 return;
16864
16865 r = alloc(len + 1);
16866 if (r != NULL)
16867 {
16868 for (i = 0; i < n; i++)
16869 mch_memmove(r + i * slen, p, (size_t)slen);
16870 r[len] = NUL;
16871 }
16872
16873 rettv->vval.v_string = r;
16874 }
16875}
16876
16877/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016878 * "resolve()" function
16879 */
16880 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016881f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016882{
16883 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016884#ifdef HAVE_READLINK
16885 char_u *buf = NULL;
16886#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016888 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016889#ifdef FEAT_SHORTCUT
16890 {
16891 char_u *v = NULL;
16892
16893 v = mch_resolve_shortcut(p);
16894 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016895 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016897 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016898 }
16899#else
16900# ifdef HAVE_READLINK
16901 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016902 char_u *cpy;
16903 int len;
16904 char_u *remain = NULL;
16905 char_u *q;
16906 int is_relative_to_current = FALSE;
16907 int has_trailing_pathsep = FALSE;
16908 int limit = 100;
16909
16910 p = vim_strsave(p);
16911
16912 if (p[0] == '.' && (vim_ispathsep(p[1])
16913 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16914 is_relative_to_current = TRUE;
16915
16916 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016917 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016918 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016919 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016920 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16921 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016922
16923 q = getnextcomp(p);
16924 if (*q != NUL)
16925 {
16926 /* Separate the first path component in "p", and keep the
16927 * remainder (beginning with the path separator). */
16928 remain = vim_strsave(q - 1);
16929 q[-1] = NUL;
16930 }
16931
Bram Moolenaard9462e32011-04-11 21:35:11 +020016932 buf = alloc(MAXPATHL + 1);
16933 if (buf == NULL)
16934 goto fail;
16935
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 for (;;)
16937 {
16938 for (;;)
16939 {
16940 len = readlink((char *)p, (char *)buf, MAXPATHL);
16941 if (len <= 0)
16942 break;
16943 buf[len] = NUL;
16944
16945 if (limit-- == 0)
16946 {
16947 vim_free(p);
16948 vim_free(remain);
16949 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016950 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016951 goto fail;
16952 }
16953
16954 /* Ensure that the result will have a trailing path separator
16955 * if the argument has one. */
16956 if (remain == NULL && has_trailing_pathsep)
16957 add_pathsep(buf);
16958
16959 /* Separate the first path component in the link value and
16960 * concatenate the remainders. */
16961 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16962 if (*q != NUL)
16963 {
16964 if (remain == NULL)
16965 remain = vim_strsave(q - 1);
16966 else
16967 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016968 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 if (cpy != NULL)
16970 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016971 vim_free(remain);
16972 remain = cpy;
16973 }
16974 }
16975 q[-1] = NUL;
16976 }
16977
16978 q = gettail(p);
16979 if (q > p && *q == NUL)
16980 {
16981 /* Ignore trailing path separator. */
16982 q[-1] = NUL;
16983 q = gettail(p);
16984 }
16985 if (q > p && !mch_isFullName(buf))
16986 {
16987 /* symlink is relative to directory of argument */
16988 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16989 if (cpy != NULL)
16990 {
16991 STRCPY(cpy, p);
16992 STRCPY(gettail(cpy), buf);
16993 vim_free(p);
16994 p = cpy;
16995 }
16996 }
16997 else
16998 {
16999 vim_free(p);
17000 p = vim_strsave(buf);
17001 }
17002 }
17003
17004 if (remain == NULL)
17005 break;
17006
17007 /* Append the first path component of "remain" to "p". */
17008 q = getnextcomp(remain + 1);
17009 len = q - remain - (*q != NUL);
17010 cpy = vim_strnsave(p, STRLEN(p) + len);
17011 if (cpy != NULL)
17012 {
17013 STRNCAT(cpy, remain, len);
17014 vim_free(p);
17015 p = cpy;
17016 }
17017 /* Shorten "remain". */
17018 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017019 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017020 else
17021 {
17022 vim_free(remain);
17023 remain = NULL;
17024 }
17025 }
17026
17027 /* If the result is a relative path name, make it explicitly relative to
17028 * the current directory if and only if the argument had this form. */
17029 if (!vim_ispathsep(*p))
17030 {
17031 if (is_relative_to_current
17032 && *p != NUL
17033 && !(p[0] == '.'
17034 && (p[1] == NUL
17035 || vim_ispathsep(p[1])
17036 || (p[1] == '.'
17037 && (p[2] == NUL
17038 || vim_ispathsep(p[2]))))))
17039 {
17040 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017041 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 if (cpy != NULL)
17043 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017044 vim_free(p);
17045 p = cpy;
17046 }
17047 }
17048 else if (!is_relative_to_current)
17049 {
17050 /* Strip leading "./". */
17051 q = p;
17052 while (q[0] == '.' && vim_ispathsep(q[1]))
17053 q += 2;
17054 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017055 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017056 }
17057 }
17058
17059 /* Ensure that the result will have no trailing path separator
17060 * if the argument had none. But keep "/" or "//". */
17061 if (!has_trailing_pathsep)
17062 {
17063 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017064 if (after_pathsep(p, q))
17065 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017066 }
17067
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017068 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017069 }
17070# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017071 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017072# endif
17073#endif
17074
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017075 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076
17077#ifdef HAVE_READLINK
17078fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017079 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017081 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017082}
17083
17084/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017085 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017086 */
17087 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017088f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017089{
Bram Moolenaar33570922005-01-25 22:26:29 +000017090 list_T *l;
17091 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017092
Bram Moolenaar0d660222005-01-07 21:51:51 +000017093 if (argvars[0].v_type != VAR_LIST)
17094 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017095 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017096 && !tv_check_lock(l->lv_lock,
17097 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017098 {
17099 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017100 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017101 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017102 while (li != NULL)
17103 {
17104 ni = li->li_prev;
17105 list_append(l, li);
17106 li = ni;
17107 }
17108 rettv->vval.v_list = l;
17109 rettv->v_type = VAR_LIST;
17110 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017111 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017112 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017113}
17114
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017115#define SP_NOMOVE 0x01 /* don't move cursor */
17116#define SP_REPEAT 0x02 /* repeat to find outer pair */
17117#define SP_RETCOUNT 0x04 /* return matchcount */
17118#define SP_SETPCMARK 0x08 /* set previous context mark */
17119#define SP_START 0x10 /* accept match at start position */
17120#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17121#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017122#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017123
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017124static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017125
17126/*
17127 * Get flags for a search function.
17128 * Possibly sets "p_ws".
17129 * Returns BACKWARD, FORWARD or zero (for an error).
17130 */
17131 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017132get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017133{
17134 int dir = FORWARD;
17135 char_u *flags;
17136 char_u nbuf[NUMBUFLEN];
17137 int mask;
17138
17139 if (varp->v_type != VAR_UNKNOWN)
17140 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017141 flags = get_tv_string_buf_chk(varp, nbuf);
17142 if (flags == NULL)
17143 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017144 while (*flags != NUL)
17145 {
17146 switch (*flags)
17147 {
17148 case 'b': dir = BACKWARD; break;
17149 case 'w': p_ws = TRUE; break;
17150 case 'W': p_ws = FALSE; break;
17151 default: mask = 0;
17152 if (flagsp != NULL)
17153 switch (*flags)
17154 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017155 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017156 case 'e': mask = SP_END; break;
17157 case 'm': mask = SP_RETCOUNT; break;
17158 case 'n': mask = SP_NOMOVE; break;
17159 case 'p': mask = SP_SUBPAT; break;
17160 case 'r': mask = SP_REPEAT; break;
17161 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017162 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017163 }
17164 if (mask == 0)
17165 {
17166 EMSG2(_(e_invarg2), flags);
17167 dir = 0;
17168 }
17169 else
17170 *flagsp |= mask;
17171 }
17172 if (dir == 0)
17173 break;
17174 ++flags;
17175 }
17176 }
17177 return dir;
17178}
17179
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017181 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017182 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017183 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017184search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017186 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017187 char_u *pat;
17188 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017189 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017190 int save_p_ws = p_ws;
17191 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017192 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017193 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017194 proftime_T tm;
17195#ifdef FEAT_RELTIME
17196 long time_limit = 0;
17197#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017198 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017199 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017200
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017201 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017202 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017203 if (dir == 0)
17204 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017205 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017206 if (flags & SP_START)
17207 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017208 if (flags & SP_END)
17209 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017210 if (flags & SP_COLUMN)
17211 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017212
Bram Moolenaar76929292008-01-06 19:07:36 +000017213 /* Optional arguments: line number to stop searching and timeout. */
17214 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017215 {
17216 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17217 if (lnum_stop < 0)
17218 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017219#ifdef FEAT_RELTIME
17220 if (argvars[3].v_type != VAR_UNKNOWN)
17221 {
17222 time_limit = get_tv_number_chk(&argvars[3], NULL);
17223 if (time_limit < 0)
17224 goto theend;
17225 }
17226#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017227 }
17228
Bram Moolenaar76929292008-01-06 19:07:36 +000017229#ifdef FEAT_RELTIME
17230 /* Set the time limit, if there is one. */
17231 profile_setlimit(time_limit, &tm);
17232#endif
17233
Bram Moolenaar231334e2005-07-25 20:46:57 +000017234 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017235 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017236 * Check to make sure only those flags are set.
17237 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17238 * flags cannot be set. Check for that condition also.
17239 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017240 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017241 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017242 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017243 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017244 goto theend;
17245 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017246
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017247 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017248 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017249 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017250 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017251 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017252 if (flags & SP_SUBPAT)
17253 retval = subpatnum;
17254 else
17255 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017256 if (flags & SP_SETPCMARK)
17257 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017258 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017259 if (match_pos != NULL)
17260 {
17261 /* Store the match cursor position */
17262 match_pos->lnum = pos.lnum;
17263 match_pos->col = pos.col + 1;
17264 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017265 /* "/$" will put the cursor after the end of the line, may need to
17266 * correct that here */
17267 check_cursor();
17268 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017269
17270 /* If 'n' flag is used: restore cursor position. */
17271 if (flags & SP_NOMOVE)
17272 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017273 else
17274 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017275theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017276 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017277
17278 return retval;
17279}
17280
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017281#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017282
17283/*
17284 * round() is not in C90, use ceil() or floor() instead.
17285 */
17286 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017287vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017288{
17289 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17290}
17291
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017292/*
17293 * "round({float})" function
17294 */
17295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017296f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017297{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017298 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017299
17300 rettv->v_type = VAR_FLOAT;
17301 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017302 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017303 else
17304 rettv->vval.v_float = 0.0;
17305}
17306#endif
17307
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017308/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017309 * "screenattr()" function
17310 */
17311 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017312f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017313{
17314 int row;
17315 int col;
17316 int c;
17317
17318 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17319 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17320 if (row < 0 || row >= screen_Rows
17321 || col < 0 || col >= screen_Columns)
17322 c = -1;
17323 else
17324 c = ScreenAttrs[LineOffset[row] + col];
17325 rettv->vval.v_number = c;
17326}
17327
17328/*
17329 * "screenchar()" function
17330 */
17331 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017332f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017333{
17334 int row;
17335 int col;
17336 int off;
17337 int c;
17338
17339 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17340 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17341 if (row < 0 || row >= screen_Rows
17342 || col < 0 || col >= screen_Columns)
17343 c = -1;
17344 else
17345 {
17346 off = LineOffset[row] + col;
17347#ifdef FEAT_MBYTE
17348 if (enc_utf8 && ScreenLinesUC[off] != 0)
17349 c = ScreenLinesUC[off];
17350 else
17351#endif
17352 c = ScreenLines[off];
17353 }
17354 rettv->vval.v_number = c;
17355}
17356
17357/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017358 * "screencol()" function
17359 *
17360 * First column is 1 to be consistent with virtcol().
17361 */
17362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017363f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017364{
17365 rettv->vval.v_number = screen_screencol() + 1;
17366}
17367
17368/*
17369 * "screenrow()" function
17370 */
17371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017372f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017373{
17374 rettv->vval.v_number = screen_screenrow() + 1;
17375}
17376
17377/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017378 * "search()" function
17379 */
17380 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017381f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017382{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017383 int flags = 0;
17384
17385 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386}
17387
Bram Moolenaar071d4272004-06-13 20:20:40 +000017388/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017389 * "searchdecl()" function
17390 */
17391 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017392f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017393{
17394 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017395 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017396 int error = FALSE;
17397 char_u *name;
17398
17399 rettv->vval.v_number = 1; /* default: FAIL */
17400
17401 name = get_tv_string_chk(&argvars[0]);
17402 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017403 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017404 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017405 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17406 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17407 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017408 if (!error && name != NULL)
17409 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017410 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017411}
17412
17413/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017414 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017415 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017416 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017417searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017418{
17419 char_u *spat, *mpat, *epat;
17420 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017421 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017422 int dir;
17423 int flags = 0;
17424 char_u nbuf1[NUMBUFLEN];
17425 char_u nbuf2[NUMBUFLEN];
17426 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017427 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017428 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017429 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017430
Bram Moolenaar071d4272004-06-13 20:20:40 +000017431 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017432 spat = get_tv_string_chk(&argvars[0]);
17433 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17434 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17435 if (spat == NULL || mpat == NULL || epat == NULL)
17436 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017437
Bram Moolenaar071d4272004-06-13 20:20:40 +000017438 /* Handle the optional fourth argument: flags */
17439 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017440 if (dir == 0)
17441 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017442
17443 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017444 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17445 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017446 if ((flags & (SP_END | SP_SUBPAT)) != 0
17447 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017448 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017449 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017450 goto theend;
17451 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017452
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017453 /* Using 'r' implies 'W', otherwise it doesn't work. */
17454 if (flags & SP_REPEAT)
17455 p_ws = FALSE;
17456
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017457 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017458 if (argvars[3].v_type == VAR_UNKNOWN
17459 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017460 skip = (char_u *)"";
17461 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017463 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017464 if (argvars[5].v_type != VAR_UNKNOWN)
17465 {
17466 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17467 if (lnum_stop < 0)
17468 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017469#ifdef FEAT_RELTIME
17470 if (argvars[6].v_type != VAR_UNKNOWN)
17471 {
17472 time_limit = get_tv_number_chk(&argvars[6], NULL);
17473 if (time_limit < 0)
17474 goto theend;
17475 }
17476#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017477 }
17478 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017479 if (skip == NULL)
17480 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017481
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017482 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017483 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017484
17485theend:
17486 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017487
17488 return retval;
17489}
17490
17491/*
17492 * "searchpair()" function
17493 */
17494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017495f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017496{
17497 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17498}
17499
17500/*
17501 * "searchpairpos()" function
17502 */
17503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017504f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017505{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017506 pos_T match_pos;
17507 int lnum = 0;
17508 int col = 0;
17509
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017510 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017511 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017512
17513 if (searchpair_cmn(argvars, &match_pos) > 0)
17514 {
17515 lnum = match_pos.lnum;
17516 col = match_pos.col;
17517 }
17518
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017519 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17520 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017521}
17522
17523/*
17524 * Search for a start/middle/end thing.
17525 * Used by searchpair(), see its documentation for the details.
17526 * Returns 0 or -1 for no match,
17527 */
17528 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017529do_searchpair(
17530 char_u *spat, /* start pattern */
17531 char_u *mpat, /* middle pattern */
17532 char_u *epat, /* end pattern */
17533 int dir, /* BACKWARD or FORWARD */
17534 char_u *skip, /* skip expression */
17535 int flags, /* SP_SETPCMARK and other SP_ values */
17536 pos_T *match_pos,
17537 linenr_T lnum_stop, /* stop at this line if not zero */
17538 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017539{
17540 char_u *save_cpo;
17541 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17542 long retval = 0;
17543 pos_T pos;
17544 pos_T firstpos;
17545 pos_T foundpos;
17546 pos_T save_cursor;
17547 pos_T save_pos;
17548 int n;
17549 int r;
17550 int nest = 1;
17551 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017552 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017553 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017554
17555 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17556 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017557 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017558
Bram Moolenaar76929292008-01-06 19:07:36 +000017559#ifdef FEAT_RELTIME
17560 /* Set the time limit, if there is one. */
17561 profile_setlimit(time_limit, &tm);
17562#endif
17563
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017564 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17565 * start/middle/end (pat3, for the top pair). */
17566 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17567 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17568 if (pat2 == NULL || pat3 == NULL)
17569 goto theend;
17570 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17571 if (*mpat == NUL)
17572 STRCPY(pat3, pat2);
17573 else
17574 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17575 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017576 if (flags & SP_START)
17577 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017578
Bram Moolenaar071d4272004-06-13 20:20:40 +000017579 save_cursor = curwin->w_cursor;
17580 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017581 clearpos(&firstpos);
17582 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017583 pat = pat3;
17584 for (;;)
17585 {
17586 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017587 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017588 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17589 /* didn't find it or found the first match again: FAIL */
17590 break;
17591
17592 if (firstpos.lnum == 0)
17593 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017594 if (equalpos(pos, foundpos))
17595 {
17596 /* Found the same position again. Can happen with a pattern that
17597 * has "\zs" at the end and searching backwards. Advance one
17598 * character and try again. */
17599 if (dir == BACKWARD)
17600 decl(&pos);
17601 else
17602 incl(&pos);
17603 }
17604 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017606 /* clear the start flag to avoid getting stuck here */
17607 options &= ~SEARCH_START;
17608
Bram Moolenaar071d4272004-06-13 20:20:40 +000017609 /* If the skip pattern matches, ignore this match. */
17610 if (*skip != NUL)
17611 {
17612 save_pos = curwin->w_cursor;
17613 curwin->w_cursor = pos;
17614 r = eval_to_bool(skip, &err, NULL, FALSE);
17615 curwin->w_cursor = save_pos;
17616 if (err)
17617 {
17618 /* Evaluating {skip} caused an error, break here. */
17619 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017620 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017621 break;
17622 }
17623 if (r)
17624 continue;
17625 }
17626
17627 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17628 {
17629 /* Found end when searching backwards or start when searching
17630 * forward: nested pair. */
17631 ++nest;
17632 pat = pat2; /* nested, don't search for middle */
17633 }
17634 else
17635 {
17636 /* Found end when searching forward or start when searching
17637 * backward: end of (nested) pair; or found middle in outer pair. */
17638 if (--nest == 1)
17639 pat = pat3; /* outer level, search for middle */
17640 }
17641
17642 if (nest == 0)
17643 {
17644 /* Found the match: return matchcount or line number. */
17645 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017646 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017647 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017648 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017649 if (flags & SP_SETPCMARK)
17650 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017651 curwin->w_cursor = pos;
17652 if (!(flags & SP_REPEAT))
17653 break;
17654 nest = 1; /* search for next unmatched */
17655 }
17656 }
17657
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017658 if (match_pos != NULL)
17659 {
17660 /* Store the match cursor position */
17661 match_pos->lnum = curwin->w_cursor.lnum;
17662 match_pos->col = curwin->w_cursor.col + 1;
17663 }
17664
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017666 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 curwin->w_cursor = save_cursor;
17668
17669theend:
17670 vim_free(pat2);
17671 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017672 if (p_cpo == empty_option)
17673 p_cpo = save_cpo;
17674 else
17675 /* Darn, evaluating the {skip} expression changed the value. */
17676 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017677
17678 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017679}
17680
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017681/*
17682 * "searchpos()" function
17683 */
17684 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017685f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017686{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017687 pos_T match_pos;
17688 int lnum = 0;
17689 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017690 int n;
17691 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017692
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017693 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017694 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017695
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017696 n = search_cmn(argvars, &match_pos, &flags);
17697 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017698 {
17699 lnum = match_pos.lnum;
17700 col = match_pos.col;
17701 }
17702
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017703 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17704 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017705 if (flags & SP_SUBPAT)
17706 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017707}
17708
Bram Moolenaar0d660222005-01-07 21:51:51 +000017709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017710f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017711{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017712#ifdef FEAT_CLIENTSERVER
17713 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017714 char_u *server = get_tv_string_chk(&argvars[0]);
17715 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017716
Bram Moolenaar0d660222005-01-07 21:51:51 +000017717 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017718 if (server == NULL || reply == NULL)
17719 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017720 if (check_restricted() || check_secure())
17721 return;
17722# ifdef FEAT_X11
17723 if (check_connection() == FAIL)
17724 return;
17725# endif
17726
17727 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017728 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017729 EMSG(_("E258: Unable to send to client"));
17730 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017731 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017732 rettv->vval.v_number = 0;
17733#else
17734 rettv->vval.v_number = -1;
17735#endif
17736}
17737
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017739f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017740{
17741 char_u *r = NULL;
17742
17743#ifdef FEAT_CLIENTSERVER
17744# ifdef WIN32
17745 r = serverGetVimNames();
17746# else
17747 make_connection();
17748 if (X_DISPLAY != NULL)
17749 r = serverGetVimNames(X_DISPLAY);
17750# endif
17751#endif
17752 rettv->v_type = VAR_STRING;
17753 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017754}
17755
17756/*
17757 * "setbufvar()" function
17758 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017760f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761{
17762 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017763 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017765 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017766 char_u nbuf[NUMBUFLEN];
17767
17768 if (check_restricted() || check_secure())
17769 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017770 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17771 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017772 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017773 varp = &argvars[2];
17774
17775 if (buf != NULL && varname != NULL && varp != NULL)
17776 {
17777 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017778 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017779
17780 if (*varname == '&')
17781 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017782 long numval;
17783 char_u *strval;
17784 int error = FALSE;
17785
Bram Moolenaar071d4272004-06-13 20:20:40 +000017786 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 numval = get_tv_number_chk(varp, &error);
17788 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017789 if (!error && strval != NULL)
17790 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017791 }
17792 else
17793 {
17794 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17795 if (bufvarname != NULL)
17796 {
17797 STRCPY(bufvarname, "b:");
17798 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017799 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017800 vim_free(bufvarname);
17801 }
17802 }
17803
17804 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017807}
17808
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017809 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017810f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017811{
17812 dict_T *d;
17813 dictitem_T *di;
17814 char_u *csearch;
17815
17816 if (argvars[0].v_type != VAR_DICT)
17817 {
17818 EMSG(_(e_dictreq));
17819 return;
17820 }
17821
17822 if ((d = argvars[0].vval.v_dict) != NULL)
17823 {
17824 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17825 if (csearch != NULL)
17826 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017827#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017828 if (enc_utf8)
17829 {
17830 int pcc[MAX_MCO];
17831 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017832
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017833 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17834 }
17835 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017836#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017837 set_last_csearch(PTR2CHAR(csearch),
17838 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017839 }
17840
17841 di = dict_find(d, (char_u *)"forward", -1);
17842 if (di != NULL)
17843 set_csearch_direction(get_tv_number(&di->di_tv)
17844 ? FORWARD : BACKWARD);
17845
17846 di = dict_find(d, (char_u *)"until", -1);
17847 if (di != NULL)
17848 set_csearch_until(!!get_tv_number(&di->di_tv));
17849 }
17850}
17851
Bram Moolenaar071d4272004-06-13 20:20:40 +000017852/*
17853 * "setcmdpos()" function
17854 */
17855 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017856f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017857{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017858 int pos = (int)get_tv_number(&argvars[0]) - 1;
17859
17860 if (pos >= 0)
17861 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017862}
17863
17864/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017865 * "setfperm({fname}, {mode})" function
17866 */
17867 static void
17868f_setfperm(typval_T *argvars, typval_T *rettv)
17869{
17870 char_u *fname;
17871 char_u modebuf[NUMBUFLEN];
17872 char_u *mode_str;
17873 int i;
17874 int mask;
17875 int mode = 0;
17876
17877 rettv->vval.v_number = 0;
17878 fname = get_tv_string_chk(&argvars[0]);
17879 if (fname == NULL)
17880 return;
17881 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17882 if (mode_str == NULL)
17883 return;
17884 if (STRLEN(mode_str) != 9)
17885 {
17886 EMSG2(_(e_invarg2), mode_str);
17887 return;
17888 }
17889
17890 mask = 1;
17891 for (i = 8; i >= 0; --i)
17892 {
17893 if (mode_str[i] != '-')
17894 mode |= mask;
17895 mask = mask << 1;
17896 }
17897 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17898}
17899
17900/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017901 * "setline()" function
17902 */
17903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017904f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017905{
17906 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017907 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017908 list_T *l = NULL;
17909 listitem_T *li = NULL;
17910 long added = 0;
17911 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017912
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017913 lnum = get_tv_lnum(&argvars[0]);
17914 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017915 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017916 l = argvars[1].vval.v_list;
17917 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017918 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017919 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017920 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017921
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017922 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017923 for (;;)
17924 {
17925 if (l != NULL)
17926 {
17927 /* list argument, get next string */
17928 if (li == NULL)
17929 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017930 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017931 li = li->li_next;
17932 }
17933
17934 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017935 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017936 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017937
17938 /* When coming here from Insert mode, sync undo, so that this can be
17939 * undone separately from what was previously inserted. */
17940 if (u_sync_once == 2)
17941 {
17942 u_sync_once = 1; /* notify that u_sync() was called */
17943 u_sync(TRUE);
17944 }
17945
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017946 if (lnum <= curbuf->b_ml.ml_line_count)
17947 {
17948 /* existing line, replace it */
17949 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17950 {
17951 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017952 if (lnum == curwin->w_cursor.lnum)
17953 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017954 rettv->vval.v_number = 0; /* OK */
17955 }
17956 }
17957 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17958 {
17959 /* lnum is one past the last line, append the line */
17960 ++added;
17961 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17962 rettv->vval.v_number = 0; /* OK */
17963 }
17964
17965 if (l == NULL) /* only one string argument */
17966 break;
17967 ++lnum;
17968 }
17969
17970 if (added > 0)
17971 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017972}
17973
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017974static 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 +000017975
Bram Moolenaar071d4272004-06-13 20:20:40 +000017976/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017977 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017978 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017979 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017980set_qf_ll_list(
17981 win_T *wp UNUSED,
17982 typval_T *list_arg UNUSED,
17983 typval_T *action_arg UNUSED,
17984 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017985{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017986#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017987 char_u *act;
17988 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017989#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017990
Bram Moolenaar2641f772005-03-25 21:58:17 +000017991 rettv->vval.v_number = -1;
17992
17993#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017994 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017995 EMSG(_(e_listreq));
17996 else
17997 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017998 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017999
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018000 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018001 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018002 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018003 if (act == NULL)
18004 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018005 if (*act == 'a' || *act == 'r')
18006 action = *act;
18007 }
18008
Bram Moolenaar81484f42012-12-05 15:16:47 +010018009 if (l != NULL && set_errorlist(wp, l, action,
18010 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018011 rettv->vval.v_number = 0;
18012 }
18013#endif
18014}
18015
18016/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018017 * "setloclist()" function
18018 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018020f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018021{
18022 win_T *win;
18023
18024 rettv->vval.v_number = -1;
18025
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018026 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018027 if (win != NULL)
18028 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18029}
18030
18031/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018032 * "setmatches()" function
18033 */
18034 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018035f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018036{
18037#ifdef FEAT_SEARCH_EXTRA
18038 list_T *l;
18039 listitem_T *li;
18040 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018041 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018042
18043 rettv->vval.v_number = -1;
18044 if (argvars[0].v_type != VAR_LIST)
18045 {
18046 EMSG(_(e_listreq));
18047 return;
18048 }
18049 if ((l = argvars[0].vval.v_list) != NULL)
18050 {
18051
18052 /* To some extent make sure that we are dealing with a list from
18053 * "getmatches()". */
18054 li = l->lv_first;
18055 while (li != NULL)
18056 {
18057 if (li->li_tv.v_type != VAR_DICT
18058 || (d = li->li_tv.vval.v_dict) == NULL)
18059 {
18060 EMSG(_(e_invarg));
18061 return;
18062 }
18063 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018064 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18065 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018066 && dict_find(d, (char_u *)"priority", -1) != NULL
18067 && dict_find(d, (char_u *)"id", -1) != NULL))
18068 {
18069 EMSG(_(e_invarg));
18070 return;
18071 }
18072 li = li->li_next;
18073 }
18074
18075 clear_matches(curwin);
18076 li = l->lv_first;
18077 while (li != NULL)
18078 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018079 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018080 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018081 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018082 char_u *group;
18083 int priority;
18084 int id;
18085 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018086
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018087 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018088 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18089 {
18090 if (s == NULL)
18091 {
18092 s = list_alloc();
18093 if (s == NULL)
18094 return;
18095 }
18096
18097 /* match from matchaddpos() */
18098 for (i = 1; i < 9; i++)
18099 {
18100 sprintf((char *)buf, (char *)"pos%d", i);
18101 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18102 {
18103 if (di->di_tv.v_type != VAR_LIST)
18104 return;
18105
18106 list_append_tv(s, &di->di_tv);
18107 s->lv_refcount++;
18108 }
18109 else
18110 break;
18111 }
18112 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018113
18114 group = get_dict_string(d, (char_u *)"group", FALSE);
18115 priority = (int)get_dict_number(d, (char_u *)"priority");
18116 id = (int)get_dict_number(d, (char_u *)"id");
18117 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18118 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18119 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018120 if (i == 0)
18121 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018122 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018123 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018124 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018125 }
18126 else
18127 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018128 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018129 list_unref(s);
18130 s = NULL;
18131 }
18132
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018133 li = li->li_next;
18134 }
18135 rettv->vval.v_number = 0;
18136 }
18137#endif
18138}
18139
18140/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018141 * "setpos()" function
18142 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018144f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018145{
18146 pos_T pos;
18147 int fnum;
18148 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018149 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018150
Bram Moolenaar08250432008-02-13 11:42:46 +000018151 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018152 name = get_tv_string_chk(argvars);
18153 if (name != NULL)
18154 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018155 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018156 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018157 if (--pos.col < 0)
18158 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018159 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018160 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018161 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018162 if (fnum == curbuf->b_fnum)
18163 {
18164 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018165 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018166 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018167 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018168 curwin->w_set_curswant = FALSE;
18169 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018170 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018171 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018172 }
18173 else
18174 EMSG(_(e_invarg));
18175 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018176 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18177 {
18178 /* set mark */
18179 if (setmark_pos(name[1], &pos, fnum) == OK)
18180 rettv->vval.v_number = 0;
18181 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018182 else
18183 EMSG(_(e_invarg));
18184 }
18185 }
18186}
18187
18188/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018189 * "setqflist()" function
18190 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018192f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018193{
18194 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18195}
18196
18197/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 * "setreg()" function
18199 */
18200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018201f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018202{
18203 int regname;
18204 char_u *strregname;
18205 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018206 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018207 int append;
18208 char_u yank_type;
18209 long block_len;
18210
18211 block_len = -1;
18212 yank_type = MAUTO;
18213 append = FALSE;
18214
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018215 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018216 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018217
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018218 if (strregname == NULL)
18219 return; /* type error; errmsg already given */
18220 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221 if (regname == 0 || regname == '@')
18222 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018224 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018225 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018226 stropt = get_tv_string_chk(&argvars[2]);
18227 if (stropt == NULL)
18228 return; /* type error */
18229 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018230 switch (*stropt)
18231 {
18232 case 'a': case 'A': /* append */
18233 append = TRUE;
18234 break;
18235 case 'v': case 'c': /* character-wise selection */
18236 yank_type = MCHAR;
18237 break;
18238 case 'V': case 'l': /* line-wise selection */
18239 yank_type = MLINE;
18240 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018241 case 'b': case Ctrl_V: /* block-wise selection */
18242 yank_type = MBLOCK;
18243 if (VIM_ISDIGIT(stropt[1]))
18244 {
18245 ++stropt;
18246 block_len = getdigits(&stropt) - 1;
18247 --stropt;
18248 }
18249 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018250 }
18251 }
18252
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018253 if (argvars[1].v_type == VAR_LIST)
18254 {
18255 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018256 char_u **allocval;
18257 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018258 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018259 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018260 int len = argvars[1].vval.v_list->lv_len;
18261 listitem_T *li;
18262
Bram Moolenaar7d647822014-04-05 21:28:56 +020018263 /* First half: use for pointers to result lines; second half: use for
18264 * pointers to allocated copies. */
18265 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018266 if (lstval == NULL)
18267 return;
18268 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018269 allocval = lstval + len + 2;
18270 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018271
18272 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18273 li = li->li_next)
18274 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018275 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018276 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018277 goto free_lstval;
18278 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018279 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018280 /* Need to make a copy, next get_tv_string_buf_chk() will
18281 * overwrite the string. */
18282 strval = vim_strsave(buf);
18283 if (strval == NULL)
18284 goto free_lstval;
18285 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018286 }
18287 *curval++ = strval;
18288 }
18289 *curval++ = NULL;
18290
18291 write_reg_contents_lst(regname, lstval, -1,
18292 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018293free_lstval:
18294 while (curallocval > allocval)
18295 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018296 vim_free(lstval);
18297 }
18298 else
18299 {
18300 strval = get_tv_string_chk(&argvars[1]);
18301 if (strval == NULL)
18302 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018303 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018304 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018305 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018306 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018307}
18308
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018309/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018310 * "settabvar()" function
18311 */
18312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018313f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018314{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018315#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018316 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018317 tabpage_T *tp;
18318#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018319 char_u *varname, *tabvarname;
18320 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018321
18322 rettv->vval.v_number = 0;
18323
18324 if (check_restricted() || check_secure())
18325 return;
18326
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018327#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018328 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018329#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018330 varname = get_tv_string_chk(&argvars[1]);
18331 varp = &argvars[2];
18332
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018333 if (varname != NULL && varp != NULL
18334#ifdef FEAT_WINDOWS
18335 && tp != NULL
18336#endif
18337 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018338 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018339#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018340 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018341 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018342#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018343
18344 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18345 if (tabvarname != NULL)
18346 {
18347 STRCPY(tabvarname, "t:");
18348 STRCPY(tabvarname + 2, varname);
18349 set_var(tabvarname, varp, TRUE);
18350 vim_free(tabvarname);
18351 }
18352
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018353#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018354 /* Restore current tabpage */
18355 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018356 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018357#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018358 }
18359}
18360
18361/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018362 * "settabwinvar()" function
18363 */
18364 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018365f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018366{
18367 setwinvar(argvars, rettv, 1);
18368}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018369
18370/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018371 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018372 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018374f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018375{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018376 setwinvar(argvars, rettv, 0);
18377}
18378
18379/*
18380 * "setwinvar()" and "settabwinvar()" functions
18381 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018382
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018383 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018384setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018385{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386 win_T *win;
18387#ifdef FEAT_WINDOWS
18388 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018389 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018390 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391#endif
18392 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018393 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018395 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018396
18397 if (check_restricted() || check_secure())
18398 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018399
18400#ifdef FEAT_WINDOWS
18401 if (off == 1)
18402 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18403 else
18404 tp = curtab;
18405#endif
18406 win = find_win_by_nr(&argvars[off], tp);
18407 varname = get_tv_string_chk(&argvars[off + 1]);
18408 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018409
18410 if (win != NULL && varname != NULL && varp != NULL)
18411 {
18412#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018413 need_switch_win = !(tp == curtab && win == curwin);
18414 if (!need_switch_win
18415 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018418 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018419 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018420 long numval;
18421 char_u *strval;
18422 int error = FALSE;
18423
18424 ++varname;
18425 numval = get_tv_number_chk(varp, &error);
18426 strval = get_tv_string_buf_chk(varp, nbuf);
18427 if (!error && strval != NULL)
18428 set_option_value(varname, numval, strval, OPT_LOCAL);
18429 }
18430 else
18431 {
18432 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18433 if (winvarname != NULL)
18434 {
18435 STRCPY(winvarname, "w:");
18436 STRCPY(winvarname + 2, varname);
18437 set_var(winvarname, varp, TRUE);
18438 vim_free(winvarname);
18439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440 }
18441 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018442#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018443 if (need_switch_win)
18444 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445#endif
18446 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018447}
18448
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018449#ifdef FEAT_CRYPT
18450/*
18451 * "sha256({string})" function
18452 */
18453 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018454f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018455{
18456 char_u *p;
18457
18458 p = get_tv_string(&argvars[0]);
18459 rettv->vval.v_string = vim_strsave(
18460 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18461 rettv->v_type = VAR_STRING;
18462}
18463#endif /* FEAT_CRYPT */
18464
Bram Moolenaar071d4272004-06-13 20:20:40 +000018465/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018466 * "shellescape({string})" function
18467 */
18468 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018469f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018470{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018471 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018472 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018473 rettv->v_type = VAR_STRING;
18474}
18475
18476/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018477 * shiftwidth() function
18478 */
18479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018480f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018481{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018482 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018483}
18484
18485/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018486 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018487 */
18488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018489f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018491 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018492
Bram Moolenaar0d660222005-01-07 21:51:51 +000018493 p = get_tv_string(&argvars[0]);
18494 rettv->vval.v_string = vim_strsave(p);
18495 simplify_filename(rettv->vval.v_string); /* simplify in place */
18496 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018497}
18498
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018499#ifdef FEAT_FLOAT
18500/*
18501 * "sin()" function
18502 */
18503 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018504f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018505{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018506 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018507
18508 rettv->v_type = VAR_FLOAT;
18509 if (get_float_arg(argvars, &f) == OK)
18510 rettv->vval.v_float = sin(f);
18511 else
18512 rettv->vval.v_float = 0.0;
18513}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018514
18515/*
18516 * "sinh()" function
18517 */
18518 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018519f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018520{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018521 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018522
18523 rettv->v_type = VAR_FLOAT;
18524 if (get_float_arg(argvars, &f) == OK)
18525 rettv->vval.v_float = sinh(f);
18526 else
18527 rettv->vval.v_float = 0.0;
18528}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018529#endif
18530
Bram Moolenaar0d660222005-01-07 21:51:51 +000018531static int
18532#ifdef __BORLANDC__
18533 _RTLENTRYF
18534#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018535 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018536static int
18537#ifdef __BORLANDC__
18538 _RTLENTRYF
18539#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018540 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018541
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018542/* struct used in the array that's given to qsort() */
18543typedef struct
18544{
18545 listitem_T *item;
18546 int idx;
18547} sortItem_T;
18548
Bram Moolenaar0b962472016-02-22 22:51:33 +010018549/* struct storing information about current sort */
18550typedef struct
18551{
18552 int item_compare_ic;
18553 int item_compare_numeric;
18554 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018555#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018556 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018557#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018558 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018559 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018560 dict_T *item_compare_selfdict;
18561 int item_compare_func_err;
18562 int item_compare_keep_zero;
18563} sortinfo_T;
18564static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018565static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018566#define ITEM_COMPARE_FAIL 999
18567
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018569 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018570 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018571 static int
18572#ifdef __BORLANDC__
18573_RTLENTRYF
18574#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018575item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018576{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018577 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018578 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018579 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018580 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018581 int res;
18582 char_u numbuf1[NUMBUFLEN];
18583 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018584
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018585 si1 = (sortItem_T *)s1;
18586 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018587 tv1 = &si1->item->li_tv;
18588 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018589
Bram Moolenaar0b962472016-02-22 22:51:33 +010018590 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018591 {
18592 long v1 = get_tv_number(tv1);
18593 long v2 = get_tv_number(tv2);
18594
18595 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18596 }
18597
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018598#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018599 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018600 {
18601 float_T v1 = get_tv_float(tv1);
18602 float_T v2 = get_tv_float(tv2);
18603
18604 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18605 }
18606#endif
18607
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018608 /* tv2string() puts quotes around a string and allocates memory. Don't do
18609 * that for string variables. Use a single quote when comparing with a
18610 * non-string to do what the docs promise. */
18611 if (tv1->v_type == VAR_STRING)
18612 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018613 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018614 p1 = (char_u *)"'";
18615 else
18616 p1 = tv1->vval.v_string;
18617 }
18618 else
18619 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18620 if (tv2->v_type == VAR_STRING)
18621 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018622 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018623 p2 = (char_u *)"'";
18624 else
18625 p2 = tv2->vval.v_string;
18626 }
18627 else
18628 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018629 if (p1 == NULL)
18630 p1 = (char_u *)"";
18631 if (p2 == NULL)
18632 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018633 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018634 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018635 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018636 res = STRICMP(p1, p2);
18637 else
18638 res = STRCMP(p1, p2);
18639 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018640 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018641 {
18642 double n1, n2;
18643 n1 = strtod((char *)p1, (char **)&p1);
18644 n2 = strtod((char *)p2, (char **)&p2);
18645 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18646 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018647
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018648 /* When the result would be zero, compare the item indexes. Makes the
18649 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018650 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018651 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018652
Bram Moolenaar0d660222005-01-07 21:51:51 +000018653 vim_free(tofree1);
18654 vim_free(tofree2);
18655 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018656}
18657
18658 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018659#ifdef __BORLANDC__
18660_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018661#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018662item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018664 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018666 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018667 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018668 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018669 char_u *func_name;
18670 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018671
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018672 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018673 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018674 return 0;
18675
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018676 si1 = (sortItem_T *)s1;
18677 si2 = (sortItem_T *)s2;
18678
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018679 if (partial == NULL)
18680 func_name = sortinfo->item_compare_func;
18681 else
18682 func_name = partial->pt_name;
18683
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018684 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018685 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018686 copy_tv(&si1->item->li_tv, &argv[0]);
18687 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018688
18689 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018690 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018691 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018692 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018693 clear_tv(&argv[0]);
18694 clear_tv(&argv[1]);
18695
18696 if (res == FAIL)
18697 res = ITEM_COMPARE_FAIL;
18698 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018699 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18700 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018701 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018702 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018703
18704 /* When the result would be zero, compare the pointers themselves. Makes
18705 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018706 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018707 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018708
Bram Moolenaar0d660222005-01-07 21:51:51 +000018709 return res;
18710}
18711
18712/*
18713 * "sort({list})" function
18714 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018716do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717{
Bram Moolenaar33570922005-01-25 22:26:29 +000018718 list_T *l;
18719 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018720 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 sortinfo_T *old_sortinfo;
18722 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018723 long len;
18724 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018725
Bram Moolenaar0b962472016-02-22 22:51:33 +010018726 /* Pointer to current info struct used in compare function. Save and
18727 * restore the current one for nested calls. */
18728 old_sortinfo = sortinfo;
18729 sortinfo = &info;
18730
Bram Moolenaar0d660222005-01-07 21:51:51 +000018731 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018732 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018733 else
18734 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018735 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018736 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018737 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18738 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018739 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018740 rettv->vval.v_list = l;
18741 rettv->v_type = VAR_LIST;
18742 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018743
Bram Moolenaar0d660222005-01-07 21:51:51 +000018744 len = list_len(l);
18745 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018746 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018747
Bram Moolenaar0b962472016-02-22 22:51:33 +010018748 info.item_compare_ic = FALSE;
18749 info.item_compare_numeric = FALSE;
18750 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018751#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018752 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018753#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018754 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018755 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018756 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018757 if (argvars[1].v_type != VAR_UNKNOWN)
18758 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018759 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018760 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018761 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018762 else if (argvars[1].v_type == VAR_PARTIAL)
18763 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018764 else
18765 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018766 int error = FALSE;
18767
18768 i = get_tv_number_chk(&argvars[1], &error);
18769 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018770 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018771 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018772 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018773 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018774 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018775 else if (i != 0)
18776 {
18777 EMSG(_(e_invarg));
18778 goto theend;
18779 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018780 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018781 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018782 if (*info.item_compare_func == NUL)
18783 {
18784 /* empty string means default sort */
18785 info.item_compare_func = NULL;
18786 }
18787 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018788 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018789 info.item_compare_func = NULL;
18790 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018791 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018792 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018793 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018794 info.item_compare_func = NULL;
18795 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018796 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018797#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018798 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018799 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018800 info.item_compare_func = NULL;
18801 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018802 }
18803#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018804 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018805 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018806 info.item_compare_func = NULL;
18807 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018808 }
18809 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018810 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018811
18812 if (argvars[2].v_type != VAR_UNKNOWN)
18813 {
18814 /* optional third argument: {dict} */
18815 if (argvars[2].v_type != VAR_DICT)
18816 {
18817 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018818 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018819 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018820 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018821 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018823
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018825 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018826 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018827 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018828
Bram Moolenaar327aa022014-03-25 18:24:23 +010018829 i = 0;
18830 if (sort)
18831 {
18832 /* sort(): ptrs will be the list to sort */
18833 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018834 {
18835 ptrs[i].item = li;
18836 ptrs[i].idx = i;
18837 ++i;
18838 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018839
Bram Moolenaar0b962472016-02-22 22:51:33 +010018840 info.item_compare_func_err = FALSE;
18841 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018842 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018843 if ((info.item_compare_func != NULL
18844 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018845 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018846 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018847 EMSG(_("E702: Sort compare function failed"));
18848 else
18849 {
18850 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018851 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018852 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018853 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018854 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018855
Bram Moolenaar0b962472016-02-22 22:51:33 +010018856 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018857 {
18858 /* Clear the List and append the items in sorted order. */
18859 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18860 l->lv_len = 0;
18861 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018862 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018863 }
18864 }
18865 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018866 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018867 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018868 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018869
18870 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018871 info.item_compare_func_err = FALSE;
18872 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018873 item_compare_func_ptr = info.item_compare_func != NULL
18874 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018875 ? item_compare2 : item_compare;
18876
18877 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18878 li = li->li_next)
18879 {
18880 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18881 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018882 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018883 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018884 {
18885 EMSG(_("E882: Uniq compare function failed"));
18886 break;
18887 }
18888 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018889
Bram Moolenaar0b962472016-02-22 22:51:33 +010018890 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018891 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018892 while (--i >= 0)
18893 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018894 li = ptrs[i].item->li_next;
18895 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018896 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018897 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018898 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018899 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018900 list_fix_watch(l, li);
18901 listitem_free(li);
18902 l->lv_len--;
18903 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018904 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018905 }
18906
18907 vim_free(ptrs);
18908 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018909theend:
18910 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018911}
18912
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018913/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018914 * "sort({list})" function
18915 */
18916 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018917f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018918{
18919 do_sort_uniq(argvars, rettv, TRUE);
18920}
18921
18922/*
18923 * "uniq({list})" function
18924 */
18925 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018926f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018927{
18928 do_sort_uniq(argvars, rettv, FALSE);
18929}
18930
18931/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018932 * "soundfold({word})" function
18933 */
18934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018935f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018936{
18937 char_u *s;
18938
18939 rettv->v_type = VAR_STRING;
18940 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018941#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018942 rettv->vval.v_string = eval_soundfold(s);
18943#else
18944 rettv->vval.v_string = vim_strsave(s);
18945#endif
18946}
18947
18948/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018949 * "spellbadword()" function
18950 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018952f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018953{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018954 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018955 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018956 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018957
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018958 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018959 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018960
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018961#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018962 if (argvars[0].v_type == VAR_UNKNOWN)
18963 {
18964 /* Find the start and length of the badly spelled word. */
18965 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18966 if (len != 0)
18967 word = ml_get_cursor();
18968 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018969 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018970 {
18971 char_u *str = get_tv_string_chk(&argvars[0]);
18972 int capcol = -1;
18973
18974 if (str != NULL)
18975 {
18976 /* Check the argument for spelling. */
18977 while (*str != NUL)
18978 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018979 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018980 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018981 {
18982 word = str;
18983 break;
18984 }
18985 str += len;
18986 }
18987 }
18988 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018989#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018990
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018991 list_append_string(rettv->vval.v_list, word, len);
18992 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018993 attr == HLF_SPB ? "bad" :
18994 attr == HLF_SPR ? "rare" :
18995 attr == HLF_SPL ? "local" :
18996 attr == HLF_SPC ? "caps" :
18997 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018998}
18999
19000/*
19001 * "spellsuggest()" function
19002 */
19003 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019004f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019005{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019006#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019007 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019008 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019009 int maxcount;
19010 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019011 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019012 listitem_T *li;
19013 int need_capital = FALSE;
19014#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019015
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019016 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019017 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019018
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019019#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019020 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019021 {
19022 str = get_tv_string(&argvars[0]);
19023 if (argvars[1].v_type != VAR_UNKNOWN)
19024 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019025 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019026 if (maxcount <= 0)
19027 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019028 if (argvars[2].v_type != VAR_UNKNOWN)
19029 {
19030 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19031 if (typeerr)
19032 return;
19033 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019034 }
19035 else
19036 maxcount = 25;
19037
Bram Moolenaar4770d092006-01-12 23:22:24 +000019038 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019039
19040 for (i = 0; i < ga.ga_len; ++i)
19041 {
19042 str = ((char_u **)ga.ga_data)[i];
19043
19044 li = listitem_alloc();
19045 if (li == NULL)
19046 vim_free(str);
19047 else
19048 {
19049 li->li_tv.v_type = VAR_STRING;
19050 li->li_tv.v_lock = 0;
19051 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019052 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019053 }
19054 }
19055 ga_clear(&ga);
19056 }
19057#endif
19058}
19059
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019061f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019062{
19063 char_u *str;
19064 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019065 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019066 regmatch_T regmatch;
19067 char_u patbuf[NUMBUFLEN];
19068 char_u *save_cpo;
19069 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019070 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019071 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019072 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019073
19074 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19075 save_cpo = p_cpo;
19076 p_cpo = (char_u *)"";
19077
19078 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019079 if (argvars[1].v_type != VAR_UNKNOWN)
19080 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019081 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19082 if (pat == NULL)
19083 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019084 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019085 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019086 }
19087 if (pat == NULL || *pat == NUL)
19088 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019089
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019090 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019091 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019092 if (typeerr)
19093 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019094
Bram Moolenaar0d660222005-01-07 21:51:51 +000019095 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19096 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019097 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019099 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019100 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019101 if (*str == NUL)
19102 match = FALSE; /* empty item at the end */
19103 else
19104 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019105 if (match)
19106 end = regmatch.startp[0];
19107 else
19108 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019109 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19110 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019111 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019112 if (list_append_string(rettv->vval.v_list, str,
19113 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019114 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019115 }
19116 if (!match)
19117 break;
19118 /* Advance to just after the match. */
19119 if (regmatch.endp[0] > str)
19120 col = 0;
19121 else
19122 {
19123 /* Don't get stuck at the same match. */
19124#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019125 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019126#else
19127 col = 1;
19128#endif
19129 }
19130 str = regmatch.endp[0];
19131 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132
Bram Moolenaar473de612013-06-08 18:19:48 +020019133 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019134 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135
Bram Moolenaar0d660222005-01-07 21:51:51 +000019136 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019137}
19138
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019139#ifdef FEAT_FLOAT
19140/*
19141 * "sqrt()" function
19142 */
19143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019144f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019145{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019146 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019147
19148 rettv->v_type = VAR_FLOAT;
19149 if (get_float_arg(argvars, &f) == OK)
19150 rettv->vval.v_float = sqrt(f);
19151 else
19152 rettv->vval.v_float = 0.0;
19153}
19154
19155/*
19156 * "str2float()" function
19157 */
19158 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019159f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019160{
19161 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19162
19163 if (*p == '+')
19164 p = skipwhite(p + 1);
19165 (void)string2float(p, &rettv->vval.v_float);
19166 rettv->v_type = VAR_FLOAT;
19167}
19168#endif
19169
Bram Moolenaar2c932302006-03-18 21:42:09 +000019170/*
19171 * "str2nr()" function
19172 */
19173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019174f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019175{
19176 int base = 10;
19177 char_u *p;
19178 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019179 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019180
19181 if (argvars[1].v_type != VAR_UNKNOWN)
19182 {
19183 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019184 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019185 {
19186 EMSG(_(e_invarg));
19187 return;
19188 }
19189 }
19190
19191 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019192 if (*p == '+')
19193 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019194 switch (base)
19195 {
19196 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19197 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19198 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19199 default: what = 0;
19200 }
19201 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019202 rettv->vval.v_number = n;
19203}
19204
Bram Moolenaar071d4272004-06-13 20:20:40 +000019205#ifdef HAVE_STRFTIME
19206/*
19207 * "strftime({format}[, {time}])" function
19208 */
19209 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019210f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019211{
19212 char_u result_buf[256];
19213 struct tm *curtime;
19214 time_t seconds;
19215 char_u *p;
19216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019219 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019220 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019221 seconds = time(NULL);
19222 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019223 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019224 curtime = localtime(&seconds);
19225 /* MSVC returns NULL for an invalid value of seconds. */
19226 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019227 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019228 else
19229 {
19230# ifdef FEAT_MBYTE
19231 vimconv_T conv;
19232 char_u *enc;
19233
19234 conv.vc_type = CONV_NONE;
19235 enc = enc_locale();
19236 convert_setup(&conv, p_enc, enc);
19237 if (conv.vc_type != CONV_NONE)
19238 p = string_convert(&conv, p, NULL);
19239# endif
19240 if (p != NULL)
19241 (void)strftime((char *)result_buf, sizeof(result_buf),
19242 (char *)p, curtime);
19243 else
19244 result_buf[0] = NUL;
19245
19246# ifdef FEAT_MBYTE
19247 if (conv.vc_type != CONV_NONE)
19248 vim_free(p);
19249 convert_setup(&conv, enc, p_enc);
19250 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019251 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019252 else
19253# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019254 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019255
19256# ifdef FEAT_MBYTE
19257 /* Release conversion descriptors */
19258 convert_setup(&conv, NULL, NULL);
19259 vim_free(enc);
19260# endif
19261 }
19262}
19263#endif
19264
19265/*
19266 * "stridx()" function
19267 */
19268 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019269f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019270{
19271 char_u buf[NUMBUFLEN];
19272 char_u *needle;
19273 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019276 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019277
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019278 needle = get_tv_string_chk(&argvars[1]);
19279 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019280 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019281 if (needle == NULL || haystack == NULL)
19282 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019283
Bram Moolenaar33570922005-01-25 22:26:29 +000019284 if (argvars[2].v_type != VAR_UNKNOWN)
19285 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019286 int error = FALSE;
19287
19288 start_idx = get_tv_number_chk(&argvars[2], &error);
19289 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019290 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019291 if (start_idx >= 0)
19292 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019293 }
19294
19295 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19296 if (pos != NULL)
19297 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298}
19299
19300/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019301 * "string()" function
19302 */
19303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019304f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019305{
19306 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019307 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019308
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019309 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019310 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19311 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019312 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019313 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019314 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019315}
19316
19317/*
19318 * "strlen()" function
19319 */
19320 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019321f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019322{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019323 rettv->vval.v_number = (varnumber_T)(STRLEN(
19324 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019325}
19326
19327/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019328 * "strchars()" function
19329 */
19330 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019331f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019332{
19333 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019334 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019335#ifdef FEAT_MBYTE
19336 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019337 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019338#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019339
19340 if (argvars[1].v_type != VAR_UNKNOWN)
19341 skipcc = get_tv_number_chk(&argvars[1], NULL);
19342 if (skipcc < 0 || skipcc > 1)
19343 EMSG(_(e_invarg));
19344 else
19345 {
19346#ifdef FEAT_MBYTE
19347 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19348 while (*s != NUL)
19349 {
19350 func_mb_ptr2char_adv(&s);
19351 ++len;
19352 }
19353 rettv->vval.v_number = len;
19354#else
19355 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19356#endif
19357 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019358}
19359
19360/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019361 * "strdisplaywidth()" function
19362 */
19363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019364f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019365{
19366 char_u *s = get_tv_string(&argvars[0]);
19367 int col = 0;
19368
19369 if (argvars[1].v_type != VAR_UNKNOWN)
19370 col = get_tv_number(&argvars[1]);
19371
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019372 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019373}
19374
19375/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019376 * "strwidth()" function
19377 */
19378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019379f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019380{
19381 char_u *s = get_tv_string(&argvars[0]);
19382
19383 rettv->vval.v_number = (varnumber_T)(
19384#ifdef FEAT_MBYTE
19385 mb_string2cells(s, -1)
19386#else
19387 STRLEN(s)
19388#endif
19389 );
19390}
19391
19392/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019393 * "strpart()" function
19394 */
19395 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019396f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019397{
19398 char_u *p;
19399 int n;
19400 int len;
19401 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019402 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019404 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019405 slen = (int)STRLEN(p);
19406
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019407 n = get_tv_number_chk(&argvars[1], &error);
19408 if (error)
19409 len = 0;
19410 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019411 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019412 else
19413 len = slen - n; /* default len: all bytes that are available. */
19414
19415 /*
19416 * Only return the overlap between the specified part and the actual
19417 * string.
19418 */
19419 if (n < 0)
19420 {
19421 len += n;
19422 n = 0;
19423 }
19424 else if (n > slen)
19425 n = slen;
19426 if (len < 0)
19427 len = 0;
19428 else if (n + len > slen)
19429 len = slen - n;
19430
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019431 rettv->v_type = VAR_STRING;
19432 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019433}
19434
19435/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019436 * "strridx()" function
19437 */
19438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019439f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019440{
19441 char_u buf[NUMBUFLEN];
19442 char_u *needle;
19443 char_u *haystack;
19444 char_u *rest;
19445 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019446 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019447
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019448 needle = get_tv_string_chk(&argvars[1]);
19449 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019450
19451 rettv->vval.v_number = -1;
19452 if (needle == NULL || haystack == NULL)
19453 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019454
19455 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019456 if (argvars[2].v_type != VAR_UNKNOWN)
19457 {
19458 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019459 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019460 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019462 }
19463 else
19464 end_idx = haystack_len;
19465
Bram Moolenaar0d660222005-01-07 21:51:51 +000019466 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019467 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019468 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019469 lastmatch = haystack + end_idx;
19470 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019471 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019472 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019473 for (rest = haystack; *rest != '\0'; ++rest)
19474 {
19475 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019476 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019477 break;
19478 lastmatch = rest;
19479 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019480 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019481
19482 if (lastmatch == NULL)
19483 rettv->vval.v_number = -1;
19484 else
19485 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19486}
19487
19488/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019489 * "strtrans()" function
19490 */
19491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019492f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019493{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019494 rettv->v_type = VAR_STRING;
19495 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019496}
19497
19498/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019499 * "submatch()" function
19500 */
19501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019502f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019503{
Bram Moolenaar41571762014-04-02 19:00:58 +020019504 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019505 int no;
19506 int retList = 0;
19507
19508 no = (int)get_tv_number_chk(&argvars[0], &error);
19509 if (error)
19510 return;
19511 error = FALSE;
19512 if (argvars[1].v_type != VAR_UNKNOWN)
19513 retList = get_tv_number_chk(&argvars[1], &error);
19514 if (error)
19515 return;
19516
19517 if (retList == 0)
19518 {
19519 rettv->v_type = VAR_STRING;
19520 rettv->vval.v_string = reg_submatch(no);
19521 }
19522 else
19523 {
19524 rettv->v_type = VAR_LIST;
19525 rettv->vval.v_list = reg_submatch_list(no);
19526 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019527}
19528
19529/*
19530 * "substitute()" function
19531 */
19532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019533f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019534{
19535 char_u patbuf[NUMBUFLEN];
19536 char_u subbuf[NUMBUFLEN];
19537 char_u flagsbuf[NUMBUFLEN];
19538
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019539 char_u *str = get_tv_string_chk(&argvars[0]);
19540 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19541 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19542 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19543
Bram Moolenaar0d660222005-01-07 21:51:51 +000019544 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019545 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19546 rettv->vval.v_string = NULL;
19547 else
19548 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019549}
19550
19551/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019552 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019553 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019555f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556{
19557 int id = 0;
19558#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019559 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019560 long col;
19561 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019562 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019564 lnum = get_tv_lnum(argvars); /* -1 on type error */
19565 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19566 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019567
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019568 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019569 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019570 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019571#endif
19572
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019573 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019574}
19575
19576/*
19577 * "synIDattr(id, what [, mode])" function
19578 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019580f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019581{
19582 char_u *p = NULL;
19583#ifdef FEAT_SYN_HL
19584 int id;
19585 char_u *what;
19586 char_u *mode;
19587 char_u modebuf[NUMBUFLEN];
19588 int modec;
19589
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019590 id = get_tv_number(&argvars[0]);
19591 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019592 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019594 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019596 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019597 modec = 0; /* replace invalid with current */
19598 }
19599 else
19600 {
19601#ifdef FEAT_GUI
19602 if (gui.in_use)
19603 modec = 'g';
19604 else
19605#endif
19606 if (t_colors > 1)
19607 modec = 'c';
19608 else
19609 modec = 't';
19610 }
19611
19612
19613 switch (TOLOWER_ASC(what[0]))
19614 {
19615 case 'b':
19616 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19617 p = highlight_color(id, what, modec);
19618 else /* bold */
19619 p = highlight_has_attr(id, HL_BOLD, modec);
19620 break;
19621
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019622 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019623 p = highlight_color(id, what, modec);
19624 break;
19625
19626 case 'i':
19627 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19628 p = highlight_has_attr(id, HL_INVERSE, modec);
19629 else /* italic */
19630 p = highlight_has_attr(id, HL_ITALIC, modec);
19631 break;
19632
19633 case 'n': /* name */
19634 p = get_highlight_name(NULL, id - 1);
19635 break;
19636
19637 case 'r': /* reverse */
19638 p = highlight_has_attr(id, HL_INVERSE, modec);
19639 break;
19640
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019641 case 's':
19642 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19643 p = highlight_color(id, what, modec);
19644 else /* standout */
19645 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019646 break;
19647
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019648 case 'u':
19649 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19650 /* underline */
19651 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19652 else
19653 /* undercurl */
19654 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019655 break;
19656 }
19657
19658 if (p != NULL)
19659 p = vim_strsave(p);
19660#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019661 rettv->v_type = VAR_STRING;
19662 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019663}
19664
19665/*
19666 * "synIDtrans(id)" function
19667 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019669f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019670{
19671 int id;
19672
19673#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019674 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675
19676 if (id > 0)
19677 id = syn_get_final_id(id);
19678 else
19679#endif
19680 id = 0;
19681
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019682 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019683}
19684
19685/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019686 * "synconcealed(lnum, col)" function
19687 */
19688 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019689f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019690{
19691#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19692 long lnum;
19693 long col;
19694 int syntax_flags = 0;
19695 int cchar;
19696 int matchid = 0;
19697 char_u str[NUMBUFLEN];
19698#endif
19699
19700 rettv->v_type = VAR_LIST;
19701 rettv->vval.v_list = NULL;
19702
19703#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19704 lnum = get_tv_lnum(argvars); /* -1 on type error */
19705 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19706
19707 vim_memset(str, NUL, sizeof(str));
19708
19709 if (rettv_list_alloc(rettv) != FAIL)
19710 {
19711 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19712 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19713 && curwin->w_p_cole > 0)
19714 {
19715 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19716 syntax_flags = get_syntax_info(&matchid);
19717
19718 /* get the conceal character */
19719 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19720 {
19721 cchar = syn_get_sub_char();
19722 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19723 cchar = lcs_conceal;
19724 if (cchar != NUL)
19725 {
19726# ifdef FEAT_MBYTE
19727 if (has_mbyte)
19728 (*mb_char2bytes)(cchar, str);
19729 else
19730# endif
19731 str[0] = cchar;
19732 }
19733 }
19734 }
19735
19736 list_append_number(rettv->vval.v_list,
19737 (syntax_flags & HL_CONCEAL) != 0);
19738 /* -1 to auto-determine strlen */
19739 list_append_string(rettv->vval.v_list, str, -1);
19740 list_append_number(rettv->vval.v_list, matchid);
19741 }
19742#endif
19743}
19744
19745/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019746 * "synstack(lnum, col)" function
19747 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019749f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019750{
19751#ifdef FEAT_SYN_HL
19752 long lnum;
19753 long col;
19754 int i;
19755 int id;
19756#endif
19757
19758 rettv->v_type = VAR_LIST;
19759 rettv->vval.v_list = NULL;
19760
19761#ifdef FEAT_SYN_HL
19762 lnum = get_tv_lnum(argvars); /* -1 on type error */
19763 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19764
19765 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019766 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019767 && rettv_list_alloc(rettv) != FAIL)
19768 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019769 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019770 for (i = 0; ; ++i)
19771 {
19772 id = syn_get_stack_item(i);
19773 if (id < 0)
19774 break;
19775 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19776 break;
19777 }
19778 }
19779#endif
19780}
19781
Bram Moolenaar071d4272004-06-13 20:20:40 +000019782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019783get_cmd_output_as_rettv(
19784 typval_T *argvars,
19785 typval_T *rettv,
19786 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019788 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019789 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019790 char_u *infile = NULL;
19791 char_u buf[NUMBUFLEN];
19792 int err = FALSE;
19793 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019794 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019795 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019796
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019797 rettv->v_type = VAR_STRING;
19798 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019799 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019800 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019801
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019802 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019803 {
19804 /*
19805 * Write the string to a temp file, to be used for input of the shell
19806 * command.
19807 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019808 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019809 {
19810 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019811 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019812 }
19813
19814 fd = mch_fopen((char *)infile, WRITEBIN);
19815 if (fd == NULL)
19816 {
19817 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019818 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019819 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019820 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019821 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019822 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19823 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019824 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019825 else
19826 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019827 size_t len;
19828
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019829 p = get_tv_string_buf_chk(&argvars[1], buf);
19830 if (p == NULL)
19831 {
19832 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019833 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019834 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019835 len = STRLEN(p);
19836 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019837 err = TRUE;
19838 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019839 if (fclose(fd) != 0)
19840 err = TRUE;
19841 if (err)
19842 {
19843 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019844 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019845 }
19846 }
19847
Bram Moolenaar52a72462014-08-29 15:53:52 +020019848 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19849 * echoes typeahead, that messes up the display. */
19850 if (!msg_silent)
19851 flags += SHELL_COOKED;
19852
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019853 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019854 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019855 int len;
19856 listitem_T *li;
19857 char_u *s = NULL;
19858 char_u *start;
19859 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019860 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019861
Bram Moolenaar52a72462014-08-29 15:53:52 +020019862 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019863 if (res == NULL)
19864 goto errret;
19865
19866 list = list_alloc();
19867 if (list == NULL)
19868 goto errret;
19869
19870 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019871 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019872 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019873 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019874 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019875 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019876
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019877 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019878 if (s == NULL)
19879 goto errret;
19880
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019881 for (p = s; start < end; ++p, ++start)
19882 *p = *start == NUL ? NL : *start;
19883 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019884
19885 li = listitem_alloc();
19886 if (li == NULL)
19887 {
19888 vim_free(s);
19889 goto errret;
19890 }
19891 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019892 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019893 li->li_tv.vval.v_string = s;
19894 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019895 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019896
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019897 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019898 rettv->v_type = VAR_LIST;
19899 rettv->vval.v_list = list;
19900 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019901 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019902 else
19903 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019904 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019905#ifdef USE_CR
19906 /* translate <CR> into <NL> */
19907 if (res != NULL)
19908 {
19909 char_u *s;
19910
19911 for (s = res; *s; ++s)
19912 {
19913 if (*s == CAR)
19914 *s = NL;
19915 }
19916 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019917#else
19918# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019919 /* translate <CR><NL> into <NL> */
19920 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019921 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019922 char_u *s, *d;
19923
19924 d = res;
19925 for (s = res; *s; ++s)
19926 {
19927 if (s[0] == CAR && s[1] == NL)
19928 ++s;
19929 *d++ = *s;
19930 }
19931 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019932 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019933# endif
19934#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019935 rettv->vval.v_string = res;
19936 res = NULL;
19937 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019938
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019939errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019940 if (infile != NULL)
19941 {
19942 mch_remove(infile);
19943 vim_free(infile);
19944 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019945 if (res != NULL)
19946 vim_free(res);
19947 if (list != NULL)
19948 list_free(list, TRUE);
19949}
19950
19951/*
19952 * "system()" function
19953 */
19954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019955f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019956{
19957 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19958}
19959
19960/*
19961 * "systemlist()" function
19962 */
19963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019964f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019965{
19966 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019967}
19968
19969/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019970 * "tabpagebuflist()" function
19971 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019973f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019974{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019975#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019976 tabpage_T *tp;
19977 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019978
19979 if (argvars[0].v_type == VAR_UNKNOWN)
19980 wp = firstwin;
19981 else
19982 {
19983 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19984 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019985 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019986 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019987 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019988 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019989 for (; wp != NULL; wp = wp->w_next)
19990 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019991 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019992 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019993 }
19994#endif
19995}
19996
19997
19998/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019999 * "tabpagenr()" function
20000 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020002f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020003{
20004 int nr = 1;
20005#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020006 char_u *arg;
20007
20008 if (argvars[0].v_type != VAR_UNKNOWN)
20009 {
20010 arg = get_tv_string_chk(&argvars[0]);
20011 nr = 0;
20012 if (arg != NULL)
20013 {
20014 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020015 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020016 else
20017 EMSG2(_(e_invexpr2), arg);
20018 }
20019 }
20020 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020021 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020022#endif
20023 rettv->vval.v_number = nr;
20024}
20025
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020026
20027#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020028static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020029
20030/*
20031 * Common code for tabpagewinnr() and winnr().
20032 */
20033 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020034get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020035{
20036 win_T *twin;
20037 int nr = 1;
20038 win_T *wp;
20039 char_u *arg;
20040
20041 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20042 if (argvar->v_type != VAR_UNKNOWN)
20043 {
20044 arg = get_tv_string_chk(argvar);
20045 if (arg == NULL)
20046 nr = 0; /* type error; errmsg already given */
20047 else if (STRCMP(arg, "$") == 0)
20048 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20049 else if (STRCMP(arg, "#") == 0)
20050 {
20051 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20052 if (twin == NULL)
20053 nr = 0;
20054 }
20055 else
20056 {
20057 EMSG2(_(e_invexpr2), arg);
20058 nr = 0;
20059 }
20060 }
20061
20062 if (nr > 0)
20063 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20064 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020065 {
20066 if (wp == NULL)
20067 {
20068 /* didn't find it in this tabpage */
20069 nr = 0;
20070 break;
20071 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020072 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020073 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020074 return nr;
20075}
20076#endif
20077
20078/*
20079 * "tabpagewinnr()" function
20080 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020082f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020083{
20084 int nr = 1;
20085#ifdef FEAT_WINDOWS
20086 tabpage_T *tp;
20087
20088 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20089 if (tp == NULL)
20090 nr = 0;
20091 else
20092 nr = get_winnr(tp, &argvars[1]);
20093#endif
20094 rettv->vval.v_number = nr;
20095}
20096
20097
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020098/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020099 * "tagfiles()" function
20100 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020101 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020102f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020103{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020104 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020105 tagname_T tn;
20106 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020107
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020108 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020109 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020110 fname = alloc(MAXPATHL);
20111 if (fname == NULL)
20112 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020113
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020114 for (first = TRUE; ; first = FALSE)
20115 if (get_tagfname(&tn, first, fname) == FAIL
20116 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020117 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020118 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020119 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020120}
20121
20122/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020123 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020124 */
20125 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020126f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020127{
20128 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020129
20130 tag_pattern = get_tv_string(&argvars[0]);
20131
20132 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020133 if (*tag_pattern == NUL)
20134 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020135
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020136 if (rettv_list_alloc(rettv) == OK)
20137 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020138}
20139
20140/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 * "tempname()" function
20142 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020144f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020145{
20146 static int x = 'A';
20147
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020148 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020149 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020150
20151 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20152 * names. Skip 'I' and 'O', they are used for shell redirection. */
20153 do
20154 {
20155 if (x == 'Z')
20156 x = '0';
20157 else if (x == '9')
20158 x = 'A';
20159 else
20160 {
20161#ifdef EBCDIC
20162 if (x == 'I')
20163 x = 'J';
20164 else if (x == 'R')
20165 x = 'S';
20166 else
20167#endif
20168 ++x;
20169 }
20170 } while (x == 'I' || x == 'O');
20171}
20172
20173/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020174 * "test(list)" function: Just checking the walls...
20175 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020176 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020177f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020178{
20179 /* Used for unit testing. Change the code below to your liking. */
20180#if 0
20181 listitem_T *li;
20182 list_T *l;
20183 char_u *bad, *good;
20184
20185 if (argvars[0].v_type != VAR_LIST)
20186 return;
20187 l = argvars[0].vval.v_list;
20188 if (l == NULL)
20189 return;
20190 li = l->lv_first;
20191 if (li == NULL)
20192 return;
20193 bad = get_tv_string(&li->li_tv);
20194 li = li->li_next;
20195 if (li == NULL)
20196 return;
20197 good = get_tv_string(&li->li_tv);
20198 rettv->vval.v_number = test_edit_score(bad, good);
20199#endif
20200}
20201
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020202#ifdef FEAT_FLOAT
20203/*
20204 * "tan()" function
20205 */
20206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020208{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020209 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020210
20211 rettv->v_type = VAR_FLOAT;
20212 if (get_float_arg(argvars, &f) == OK)
20213 rettv->vval.v_float = tan(f);
20214 else
20215 rettv->vval.v_float = 0.0;
20216}
20217
20218/*
20219 * "tanh()" function
20220 */
20221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020222f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020223{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020224 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020225
20226 rettv->v_type = VAR_FLOAT;
20227 if (get_float_arg(argvars, &f) == OK)
20228 rettv->vval.v_float = tanh(f);
20229 else
20230 rettv->vval.v_float = 0.0;
20231}
20232#endif
20233
Bram Moolenaar975b5272016-03-15 23:10:59 +010020234#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20235/*
20236 * Get a callback from "arg". It can be a Funcref or a function name.
20237 * When "arg" is zero return an empty string.
20238 * Return NULL for an invalid argument.
20239 */
20240 char_u *
20241get_callback(typval_T *arg, partial_T **pp)
20242{
20243 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20244 {
20245 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020246 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020247 return (*pp)->pt_name;
20248 }
20249 *pp = NULL;
20250 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20251 return arg->vval.v_string;
20252 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20253 return (char_u *)"";
20254 EMSG(_("E921: Invalid callback argument"));
20255 return NULL;
20256}
20257#endif
20258
20259#ifdef FEAT_TIMERS
20260/*
20261 * "timer_start(time, callback [, options])" function
20262 */
20263 static void
20264f_timer_start(typval_T *argvars, typval_T *rettv)
20265{
20266 long msec = get_tv_number(&argvars[0]);
20267 timer_T *timer;
20268 int repeat = 0;
20269 char_u *callback;
20270 dict_T *dict;
20271
20272 if (argvars[2].v_type != VAR_UNKNOWN)
20273 {
20274 if (argvars[2].v_type != VAR_DICT
20275 || (dict = argvars[2].vval.v_dict) == NULL)
20276 {
20277 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20278 return;
20279 }
20280 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20281 repeat = get_dict_number(dict, (char_u *)"repeat");
20282 }
20283
20284 timer = create_timer(msec, repeat);
20285 callback = get_callback(&argvars[1], &timer->tr_partial);
20286 if (callback == NULL)
20287 {
20288 stop_timer(timer);
20289 rettv->vval.v_number = -1;
20290 }
20291 else
20292 {
20293 timer->tr_callback = vim_strsave(callback);
20294 rettv->vval.v_number = timer->tr_id;
20295 }
20296}
20297
20298/*
20299 * "timer_stop(timer)" function
20300 */
20301 static void
20302f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20303{
20304 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20305
20306 if (timer != NULL)
20307 stop_timer(timer);
20308}
20309#endif
20310
Bram Moolenaard52d9742005-08-21 22:20:28 +000020311/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020312 * "tolower(string)" function
20313 */
20314 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020315f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020316{
20317 char_u *p;
20318
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020319 p = vim_strsave(get_tv_string(&argvars[0]));
20320 rettv->v_type = VAR_STRING;
20321 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020322
20323 if (p != NULL)
20324 while (*p != NUL)
20325 {
20326#ifdef FEAT_MBYTE
20327 int l;
20328
20329 if (enc_utf8)
20330 {
20331 int c, lc;
20332
20333 c = utf_ptr2char(p);
20334 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020335 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020336 /* TODO: reallocate string when byte count changes. */
20337 if (utf_char2len(lc) == l)
20338 utf_char2bytes(lc, p);
20339 p += l;
20340 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020341 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020342 p += l; /* skip multi-byte character */
20343 else
20344#endif
20345 {
20346 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20347 ++p;
20348 }
20349 }
20350}
20351
20352/*
20353 * "toupper(string)" function
20354 */
20355 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020356f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020357{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020358 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020359 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020360}
20361
20362/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020363 * "tr(string, fromstr, tostr)" function
20364 */
20365 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020366f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020367{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020368 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020369 char_u *fromstr;
20370 char_u *tostr;
20371 char_u *p;
20372#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020373 int inlen;
20374 int fromlen;
20375 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020376 int idx;
20377 char_u *cpstr;
20378 int cplen;
20379 int first = TRUE;
20380#endif
20381 char_u buf[NUMBUFLEN];
20382 char_u buf2[NUMBUFLEN];
20383 garray_T ga;
20384
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020385 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020386 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20387 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020388
20389 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020390 rettv->v_type = VAR_STRING;
20391 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020392 if (fromstr == NULL || tostr == NULL)
20393 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020394 ga_init2(&ga, (int)sizeof(char), 80);
20395
20396#ifdef FEAT_MBYTE
20397 if (!has_mbyte)
20398#endif
20399 /* not multi-byte: fromstr and tostr must be the same length */
20400 if (STRLEN(fromstr) != STRLEN(tostr))
20401 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020402#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020403error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020404#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020405 EMSG2(_(e_invarg2), fromstr);
20406 ga_clear(&ga);
20407 return;
20408 }
20409
20410 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020411 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020412 {
20413#ifdef FEAT_MBYTE
20414 if (has_mbyte)
20415 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020416 inlen = (*mb_ptr2len)(in_str);
20417 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020418 cplen = inlen;
20419 idx = 0;
20420 for (p = fromstr; *p != NUL; p += fromlen)
20421 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020422 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020423 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020424 {
20425 for (p = tostr; *p != NUL; p += tolen)
20426 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020427 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020428 if (idx-- == 0)
20429 {
20430 cplen = tolen;
20431 cpstr = p;
20432 break;
20433 }
20434 }
20435 if (*p == NUL) /* tostr is shorter than fromstr */
20436 goto error;
20437 break;
20438 }
20439 ++idx;
20440 }
20441
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020442 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020443 {
20444 /* Check that fromstr and tostr have the same number of
20445 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020446 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020447 first = FALSE;
20448 for (p = tostr; *p != NUL; p += tolen)
20449 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020450 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020451 --idx;
20452 }
20453 if (idx != 0)
20454 goto error;
20455 }
20456
Bram Moolenaarcde88542015-08-11 19:14:00 +020020457 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020458 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020459 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020460
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020461 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020462 }
20463 else
20464#endif
20465 {
20466 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020467 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020468 if (p != NULL)
20469 ga_append(&ga, tostr[p - fromstr]);
20470 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020471 ga_append(&ga, *in_str);
20472 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020473 }
20474 }
20475
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020476 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020477 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020478 ga_append(&ga, NUL);
20479
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020480 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020481}
20482
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020483#ifdef FEAT_FLOAT
20484/*
20485 * "trunc({float})" function
20486 */
20487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020488f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020489{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020490 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020491
20492 rettv->v_type = VAR_FLOAT;
20493 if (get_float_arg(argvars, &f) == OK)
20494 /* trunc() is not in C90, use floor() or ceil() instead. */
20495 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20496 else
20497 rettv->vval.v_float = 0.0;
20498}
20499#endif
20500
Bram Moolenaar8299df92004-07-10 09:47:34 +000020501/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020502 * "type(expr)" function
20503 */
20504 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020505f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020506{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020507 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020508
20509 switch (argvars[0].v_type)
20510 {
20511 case VAR_NUMBER: n = 0; break;
20512 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020513 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020514 case VAR_FUNC: n = 2; break;
20515 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020516 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020517 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020518 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020519 if (argvars[0].vval.v_number == VVAL_FALSE
20520 || argvars[0].vval.v_number == VVAL_TRUE)
20521 n = 6;
20522 else
20523 n = 7;
20524 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020525 case VAR_JOB: n = 8; break;
20526 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020527 case VAR_UNKNOWN:
20528 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20529 n = -1;
20530 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020531 }
20532 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020533}
20534
20535/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020536 * "undofile(name)" function
20537 */
20538 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020539f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020540{
20541 rettv->v_type = VAR_STRING;
20542#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020543 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020544 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020545
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020546 if (*fname == NUL)
20547 {
20548 /* If there is no file name there will be no undo file. */
20549 rettv->vval.v_string = NULL;
20550 }
20551 else
20552 {
20553 char_u *ffname = FullName_save(fname, FALSE);
20554
20555 if (ffname != NULL)
20556 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20557 vim_free(ffname);
20558 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020559 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020560#else
20561 rettv->vval.v_string = NULL;
20562#endif
20563}
20564
20565/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020566 * "undotree()" function
20567 */
20568 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020569f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020570{
20571 if (rettv_dict_alloc(rettv) == OK)
20572 {
20573 dict_T *dict = rettv->vval.v_dict;
20574 list_T *list;
20575
Bram Moolenaar730cde92010-06-27 05:18:54 +020020576 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020577 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020578 dict_add_nr_str(dict, "save_last",
20579 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020580 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20581 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020582 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020583
20584 list = list_alloc();
20585 if (list != NULL)
20586 {
20587 u_eval_tree(curbuf->b_u_oldhead, list);
20588 dict_add_list(dict, "entries", list);
20589 }
20590 }
20591}
20592
20593/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020594 * "values(dict)" function
20595 */
20596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020597f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020598{
20599 dict_list(argvars, rettv, 1);
20600}
20601
20602/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603 * "virtcol(string)" function
20604 */
20605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020606f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607{
20608 colnr_T vcol = 0;
20609 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020610 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020611
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020612 fp = var2fpos(&argvars[0], FALSE, &fnum);
20613 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20614 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020615 {
20616 getvvcol(curwin, fp, NULL, NULL, &vcol);
20617 ++vcol;
20618 }
20619
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020620 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020621}
20622
20623/*
20624 * "visualmode()" function
20625 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020627f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020628{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020629 char_u str[2];
20630
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020631 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020632 str[0] = curbuf->b_visual_mode_eval;
20633 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635
20636 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020637 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020638 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639}
20640
20641/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020642 * "wildmenumode()" function
20643 */
20644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020645f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020646{
20647#ifdef FEAT_WILDMENU
20648 if (wild_menu_showing)
20649 rettv->vval.v_number = 1;
20650#endif
20651}
20652
20653/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020654 * "winbufnr(nr)" function
20655 */
20656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020657f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020658{
20659 win_T *wp;
20660
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020661 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020665 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020666}
20667
20668/*
20669 * "wincol()" function
20670 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020672f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020673{
20674 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020675 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020676}
20677
20678/*
20679 * "winheight(nr)" function
20680 */
20681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020682f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020683{
20684 win_T *wp;
20685
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020686 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020690 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020691}
20692
20693/*
20694 * "winline()" function
20695 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020697f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020698{
20699 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020700 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020701}
20702
20703/*
20704 * "winnr()" function
20705 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020707f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020708{
20709 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020710
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020712 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020714 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020715}
20716
20717/*
20718 * "winrestcmd()" function
20719 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020721f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020722{
20723#ifdef FEAT_WINDOWS
20724 win_T *wp;
20725 int winnr = 1;
20726 garray_T ga;
20727 char_u buf[50];
20728
20729 ga_init2(&ga, (int)sizeof(char), 70);
20730 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20731 {
20732 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20733 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20735 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020736 ++winnr;
20737 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020738 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020744 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020745}
20746
20747/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020748 * "winrestview()" function
20749 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020750 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020751f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020752{
20753 dict_T *dict;
20754
20755 if (argvars[0].v_type != VAR_DICT
20756 || (dict = argvars[0].vval.v_dict) == NULL)
20757 EMSG(_(e_invarg));
20758 else
20759 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020760 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20761 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20762 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20763 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020764#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020765 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20766 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020767#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020768 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20769 {
20770 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20771 curwin->w_set_curswant = FALSE;
20772 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020773
Bram Moolenaar82c25852014-05-28 16:47:16 +020020774 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20775 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020776#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020777 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20778 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020779#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020780 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20781 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20782 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20783 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020784
20785 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020786 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020787# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020788 win_new_width(curwin, W_WIDTH(curwin));
20789# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020790 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020791
Bram Moolenaarb851a962014-10-31 15:45:52 +010020792 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020793 curwin->w_topline = 1;
20794 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20795 curwin->w_topline = curbuf->b_ml.ml_line_count;
20796#ifdef FEAT_DIFF
20797 check_topfill(curwin, TRUE);
20798#endif
20799 }
20800}
20801
20802/*
20803 * "winsaveview()" function
20804 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020806f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020807{
20808 dict_T *dict;
20809
Bram Moolenaara800b422010-06-27 01:15:55 +020020810 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020811 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020812 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020813
20814 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20815 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20816#ifdef FEAT_VIRTUALEDIT
20817 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20818#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020819 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020820 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20821
20822 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20823#ifdef FEAT_DIFF
20824 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20825#endif
20826 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20827 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20828}
20829
20830/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020831 * "winwidth(nr)" function
20832 */
20833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020834f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020835{
20836 win_T *wp;
20837
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020838 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020841 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020842#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020845 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020846#endif
20847}
20848
Bram Moolenaar071d4272004-06-13 20:20:40 +000020849/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020850 * "wordcount()" function
20851 */
20852 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020853f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020854{
20855 if (rettv_dict_alloc(rettv) == FAIL)
20856 return;
20857 cursor_pos_info(rettv->vval.v_dict);
20858}
20859
20860/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020861 * Write list of strings to file
20862 */
20863 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020864write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020865{
20866 listitem_T *li;
20867 int c;
20868 int ret = OK;
20869 char_u *s;
20870
20871 for (li = list->lv_first; li != NULL; li = li->li_next)
20872 {
20873 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20874 {
20875 if (*s == '\n')
20876 c = putc(NUL, fd);
20877 else
20878 c = putc(*s, fd);
20879 if (c == EOF)
20880 {
20881 ret = FAIL;
20882 break;
20883 }
20884 }
20885 if (!binary || li->li_next != NULL)
20886 if (putc('\n', fd) == EOF)
20887 {
20888 ret = FAIL;
20889 break;
20890 }
20891 if (ret == FAIL)
20892 {
20893 EMSG(_(e_write));
20894 break;
20895 }
20896 }
20897 return ret;
20898}
20899
20900/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020901 * "writefile()" function
20902 */
20903 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020904f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020905{
20906 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020907 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020908 char_u *fname;
20909 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020910 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020911
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020912 if (check_restricted() || check_secure())
20913 return;
20914
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020915 if (argvars[0].v_type != VAR_LIST)
20916 {
20917 EMSG2(_(e_listarg), "writefile()");
20918 return;
20919 }
20920 if (argvars[0].vval.v_list == NULL)
20921 return;
20922
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020923 if (argvars[2].v_type != VAR_UNKNOWN)
20924 {
20925 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20926 binary = TRUE;
20927 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20928 append = TRUE;
20929 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020930
20931 /* Always open the file in binary mode, library functions have a mind of
20932 * their own about CR-LF conversion. */
20933 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020934 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20935 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020936 {
20937 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20938 ret = -1;
20939 }
20940 else
20941 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020942 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20943 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020944 fclose(fd);
20945 }
20946
20947 rettv->vval.v_number = ret;
20948}
20949
20950/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020951 * "xor(expr, expr)" function
20952 */
20953 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020954f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020955{
20956 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20957 ^ get_tv_number_chk(&argvars[1], NULL);
20958}
20959
20960
20961/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020963 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020964 */
20965 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020966var2fpos(
20967 typval_T *varp,
20968 int dollar_lnum, /* TRUE when $ is last line */
20969 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020971 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020973 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020974
Bram Moolenaara5525202006-03-02 22:52:09 +000020975 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020976 if (varp->v_type == VAR_LIST)
20977 {
20978 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020979 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020980 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020981 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020982
20983 l = varp->vval.v_list;
20984 if (l == NULL)
20985 return NULL;
20986
20987 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020988 pos.lnum = list_find_nr(l, 0L, &error);
20989 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020990 return NULL; /* invalid line number */
20991
20992 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020993 pos.col = list_find_nr(l, 1L, &error);
20994 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020995 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020996 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020997
20998 /* We accept "$" for the column number: last column. */
20999 li = list_find(l, 1L);
21000 if (li != NULL && li->li_tv.v_type == VAR_STRING
21001 && li->li_tv.vval.v_string != NULL
21002 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21003 pos.col = len + 1;
21004
Bram Moolenaara5525202006-03-02 22:52:09 +000021005 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021006 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021007 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021008 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021009
Bram Moolenaara5525202006-03-02 22:52:09 +000021010#ifdef FEAT_VIRTUALEDIT
21011 /* Get the virtual offset. Defaults to zero. */
21012 pos.coladd = list_find_nr(l, 2L, &error);
21013 if (error)
21014 pos.coladd = 0;
21015#endif
21016
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021017 return &pos;
21018 }
21019
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021020 name = get_tv_string_chk(varp);
21021 if (name == NULL)
21022 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021023 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021024 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021025 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21026 {
21027 if (VIsual_active)
21028 return &VIsual;
21029 return &curwin->w_cursor;
21030 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021031 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021033 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021034 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21035 return NULL;
21036 return pp;
21037 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021038
21039#ifdef FEAT_VIRTUALEDIT
21040 pos.coladd = 0;
21041#endif
21042
Bram Moolenaar477933c2007-07-17 14:32:23 +000021043 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021044 {
21045 pos.col = 0;
21046 if (name[1] == '0') /* "w0": first visible line */
21047 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021048 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021049 pos.lnum = curwin->w_topline;
21050 return &pos;
21051 }
21052 else if (name[1] == '$') /* "w$": last visible line */
21053 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021054 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021055 pos.lnum = curwin->w_botline - 1;
21056 return &pos;
21057 }
21058 }
21059 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021061 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021062 {
21063 pos.lnum = curbuf->b_ml.ml_line_count;
21064 pos.col = 0;
21065 }
21066 else
21067 {
21068 pos.lnum = curwin->w_cursor.lnum;
21069 pos.col = (colnr_T)STRLEN(ml_get_curline());
21070 }
21071 return &pos;
21072 }
21073 return NULL;
21074}
21075
21076/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021077 * Convert list in "arg" into a position and optional file number.
21078 * When "fnump" is NULL there is no file number, only 3 items.
21079 * Note that the column is passed on as-is, the caller may want to decrement
21080 * it to use 1 for the first column.
21081 * Return FAIL when conversion is not possible, doesn't check the position for
21082 * validity.
21083 */
21084 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021085list2fpos(
21086 typval_T *arg,
21087 pos_T *posp,
21088 int *fnump,
21089 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021090{
21091 list_T *l = arg->vval.v_list;
21092 long i = 0;
21093 long n;
21094
Bram Moolenaar493c1782014-05-28 14:34:46 +020021095 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21096 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021097 if (arg->v_type != VAR_LIST
21098 || l == NULL
21099 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021100 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021101 return FAIL;
21102
21103 if (fnump != NULL)
21104 {
21105 n = list_find_nr(l, i++, NULL); /* fnum */
21106 if (n < 0)
21107 return FAIL;
21108 if (n == 0)
21109 n = curbuf->b_fnum; /* current buffer */
21110 *fnump = n;
21111 }
21112
21113 n = list_find_nr(l, i++, NULL); /* lnum */
21114 if (n < 0)
21115 return FAIL;
21116 posp->lnum = n;
21117
21118 n = list_find_nr(l, i++, NULL); /* col */
21119 if (n < 0)
21120 return FAIL;
21121 posp->col = n;
21122
21123#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021124 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021125 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021126 posp->coladd = 0;
21127 else
21128 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021129#endif
21130
Bram Moolenaar493c1782014-05-28 14:34:46 +020021131 if (curswantp != NULL)
21132 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21133
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021134 return OK;
21135}
21136
21137/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021138 * Get the length of an environment variable name.
21139 * Advance "arg" to the first character after the name.
21140 * Return 0 for error.
21141 */
21142 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021143get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021144{
21145 char_u *p;
21146 int len;
21147
21148 for (p = *arg; vim_isIDc(*p); ++p)
21149 ;
21150 if (p == *arg) /* no name found */
21151 return 0;
21152
21153 len = (int)(p - *arg);
21154 *arg = p;
21155 return len;
21156}
21157
21158/*
21159 * Get the length of the name of a function or internal variable.
21160 * "arg" is advanced to the first non-white character after the name.
21161 * Return 0 if something is wrong.
21162 */
21163 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021164get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165{
21166 char_u *p;
21167 int len;
21168
21169 /* Find the end of the name. */
21170 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021171 {
21172 if (*p == ':')
21173 {
21174 /* "s:" is start of "s:var", but "n:" is not and can be used in
21175 * slice "[n:]". Also "xx:" is not a namespace. */
21176 len = (int)(p - *arg);
21177 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21178 || len > 1)
21179 break;
21180 }
21181 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021182 if (p == *arg) /* no name found */
21183 return 0;
21184
21185 len = (int)(p - *arg);
21186 *arg = skipwhite(p);
21187
21188 return len;
21189}
21190
21191/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021192 * Get the length of the name of a variable or function.
21193 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021194 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021195 * Return -1 if curly braces expansion failed.
21196 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021197 * If the name contains 'magic' {}'s, expand them and return the
21198 * expanded name in an allocated string via 'alias' - caller must free.
21199 */
21200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021201get_name_len(
21202 char_u **arg,
21203 char_u **alias,
21204 int evaluate,
21205 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206{
21207 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021208 char_u *p;
21209 char_u *expr_start;
21210 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021211
21212 *alias = NULL; /* default to no alias */
21213
21214 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21215 && (*arg)[2] == (int)KE_SNR)
21216 {
21217 /* hard coded <SNR>, already translated */
21218 *arg += 3;
21219 return get_id_len(arg) + 3;
21220 }
21221 len = eval_fname_script(*arg);
21222 if (len > 0)
21223 {
21224 /* literal "<SID>", "s:" or "<SNR>" */
21225 *arg += len;
21226 }
21227
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021229 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021231 p = find_name_end(*arg, &expr_start, &expr_end,
21232 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021233 if (expr_start != NULL)
21234 {
21235 char_u *temp_string;
21236
21237 if (!evaluate)
21238 {
21239 len += (int)(p - *arg);
21240 *arg = skipwhite(p);
21241 return len;
21242 }
21243
21244 /*
21245 * Include any <SID> etc in the expanded string:
21246 * Thus the -len here.
21247 */
21248 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21249 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021250 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021251 *alias = temp_string;
21252 *arg = skipwhite(p);
21253 return (int)STRLEN(temp_string);
21254 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021255
21256 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021257 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021258 EMSG2(_(e_invexpr2), *arg);
21259
21260 return len;
21261}
21262
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021263/*
21264 * Find the end of a variable or function name, taking care of magic braces.
21265 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21266 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021267 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021268 * Return a pointer to just after the name. Equal to "arg" if there is no
21269 * valid name.
21270 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021271 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021272find_name_end(
21273 char_u *arg,
21274 char_u **expr_start,
21275 char_u **expr_end,
21276 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021277{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021278 int mb_nest = 0;
21279 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021281 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021284 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021285 *expr_start = NULL;
21286 *expr_end = NULL;
21287 }
21288
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021289 /* Quick check for valid starting character. */
21290 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21291 return arg;
21292
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021293 for (p = arg; *p != NUL
21294 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021295 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021296 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021297 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021298 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021299 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021300 if (*p == '\'')
21301 {
21302 /* skip over 'string' to avoid counting [ and ] inside it. */
21303 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21304 ;
21305 if (*p == NUL)
21306 break;
21307 }
21308 else if (*p == '"')
21309 {
21310 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21311 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21312 if (*p == '\\' && p[1] != NUL)
21313 ++p;
21314 if (*p == NUL)
21315 break;
21316 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021317 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21318 {
21319 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021320 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021321 len = (int)(p - arg);
21322 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021323 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021324 break;
21325 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021327 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021328 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021329 if (*p == '[')
21330 ++br_nest;
21331 else if (*p == ']')
21332 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021334
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021337 if (*p == '{')
21338 {
21339 mb_nest++;
21340 if (expr_start != NULL && *expr_start == NULL)
21341 *expr_start = p;
21342 }
21343 else if (*p == '}')
21344 {
21345 mb_nest--;
21346 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21347 *expr_end = p;
21348 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021350 }
21351
21352 return p;
21353}
21354
21355/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021356 * Expands out the 'magic' {}'s in a variable/function name.
21357 * Note that this can call itself recursively, to deal with
21358 * constructs like foo{bar}{baz}{bam}
21359 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21360 * "in_start" ^
21361 * "expr_start" ^
21362 * "expr_end" ^
21363 * "in_end" ^
21364 *
21365 * Returns a new allocated string, which the caller must free.
21366 * Returns NULL for failure.
21367 */
21368 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021369make_expanded_name(
21370 char_u *in_start,
21371 char_u *expr_start,
21372 char_u *expr_end,
21373 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021374{
21375 char_u c1;
21376 char_u *retval = NULL;
21377 char_u *temp_result;
21378 char_u *nextcmd = NULL;
21379
21380 if (expr_end == NULL || in_end == NULL)
21381 return NULL;
21382 *expr_start = NUL;
21383 *expr_end = NUL;
21384 c1 = *in_end;
21385 *in_end = NUL;
21386
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021387 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021388 if (temp_result != NULL && nextcmd == NULL)
21389 {
21390 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21391 + (in_end - expr_end) + 1));
21392 if (retval != NULL)
21393 {
21394 STRCPY(retval, in_start);
21395 STRCAT(retval, temp_result);
21396 STRCAT(retval, expr_end + 1);
21397 }
21398 }
21399 vim_free(temp_result);
21400
21401 *in_end = c1; /* put char back for error messages */
21402 *expr_start = '{';
21403 *expr_end = '}';
21404
21405 if (retval != NULL)
21406 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021407 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021408 if (expr_start != NULL)
21409 {
21410 /* Further expansion! */
21411 temp_result = make_expanded_name(retval, expr_start,
21412 expr_end, temp_result);
21413 vim_free(retval);
21414 retval = temp_result;
21415 }
21416 }
21417
21418 return retval;
21419}
21420
21421/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021423 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021424 */
21425 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021426eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021427{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021428 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21429}
21430
21431/*
21432 * Return TRUE if character "c" can be used as the first character in a
21433 * variable or function name (excluding '{' and '}').
21434 */
21435 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021436eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021437{
21438 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021439}
21440
21441/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021442 * Set number v: variable to "val".
21443 */
21444 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021445set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021447 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021448}
21449
21450/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021451 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021452 */
21453 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021454get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021456 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021457}
21458
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021459/*
21460 * Get string v: variable value. Uses a static buffer, can only be used once.
21461 */
21462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021463get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021464{
21465 return get_tv_string(&vimvars[idx].vv_tv);
21466}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021467
Bram Moolenaar071d4272004-06-13 20:20:40 +000021468/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021469 * Get List v: variable value. Caller must take care of reference count when
21470 * needed.
21471 */
21472 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021473get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021474{
21475 return vimvars[idx].vv_list;
21476}
21477
21478/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021479 * Set v:char to character "c".
21480 */
21481 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021482set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021483{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021484 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021485
21486#ifdef FEAT_MBYTE
21487 if (has_mbyte)
21488 buf[(*mb_char2bytes)(c, buf)] = NUL;
21489 else
21490#endif
21491 {
21492 buf[0] = c;
21493 buf[1] = NUL;
21494 }
21495 set_vim_var_string(VV_CHAR, buf, -1);
21496}
21497
21498/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021499 * Set v:count to "count" and v:count1 to "count1".
21500 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021501 */
21502 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021503set_vcount(
21504 long count,
21505 long count1,
21506 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021507{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021508 if (set_prevcount)
21509 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021510 vimvars[VV_COUNT].vv_nr = count;
21511 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512}
21513
21514/*
21515 * Set string v: variable to a copy of "val".
21516 */
21517 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021518set_vim_var_string(
21519 int idx,
21520 char_u *val,
21521 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021522{
Bram Moolenaara542c682016-01-31 16:28:04 +010021523 clear_tv(&vimvars[idx].vv_di.di_tv);
21524 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021526 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021528 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021530 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021531}
21532
21533/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021534 * Set List v: variable to "val".
21535 */
21536 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021537set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021538{
Bram Moolenaara542c682016-01-31 16:28:04 +010021539 clear_tv(&vimvars[idx].vv_di.di_tv);
21540 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021541 vimvars[idx].vv_list = val;
21542 if (val != NULL)
21543 ++val->lv_refcount;
21544}
21545
21546/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021547 * Set Dictionary v: variable to "val".
21548 */
21549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021550set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021551{
21552 int todo;
21553 hashitem_T *hi;
21554
Bram Moolenaara542c682016-01-31 16:28:04 +010021555 clear_tv(&vimvars[idx].vv_di.di_tv);
21556 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021557 vimvars[idx].vv_dict = val;
21558 if (val != NULL)
21559 {
21560 ++val->dv_refcount;
21561
21562 /* Set readonly */
21563 todo = (int)val->dv_hashtab.ht_used;
21564 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21565 {
21566 if (HASHITEM_EMPTY(hi))
21567 continue;
21568 --todo;
21569 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21570 }
21571 }
21572}
21573
21574/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021575 * Set v:register if needed.
21576 */
21577 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021578set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021579{
21580 char_u regname;
21581
21582 if (c == 0 || c == ' ')
21583 regname = '"';
21584 else
21585 regname = c;
21586 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021587 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588 set_vim_var_string(VV_REG, &regname, 1);
21589}
21590
21591/*
21592 * Get or set v:exception. If "oldval" == NULL, return the current value.
21593 * Otherwise, restore the value to "oldval" and return NULL.
21594 * Must always be called in pairs to save and restore v:exception! Does not
21595 * take care of memory allocations.
21596 */
21597 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021598v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021599{
21600 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021601 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602
Bram Moolenaare9a41262005-01-15 22:18:47 +000021603 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021604 return NULL;
21605}
21606
21607/*
21608 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21609 * Otherwise, restore the value to "oldval" and return NULL.
21610 * Must always be called in pairs to save and restore v:throwpoint! Does not
21611 * take care of memory allocations.
21612 */
21613 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021614v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021615{
21616 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021617 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618
Bram Moolenaare9a41262005-01-15 22:18:47 +000021619 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021620 return NULL;
21621}
21622
21623#if defined(FEAT_AUTOCMD) || defined(PROTO)
21624/*
21625 * Set v:cmdarg.
21626 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21627 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21628 * Must always be called in pairs!
21629 */
21630 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021631set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021632{
21633 char_u *oldval;
21634 char_u *newval;
21635 unsigned len;
21636
Bram Moolenaare9a41262005-01-15 22:18:47 +000021637 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021638 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021640 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021641 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021642 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021643 }
21644
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021645 if (eap->force_bin == FORCE_BIN)
21646 len = 6;
21647 else if (eap->force_bin == FORCE_NOBIN)
21648 len = 8;
21649 else
21650 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021651
21652 if (eap->read_edit)
21653 len += 7;
21654
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021655 if (eap->force_ff != 0)
21656 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21657# ifdef FEAT_MBYTE
21658 if (eap->force_enc != 0)
21659 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021660 if (eap->bad_char != 0)
21661 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021662# endif
21663
21664 newval = alloc(len + 1);
21665 if (newval == NULL)
21666 return NULL;
21667
21668 if (eap->force_bin == FORCE_BIN)
21669 sprintf((char *)newval, " ++bin");
21670 else if (eap->force_bin == FORCE_NOBIN)
21671 sprintf((char *)newval, " ++nobin");
21672 else
21673 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021674
21675 if (eap->read_edit)
21676 STRCAT(newval, " ++edit");
21677
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021678 if (eap->force_ff != 0)
21679 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21680 eap->cmd + eap->force_ff);
21681# ifdef FEAT_MBYTE
21682 if (eap->force_enc != 0)
21683 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21684 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021685 if (eap->bad_char == BAD_KEEP)
21686 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21687 else if (eap->bad_char == BAD_DROP)
21688 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21689 else if (eap->bad_char != 0)
21690 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021691# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021692 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021693 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021694}
21695#endif
21696
21697/*
21698 * Get the value of internal variable "name".
21699 * Return OK or FAIL.
21700 */
21701 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021702get_var_tv(
21703 char_u *name,
21704 int len, /* length of "name" */
21705 typval_T *rettv, /* NULL when only checking existence */
21706 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21707 int verbose, /* may give error message */
21708 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021709{
21710 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021711 typval_T *tv = NULL;
21712 typval_T atv;
21713 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021714 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021715
21716 /* truncate the name, so that we can use strcmp() */
21717 cc = name[len];
21718 name[len] = NUL;
21719
21720 /*
21721 * Check for "b:changedtick".
21722 */
21723 if (STRCMP(name, "b:changedtick") == 0)
21724 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021725 atv.v_type = VAR_NUMBER;
21726 atv.vval.v_number = curbuf->b_changedtick;
21727 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021728 }
21729
21730 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021731 * Check for user-defined variables.
21732 */
21733 else
21734 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021735 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021736 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021737 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021738 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021739 if (dip != NULL)
21740 *dip = v;
21741 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021742 }
21743
Bram Moolenaare9a41262005-01-15 22:18:47 +000021744 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021745 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021746 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021747 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021748 ret = FAIL;
21749 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021750 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021751 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021752
21753 name[len] = cc;
21754
21755 return ret;
21756}
21757
21758/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021759 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21760 * Also handle function call with Funcref variable: func(expr)
21761 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21762 */
21763 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021764handle_subscript(
21765 char_u **arg,
21766 typval_T *rettv,
21767 int evaluate, /* do more than finding the end */
21768 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021769{
21770 int ret = OK;
21771 dict_T *selfdict = NULL;
21772 char_u *s;
21773 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021774 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021775
21776 while (ret == OK
21777 && (**arg == '['
21778 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021779 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21780 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021781 && !vim_iswhite(*(*arg - 1)))
21782 {
21783 if (**arg == '(')
21784 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021785 partial_T *pt = NULL;
21786
Bram Moolenaard9fba312005-06-26 22:34:35 +000021787 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021788 if (evaluate)
21789 {
21790 functv = *rettv;
21791 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021792
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021793 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021794 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021795 {
21796 pt = functv.vval.v_partial;
21797 s = pt->pt_name;
21798 }
21799 else
21800 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021801 }
21802 else
21803 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021804 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021805 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021806 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021807
21808 /* Clear the funcref afterwards, so that deleting it while
21809 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021810 if (evaluate)
21811 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021812
21813 /* Stop the expression evaluation when immediately aborting on
21814 * error, or when an interrupt occurred or an exception was thrown
21815 * but not caught. */
21816 if (aborting())
21817 {
21818 if (ret == OK)
21819 clear_tv(rettv);
21820 ret = FAIL;
21821 }
21822 dict_unref(selfdict);
21823 selfdict = NULL;
21824 }
21825 else /* **arg == '[' || **arg == '.' */
21826 {
21827 dict_unref(selfdict);
21828 if (rettv->v_type == VAR_DICT)
21829 {
21830 selfdict = rettv->vval.v_dict;
21831 if (selfdict != NULL)
21832 ++selfdict->dv_refcount;
21833 }
21834 else
21835 selfdict = NULL;
21836 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21837 {
21838 clear_tv(rettv);
21839 ret = FAIL;
21840 }
21841 }
21842 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021843
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021844 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21845 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021846 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021847 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21848 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021849 char_u *tofree = NULL;
21850 ufunc_T *fp;
21851 char_u fname_buf[FLEN_FIXED + 1];
21852 int error;
21853
21854 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021855 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021856 fp = find_func(fname);
21857 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021858
21859 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021860 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021861 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021862 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21863
21864 if (pt != NULL)
21865 {
21866 pt->pt_refcount = 1;
21867 pt->pt_dict = selfdict;
21868 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021869 if (rettv->v_type == VAR_FUNC)
21870 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021871 /* Just a function: Take over the function name and use
21872 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021873 pt->pt_name = rettv->vval.v_string;
21874 }
21875 else
21876 {
21877 partial_T *ret_pt = rettv->vval.v_partial;
21878 int i;
21879
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021880 /* Partial: copy the function name, use selfdict and copy
21881 * args. Can't take over name or args, the partial might
21882 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021883 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021884 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021885 if (ret_pt->pt_argc > 0)
21886 {
21887 pt->pt_argv = (typval_T *)alloc(
21888 sizeof(typval_T) * ret_pt->pt_argc);
21889 if (pt->pt_argv == NULL)
21890 /* out of memory: drop the arguments */
21891 pt->pt_argc = 0;
21892 else
21893 {
21894 pt->pt_argc = ret_pt->pt_argc;
21895 for (i = 0; i < pt->pt_argc; i++)
21896 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21897 }
21898 }
21899 partial_unref(ret_pt);
21900 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021901 rettv->v_type = VAR_PARTIAL;
21902 rettv->vval.v_partial = pt;
21903 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021904 }
21905 }
21906
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021907 dict_unref(selfdict);
21908 return ret;
21909}
21910
21911/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021912 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021913 * value).
21914 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021915 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021916alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917{
Bram Moolenaar33570922005-01-25 22:26:29 +000021918 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021919}
21920
21921/*
21922 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021923 * The string "s" must have been allocated, it is consumed.
21924 * Return NULL for out of memory, the variable otherwise.
21925 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021926 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021927alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928{
Bram Moolenaar33570922005-01-25 22:26:29 +000021929 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021930
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021931 rettv = alloc_tv();
21932 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021933 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021934 rettv->v_type = VAR_STRING;
21935 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021936 }
21937 else
21938 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021939 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021940}
21941
21942/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021943 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021944 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021945 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021946free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021947{
21948 if (varp != NULL)
21949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 switch (varp->v_type)
21951 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021952 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021953 func_unref(varp->vval.v_string);
21954 /*FALLTHROUGH*/
21955 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021956 vim_free(varp->vval.v_string);
21957 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021958 case VAR_PARTIAL:
21959 partial_unref(varp->vval.v_partial);
21960 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021961 case VAR_LIST:
21962 list_unref(varp->vval.v_list);
21963 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021964 case VAR_DICT:
21965 dict_unref(varp->vval.v_dict);
21966 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021967 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021968#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021969 job_unref(varp->vval.v_job);
21970 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021971#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021972 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021973#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021974 channel_unref(varp->vval.v_channel);
21975 break;
21976#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021977 case VAR_NUMBER:
21978 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021979 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021980 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021981 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021982 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021983 vim_free(varp);
21984 }
21985}
21986
21987/*
21988 * Free the memory for a variable value and set the value to NULL or 0.
21989 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021990 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021991clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021992{
21993 if (varp != NULL)
21994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021995 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021996 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021997 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021998 func_unref(varp->vval.v_string);
21999 /*FALLTHROUGH*/
22000 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022001 vim_free(varp->vval.v_string);
22002 varp->vval.v_string = NULL;
22003 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022004 case VAR_PARTIAL:
22005 partial_unref(varp->vval.v_partial);
22006 varp->vval.v_partial = NULL;
22007 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022008 case VAR_LIST:
22009 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022010 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022011 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022012 case VAR_DICT:
22013 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022014 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022015 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022016 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022017 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022018 varp->vval.v_number = 0;
22019 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022021#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022022 varp->vval.v_float = 0.0;
22023 break;
22024#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022025 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022026#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022027 job_unref(varp->vval.v_job);
22028 varp->vval.v_job = NULL;
22029#endif
22030 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022031 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022032#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022033 channel_unref(varp->vval.v_channel);
22034 varp->vval.v_channel = NULL;
22035#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022036 case VAR_UNKNOWN:
22037 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022039 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 }
22041}
22042
22043/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022044 * Set the value of a variable to NULL without freeing items.
22045 */
22046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022047init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022048{
22049 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022050 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022051}
22052
22053/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022054 * Get the number value of a variable.
22055 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022056 * For incompatible types, return 0.
22057 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22058 * caller of incompatible types: it sets *denote to TRUE if "denote"
22059 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022060 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022061 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022062get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022063{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022064 int error = FALSE;
22065
22066 return get_tv_number_chk(varp, &error); /* return 0L on error */
22067}
22068
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022069 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022070get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022071{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022076 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022077 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022078 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022079#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022080 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022081 break;
22082#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022083 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022084 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022085 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022086 break;
22087 case VAR_STRING:
22088 if (varp->vval.v_string != NULL)
22089 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022090 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022091 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022092 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022093 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022094 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022095 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022096 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022097 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022098 case VAR_SPECIAL:
22099 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22100 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022101 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022102#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022103 EMSG(_("E910: Using a Job as a Number"));
22104 break;
22105#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022106 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022107#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022108 EMSG(_("E913: Using a Channel as a Number"));
22109 break;
22110#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022111 case VAR_UNKNOWN:
22112 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022113 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022114 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022115 if (denote == NULL) /* useful for values that must be unsigned */
22116 n = -1;
22117 else
22118 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022119 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022120}
22121
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022122#ifdef FEAT_FLOAT
22123 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022124get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022125{
22126 switch (varp->v_type)
22127 {
22128 case VAR_NUMBER:
22129 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022130 case VAR_FLOAT:
22131 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022132 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022133 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022134 EMSG(_("E891: Using a Funcref as a Float"));
22135 break;
22136 case VAR_STRING:
22137 EMSG(_("E892: Using a String as a Float"));
22138 break;
22139 case VAR_LIST:
22140 EMSG(_("E893: Using a List as a Float"));
22141 break;
22142 case VAR_DICT:
22143 EMSG(_("E894: Using a Dictionary as a Float"));
22144 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022145 case VAR_SPECIAL:
22146 EMSG(_("E907: Using a special value as a Float"));
22147 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022148 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022149# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022150 EMSG(_("E911: Using a Job as a Float"));
22151 break;
22152# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022153 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022154# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022155 EMSG(_("E914: Using a Channel as a Float"));
22156 break;
22157# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022158 case VAR_UNKNOWN:
22159 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022160 break;
22161 }
22162 return 0;
22163}
22164#endif
22165
Bram Moolenaar071d4272004-06-13 20:20:40 +000022166/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022167 * Get the lnum from the first argument.
22168 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022169 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022170 */
22171 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022172get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173{
Bram Moolenaar33570922005-01-25 22:26:29 +000022174 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022175 linenr_T lnum;
22176
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022177 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 if (lnum == 0) /* no valid number, try using line() */
22179 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022180 rettv.v_type = VAR_NUMBER;
22181 f_line(argvars, &rettv);
22182 lnum = rettv.vval.v_number;
22183 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022184 }
22185 return lnum;
22186}
22187
22188/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022189 * Get the lnum from the first argument.
22190 * Also accepts "$", then "buf" is used.
22191 * Returns 0 on error.
22192 */
22193 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022194get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022195{
22196 if (argvars[0].v_type == VAR_STRING
22197 && argvars[0].vval.v_string != NULL
22198 && argvars[0].vval.v_string[0] == '$'
22199 && buf != NULL)
22200 return buf->b_ml.ml_line_count;
22201 return get_tv_number_chk(&argvars[0], NULL);
22202}
22203
22204/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022205 * Get the string value of a variable.
22206 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022207 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22208 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022209 * If the String variable has never been set, return an empty string.
22210 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022211 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22212 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022213 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022214 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022215get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022216{
22217 static char_u mybuf[NUMBUFLEN];
22218
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022219 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022220}
22221
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022222 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022223get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022224{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022225 char_u *res = get_tv_string_buf_chk(varp, buf);
22226
22227 return res != NULL ? res : (char_u *)"";
22228}
22229
Bram Moolenaar7d647822014-04-05 21:28:56 +020022230/*
22231 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22232 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022233 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022234get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022235{
22236 static char_u mybuf[NUMBUFLEN];
22237
22238 return get_tv_string_buf_chk(varp, mybuf);
22239}
22240
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022241 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022242get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022243{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022244 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022245 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022246 case VAR_NUMBER:
22247 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22248 return buf;
22249 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022250 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022251 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022252 break;
22253 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022254 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022255 break;
22256 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022257 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022258 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022259 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022260#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022261 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022262 break;
22263#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022264 case VAR_STRING:
22265 if (varp->vval.v_string != NULL)
22266 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022267 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022268 case VAR_SPECIAL:
22269 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22270 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022271 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022273 {
22274 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022275 char *status;
22276
22277 if (job == NULL)
22278 return (char_u *)"no process";
22279 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022280 : job->jv_status == JOB_ENDED ? "dead"
22281 : "run";
22282# ifdef UNIX
22283 vim_snprintf((char *)buf, NUMBUFLEN,
22284 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022285# elif defined(WIN32)
22286 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022287 "process %ld %s",
22288 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022289 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022290# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022291 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022292 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22293# endif
22294 return buf;
22295 }
22296#endif
22297 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022298 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022299#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022300 {
22301 channel_T *channel = varp->vval.v_channel;
22302 char *status = channel_status(channel);
22303
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022304 if (channel == NULL)
22305 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22306 else
22307 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022308 "channel %d %s", channel->ch_id, status);
22309 return buf;
22310 }
22311#endif
22312 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022313 case VAR_UNKNOWN:
22314 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022315 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022317 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022318}
22319
22320/*
22321 * Find variable "name" in the list of variables.
22322 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022323 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022324 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022326 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022327 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022328find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022329{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022331 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022332
Bram Moolenaara7043832005-01-21 11:56:39 +000022333 ht = find_var_ht(name, &varname);
22334 if (htp != NULL)
22335 *htp = ht;
22336 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022338 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022339}
22340
22341/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022342 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022343 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022344 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022345 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022346find_var_in_ht(
22347 hashtab_T *ht,
22348 int htname,
22349 char_u *varname,
22350 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022351{
Bram Moolenaar33570922005-01-25 22:26:29 +000022352 hashitem_T *hi;
22353
22354 if (*varname == NUL)
22355 {
22356 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022357 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022359 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022360 case 'g': return &globvars_var;
22361 case 'v': return &vimvars_var;
22362 case 'b': return &curbuf->b_bufvar;
22363 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022364#ifdef FEAT_WINDOWS
22365 case 't': return &curtab->tp_winvar;
22366#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022367 case 'l': return current_funccal == NULL
22368 ? NULL : &current_funccal->l_vars_var;
22369 case 'a': return current_funccal == NULL
22370 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 }
22372 return NULL;
22373 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022374
22375 hi = hash_find(ht, varname);
22376 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022377 {
22378 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022379 * worked find the variable again. Don't auto-load a script if it was
22380 * loaded already, otherwise it would be loaded every time when
22381 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022382 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022383 {
22384 /* Note: script_autoload() may make "hi" invalid. It must either
22385 * be obtained again or not used. */
22386 if (!script_autoload(varname, FALSE) || aborting())
22387 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022388 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022389 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022390 if (HASHITEM_EMPTY(hi))
22391 return NULL;
22392 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022393 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022394}
22395
22396/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022397 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022398 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022399 * Set "varname" to the start of name without ':'.
22400 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022401 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022402find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022403{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022404 hashitem_T *hi;
22405
Bram Moolenaar73627d02015-08-11 15:46:09 +020022406 if (name[0] == NUL)
22407 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408 if (name[1] != ':')
22409 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022410 /* The name must not start with a colon or #. */
22411 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022412 return NULL;
22413 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022414
22415 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022416 hi = hash_find(&compat_hashtab, name);
22417 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022418 return &compat_hashtab;
22419
Bram Moolenaar071d4272004-06-13 20:20:40 +000022420 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022421 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022422 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 }
22424 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022425 if (*name == 'g') /* global variable */
22426 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022427 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22428 */
22429 if (vim_strchr(name + 2, ':') != NULL
22430 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022431 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022433 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022434 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022435 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022436#ifdef FEAT_WINDOWS
22437 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022438 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022439#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 if (*name == 'v') /* v: variable */
22441 return &vimvarht;
22442 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022443 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022444 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022445 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022446 if (*name == 's' /* script variable */
22447 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22448 return &SCRIPT_VARS(current_SID);
22449 return NULL;
22450}
22451
22452/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022453 * Get function call environment based on bactrace debug level
22454 */
22455 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022456get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022457{
22458 int i;
22459 funccall_T *funccal;
22460 funccall_T *temp_funccal;
22461
22462 funccal = current_funccal;
22463 if (debug_backtrace_level > 0)
22464 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022465 for (i = 0; i < debug_backtrace_level; i++)
22466 {
22467 temp_funccal = funccal->caller;
22468 if (temp_funccal)
22469 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022470 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022471 /* backtrace level overflow. reset to max */
22472 debug_backtrace_level = i;
22473 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022474 }
22475 return funccal;
22476}
22477
22478/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022480 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022481 * Returns NULL when it doesn't exist.
22482 */
22483 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022484get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485{
Bram Moolenaar33570922005-01-25 22:26:29 +000022486 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022488 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022489 if (v == NULL)
22490 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022491 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022492}
22493
22494/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022495 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022496 * sourcing this script and when executing functions defined in the script.
22497 */
22498 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022499new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022500{
Bram Moolenaara7043832005-01-21 11:56:39 +000022501 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022502 hashtab_T *ht;
22503 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022504
Bram Moolenaar071d4272004-06-13 20:20:40 +000022505 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22506 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022507 /* Re-allocating ga_data means that an ht_array pointing to
22508 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022509 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022510 for (i = 1; i <= ga_scripts.ga_len; ++i)
22511 {
22512 ht = &SCRIPT_VARS(i);
22513 if (ht->ht_mask == HT_INIT_SIZE - 1)
22514 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022515 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022516 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022517 }
22518
Bram Moolenaar071d4272004-06-13 20:20:40 +000022519 while (ga_scripts.ga_len < id)
22520 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022521 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022522 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022523 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022524 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022525 }
22526 }
22527}
22528
22529/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022530 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22531 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022532 */
22533 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022534init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022535{
Bram Moolenaar33570922005-01-25 22:26:29 +000022536 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022537 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022538 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022539 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022540 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022541 dict_var->di_tv.vval.v_dict = dict;
22542 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022543 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022544 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22545 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022546}
22547
22548/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022549 * Unreference a dictionary initialized by init_var_dict().
22550 */
22551 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022552unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022553{
22554 /* Now the dict needs to be freed if no one else is using it, go back to
22555 * normal reference counting. */
22556 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22557 dict_unref(dict);
22558}
22559
22560/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022561 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022562 * Frees all allocated variables and the value they contain.
22563 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022564 */
22565 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022566vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022567{
22568 vars_clear_ext(ht, TRUE);
22569}
22570
22571/*
22572 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22573 */
22574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022575vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022576{
Bram Moolenaara7043832005-01-21 11:56:39 +000022577 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022578 hashitem_T *hi;
22579 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022580
Bram Moolenaar33570922005-01-25 22:26:29 +000022581 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022582 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022583 for (hi = ht->ht_array; todo > 0; ++hi)
22584 {
22585 if (!HASHITEM_EMPTY(hi))
22586 {
22587 --todo;
22588
Bram Moolenaar33570922005-01-25 22:26:29 +000022589 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022590 * ht_array might change then. hash_clear() takes care of it
22591 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 v = HI2DI(hi);
22593 if (free_val)
22594 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022595 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022596 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022597 }
22598 }
22599 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022600 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022601}
22602
Bram Moolenaara7043832005-01-21 11:56:39 +000022603/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022604 * Delete a variable from hashtab "ht" at item "hi".
22605 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022606 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022608delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022609{
Bram Moolenaar33570922005-01-25 22:26:29 +000022610 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022611
22612 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022613 clear_tv(&di->di_tv);
22614 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022615}
22616
22617/*
22618 * List the value of one internal variable.
22619 */
22620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022621list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022622{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022623 char_u *tofree;
22624 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022625 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022626
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022627 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022628 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022629 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022630 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631}
22632
Bram Moolenaar071d4272004-06-13 20:20:40 +000022633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022634list_one_var_a(
22635 char_u *prefix,
22636 char_u *name,
22637 int type,
22638 char_u *string,
22639 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022640{
Bram Moolenaar31859182007-08-14 20:41:13 +000022641 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22642 msg_start();
22643 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022644 if (name != NULL) /* "a:" vars don't have a name stored */
22645 msg_puts(name);
22646 msg_putchar(' ');
22647 msg_advance(22);
22648 if (type == VAR_NUMBER)
22649 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022650 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022651 msg_putchar('*');
22652 else if (type == VAR_LIST)
22653 {
22654 msg_putchar('[');
22655 if (*string == '[')
22656 ++string;
22657 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022658 else if (type == VAR_DICT)
22659 {
22660 msg_putchar('{');
22661 if (*string == '{')
22662 ++string;
22663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022664 else
22665 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666
Bram Moolenaar071d4272004-06-13 20:20:40 +000022667 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022668
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022669 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022670 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022671 if (*first)
22672 {
22673 msg_clr_eos();
22674 *first = FALSE;
22675 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022676}
22677
22678/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022679 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022680 * If the variable already exists, the value is updated.
22681 * Otherwise the variable is created.
22682 */
22683 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022684set_var(
22685 char_u *name,
22686 typval_T *tv,
22687 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688{
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022691 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022692
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022693 ht = find_var_ht(name, &varname);
22694 if (ht == NULL || *varname == NUL)
22695 {
22696 EMSG2(_(e_illvar), name);
22697 return;
22698 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022699 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022700
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022701 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22702 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022703 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022704
Bram Moolenaar33570922005-01-25 22:26:29 +000022705 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022706 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022707 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022708 if (var_check_ro(v->di_flags, name, FALSE)
22709 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022710 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022711
22712 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022713 * Handle setting internal v: variables separately where needed to
22714 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022715 */
22716 if (ht == &vimvarht)
22717 {
22718 if (v->di_tv.v_type == VAR_STRING)
22719 {
22720 vim_free(v->di_tv.vval.v_string);
22721 if (copy || tv->v_type != VAR_STRING)
22722 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22723 else
22724 {
22725 /* Take over the string to avoid an extra alloc/free. */
22726 v->di_tv.vval.v_string = tv->vval.v_string;
22727 tv->vval.v_string = NULL;
22728 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022729 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022730 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022731 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022732 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022733 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022734 if (STRCMP(varname, "searchforward") == 0)
22735 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022736#ifdef FEAT_SEARCH_EXTRA
22737 else if (STRCMP(varname, "hlsearch") == 0)
22738 {
22739 no_hlsearch = !v->di_tv.vval.v_number;
22740 redraw_all_later(SOME_VALID);
22741 }
22742#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022743 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022744 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022745 else if (v->di_tv.v_type != tv->v_type)
22746 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022747 }
22748
22749 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022750 }
22751 else /* add a new variable */
22752 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022753 /* Can't add "v:" variable. */
22754 if (ht == &vimvarht)
22755 {
22756 EMSG2(_(e_illvar), name);
22757 return;
22758 }
22759
Bram Moolenaar92124a32005-06-17 22:03:40 +000022760 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022761 if (!valid_varname(varname))
22762 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022763
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022764 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22765 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022766 if (v == NULL)
22767 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022768 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022769 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022771 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022772 return;
22773 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022774 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022775 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022776
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022777 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022778 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022779 else
22780 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022781 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022782 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022783 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022784 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022785}
22786
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022787/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022788 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022789 * Also give an error message.
22790 */
22791 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022792var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022793{
22794 if (flags & DI_FLAGS_RO)
22795 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022796 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022797 return TRUE;
22798 }
22799 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22800 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022801 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022802 return TRUE;
22803 }
22804 return FALSE;
22805}
22806
22807/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022808 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22809 * Also give an error message.
22810 */
22811 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022812var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022813{
22814 if (flags & DI_FLAGS_FIX)
22815 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022816 EMSG2(_("E795: Cannot delete variable %s"),
22817 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022818 return TRUE;
22819 }
22820 return FALSE;
22821}
22822
22823/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022824 * Check if a funcref is assigned to a valid variable name.
22825 * Return TRUE and give an error if not.
22826 */
22827 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022828var_check_func_name(
22829 char_u *name, /* points to start of variable name */
22830 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022831{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022832 /* Allow for w: b: s: and t:. */
22833 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022834 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22835 ? name[2] : name[0]))
22836 {
22837 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22838 name);
22839 return TRUE;
22840 }
22841 /* Don't allow hiding a function. When "v" is not NULL we might be
22842 * assigning another function to the same var, the type is checked
22843 * below. */
22844 if (new_var && function_exists(name))
22845 {
22846 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22847 name);
22848 return TRUE;
22849 }
22850 return FALSE;
22851}
22852
22853/*
22854 * Check if a variable name is valid.
22855 * Return FALSE and give an error if not.
22856 */
22857 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022858valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022859{
22860 char_u *p;
22861
22862 for (p = varname; *p != NUL; ++p)
22863 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22864 && *p != AUTOLOAD_CHAR)
22865 {
22866 EMSG2(_(e_illvar), varname);
22867 return FALSE;
22868 }
22869 return TRUE;
22870}
22871
22872/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022873 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022874 * Also give an error message, using "name" or _("name") when use_gettext is
22875 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022876 */
22877 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022878tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022879{
22880 if (lock & VAR_LOCKED)
22881 {
22882 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022883 name == NULL ? (char_u *)_("Unknown")
22884 : use_gettext ? (char_u *)_(name)
22885 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022886 return TRUE;
22887 }
22888 if (lock & VAR_FIXED)
22889 {
22890 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022891 name == NULL ? (char_u *)_("Unknown")
22892 : use_gettext ? (char_u *)_(name)
22893 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022894 return TRUE;
22895 }
22896 return FALSE;
22897}
22898
22899/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022900 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022901 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022902 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022903 * It is OK for "from" and "to" to point to the same item. This is used to
22904 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022905 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022906 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022907copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022908{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022909 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022910 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022911 switch (from->v_type)
22912 {
22913 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022914 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022915 to->vval.v_number = from->vval.v_number;
22916 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022917 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022918#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022919 to->vval.v_float = from->vval.v_float;
22920 break;
22921#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022922 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022923#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022924 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022925 if (to->vval.v_job != NULL)
22926 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022927 break;
22928#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022929 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022930#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022931 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022932 if (to->vval.v_channel != NULL)
22933 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022934 break;
22935#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022936 case VAR_STRING:
22937 case VAR_FUNC:
22938 if (from->vval.v_string == NULL)
22939 to->vval.v_string = NULL;
22940 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022941 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022942 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022943 if (from->v_type == VAR_FUNC)
22944 func_ref(to->vval.v_string);
22945 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022946 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022947 case VAR_PARTIAL:
22948 if (from->vval.v_partial == NULL)
22949 to->vval.v_partial = NULL;
22950 else
22951 {
22952 to->vval.v_partial = from->vval.v_partial;
22953 ++to->vval.v_partial->pt_refcount;
22954 }
22955 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022956 case VAR_LIST:
22957 if (from->vval.v_list == NULL)
22958 to->vval.v_list = NULL;
22959 else
22960 {
22961 to->vval.v_list = from->vval.v_list;
22962 ++to->vval.v_list->lv_refcount;
22963 }
22964 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022965 case VAR_DICT:
22966 if (from->vval.v_dict == NULL)
22967 to->vval.v_dict = NULL;
22968 else
22969 {
22970 to->vval.v_dict = from->vval.v_dict;
22971 ++to->vval.v_dict->dv_refcount;
22972 }
22973 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022974 case VAR_UNKNOWN:
22975 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022976 break;
22977 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978}
22979
22980/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022981 * Make a copy of an item.
22982 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022983 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22984 * reference to an already copied list/dict can be used.
22985 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022986 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022987 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022988item_copy(
22989 typval_T *from,
22990 typval_T *to,
22991 int deep,
22992 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022993{
22994 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022995 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022996
Bram Moolenaar33570922005-01-25 22:26:29 +000022997 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022998 {
22999 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023000 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023001 }
23002 ++recurse;
23003
23004 switch (from->v_type)
23005 {
23006 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023007 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023008 case VAR_STRING:
23009 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023010 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023011 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023012 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023013 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023014 copy_tv(from, to);
23015 break;
23016 case VAR_LIST:
23017 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023018 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023019 if (from->vval.v_list == NULL)
23020 to->vval.v_list = NULL;
23021 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23022 {
23023 /* use the copy made earlier */
23024 to->vval.v_list = from->vval.v_list->lv_copylist;
23025 ++to->vval.v_list->lv_refcount;
23026 }
23027 else
23028 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23029 if (to->vval.v_list == NULL)
23030 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023031 break;
23032 case VAR_DICT:
23033 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023034 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023035 if (from->vval.v_dict == NULL)
23036 to->vval.v_dict = NULL;
23037 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23038 {
23039 /* use the copy made earlier */
23040 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23041 ++to->vval.v_dict->dv_refcount;
23042 }
23043 else
23044 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23045 if (to->vval.v_dict == NULL)
23046 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023047 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023048 case VAR_UNKNOWN:
23049 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023050 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023051 }
23052 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023053 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023054}
23055
23056/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023057 * ":echo expr1 ..." print each argument separated with a space, add a
23058 * newline at the end.
23059 * ":echon expr1 ..." print each argument plain.
23060 */
23061 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023062ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023063{
23064 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023065 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023066 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023067 char_u *p;
23068 int needclr = TRUE;
23069 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023070 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023071
23072 if (eap->skip)
23073 ++emsg_skip;
23074 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23075 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023076 /* If eval1() causes an error message the text from the command may
23077 * still need to be cleared. E.g., "echo 22,44". */
23078 need_clr_eos = needclr;
23079
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023081 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023082 {
23083 /*
23084 * Report the invalid expression unless the expression evaluation
23085 * has been cancelled due to an aborting error, an interrupt, or an
23086 * exception.
23087 */
23088 if (!aborting())
23089 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023090 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023091 break;
23092 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023093 need_clr_eos = FALSE;
23094
Bram Moolenaar071d4272004-06-13 20:20:40 +000023095 if (!eap->skip)
23096 {
23097 if (atstart)
23098 {
23099 atstart = FALSE;
23100 /* Call msg_start() after eval1(), evaluating the expression
23101 * may cause a message to appear. */
23102 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023103 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023104 /* Mark the saved text as finishing the line, so that what
23105 * follows is displayed on a new line when scrolling back
23106 * at the more prompt. */
23107 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023109 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023110 }
23111 else if (eap->cmdidx == CMD_echo)
23112 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023113 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023114 if (p != NULL)
23115 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023117 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023118 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023119 if (*p != TAB && needclr)
23120 {
23121 /* remove any text still there from the command */
23122 msg_clr_eos();
23123 needclr = FALSE;
23124 }
23125 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023126 }
23127 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023128 {
23129#ifdef FEAT_MBYTE
23130 if (has_mbyte)
23131 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023132 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023133
23134 (void)msg_outtrans_len_attr(p, i, echo_attr);
23135 p += i - 1;
23136 }
23137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023138#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023139 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23140 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023142 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023144 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023145 arg = skipwhite(arg);
23146 }
23147 eap->nextcmd = check_nextcmd(arg);
23148
23149 if (eap->skip)
23150 --emsg_skip;
23151 else
23152 {
23153 /* remove text that may still be there from the command */
23154 if (needclr)
23155 msg_clr_eos();
23156 if (eap->cmdidx == CMD_echo)
23157 msg_end();
23158 }
23159}
23160
23161/*
23162 * ":echohl {name}".
23163 */
23164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023165ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166{
23167 int id;
23168
23169 id = syn_name2id(eap->arg);
23170 if (id == 0)
23171 echo_attr = 0;
23172 else
23173 echo_attr = syn_id2attr(id);
23174}
23175
23176/*
23177 * ":execute expr1 ..." execute the result of an expression.
23178 * ":echomsg expr1 ..." Print a message
23179 * ":echoerr expr1 ..." Print an error
23180 * Each gets spaces around each argument and a newline at the end for
23181 * echo commands
23182 */
23183 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023184ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023185{
23186 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023187 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023188 int ret = OK;
23189 char_u *p;
23190 garray_T ga;
23191 int len;
23192 int save_did_emsg;
23193
23194 ga_init2(&ga, 1, 80);
23195
23196 if (eap->skip)
23197 ++emsg_skip;
23198 while (*arg != NUL && *arg != '|' && *arg != '\n')
23199 {
23200 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023201 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023202 {
23203 /*
23204 * Report the invalid expression unless the expression evaluation
23205 * has been cancelled due to an aborting error, an interrupt, or an
23206 * exception.
23207 */
23208 if (!aborting())
23209 EMSG2(_(e_invexpr2), p);
23210 ret = FAIL;
23211 break;
23212 }
23213
23214 if (!eap->skip)
23215 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023216 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023217 len = (int)STRLEN(p);
23218 if (ga_grow(&ga, len + 2) == FAIL)
23219 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023220 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023221 ret = FAIL;
23222 break;
23223 }
23224 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023226 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023227 ga.ga_len += len;
23228 }
23229
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023230 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023231 arg = skipwhite(arg);
23232 }
23233
23234 if (ret != FAIL && ga.ga_data != NULL)
23235 {
23236 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023237 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023238 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023239 out_flush();
23240 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023241 else if (eap->cmdidx == CMD_echoerr)
23242 {
23243 /* We don't want to abort following commands, restore did_emsg. */
23244 save_did_emsg = did_emsg;
23245 EMSG((char_u *)ga.ga_data);
23246 if (!force_abort)
23247 did_emsg = save_did_emsg;
23248 }
23249 else if (eap->cmdidx == CMD_execute)
23250 do_cmdline((char_u *)ga.ga_data,
23251 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23252 }
23253
23254 ga_clear(&ga);
23255
23256 if (eap->skip)
23257 --emsg_skip;
23258
23259 eap->nextcmd = check_nextcmd(arg);
23260}
23261
23262/*
23263 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23264 * "arg" points to the "&" or '+' when called, to "option" when returning.
23265 * Returns NULL when no option name found. Otherwise pointer to the char
23266 * after the option name.
23267 */
23268 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023269find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023270{
23271 char_u *p = *arg;
23272
23273 ++p;
23274 if (*p == 'g' && p[1] == ':')
23275 {
23276 *opt_flags = OPT_GLOBAL;
23277 p += 2;
23278 }
23279 else if (*p == 'l' && p[1] == ':')
23280 {
23281 *opt_flags = OPT_LOCAL;
23282 p += 2;
23283 }
23284 else
23285 *opt_flags = 0;
23286
23287 if (!ASCII_ISALPHA(*p))
23288 return NULL;
23289 *arg = p;
23290
23291 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23292 p += 4; /* termcap option */
23293 else
23294 while (ASCII_ISALPHA(*p))
23295 ++p;
23296 return p;
23297}
23298
23299/*
23300 * ":function"
23301 */
23302 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023303ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023304{
23305 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023306 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 int j;
23308 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023310 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023311 char_u *name = NULL;
23312 char_u *p;
23313 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023314 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023315 garray_T newargs;
23316 garray_T newlines;
23317 int varargs = FALSE;
23318 int mustend = FALSE;
23319 int flags = 0;
23320 ufunc_T *fp;
23321 int indent;
23322 int nesting;
23323 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023324 dictitem_T *v;
23325 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023326 static int func_nr = 0; /* number for nameless function */
23327 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023328 hashtab_T *ht;
23329 int todo;
23330 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023331 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023332
23333 /*
23334 * ":function" without argument: list functions.
23335 */
23336 if (ends_excmd(*eap->arg))
23337 {
23338 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023339 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023340 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023341 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023342 {
23343 if (!HASHITEM_EMPTY(hi))
23344 {
23345 --todo;
23346 fp = HI2UF(hi);
23347 if (!isdigit(*fp->uf_name))
23348 list_func_head(fp, FALSE);
23349 }
23350 }
23351 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023352 eap->nextcmd = check_nextcmd(eap->arg);
23353 return;
23354 }
23355
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023356 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023357 * ":function /pat": list functions matching pattern.
23358 */
23359 if (*eap->arg == '/')
23360 {
23361 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23362 if (!eap->skip)
23363 {
23364 regmatch_T regmatch;
23365
23366 c = *p;
23367 *p = NUL;
23368 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23369 *p = c;
23370 if (regmatch.regprog != NULL)
23371 {
23372 regmatch.rm_ic = p_ic;
23373
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023374 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023375 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23376 {
23377 if (!HASHITEM_EMPTY(hi))
23378 {
23379 --todo;
23380 fp = HI2UF(hi);
23381 if (!isdigit(*fp->uf_name)
23382 && vim_regexec(&regmatch, fp->uf_name, 0))
23383 list_func_head(fp, FALSE);
23384 }
23385 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023386 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023387 }
23388 }
23389 if (*p == '/')
23390 ++p;
23391 eap->nextcmd = check_nextcmd(p);
23392 return;
23393 }
23394
23395 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023396 * Get the function name. There are these situations:
23397 * func normal function name
23398 * "name" == func, "fudi.fd_dict" == NULL
23399 * dict.func new dictionary entry
23400 * "name" == NULL, "fudi.fd_dict" set,
23401 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23402 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023403 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023404 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23405 * dict.func existing dict entry that's not a Funcref
23406 * "name" == NULL, "fudi.fd_dict" set,
23407 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023408 * s:func script-local function name
23409 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023410 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023411 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023412 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023413 paren = (vim_strchr(p, '(') != NULL);
23414 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023415 {
23416 /*
23417 * Return on an invalid expression in braces, unless the expression
23418 * evaluation has been cancelled due to an aborting error, an
23419 * interrupt, or an exception.
23420 */
23421 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023422 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023423 if (!eap->skip && fudi.fd_newkey != NULL)
23424 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023427 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023428 else
23429 eap->skip = TRUE;
23430 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023431
Bram Moolenaar071d4272004-06-13 20:20:40 +000023432 /* An error in a function call during evaluation of an expression in magic
23433 * braces should not cause the function not to be defined. */
23434 saved_did_emsg = did_emsg;
23435 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023436
23437 /*
23438 * ":function func" with only function name: list function.
23439 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023440 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023441 {
23442 if (!ends_excmd(*skipwhite(p)))
23443 {
23444 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023445 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023446 }
23447 eap->nextcmd = check_nextcmd(p);
23448 if (eap->nextcmd != NULL)
23449 *p = NUL;
23450 if (!eap->skip && !got_int)
23451 {
23452 fp = find_func(name);
23453 if (fp != NULL)
23454 {
23455 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023456 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023457 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023458 if (FUNCLINE(fp, j) == NULL)
23459 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023460 msg_putchar('\n');
23461 msg_outnum((long)(j + 1));
23462 if (j < 9)
23463 msg_putchar(' ');
23464 if (j < 99)
23465 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023466 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023467 out_flush(); /* show a line at a time */
23468 ui_breakcheck();
23469 }
23470 if (!got_int)
23471 {
23472 msg_putchar('\n');
23473 msg_puts((char_u *)" endfunction");
23474 }
23475 }
23476 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023477 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023479 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023480 }
23481
23482 /*
23483 * ":function name(arg1, arg2)" Define function.
23484 */
23485 p = skipwhite(p);
23486 if (*p != '(')
23487 {
23488 if (!eap->skip)
23489 {
23490 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023491 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023492 }
23493 /* attempt to continue by skipping some text */
23494 if (vim_strchr(p, '(') != NULL)
23495 p = vim_strchr(p, '(');
23496 }
23497 p = skipwhite(p + 1);
23498
23499 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23500 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23501
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023502 if (!eap->skip)
23503 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023504 /* Check the name of the function. Unless it's a dictionary function
23505 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023506 if (name != NULL)
23507 arg = name;
23508 else
23509 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023510 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023511 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23512 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023513 {
23514 if (*arg == K_SPECIAL)
23515 j = 3;
23516 else
23517 j = 0;
23518 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23519 : eval_isnamec(arg[j])))
23520 ++j;
23521 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023522 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023523 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023524 /* Disallow using the g: dict. */
23525 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23526 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023527 }
23528
Bram Moolenaar071d4272004-06-13 20:20:40 +000023529 /*
23530 * Isolate the arguments: "arg1, arg2, ...)"
23531 */
23532 while (*p != ')')
23533 {
23534 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23535 {
23536 varargs = TRUE;
23537 p += 3;
23538 mustend = TRUE;
23539 }
23540 else
23541 {
23542 arg = p;
23543 while (ASCII_ISALNUM(*p) || *p == '_')
23544 ++p;
23545 if (arg == p || isdigit(*arg)
23546 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23547 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23548 {
23549 if (!eap->skip)
23550 EMSG2(_("E125: Illegal argument: %s"), arg);
23551 break;
23552 }
23553 if (ga_grow(&newargs, 1) == FAIL)
23554 goto erret;
23555 c = *p;
23556 *p = NUL;
23557 arg = vim_strsave(arg);
23558 if (arg == NULL)
23559 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023560
23561 /* Check for duplicate argument name. */
23562 for (i = 0; i < newargs.ga_len; ++i)
23563 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23564 {
23565 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023566 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023567 goto erret;
23568 }
23569
Bram Moolenaar071d4272004-06-13 20:20:40 +000023570 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23571 *p = c;
23572 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023573 if (*p == ',')
23574 ++p;
23575 else
23576 mustend = TRUE;
23577 }
23578 p = skipwhite(p);
23579 if (mustend && *p != ')')
23580 {
23581 if (!eap->skip)
23582 EMSG2(_(e_invarg2), eap->arg);
23583 break;
23584 }
23585 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023586 if (*p != ')')
23587 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023588 ++p; /* skip the ')' */
23589
Bram Moolenaare9a41262005-01-15 22:18:47 +000023590 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023591 for (;;)
23592 {
23593 p = skipwhite(p);
23594 if (STRNCMP(p, "range", 5) == 0)
23595 {
23596 flags |= FC_RANGE;
23597 p += 5;
23598 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023599 else if (STRNCMP(p, "dict", 4) == 0)
23600 {
23601 flags |= FC_DICT;
23602 p += 4;
23603 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023604 else if (STRNCMP(p, "abort", 5) == 0)
23605 {
23606 flags |= FC_ABORT;
23607 p += 5;
23608 }
23609 else
23610 break;
23611 }
23612
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023613 /* When there is a line break use what follows for the function body.
23614 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23615 if (*p == '\n')
23616 line_arg = p + 1;
23617 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 EMSG(_(e_trailing));
23619
23620 /*
23621 * Read the body of the function, until ":endfunction" is found.
23622 */
23623 if (KeyTyped)
23624 {
23625 /* Check if the function already exists, don't let the user type the
23626 * whole function before telling him it doesn't work! For a script we
23627 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023628 if (!eap->skip && !eap->forceit)
23629 {
23630 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23631 EMSG(_(e_funcdict));
23632 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023633 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023634 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023635
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023636 if (!eap->skip && did_emsg)
23637 goto erret;
23638
Bram Moolenaar071d4272004-06-13 20:20:40 +000023639 msg_putchar('\n'); /* don't overwrite the function name */
23640 cmdline_row = msg_row;
23641 }
23642
23643 indent = 2;
23644 nesting = 0;
23645 for (;;)
23646 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023647 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023648 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023649 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023650 saved_wait_return = FALSE;
23651 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023652 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023653 sourcing_lnum_off = sourcing_lnum;
23654
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023655 if (line_arg != NULL)
23656 {
23657 /* Use eap->arg, split up in parts by line breaks. */
23658 theline = line_arg;
23659 p = vim_strchr(theline, '\n');
23660 if (p == NULL)
23661 line_arg += STRLEN(line_arg);
23662 else
23663 {
23664 *p = NUL;
23665 line_arg = p + 1;
23666 }
23667 }
23668 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023669 theline = getcmdline(':', 0L, indent);
23670 else
23671 theline = eap->getline(':', eap->cookie, indent);
23672 if (KeyTyped)
23673 lines_left = Rows - 1;
23674 if (theline == NULL)
23675 {
23676 EMSG(_("E126: Missing :endfunction"));
23677 goto erret;
23678 }
23679
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023680 /* Detect line continuation: sourcing_lnum increased more than one. */
23681 if (sourcing_lnum > sourcing_lnum_off + 1)
23682 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23683 else
23684 sourcing_lnum_off = 0;
23685
Bram Moolenaar071d4272004-06-13 20:20:40 +000023686 if (skip_until != NULL)
23687 {
23688 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23689 * don't check for ":endfunc". */
23690 if (STRCMP(theline, skip_until) == 0)
23691 {
23692 vim_free(skip_until);
23693 skip_until = NULL;
23694 }
23695 }
23696 else
23697 {
23698 /* skip ':' and blanks*/
23699 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23700 ;
23701
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023702 /* Check for "endfunction". */
23703 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023704 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023705 if (line_arg == NULL)
23706 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023707 break;
23708 }
23709
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023710 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023711 * at "end". */
23712 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23713 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023714 else if (STRNCMP(p, "if", 2) == 0
23715 || STRNCMP(p, "wh", 2) == 0
23716 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023717 || STRNCMP(p, "try", 3) == 0)
23718 indent += 2;
23719
23720 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023721 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023722 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023723 if (*p == '!')
23724 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023725 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023726 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023727 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023728 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023729 ++nesting;
23730 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023731 }
23732 }
23733
23734 /* Check for ":append" or ":insert". */
23735 p = skip_range(p, NULL);
23736 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23737 || (p[0] == 'i'
23738 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23739 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23740 skip_until = vim_strsave((char_u *)".");
23741
23742 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23743 arg = skipwhite(skiptowhite(p));
23744 if (arg[0] == '<' && arg[1] =='<'
23745 && ((p[0] == 'p' && p[1] == 'y'
23746 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23747 || (p[0] == 'p' && p[1] == 'e'
23748 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23749 || (p[0] == 't' && p[1] == 'c'
23750 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023751 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23752 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023753 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23754 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023755 || (p[0] == 'm' && p[1] == 'z'
23756 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023757 ))
23758 {
23759 /* ":python <<" continues until a dot, like ":append" */
23760 p = skipwhite(arg + 2);
23761 if (*p == NUL)
23762 skip_until = vim_strsave((char_u *)".");
23763 else
23764 skip_until = vim_strsave(p);
23765 }
23766 }
23767
23768 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023769 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023770 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023771 if (line_arg == NULL)
23772 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023773 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023774 }
23775
23776 /* Copy the line to newly allocated memory. get_one_sourceline()
23777 * allocates 250 bytes per line, this saves 80% on average. The cost
23778 * is an extra alloc/free. */
23779 p = vim_strsave(theline);
23780 if (p != NULL)
23781 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023782 if (line_arg == NULL)
23783 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023784 theline = p;
23785 }
23786
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023787 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23788
23789 /* Add NULL lines for continuation lines, so that the line count is
23790 * equal to the index in the growarray. */
23791 while (sourcing_lnum_off-- > 0)
23792 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023793
23794 /* Check for end of eap->arg. */
23795 if (line_arg != NULL && *line_arg == NUL)
23796 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023797 }
23798
23799 /* Don't define the function when skipping commands or when an error was
23800 * detected. */
23801 if (eap->skip || did_emsg)
23802 goto erret;
23803
23804 /*
23805 * If there are no errors, add the function
23806 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023807 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023808 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023809 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023810 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023811 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023812 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023813 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023814 goto erret;
23815 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023816
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023817 fp = find_func(name);
23818 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023819 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023820 if (!eap->forceit)
23821 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023822 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023823 goto erret;
23824 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023825 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023826 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023827 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023828 name);
23829 goto erret;
23830 }
23831 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023832 ga_clear_strings(&(fp->uf_args));
23833 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023834 vim_free(name);
23835 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023836 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023837 }
23838 else
23839 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023840 char numbuf[20];
23841
23842 fp = NULL;
23843 if (fudi.fd_newkey == NULL && !eap->forceit)
23844 {
23845 EMSG(_(e_funcdict));
23846 goto erret;
23847 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023848 if (fudi.fd_di == NULL)
23849 {
23850 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023851 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023852 goto erret;
23853 }
23854 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023855 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023856 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023857
23858 /* Give the function a sequential number. Can only be used with a
23859 * Funcref! */
23860 vim_free(name);
23861 sprintf(numbuf, "%d", ++func_nr);
23862 name = vim_strsave((char_u *)numbuf);
23863 if (name == NULL)
23864 goto erret;
23865 }
23866
23867 if (fp == NULL)
23868 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023869 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023870 {
23871 int slen, plen;
23872 char_u *scriptname;
23873
23874 /* Check that the autoload name matches the script name. */
23875 j = FAIL;
23876 if (sourcing_name != NULL)
23877 {
23878 scriptname = autoload_name(name);
23879 if (scriptname != NULL)
23880 {
23881 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023882 plen = (int)STRLEN(p);
23883 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023884 if (slen > plen && fnamecmp(p,
23885 sourcing_name + slen - plen) == 0)
23886 j = OK;
23887 vim_free(scriptname);
23888 }
23889 }
23890 if (j == FAIL)
23891 {
23892 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23893 goto erret;
23894 }
23895 }
23896
23897 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023898 if (fp == NULL)
23899 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023900
23901 if (fudi.fd_dict != NULL)
23902 {
23903 if (fudi.fd_di == NULL)
23904 {
23905 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023906 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 if (fudi.fd_di == NULL)
23908 {
23909 vim_free(fp);
23910 goto erret;
23911 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023912 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23913 {
23914 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023915 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023916 goto erret;
23917 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023918 }
23919 else
23920 /* overwrite existing dict entry */
23921 clear_tv(&fudi.fd_di->di_tv);
23922 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023923 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023924 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023925 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023926
23927 /* behave like "dict" was used */
23928 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023929 }
23930
Bram Moolenaar071d4272004-06-13 20:20:40 +000023931 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023932 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023933 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23934 {
23935 vim_free(fp);
23936 goto erret;
23937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023938 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023939 fp->uf_args = newargs;
23940 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023941#ifdef FEAT_PROFILE
23942 fp->uf_tml_count = NULL;
23943 fp->uf_tml_total = NULL;
23944 fp->uf_tml_self = NULL;
23945 fp->uf_profiling = FALSE;
23946 if (prof_def_func())
23947 func_do_profile(fp);
23948#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023949 fp->uf_varargs = varargs;
23950 fp->uf_flags = flags;
23951 fp->uf_calls = 0;
23952 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023953 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954
23955erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023956 ga_clear_strings(&newargs);
23957 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023958ret_free:
23959 vim_free(skip_until);
23960 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023961 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023963 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023964}
23965
23966/*
23967 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023968 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023969 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023970 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023971 * TFN_INT: internal function name OK
23972 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023973 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023974 * Advances "pp" to just after the function name (if no error).
23975 */
23976 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023977trans_function_name(
23978 char_u **pp,
23979 int skip, /* only find the end, don't evaluate */
23980 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023981 funcdict_T *fdp, /* return: info about dictionary used */
23982 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023984 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023985 char_u *start;
23986 char_u *end;
23987 int lead;
23988 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023989 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023990 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023991
23992 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023993 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023994 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023995
23996 /* Check for hard coded <SNR>: already translated function ID (from a user
23997 * command). */
23998 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23999 && (*pp)[2] == (int)KE_SNR)
24000 {
24001 *pp += 3;
24002 len = get_id_len(pp) + 3;
24003 return vim_strnsave(start, len);
24004 }
24005
24006 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24007 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024009 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024010 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024011
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024012 /* Note that TFN_ flags use the same values as GLV_ flags. */
24013 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024014 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024015 if (end == start)
24016 {
24017 if (!skip)
24018 EMSG(_("E129: Function name required"));
24019 goto theend;
24020 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024021 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024022 {
24023 /*
24024 * Report an invalid expression in braces, unless the expression
24025 * evaluation has been cancelled due to an aborting error, an
24026 * interrupt, or an exception.
24027 */
24028 if (!aborting())
24029 {
24030 if (end != NULL)
24031 EMSG2(_(e_invarg2), start);
24032 }
24033 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024034 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024035 goto theend;
24036 }
24037
24038 if (lv.ll_tv != NULL)
24039 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024040 if (fdp != NULL)
24041 {
24042 fdp->fd_dict = lv.ll_dict;
24043 fdp->fd_newkey = lv.ll_newkey;
24044 lv.ll_newkey = NULL;
24045 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024046 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024047 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24048 {
24049 name = vim_strsave(lv.ll_tv->vval.v_string);
24050 *pp = end;
24051 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024052 else if (lv.ll_tv->v_type == VAR_PARTIAL
24053 && lv.ll_tv->vval.v_partial != NULL)
24054 {
24055 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24056 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024057 if (partial != NULL)
24058 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024059 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024060 else
24061 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024062 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24063 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024064 EMSG(_(e_funcref));
24065 else
24066 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024067 name = NULL;
24068 }
24069 goto theend;
24070 }
24071
24072 if (lv.ll_name == NULL)
24073 {
24074 /* Error found, but continue after the function name. */
24075 *pp = end;
24076 goto theend;
24077 }
24078
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024079 /* Check if the name is a Funcref. If so, use the value. */
24080 if (lv.ll_exp_name != NULL)
24081 {
24082 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024083 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024084 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024085 if (name == lv.ll_exp_name)
24086 name = NULL;
24087 }
24088 else
24089 {
24090 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024091 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024092 if (name == *pp)
24093 name = NULL;
24094 }
24095 if (name != NULL)
24096 {
24097 name = vim_strsave(name);
24098 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024099 if (STRNCMP(name, "<SNR>", 5) == 0)
24100 {
24101 /* Change "<SNR>" to the byte sequence. */
24102 name[0] = K_SPECIAL;
24103 name[1] = KS_EXTRA;
24104 name[2] = (int)KE_SNR;
24105 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24106 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024107 goto theend;
24108 }
24109
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024110 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024111 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024112 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024113 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24114 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24115 {
24116 /* When there was "s:" already or the name expanded to get a
24117 * leading "s:" then remove it. */
24118 lv.ll_name += 2;
24119 len -= 2;
24120 lead = 2;
24121 }
24122 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024123 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024124 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024125 /* skip over "s:" and "g:" */
24126 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024127 lv.ll_name += 2;
24128 len = (int)(end - lv.ll_name);
24129 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024130
24131 /*
24132 * Copy the function name to allocated memory.
24133 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24134 * Accept <SNR>123_name() outside a script.
24135 */
24136 if (skip)
24137 lead = 0; /* do nothing */
24138 else if (lead > 0)
24139 {
24140 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024141 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24142 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024143 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024144 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024145 if (current_SID <= 0)
24146 {
24147 EMSG(_(e_usingsid));
24148 goto theend;
24149 }
24150 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24151 lead += (int)STRLEN(sid_buf);
24152 }
24153 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024154 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024155 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024156 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024157 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024158 goto theend;
24159 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024160 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024161 {
24162 char_u *cp = vim_strchr(lv.ll_name, ':');
24163
24164 if (cp != NULL && cp < end)
24165 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024166 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024167 goto theend;
24168 }
24169 }
24170
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024171 name = alloc((unsigned)(len + lead + 1));
24172 if (name != NULL)
24173 {
24174 if (lead > 0)
24175 {
24176 name[0] = K_SPECIAL;
24177 name[1] = KS_EXTRA;
24178 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024179 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024180 STRCPY(name + 3, sid_buf);
24181 }
24182 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024183 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024184 }
24185 *pp = end;
24186
24187theend:
24188 clear_lval(&lv);
24189 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024190}
24191
24192/*
24193 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24194 * Return 2 if "p" starts with "s:".
24195 * Return 0 otherwise.
24196 */
24197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024198eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024199{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024200 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24201 * the standard library function. */
24202 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24203 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024204 return 5;
24205 if (p[0] == 's' && p[1] == ':')
24206 return 2;
24207 return 0;
24208}
24209
24210/*
24211 * Return TRUE if "p" starts with "<SID>" or "s:".
24212 * Only works if eval_fname_script() returned non-zero for "p"!
24213 */
24214 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024215eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024216{
24217 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24218}
24219
24220/*
24221 * List the head of the function: "name(arg1, arg2)".
24222 */
24223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024224list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024225{
24226 int j;
24227
24228 msg_start();
24229 if (indent)
24230 MSG_PUTS(" ");
24231 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024232 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024233 {
24234 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024235 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024236 }
24237 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024238 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024240 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024241 {
24242 if (j)
24243 MSG_PUTS(", ");
24244 msg_puts(FUNCARG(fp, j));
24245 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024246 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024247 {
24248 if (j)
24249 MSG_PUTS(", ");
24250 MSG_PUTS("...");
24251 }
24252 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024253 if (fp->uf_flags & FC_ABORT)
24254 MSG_PUTS(" abort");
24255 if (fp->uf_flags & FC_RANGE)
24256 MSG_PUTS(" range");
24257 if (fp->uf_flags & FC_DICT)
24258 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024259 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024260 if (p_verbose > 0)
24261 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024262}
24263
24264/*
24265 * Find a function by name, return pointer to it in ufuncs.
24266 * Return NULL for unknown function.
24267 */
24268 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024269find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024272
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024273 hi = hash_find(&func_hashtab, name);
24274 if (!HASHITEM_EMPTY(hi))
24275 return HI2UF(hi);
24276 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024277}
24278
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024279#if defined(EXITFREE) || defined(PROTO)
24280 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024281free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024282{
24283 hashitem_T *hi;
24284
24285 /* Need to start all over every time, because func_free() may change the
24286 * hash table. */
24287 while (func_hashtab.ht_used > 0)
24288 for (hi = func_hashtab.ht_array; ; ++hi)
24289 if (!HASHITEM_EMPTY(hi))
24290 {
24291 func_free(HI2UF(hi));
24292 break;
24293 }
24294}
24295#endif
24296
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024297 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024298translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024299{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024300 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024301 return find_internal_func(name) >= 0;
24302 return find_func(name) != NULL;
24303}
24304
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024305/*
24306 * Return TRUE if a function "name" exists.
24307 */
24308 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024309function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024310{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024311 char_u *nm = name;
24312 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024313 int n = FALSE;
24314
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024315 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024316 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024317 nm = skipwhite(nm);
24318
24319 /* Only accept "funcname", "funcname ", "funcname (..." and
24320 * "funcname(...", not "funcname!...". */
24321 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024322 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024323 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024324 return n;
24325}
24326
Bram Moolenaara1544c02013-05-30 12:35:52 +020024327 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024328get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024329{
24330 char_u *nm = name;
24331 char_u *p;
24332
Bram Moolenaar65639032016-03-16 21:40:30 +010024333 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024334
24335 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024336 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024337 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024338
Bram Moolenaara1544c02013-05-30 12:35:52 +020024339 vim_free(p);
24340 return NULL;
24341}
24342
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024343/*
24344 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024345 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24346 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024347 */
24348 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024349builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024350{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024351 char_u *p;
24352
24353 if (!ASCII_ISLOWER(name[0]))
24354 return FALSE;
24355 p = vim_strchr(name, AUTOLOAD_CHAR);
24356 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024357}
24358
Bram Moolenaar05159a02005-02-26 23:04:13 +000024359#if defined(FEAT_PROFILE) || defined(PROTO)
24360/*
24361 * Start profiling function "fp".
24362 */
24363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024364func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024365{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024366 int len = fp->uf_lines.ga_len;
24367
24368 if (len == 0)
24369 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024370 fp->uf_tm_count = 0;
24371 profile_zero(&fp->uf_tm_self);
24372 profile_zero(&fp->uf_tm_total);
24373 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024374 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024375 if (fp->uf_tml_total == NULL)
24376 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024377 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024378 if (fp->uf_tml_self == NULL)
24379 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024380 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024381 fp->uf_tml_idx = -1;
24382 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24383 || fp->uf_tml_self == NULL)
24384 return; /* out of memory */
24385
24386 fp->uf_profiling = TRUE;
24387}
24388
24389/*
24390 * Dump the profiling results for all functions in file "fd".
24391 */
24392 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024393func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024394{
24395 hashitem_T *hi;
24396 int todo;
24397 ufunc_T *fp;
24398 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024399 ufunc_T **sorttab;
24400 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024401
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024402 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024403 if (todo == 0)
24404 return; /* nothing to dump */
24405
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024406 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024407
Bram Moolenaar05159a02005-02-26 23:04:13 +000024408 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24409 {
24410 if (!HASHITEM_EMPTY(hi))
24411 {
24412 --todo;
24413 fp = HI2UF(hi);
24414 if (fp->uf_profiling)
24415 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024416 if (sorttab != NULL)
24417 sorttab[st_len++] = fp;
24418
Bram Moolenaar05159a02005-02-26 23:04:13 +000024419 if (fp->uf_name[0] == K_SPECIAL)
24420 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24421 else
24422 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24423 if (fp->uf_tm_count == 1)
24424 fprintf(fd, "Called 1 time\n");
24425 else
24426 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24427 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24428 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24429 fprintf(fd, "\n");
24430 fprintf(fd, "count total (s) self (s)\n");
24431
24432 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24433 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024434 if (FUNCLINE(fp, i) == NULL)
24435 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024436 prof_func_line(fd, fp->uf_tml_count[i],
24437 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024438 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24439 }
24440 fprintf(fd, "\n");
24441 }
24442 }
24443 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024444
24445 if (sorttab != NULL && st_len > 0)
24446 {
24447 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24448 prof_total_cmp);
24449 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24450 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24451 prof_self_cmp);
24452 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24453 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024454
24455 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024456}
Bram Moolenaar73830342005-02-28 22:48:19 +000024457
24458 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024459prof_sort_list(
24460 FILE *fd,
24461 ufunc_T **sorttab,
24462 int st_len,
24463 char *title,
24464 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024465{
24466 int i;
24467 ufunc_T *fp;
24468
24469 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24470 fprintf(fd, "count total (s) self (s) function\n");
24471 for (i = 0; i < 20 && i < st_len; ++i)
24472 {
24473 fp = sorttab[i];
24474 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24475 prefer_self);
24476 if (fp->uf_name[0] == K_SPECIAL)
24477 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24478 else
24479 fprintf(fd, " %s()\n", fp->uf_name);
24480 }
24481 fprintf(fd, "\n");
24482}
24483
24484/*
24485 * Print the count and times for one function or function line.
24486 */
24487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024488prof_func_line(
24489 FILE *fd,
24490 int count,
24491 proftime_T *total,
24492 proftime_T *self,
24493 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024494{
24495 if (count > 0)
24496 {
24497 fprintf(fd, "%5d ", count);
24498 if (prefer_self && profile_equal(total, self))
24499 fprintf(fd, " ");
24500 else
24501 fprintf(fd, "%s ", profile_msg(total));
24502 if (!prefer_self && profile_equal(total, self))
24503 fprintf(fd, " ");
24504 else
24505 fprintf(fd, "%s ", profile_msg(self));
24506 }
24507 else
24508 fprintf(fd, " ");
24509}
24510
24511/*
24512 * Compare function for total time sorting.
24513 */
24514 static int
24515#ifdef __BORLANDC__
24516_RTLENTRYF
24517#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024518prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024519{
24520 ufunc_T *p1, *p2;
24521
24522 p1 = *(ufunc_T **)s1;
24523 p2 = *(ufunc_T **)s2;
24524 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24525}
24526
24527/*
24528 * Compare function for self time sorting.
24529 */
24530 static int
24531#ifdef __BORLANDC__
24532_RTLENTRYF
24533#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024534prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024535{
24536 ufunc_T *p1, *p2;
24537
24538 p1 = *(ufunc_T **)s1;
24539 p2 = *(ufunc_T **)s2;
24540 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24541}
24542
Bram Moolenaar05159a02005-02-26 23:04:13 +000024543#endif
24544
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024545/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024546 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024547 * Return TRUE if a package was loaded.
24548 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024549 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024550script_autoload(
24551 char_u *name,
24552 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024553{
24554 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024555 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024556 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024557 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024558
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024559 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024560 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024561 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024562 return FALSE;
24563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024564 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024565
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024566 /* Find the name in the list of previously loaded package names. Skip
24567 * "autoload/", it's always the same. */
24568 for (i = 0; i < ga_loaded.ga_len; ++i)
24569 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24570 break;
24571 if (!reload && i < ga_loaded.ga_len)
24572 ret = FALSE; /* was loaded already */
24573 else
24574 {
24575 /* Remember the name if it wasn't loaded already. */
24576 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24577 {
24578 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24579 tofree = NULL;
24580 }
24581
24582 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024583 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024584 ret = TRUE;
24585 }
24586
24587 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024588 return ret;
24589}
24590
24591/*
24592 * Return the autoload script name for a function or variable name.
24593 * Returns NULL when out of memory.
24594 */
24595 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024596autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024597{
24598 char_u *p;
24599 char_u *scriptname;
24600
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024601 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024602 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24603 if (scriptname == NULL)
24604 return FALSE;
24605 STRCPY(scriptname, "autoload/");
24606 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024607 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024608 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024609 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024610 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024611 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024612}
24613
Bram Moolenaar071d4272004-06-13 20:20:40 +000024614#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24615
24616/*
24617 * Function given to ExpandGeneric() to obtain the list of user defined
24618 * function names.
24619 */
24620 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024621get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024622{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024623 static long_u done;
24624 static hashitem_T *hi;
24625 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626
24627 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024628 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024629 done = 0;
24630 hi = func_hashtab.ht_array;
24631 }
24632 if (done < func_hashtab.ht_used)
24633 {
24634 if (done++ > 0)
24635 ++hi;
24636 while (HASHITEM_EMPTY(hi))
24637 ++hi;
24638 fp = HI2UF(hi);
24639
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024640 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024641 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024642
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024643 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24644 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024645
24646 cat_func_name(IObuff, fp);
24647 if (xp->xp_context != EXPAND_USER_FUNC)
24648 {
24649 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024650 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 STRCAT(IObuff, ")");
24652 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024653 return IObuff;
24654 }
24655 return NULL;
24656}
24657
24658#endif /* FEAT_CMDL_COMPL */
24659
24660/*
24661 * Copy the function name of "fp" to buffer "buf".
24662 * "buf" must be able to hold the function name plus three bytes.
24663 * Takes care of script-local function names.
24664 */
24665 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024666cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024668 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024669 {
24670 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024671 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024672 }
24673 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024674 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675}
24676
24677/*
24678 * ":delfunction {name}"
24679 */
24680 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024681ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024683 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024684 char_u *p;
24685 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024686 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024687
24688 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024689 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024690 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024691 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024692 {
24693 if (fudi.fd_dict != NULL && !eap->skip)
24694 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024696 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024697 if (!ends_excmd(*skipwhite(p)))
24698 {
24699 vim_free(name);
24700 EMSG(_(e_trailing));
24701 return;
24702 }
24703 eap->nextcmd = check_nextcmd(p);
24704 if (eap->nextcmd != NULL)
24705 *p = NUL;
24706
24707 if (!eap->skip)
24708 fp = find_func(name);
24709 vim_free(name);
24710
24711 if (!eap->skip)
24712 {
24713 if (fp == NULL)
24714 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024715 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024716 return;
24717 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024718 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024719 {
24720 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24721 return;
24722 }
24723
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024725 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024726 /* Delete the dict item that refers to the function, it will
24727 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024728 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024729 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024730 else
24731 func_free(fp);
24732 }
24733}
24734
24735/*
24736 * Free a function and remove it from the list of functions.
24737 */
24738 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024739func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024740{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024741 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024742
24743 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024744 ga_clear_strings(&(fp->uf_args));
24745 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024746#ifdef FEAT_PROFILE
24747 vim_free(fp->uf_tml_count);
24748 vim_free(fp->uf_tml_total);
24749 vim_free(fp->uf_tml_self);
24750#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024751
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024752 /* remove the function from the function hashtable */
24753 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24754 if (HASHITEM_EMPTY(hi))
24755 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024756 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024757 hash_remove(&func_hashtab, hi);
24758
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024759 vim_free(fp);
24760}
24761
24762/*
24763 * Unreference a Function: decrement the reference count and free it when it
24764 * becomes zero. Only for numbered functions.
24765 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024766 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024767func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024768{
24769 ufunc_T *fp;
24770
24771 if (name != NULL && isdigit(*name))
24772 {
24773 fp = find_func(name);
24774 if (fp == NULL)
24775 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024777 {
24778 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024779 * when "uf_calls" becomes zero. */
24780 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024781 func_free(fp);
24782 }
24783 }
24784}
24785
24786/*
24787 * Count a reference to a Function.
24788 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024789 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024790func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024791{
24792 ufunc_T *fp;
24793
24794 if (name != NULL && isdigit(*name))
24795 {
24796 fp = find_func(name);
24797 if (fp == NULL)
24798 EMSG2(_(e_intern2), "func_ref()");
24799 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024800 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024801 }
24802}
24803
24804/*
24805 * Call a user function.
24806 */
24807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024808call_user_func(
24809 ufunc_T *fp, /* pointer to function */
24810 int argcount, /* nr of args */
24811 typval_T *argvars, /* arguments */
24812 typval_T *rettv, /* return value */
24813 linenr_T firstline, /* first line of range */
24814 linenr_T lastline, /* last line of range */
24815 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024816{
Bram Moolenaar33570922005-01-25 22:26:29 +000024817 char_u *save_sourcing_name;
24818 linenr_T save_sourcing_lnum;
24819 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024820 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024821 int save_did_emsg;
24822 static int depth = 0;
24823 dictitem_T *v;
24824 int fixvar_idx = 0; /* index in fixvar[] */
24825 int i;
24826 int ai;
24827 char_u numbuf[NUMBUFLEN];
24828 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024829 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024830#ifdef FEAT_PROFILE
24831 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024832 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024833#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024834
24835 /* If depth of calling is getting too high, don't execute the function */
24836 if (depth >= p_mfd)
24837 {
24838 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024839 rettv->v_type = VAR_NUMBER;
24840 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024841 return;
24842 }
24843 ++depth;
24844
24845 line_breakcheck(); /* check for CTRL-C hit */
24846
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024847 fc = (funccall_T *)alloc(sizeof(funccall_T));
24848 fc->caller = current_funccal;
24849 current_funccal = fc;
24850 fc->func = fp;
24851 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024852 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024853 fc->linenr = 0;
24854 fc->returned = FALSE;
24855 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024856 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024857 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24858 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024859
Bram Moolenaar33570922005-01-25 22:26:29 +000024860 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024861 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024862 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24863 * each argument variable and saves a lot of time.
24864 */
24865 /*
24866 * Init l: variables.
24867 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024868 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024869 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024870 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024871 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24872 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024873 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024874 name = v->di_key;
24875 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024876 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024877 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024879 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024880 v->di_tv.vval.v_dict = selfdict;
24881 ++selfdict->dv_refcount;
24882 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024883
Bram Moolenaar33570922005-01-25 22:26:29 +000024884 /*
24885 * Init a: variables.
24886 * Set a:0 to "argcount".
24887 * Set a:000 to a list with room for the "..." arguments.
24888 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024889 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024890 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024891 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024892 /* Use "name" to avoid a warning from some compiler that checks the
24893 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024894 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024895 name = v->di_key;
24896 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024897 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024898 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024899 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024900 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024901 v->di_tv.vval.v_list = &fc->l_varlist;
24902 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24903 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24904 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024905
24906 /*
24907 * Set a:firstline to "firstline" and a:lastline to "lastline".
24908 * Set a:name to named arguments.
24909 * Set a:N to the "..." arguments.
24910 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024911 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024912 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024913 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024914 (varnumber_T)lastline);
24915 for (i = 0; i < argcount; ++i)
24916 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024917 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024918 if (ai < 0)
24919 /* named argument a:name */
24920 name = FUNCARG(fp, i);
24921 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024922 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024923 /* "..." argument a:1, a:2, etc. */
24924 sprintf((char *)numbuf, "%d", ai + 1);
24925 name = numbuf;
24926 }
24927 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24928 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024929 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024930 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24931 }
24932 else
24933 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024934 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24935 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024936 if (v == NULL)
24937 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024938 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024939 }
24940 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024941 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024942
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024943 /* Note: the values are copied directly to avoid alloc/free.
24944 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024945 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024946 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024947
24948 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24949 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024950 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24951 fc->l_listitems[ai].li_tv = argvars[i];
24952 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024953 }
24954 }
24955
Bram Moolenaar071d4272004-06-13 20:20:40 +000024956 /* Don't redraw while executing the function. */
24957 ++RedrawingDisabled;
24958 save_sourcing_name = sourcing_name;
24959 save_sourcing_lnum = sourcing_lnum;
24960 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024961 /* need space for function name + ("function " + 3) or "[number]" */
24962 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24963 + STRLEN(fp->uf_name) + 20;
24964 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 if (sourcing_name != NULL)
24966 {
24967 if (save_sourcing_name != NULL
24968 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024969 sprintf((char *)sourcing_name, "%s[%d]..",
24970 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024971 else
24972 STRCPY(sourcing_name, "function ");
24973 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24974
24975 if (p_verbose >= 12)
24976 {
24977 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024978 verbose_enter_scroll();
24979
Bram Moolenaar555b2802005-05-19 21:08:39 +000024980 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 if (p_verbose >= 14)
24982 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024983 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024984 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024985 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024986 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987
24988 msg_puts((char_u *)"(");
24989 for (i = 0; i < argcount; ++i)
24990 {
24991 if (i > 0)
24992 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024993 if (argvars[i].v_type == VAR_NUMBER)
24994 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995 else
24996 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024997 /* Do not want errors such as E724 here. */
24998 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024999 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025000 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025001 if (s != NULL)
25002 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025003 if (vim_strsize(s) > MSG_BUF_CLEN)
25004 {
25005 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25006 s = buf;
25007 }
25008 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025009 vim_free(tofree);
25010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025011 }
25012 }
25013 msg_puts((char_u *)")");
25014 }
25015 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025016
25017 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025018 --no_wait_return;
25019 }
25020 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025021#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025022 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025023 {
25024 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25025 func_do_profile(fp);
25026 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025027 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025028 {
25029 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025030 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025031 profile_zero(&fp->uf_tm_children);
25032 }
25033 script_prof_save(&wait_start);
25034 }
25035#endif
25036
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025038 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025039 save_did_emsg = did_emsg;
25040 did_emsg = FALSE;
25041
25042 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025043 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025044 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25045
25046 --RedrawingDisabled;
25047
25048 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025049 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025050 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025051 clear_tv(rettv);
25052 rettv->v_type = VAR_NUMBER;
25053 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025054 }
25055
Bram Moolenaar05159a02005-02-26 23:04:13 +000025056#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025057 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025058 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025059 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025060 profile_end(&call_start);
25061 profile_sub_wait(&wait_start, &call_start);
25062 profile_add(&fp->uf_tm_total, &call_start);
25063 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025064 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025065 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025066 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25067 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025068 }
25069 }
25070#endif
25071
Bram Moolenaar071d4272004-06-13 20:20:40 +000025072 /* when being verbose, mention the return value */
25073 if (p_verbose >= 12)
25074 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025076 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025077
Bram Moolenaar071d4272004-06-13 20:20:40 +000025078 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025079 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025080 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025081 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025082 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025083 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025084 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025085 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025086 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025087 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025088 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025089
Bram Moolenaar555b2802005-05-19 21:08:39 +000025090 /* The value may be very long. Skip the middle part, so that we
25091 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025092 * truncate it at the end. Don't want errors such as E724 here. */
25093 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025094 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025095 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025096 if (s != NULL)
25097 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025098 if (vim_strsize(s) > MSG_BUF_CLEN)
25099 {
25100 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25101 s = buf;
25102 }
25103 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025104 vim_free(tofree);
25105 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025106 }
25107 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025108
25109 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025110 --no_wait_return;
25111 }
25112
25113 vim_free(sourcing_name);
25114 sourcing_name = save_sourcing_name;
25115 sourcing_lnum = save_sourcing_lnum;
25116 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025117#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025118 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025119 script_prof_restore(&wait_start);
25120#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025121
25122 if (p_verbose >= 12 && sourcing_name != NULL)
25123 {
25124 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025125 verbose_enter_scroll();
25126
Bram Moolenaar555b2802005-05-19 21:08:39 +000025127 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025128 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025129
25130 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025131 --no_wait_return;
25132 }
25133
25134 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025135 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025136 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025137
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025138 /* If the a:000 list and the l: and a: dicts are not referenced we can
25139 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025140 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25141 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25142 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25143 {
25144 free_funccal(fc, FALSE);
25145 }
25146 else
25147 {
25148 hashitem_T *hi;
25149 listitem_T *li;
25150 int todo;
25151
25152 /* "fc" is still in use. This can happen when returning "a:000" or
25153 * assigning "l:" to a global variable.
25154 * Link "fc" in the list for garbage collection later. */
25155 fc->caller = previous_funccal;
25156 previous_funccal = fc;
25157
25158 /* Make a copy of the a: variables, since we didn't do that above. */
25159 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25160 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25161 {
25162 if (!HASHITEM_EMPTY(hi))
25163 {
25164 --todo;
25165 v = HI2DI(hi);
25166 copy_tv(&v->di_tv, &v->di_tv);
25167 }
25168 }
25169
25170 /* Make a copy of the a:000 items, since we didn't do that above. */
25171 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25172 copy_tv(&li->li_tv, &li->li_tv);
25173 }
25174}
25175
25176/*
25177 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025178 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025179 */
25180 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025181can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025182{
25183 return (fc->l_varlist.lv_copyID != copyID
25184 && fc->l_vars.dv_copyID != copyID
25185 && fc->l_avars.dv_copyID != copyID);
25186}
25187
25188/*
25189 * Free "fc" and what it contains.
25190 */
25191 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025192free_funccal(
25193 funccall_T *fc,
25194 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025195{
25196 listitem_T *li;
25197
25198 /* The a: variables typevals may not have been allocated, only free the
25199 * allocated variables. */
25200 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25201
25202 /* free all l: variables */
25203 vars_clear(&fc->l_vars.dv_hashtab);
25204
25205 /* Free the a:000 variables if they were allocated. */
25206 if (free_val)
25207 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25208 clear_tv(&li->li_tv);
25209
25210 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025211}
25212
25213/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025214 * Add a number variable "name" to dict "dp" with value "nr".
25215 */
25216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025217add_nr_var(
25218 dict_T *dp,
25219 dictitem_T *v,
25220 char *name,
25221 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025222{
25223 STRCPY(v->di_key, name);
25224 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25225 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25226 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025227 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025228 v->di_tv.vval.v_number = nr;
25229}
25230
25231/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232 * ":return [expr]"
25233 */
25234 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025235ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025236{
25237 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025238 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025239 int returning = FALSE;
25240
25241 if (current_funccal == NULL)
25242 {
25243 EMSG(_("E133: :return not inside a function"));
25244 return;
25245 }
25246
25247 if (eap->skip)
25248 ++emsg_skip;
25249
25250 eap->nextcmd = NULL;
25251 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025252 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025253 {
25254 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025255 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025257 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258 }
25259 /* It's safer to return also on error. */
25260 else if (!eap->skip)
25261 {
25262 /*
25263 * Return unless the expression evaluation has been cancelled due to an
25264 * aborting error, an interrupt, or an exception.
25265 */
25266 if (!aborting())
25267 returning = do_return(eap, FALSE, TRUE, NULL);
25268 }
25269
25270 /* When skipping or the return gets pending, advance to the next command
25271 * in this line (!returning). Otherwise, ignore the rest of the line.
25272 * Following lines will be ignored by get_func_line(). */
25273 if (returning)
25274 eap->nextcmd = NULL;
25275 else if (eap->nextcmd == NULL) /* no argument */
25276 eap->nextcmd = check_nextcmd(arg);
25277
25278 if (eap->skip)
25279 --emsg_skip;
25280}
25281
25282/*
25283 * Return from a function. Possibly makes the return pending. Also called
25284 * for a pending return at the ":endtry" or after returning from an extra
25285 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025286 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025287 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025288 * FALSE when the return gets pending.
25289 */
25290 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025291do_return(
25292 exarg_T *eap,
25293 int reanimate,
25294 int is_cmd,
25295 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025296{
25297 int idx;
25298 struct condstack *cstack = eap->cstack;
25299
25300 if (reanimate)
25301 /* Undo the return. */
25302 current_funccal->returned = FALSE;
25303
25304 /*
25305 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25306 * not in its finally clause (which then is to be executed next) is found.
25307 * In this case, make the ":return" pending for execution at the ":endtry".
25308 * Otherwise, return normally.
25309 */
25310 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25311 if (idx >= 0)
25312 {
25313 cstack->cs_pending[idx] = CSTP_RETURN;
25314
25315 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025316 /* A pending return again gets pending. "rettv" points to an
25317 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025319 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025320 else
25321 {
25322 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025323 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025325 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025327 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025328 {
25329 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025330 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025331 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025332 else
25333 EMSG(_(e_outofmem));
25334 }
25335 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025336 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025337
25338 if (reanimate)
25339 {
25340 /* The pending return value could be overwritten by a ":return"
25341 * without argument in a finally clause; reset the default
25342 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025343 current_funccal->rettv->v_type = VAR_NUMBER;
25344 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025345 }
25346 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025347 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025348 }
25349 else
25350 {
25351 current_funccal->returned = TRUE;
25352
25353 /* If the return is carried out now, store the return value. For
25354 * a return immediately after reanimation, the value is already
25355 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025356 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025357 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025358 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025359 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025361 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025362 }
25363 }
25364
25365 return idx < 0;
25366}
25367
25368/*
25369 * Free the variable with a pending return value.
25370 */
25371 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025372discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373{
Bram Moolenaar33570922005-01-25 22:26:29 +000025374 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025375}
25376
25377/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025378 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025379 * is an allocated string. Used by report_pending() for verbose messages.
25380 */
25381 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025382get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025383{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025384 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025385 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025386 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025387
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025388 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025389 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025390 if (s == NULL)
25391 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025392
25393 STRCPY(IObuff, ":return ");
25394 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25395 if (STRLEN(s) + 8 >= IOSIZE)
25396 STRCPY(IObuff + IOSIZE - 4, "...");
25397 vim_free(tofree);
25398 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025399}
25400
25401/*
25402 * Get next function line.
25403 * Called by do_cmdline() to get the next line.
25404 * Returns allocated string, or NULL for end of function.
25405 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025406 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025407get_func_line(
25408 int c UNUSED,
25409 void *cookie,
25410 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025411{
Bram Moolenaar33570922005-01-25 22:26:29 +000025412 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025413 ufunc_T *fp = fcp->func;
25414 char_u *retval;
25415 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025416
25417 /* If breakpoints have been added/deleted need to check for it. */
25418 if (fcp->dbg_tick != debug_tick)
25419 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025420 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025421 sourcing_lnum);
25422 fcp->dbg_tick = debug_tick;
25423 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025424#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025425 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025426 func_line_end(cookie);
25427#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025428
Bram Moolenaar05159a02005-02-26 23:04:13 +000025429 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025430 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25431 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025432 retval = NULL;
25433 else
25434 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025435 /* Skip NULL lines (continuation lines). */
25436 while (fcp->linenr < gap->ga_len
25437 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25438 ++fcp->linenr;
25439 if (fcp->linenr >= gap->ga_len)
25440 retval = NULL;
25441 else
25442 {
25443 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25444 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025445#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025446 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025447 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025448#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025450 }
25451
25452 /* Did we encounter a breakpoint? */
25453 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25454 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025455 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025457 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025458 sourcing_lnum);
25459 fcp->dbg_tick = debug_tick;
25460 }
25461
25462 return retval;
25463}
25464
Bram Moolenaar05159a02005-02-26 23:04:13 +000025465#if defined(FEAT_PROFILE) || defined(PROTO)
25466/*
25467 * Called when starting to read a function line.
25468 * "sourcing_lnum" must be correct!
25469 * When skipping lines it may not actually be executed, but we won't find out
25470 * until later and we need to store the time now.
25471 */
25472 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025473func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025474{
25475 funccall_T *fcp = (funccall_T *)cookie;
25476 ufunc_T *fp = fcp->func;
25477
25478 if (fp->uf_profiling && sourcing_lnum >= 1
25479 && sourcing_lnum <= fp->uf_lines.ga_len)
25480 {
25481 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025482 /* Skip continuation lines. */
25483 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25484 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025485 fp->uf_tml_execed = FALSE;
25486 profile_start(&fp->uf_tml_start);
25487 profile_zero(&fp->uf_tml_children);
25488 profile_get_wait(&fp->uf_tml_wait);
25489 }
25490}
25491
25492/*
25493 * Called when actually executing a function line.
25494 */
25495 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025496func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025497{
25498 funccall_T *fcp = (funccall_T *)cookie;
25499 ufunc_T *fp = fcp->func;
25500
25501 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25502 fp->uf_tml_execed = TRUE;
25503}
25504
25505/*
25506 * Called when done with a function line.
25507 */
25508 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025509func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025510{
25511 funccall_T *fcp = (funccall_T *)cookie;
25512 ufunc_T *fp = fcp->func;
25513
25514 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25515 {
25516 if (fp->uf_tml_execed)
25517 {
25518 ++fp->uf_tml_count[fp->uf_tml_idx];
25519 profile_end(&fp->uf_tml_start);
25520 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025521 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025522 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25523 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025524 }
25525 fp->uf_tml_idx = -1;
25526 }
25527}
25528#endif
25529
Bram Moolenaar071d4272004-06-13 20:20:40 +000025530/*
25531 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025532 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025533 */
25534 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025535func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536{
Bram Moolenaar33570922005-01-25 22:26:29 +000025537 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025538
25539 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25540 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025541 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025542 || fcp->returned);
25543}
25544
25545/*
25546 * return TRUE if cookie indicates a function which "abort"s on errors.
25547 */
25548 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025549func_has_abort(
25550 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025552 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025553}
25554
25555#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25556typedef enum
25557{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025558 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25559 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25560 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025561} var_flavour_T;
25562
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025563static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025564
25565 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025566var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025567{
25568 char_u *p = varname;
25569
25570 if (ASCII_ISUPPER(*p))
25571 {
25572 while (*(++p))
25573 if (ASCII_ISLOWER(*p))
25574 return VAR_FLAVOUR_SESSION;
25575 return VAR_FLAVOUR_VIMINFO;
25576 }
25577 else
25578 return VAR_FLAVOUR_DEFAULT;
25579}
25580#endif
25581
25582#if defined(FEAT_VIMINFO) || defined(PROTO)
25583/*
25584 * Restore global vars that start with a capital from the viminfo file
25585 */
25586 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025587read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025588{
25589 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025590 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025591 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025592 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025593
25594 if (!writing && (find_viminfo_parameter('!') != NULL))
25595 {
25596 tab = vim_strchr(virp->vir_line + 1, '\t');
25597 if (tab != NULL)
25598 {
25599 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025600 switch (*tab)
25601 {
25602 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025603#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025604 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025605#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025606 case 'D': type = VAR_DICT; break;
25607 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025608 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025609 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025610
25611 tab = vim_strchr(tab, '\t');
25612 if (tab != NULL)
25613 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025614 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025615 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025616 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025617 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025618#ifdef FEAT_FLOAT
25619 else if (type == VAR_FLOAT)
25620 (void)string2float(tab + 1, &tv.vval.v_float);
25621#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025622 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025623 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025624 if (type == VAR_DICT || type == VAR_LIST)
25625 {
25626 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25627
25628 if (etv == NULL)
25629 /* Failed to parse back the dict or list, use it as a
25630 * string. */
25631 tv.v_type = VAR_STRING;
25632 else
25633 {
25634 vim_free(tv.vval.v_string);
25635 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025636 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025637 }
25638 }
25639
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025640 /* when in a function use global variables */
25641 save_funccal = current_funccal;
25642 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025643 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025644 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025645
25646 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025647 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025648 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25649 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025650 }
25651 }
25652 }
25653
25654 return viminfo_readline(virp);
25655}
25656
25657/*
25658 * Write global vars that start with a capital to the viminfo file
25659 */
25660 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025661write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025662{
Bram Moolenaar33570922005-01-25 22:26:29 +000025663 hashitem_T *hi;
25664 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025665 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025666 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025667 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025668 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025669 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025670
25671 if (find_viminfo_parameter('!') == NULL)
25672 return;
25673
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025674 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025675
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025676 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025677 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025679 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025680 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025681 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025682 this_var = HI2DI(hi);
25683 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025684 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025685 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025686 {
25687 case VAR_STRING: s = "STR"; break;
25688 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025689 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025690 case VAR_DICT: s = "DIC"; break;
25691 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025692 case VAR_SPECIAL: s = "XPL"; break;
25693
25694 case VAR_UNKNOWN:
25695 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025696 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025697 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025698 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025699 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025700 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025701 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025702 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025703 if (p != NULL)
25704 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025705 vim_free(tofree);
25706 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025707 }
25708 }
25709}
25710#endif
25711
25712#if defined(FEAT_SESSION) || defined(PROTO)
25713 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025714store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025715{
Bram Moolenaar33570922005-01-25 22:26:29 +000025716 hashitem_T *hi;
25717 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025718 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025719 char_u *p, *t;
25720
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025721 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025722 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025724 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025725 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025726 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025727 this_var = HI2DI(hi);
25728 if ((this_var->di_tv.v_type == VAR_NUMBER
25729 || this_var->di_tv.v_type == VAR_STRING)
25730 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025731 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025732 /* Escape special characters with a backslash. Turn a LF and
25733 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025734 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025735 (char_u *)"\\\"\n\r");
25736 if (p == NULL) /* out of memory */
25737 break;
25738 for (t = p; *t != NUL; ++t)
25739 if (*t == '\n')
25740 *t = 'n';
25741 else if (*t == '\r')
25742 *t = 'r';
25743 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025744 this_var->di_key,
25745 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25746 : ' ',
25747 p,
25748 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25749 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025750 || put_eol(fd) == FAIL)
25751 {
25752 vim_free(p);
25753 return FAIL;
25754 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025755 vim_free(p);
25756 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025757#ifdef FEAT_FLOAT
25758 else if (this_var->di_tv.v_type == VAR_FLOAT
25759 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25760 {
25761 float_T f = this_var->di_tv.vval.v_float;
25762 int sign = ' ';
25763
25764 if (f < 0)
25765 {
25766 f = -f;
25767 sign = '-';
25768 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025769 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025770 this_var->di_key, sign, f) < 0)
25771 || put_eol(fd) == FAIL)
25772 return FAIL;
25773 }
25774#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025775 }
25776 }
25777 return OK;
25778}
25779#endif
25780
Bram Moolenaar661b1822005-07-28 22:36:45 +000025781/*
25782 * Display script name where an item was last set.
25783 * Should only be invoked when 'verbose' is non-zero.
25784 */
25785 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025786last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025787{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025788 char_u *p;
25789
Bram Moolenaar661b1822005-07-28 22:36:45 +000025790 if (scriptID != 0)
25791 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025792 p = home_replace_save(NULL, get_scriptname(scriptID));
25793 if (p != NULL)
25794 {
25795 verbose_enter();
25796 MSG_PUTS(_("\n\tLast set from "));
25797 MSG_PUTS(p);
25798 vim_free(p);
25799 verbose_leave();
25800 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025801 }
25802}
25803
Bram Moolenaard812df62008-11-09 12:46:09 +000025804/*
25805 * List v:oldfiles in a nice way.
25806 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025808ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025809{
25810 list_T *l = vimvars[VV_OLDFILES].vv_list;
25811 listitem_T *li;
25812 int nr = 0;
25813
25814 if (l == NULL)
25815 msg((char_u *)_("No old files"));
25816 else
25817 {
25818 msg_start();
25819 msg_scroll = TRUE;
25820 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25821 {
25822 msg_outnum((long)++nr);
25823 MSG_PUTS(": ");
25824 msg_outtrans(get_tv_string(&li->li_tv));
25825 msg_putchar('\n');
25826 out_flush(); /* output one line at a time */
25827 ui_breakcheck();
25828 }
25829 /* Assume "got_int" was set to truncate the listing. */
25830 got_int = FALSE;
25831
25832#ifdef FEAT_BROWSE_CMD
25833 if (cmdmod.browse)
25834 {
25835 quit_more = FALSE;
25836 nr = prompt_for_number(FALSE);
25837 msg_starthere();
25838 if (nr > 0)
25839 {
25840 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25841 (long)nr);
25842
25843 if (p != NULL)
25844 {
25845 p = expand_env_save(p);
25846 eap->arg = p;
25847 eap->cmdidx = CMD_edit;
25848 cmdmod.browse = FALSE;
25849 do_exedit(eap, NULL);
25850 vim_free(p);
25851 }
25852 }
25853 }
25854#endif
25855 }
25856}
25857
Bram Moolenaar53744302015-07-17 17:38:22 +020025858/* reset v:option_new, v:option_old and v:option_type */
25859 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025860reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025861{
25862 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25863 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25864 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25865}
25866
25867
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868#endif /* FEAT_EVAL */
25869
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025871#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025872
25873#ifdef WIN3264
25874/*
25875 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25876 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025877static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25878static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25879static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025880
25881/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025882 * Get the short path (8.3) for the filename in "fnamep".
25883 * Only works for a valid file name.
25884 * When the path gets longer "fnamep" is changed and the allocated buffer
25885 * is put in "bufp".
25886 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25887 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025888 */
25889 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025890get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025892 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025893 char_u *newbuf;
25894
25895 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025896 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025897 if (l > len - 1)
25898 {
25899 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025900 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025901 newbuf = vim_strnsave(*fnamep, l);
25902 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025903 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025904
25905 vim_free(*bufp);
25906 *fnamep = *bufp = newbuf;
25907
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025908 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025909 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 }
25911
25912 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025913 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025914}
25915
25916/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025917 * Get the short path (8.3) for the filename in "fname". The converted
25918 * path is returned in "bufp".
25919 *
25920 * Some of the directories specified in "fname" may not exist. This function
25921 * will shorten the existing directories at the beginning of the path and then
25922 * append the remaining non-existing path.
25923 *
25924 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025925 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025926 * bufp - Pointer to an allocated buffer for the filename.
25927 * fnamelen - Length of the filename pointed to by fname
25928 *
25929 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025930 */
25931 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025932shortpath_for_invalid_fname(
25933 char_u **fname,
25934 char_u **bufp,
25935 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025937 char_u *short_fname, *save_fname, *pbuf_unused;
25938 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025939 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025940 int old_len, len;
25941 int new_len, sfx_len;
25942 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025943
25944 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025945 old_len = *fnamelen;
25946 save_fname = vim_strnsave(*fname, old_len);
25947 pbuf_unused = NULL;
25948 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025949
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025950 endp = save_fname + old_len - 1; /* Find the end of the copy */
25951 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025952
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025953 /*
25954 * Try shortening the supplied path till it succeeds by removing one
25955 * directory at a time from the tail of the path.
25956 */
25957 len = 0;
25958 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025959 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025960 /* go back one path-separator */
25961 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25962 --endp;
25963 if (endp <= save_fname)
25964 break; /* processed the complete path */
25965
25966 /*
25967 * Replace the path separator with a NUL and try to shorten the
25968 * resulting path.
25969 */
25970 ch = *endp;
25971 *endp = 0;
25972 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025973 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025974 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25975 {
25976 retval = FAIL;
25977 goto theend;
25978 }
25979 *endp = ch; /* preserve the string */
25980
25981 if (len > 0)
25982 break; /* successfully shortened the path */
25983
25984 /* failed to shorten the path. Skip the path separator */
25985 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025986 }
25987
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025988 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025989 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025990 /*
25991 * Succeeded in shortening the path. Now concatenate the shortened
25992 * path with the remaining path at the tail.
25993 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025994
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025995 /* Compute the length of the new path. */
25996 sfx_len = (int)(save_endp - endp) + 1;
25997 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025999 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026001 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026002 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026003 /* There is not enough space in the currently allocated string,
26004 * copy it to a buffer big enough. */
26005 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026006 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026007 {
26008 retval = FAIL;
26009 goto theend;
26010 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026011 }
26012 else
26013 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026014 /* Transfer short_fname to the main buffer (it's big enough),
26015 * unless get_short_pathname() did its work in-place. */
26016 *fname = *bufp = save_fname;
26017 if (short_fname != save_fname)
26018 vim_strncpy(save_fname, short_fname, len);
26019 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026020 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026021
26022 /* concat the not-shortened part of the path */
26023 vim_strncpy(*fname + len, endp, sfx_len);
26024 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026025 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026026
26027theend:
26028 vim_free(pbuf_unused);
26029 vim_free(save_fname);
26030
26031 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026032}
26033
26034/*
26035 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026036 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026037 */
26038 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026039shortpath_for_partial(
26040 char_u **fnamep,
26041 char_u **bufp,
26042 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026043{
26044 int sepcount, len, tflen;
26045 char_u *p;
26046 char_u *pbuf, *tfname;
26047 int hasTilde;
26048
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026049 /* Count up the path separators from the RHS.. so we know which part
26050 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026052 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026053 if (vim_ispathsep(*p))
26054 ++sepcount;
26055
26056 /* Need full path first (use expand_env() to remove a "~/") */
26057 hasTilde = (**fnamep == '~');
26058 if (hasTilde)
26059 pbuf = tfname = expand_env_save(*fnamep);
26060 else
26061 pbuf = tfname = FullName_save(*fnamep, FALSE);
26062
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026063 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026064
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026065 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26066 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026067
26068 if (len == 0)
26069 {
26070 /* Don't have a valid filename, so shorten the rest of the
26071 * path if we can. This CAN give us invalid 8.3 filenames, but
26072 * there's not a lot of point in guessing what it might be.
26073 */
26074 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026075 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26076 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026077 }
26078
26079 /* Count the paths backward to find the beginning of the desired string. */
26080 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026081 {
26082#ifdef FEAT_MBYTE
26083 if (has_mbyte)
26084 p -= mb_head_off(tfname, p);
26085#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026086 if (vim_ispathsep(*p))
26087 {
26088 if (sepcount == 0 || (hasTilde && sepcount == 1))
26089 break;
26090 else
26091 sepcount --;
26092 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026093 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026094 if (hasTilde)
26095 {
26096 --p;
26097 if (p >= tfname)
26098 *p = '~';
26099 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026100 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026101 }
26102 else
26103 ++p;
26104
26105 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26106 vim_free(*bufp);
26107 *fnamelen = (int)STRLEN(p);
26108 *bufp = pbuf;
26109 *fnamep = p;
26110
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026111 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026112}
26113#endif /* WIN3264 */
26114
26115/*
26116 * Adjust a filename, according to a string of modifiers.
26117 * *fnamep must be NUL terminated when called. When returning, the length is
26118 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026119 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026120 * When there is an error, *fnamep is set to NULL.
26121 */
26122 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026123modify_fname(
26124 char_u *src, /* string with modifiers */
26125 int *usedlen, /* characters after src that are used */
26126 char_u **fnamep, /* file name so far */
26127 char_u **bufp, /* buffer for allocated file name or NULL */
26128 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026129{
26130 int valid = 0;
26131 char_u *tail;
26132 char_u *s, *p, *pbuf;
26133 char_u dirname[MAXPATHL];
26134 int c;
26135 int has_fullname = 0;
26136#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026137 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026138 int has_shortname = 0;
26139#endif
26140
26141repeat:
26142 /* ":p" - full path/file_name */
26143 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26144 {
26145 has_fullname = 1;
26146
26147 valid |= VALID_PATH;
26148 *usedlen += 2;
26149
26150 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26151 if ((*fnamep)[0] == '~'
26152#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26153 && ((*fnamep)[1] == '/'
26154# ifdef BACKSLASH_IN_FILENAME
26155 || (*fnamep)[1] == '\\'
26156# endif
26157 || (*fnamep)[1] == NUL)
26158
26159#endif
26160 )
26161 {
26162 *fnamep = expand_env_save(*fnamep);
26163 vim_free(*bufp); /* free any allocated file name */
26164 *bufp = *fnamep;
26165 if (*fnamep == NULL)
26166 return -1;
26167 }
26168
26169 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026170 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026171 {
26172 if (vim_ispathsep(*p)
26173 && p[1] == '.'
26174 && (p[2] == NUL
26175 || vim_ispathsep(p[2])
26176 || (p[2] == '.'
26177 && (p[3] == NUL || vim_ispathsep(p[3])))))
26178 break;
26179 }
26180
26181 /* FullName_save() is slow, don't use it when not needed. */
26182 if (*p != NUL || !vim_isAbsName(*fnamep))
26183 {
26184 *fnamep = FullName_save(*fnamep, *p != NUL);
26185 vim_free(*bufp); /* free any allocated file name */
26186 *bufp = *fnamep;
26187 if (*fnamep == NULL)
26188 return -1;
26189 }
26190
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026191#ifdef WIN3264
26192# if _WIN32_WINNT >= 0x0500
26193 if (vim_strchr(*fnamep, '~') != NULL)
26194 {
26195 /* Expand 8.3 filename to full path. Needed to make sure the same
26196 * file does not have two different names.
26197 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26198 p = alloc(_MAX_PATH + 1);
26199 if (p != NULL)
26200 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026201 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026202 {
26203 vim_free(*bufp);
26204 *bufp = *fnamep = p;
26205 }
26206 else
26207 vim_free(p);
26208 }
26209 }
26210# endif
26211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026212 /* Append a path separator to a directory. */
26213 if (mch_isdir(*fnamep))
26214 {
26215 /* Make room for one or two extra characters. */
26216 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26217 vim_free(*bufp); /* free any allocated file name */
26218 *bufp = *fnamep;
26219 if (*fnamep == NULL)
26220 return -1;
26221 add_pathsep(*fnamep);
26222 }
26223 }
26224
26225 /* ":." - path relative to the current directory */
26226 /* ":~" - path relative to the home directory */
26227 /* ":8" - shortname path - postponed till after */
26228 while (src[*usedlen] == ':'
26229 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26230 {
26231 *usedlen += 2;
26232 if (c == '8')
26233 {
26234#ifdef WIN3264
26235 has_shortname = 1; /* Postpone this. */
26236#endif
26237 continue;
26238 }
26239 pbuf = NULL;
26240 /* Need full path first (use expand_env() to remove a "~/") */
26241 if (!has_fullname)
26242 {
26243 if (c == '.' && **fnamep == '~')
26244 p = pbuf = expand_env_save(*fnamep);
26245 else
26246 p = pbuf = FullName_save(*fnamep, FALSE);
26247 }
26248 else
26249 p = *fnamep;
26250
26251 has_fullname = 0;
26252
26253 if (p != NULL)
26254 {
26255 if (c == '.')
26256 {
26257 mch_dirname(dirname, MAXPATHL);
26258 s = shorten_fname(p, dirname);
26259 if (s != NULL)
26260 {
26261 *fnamep = s;
26262 if (pbuf != NULL)
26263 {
26264 vim_free(*bufp); /* free any allocated file name */
26265 *bufp = pbuf;
26266 pbuf = NULL;
26267 }
26268 }
26269 }
26270 else
26271 {
26272 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26273 /* Only replace it when it starts with '~' */
26274 if (*dirname == '~')
26275 {
26276 s = vim_strsave(dirname);
26277 if (s != NULL)
26278 {
26279 *fnamep = s;
26280 vim_free(*bufp);
26281 *bufp = s;
26282 }
26283 }
26284 }
26285 vim_free(pbuf);
26286 }
26287 }
26288
26289 tail = gettail(*fnamep);
26290 *fnamelen = (int)STRLEN(*fnamep);
26291
26292 /* ":h" - head, remove "/file_name", can be repeated */
26293 /* Don't remove the first "/" or "c:\" */
26294 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26295 {
26296 valid |= VALID_HEAD;
26297 *usedlen += 2;
26298 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026299 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026300 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026301 *fnamelen = (int)(tail - *fnamep);
26302#ifdef VMS
26303 if (*fnamelen > 0)
26304 *fnamelen += 1; /* the path separator is part of the path */
26305#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026306 if (*fnamelen == 0)
26307 {
26308 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26309 p = vim_strsave((char_u *)".");
26310 if (p == NULL)
26311 return -1;
26312 vim_free(*bufp);
26313 *bufp = *fnamep = tail = p;
26314 *fnamelen = 1;
26315 }
26316 else
26317 {
26318 while (tail > s && !after_pathsep(s, tail))
26319 mb_ptr_back(*fnamep, tail);
26320 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026321 }
26322
26323 /* ":8" - shortname */
26324 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26325 {
26326 *usedlen += 2;
26327#ifdef WIN3264
26328 has_shortname = 1;
26329#endif
26330 }
26331
26332#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026333 /*
26334 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026335 */
26336 if (has_shortname)
26337 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026338 /* Copy the string if it is shortened by :h and when it wasn't copied
26339 * yet, because we are going to change it in place. Avoids changing
26340 * the buffer name for "%:8". */
26341 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026342 {
26343 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026344 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026345 return -1;
26346 vim_free(*bufp);
26347 *bufp = *fnamep = p;
26348 }
26349
26350 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026351 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026352 if (!has_fullname && !vim_isAbsName(*fnamep))
26353 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026354 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026355 return -1;
26356 }
26357 else
26358 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026359 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360
Bram Moolenaardc935552011-08-17 15:23:23 +020026361 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026363 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026364 return -1;
26365
26366 if (l == 0)
26367 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026368 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026370 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026371 return -1;
26372 }
26373 *fnamelen = l;
26374 }
26375 }
26376#endif /* WIN3264 */
26377
26378 /* ":t" - tail, just the basename */
26379 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26380 {
26381 *usedlen += 2;
26382 *fnamelen -= (int)(tail - *fnamep);
26383 *fnamep = tail;
26384 }
26385
26386 /* ":e" - extension, can be repeated */
26387 /* ":r" - root, without extension, can be repeated */
26388 while (src[*usedlen] == ':'
26389 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26390 {
26391 /* find a '.' in the tail:
26392 * - for second :e: before the current fname
26393 * - otherwise: The last '.'
26394 */
26395 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26396 s = *fnamep - 2;
26397 else
26398 s = *fnamep + *fnamelen - 1;
26399 for ( ; s > tail; --s)
26400 if (s[0] == '.')
26401 break;
26402 if (src[*usedlen + 1] == 'e') /* :e */
26403 {
26404 if (s > tail)
26405 {
26406 *fnamelen += (int)(*fnamep - (s + 1));
26407 *fnamep = s + 1;
26408#ifdef VMS
26409 /* cut version from the extension */
26410 s = *fnamep + *fnamelen - 1;
26411 for ( ; s > *fnamep; --s)
26412 if (s[0] == ';')
26413 break;
26414 if (s > *fnamep)
26415 *fnamelen = s - *fnamep;
26416#endif
26417 }
26418 else if (*fnamep <= tail)
26419 *fnamelen = 0;
26420 }
26421 else /* :r */
26422 {
26423 if (s > tail) /* remove one extension */
26424 *fnamelen = (int)(s - *fnamep);
26425 }
26426 *usedlen += 2;
26427 }
26428
26429 /* ":s?pat?foo?" - substitute */
26430 /* ":gs?pat?foo?" - global substitute */
26431 if (src[*usedlen] == ':'
26432 && (src[*usedlen + 1] == 's'
26433 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26434 {
26435 char_u *str;
26436 char_u *pat;
26437 char_u *sub;
26438 int sep;
26439 char_u *flags;
26440 int didit = FALSE;
26441
26442 flags = (char_u *)"";
26443 s = src + *usedlen + 2;
26444 if (src[*usedlen + 1] == 'g')
26445 {
26446 flags = (char_u *)"g";
26447 ++s;
26448 }
26449
26450 sep = *s++;
26451 if (sep)
26452 {
26453 /* find end of pattern */
26454 p = vim_strchr(s, sep);
26455 if (p != NULL)
26456 {
26457 pat = vim_strnsave(s, (int)(p - s));
26458 if (pat != NULL)
26459 {
26460 s = p + 1;
26461 /* find end of substitution */
26462 p = vim_strchr(s, sep);
26463 if (p != NULL)
26464 {
26465 sub = vim_strnsave(s, (int)(p - s));
26466 str = vim_strnsave(*fnamep, *fnamelen);
26467 if (sub != NULL && str != NULL)
26468 {
26469 *usedlen = (int)(p + 1 - src);
26470 s = do_string_sub(str, pat, sub, flags);
26471 if (s != NULL)
26472 {
26473 *fnamep = s;
26474 *fnamelen = (int)STRLEN(s);
26475 vim_free(*bufp);
26476 *bufp = s;
26477 didit = TRUE;
26478 }
26479 }
26480 vim_free(sub);
26481 vim_free(str);
26482 }
26483 vim_free(pat);
26484 }
26485 }
26486 /* after using ":s", repeat all the modifiers */
26487 if (didit)
26488 goto repeat;
26489 }
26490 }
26491
Bram Moolenaar26df0922014-02-23 23:39:13 +010026492 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26493 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026494 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026495 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026496 if (c != NUL)
26497 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026498 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026499 if (c != NUL)
26500 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026501 if (p == NULL)
26502 return -1;
26503 vim_free(*bufp);
26504 *bufp = *fnamep = p;
26505 *fnamelen = (int)STRLEN(p);
26506 *usedlen += 2;
26507 }
26508
Bram Moolenaar071d4272004-06-13 20:20:40 +000026509 return valid;
26510}
26511
26512/*
26513 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26514 * "flags" can be "g" to do a global substitute.
26515 * Returns an allocated string, NULL for error.
26516 */
26517 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026518do_string_sub(
26519 char_u *str,
26520 char_u *pat,
26521 char_u *sub,
26522 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026523{
26524 int sublen;
26525 regmatch_T regmatch;
26526 int i;
26527 int do_all;
26528 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026529 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026530 garray_T ga;
26531 char_u *ret;
26532 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026533 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026534
26535 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26536 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026537 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026538
26539 ga_init2(&ga, 1, 200);
26540
26541 do_all = (flags[0] == 'g');
26542
26543 regmatch.rm_ic = p_ic;
26544 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26545 if (regmatch.regprog != NULL)
26546 {
26547 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026548 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026549 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26550 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026551 /* Skip empty match except for first match. */
26552 if (regmatch.startp[0] == regmatch.endp[0])
26553 {
26554 if (zero_width == regmatch.startp[0])
26555 {
26556 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026557 i = MB_PTR2LEN(tail);
26558 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26559 (size_t)i);
26560 ga.ga_len += i;
26561 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026562 continue;
26563 }
26564 zero_width = regmatch.startp[0];
26565 }
26566
Bram Moolenaar071d4272004-06-13 20:20:40 +000026567 /*
26568 * Get some space for a temporary buffer to do the substitution
26569 * into. It will contain:
26570 * - The text up to where the match is.
26571 * - The substituted text.
26572 * - The text after the match.
26573 */
26574 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026575 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026576 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26577 {
26578 ga_clear(&ga);
26579 break;
26580 }
26581
26582 /* copy the text up to where the match is */
26583 i = (int)(regmatch.startp[0] - tail);
26584 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26585 /* add the substituted text */
26586 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26587 + ga.ga_len + i, TRUE, TRUE, FALSE);
26588 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026589 tail = regmatch.endp[0];
26590 if (*tail == NUL)
26591 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026592 if (!do_all)
26593 break;
26594 }
26595
26596 if (ga.ga_data != NULL)
26597 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26598
Bram Moolenaar473de612013-06-08 18:19:48 +020026599 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026600 }
26601
26602 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26603 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026604 if (p_cpo == empty_option)
26605 p_cpo = save_cpo;
26606 else
26607 /* Darn, evaluating {sub} expression changed the value. */
26608 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026609
26610 return ret;
26611}
26612
26613#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */