blob: 2c093d4154e630f89abaa28aff258aef571f4e9c [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;
6030 ++l->lv_refcount;
6031 return OK;
6032}
6033
6034/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006035 * Unreference a list: decrement the reference count and free it when it
6036 * becomes zero.
6037 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006038 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006039list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006040{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006041 if (l != NULL && --l->lv_refcount <= 0)
6042 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043}
6044
6045/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006046 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006047 * Ignores the reference count.
6048 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006049 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006050list_free(
6051 list_T *l,
6052 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006053{
Bram Moolenaar33570922005-01-25 22:26:29 +00006054 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006055
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006056 /* Remove the list from the list of lists for garbage collection. */
6057 if (l->lv_used_prev == NULL)
6058 first_list = l->lv_used_next;
6059 else
6060 l->lv_used_prev->lv_used_next = l->lv_used_next;
6061 if (l->lv_used_next != NULL)
6062 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6063
Bram Moolenaard9fba312005-06-26 22:34:35 +00006064 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006065 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006066 /* Remove the item before deleting it. */
6067 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006068 if (recurse || (item->li_tv.v_type != VAR_LIST
6069 && item->li_tv.v_type != VAR_DICT))
6070 clear_tv(&item->li_tv);
6071 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006072 }
6073 vim_free(l);
6074}
6075
6076/*
6077 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006078 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006079 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006080 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006081listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082{
Bram Moolenaar33570922005-01-25 22:26:29 +00006083 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006084}
6085
6086/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006087 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006088 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006089 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006090listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006092 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006093 vim_free(item);
6094}
6095
6096/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006097 * Remove a list item from a List and free it. Also clears the value.
6098 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006099 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006100listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006101{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006102 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006103 listitem_free(item);
6104}
6105
6106/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006107 * Get the number of items in a list.
6108 */
6109 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006110list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006111{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006112 if (l == NULL)
6113 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006114 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115}
6116
6117/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006118 * Return TRUE when two lists have exactly the same values.
6119 */
6120 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006121list_equal(
6122 list_T *l1,
6123 list_T *l2,
6124 int ic, /* ignore case for strings */
6125 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006126{
Bram Moolenaar33570922005-01-25 22:26:29 +00006127 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006128
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006129 if (l1 == NULL || l2 == NULL)
6130 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006131 if (l1 == l2)
6132 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006133 if (list_len(l1) != list_len(l2))
6134 return FALSE;
6135
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006136 for (item1 = l1->lv_first, item2 = l2->lv_first;
6137 item1 != NULL && item2 != NULL;
6138 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006139 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006140 return FALSE;
6141 return item1 == NULL && item2 == NULL;
6142}
6143
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006144/*
6145 * Return the dictitem that an entry in a hashtable points to.
6146 */
6147 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006148dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006149{
6150 return HI2DI(hi);
6151}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006152
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006153/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006154 * Return TRUE when two dictionaries have exactly the same key/values.
6155 */
6156 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006157dict_equal(
6158 dict_T *d1,
6159 dict_T *d2,
6160 int ic, /* ignore case for strings */
6161 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006162{
Bram Moolenaar33570922005-01-25 22:26:29 +00006163 hashitem_T *hi;
6164 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006165 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006166
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006167 if (d1 == NULL || d2 == NULL)
6168 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006169 if (d1 == d2)
6170 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006171 if (dict_len(d1) != dict_len(d2))
6172 return FALSE;
6173
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006174 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006175 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006176 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006177 if (!HASHITEM_EMPTY(hi))
6178 {
6179 item2 = dict_find(d2, hi->hi_key, -1);
6180 if (item2 == NULL)
6181 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006182 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006183 return FALSE;
6184 --todo;
6185 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006186 }
6187 return TRUE;
6188}
6189
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006190static int tv_equal_recurse_limit;
6191
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006192/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006193 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006194 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006195 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 */
6197 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006198tv_equal(
6199 typval_T *tv1,
6200 typval_T *tv2,
6201 int ic, /* ignore case */
6202 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006203{
6204 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006205 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006206 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006207 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006208
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006209 /* For VAR_FUNC and VAR_PARTIAL only compare the function name. */
6210 if ((tv1->v_type == VAR_FUNC
6211 || (tv1->v_type == VAR_PARTIAL && tv1->vval.v_partial != NULL))
6212 && (tv2->v_type == VAR_FUNC
6213 || (tv2->v_type == VAR_PARTIAL && tv2->vval.v_partial != NULL)))
6214 {
6215 s1 = tv1->v_type == VAR_FUNC ? tv1->vval.v_string
6216 : tv1->vval.v_partial->pt_name;
6217 s2 = tv2->v_type == VAR_FUNC ? tv2->vval.v_string
6218 : tv2->vval.v_partial->pt_name;
6219 return (s1 != NULL && s2 != NULL && STRCMP(s1, s2) == 0);
6220 }
6221
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006222 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006223 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006224
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006225 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006226 * recursiveness to a limit. We guess they are equal then.
6227 * A fixed limit has the problem of still taking an awful long time.
6228 * Reduce the limit every time running into it. That should work fine for
6229 * deeply linked structures that are not recursively linked and catch
6230 * recursiveness quickly. */
6231 if (!recursive)
6232 tv_equal_recurse_limit = 1000;
6233 if (recursive_cnt >= tv_equal_recurse_limit)
6234 {
6235 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006236 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006237 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006238
6239 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006240 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006241 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006242 ++recursive_cnt;
6243 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6244 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006245 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006246
6247 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006248 ++recursive_cnt;
6249 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6250 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006251 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006252
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006253 case VAR_NUMBER:
6254 return tv1->vval.v_number == tv2->vval.v_number;
6255
6256 case VAR_STRING:
6257 s1 = get_tv_string_buf(tv1, buf1);
6258 s2 = get_tv_string_buf(tv2, buf2);
6259 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006260
6261 case VAR_SPECIAL:
6262 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006263
6264 case VAR_FLOAT:
6265#ifdef FEAT_FLOAT
6266 return tv1->vval.v_float == tv2->vval.v_float;
6267#endif
6268 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006269#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006270 return tv1->vval.v_job == tv2->vval.v_job;
6271#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006272 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006273#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006274 return tv1->vval.v_channel == tv2->vval.v_channel;
6275#endif
Bram Moolenaarf0e86a02016-03-19 19:38:12 +01006276 case VAR_FUNC:
6277 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01006278 case VAR_UNKNOWN:
6279 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006280 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006281
Bram Moolenaara03f2332016-02-06 18:09:59 +01006282 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6283 * does not equal anything, not even itself. */
6284 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006285}
6286
6287/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006288 * Locate item with index "n" in list "l" and return it.
6289 * A negative index is counted from the end; -1 is the last item.
6290 * Returns NULL when "n" is out of range.
6291 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006292 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006293list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006294{
Bram Moolenaar33570922005-01-25 22:26:29 +00006295 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006296 long idx;
6297
6298 if (l == NULL)
6299 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300
6301 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006302 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006303 n = l->lv_len + n;
6304
6305 /* Check for index out of range. */
6306 if (n < 0 || n >= l->lv_len)
6307 return NULL;
6308
6309 /* When there is a cached index may start search from there. */
6310 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006311 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006312 if (n < l->lv_idx / 2)
6313 {
6314 /* closest to the start of the list */
6315 item = l->lv_first;
6316 idx = 0;
6317 }
6318 else if (n > (l->lv_idx + l->lv_len) / 2)
6319 {
6320 /* closest to the end of the list */
6321 item = l->lv_last;
6322 idx = l->lv_len - 1;
6323 }
6324 else
6325 {
6326 /* closest to the cached index */
6327 item = l->lv_idx_item;
6328 idx = l->lv_idx;
6329 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006330 }
6331 else
6332 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006333 if (n < l->lv_len / 2)
6334 {
6335 /* closest to the start of the list */
6336 item = l->lv_first;
6337 idx = 0;
6338 }
6339 else
6340 {
6341 /* closest to the end of the list */
6342 item = l->lv_last;
6343 idx = l->lv_len - 1;
6344 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006345 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006346
6347 while (n > idx)
6348 {
6349 /* search forward */
6350 item = item->li_next;
6351 ++idx;
6352 }
6353 while (n < idx)
6354 {
6355 /* search backward */
6356 item = item->li_prev;
6357 --idx;
6358 }
6359
6360 /* cache the used index */
6361 l->lv_idx = idx;
6362 l->lv_idx_item = item;
6363
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006364 return item;
6365}
6366
6367/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006368 * Get list item "l[idx]" as a number.
6369 */
6370 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006371list_find_nr(
6372 list_T *l,
6373 long idx,
6374 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006375{
6376 listitem_T *li;
6377
6378 li = list_find(l, idx);
6379 if (li == NULL)
6380 {
6381 if (errorp != NULL)
6382 *errorp = TRUE;
6383 return -1L;
6384 }
6385 return get_tv_number_chk(&li->li_tv, errorp);
6386}
6387
6388/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006389 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6390 */
6391 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006392list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006393{
6394 listitem_T *li;
6395
6396 li = list_find(l, idx - 1);
6397 if (li == NULL)
6398 {
6399 EMSGN(_(e_listidx), idx);
6400 return NULL;
6401 }
6402 return get_tv_string(&li->li_tv);
6403}
6404
6405/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006406 * Locate "item" list "l" and return its index.
6407 * Returns -1 when "item" is not in the list.
6408 */
6409 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006410list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006411{
6412 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006413 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006414
6415 if (l == NULL)
6416 return -1;
6417 idx = 0;
6418 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6419 ++idx;
6420 if (li == NULL)
6421 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006422 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006423}
6424
6425/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006426 * Append item "item" to the end of list "l".
6427 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006428 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006429list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006430{
6431 if (l->lv_last == NULL)
6432 {
6433 /* empty list */
6434 l->lv_first = item;
6435 l->lv_last = item;
6436 item->li_prev = NULL;
6437 }
6438 else
6439 {
6440 l->lv_last->li_next = item;
6441 item->li_prev = l->lv_last;
6442 l->lv_last = item;
6443 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006444 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006445 item->li_next = NULL;
6446}
6447
6448/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006449 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006450 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006452 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006453list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006454{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006455 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006456
Bram Moolenaar05159a02005-02-26 23:04:13 +00006457 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006458 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006459 copy_tv(tv, &li->li_tv);
6460 list_append(l, li);
6461 return OK;
6462}
6463
6464/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006465 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006466 * Return FAIL when out of memory.
6467 */
6468 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006469list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006470{
6471 listitem_T *li = listitem_alloc();
6472
6473 if (li == NULL)
6474 return FAIL;
6475 li->li_tv.v_type = VAR_DICT;
6476 li->li_tv.v_lock = 0;
6477 li->li_tv.vval.v_dict = dict;
6478 list_append(list, li);
6479 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006480 return OK;
6481}
6482
6483/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006484 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006485 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006486 * Returns FAIL when out of memory.
6487 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006488 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006489list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006490{
6491 listitem_T *li = listitem_alloc();
6492
6493 if (li == NULL)
6494 return FAIL;
6495 list_append(l, li);
6496 li->li_tv.v_type = VAR_STRING;
6497 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006498 if (str == NULL)
6499 li->li_tv.vval.v_string = NULL;
6500 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006501 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006502 return FAIL;
6503 return OK;
6504}
6505
6506/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006507 * Append "n" to list "l".
6508 * Returns FAIL when out of memory.
6509 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006510 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006511list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006512{
6513 listitem_T *li;
6514
6515 li = listitem_alloc();
6516 if (li == NULL)
6517 return FAIL;
6518 li->li_tv.v_type = VAR_NUMBER;
6519 li->li_tv.v_lock = 0;
6520 li->li_tv.vval.v_number = n;
6521 list_append(l, li);
6522 return OK;
6523}
6524
6525/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006526 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006527 * If "item" is NULL append at the end.
6528 * Return FAIL when out of memory.
6529 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006530 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006531list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006532{
Bram Moolenaar33570922005-01-25 22:26:29 +00006533 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006534
6535 if (ni == NULL)
6536 return FAIL;
6537 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006538 list_insert(l, ni, item);
6539 return OK;
6540}
6541
6542 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006543list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006544{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006545 if (item == NULL)
6546 /* Append new item at end of list. */
6547 list_append(l, ni);
6548 else
6549 {
6550 /* Insert new item before existing item. */
6551 ni->li_prev = item->li_prev;
6552 ni->li_next = item;
6553 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006554 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006556 ++l->lv_idx;
6557 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006558 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006559 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 l->lv_idx_item = NULL;
6562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006564 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006565 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006566}
6567
6568/*
6569 * Extend "l1" with "l2".
6570 * If "bef" is NULL append at the end, otherwise insert before this item.
6571 * Returns FAIL when out of memory.
6572 */
6573 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006574list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575{
Bram Moolenaar33570922005-01-25 22:26:29 +00006576 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006577 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006578
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006579 /* We also quit the loop when we have inserted the original item count of
6580 * the list, avoid a hang when we extend a list with itself. */
6581 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006582 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6583 return FAIL;
6584 return OK;
6585}
6586
6587/*
6588 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6589 * Return FAIL when out of memory.
6590 */
6591 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006592list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006593{
Bram Moolenaar33570922005-01-25 22:26:29 +00006594 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006595
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006596 if (l1 == NULL || l2 == NULL)
6597 return FAIL;
6598
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006599 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006600 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006601 if (l == NULL)
6602 return FAIL;
6603 tv->v_type = VAR_LIST;
6604 tv->vval.v_list = l;
6605
6606 /* append all items from the second list */
6607 return list_extend(l, l2, NULL);
6608}
6609
6610/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006611 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006612 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006613 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006614 * Returns NULL when out of memory.
6615 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006617list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006618{
Bram Moolenaar33570922005-01-25 22:26:29 +00006619 list_T *copy;
6620 listitem_T *item;
6621 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 if (orig == NULL)
6624 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006625
6626 copy = list_alloc();
6627 if (copy != NULL)
6628 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006629 if (copyID != 0)
6630 {
6631 /* Do this before adding the items, because one of the items may
6632 * refer back to this list. */
6633 orig->lv_copyID = copyID;
6634 orig->lv_copylist = copy;
6635 }
6636 for (item = orig->lv_first; item != NULL && !got_int;
6637 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006638 {
6639 ni = listitem_alloc();
6640 if (ni == NULL)
6641 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006642 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006643 {
6644 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6645 {
6646 vim_free(ni);
6647 break;
6648 }
6649 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006650 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006651 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006652 list_append(copy, ni);
6653 }
6654 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006655 if (item != NULL)
6656 {
6657 list_unref(copy);
6658 copy = NULL;
6659 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006660 }
6661
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006662 return copy;
6663}
6664
6665/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006666 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006667 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006668 * This used to be called list_remove, but that conflicts with a Sun header
6669 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006670 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006671 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006672vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673{
Bram Moolenaar33570922005-01-25 22:26:29 +00006674 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006676 /* notify watchers */
6677 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006678 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006679 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006680 list_fix_watch(l, ip);
6681 if (ip == item2)
6682 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006683 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006684
6685 if (item2->li_next == NULL)
6686 l->lv_last = item->li_prev;
6687 else
6688 item2->li_next->li_prev = item->li_prev;
6689 if (item->li_prev == NULL)
6690 l->lv_first = item2->li_next;
6691 else
6692 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006693 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006694}
6695
6696/*
6697 * Return an allocated string with the string representation of a list.
6698 * May return NULL.
6699 */
6700 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006701list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006702{
6703 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006704
6705 if (tv->vval.v_list == NULL)
6706 return NULL;
6707 ga_init2(&ga, (int)sizeof(char), 80);
6708 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006709 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006710 {
6711 vim_free(ga.ga_data);
6712 return NULL;
6713 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006714 ga_append(&ga, ']');
6715 ga_append(&ga, NUL);
6716 return (char_u *)ga.ga_data;
6717}
6718
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006719typedef struct join_S {
6720 char_u *s;
6721 char_u *tofree;
6722} join_T;
6723
6724 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006725list_join_inner(
6726 garray_T *gap, /* to store the result in */
6727 list_T *l,
6728 char_u *sep,
6729 int echo_style,
6730 int copyID,
6731 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006732{
6733 int i;
6734 join_T *p;
6735 int len;
6736 int sumlen = 0;
6737 int first = TRUE;
6738 char_u *tofree;
6739 char_u numbuf[NUMBUFLEN];
6740 listitem_T *item;
6741 char_u *s;
6742
6743 /* Stringify each item in the list. */
6744 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6745 {
6746 if (echo_style)
6747 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6748 else
6749 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6750 if (s == NULL)
6751 return FAIL;
6752
6753 len = (int)STRLEN(s);
6754 sumlen += len;
6755
Bram Moolenaarcde88542015-08-11 19:14:00 +02006756 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006757 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6758 if (tofree != NULL || s != numbuf)
6759 {
6760 p->s = s;
6761 p->tofree = tofree;
6762 }
6763 else
6764 {
6765 p->s = vim_strnsave(s, len);
6766 p->tofree = p->s;
6767 }
6768
6769 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006770 if (did_echo_string_emsg) /* recursion error, bail out */
6771 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006772 }
6773
6774 /* Allocate result buffer with its total size, avoid re-allocation and
6775 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6776 if (join_gap->ga_len >= 2)
6777 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6778 if (ga_grow(gap, sumlen + 2) == FAIL)
6779 return FAIL;
6780
6781 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6782 {
6783 if (first)
6784 first = FALSE;
6785 else
6786 ga_concat(gap, sep);
6787 p = ((join_T *)join_gap->ga_data) + i;
6788
6789 if (p->s != NULL)
6790 ga_concat(gap, p->s);
6791 line_breakcheck();
6792 }
6793
6794 return OK;
6795}
6796
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006797/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006799 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006800 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006801 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006802 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006803list_join(
6804 garray_T *gap,
6805 list_T *l,
6806 char_u *sep,
6807 int echo_style,
6808 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006809{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006810 garray_T join_ga;
6811 int retval;
6812 join_T *p;
6813 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006814
Bram Moolenaard39a7512015-04-16 22:51:22 +02006815 if (l->lv_len < 1)
6816 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006817 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6818 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6819
6820 /* Dispose each item in join_ga. */
6821 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006822 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006823 p = (join_T *)join_ga.ga_data;
6824 for (i = 0; i < join_ga.ga_len; ++i)
6825 {
6826 vim_free(p->tofree);
6827 ++p;
6828 }
6829 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006831
6832 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006833}
6834
6835/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006836 * Return the next (unique) copy ID.
6837 * Used for serializing nested structures.
6838 */
6839 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006840get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006841{
6842 current_copyID += COPYID_INC;
6843 return current_copyID;
6844}
6845
6846/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006847 * Garbage collection for lists and dictionaries.
6848 *
6849 * We use reference counts to be able to free most items right away when they
6850 * are no longer used. But for composite items it's possible that it becomes
6851 * unused while the reference count is > 0: When there is a recursive
6852 * reference. Example:
6853 * :let l = [1, 2, 3]
6854 * :let d = {9: l}
6855 * :let l[1] = d
6856 *
6857 * Since this is quite unusual we handle this with garbage collection: every
6858 * once in a while find out which lists and dicts are not referenced from any
6859 * variable.
6860 *
6861 * Here is a good reference text about garbage collection (refers to Python
6862 * but it applies to all reference-counting mechanisms):
6863 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006864 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006865
6866/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 * Do garbage collection for lists and dicts.
6868 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006869 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006870 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006871garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006872{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006873 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006874 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006875 buf_T *buf;
6876 win_T *wp;
6877 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006878 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006879 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006880 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006881#ifdef FEAT_WINDOWS
6882 tabpage_T *tp;
6883#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006884
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006885 /* Only do this once. */
6886 want_garbage_collect = FALSE;
6887 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006888 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006889
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890 /* We advance by two because we add one for items referenced through
6891 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006892 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006893
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006894 /*
6895 * 1. Go through all accessible variables and mark all lists and dicts
6896 * with copyID.
6897 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006898
6899 /* Don't free variables in the previous_funccal list unless they are only
6900 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006901 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006902 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6903 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006904 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6905 NULL);
6906 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6907 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006908 }
6909
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910 /* script-local variables */
6911 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006912 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006913
6914 /* buffer-local variables */
6915 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006916 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6917 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006918
6919 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006920 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006921 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6922 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006923#ifdef FEAT_AUTOCMD
6924 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006925 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6926 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006927#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006928
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006929#ifdef FEAT_WINDOWS
6930 /* tabpage-local variables */
6931 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006932 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6933 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006934#endif
6935
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006936 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006937 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006938
6939 /* function-local variables */
6940 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6941 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006942 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6943 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006944 }
6945
Bram Moolenaard812df62008-11-09 12:46:09 +00006946 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006948
Bram Moolenaar1dced572012-04-05 16:54:08 +02006949#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006950 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006951#endif
6952
Bram Moolenaardb913952012-06-29 12:54:53 +02006953#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006954 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006955#endif
6956
6957#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006958 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006959#endif
6960
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006961#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006962 abort = abort || set_ref_in_channel(copyID);
6963#endif
6964
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006965 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006966 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006967 /*
6968 * 2. Free lists and dictionaries that are not referenced.
6969 */
6970 did_free = free_unref_items(copyID);
6971
6972 /*
6973 * 3. Check if any funccal can be freed now.
6974 */
6975 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006976 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006977 if (can_free_funccal(*pfc, copyID))
6978 {
6979 fc = *pfc;
6980 *pfc = fc->caller;
6981 free_funccal(fc, TRUE);
6982 did_free = TRUE;
6983 did_free_funccal = TRUE;
6984 }
6985 else
6986 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006987 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006988 if (did_free_funccal)
6989 /* When a funccal was freed some more items might be garbage
6990 * collected, so run again. */
6991 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006992 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006993 else if (p_verbose > 0)
6994 {
6995 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6996 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006997
6998 return did_free;
6999}
7000
7001/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01007002 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003 */
7004 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007005free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007007 dict_T *dd, *dd_next;
7008 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 int did_free = FALSE;
7010
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007011 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007012 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007013 */
7014 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007015 {
7016 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007017 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007018 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007019 /* Free the Dictionary and ordinary items it contains, but don't
7020 * recurse into Lists and Dictionaries, they will be in the list
7021 * of dicts or list of lists. */
7022 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007023 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007025 dd = dd_next;
7026 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007027
7028 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007029 * Go through the list of lists and free items without the copyID.
7030 * But don't free a list that has a watcher (used in a for loop), these
7031 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007032 */
7033 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007034 {
7035 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007036 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7037 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007038 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007039 /* Free the List and ordinary items it contains, but don't recurse
7040 * into Lists and Dictionaries, they will be in the list of dicts
7041 * or list of lists. */
7042 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007043 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007044 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007045 ll = ll_next;
7046 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007047
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007048 return did_free;
7049}
7050
7051/*
7052 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007053 * "list_stack" is used to add lists to be marked. Can be NULL.
7054 *
7055 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007057 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007058set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059{
7060 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007061 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007062 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007063 hashtab_T *cur_ht;
7064 ht_stack_T *ht_stack = NULL;
7065 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007066
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007067 cur_ht = ht;
7068 for (;;)
7069 {
7070 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007071 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007072 /* Mark each item in the hashtab. If the item contains a hashtab
7073 * it is added to ht_stack, if it contains a list it is added to
7074 * list_stack. */
7075 todo = (int)cur_ht->ht_used;
7076 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7077 if (!HASHITEM_EMPTY(hi))
7078 {
7079 --todo;
7080 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7081 &ht_stack, list_stack);
7082 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007083 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007084
7085 if (ht_stack == NULL)
7086 break;
7087
7088 /* take an item from the stack */
7089 cur_ht = ht_stack->ht;
7090 tempitem = ht_stack;
7091 ht_stack = ht_stack->prev;
7092 free(tempitem);
7093 }
7094
7095 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007096}
7097
7098/*
7099 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007100 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7101 *
7102 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007105set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007106{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007107 listitem_T *li;
7108 int abort = FALSE;
7109 list_T *cur_l;
7110 list_stack_T *list_stack = NULL;
7111 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007112
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007113 cur_l = l;
7114 for (;;)
7115 {
7116 if (!abort)
7117 /* Mark each item in the list. If the item contains a hashtab
7118 * it is added to ht_stack, if it contains a list it is added to
7119 * list_stack. */
7120 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7121 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7122 ht_stack, &list_stack);
7123 if (list_stack == NULL)
7124 break;
7125
7126 /* take an item from the stack */
7127 cur_l = list_stack->list;
7128 tempitem = list_stack;
7129 list_stack = list_stack->prev;
7130 free(tempitem);
7131 }
7132
7133 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007134}
7135
7136/*
7137 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007138 * "list_stack" is used to add lists to be marked. Can be NULL.
7139 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7140 *
7141 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007142 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007143 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007144set_ref_in_item(
7145 typval_T *tv,
7146 int copyID,
7147 ht_stack_T **ht_stack,
7148 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007149{
7150 dict_T *dd;
7151 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007152 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007153
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007154 if (tv->v_type == VAR_DICT || tv->v_type == VAR_PARTIAL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007155 {
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007156 if (tv->v_type == VAR_DICT)
7157 dd = tv->vval.v_dict;
7158 else if (tv->vval.v_partial != NULL)
7159 dd = tv->vval.v_partial->pt_dict;
7160 else
7161 dd = NULL;
Bram Moolenaara03f2332016-02-06 18:09:59 +01007162 if (dd != NULL && dd->dv_copyID != copyID)
7163 {
7164 /* Didn't see this dict yet. */
7165 dd->dv_copyID = copyID;
7166 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007167 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007168 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7169 }
7170 else
7171 {
7172 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7173 if (newitem == NULL)
7174 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007175 else
7176 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007177 newitem->ht = &dd->dv_hashtab;
7178 newitem->prev = *ht_stack;
7179 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007180 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007181 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007182 }
7183 }
7184 else if (tv->v_type == VAR_LIST)
7185 {
7186 ll = tv->vval.v_list;
7187 if (ll != NULL && ll->lv_copyID != copyID)
7188 {
7189 /* Didn't see this list yet. */
7190 ll->lv_copyID = copyID;
7191 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007192 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007193 abort = set_ref_in_list(ll, copyID, ht_stack);
7194 }
7195 else
7196 {
7197 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007199 if (newitem == NULL)
7200 abort = TRUE;
7201 else
7202 {
7203 newitem->list = ll;
7204 newitem->prev = *list_stack;
7205 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007206 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007207 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007208 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007209 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007210 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007211}
7212
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007213 static void
7214partial_free(partial_T *pt, int free_dict)
7215{
7216 int i;
7217
7218 for (i = 0; i < pt->pt_argc; ++i)
7219 clear_tv(&pt->pt_argv[i]);
7220 vim_free(pt->pt_argv);
7221 if (free_dict)
7222 dict_unref(pt->pt_dict);
7223 func_unref(pt->pt_name);
7224 vim_free(pt->pt_name);
7225 vim_free(pt);
7226}
7227
7228/*
7229 * Unreference a closure: decrement the reference count and free it when it
7230 * becomes zero.
7231 */
7232 void
7233partial_unref(partial_T *pt)
7234{
7235 if (pt != NULL && --pt->pt_refcount <= 0)
7236 partial_free(pt, TRUE);
7237}
7238
Bram Moolenaard9fba312005-06-26 22:34:35 +00007239/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007240 * Allocate an empty header for a dictionary.
7241 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007242 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007243dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007244{
Bram Moolenaar33570922005-01-25 22:26:29 +00007245 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007246
Bram Moolenaar33570922005-01-25 22:26:29 +00007247 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007248 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007249 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007250 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007251 if (first_dict != NULL)
7252 first_dict->dv_used_prev = d;
7253 d->dv_used_next = first_dict;
7254 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007255 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007256
Bram Moolenaar33570922005-01-25 22:26:29 +00007257 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007258 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007259 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007260 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007261 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007262 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007263 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264}
7265
7266/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007267 * Allocate an empty dict for a return value.
7268 * Returns OK or FAIL.
7269 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007270 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007271rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007272{
7273 dict_T *d = dict_alloc();
7274
7275 if (d == NULL)
7276 return FAIL;
7277
7278 rettv->vval.v_dict = d;
7279 rettv->v_type = VAR_DICT;
7280 ++d->dv_refcount;
7281 return OK;
7282}
7283
7284
7285/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007286 * Unreference a Dictionary: decrement the reference count and free it when it
7287 * becomes zero.
7288 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007289 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007290dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007291{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007292 if (d != NULL && --d->dv_refcount <= 0)
7293 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007294}
7295
7296/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007297 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007298 * Ignores the reference count.
7299 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007301dict_free(
7302 dict_T *d,
7303 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007304{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007305 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007306 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007307 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007308
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007309 /* Remove the dict from the list of dicts for garbage collection. */
7310 if (d->dv_used_prev == NULL)
7311 first_dict = d->dv_used_next;
7312 else
7313 d->dv_used_prev->dv_used_next = d->dv_used_next;
7314 if (d->dv_used_next != NULL)
7315 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7316
7317 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007318 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007319 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007320 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007321 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 if (!HASHITEM_EMPTY(hi))
7323 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007324 /* Remove the item before deleting it, just in case there is
7325 * something recursive causing trouble. */
7326 di = HI2DI(hi);
7327 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007328 if (recurse || (di->di_tv.v_type != VAR_LIST
7329 && di->di_tv.v_type != VAR_DICT))
Bram Moolenaar5f436fc2016-03-22 22:34:03 +01007330 {
7331 if (!recurse && di->di_tv.v_type == VAR_PARTIAL)
7332 {
7333 partial_T *pt = di->di_tv.vval.v_partial;
7334
7335 /* We unref the partial but not the dict it refers to. */
7336 if (pt != NULL && --pt->pt_refcount == 0)
7337 partial_free(pt, FALSE);
7338 }
7339 else
7340 clear_tv(&di->di_tv);
7341 }
Bram Moolenaar685295c2006-10-15 20:37:38 +00007342 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007343 --todo;
7344 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007345 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007346 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007347 vim_free(d);
7348}
7349
7350/*
7351 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007352 * The "key" is copied to the new item.
7353 * Note that the value of the item "di_tv" still needs to be initialized!
7354 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007355 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007356 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007357dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007358{
Bram Moolenaar33570922005-01-25 22:26:29 +00007359 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007360
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007361 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007362 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007363 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007364 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007365 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007366 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007367 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007368}
7369
7370/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007371 * Make a copy of a Dictionary item.
7372 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007373 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007374dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375{
Bram Moolenaar33570922005-01-25 22:26:29 +00007376 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007377
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007378 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7379 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007380 if (di != NULL)
7381 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007383 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384 copy_tv(&org->di_tv, &di->di_tv);
7385 }
7386 return di;
7387}
7388
7389/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007390 * Remove item "item" from Dictionary "dict" and free it.
7391 */
7392 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007393dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007394{
Bram Moolenaar33570922005-01-25 22:26:29 +00007395 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007396
Bram Moolenaar33570922005-01-25 22:26:29 +00007397 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007398 if (HASHITEM_EMPTY(hi))
7399 EMSG2(_(e_intern2), "dictitem_remove()");
7400 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007401 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007402 dictitem_free(item);
7403}
7404
7405/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007406 * Free a dict item. Also clears the value.
7407 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007408 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007409dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007410{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007411 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007412 if (item->di_flags & DI_FLAGS_ALLOC)
7413 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007414}
7415
7416/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007417 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7418 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007419 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007420 * Returns NULL when out of memory.
7421 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007422 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007423dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007424{
Bram Moolenaar33570922005-01-25 22:26:29 +00007425 dict_T *copy;
7426 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007427 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007428 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007429
7430 if (orig == NULL)
7431 return NULL;
7432
7433 copy = dict_alloc();
7434 if (copy != NULL)
7435 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007436 if (copyID != 0)
7437 {
7438 orig->dv_copyID = copyID;
7439 orig->dv_copydict = copy;
7440 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007441 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007442 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007443 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007444 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007445 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007446 --todo;
7447
7448 di = dictitem_alloc(hi->hi_key);
7449 if (di == NULL)
7450 break;
7451 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007452 {
7453 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7454 copyID) == FAIL)
7455 {
7456 vim_free(di);
7457 break;
7458 }
7459 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007460 else
7461 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7462 if (dict_add(copy, di) == FAIL)
7463 {
7464 dictitem_free(di);
7465 break;
7466 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007467 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007468 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007469
Bram Moolenaare9a41262005-01-15 22:18:47 +00007470 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007471 if (todo > 0)
7472 {
7473 dict_unref(copy);
7474 copy = NULL;
7475 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007476 }
7477
7478 return copy;
7479}
7480
7481/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007482 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007483 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007484 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007486dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007487{
Bram Moolenaar33570922005-01-25 22:26:29 +00007488 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007489}
7490
Bram Moolenaar8c711452005-01-14 21:53:12 +00007491/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007492 * Add a number or string entry to dictionary "d".
7493 * When "str" is NULL use number "nr", otherwise use "str".
7494 * Returns FAIL when out of memory and when key already exists.
7495 */
7496 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007497dict_add_nr_str(
7498 dict_T *d,
7499 char *key,
7500 long nr,
7501 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007502{
7503 dictitem_T *item;
7504
7505 item = dictitem_alloc((char_u *)key);
7506 if (item == NULL)
7507 return FAIL;
7508 item->di_tv.v_lock = 0;
7509 if (str == NULL)
7510 {
7511 item->di_tv.v_type = VAR_NUMBER;
7512 item->di_tv.vval.v_number = nr;
7513 }
7514 else
7515 {
7516 item->di_tv.v_type = VAR_STRING;
7517 item->di_tv.vval.v_string = vim_strsave(str);
7518 }
7519 if (dict_add(d, item) == FAIL)
7520 {
7521 dictitem_free(item);
7522 return FAIL;
7523 }
7524 return OK;
7525}
7526
7527/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007528 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007529 * Returns FAIL when out of memory and when key already exists.
7530 */
7531 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007532dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007533{
7534 dictitem_T *item;
7535
7536 item = dictitem_alloc((char_u *)key);
7537 if (item == NULL)
7538 return FAIL;
7539 item->di_tv.v_lock = 0;
7540 item->di_tv.v_type = VAR_LIST;
7541 item->di_tv.vval.v_list = list;
7542 if (dict_add(d, item) == FAIL)
7543 {
7544 dictitem_free(item);
7545 return FAIL;
7546 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007547 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007548 return OK;
7549}
7550
7551/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007552 * Get the number of items in a Dictionary.
7553 */
7554 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007555dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007556{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007557 if (d == NULL)
7558 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007559 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007560}
7561
7562/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007563 * Find item "key[len]" in Dictionary "d".
7564 * If "len" is negative use strlen(key).
7565 * Returns NULL when not found.
7566 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007567 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007568dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007569{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007570#define AKEYLEN 200
7571 char_u buf[AKEYLEN];
7572 char_u *akey;
7573 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007574 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007575
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007576 if (len < 0)
7577 akey = key;
7578 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007579 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007580 tofree = akey = vim_strnsave(key, len);
7581 if (akey == NULL)
7582 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007583 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007584 else
7585 {
7586 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007587 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007588 akey = buf;
7589 }
7590
Bram Moolenaar33570922005-01-25 22:26:29 +00007591 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007592 vim_free(tofree);
7593 if (HASHITEM_EMPTY(hi))
7594 return NULL;
7595 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007596}
7597
7598/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007599 * Get a string item from a dictionary.
7600 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007601 * Returns NULL if the entry doesn't exist or out of memory.
7602 */
7603 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007604get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007605{
7606 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007607 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007608
7609 di = dict_find(d, key, -1);
7610 if (di == NULL)
7611 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007612 s = get_tv_string(&di->di_tv);
7613 if (save && s != NULL)
7614 s = vim_strsave(s);
7615 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007616}
7617
7618/*
7619 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007620 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007621 */
7622 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007623get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007624{
7625 dictitem_T *di;
7626
7627 di = dict_find(d, key, -1);
7628 if (di == NULL)
7629 return 0;
7630 return get_tv_number(&di->di_tv);
7631}
7632
7633/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007634 * Return an allocated string with the string representation of a Dictionary.
7635 * May return NULL.
7636 */
7637 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007638dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007639{
7640 garray_T ga;
7641 int first = TRUE;
7642 char_u *tofree;
7643 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007644 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007645 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007646 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007647 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007648
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007649 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007650 return NULL;
7651 ga_init2(&ga, (int)sizeof(char), 80);
7652 ga_append(&ga, '{');
7653
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007654 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007655 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007656 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007657 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007658 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007659 --todo;
7660
7661 if (first)
7662 first = FALSE;
7663 else
7664 ga_concat(&ga, (char_u *)", ");
7665
7666 tofree = string_quote(hi->hi_key, FALSE);
7667 if (tofree != NULL)
7668 {
7669 ga_concat(&ga, tofree);
7670 vim_free(tofree);
7671 }
7672 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007673 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007674 if (s != NULL)
7675 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007676 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007677 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007678 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007679 line_breakcheck();
7680
Bram Moolenaar8c711452005-01-14 21:53:12 +00007681 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007682 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007683 if (todo > 0)
7684 {
7685 vim_free(ga.ga_data);
7686 return NULL;
7687 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007688
7689 ga_append(&ga, '}');
7690 ga_append(&ga, NUL);
7691 return (char_u *)ga.ga_data;
7692}
7693
7694/*
7695 * Allocate a variable for a Dictionary and fill it from "*arg".
7696 * Return OK or FAIL. Returns NOTDONE for {expr}.
7697 */
7698 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007699get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700{
Bram Moolenaar33570922005-01-25 22:26:29 +00007701 dict_T *d = NULL;
7702 typval_T tvkey;
7703 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007704 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007705 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007706 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007707 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007708
7709 /*
7710 * First check if it's not a curly-braces thing: {expr}.
7711 * Must do this without evaluating, otherwise a function may be called
7712 * twice. Unfortunately this means we need to call eval1() twice for the
7713 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007714 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007715 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007716 if (*start != '}')
7717 {
7718 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7719 return FAIL;
7720 if (*start == '}')
7721 return NOTDONE;
7722 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007723
7724 if (evaluate)
7725 {
7726 d = dict_alloc();
7727 if (d == NULL)
7728 return FAIL;
7729 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007730 tvkey.v_type = VAR_UNKNOWN;
7731 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732
7733 *arg = skipwhite(*arg + 1);
7734 while (**arg != '}' && **arg != NUL)
7735 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007736 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 goto failret;
7738 if (**arg != ':')
7739 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007740 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007741 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007742 goto failret;
7743 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007744 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007746 key = get_tv_string_buf_chk(&tvkey, buf);
7747 if (key == NULL || *key == NUL)
7748 {
7749 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7750 if (key != NULL)
7751 EMSG(_(e_emptykey));
7752 clear_tv(&tvkey);
7753 goto failret;
7754 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007755 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756
7757 *arg = skipwhite(*arg + 1);
7758 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7759 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007760 if (evaluate)
7761 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007762 goto failret;
7763 }
7764 if (evaluate)
7765 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007766 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007767 if (item != NULL)
7768 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007769 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007770 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007771 clear_tv(&tv);
7772 goto failret;
7773 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007774 item = dictitem_alloc(key);
7775 clear_tv(&tvkey);
7776 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007777 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007778 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007779 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007780 if (dict_add(d, item) == FAIL)
7781 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007782 }
7783 }
7784
7785 if (**arg == '}')
7786 break;
7787 if (**arg != ',')
7788 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007789 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007790 goto failret;
7791 }
7792 *arg = skipwhite(*arg + 1);
7793 }
7794
7795 if (**arg != '}')
7796 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007797 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007798failret:
7799 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007800 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007801 return FAIL;
7802 }
7803
7804 *arg = skipwhite(*arg + 1);
7805 if (evaluate)
7806 {
7807 rettv->v_type = VAR_DICT;
7808 rettv->vval.v_dict = d;
7809 ++d->dv_refcount;
7810 }
7811
7812 return OK;
7813}
7814
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007815#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007816#endif
7817
Bram Moolenaar17a13432016-01-24 14:22:10 +01007818 static char *
7819get_var_special_name(int nr)
7820{
7821 switch (nr)
7822 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007823 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007824 case VVAL_TRUE: return "v:true";
7825 case VVAL_NONE: return "v:none";
7826 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007827 }
7828 EMSG2(_(e_intern2), "get_var_special_name()");
7829 return "42";
7830}
7831
Bram Moolenaar8c711452005-01-14 21:53:12 +00007832/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 * Return a string with the string representation of a variable.
7834 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007835 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007836 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007837 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007838 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007839 */
7840 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007841echo_string(
7842 typval_T *tv,
7843 char_u **tofree,
7844 char_u *numbuf,
7845 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007846{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007847 static int recurse = 0;
7848 char_u *r = NULL;
7849
Bram Moolenaar33570922005-01-25 22:26:29 +00007850 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007851 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007852 if (!did_echo_string_emsg)
7853 {
7854 /* Only give this message once for a recursive call to avoid
7855 * flooding the user with errors. And stop iterating over lists
7856 * and dicts. */
7857 did_echo_string_emsg = TRUE;
7858 EMSG(_("E724: variable nested too deep for displaying"));
7859 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007860 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007861 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007862 }
7863 ++recurse;
7864
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007865 switch (tv->v_type)
7866 {
7867 case VAR_FUNC:
7868 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 r = tv->vval.v_string;
7870 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007871
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007872 case VAR_PARTIAL:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01007873 {
7874 partial_T *pt = tv->vval.v_partial;
7875 char_u *fname = string_quote(pt == NULL ? NULL
7876 : pt->pt_name, FALSE);
7877 garray_T ga;
7878 int i;
7879 char_u *tf;
7880
7881 ga_init2(&ga, 1, 100);
7882 ga_concat(&ga, (char_u *)"function(");
7883 if (fname != NULL)
7884 {
7885 ga_concat(&ga, fname);
7886 vim_free(fname);
7887 }
7888 if (pt != NULL && pt->pt_argc > 0)
7889 {
7890 ga_concat(&ga, (char_u *)", [");
7891 for (i = 0; i < pt->pt_argc; ++i)
7892 {
7893 if (i > 0)
7894 ga_concat(&ga, (char_u *)", ");
7895 ga_concat(&ga,
7896 tv2string(&pt->pt_argv[i], &tf, numbuf, copyID));
7897 vim_free(tf);
7898 }
7899 ga_concat(&ga, (char_u *)"]");
7900 }
7901 if (pt != NULL && pt->pt_dict != NULL)
7902 {
7903 typval_T dtv;
7904
7905 ga_concat(&ga, (char_u *)", ");
7906 dtv.v_type = VAR_DICT;
7907 dtv.vval.v_dict = pt->pt_dict;
7908 ga_concat(&ga, tv2string(&dtv, &tf, numbuf, copyID));
7909 vim_free(tf);
7910 }
7911 ga_concat(&ga, (char_u *)")");
7912
7913 *tofree = ga.ga_data;
7914 r = *tofree;
7915 break;
7916 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007917
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007918 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007919 if (tv->vval.v_list == NULL)
7920 {
7921 *tofree = NULL;
7922 r = NULL;
7923 }
7924 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7925 {
7926 *tofree = NULL;
7927 r = (char_u *)"[...]";
7928 }
7929 else
7930 {
7931 tv->vval.v_list->lv_copyID = copyID;
7932 *tofree = list2string(tv, copyID);
7933 r = *tofree;
7934 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007935 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007936
Bram Moolenaar8c711452005-01-14 21:53:12 +00007937 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007938 if (tv->vval.v_dict == NULL)
7939 {
7940 *tofree = NULL;
7941 r = NULL;
7942 }
7943 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7944 {
7945 *tofree = NULL;
7946 r = (char_u *)"{...}";
7947 }
7948 else
7949 {
7950 tv->vval.v_dict->dv_copyID = copyID;
7951 *tofree = dict2string(tv, copyID);
7952 r = *tofree;
7953 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007954 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007955
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007956 case VAR_STRING:
7957 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007958 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007959 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007960 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007961 *tofree = NULL;
7962 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007963 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007964
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007965 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007966#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007967 *tofree = NULL;
7968 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7969 r = numbuf;
7970 break;
7971#endif
7972
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007973 case VAR_SPECIAL:
7974 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007975 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007976 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007977 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007978
Bram Moolenaar8502c702014-06-17 12:51:16 +02007979 if (--recurse == 0)
7980 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007981 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007982}
7983
7984/*
7985 * Return a string with the string representation of a variable.
7986 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7987 * "numbuf" is used for a number.
7988 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007989 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007990 */
7991 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007992tv2string(
7993 typval_T *tv,
7994 char_u **tofree,
7995 char_u *numbuf,
7996 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007997{
7998 switch (tv->v_type)
7999 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008000 case VAR_FUNC:
8001 *tofree = string_quote(tv->vval.v_string, TRUE);
8002 return *tofree;
8003 case VAR_STRING:
8004 *tofree = string_quote(tv->vval.v_string, FALSE);
8005 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008006 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01008007#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008008 *tofree = NULL;
8009 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
8010 return numbuf;
8011#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00008012 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008013 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00008014 case VAR_DICT:
Bram Moolenaar24c77a12016-03-24 21:23:06 +01008015 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008016 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01008017 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01008018 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01008019 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00008020 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008021 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00008022 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008023}
8024
8025/*
Bram Moolenaar33570922005-01-25 22:26:29 +00008026 * Return string "str" in ' quotes, doubling ' characters.
8027 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00008028 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008029 */
8030 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008031string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008032{
Bram Moolenaar33570922005-01-25 22:26:29 +00008033 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008034 char_u *p, *r, *s;
8035
Bram Moolenaar33570922005-01-25 22:26:29 +00008036 len = (function ? 13 : 3);
8037 if (str != NULL)
8038 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008039 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00008040 for (p = str; *p != NUL; mb_ptr_adv(p))
8041 if (*p == '\'')
8042 ++len;
8043 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008044 s = r = alloc(len);
8045 if (r != NULL)
8046 {
8047 if (function)
8048 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00008049 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008050 r += 10;
8051 }
8052 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00008053 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00008054 if (str != NULL)
8055 for (p = str; *p != NUL; )
8056 {
8057 if (*p == '\'')
8058 *r++ = '\'';
8059 MB_COPY_CHAR(p, r);
8060 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00008061 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008062 if (function)
8063 *r++ = ')';
8064 *r++ = NUL;
8065 }
8066 return s;
8067}
8068
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008069#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008070/*
8071 * Convert the string "text" to a floating point number.
8072 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
8073 * this always uses a decimal point.
8074 * Returns the length of the text that was consumed.
8075 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01008076 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008077string2float(
8078 char_u *text,
8079 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008080{
8081 char *s = (char *)text;
8082 float_T f;
8083
8084 f = strtod(s, &s);
8085 *value = f;
8086 return (int)((char_u *)s - text);
8087}
8088#endif
8089
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008091 * Get the value of an environment variable.
8092 * "arg" is pointing to the '$'. It is advanced to after the name.
8093 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008094 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008095 */
8096 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008097get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008098{
8099 char_u *string = NULL;
8100 int len;
8101 int cc;
8102 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008103 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008104
8105 ++*arg;
8106 name = *arg;
8107 len = get_env_len(arg);
8108 if (evaluate)
8109 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008110 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008111 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008112
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008113 cc = name[len];
8114 name[len] = NUL;
8115 /* first try vim_getenv(), fast for normal environment vars */
8116 string = vim_getenv(name, &mustfree);
8117 if (string != NULL && *string != NUL)
8118 {
8119 if (!mustfree)
8120 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008121 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008122 else
8123 {
8124 if (mustfree)
8125 vim_free(string);
8126
8127 /* next try expanding things like $VIM and ${HOME} */
8128 string = expand_env_save(name - 1);
8129 if (string != NULL && *string == '$')
8130 {
8131 vim_free(string);
8132 string = NULL;
8133 }
8134 }
8135 name[len] = cc;
8136
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008137 rettv->v_type = VAR_STRING;
8138 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008139 }
8140
8141 return OK;
8142}
8143
8144/*
8145 * Array with names and number of arguments of all internal functions
8146 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8147 */
8148static struct fst
8149{
8150 char *f_name; /* function name */
8151 char f_min_argc; /* minimal number of arguments */
8152 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008153 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008154 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008155} functions[] =
8156{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008157#ifdef FEAT_FLOAT
8158 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008159 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008160#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008161 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008162 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008163 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"append", 2, 2, f_append},
8165 {"argc", 0, 0, f_argc},
8166 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008167 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008168 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008169#ifdef FEAT_FLOAT
8170 {"asin", 1, 1, f_asin}, /* WJMc */
8171#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008172 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008173 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008174 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008175 {"assert_false", 1, 2, f_assert_false},
Bram Moolenaarea6553b2016-03-27 15:13:38 +02008176 {"assert_match", 2, 3, f_assert_match},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008177 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008178#ifdef FEAT_FLOAT
8179 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008180 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008182 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008183 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008184 {"bufexists", 1, 1, f_bufexists},
8185 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8186 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8187 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8188 {"buflisted", 1, 1, f_buflisted},
8189 {"bufloaded", 1, 1, f_bufloaded},
8190 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008191 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008192 {"bufwinnr", 1, 1, f_bufwinnr},
8193 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008194 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008195 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008196 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008197#ifdef FEAT_FLOAT
8198 {"ceil", 1, 1, f_ceil},
8199#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008200#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008201 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008202 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8203 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008204 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008205 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar03602ec2016-03-20 20:57:45 +01008206 {"ch_info", 1, 1, f_ch_info},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008207 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008208 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008209 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008210 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008211 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008212 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8213 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008214 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008215 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008216#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008217 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008218 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008220 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008221 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008222#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008223 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008224 {"complete_add", 1, 1, f_complete_add},
8225 {"complete_check", 0, 0, f_complete_check},
8226#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008227 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008228 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008229#ifdef FEAT_FLOAT
8230 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008231 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008232#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008233 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008234 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008235 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008236 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008237 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008238 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008239 {"diff_filler", 1, 1, f_diff_filler},
8240 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008241 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008242 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008243 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008244 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008245 {"eventhandler", 0, 0, f_eventhandler},
8246 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008247 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008248 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008249#ifdef FEAT_FLOAT
8250 {"exp", 1, 1, f_exp},
8251#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008252 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008253 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008254 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008255 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8256 {"filereadable", 1, 1, f_filereadable},
8257 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008258 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008259 {"finddir", 1, 3, f_finddir},
8260 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008261#ifdef FEAT_FLOAT
8262 {"float2nr", 1, 1, f_float2nr},
8263 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008264 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008265#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008266 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008267 {"fnamemodify", 2, 2, f_fnamemodify},
8268 {"foldclosed", 1, 1, f_foldclosed},
8269 {"foldclosedend", 1, 1, f_foldclosedend},
8270 {"foldlevel", 1, 1, f_foldlevel},
8271 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008272 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008273 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008274 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008275 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008276 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008277 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008278 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008279 {"getchar", 0, 1, f_getchar},
8280 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008281 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008282 {"getcmdline", 0, 0, f_getcmdline},
8283 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008284 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008285 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008286 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008287 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008288 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008289 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008290 {"getfsize", 1, 1, f_getfsize},
8291 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008292 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008293 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008294 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008295 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008296 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008297 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008298 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008299 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008300 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008301 {"gettabvar", 2, 3, f_gettabvar},
8302 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008303 {"getwinposx", 0, 0, f_getwinposx},
8304 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008305 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008306 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008307 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008308 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008309 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008310 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008311 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008312 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008313 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8314 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8315 {"histadd", 2, 2, f_histadd},
8316 {"histdel", 1, 2, f_histdel},
8317 {"histget", 1, 2, f_histget},
8318 {"histnr", 1, 1, f_histnr},
8319 {"hlID", 1, 1, f_hlID},
8320 {"hlexists", 1, 1, f_hlexists},
8321 {"hostname", 0, 0, f_hostname},
8322 {"iconv", 3, 3, f_iconv},
8323 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008324 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008325 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008326 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008327 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008328 {"inputrestore", 0, 0, f_inputrestore},
8329 {"inputsave", 0, 0, f_inputsave},
8330 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008331 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008332 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008333 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008334 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008335#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8336 {"isnan", 1, 1, f_isnan},
8337#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008338 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008339#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008340 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008341 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008342 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008343 {"job_start", 1, 2, f_job_start},
8344 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008345 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008346#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008347 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008348 {"js_decode", 1, 1, f_js_decode},
8349 {"js_encode", 1, 1, f_js_encode},
8350 {"json_decode", 1, 1, f_json_decode},
8351 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008352 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008353 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008354 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008355 {"libcall", 3, 3, f_libcall},
8356 {"libcallnr", 3, 3, f_libcallnr},
8357 {"line", 1, 1, f_line},
8358 {"line2byte", 1, 1, f_line2byte},
8359 {"lispindent", 1, 1, f_lispindent},
8360 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008361#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008362 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008363 {"log10", 1, 1, f_log10},
8364#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008365#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008366 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008367#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008368 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008369 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008370 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008371 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008372 {"matchadd", 2, 5, f_matchadd},
8373 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008374 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008375 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008376 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008377 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008378 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008379 {"max", 1, 1, f_max},
8380 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008381#ifdef vim_mkdir
8382 {"mkdir", 1, 3, f_mkdir},
8383#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008384 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008385#ifdef FEAT_MZSCHEME
8386 {"mzeval", 1, 1, f_mzeval},
8387#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008388 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008389 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008390 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008391 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008392#ifdef FEAT_PERL
8393 {"perleval", 1, 1, f_perleval},
8394#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008395#ifdef FEAT_FLOAT
8396 {"pow", 2, 2, f_pow},
8397#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008398 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008399 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008400 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008401#ifdef FEAT_PYTHON3
8402 {"py3eval", 1, 1, f_py3eval},
8403#endif
8404#ifdef FEAT_PYTHON
8405 {"pyeval", 1, 1, f_pyeval},
8406#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008407 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008408 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008409 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008410#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008411 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008412#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008413 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008414 {"remote_expr", 2, 3, f_remote_expr},
8415 {"remote_foreground", 1, 1, f_remote_foreground},
8416 {"remote_peek", 1, 2, f_remote_peek},
8417 {"remote_read", 1, 1, f_remote_read},
8418 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008419 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008420 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008421 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008422 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008423 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008424#ifdef FEAT_FLOAT
8425 {"round", 1, 1, f_round},
8426#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008427 {"screenattr", 2, 2, f_screenattr},
8428 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008429 {"screencol", 0, 0, f_screencol},
8430 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008431 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008432 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008433 {"searchpair", 3, 7, f_searchpair},
8434 {"searchpairpos", 3, 7, f_searchpairpos},
8435 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008436 {"server2client", 2, 2, f_server2client},
8437 {"serverlist", 0, 0, f_serverlist},
8438 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008439 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008440 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008441 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008443 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008444 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008445 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008446 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008448 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008449 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008451#ifdef FEAT_CRYPT
8452 {"sha256", 1, 1, f_sha256},
8453#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008454 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008455 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008456 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008457#ifdef FEAT_FLOAT
8458 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008459 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008460#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008461 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008462 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008463 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008464 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008465 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008466#ifdef FEAT_FLOAT
8467 {"sqrt", 1, 1, f_sqrt},
8468 {"str2float", 1, 1, f_str2float},
8469#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008470 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008471 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008472 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008473#ifdef HAVE_STRFTIME
8474 {"strftime", 1, 2, f_strftime},
8475#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008476 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008477 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008478 {"strlen", 1, 1, f_strlen},
8479 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008480 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008481 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008482 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008483 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008484 {"substitute", 4, 4, f_substitute},
8485 {"synID", 3, 3, f_synID},
8486 {"synIDattr", 2, 3, f_synIDattr},
8487 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008488 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008489 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008490 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008491 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008492 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008493 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008494 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008495 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008496 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008497#ifdef FEAT_FLOAT
8498 {"tan", 1, 1, f_tan},
8499 {"tanh", 1, 1, f_tanh},
8500#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008501 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008502 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008503#ifdef FEAT_TIMERS
8504 {"timer_start", 2, 3, f_timer_start},
8505 {"timer_stop", 1, 1, f_timer_stop},
8506#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008507 {"tolower", 1, 1, f_tolower},
8508 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008509 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008510#ifdef FEAT_FLOAT
8511 {"trunc", 1, 1, f_trunc},
8512#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008513 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008514 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008515 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008516 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008517 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008518 {"virtcol", 1, 1, f_virtcol},
8519 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008520 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008521 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008522 {"win_getid", 0, 2, f_win_getid},
8523 {"win_gotoid", 1, 1, f_win_gotoid},
8524 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8525 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008526 {"winbufnr", 1, 1, f_winbufnr},
8527 {"wincol", 0, 0, f_wincol},
8528 {"winheight", 1, 1, f_winheight},
8529 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008530 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008531 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008532 {"winrestview", 1, 1, f_winrestview},
8533 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008534 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008535 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008536 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008537 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008538};
8539
8540#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8541
8542/*
8543 * Function given to ExpandGeneric() to obtain the list of internal
8544 * or user defined function names.
8545 */
8546 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008547get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008548{
8549 static int intidx = -1;
8550 char_u *name;
8551
8552 if (idx == 0)
8553 intidx = -1;
8554 if (intidx < 0)
8555 {
8556 name = get_user_func_name(xp, idx);
8557 if (name != NULL)
8558 return name;
8559 }
8560 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8561 {
8562 STRCPY(IObuff, functions[intidx].f_name);
8563 STRCAT(IObuff, "(");
8564 if (functions[intidx].f_max_argc == 0)
8565 STRCAT(IObuff, ")");
8566 return IObuff;
8567 }
8568
8569 return NULL;
8570}
8571
8572/*
8573 * Function given to ExpandGeneric() to obtain the list of internal or
8574 * user defined variable or function names.
8575 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008576 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008577get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008578{
8579 static int intidx = -1;
8580 char_u *name;
8581
8582 if (idx == 0)
8583 intidx = -1;
8584 if (intidx < 0)
8585 {
8586 name = get_function_name(xp, idx);
8587 if (name != NULL)
8588 return name;
8589 }
8590 return get_user_var_name(xp, ++intidx);
8591}
8592
8593#endif /* FEAT_CMDL_COMPL */
8594
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008595#if defined(EBCDIC) || defined(PROTO)
8596/*
8597 * Compare struct fst by function name.
8598 */
8599 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008600compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008601{
8602 struct fst *p1 = (struct fst *)s1;
8603 struct fst *p2 = (struct fst *)s2;
8604
8605 return STRCMP(p1->f_name, p2->f_name);
8606}
8607
8608/*
8609 * Sort the function table by function name.
8610 * The sorting of the table above is ASCII dependant.
8611 * On machines using EBCDIC we have to sort it.
8612 */
8613 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008614sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008615{
8616 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8617
8618 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8619}
8620#endif
8621
8622
Bram Moolenaar071d4272004-06-13 20:20:40 +00008623/*
8624 * Find internal function in table above.
8625 * Return index, or -1 if not found
8626 */
8627 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008628find_internal_func(
8629 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008630{
8631 int first = 0;
8632 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8633 int cmp;
8634 int x;
8635
8636 /*
8637 * Find the function name in the table. Binary search.
8638 */
8639 while (first <= last)
8640 {
8641 x = first + ((unsigned)(last - first) >> 1);
8642 cmp = STRCMP(name, functions[x].f_name);
8643 if (cmp < 0)
8644 last = x - 1;
8645 else if (cmp > 0)
8646 first = x + 1;
8647 else
8648 return x;
8649 }
8650 return -1;
8651}
8652
8653/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008654 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8655 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008656 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8657 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008658 */
8659 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008660deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008661{
Bram Moolenaar33570922005-01-25 22:26:29 +00008662 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008663 int cc;
8664
Bram Moolenaar65639032016-03-16 21:40:30 +01008665 if (partialp != NULL)
8666 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008667
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008668 cc = name[*lenp];
8669 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008670 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008671 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008672 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008673 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008674 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008675 {
8676 *lenp = 0;
8677 return (char_u *)""; /* just in case */
8678 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008679 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008680 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008681 }
8682
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008683 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8684 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008685 partial_T *pt = v->di_tv.vval.v_partial;
8686
8687 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008688 {
8689 *lenp = 0;
8690 return (char_u *)""; /* just in case */
8691 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008692 if (partialp != NULL)
8693 *partialp = pt;
8694 *lenp = (int)STRLEN(pt->pt_name);
8695 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008696 }
8697
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008698 return name;
8699}
8700
8701/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008702 * Allocate a variable for the result of a function.
8703 * Return OK or FAIL.
8704 */
8705 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008706get_func_tv(
8707 char_u *name, /* name of the function */
8708 int len, /* length of "name" */
8709 typval_T *rettv,
8710 char_u **arg, /* argument, pointing to the '(' */
8711 linenr_T firstline, /* first line of range */
8712 linenr_T lastline, /* last line of range */
8713 int *doesrange, /* return: function handled range */
8714 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008715 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008716 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008717{
8718 char_u *argp;
8719 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008720 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008721 int argcount = 0; /* number of arguments found */
8722
8723 /*
8724 * Get the arguments.
8725 */
8726 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008727 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008728 {
8729 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8730 if (*argp == ')' || *argp == ',' || *argp == NUL)
8731 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008732 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8733 {
8734 ret = FAIL;
8735 break;
8736 }
8737 ++argcount;
8738 if (*argp != ',')
8739 break;
8740 }
8741 if (*argp == ')')
8742 ++argp;
8743 else
8744 ret = FAIL;
8745
8746 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008747 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008748 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008749 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008750 {
8751 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008752 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008753 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008754 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008756
8757 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008758 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008759
8760 *arg = skipwhite(argp);
8761 return ret;
8762}
8763
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008764#define ERROR_UNKNOWN 0
8765#define ERROR_TOOMANY 1
8766#define ERROR_TOOFEW 2
8767#define ERROR_SCRIPT 3
8768#define ERROR_DICT 4
8769#define ERROR_NONE 5
8770#define ERROR_OTHER 6
8771#define FLEN_FIXED 40
8772
8773/*
8774 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8775 * Change <SNR>123_name() to K_SNR 123_name().
8776 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8777 * (slow).
8778 */
8779 static char_u *
8780fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8781{
8782 int llen;
8783 char_u *fname;
8784 int i;
8785
8786 llen = eval_fname_script(name);
8787 if (llen > 0)
8788 {
8789 fname_buf[0] = K_SPECIAL;
8790 fname_buf[1] = KS_EXTRA;
8791 fname_buf[2] = (int)KE_SNR;
8792 i = 3;
8793 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8794 {
8795 if (current_SID <= 0)
8796 *error = ERROR_SCRIPT;
8797 else
8798 {
8799 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8800 i = (int)STRLEN(fname_buf);
8801 }
8802 }
8803 if (i + STRLEN(name + llen) < FLEN_FIXED)
8804 {
8805 STRCPY(fname_buf + i, name + llen);
8806 fname = fname_buf;
8807 }
8808 else
8809 {
8810 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8811 if (fname == NULL)
8812 *error = ERROR_OTHER;
8813 else
8814 {
8815 *tofree = fname;
8816 mch_memmove(fname, fname_buf, (size_t)i);
8817 STRCPY(fname + i, name + llen);
8818 }
8819 }
8820 }
8821 else
8822 fname = name;
8823 return fname;
8824}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008825
8826/*
8827 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008828 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008829 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008830 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008831 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008832call_func(
8833 char_u *funcname, /* name of the function */
8834 int len, /* length of "name" */
8835 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008836 int argcount_in, /* number of "argvars" */
8837 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008838 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008839 linenr_T firstline, /* first line of range */
8840 linenr_T lastline, /* last line of range */
8841 int *doesrange, /* return: function handled range */
8842 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008843 partial_T *partial, /* optional, can be NULL */
8844 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008845{
8846 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008847 int error = ERROR_NONE;
8848 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008850 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008851 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008852 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008853 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008854 int argcount = argcount_in;
8855 typval_T *argvars = argvars_in;
8856 dict_T *selfdict = selfdict_in;
8857 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8858 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008859
8860 /* Make a copy of the name, if it comes from a funcref variable it could
8861 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008862 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008863 if (name == NULL)
8864 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008865
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008866 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008867
8868 *doesrange = FALSE;
8869
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008870 if (partial != NULL)
8871 {
8872 if (partial->pt_dict != NULL)
8873 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008874 /* When the function has a partial with a dict and there is a dict
8875 * argument, use the dict argument. That is backwards compatible.
8876 */
8877 if (selfdict_in == NULL)
8878 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008879 }
8880 if (error == ERROR_NONE && partial->pt_argc > 0)
8881 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008882 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8883 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8884 for (i = 0; i < argcount_in; ++i)
8885 argv[i + argv_clear] = argvars_in[i];
8886 argvars = argv;
8887 argcount = partial->pt_argc + argcount_in;
8888 }
8889 }
8890
Bram Moolenaar071d4272004-06-13 20:20:40 +00008891
8892 /* execute the function if no errors detected and executing */
8893 if (evaluate && error == ERROR_NONE)
8894 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008895 char_u *rfname = fname;
8896
8897 /* Ignore "g:" before a function name. */
8898 if (fname[0] == 'g' && fname[1] == ':')
8899 rfname = fname + 2;
8900
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008901 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8902 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008903 error = ERROR_UNKNOWN;
8904
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008905 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008906 {
8907 /*
8908 * User defined function.
8909 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008910 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008911
Bram Moolenaar071d4272004-06-13 20:20:40 +00008912#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008913 /* Trigger FuncUndefined event, may load the function. */
8914 if (fp == NULL
8915 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008916 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008917 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008918 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008919 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008920 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008921 }
8922#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008923 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008924 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008925 {
8926 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008927 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008928 }
8929
Bram Moolenaar071d4272004-06-13 20:20:40 +00008930 if (fp != NULL)
8931 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008932 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008933 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008934 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008935 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008936 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008937 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008938 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008939 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008940 else
8941 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008942 int did_save_redo = FALSE;
8943
Bram Moolenaar071d4272004-06-13 20:20:40 +00008944 /*
8945 * Call the user function.
8946 * Save and restore search patterns, script variables and
8947 * redo buffer.
8948 */
8949 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008950#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008951 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008952#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008953 {
8954 saveRedobuff();
8955 did_save_redo = TRUE;
8956 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008957 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008958 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008959 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008960 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8961 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8962 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008963 /* Function was unreferenced while being used, free it
8964 * now. */
8965 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008966 if (did_save_redo)
8967 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008968 restore_search_patterns();
8969 error = ERROR_NONE;
8970 }
8971 }
8972 }
8973 else
8974 {
8975 /*
8976 * Find the function name in the table, call its implementation.
8977 */
8978 i = find_internal_func(fname);
8979 if (i >= 0)
8980 {
8981 if (argcount < functions[i].f_min_argc)
8982 error = ERROR_TOOFEW;
8983 else if (argcount > functions[i].f_max_argc)
8984 error = ERROR_TOOMANY;
8985 else
8986 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008987 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008988 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008989 error = ERROR_NONE;
8990 }
8991 }
8992 }
8993 /*
8994 * The function call (or "FuncUndefined" autocommand sequence) might
8995 * have been aborted by an error, an interrupt, or an explicitly thrown
8996 * exception that has not been caught so far. This situation can be
8997 * tested for by calling aborting(). For an error in an internal
8998 * function or for the "E132" error in call_user_func(), however, the
8999 * throw point at which the "force_abort" flag (temporarily reset by
9000 * emsg()) is normally updated has not been reached yet. We need to
9001 * update that flag first to make aborting() reliable.
9002 */
9003 update_force_abort();
9004 }
9005 if (error == ERROR_NONE)
9006 ret = OK;
9007
9008 /*
9009 * Report an error unless the argument evaluation or function call has been
9010 * cancelled due to an aborting error, an interrupt, or an exception.
9011 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00009012 if (!aborting())
9013 {
9014 switch (error)
9015 {
9016 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009017 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009018 break;
9019 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009020 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00009021 break;
9022 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009023 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009024 name);
9025 break;
9026 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009027 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00009028 name);
9029 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00009030 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009031 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00009032 name);
9033 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00009034 }
9035 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009036
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009037 while (argv_clear > 0)
9038 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01009039 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02009040 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009041
9042 return ret;
9043}
9044
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009045/*
9046 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00009047 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009048 */
9049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009051{
9052 char_u *p;
9053
9054 if (*name == K_SPECIAL)
9055 p = concat_str((char_u *)"<SNR>", name + 3);
9056 else
9057 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00009058 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00009059 if (p != name)
9060 vim_free(p);
9061}
9062
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009063/*
9064 * Return TRUE for a non-zero Number and a non-empty String.
9065 */
9066 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009067non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00009068{
9069 return ((argvars[0].v_type == VAR_NUMBER
9070 && argvars[0].vval.v_number != 0)
9071 || (argvars[0].v_type == VAR_STRING
9072 && argvars[0].vval.v_string != NULL
9073 && *argvars[0].vval.v_string != NUL));
9074}
9075
Bram Moolenaar071d4272004-06-13 20:20:40 +00009076/*********************************************
9077 * Implementation of the built-in functions
9078 */
9079
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009080#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009081static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009082
9083/*
9084 * Get the float value of "argvars[0]" into "f".
9085 * Returns FAIL when the argument is not a Number or Float.
9086 */
9087 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009088get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009089{
9090 if (argvars[0].v_type == VAR_FLOAT)
9091 {
9092 *f = argvars[0].vval.v_float;
9093 return OK;
9094 }
9095 if (argvars[0].v_type == VAR_NUMBER)
9096 {
9097 *f = (float_T)argvars[0].vval.v_number;
9098 return OK;
9099 }
9100 EMSG(_("E808: Number or Float required"));
9101 return FAIL;
9102}
9103
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009104/*
9105 * "abs(expr)" function
9106 */
9107 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009108f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009109{
9110 if (argvars[0].v_type == VAR_FLOAT)
9111 {
9112 rettv->v_type = VAR_FLOAT;
9113 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9114 }
9115 else
9116 {
9117 varnumber_T n;
9118 int error = FALSE;
9119
9120 n = get_tv_number_chk(&argvars[0], &error);
9121 if (error)
9122 rettv->vval.v_number = -1;
9123 else if (n > 0)
9124 rettv->vval.v_number = n;
9125 else
9126 rettv->vval.v_number = -n;
9127 }
9128}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009129
9130/*
9131 * "acos()" function
9132 */
9133 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009134f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009135{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009136 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009137
9138 rettv->v_type = VAR_FLOAT;
9139 if (get_float_arg(argvars, &f) == OK)
9140 rettv->vval.v_float = acos(f);
9141 else
9142 rettv->vval.v_float = 0.0;
9143}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009144#endif
9145
Bram Moolenaar071d4272004-06-13 20:20:40 +00009146/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009147 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009148 */
9149 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009150f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009151{
Bram Moolenaar33570922005-01-25 22:26:29 +00009152 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009153
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009154 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009155 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009156 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009157 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009158 && !tv_check_lock(l->lv_lock,
9159 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009160 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009161 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009162 }
9163 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 EMSG(_(e_listreq));
9165}
9166
9167/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009168 * "alloc_fail(id, countdown, repeat)" function
9169 */
9170 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009171f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009172{
9173 if (argvars[0].v_type != VAR_NUMBER
9174 || argvars[0].vval.v_number <= 0
9175 || argvars[1].v_type != VAR_NUMBER
9176 || argvars[1].vval.v_number < 0
9177 || argvars[2].v_type != VAR_NUMBER)
9178 EMSG(_(e_invarg));
9179 else
9180 {
9181 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009182 if (alloc_fail_id >= aid_last)
9183 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009184 alloc_fail_countdown = argvars[1].vval.v_number;
9185 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009186 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009187 }
9188}
9189
9190/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009191 * "and(expr, expr)" function
9192 */
9193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009194f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009195{
9196 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9197 & get_tv_number_chk(&argvars[1], NULL);
9198}
9199
9200/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009201 * "append(lnum, string/list)" function
9202 */
9203 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009204f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009205{
9206 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009207 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009208 list_T *l = NULL;
9209 listitem_T *li = NULL;
9210 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009211 long added = 0;
9212
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009213 /* When coming here from Insert mode, sync undo, so that this can be
9214 * undone separately from what was previously inserted. */
9215 if (u_sync_once == 2)
9216 {
9217 u_sync_once = 1; /* notify that u_sync() was called */
9218 u_sync(TRUE);
9219 }
9220
Bram Moolenaar0d660222005-01-07 21:51:51 +00009221 lnum = get_tv_lnum(argvars);
9222 if (lnum >= 0
9223 && lnum <= curbuf->b_ml.ml_line_count
9224 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009225 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009226 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009227 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009228 l = argvars[1].vval.v_list;
9229 if (l == NULL)
9230 return;
9231 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009232 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009233 for (;;)
9234 {
9235 if (l == NULL)
9236 tv = &argvars[1]; /* append a string */
9237 else if (li == NULL)
9238 break; /* end of list */
9239 else
9240 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009241 line = get_tv_string_chk(tv);
9242 if (line == NULL) /* type error */
9243 {
9244 rettv->vval.v_number = 1; /* Failed */
9245 break;
9246 }
9247 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009248 ++added;
9249 if (l == NULL)
9250 break;
9251 li = li->li_next;
9252 }
9253
9254 appended_lines_mark(lnum, added);
9255 if (curwin->w_cursor.lnum > lnum)
9256 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009257 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009258 else
9259 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009260}
9261
9262/*
9263 * "argc()" function
9264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009266f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009267{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009268 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009269}
9270
9271/*
9272 * "argidx()" function
9273 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009275f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009276{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009277 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009278}
9279
9280/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009281 * "arglistid()" function
9282 */
9283 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009284f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009285{
9286 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009287
9288 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009289 wp = find_tabwin(&argvars[0], &argvars[1]);
9290 if (wp != NULL)
9291 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009292}
9293
9294/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009295 * "argv(nr)" function
9296 */
9297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009298f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009299{
9300 int idx;
9301
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009302 if (argvars[0].v_type != VAR_UNKNOWN)
9303 {
9304 idx = get_tv_number_chk(&argvars[0], NULL);
9305 if (idx >= 0 && idx < ARGCOUNT)
9306 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9307 else
9308 rettv->vval.v_string = NULL;
9309 rettv->v_type = VAR_STRING;
9310 }
9311 else if (rettv_list_alloc(rettv) == OK)
9312 for (idx = 0; idx < ARGCOUNT; ++idx)
9313 list_append_string(rettv->vval.v_list,
9314 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009315}
9316
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009317static void prepare_assert_error(garray_T*gap);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009318static 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 +01009319static void assert_error(garray_T *gap);
9320static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009321
9322/*
9323 * Prepare "gap" for an assert error and add the sourcing position.
9324 */
9325 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009326prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009327{
9328 char buf[NUMBUFLEN];
9329
9330 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009331 if (sourcing_name != NULL)
9332 {
9333 ga_concat(gap, sourcing_name);
9334 if (sourcing_lnum > 0)
9335 ga_concat(gap, (char_u *)" ");
9336 }
9337 if (sourcing_lnum > 0)
9338 {
9339 sprintf(buf, "line %ld", (long)sourcing_lnum);
9340 ga_concat(gap, (char_u *)buf);
9341 }
9342 if (sourcing_name != NULL || sourcing_lnum > 0)
9343 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009344}
9345
9346/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009347 * Append "str" to "gap", escaping unprintable characters.
9348 * Changes NL to \n, CR to \r, etc.
9349 */
9350 static void
9351ga_concat_esc(garray_T *gap, char_u *str)
9352{
9353 char_u *p;
9354 char_u buf[NUMBUFLEN];
9355
Bram Moolenaarf1551962016-03-15 12:55:58 +01009356 if (str == NULL)
9357 {
9358 ga_concat(gap, (char_u *)"NULL");
9359 return;
9360 }
9361
Bram Moolenaar23689172016-02-15 22:37:37 +01009362 for (p = str; *p != NUL; ++p)
9363 switch (*p)
9364 {
9365 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9366 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9367 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9368 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9369 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9370 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9371 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9372 default:
9373 if (*p < ' ')
9374 {
9375 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9376 ga_concat(gap, buf);
9377 }
9378 else
9379 ga_append(gap, *p);
9380 break;
9381 }
9382}
9383
9384/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009385 * Fill "gap" with information about an assert error.
9386 */
9387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009388fill_assert_error(
9389 garray_T *gap,
9390 typval_T *opt_msg_tv,
9391 char_u *exp_str,
9392 typval_T *exp_tv,
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009393 typval_T *got_tv,
9394 int is_match)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009395{
9396 char_u numbuf[NUMBUFLEN];
9397 char_u *tofree;
9398
9399 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9400 {
9401 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9402 vim_free(tofree);
9403 }
9404 else
9405 {
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009406 if (is_match)
9407 ga_concat(gap, (char_u *)"Pattern ");
9408 else
9409 ga_concat(gap, (char_u *)"Expected ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009410 if (exp_str == NULL)
9411 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009412 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009413 vim_free(tofree);
9414 }
9415 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009416 ga_concat_esc(gap, exp_str);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009417 if (is_match)
9418 ga_concat(gap, (char_u *)" does not match ");
9419 else
9420 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009421 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009422 vim_free(tofree);
9423 }
9424}
Bram Moolenaar43345542015-11-29 17:35:35 +01009425
9426/*
9427 * Add an assert error to v:errors.
9428 */
9429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009430assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009431{
9432 struct vimvar *vp = &vimvars[VV_ERRORS];
9433
9434 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9435 /* Make sure v:errors is a list. */
9436 set_vim_var_list(VV_ERRORS, list_alloc());
9437 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9438}
9439
Bram Moolenaar43345542015-11-29 17:35:35 +01009440/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009441 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009442 */
9443 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009444f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009445{
9446 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009447
9448 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9449 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009450 prepare_assert_error(&ga);
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009451 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9452 FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009453 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009454 ga_clear(&ga);
9455 }
9456}
9457
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009458/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009459 * "assert_exception(string[, msg])" function
9460 */
9461 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009462f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009463{
9464 garray_T ga;
9465 char *error;
9466
9467 error = (char *)get_tv_string_chk(&argvars[0]);
9468 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9469 {
9470 prepare_assert_error(&ga);
9471 ga_concat(&ga, (char_u *)"v:exception is not set");
9472 assert_error(&ga);
9473 ga_clear(&ga);
9474 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009475 else if (error != NULL
9476 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009477 {
9478 prepare_assert_error(&ga);
9479 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009480 &vimvars[VV_EXCEPTION].vv_tv, FALSE);
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009481 assert_error(&ga);
9482 ga_clear(&ga);
9483 }
9484}
9485
9486/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009487 * "assert_fails(cmd [, error])" function
9488 */
9489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009490f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009491{
9492 char_u *cmd = get_tv_string_chk(&argvars[0]);
9493 garray_T ga;
9494
9495 called_emsg = FALSE;
9496 suppress_errthrow = TRUE;
9497 emsg_silent = TRUE;
9498 do_cmdline_cmd(cmd);
9499 if (!called_emsg)
9500 {
9501 prepare_assert_error(&ga);
9502 ga_concat(&ga, (char_u *)"command did not fail: ");
9503 ga_concat(&ga, cmd);
9504 assert_error(&ga);
9505 ga_clear(&ga);
9506 }
9507 else if (argvars[1].v_type != VAR_UNKNOWN)
9508 {
9509 char_u buf[NUMBUFLEN];
9510 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9511
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009512 if (error == NULL
9513 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009514 {
9515 prepare_assert_error(&ga);
9516 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009517 &vimvars[VV_ERRMSG].vv_tv, FALSE);
Bram Moolenaara260b872016-01-15 20:48:22 +01009518 assert_error(&ga);
9519 ga_clear(&ga);
9520 }
9521 }
9522
9523 called_emsg = FALSE;
9524 suppress_errthrow = FALSE;
9525 emsg_silent = FALSE;
9526 emsg_on_display = FALSE;
9527 set_vim_var_string(VV_ERRMSG, NULL, 0);
9528}
9529
9530/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009531 * Common for assert_true() and assert_false().
9532 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009534assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009535{
9536 int error = FALSE;
9537 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009538
Bram Moolenaar37127922016-02-06 20:29:28 +01009539 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009540 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009541 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009542 if (argvars[0].v_type != VAR_NUMBER
9543 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9544 || error)
9545 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009546 prepare_assert_error(&ga);
9547 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009548 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009549 NULL, &argvars[0], FALSE);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009550 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009551 ga_clear(&ga);
9552 }
9553}
9554
9555/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009556 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009557 */
9558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009559f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009560{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009561 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009562}
9563
9564/*
Bram Moolenaarea6553b2016-03-27 15:13:38 +02009565 * "assert_match(pattern, actual[, msg])" function
9566 */
9567 static void
9568f_assert_match(typval_T *argvars, typval_T *rettv UNUSED)
9569{
9570 garray_T ga;
9571 char_u buf1[NUMBUFLEN];
9572 char_u buf2[NUMBUFLEN];
9573 char_u *pat = get_tv_string_buf_chk(&argvars[0], buf1);
9574 char_u *text = get_tv_string_buf_chk(&argvars[1], buf2);
9575
9576 if (!pattern_match(pat, text, FALSE))
9577 {
9578 prepare_assert_error(&ga);
9579 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1],
9580 TRUE);
9581 assert_error(&ga);
9582 ga_clear(&ga);
9583 }
9584}
9585
9586/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009587 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009588 */
9589 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009590f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009591{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009592 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009593}
9594
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009595#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009596/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009597 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009598 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009599 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009600f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009601{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009602 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009603
9604 rettv->v_type = VAR_FLOAT;
9605 if (get_float_arg(argvars, &f) == OK)
9606 rettv->vval.v_float = asin(f);
9607 else
9608 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009609}
9610
9611/*
9612 * "atan()" function
9613 */
9614 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009615f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009616{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009617 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009618
9619 rettv->v_type = VAR_FLOAT;
9620 if (get_float_arg(argvars, &f) == OK)
9621 rettv->vval.v_float = atan(f);
9622 else
9623 rettv->vval.v_float = 0.0;
9624}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009625
9626/*
9627 * "atan2()" function
9628 */
9629 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009630f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009631{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009632 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009633
9634 rettv->v_type = VAR_FLOAT;
9635 if (get_float_arg(argvars, &fx) == OK
9636 && get_float_arg(&argvars[1], &fy) == OK)
9637 rettv->vval.v_float = atan2(fx, fy);
9638 else
9639 rettv->vval.v_float = 0.0;
9640}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009641#endif
9642
Bram Moolenaar071d4272004-06-13 20:20:40 +00009643/*
9644 * "browse(save, title, initdir, default)" function
9645 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009646 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009647f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009648{
9649#ifdef FEAT_BROWSE
9650 int save;
9651 char_u *title;
9652 char_u *initdir;
9653 char_u *defname;
9654 char_u buf[NUMBUFLEN];
9655 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009656 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009657
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009658 save = get_tv_number_chk(&argvars[0], &error);
9659 title = get_tv_string_chk(&argvars[1]);
9660 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9661 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009662
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009663 if (error || title == NULL || initdir == NULL || defname == NULL)
9664 rettv->vval.v_string = NULL;
9665 else
9666 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009667 do_browse(save ? BROWSE_SAVE : 0,
9668 title, defname, NULL, initdir, NULL, curbuf);
9669#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009670 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009671#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009672 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009673}
9674
9675/*
9676 * "browsedir(title, initdir)" function
9677 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009678 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009679f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009680{
9681#ifdef FEAT_BROWSE
9682 char_u *title;
9683 char_u *initdir;
9684 char_u buf[NUMBUFLEN];
9685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009686 title = get_tv_string_chk(&argvars[0]);
9687 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009688
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009689 if (title == NULL || initdir == NULL)
9690 rettv->vval.v_string = NULL;
9691 else
9692 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009693 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009694#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009697 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009698}
9699
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009700static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009701
Bram Moolenaar071d4272004-06-13 20:20:40 +00009702/*
9703 * Find a buffer by number or exact name.
9704 */
9705 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009706find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009707{
9708 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009710 if (avar->v_type == VAR_NUMBER)
9711 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009712 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009713 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009714 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009715 if (buf == NULL)
9716 {
9717 /* No full path name match, try a match with a URL or a "nofile"
9718 * buffer, these don't use the full path. */
9719 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9720 if (buf->b_fname != NULL
9721 && (path_with_url(buf->b_fname)
9722#ifdef FEAT_QUICKFIX
9723 || bt_nofile(buf)
9724#endif
9725 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009726 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009727 break;
9728 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009729 }
9730 return buf;
9731}
9732
9733/*
9734 * "bufexists(expr)" function
9735 */
9736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009737f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009738{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009739 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009740}
9741
9742/*
9743 * "buflisted(expr)" function
9744 */
9745 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009746f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009747{
9748 buf_T *buf;
9749
9750 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009751 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009752}
9753
9754/*
9755 * "bufloaded(expr)" function
9756 */
9757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009758f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009759{
9760 buf_T *buf;
9761
9762 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009763 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009764}
9765
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009766 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009767buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009768{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009769 int save_magic;
9770 char_u *save_cpo;
9771 buf_T *buf;
9772
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9774 save_magic = p_magic;
9775 p_magic = TRUE;
9776 save_cpo = p_cpo;
9777 p_cpo = (char_u *)"";
9778
9779 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009780 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009781
9782 p_magic = save_magic;
9783 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009784 return buf;
9785}
9786
9787/*
9788 * Get buffer by number or pattern.
9789 */
9790 static buf_T *
9791get_buf_tv(typval_T *tv, int curtab_only)
9792{
9793 char_u *name = tv->vval.v_string;
9794 buf_T *buf;
9795
9796 if (tv->v_type == VAR_NUMBER)
9797 return buflist_findnr((int)tv->vval.v_number);
9798 if (tv->v_type != VAR_STRING)
9799 return NULL;
9800 if (name == NULL || *name == NUL)
9801 return curbuf;
9802 if (name[0] == '$' && name[1] == NUL)
9803 return lastbuf;
9804
9805 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009806
9807 /* If not found, try expanding the name, like done for bufexists(). */
9808 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009809 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009810
9811 return buf;
9812}
9813
9814/*
9815 * "bufname(expr)" function
9816 */
9817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009818f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009819{
9820 buf_T *buf;
9821
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009822 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009823 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009824 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009825 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009826 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009827 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009828 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009829 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009830 --emsg_off;
9831}
9832
9833/*
9834 * "bufnr(expr)" function
9835 */
9836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009837f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009838{
9839 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009840 int error = FALSE;
9841 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009842
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009843 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009844 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009845 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009846 --emsg_off;
9847
9848 /* If the buffer isn't found and the second argument is not zero create a
9849 * new buffer. */
9850 if (buf == NULL
9851 && argvars[1].v_type != VAR_UNKNOWN
9852 && get_tv_number_chk(&argvars[1], &error) != 0
9853 && !error
9854 && (name = get_tv_string_chk(&argvars[0])) != NULL
9855 && !error)
9856 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9857
Bram Moolenaar071d4272004-06-13 20:20:40 +00009858 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009859 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009860 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009861 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009862}
9863
9864/*
9865 * "bufwinnr(nr)" function
9866 */
9867 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009868f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009869{
9870#ifdef FEAT_WINDOWS
9871 win_T *wp;
9872 int winnr = 0;
9873#endif
9874 buf_T *buf;
9875
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009876 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009877 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009878 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009879#ifdef FEAT_WINDOWS
9880 for (wp = firstwin; wp; wp = wp->w_next)
9881 {
9882 ++winnr;
9883 if (wp->w_buffer == buf)
9884 break;
9885 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009886 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009887#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009888 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009889#endif
9890 --emsg_off;
9891}
9892
9893/*
9894 * "byte2line(byte)" function
9895 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009896 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009897f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009898{
9899#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009900 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009901#else
9902 long boff = 0;
9903
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009904 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009905 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009906 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009907 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009908 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009909 (linenr_T)0, &boff);
9910#endif
9911}
9912
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009914byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009915{
9916#ifdef FEAT_MBYTE
9917 char_u *t;
9918#endif
9919 char_u *str;
9920 long idx;
9921
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009922 str = get_tv_string_chk(&argvars[0]);
9923 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009924 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009925 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009926 return;
9927
9928#ifdef FEAT_MBYTE
9929 t = str;
9930 for ( ; idx > 0; idx--)
9931 {
9932 if (*t == NUL) /* EOL reached */
9933 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009934 if (enc_utf8 && comp)
9935 t += utf_ptr2len(t);
9936 else
9937 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009938 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009939 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009940#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009942 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009943#endif
9944}
9945
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009946/*
9947 * "byteidx()" function
9948 */
9949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009950f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009951{
9952 byteidx(argvars, rettv, FALSE);
9953}
9954
9955/*
9956 * "byteidxcomp()" function
9957 */
9958 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009959f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009960{
9961 byteidx(argvars, rettv, TRUE);
9962}
9963
Bram Moolenaardb913952012-06-29 12:54:53 +02009964 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009965func_call(
9966 char_u *name,
9967 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009968 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009969 dict_T *selfdict,
9970 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009971{
9972 listitem_T *item;
9973 typval_T argv[MAX_FUNC_ARGS + 1];
9974 int argc = 0;
9975 int dummy;
9976 int r = 0;
9977
9978 for (item = args->vval.v_list->lv_first; item != NULL;
9979 item = item->li_next)
9980 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009981 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009982 {
9983 EMSG(_("E699: Too many arguments"));
9984 break;
9985 }
9986 /* Make a copy of each argument. This is needed to be able to set
9987 * v_lock to VAR_FIXED in the copy without changing the original list.
9988 */
9989 copy_tv(&item->li_tv, &argv[argc++]);
9990 }
9991
9992 if (item == NULL)
9993 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9994 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009995 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009996
9997 /* Free the arguments. */
9998 while (argc > 0)
9999 clear_tv(&argv[--argc]);
10000
10001 return r;
10002}
10003
Bram Moolenaarab79bcb2004-07-18 21:34:53 +000010004/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010005 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010006 */
10007 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010008f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010009{
10010 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010011 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000010012 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010013
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010014 if (argvars[1].v_type != VAR_LIST)
10015 {
10016 EMSG(_(e_listreq));
10017 return;
10018 }
10019 if (argvars[1].vval.v_list == NULL)
10020 return;
10021
10022 if (argvars[0].v_type == VAR_FUNC)
10023 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010024 else if (argvars[0].v_type == VAR_PARTIAL)
10025 {
10026 partial = argvars[0].vval.v_partial;
10027 func = partial->pt_name;
10028 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010029 else
10030 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010031 if (*func == NUL)
10032 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010033
Bram Moolenaare9a41262005-01-15 22:18:47 +000010034 if (argvars[2].v_type != VAR_UNKNOWN)
10035 {
10036 if (argvars[2].v_type != VAR_DICT)
10037 {
10038 EMSG(_(e_dictreq));
10039 return;
10040 }
10041 selfdict = argvars[2].vval.v_dict;
10042 }
10043
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010044 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010045}
10046
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010047#ifdef FEAT_FLOAT
10048/*
10049 * "ceil({float})" function
10050 */
10051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010052f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010053{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010054 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010055
10056 rettv->v_type = VAR_FLOAT;
10057 if (get_float_arg(argvars, &f) == OK)
10058 rettv->vval.v_float = ceil(f);
10059 else
10060 rettv->vval.v_float = 0.0;
10061}
10062#endif
10063
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010064#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010065/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010066 * "ch_close()" function
10067 */
10068 static void
10069f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
10070{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010071 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010072
10073 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +010010074 {
Bram Moolenaar8b374212016-02-24 20:43:06 +010010075 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +010010076 channel_clear(channel);
10077 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010078}
10079
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010080/*
10081 * "ch_getbufnr()" function
10082 */
10083 static void
10084f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
10085{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010086 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +010010087
10088 rettv->vval.v_number = -1;
10089 if (channel != NULL)
10090 {
10091 char_u *what = get_tv_string(&argvars[1]);
10092 int part;
10093
10094 if (STRCMP(what, "err") == 0)
10095 part = PART_ERR;
10096 else if (STRCMP(what, "out") == 0)
10097 part = PART_OUT;
10098 else if (STRCMP(what, "in") == 0)
10099 part = PART_IN;
10100 else
10101 part = PART_SOCK;
10102 if (channel->ch_part[part].ch_buffer != NULL)
10103 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
10104 }
10105}
10106
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010107/*
10108 * "ch_getjob()" function
10109 */
10110 static void
10111f_ch_getjob(typval_T *argvars, typval_T *rettv)
10112{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010113 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010114
10115 if (channel != NULL)
10116 {
10117 rettv->v_type = VAR_JOB;
10118 rettv->vval.v_job = channel->ch_job;
10119 if (channel->ch_job != NULL)
10120 ++channel->ch_job->jv_refcount;
10121 }
10122}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010123
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010124/*
Bram Moolenaar03602ec2016-03-20 20:57:45 +010010125 * "ch_info()" function
10126 */
10127 static void
10128f_ch_info(typval_T *argvars, typval_T *rettv UNUSED)
10129{
10130 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
10131
10132 if (channel != NULL && rettv_dict_alloc(rettv) != FAIL)
10133 channel_info(channel, rettv->vval.v_dict);
10134}
10135
10136/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010137 * "ch_log()" function
10138 */
10139 static void
10140f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10141{
10142 char_u *msg = get_tv_string(&argvars[0]);
10143 channel_T *channel = NULL;
10144
10145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010146 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010147
10148 ch_log(channel, (char *)msg);
10149}
10150
10151/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010152 * "ch_logfile()" function
10153 */
10154 static void
10155f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10156{
10157 char_u *fname;
10158 char_u *opt = (char_u *)"";
10159 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010160
10161 fname = get_tv_string(&argvars[0]);
10162 if (argvars[1].v_type == VAR_STRING)
10163 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010164 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010165}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010166
10167/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010168 * "ch_open()" function
10169 */
10170 static void
10171f_ch_open(typval_T *argvars, typval_T *rettv)
10172{
Bram Moolenaar77073442016-02-13 23:23:53 +010010173 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010174 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010175}
10176
10177/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010178 * "ch_read()" function
10179 */
10180 static void
10181f_ch_read(typval_T *argvars, typval_T *rettv)
10182{
10183 common_channel_read(argvars, rettv, FALSE);
10184}
10185
10186/*
10187 * "ch_readraw()" function
10188 */
10189 static void
10190f_ch_readraw(typval_T *argvars, typval_T *rettv)
10191{
10192 common_channel_read(argvars, rettv, TRUE);
10193}
10194
10195/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010196 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010197 */
10198 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010199f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10200{
10201 ch_expr_common(argvars, rettv, TRUE);
10202}
10203
10204/*
10205 * "ch_sendexpr()" function
10206 */
10207 static void
10208f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10209{
10210 ch_expr_common(argvars, rettv, FALSE);
10211}
10212
10213/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010214 * "ch_evalraw()" function
10215 */
10216 static void
10217f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10218{
10219 ch_raw_common(argvars, rettv, TRUE);
10220}
10221
10222/*
10223 * "ch_sendraw()" function
10224 */
10225 static void
10226f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10227{
10228 ch_raw_common(argvars, rettv, FALSE);
10229}
10230
10231/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010232 * "ch_setoptions()" function
10233 */
10234 static void
10235f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10236{
10237 channel_T *channel;
10238 jobopt_T opt;
10239
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010240 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010241 if (channel == NULL)
10242 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010243 clear_job_options(&opt);
10244 if (get_job_options(&argvars[1], &opt,
10245 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010246 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010247 channel_set_options(channel, &opt);
10248}
10249
10250/*
10251 * "ch_status()" function
10252 */
10253 static void
10254f_ch_status(typval_T *argvars, typval_T *rettv)
10255{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010256 channel_T *channel;
10257
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010258 /* return an empty string by default */
10259 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010260 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010261
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010262 channel = get_channel_arg(&argvars[0], FALSE);
10263 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010264}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010265#endif
10266
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010267/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010268 * "changenr()" function
10269 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010270 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010271f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010272{
10273 rettv->vval.v_number = curbuf->b_u_seq_cur;
10274}
10275
10276/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010277 * "char2nr(string)" function
10278 */
10279 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010280f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010281{
10282#ifdef FEAT_MBYTE
10283 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010284 {
10285 int utf8 = 0;
10286
10287 if (argvars[1].v_type != VAR_UNKNOWN)
10288 utf8 = get_tv_number_chk(&argvars[1], NULL);
10289
10290 if (utf8)
10291 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10292 else
10293 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10294 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010295 else
10296#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010297 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010298}
10299
10300/*
10301 * "cindent(lnum)" function
10302 */
10303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010304f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010305{
10306#ifdef FEAT_CINDENT
10307 pos_T pos;
10308 linenr_T lnum;
10309
10310 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010311 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010312 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10313 {
10314 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010315 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316 curwin->w_cursor = pos;
10317 }
10318 else
10319#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010320 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321}
10322
10323/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010324 * "clearmatches()" function
10325 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010326 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010327f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010328{
10329#ifdef FEAT_SEARCH_EXTRA
10330 clear_matches(curwin);
10331#endif
10332}
10333
10334/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010335 * "col(string)" function
10336 */
10337 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010338f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010339{
10340 colnr_T col = 0;
10341 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010342 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010343
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010344 fp = var2fpos(&argvars[0], FALSE, &fnum);
10345 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010346 {
10347 if (fp->col == MAXCOL)
10348 {
10349 /* '> can be MAXCOL, get the length of the line then */
10350 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010351 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010352 else
10353 col = MAXCOL;
10354 }
10355 else
10356 {
10357 col = fp->col + 1;
10358#ifdef FEAT_VIRTUALEDIT
10359 /* col(".") when the cursor is on the NUL at the end of the line
10360 * because of "coladd" can be seen as an extra column. */
10361 if (virtual_active() && fp == &curwin->w_cursor)
10362 {
10363 char_u *p = ml_get_cursor();
10364
10365 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10366 curwin->w_virtcol - curwin->w_cursor.coladd))
10367 {
10368# ifdef FEAT_MBYTE
10369 int l;
10370
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010371 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010372 col += l;
10373# else
10374 if (*p != NUL && p[1] == NUL)
10375 ++col;
10376# endif
10377 }
10378 }
10379#endif
10380 }
10381 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010382 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010383}
10384
Bram Moolenaar572cb562005-08-05 21:35:02 +000010385#if defined(FEAT_INS_EXPAND)
10386/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010387 * "complete()" function
10388 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010390f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010391{
10392 int startcol;
10393
10394 if ((State & INSERT) == 0)
10395 {
10396 EMSG(_("E785: complete() can only be used in Insert mode"));
10397 return;
10398 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010399
10400 /* Check for undo allowed here, because if something was already inserted
10401 * the line was already saved for undo and this check isn't done. */
10402 if (!undo_allowed())
10403 return;
10404
Bram Moolenaarade00832006-03-10 21:46:58 +000010405 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10406 {
10407 EMSG(_(e_invarg));
10408 return;
10409 }
10410
10411 startcol = get_tv_number_chk(&argvars[0], NULL);
10412 if (startcol <= 0)
10413 return;
10414
10415 set_completion(startcol - 1, argvars[1].vval.v_list);
10416}
10417
10418/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010419 * "complete_add()" function
10420 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010421 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010422f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010423{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010424 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010425}
10426
10427/*
10428 * "complete_check()" function
10429 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010431f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010432{
10433 int saved = RedrawingDisabled;
10434
10435 RedrawingDisabled = 0;
10436 ins_compl_check_keys(0);
10437 rettv->vval.v_number = compl_interrupted;
10438 RedrawingDisabled = saved;
10439}
10440#endif
10441
Bram Moolenaar071d4272004-06-13 20:20:40 +000010442/*
10443 * "confirm(message, buttons[, default [, type]])" function
10444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010446f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010447{
10448#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10449 char_u *message;
10450 char_u *buttons = NULL;
10451 char_u buf[NUMBUFLEN];
10452 char_u buf2[NUMBUFLEN];
10453 int def = 1;
10454 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010455 char_u *typestr;
10456 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010457
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010458 message = get_tv_string_chk(&argvars[0]);
10459 if (message == NULL)
10460 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010461 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10464 if (buttons == NULL)
10465 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010466 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010467 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010468 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010469 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010470 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010471 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10472 if (typestr == NULL)
10473 error = TRUE;
10474 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010475 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010476 switch (TOUPPER_ASC(*typestr))
10477 {
10478 case 'E': type = VIM_ERROR; break;
10479 case 'Q': type = VIM_QUESTION; break;
10480 case 'I': type = VIM_INFO; break;
10481 case 'W': type = VIM_WARNING; break;
10482 case 'G': type = VIM_GENERIC; break;
10483 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010484 }
10485 }
10486 }
10487 }
10488
10489 if (buttons == NULL || *buttons == NUL)
10490 buttons = (char_u *)_("&Ok");
10491
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010492 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010493 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010494 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010495#endif
10496}
10497
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010498/*
10499 * "copy()" function
10500 */
10501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010502f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010503{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010504 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010505}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010506
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010507#ifdef FEAT_FLOAT
10508/*
10509 * "cos()" function
10510 */
10511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010512f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010513{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010514 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010515
10516 rettv->v_type = VAR_FLOAT;
10517 if (get_float_arg(argvars, &f) == OK)
10518 rettv->vval.v_float = cos(f);
10519 else
10520 rettv->vval.v_float = 0.0;
10521}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010522
10523/*
10524 * "cosh()" function
10525 */
10526 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010527f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010528{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010529 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010530
10531 rettv->v_type = VAR_FLOAT;
10532 if (get_float_arg(argvars, &f) == OK)
10533 rettv->vval.v_float = cosh(f);
10534 else
10535 rettv->vval.v_float = 0.0;
10536}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010537#endif
10538
Bram Moolenaar071d4272004-06-13 20:20:40 +000010539/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010540 * "count()" function
10541 */
10542 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010543f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010544{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010545 long n = 0;
10546 int ic = FALSE;
10547
Bram Moolenaare9a41262005-01-15 22:18:47 +000010548 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010549 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010550 listitem_T *li;
10551 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010552 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010553
Bram Moolenaare9a41262005-01-15 22:18:47 +000010554 if ((l = argvars[0].vval.v_list) != NULL)
10555 {
10556 li = l->lv_first;
10557 if (argvars[2].v_type != VAR_UNKNOWN)
10558 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010559 int error = FALSE;
10560
10561 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010562 if (argvars[3].v_type != VAR_UNKNOWN)
10563 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010564 idx = get_tv_number_chk(&argvars[3], &error);
10565 if (!error)
10566 {
10567 li = list_find(l, idx);
10568 if (li == NULL)
10569 EMSGN(_(e_listidx), idx);
10570 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010571 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010572 if (error)
10573 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010574 }
10575
10576 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010577 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010578 ++n;
10579 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010580 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010581 else if (argvars[0].v_type == VAR_DICT)
10582 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010583 int todo;
10584 dict_T *d;
10585 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010586
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010587 if ((d = argvars[0].vval.v_dict) != NULL)
10588 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010589 int error = FALSE;
10590
Bram Moolenaare9a41262005-01-15 22:18:47 +000010591 if (argvars[2].v_type != VAR_UNKNOWN)
10592 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010593 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010594 if (argvars[3].v_type != VAR_UNKNOWN)
10595 EMSG(_(e_invarg));
10596 }
10597
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010598 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010599 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010600 {
10601 if (!HASHITEM_EMPTY(hi))
10602 {
10603 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010604 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010605 ++n;
10606 }
10607 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010608 }
10609 }
10610 else
10611 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010612 rettv->vval.v_number = n;
10613}
10614
10615/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010616 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10617 *
10618 * Checks the existence of a cscope connection.
10619 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010620 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010621f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010622{
10623#ifdef FEAT_CSCOPE
10624 int num = 0;
10625 char_u *dbpath = NULL;
10626 char_u *prepend = NULL;
10627 char_u buf[NUMBUFLEN];
10628
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010629 if (argvars[0].v_type != VAR_UNKNOWN
10630 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010632 num = (int)get_tv_number(&argvars[0]);
10633 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010634 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010635 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010636 }
10637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010638 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010639#endif
10640}
10641
10642/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010643 * "cursor(lnum, col)" function, or
10644 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010646 * Moves the cursor to the specified line and column.
10647 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010648 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010649 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010650f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010651{
10652 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010653#ifdef FEAT_VIRTUALEDIT
10654 long coladd = 0;
10655#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010656 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010657
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010658 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010659 if (argvars[1].v_type == VAR_UNKNOWN)
10660 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010661 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010662 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010663
Bram Moolenaar493c1782014-05-28 14:34:46 +020010664 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010665 {
10666 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010667 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010668 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010669 line = pos.lnum;
10670 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010671#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010672 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010673#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010674 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010675 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010676 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010677 set_curswant = FALSE;
10678 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010679 }
10680 else
10681 {
10682 line = get_tv_lnum(argvars);
10683 col = get_tv_number_chk(&argvars[1], NULL);
10684#ifdef FEAT_VIRTUALEDIT
10685 if (argvars[2].v_type != VAR_UNKNOWN)
10686 coladd = get_tv_number_chk(&argvars[2], NULL);
10687#endif
10688 }
10689 if (line < 0 || col < 0
10690#ifdef FEAT_VIRTUALEDIT
10691 || coladd < 0
10692#endif
10693 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010694 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010695 if (line > 0)
10696 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010697 if (col > 0)
10698 curwin->w_cursor.col = col - 1;
10699#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010700 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010701#endif
10702
10703 /* Make sure the cursor is in a valid position. */
10704 check_cursor();
10705#ifdef FEAT_MBYTE
10706 /* Correct cursor for multi-byte character. */
10707 if (has_mbyte)
10708 mb_adjust_cursor();
10709#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010710
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010711 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010712 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010713}
10714
10715/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010716 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010717 */
10718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010719f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010720{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010721 int noref = 0;
10722
10723 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010724 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010725 if (noref < 0 || noref > 1)
10726 EMSG(_(e_invarg));
10727 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010728 {
10729 current_copyID += COPYID_INC;
10730 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10731 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010732}
10733
10734/*
10735 * "delete()" function
10736 */
10737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010738f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010739{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010740 char_u nbuf[NUMBUFLEN];
10741 char_u *name;
10742 char_u *flags;
10743
10744 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010745 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010746 return;
10747
10748 name = get_tv_string(&argvars[0]);
10749 if (name == NULL || *name == NUL)
10750 {
10751 EMSG(_(e_invarg));
10752 return;
10753 }
10754
10755 if (argvars[1].v_type != VAR_UNKNOWN)
10756 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010757 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010758 flags = (char_u *)"";
10759
10760 if (*flags == NUL)
10761 /* delete a file */
10762 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10763 else if (STRCMP(flags, "d") == 0)
10764 /* delete an empty directory */
10765 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10766 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010767 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010768 rettv->vval.v_number = delete_recursive(name);
10769 else
10770 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010771}
10772
10773/*
10774 * "did_filetype()" function
10775 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010776 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010777f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010778{
10779#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010780 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010781#endif
10782}
10783
10784/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010785 * "diff_filler()" function
10786 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010787 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010788f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010789{
10790#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010791 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010792#endif
10793}
10794
10795/*
10796 * "diff_hlID()" function
10797 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010800{
10801#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010802 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010803 static linenr_T prev_lnum = 0;
10804 static int changedtick = 0;
10805 static int fnum = 0;
10806 static int change_start = 0;
10807 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010808 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010809 int filler_lines;
10810 int col;
10811
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010812 if (lnum < 0) /* ignore type error in {lnum} arg */
10813 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010814 if (lnum != prev_lnum
10815 || changedtick != curbuf->b_changedtick
10816 || fnum != curbuf->b_fnum)
10817 {
10818 /* New line, buffer, change: need to get the values. */
10819 filler_lines = diff_check(curwin, lnum);
10820 if (filler_lines < 0)
10821 {
10822 if (filler_lines == -1)
10823 {
10824 change_start = MAXCOL;
10825 change_end = -1;
10826 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10827 hlID = HLF_ADD; /* added line */
10828 else
10829 hlID = HLF_CHD; /* changed line */
10830 }
10831 else
10832 hlID = HLF_ADD; /* added line */
10833 }
10834 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010835 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010836 prev_lnum = lnum;
10837 changedtick = curbuf->b_changedtick;
10838 fnum = curbuf->b_fnum;
10839 }
10840
10841 if (hlID == HLF_CHD || hlID == HLF_TXD)
10842 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010843 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010844 if (col >= change_start && col <= change_end)
10845 hlID = HLF_TXD; /* changed text */
10846 else
10847 hlID = HLF_CHD; /* changed line */
10848 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010849 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010850#endif
10851}
10852
10853/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010854 * "disable_char_avail_for_testing({expr})" function
10855 */
10856 static void
10857f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10858{
10859 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10860}
10861
10862/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010863 * "empty({expr})" function
10864 */
10865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010866f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010867{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010868 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010869
10870 switch (argvars[0].v_type)
10871 {
10872 case VAR_STRING:
10873 case VAR_FUNC:
10874 n = argvars[0].vval.v_string == NULL
10875 || *argvars[0].vval.v_string == NUL;
10876 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010877 case VAR_PARTIAL:
10878 n = FALSE;
10879 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010880 case VAR_NUMBER:
10881 n = argvars[0].vval.v_number == 0;
10882 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010883 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010884#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010885 n = argvars[0].vval.v_float == 0.0;
10886 break;
10887#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010888 case VAR_LIST:
10889 n = argvars[0].vval.v_list == NULL
10890 || argvars[0].vval.v_list->lv_first == NULL;
10891 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010892 case VAR_DICT:
10893 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010894 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010895 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010896 case VAR_SPECIAL:
10897 n = argvars[0].vval.v_number != VVAL_TRUE;
10898 break;
10899
Bram Moolenaar835dc632016-02-07 14:27:38 +010010900 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010901#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010902 n = argvars[0].vval.v_job == NULL
10903 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10904 break;
10905#endif
10906 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010907#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010908 n = argvars[0].vval.v_channel == NULL
10909 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010910 break;
10911#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010912 case VAR_UNKNOWN:
10913 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10914 n = TRUE;
10915 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010916 }
10917
10918 rettv->vval.v_number = n;
10919}
10920
10921/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010922 * "escape({string}, {chars})" function
10923 */
10924 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010925f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926{
10927 char_u buf[NUMBUFLEN];
10928
Bram Moolenaar758711c2005-02-02 23:11:38 +000010929 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10930 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010931 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010932}
10933
10934/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010935 * "eval()" function
10936 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010938f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010939{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010940 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010941
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010942 s = get_tv_string_chk(&argvars[0]);
10943 if (s != NULL)
10944 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010945
Bram Moolenaar615b9972015-01-14 17:15:05 +010010946 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010947 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10948 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010949 if (p != NULL && !aborting())
10950 EMSG2(_(e_invexpr2), p);
10951 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010952 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010953 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010954 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010955 else if (*s != NUL)
10956 EMSG(_(e_trailing));
10957}
10958
10959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010960 * "eventhandler()" function
10961 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010962 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010963f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010965 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010966}
10967
10968/*
10969 * "executable()" function
10970 */
10971 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010972f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010973{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010974 char_u *name = get_tv_string(&argvars[0]);
10975
10976 /* Check in $PATH and also check directly if there is a directory name. */
10977 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10978 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010979}
10980
10981/*
10982 * "exepath()" function
10983 */
10984 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010985f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010986{
10987 char_u *p = NULL;
10988
Bram Moolenaarb5971142015-03-21 17:32:19 +010010989 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010990 rettv->v_type = VAR_STRING;
10991 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010992}
10993
10994/*
10995 * "exists()" function
10996 */
10997 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010998f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010999{
11000 char_u *p;
11001 char_u *name;
11002 int n = FALSE;
11003 int len = 0;
11004
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011005 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011006 if (*p == '$') /* environment variable */
11007 {
11008 /* first try "normal" environment variables (fast) */
11009 if (mch_getenv(p + 1) != NULL)
11010 n = TRUE;
11011 else
11012 {
11013 /* try expanding things like $VIM and ${HOME} */
11014 p = expand_env_save(p);
11015 if (p != NULL && *p != '$')
11016 n = TRUE;
11017 vim_free(p);
11018 }
11019 }
11020 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000011021 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011022 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000011023 if (*skipwhite(p) != NUL)
11024 n = FALSE; /* trailing garbage */
11025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011026 else if (*p == '*') /* internal or user defined function */
11027 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011028 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011029 }
11030 else if (*p == ':')
11031 {
11032 n = cmd_exists(p + 1);
11033 }
11034 else if (*p == '#')
11035 {
11036#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000011037 if (p[1] == '#')
11038 n = autocmd_supported(p + 2);
11039 else
11040 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011041#endif
11042 }
11043 else /* internal variable */
11044 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011045 char_u *tofree;
11046 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011047
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011048 /* get_name_len() takes care of expanding curly braces */
11049 name = p;
11050 len = get_name_len(&p, &tofree, TRUE, FALSE);
11051 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011052 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011053 if (tofree != NULL)
11054 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020011055 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011056 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011057 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011058 /* handle d.key, l[idx], f(expr) */
11059 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
11060 if (n)
11061 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011062 }
11063 }
Bram Moolenaar79783442006-05-05 21:18:03 +000011064 if (*p != NUL)
11065 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011066
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000011067 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011068 }
11069
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011070 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011071}
11072
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011073#ifdef FEAT_FLOAT
11074/*
11075 * "exp()" function
11076 */
11077 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011078f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011079{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011080 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011081
11082 rettv->v_type = VAR_FLOAT;
11083 if (get_float_arg(argvars, &f) == OK)
11084 rettv->vval.v_float = exp(f);
11085 else
11086 rettv->vval.v_float = 0.0;
11087}
11088#endif
11089
Bram Moolenaar071d4272004-06-13 20:20:40 +000011090/*
11091 * "expand()" function
11092 */
11093 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011094f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011095{
11096 char_u *s;
11097 int len;
11098 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011099 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011100 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011101 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011102 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011103
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011104 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011105 if (argvars[1].v_type != VAR_UNKNOWN
11106 && argvars[2].v_type != VAR_UNKNOWN
11107 && get_tv_number_chk(&argvars[2], &error)
11108 && !error)
11109 {
11110 rettv->v_type = VAR_LIST;
11111 rettv->vval.v_list = NULL;
11112 }
11113
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011114 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011115 if (*s == '%' || *s == '#' || *s == '<')
11116 {
11117 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011118 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011119 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011120 if (rettv->v_type == VAR_LIST)
11121 {
11122 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
11123 list_append_string(rettv->vval.v_list, result, -1);
11124 else
11125 vim_free(result);
11126 }
11127 else
11128 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011129 }
11130 else
11131 {
11132 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011133 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011134 if (argvars[1].v_type != VAR_UNKNOWN
11135 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011136 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011137 if (!error)
11138 {
11139 ExpandInit(&xpc);
11140 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011141 if (p_wic)
11142 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011143 if (rettv->v_type == VAR_STRING)
11144 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11145 options, WILD_ALL);
11146 else if (rettv_list_alloc(rettv) != FAIL)
11147 {
11148 int i;
11149
11150 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11151 for (i = 0; i < xpc.xp_numfiles; i++)
11152 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11153 ExpandCleanup(&xpc);
11154 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011155 }
11156 else
11157 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011158 }
11159}
11160
11161/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011162 * Go over all entries in "d2" and add them to "d1".
11163 * When "action" is "error" then a duplicate key is an error.
11164 * When "action" is "force" then a duplicate key is overwritten.
11165 * Otherwise duplicate keys are ignored ("action" is "keep").
11166 */
11167 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011168dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011169{
11170 dictitem_T *di1;
11171 hashitem_T *hi2;
11172 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011173 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011174
11175 todo = (int)d2->dv_hashtab.ht_used;
11176 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11177 {
11178 if (!HASHITEM_EMPTY(hi2))
11179 {
11180 --todo;
11181 di1 = dict_find(d1, hi2->hi_key, -1);
11182 if (d1->dv_scope != 0)
11183 {
11184 /* Disallow replacing a builtin function in l: and g:.
11185 * Check the key to be valid when adding to any
11186 * scope. */
11187 if (d1->dv_scope == VAR_DEF_SCOPE
11188 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11189 && var_check_func_name(hi2->hi_key,
11190 di1 == NULL))
11191 break;
11192 if (!valid_varname(hi2->hi_key))
11193 break;
11194 }
11195 if (di1 == NULL)
11196 {
11197 di1 = dictitem_copy(HI2DI(hi2));
11198 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11199 dictitem_free(di1);
11200 }
11201 else if (*action == 'e')
11202 {
11203 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11204 break;
11205 }
11206 else if (*action == 'f' && HI2DI(hi2) != di1)
11207 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011208 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11209 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011210 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011211 clear_tv(&di1->di_tv);
11212 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11213 }
11214 }
11215 }
11216}
11217
11218/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011219 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011220 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011221 */
11222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011223f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011224{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011225 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011226
Bram Moolenaare9a41262005-01-15 22:18:47 +000011227 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011228 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011229 list_T *l1, *l2;
11230 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011231 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011232 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011233
Bram Moolenaare9a41262005-01-15 22:18:47 +000011234 l1 = argvars[0].vval.v_list;
11235 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011236 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011237 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011238 {
11239 if (argvars[2].v_type != VAR_UNKNOWN)
11240 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011241 before = get_tv_number_chk(&argvars[2], &error);
11242 if (error)
11243 return; /* type error; errmsg already given */
11244
Bram Moolenaar758711c2005-02-02 23:11:38 +000011245 if (before == l1->lv_len)
11246 item = NULL;
11247 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011248 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011249 item = list_find(l1, before);
11250 if (item == NULL)
11251 {
11252 EMSGN(_(e_listidx), before);
11253 return;
11254 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011255 }
11256 }
11257 else
11258 item = NULL;
11259 list_extend(l1, l2, item);
11260
Bram Moolenaare9a41262005-01-15 22:18:47 +000011261 copy_tv(&argvars[0], rettv);
11262 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011263 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011264 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11265 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011266 dict_T *d1, *d2;
11267 char_u *action;
11268 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011269
11270 d1 = argvars[0].vval.v_dict;
11271 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011272 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011273 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011274 {
11275 /* Check the third argument. */
11276 if (argvars[2].v_type != VAR_UNKNOWN)
11277 {
11278 static char *(av[]) = {"keep", "force", "error"};
11279
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011280 action = get_tv_string_chk(&argvars[2]);
11281 if (action == NULL)
11282 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011283 for (i = 0; i < 3; ++i)
11284 if (STRCMP(action, av[i]) == 0)
11285 break;
11286 if (i == 3)
11287 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011288 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011289 return;
11290 }
11291 }
11292 else
11293 action = (char_u *)"force";
11294
Bram Moolenaara9922d62013-05-30 13:01:18 +020011295 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011296
Bram Moolenaare9a41262005-01-15 22:18:47 +000011297 copy_tv(&argvars[0], rettv);
11298 }
11299 }
11300 else
11301 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011302}
11303
11304/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011305 * "feedkeys()" function
11306 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011307 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011308f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011309{
11310 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011311 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011312 char_u *keys, *flags;
11313 char_u nbuf[NUMBUFLEN];
11314 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011315 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011316 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011317
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011318 /* This is not allowed in the sandbox. If the commands would still be
11319 * executed in the sandbox it would be OK, but it probably happens later,
11320 * when "sandbox" is no longer set. */
11321 if (check_secure())
11322 return;
11323
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011324 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011325
11326 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011327 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011328 flags = get_tv_string_buf(&argvars[1], nbuf);
11329 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011330 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011331 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011332 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011333 case 'n': remap = FALSE; break;
11334 case 'm': remap = TRUE; break;
11335 case 't': typed = TRUE; break;
11336 case 'i': insert = TRUE; break;
11337 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011338 }
11339 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011340 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011341
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011342 if (*keys != NUL || execute)
11343 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011344 /* Need to escape K_SPECIAL and CSI before putting the string in the
11345 * typeahead buffer. */
11346 keys_esc = vim_strsave_escape_csi(keys);
11347 if (keys_esc != NULL)
11348 {
11349 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011350 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011351 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011352 if (vgetc_busy)
11353 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011354 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011355 {
11356 int save_msg_scroll = msg_scroll;
11357
11358 /* Avoid a 1 second delay when the keys start Insert mode. */
11359 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011360 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011361 msg_scroll |= save_msg_scroll;
11362 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011363 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011364 }
11365}
11366
11367/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011368 * "filereadable()" function
11369 */
11370 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011371f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011372{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011373 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011374 char_u *p;
11375 int n;
11376
Bram Moolenaarc236c162008-07-13 17:41:49 +000011377#ifndef O_NONBLOCK
11378# define O_NONBLOCK 0
11379#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011380 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011381 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11382 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011383 {
11384 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011385 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011386 }
11387 else
11388 n = FALSE;
11389
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011390 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011391}
11392
11393/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011394 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011395 * rights to write into.
11396 */
11397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011398f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011399{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011400 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011401}
11402
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011403 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011404findfilendir(
11405 typval_T *argvars UNUSED,
11406 typval_T *rettv,
11407 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011408{
11409#ifdef FEAT_SEARCHPATH
11410 char_u *fname;
11411 char_u *fresult = NULL;
11412 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11413 char_u *p;
11414 char_u pathbuf[NUMBUFLEN];
11415 int count = 1;
11416 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011417 int error = FALSE;
11418#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011419
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011420 rettv->vval.v_string = NULL;
11421 rettv->v_type = VAR_STRING;
11422
11423#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011424 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011425
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011426 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011427 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011428 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11429 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011430 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011431 else
11432 {
11433 if (*p != NUL)
11434 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011435
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011436 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011437 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011439 }
11440
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011441 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11442 error = TRUE;
11443
11444 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011445 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011446 do
11447 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011448 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011449 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011450 fresult = find_file_in_path_option(first ? fname : NULL,
11451 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011452 0, first, path,
11453 find_what,
11454 curbuf->b_ffname,
11455 find_what == FINDFILE_DIR
11456 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011457 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011458
11459 if (fresult != NULL && rettv->v_type == VAR_LIST)
11460 list_append_string(rettv->vval.v_list, fresult, -1);
11461
11462 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011463 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011464
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011465 if (rettv->v_type == VAR_STRING)
11466 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011467#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011468}
11469
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011470static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11471static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011472
11473/*
11474 * Implementation of map() and filter().
11475 */
11476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011477filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011478{
11479 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011480 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011481 listitem_T *li, *nli;
11482 list_T *l = NULL;
11483 dictitem_T *di;
11484 hashtab_T *ht;
11485 hashitem_T *hi;
11486 dict_T *d = NULL;
11487 typval_T save_val;
11488 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011490 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011491 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011492 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011493 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011494 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011495 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011496
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011498 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011499 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011500 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011501 return;
11502 }
11503 else if (argvars[0].v_type == VAR_DICT)
11504 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011505 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011506 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011507 return;
11508 }
11509 else
11510 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011511 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011512 return;
11513 }
11514
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011515 expr = get_tv_string_buf_chk(&argvars[1], buf);
11516 /* On type errors, the preceding call has already displayed an error
11517 * message. Avoid a misleading error message for an empty string that
11518 * was not passed as argument. */
11519 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011520 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011521 prepare_vimvar(VV_VAL, &save_val);
11522 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011523
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011524 /* We reset "did_emsg" to be able to detect whether an error
11525 * occurred during evaluation of the expression. */
11526 save_did_emsg = did_emsg;
11527 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011528
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011529 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011530 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011531 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011532 vimvars[VV_KEY].vv_type = VAR_STRING;
11533
11534 ht = &d->dv_hashtab;
11535 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011536 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011537 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011538 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011539 if (!HASHITEM_EMPTY(hi))
11540 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011541 int r;
11542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011543 --todo;
11544 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011545 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011546 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11547 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011548 break;
11549 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011550 r = filter_map_one(&di->di_tv, expr, map, &rem);
11551 clear_tv(&vimvars[VV_KEY].vv_tv);
11552 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011553 break;
11554 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011555 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011556 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11557 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011558 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011559 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011560 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011561 }
11562 }
11563 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011564 }
11565 else
11566 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011567 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11568
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011569 for (li = l->lv_first; li != NULL; li = nli)
11570 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011571 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011572 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011573 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011574 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011575 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011576 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011577 break;
11578 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011579 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011580 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011581 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011582 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011583
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011584 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011585 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011586
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011587 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011588 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011589
11590 copy_tv(&argvars[0], rettv);
11591}
11592
11593 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011594filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011595{
Bram Moolenaar33570922005-01-25 22:26:29 +000011596 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011597 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011598 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011599
Bram Moolenaar33570922005-01-25 22:26:29 +000011600 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011601 s = expr;
11602 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011603 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011604 if (*s != NUL) /* check for trailing chars after expr */
11605 {
11606 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011607 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011608 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011609 }
11610 if (map)
11611 {
11612 /* map(): replace the list item value */
11613 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011614 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011615 *tv = rettv;
11616 }
11617 else
11618 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011619 int error = FALSE;
11620
Bram Moolenaare9a41262005-01-15 22:18:47 +000011621 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011622 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011623 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011624 /* On type error, nothing has been removed; return FAIL to stop the
11625 * loop. The error message was given by get_tv_number_chk(). */
11626 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011627 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011628 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011629 retval = OK;
11630theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011631 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011632 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011633}
11634
11635/*
11636 * "filter()" function
11637 */
11638 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011639f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011640{
11641 filter_map(argvars, rettv, FALSE);
11642}
11643
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011644/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011645 * "finddir({fname}[, {path}[, {count}]])" function
11646 */
11647 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011648f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011649{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011650 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011651}
11652
11653/*
11654 * "findfile({fname}[, {path}[, {count}]])" function
11655 */
11656 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011657f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011658{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011659 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011660}
11661
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011662#ifdef FEAT_FLOAT
11663/*
11664 * "float2nr({float})" function
11665 */
11666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011667f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011668{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011669 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011670
11671 if (get_float_arg(argvars, &f) == OK)
11672 {
11673 if (f < -0x7fffffff)
11674 rettv->vval.v_number = -0x7fffffff;
11675 else if (f > 0x7fffffff)
11676 rettv->vval.v_number = 0x7fffffff;
11677 else
11678 rettv->vval.v_number = (varnumber_T)f;
11679 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011680}
11681
11682/*
11683 * "floor({float})" function
11684 */
11685 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011686f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011687{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011688 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011689
11690 rettv->v_type = VAR_FLOAT;
11691 if (get_float_arg(argvars, &f) == OK)
11692 rettv->vval.v_float = floor(f);
11693 else
11694 rettv->vval.v_float = 0.0;
11695}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011696
11697/*
11698 * "fmod()" function
11699 */
11700 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011701f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011702{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011703 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011704
11705 rettv->v_type = VAR_FLOAT;
11706 if (get_float_arg(argvars, &fx) == OK
11707 && get_float_arg(&argvars[1], &fy) == OK)
11708 rettv->vval.v_float = fmod(fx, fy);
11709 else
11710 rettv->vval.v_float = 0.0;
11711}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011712#endif
11713
Bram Moolenaar0d660222005-01-07 21:51:51 +000011714/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011715 * "fnameescape({string})" function
11716 */
11717 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011718f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011719{
11720 rettv->vval.v_string = vim_strsave_fnameescape(
11721 get_tv_string(&argvars[0]), FALSE);
11722 rettv->v_type = VAR_STRING;
11723}
11724
11725/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011726 * "fnamemodify({fname}, {mods})" function
11727 */
11728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011729f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730{
11731 char_u *fname;
11732 char_u *mods;
11733 int usedlen = 0;
11734 int len;
11735 char_u *fbuf = NULL;
11736 char_u buf[NUMBUFLEN];
11737
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011738 fname = get_tv_string_chk(&argvars[0]);
11739 mods = get_tv_string_buf_chk(&argvars[1], buf);
11740 if (fname == NULL || mods == NULL)
11741 fname = NULL;
11742 else
11743 {
11744 len = (int)STRLEN(fname);
11745 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11746 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011747
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011748 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011750 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011751 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011752 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011753 vim_free(fbuf);
11754}
11755
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011756static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011757
11758/*
11759 * "foldclosed()" function
11760 */
11761 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011762foldclosed_both(
11763 typval_T *argvars UNUSED,
11764 typval_T *rettv,
11765 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011766{
11767#ifdef FEAT_FOLDING
11768 linenr_T lnum;
11769 linenr_T first, last;
11770
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011771 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011772 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11773 {
11774 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11775 {
11776 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011777 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011778 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011779 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011780 return;
11781 }
11782 }
11783#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011784 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011785}
11786
11787/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011788 * "foldclosed()" function
11789 */
11790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011791f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011792{
11793 foldclosed_both(argvars, rettv, FALSE);
11794}
11795
11796/*
11797 * "foldclosedend()" function
11798 */
11799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011800f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011801{
11802 foldclosed_both(argvars, rettv, TRUE);
11803}
11804
11805/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011806 * "foldlevel()" function
11807 */
11808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011809f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011810{
11811#ifdef FEAT_FOLDING
11812 linenr_T lnum;
11813
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011814 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011815 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011816 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011817#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011818}
11819
11820/*
11821 * "foldtext()" function
11822 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011824f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011825{
11826#ifdef FEAT_FOLDING
11827 linenr_T lnum;
11828 char_u *s;
11829 char_u *r;
11830 int len;
11831 char *txt;
11832#endif
11833
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011834 rettv->v_type = VAR_STRING;
11835 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011836#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011837 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11838 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11839 <= curbuf->b_ml.ml_line_count
11840 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011841 {
11842 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011843 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11844 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011845 {
11846 if (!linewhite(lnum))
11847 break;
11848 ++lnum;
11849 }
11850
11851 /* Find interesting text in this line. */
11852 s = skipwhite(ml_get(lnum));
11853 /* skip C comment-start */
11854 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011856 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011857 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011858 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011859 {
11860 s = skipwhite(ml_get(lnum + 1));
11861 if (*s == '*')
11862 s = skipwhite(s + 1);
11863 }
11864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011865 txt = _("+-%s%3ld lines: ");
11866 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011867 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011868 + 20 /* for %3ld */
11869 + STRLEN(s))); /* concatenated */
11870 if (r != NULL)
11871 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011872 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11873 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11874 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011875 len = (int)STRLEN(r);
11876 STRCAT(r, s);
11877 /* remove 'foldmarker' and 'commentstring' */
11878 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011879 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011880 }
11881 }
11882#endif
11883}
11884
11885/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011886 * "foldtextresult(lnum)" function
11887 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011888 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011889f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011890{
11891#ifdef FEAT_FOLDING
11892 linenr_T lnum;
11893 char_u *text;
11894 char_u buf[51];
11895 foldinfo_T foldinfo;
11896 int fold_count;
11897#endif
11898
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011899 rettv->v_type = VAR_STRING;
11900 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011901#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011902 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011903 /* treat illegal types and illegal string values for {lnum} the same */
11904 if (lnum < 0)
11905 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011906 fold_count = foldedCount(curwin, lnum, &foldinfo);
11907 if (fold_count > 0)
11908 {
11909 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11910 &foldinfo, buf);
11911 if (text == buf)
11912 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011913 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011914 }
11915#endif
11916}
11917
11918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011919 * "foreground()" function
11920 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011921 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011922f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011923{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011924#ifdef FEAT_GUI
11925 if (gui.in_use)
11926 gui_mch_set_foreground();
11927#else
11928# ifdef WIN32
11929 win32_set_foreground();
11930# endif
11931#endif
11932}
11933
11934/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011935 * "function()" function
11936 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011938f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011939{
11940 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011941 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011942 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011943 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011944
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011945 if (argvars[0].v_type == VAR_FUNC)
11946 {
11947 /* function(MyFunc, [arg], dict) */
11948 s = argvars[0].vval.v_string;
11949 }
11950 else if (argvars[0].v_type == VAR_PARTIAL
11951 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011952 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011953 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011954 arg_pt = argvars[0].vval.v_partial;
11955 s = arg_pt->pt_name;
11956 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011957 else
11958 {
11959 /* function('MyFunc', [arg], dict) */
11960 s = get_tv_string(&argvars[0]);
11961 use_string = TRUE;
11962 }
11963
11964 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011965 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011966 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011967 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11968 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011969 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011970 else
11971 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011972 int dict_idx = 0;
11973 int arg_idx = 0;
11974 list_T *list = NULL;
11975
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011976 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011977 {
11978 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011979 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011980
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011981 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11982 * also be called from another script. Using trans_function_name()
11983 * would also work, but some plugins depend on the name being
11984 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011985 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011986 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11987 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011988 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011989 STRCPY(name, sid_buf);
11990 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011991 }
11992 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011993 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011994 name = vim_strsave(s);
11995
11996 if (argvars[1].v_type != VAR_UNKNOWN)
11997 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011998 if (argvars[2].v_type != VAR_UNKNOWN)
11999 {
12000 /* function(name, [args], dict) */
12001 arg_idx = 1;
12002 dict_idx = 2;
12003 }
12004 else if (argvars[1].v_type == VAR_DICT)
12005 /* function(name, dict) */
12006 dict_idx = 1;
12007 else
12008 /* function(name, [args]) */
12009 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010012010 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012011 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012012 if (argvars[dict_idx].v_type != VAR_DICT)
12013 {
12014 EMSG(_("E922: expected a dict"));
12015 vim_free(name);
12016 return;
12017 }
12018 if (argvars[dict_idx].vval.v_dict == NULL)
12019 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012020 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012021 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012022 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010012023 if (argvars[arg_idx].v_type != VAR_LIST)
12024 {
12025 EMSG(_("E923: Second argument of function() must be a list or a dict"));
12026 vim_free(name);
12027 return;
12028 }
12029 list = argvars[arg_idx].vval.v_list;
12030 if (list == NULL || list->lv_len == 0)
12031 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012032 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010012033 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012034 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010012035 {
12036 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012037
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012038 /* result is a VAR_PARTIAL */
Bram Moolenaar9f6154f2016-03-19 14:22:11 +010012039 if (pt == NULL)
12040 vim_free(name);
12041 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012042 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012043 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012044 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012045 listitem_T *li;
12046 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012047 int arg_len = 0;
12048 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012049
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012050 if (arg_pt != NULL)
12051 arg_len = arg_pt->pt_argc;
12052 if (list != NULL)
12053 lv_len = list->lv_len;
12054 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012055 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012056 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012057 if (pt->pt_argv == NULL)
12058 {
12059 vim_free(pt);
12060 vim_free(name);
12061 return;
12062 }
12063 else
12064 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012065 for (i = 0; i < arg_len; i++)
12066 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
12067 if (lv_len > 0)
12068 for (li = list->lv_first; li != NULL;
12069 li = li->li_next)
12070 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012071 }
12072 }
12073
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012074 /* For "function(dict.func, [], dict)" and "func" is a partial
12075 * use "dict". That is backwards compatible. */
12076 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012077 {
12078 pt->pt_dict = argvars[dict_idx].vval.v_dict;
12079 ++pt->pt_dict->dv_refcount;
12080 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012081 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012082 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012083 pt->pt_dict = arg_pt->pt_dict;
12084 if (pt->pt_dict != NULL)
12085 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010012086 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012087
12088 pt->pt_refcount = 1;
12089 pt->pt_name = name;
12090 func_ref(pt->pt_name);
12091 }
12092 rettv->v_type = VAR_PARTIAL;
12093 rettv->vval.v_partial = pt;
12094 }
12095 else
12096 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010012097 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010012098 rettv->v_type = VAR_FUNC;
12099 rettv->vval.v_string = name;
12100 func_ref(name);
12101 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012102 }
12103}
12104
12105/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012106 * "garbagecollect()" function
12107 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012108 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012109f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012110{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012111 /* This is postponed until we are back at the toplevel, because we may be
12112 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12113 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012114
12115 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12116 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012117}
12118
12119/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012120 * "get()" function
12121 */
12122 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012123f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012124{
Bram Moolenaar33570922005-01-25 22:26:29 +000012125 listitem_T *li;
12126 list_T *l;
12127 dictitem_T *di;
12128 dict_T *d;
12129 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012130
Bram Moolenaare9a41262005-01-15 22:18:47 +000012131 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012132 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012133 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012134 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012135 int error = FALSE;
12136
12137 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12138 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012139 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012140 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012141 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012142 else if (argvars[0].v_type == VAR_DICT)
12143 {
12144 if ((d = argvars[0].vval.v_dict) != NULL)
12145 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012146 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012147 if (di != NULL)
12148 tv = &di->di_tv;
12149 }
12150 }
12151 else
12152 EMSG2(_(e_listdictarg), "get()");
12153
12154 if (tv == NULL)
12155 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012156 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012157 copy_tv(&argvars[2], rettv);
12158 }
12159 else
12160 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012161}
12162
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012163static 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 +000012164
12165/*
12166 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012167 * Return a range (from start to end) of lines in rettv from the specified
12168 * buffer.
12169 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012170 */
12171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012172get_buffer_lines(
12173 buf_T *buf,
12174 linenr_T start,
12175 linenr_T end,
12176 int retlist,
12177 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012178{
12179 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012180
Bram Moolenaar959a1432013-12-14 12:17:38 +010012181 rettv->v_type = VAR_STRING;
12182 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012183 if (retlist && rettv_list_alloc(rettv) == FAIL)
12184 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012185
12186 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12187 return;
12188
12189 if (!retlist)
12190 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012191 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12192 p = ml_get_buf(buf, start, FALSE);
12193 else
12194 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012195 rettv->vval.v_string = vim_strsave(p);
12196 }
12197 else
12198 {
12199 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012200 return;
12201
12202 if (start < 1)
12203 start = 1;
12204 if (end > buf->b_ml.ml_line_count)
12205 end = buf->b_ml.ml_line_count;
12206 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012207 if (list_append_string(rettv->vval.v_list,
12208 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012209 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012210 }
12211}
12212
12213/*
12214 * "getbufline()" function
12215 */
12216 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012217f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012218{
12219 linenr_T lnum;
12220 linenr_T end;
12221 buf_T *buf;
12222
12223 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12224 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012225 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012226 --emsg_off;
12227
Bram Moolenaar661b1822005-07-28 22:36:45 +000012228 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012229 if (argvars[2].v_type == VAR_UNKNOWN)
12230 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012231 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012232 end = get_tv_lnum_buf(&argvars[2], buf);
12233
Bram Moolenaar342337a2005-07-21 21:11:17 +000012234 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012235}
12236
Bram Moolenaar0d660222005-01-07 21:51:51 +000012237/*
12238 * "getbufvar()" function
12239 */
12240 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012241f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012242{
12243 buf_T *buf;
12244 buf_T *save_curbuf;
12245 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012246 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012247 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012248
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012249 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12250 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012251 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012252 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012253
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012254 rettv->v_type = VAR_STRING;
12255 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012256
12257 if (buf != NULL && varname != NULL)
12258 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012259 /* set curbuf to be our buf, temporarily */
12260 save_curbuf = curbuf;
12261 curbuf = buf;
12262
Bram Moolenaar0d660222005-01-07 21:51:51 +000012263 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012264 {
12265 if (get_option_tv(&varname, rettv, TRUE) == OK)
12266 done = TRUE;
12267 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012268 else if (STRCMP(varname, "changedtick") == 0)
12269 {
12270 rettv->v_type = VAR_NUMBER;
12271 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012272 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012273 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012274 else
12275 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012276 /* Look up the variable. */
12277 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12278 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12279 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012280 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012281 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012282 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012283 done = TRUE;
12284 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012285 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012286
12287 /* restore previous notion of curbuf */
12288 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012289 }
12290
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012291 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12292 /* use the default value */
12293 copy_tv(&argvars[2], rettv);
12294
Bram Moolenaar0d660222005-01-07 21:51:51 +000012295 --emsg_off;
12296}
12297
12298/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299 * "getchar()" function
12300 */
12301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012302f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012303{
12304 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012305 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012306
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012307 /* Position the cursor. Needed after a message that ends in a space. */
12308 windgoto(msg_row, msg_col);
12309
Bram Moolenaar071d4272004-06-13 20:20:40 +000012310 ++no_mapping;
12311 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012312 for (;;)
12313 {
12314 if (argvars[0].v_type == VAR_UNKNOWN)
12315 /* getchar(): blocking wait. */
12316 n = safe_vgetc();
12317 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12318 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012319 n = vpeekc_any();
12320 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012321 /* illegal argument or getchar(0) and no char avail: return zero */
12322 n = 0;
12323 else
12324 /* getchar(0) and char avail: return char */
12325 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012326
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012327 if (n == K_IGNORE)
12328 continue;
12329 break;
12330 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012331 --no_mapping;
12332 --allow_keys;
12333
Bram Moolenaar219b8702006-11-01 14:32:36 +000012334 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12335 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12336 vimvars[VV_MOUSE_COL].vv_nr = 0;
12337
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012338 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012339 if (IS_SPECIAL(n) || mod_mask != 0)
12340 {
12341 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12342 int i = 0;
12343
12344 /* Turn a special key into three bytes, plus modifier. */
12345 if (mod_mask != 0)
12346 {
12347 temp[i++] = K_SPECIAL;
12348 temp[i++] = KS_MODIFIER;
12349 temp[i++] = mod_mask;
12350 }
12351 if (IS_SPECIAL(n))
12352 {
12353 temp[i++] = K_SPECIAL;
12354 temp[i++] = K_SECOND(n);
12355 temp[i++] = K_THIRD(n);
12356 }
12357#ifdef FEAT_MBYTE
12358 else if (has_mbyte)
12359 i += (*mb_char2bytes)(n, temp + i);
12360#endif
12361 else
12362 temp[i++] = n;
12363 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012364 rettv->v_type = VAR_STRING;
12365 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012366
12367#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012368 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012369 {
12370 int row = mouse_row;
12371 int col = mouse_col;
12372 win_T *win;
12373 linenr_T lnum;
12374# ifdef FEAT_WINDOWS
12375 win_T *wp;
12376# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012377 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012378
12379 if (row >= 0 && col >= 0)
12380 {
12381 /* Find the window at the mouse coordinates and compute the
12382 * text position. */
12383 win = mouse_find_win(&row, &col);
12384 (void)mouse_comp_pos(win, &row, &col, &lnum);
12385# ifdef FEAT_WINDOWS
12386 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012387 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012388# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012389 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012390 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12391 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12392 }
12393 }
12394#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012395 }
12396}
12397
12398/*
12399 * "getcharmod()" function
12400 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012401 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012402f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012403{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012404 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012405}
12406
12407/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012408 * "getcharsearch()" function
12409 */
12410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012411f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012412{
12413 if (rettv_dict_alloc(rettv) != FAIL)
12414 {
12415 dict_T *dict = rettv->vval.v_dict;
12416
12417 dict_add_nr_str(dict, "char", 0L, last_csearch());
12418 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12419 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12420 }
12421}
12422
12423/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012424 * "getcmdline()" function
12425 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012426 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012427f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012428{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012429 rettv->v_type = VAR_STRING;
12430 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012431}
12432
12433/*
12434 * "getcmdpos()" function
12435 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012437f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012438{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012439 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012440}
12441
12442/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012443 * "getcmdtype()" function
12444 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012445 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012446f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012447{
12448 rettv->v_type = VAR_STRING;
12449 rettv->vval.v_string = alloc(2);
12450 if (rettv->vval.v_string != NULL)
12451 {
12452 rettv->vval.v_string[0] = get_cmdline_type();
12453 rettv->vval.v_string[1] = NUL;
12454 }
12455}
12456
12457/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012458 * "getcmdwintype()" function
12459 */
12460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012461f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012462{
12463 rettv->v_type = VAR_STRING;
12464 rettv->vval.v_string = NULL;
12465#ifdef FEAT_CMDWIN
12466 rettv->vval.v_string = alloc(2);
12467 if (rettv->vval.v_string != NULL)
12468 {
12469 rettv->vval.v_string[0] = cmdwin_type;
12470 rettv->vval.v_string[1] = NUL;
12471 }
12472#endif
12473}
12474
12475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012476 * "getcwd()" function
12477 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012479f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012480{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012481 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012482 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012483
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012484 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012485 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012486
12487 wp = find_tabwin(&argvars[0], &argvars[1]);
12488 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012489 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012490 if (wp->w_localdir != NULL)
12491 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12492 else if(globaldir != NULL)
12493 rettv->vval.v_string = vim_strsave(globaldir);
12494 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012495 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012496 cwd = alloc(MAXPATHL);
12497 if (cwd != NULL)
12498 {
12499 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12500 rettv->vval.v_string = vim_strsave(cwd);
12501 vim_free(cwd);
12502 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012503 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012504#ifdef BACKSLASH_IN_FILENAME
12505 if (rettv->vval.v_string != NULL)
12506 slash_adjust(rettv->vval.v_string);
12507#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012508 }
12509}
12510
12511/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012512 * "getfontname()" function
12513 */
12514 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012515f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012516{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012517 rettv->v_type = VAR_STRING;
12518 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012519#ifdef FEAT_GUI
12520 if (gui.in_use)
12521 {
12522 GuiFont font;
12523 char_u *name = NULL;
12524
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012525 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012526 {
12527 /* Get the "Normal" font. Either the name saved by
12528 * hl_set_font_name() or from the font ID. */
12529 font = gui.norm_font;
12530 name = hl_get_font_name();
12531 }
12532 else
12533 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012535 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12536 return;
12537 font = gui_mch_get_font(name, FALSE);
12538 if (font == NOFONT)
12539 return; /* Invalid font name, return empty string. */
12540 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012541 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012542 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012543 gui_mch_free_font(font);
12544 }
12545#endif
12546}
12547
12548/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012549 * "getfperm({fname})" function
12550 */
12551 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012552f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012553{
12554 char_u *fname;
12555 struct stat st;
12556 char_u *perm = NULL;
12557 char_u flags[] = "rwx";
12558 int i;
12559
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012560 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012561
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012562 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012563 if (mch_stat((char *)fname, &st) >= 0)
12564 {
12565 perm = vim_strsave((char_u *)"---------");
12566 if (perm != NULL)
12567 {
12568 for (i = 0; i < 9; i++)
12569 {
12570 if (st.st_mode & (1 << (8 - i)))
12571 perm[i] = flags[i % 3];
12572 }
12573 }
12574 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012575 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012576}
12577
12578/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012579 * "getfsize({fname})" function
12580 */
12581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012582f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012583{
12584 char_u *fname;
12585 struct stat st;
12586
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012587 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012588
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012589 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012590
12591 if (mch_stat((char *)fname, &st) >= 0)
12592 {
12593 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012594 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012595 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012596 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012597 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012598
12599 /* non-perfect check for overflow */
12600 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12601 rettv->vval.v_number = -2;
12602 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012603 }
12604 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012605 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012606}
12607
12608/*
12609 * "getftime({fname})" function
12610 */
12611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012612f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012613{
12614 char_u *fname;
12615 struct stat st;
12616
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012617 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012618
12619 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012620 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012621 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012622 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012623}
12624
12625/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012626 * "getftype({fname})" function
12627 */
12628 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012629f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012630{
12631 char_u *fname;
12632 struct stat st;
12633 char_u *type = NULL;
12634 char *t;
12635
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012636 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012637
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012638 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012639 if (mch_lstat((char *)fname, &st) >= 0)
12640 {
12641#ifdef S_ISREG
12642 if (S_ISREG(st.st_mode))
12643 t = "file";
12644 else if (S_ISDIR(st.st_mode))
12645 t = "dir";
12646# ifdef S_ISLNK
12647 else if (S_ISLNK(st.st_mode))
12648 t = "link";
12649# endif
12650# ifdef S_ISBLK
12651 else if (S_ISBLK(st.st_mode))
12652 t = "bdev";
12653# endif
12654# ifdef S_ISCHR
12655 else if (S_ISCHR(st.st_mode))
12656 t = "cdev";
12657# endif
12658# ifdef S_ISFIFO
12659 else if (S_ISFIFO(st.st_mode))
12660 t = "fifo";
12661# endif
12662# ifdef S_ISSOCK
12663 else if (S_ISSOCK(st.st_mode))
12664 t = "fifo";
12665# endif
12666 else
12667 t = "other";
12668#else
12669# ifdef S_IFMT
12670 switch (st.st_mode & S_IFMT)
12671 {
12672 case S_IFREG: t = "file"; break;
12673 case S_IFDIR: t = "dir"; break;
12674# ifdef S_IFLNK
12675 case S_IFLNK: t = "link"; break;
12676# endif
12677# ifdef S_IFBLK
12678 case S_IFBLK: t = "bdev"; break;
12679# endif
12680# ifdef S_IFCHR
12681 case S_IFCHR: t = "cdev"; break;
12682# endif
12683# ifdef S_IFIFO
12684 case S_IFIFO: t = "fifo"; break;
12685# endif
12686# ifdef S_IFSOCK
12687 case S_IFSOCK: t = "socket"; break;
12688# endif
12689 default: t = "other";
12690 }
12691# else
12692 if (mch_isdir(fname))
12693 t = "dir";
12694 else
12695 t = "file";
12696# endif
12697#endif
12698 type = vim_strsave((char_u *)t);
12699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012700 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012701}
12702
12703/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012704 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012705 */
12706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012707f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012708{
12709 linenr_T lnum;
12710 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012711 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012712
12713 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012714 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012715 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012716 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012717 retlist = FALSE;
12718 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012719 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012720 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012721 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012722 retlist = TRUE;
12723 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012724
Bram Moolenaar342337a2005-07-21 21:11:17 +000012725 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012726}
12727
12728/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012729 * "getmatches()" function
12730 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012731 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012732f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012733{
12734#ifdef FEAT_SEARCH_EXTRA
12735 dict_T *dict;
12736 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012737 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012738
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012739 if (rettv_list_alloc(rettv) == OK)
12740 {
12741 while (cur != NULL)
12742 {
12743 dict = dict_alloc();
12744 if (dict == NULL)
12745 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012746 if (cur->match.regprog == NULL)
12747 {
12748 /* match added with matchaddpos() */
12749 for (i = 0; i < MAXPOSMATCH; ++i)
12750 {
12751 llpos_T *llpos;
12752 char buf[6];
12753 list_T *l;
12754
12755 llpos = &cur->pos.pos[i];
12756 if (llpos->lnum == 0)
12757 break;
12758 l = list_alloc();
12759 if (l == NULL)
12760 break;
12761 list_append_number(l, (varnumber_T)llpos->lnum);
12762 if (llpos->col > 0)
12763 {
12764 list_append_number(l, (varnumber_T)llpos->col);
12765 list_append_number(l, (varnumber_T)llpos->len);
12766 }
12767 sprintf(buf, "pos%d", i + 1);
12768 dict_add_list(dict, buf, l);
12769 }
12770 }
12771 else
12772 {
12773 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12774 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012775 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012776 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12777 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012778# ifdef FEAT_CONCEAL
12779 if (cur->conceal_char)
12780 {
12781 char_u buf[MB_MAXBYTES + 1];
12782
12783 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12784 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12785 }
12786# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012787 list_append_dict(rettv->vval.v_list, dict);
12788 cur = cur->next;
12789 }
12790 }
12791#endif
12792}
12793
12794/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012795 * "getpid()" function
12796 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012798f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012799{
12800 rettv->vval.v_number = mch_get_pid();
12801}
12802
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012803static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012804
12805/*
12806 * "getcurpos()" function
12807 */
12808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012809f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012810{
12811 getpos_both(argvars, rettv, TRUE);
12812}
12813
Bram Moolenaar18081e32008-02-20 19:11:07 +000012814/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012815 * "getpos(string)" function
12816 */
12817 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012818f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012819{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012820 getpos_both(argvars, rettv, FALSE);
12821}
12822
12823 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012824getpos_both(
12825 typval_T *argvars,
12826 typval_T *rettv,
12827 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012828{
Bram Moolenaara5525202006-03-02 22:52:09 +000012829 pos_T *fp;
12830 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012831 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012832
12833 if (rettv_list_alloc(rettv) == OK)
12834 {
12835 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012836 if (getcurpos)
12837 fp = &curwin->w_cursor;
12838 else
12839 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012840 if (fnum != -1)
12841 list_append_number(l, (varnumber_T)fnum);
12842 else
12843 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012844 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12845 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012846 list_append_number(l, (fp != NULL)
12847 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012848 : (varnumber_T)0);
12849 list_append_number(l,
12850#ifdef FEAT_VIRTUALEDIT
12851 (fp != NULL) ? (varnumber_T)fp->coladd :
12852#endif
12853 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012854 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012855 {
12856 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012857 list_append_number(l, curwin->w_curswant == MAXCOL ?
12858 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012859 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012860 }
12861 else
12862 rettv->vval.v_number = FALSE;
12863}
12864
12865/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012866 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012867 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012868 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012869f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012870{
12871#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012872 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012873#endif
12874
Bram Moolenaar2641f772005-03-25 21:58:17 +000012875#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012876 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012877 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012878 wp = NULL;
12879 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12880 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012881 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012882 if (wp == NULL)
12883 return;
12884 }
12885
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012886 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012887 }
12888#endif
12889}
12890
12891/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012892 * "getreg()" function
12893 */
12894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012895f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012896{
12897 char_u *strregname;
12898 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012899 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012900 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012901 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012902
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012903 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012904 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012905 strregname = get_tv_string_chk(&argvars[0]);
12906 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012907 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012908 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012909 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012910 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12911 return_list = get_tv_number_chk(&argvars[2], &error);
12912 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012913 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012914 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012915 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012916
12917 if (error)
12918 return;
12919
Bram Moolenaar071d4272004-06-13 20:20:40 +000012920 regname = (strregname == NULL ? '"' : *strregname);
12921 if (regname == 0)
12922 regname = '"';
12923
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012924 if (return_list)
12925 {
12926 rettv->v_type = VAR_LIST;
12927 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12928 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012929 if (rettv->vval.v_list != NULL)
12930 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012931 }
12932 else
12933 {
12934 rettv->v_type = VAR_STRING;
12935 rettv->vval.v_string = get_reg_contents(regname,
12936 arg2 ? GREG_EXPR_SRC : 0);
12937 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012938}
12939
12940/*
12941 * "getregtype()" function
12942 */
12943 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012944f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012945{
12946 char_u *strregname;
12947 int regname;
12948 char_u buf[NUMBUFLEN + 2];
12949 long reglen = 0;
12950
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012951 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012952 {
12953 strregname = get_tv_string_chk(&argvars[0]);
12954 if (strregname == NULL) /* type error; errmsg already given */
12955 {
12956 rettv->v_type = VAR_STRING;
12957 rettv->vval.v_string = NULL;
12958 return;
12959 }
12960 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012961 else
12962 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012963 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012964
12965 regname = (strregname == NULL ? '"' : *strregname);
12966 if (regname == 0)
12967 regname = '"';
12968
12969 buf[0] = NUL;
12970 buf[1] = NUL;
12971 switch (get_reg_type(regname, &reglen))
12972 {
12973 case MLINE: buf[0] = 'V'; break;
12974 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012975 case MBLOCK:
12976 buf[0] = Ctrl_V;
12977 sprintf((char *)buf + 1, "%ld", reglen + 1);
12978 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012979 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012980 rettv->v_type = VAR_STRING;
12981 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012982}
12983
12984/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012985 * "gettabvar()" function
12986 */
12987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012988f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012989{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012990 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012991 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012992 dictitem_T *v;
12993 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012994 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012995
12996 rettv->v_type = VAR_STRING;
12997 rettv->vval.v_string = NULL;
12998
12999 varname = get_tv_string_chk(&argvars[1]);
13000 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13001 if (tp != NULL && varname != NULL)
13002 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020013003 /* Set tp to be our tabpage, temporarily. Also set the window to the
13004 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020013005 if (switch_win(&oldcurwin, &oldtabpage,
13006 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013007 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013008 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013009 /* look up the variable */
13010 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
13011 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
13012 if (v != NULL)
13013 {
13014 copy_tv(&v->di_tv, rettv);
13015 done = TRUE;
13016 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013017 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020013018
13019 /* restore previous notion of curwin */
13020 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013021 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013022
13023 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010013024 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020013025}
13026
13027/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013028 * "gettabwinvar()" function
13029 */
13030 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013031f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013032{
13033 getwinvar(argvars, rettv, 1);
13034}
13035
13036/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013037 * "getwinposx()" function
13038 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013039 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013040f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013041{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013042 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013043#ifdef FEAT_GUI
13044 if (gui.in_use)
13045 {
13046 int x, y;
13047
13048 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013049 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013050 }
13051#endif
13052}
13053
13054/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010013055 * "win_findbuf()" function
13056 */
13057 static void
13058f_win_findbuf(typval_T *argvars, typval_T *rettv)
13059{
13060 if (rettv_list_alloc(rettv) != FAIL)
13061 win_findbuf(argvars, rettv->vval.v_list);
13062}
13063
13064/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010013065 * "win_getid()" function
13066 */
13067 static void
13068f_win_getid(typval_T *argvars, typval_T *rettv)
13069{
13070 rettv->vval.v_number = win_getid(argvars);
13071}
13072
13073/*
13074 * "win_gotoid()" function
13075 */
13076 static void
13077f_win_gotoid(typval_T *argvars, typval_T *rettv)
13078{
13079 rettv->vval.v_number = win_gotoid(argvars);
13080}
13081
13082/*
13083 * "win_id2tabwin()" function
13084 */
13085 static void
13086f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
13087{
13088 if (rettv_list_alloc(rettv) != FAIL)
13089 win_id2tabwin(argvars, rettv->vval.v_list);
13090}
13091
13092/*
13093 * "win_id2win()" function
13094 */
13095 static void
13096f_win_id2win(typval_T *argvars, typval_T *rettv)
13097{
13098 rettv->vval.v_number = win_id2win(argvars);
13099}
13100
13101/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013102 * "getwinposy()" function
13103 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013104 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013105f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013106{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013107 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108#ifdef FEAT_GUI
13109 if (gui.in_use)
13110 {
13111 int x, y;
13112
13113 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013114 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013115 }
13116#endif
13117}
13118
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013119/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013120 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013121 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013122 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013123find_win_by_nr(
13124 typval_T *vp,
13125 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013126{
13127#ifdef FEAT_WINDOWS
13128 win_T *wp;
13129#endif
13130 int nr;
13131
13132 nr = get_tv_number_chk(vp, NULL);
13133
13134#ifdef FEAT_WINDOWS
13135 if (nr < 0)
13136 return NULL;
13137 if (nr == 0)
13138 return curwin;
13139
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013140 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13141 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013142 if (--nr <= 0)
13143 break;
13144 return wp;
13145#else
13146 if (nr == 0 || nr == 1)
13147 return curwin;
13148 return NULL;
13149#endif
13150}
13151
Bram Moolenaar071d4272004-06-13 20:20:40 +000013152/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013153 * Find window specified by "wvp" in tabpage "tvp".
13154 */
13155 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013156find_tabwin(
13157 typval_T *wvp, /* VAR_UNKNOWN for current window */
13158 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013159{
13160 win_T *wp = NULL;
13161 tabpage_T *tp = NULL;
13162 long n;
13163
13164 if (wvp->v_type != VAR_UNKNOWN)
13165 {
13166 if (tvp->v_type != VAR_UNKNOWN)
13167 {
13168 n = get_tv_number(tvp);
13169 if (n >= 0)
13170 tp = find_tabpage(n);
13171 }
13172 else
13173 tp = curtab;
13174
13175 if (tp != NULL)
13176 wp = find_win_by_nr(wvp, tp);
13177 }
13178 else
13179 wp = curwin;
13180
13181 return wp;
13182}
13183
13184/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013185 * "getwinvar()" function
13186 */
13187 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013188f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013189{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013190 getwinvar(argvars, rettv, 0);
13191}
13192
13193/*
13194 * getwinvar() and gettabwinvar()
13195 */
13196 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013197getwinvar(
13198 typval_T *argvars,
13199 typval_T *rettv,
13200 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013201{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013202 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013203 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013204 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013205 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013206 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013207#ifdef FEAT_WINDOWS
13208 win_T *oldcurwin;
13209 tabpage_T *oldtabpage;
13210 int need_switch_win;
13211#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013212
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013213#ifdef FEAT_WINDOWS
13214 if (off == 1)
13215 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13216 else
13217 tp = curtab;
13218#endif
13219 win = find_win_by_nr(&argvars[off], tp);
13220 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013221 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013222
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013223 rettv->v_type = VAR_STRING;
13224 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225
13226 if (win != NULL && varname != NULL)
13227 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013228#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013229 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013230 * otherwise the window is not valid. Only do this when needed,
13231 * autocommands get blocked. */
13232 need_switch_win = !(tp == curtab && win == curwin);
13233 if (!need_switch_win
13234 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13235#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013236 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013237 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013238 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013239 if (get_option_tv(&varname, rettv, 1) == OK)
13240 done = TRUE;
13241 }
13242 else
13243 {
13244 /* Look up the variable. */
13245 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13246 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13247 varname, FALSE);
13248 if (v != NULL)
13249 {
13250 copy_tv(&v->di_tv, rettv);
13251 done = TRUE;
13252 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013253 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013254 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013255
Bram Moolenaarba117c22015-09-29 16:53:22 +020013256#ifdef FEAT_WINDOWS
13257 if (need_switch_win)
13258 /* restore previous notion of curwin */
13259 restore_win(oldcurwin, oldtabpage, TRUE);
13260#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013261 }
13262
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013263 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13264 /* use the default return value */
13265 copy_tv(&argvars[off + 2], rettv);
13266
Bram Moolenaar071d4272004-06-13 20:20:40 +000013267 --emsg_off;
13268}
13269
13270/*
13271 * "glob()" function
13272 */
13273 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013274f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013275{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013276 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013277 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013278 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013279
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013280 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013281 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013282 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013283 if (argvars[1].v_type != VAR_UNKNOWN)
13284 {
13285 if (get_tv_number_chk(&argvars[1], &error))
13286 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013287 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013288 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013289 if (get_tv_number_chk(&argvars[2], &error))
13290 {
13291 rettv->v_type = VAR_LIST;
13292 rettv->vval.v_list = NULL;
13293 }
13294 if (argvars[3].v_type != VAR_UNKNOWN
13295 && get_tv_number_chk(&argvars[3], &error))
13296 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013297 }
13298 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013299 if (!error)
13300 {
13301 ExpandInit(&xpc);
13302 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013303 if (p_wic)
13304 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013305 if (rettv->v_type == VAR_STRING)
13306 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013307 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013308 else if (rettv_list_alloc(rettv) != FAIL)
13309 {
13310 int i;
13311
13312 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13313 NULL, options, WILD_ALL_KEEP);
13314 for (i = 0; i < xpc.xp_numfiles; i++)
13315 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13316
13317 ExpandCleanup(&xpc);
13318 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013319 }
13320 else
13321 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013322}
13323
13324/*
13325 * "globpath()" function
13326 */
13327 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013328f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013329{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013330 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013331 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013332 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013333 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013334 garray_T ga;
13335 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013336
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013337 /* When the optional second argument is non-zero, don't remove matches
13338 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013339 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013340 if (argvars[2].v_type != VAR_UNKNOWN)
13341 {
13342 if (get_tv_number_chk(&argvars[2], &error))
13343 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013344 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013345 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013346 if (get_tv_number_chk(&argvars[3], &error))
13347 {
13348 rettv->v_type = VAR_LIST;
13349 rettv->vval.v_list = NULL;
13350 }
13351 if (argvars[4].v_type != VAR_UNKNOWN
13352 && get_tv_number_chk(&argvars[4], &error))
13353 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013354 }
13355 }
13356 if (file != NULL && !error)
13357 {
13358 ga_init2(&ga, (int)sizeof(char_u *), 10);
13359 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13360 if (rettv->v_type == VAR_STRING)
13361 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13362 else if (rettv_list_alloc(rettv) != FAIL)
13363 for (i = 0; i < ga.ga_len; ++i)
13364 list_append_string(rettv->vval.v_list,
13365 ((char_u **)(ga.ga_data))[i], -1);
13366 ga_clear_strings(&ga);
13367 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013368 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013369 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013370}
13371
13372/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013373 * "glob2regpat()" function
13374 */
13375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013376f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013377{
13378 char_u *pat = get_tv_string_chk(&argvars[0]);
13379
13380 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013381 rettv->vval.v_string = (pat == NULL)
13382 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013383}
13384
13385/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013386 * "has()" function
13387 */
13388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013389f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013390{
13391 int i;
13392 char_u *name;
13393 int n = FALSE;
13394 static char *(has_list[]) =
13395 {
13396#ifdef AMIGA
13397 "amiga",
13398# ifdef FEAT_ARP
13399 "arp",
13400# endif
13401#endif
13402#ifdef __BEOS__
13403 "beos",
13404#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013405#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013406 "mac",
13407#endif
13408#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013409 "macunix", /* built with 'darwin' enabled */
13410#endif
13411#if defined(__APPLE__) && __APPLE__ == 1
13412 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013413#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013414#ifdef __QNX__
13415 "qnx",
13416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013417#ifdef UNIX
13418 "unix",
13419#endif
13420#ifdef VMS
13421 "vms",
13422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013423#ifdef WIN32
13424 "win32",
13425#endif
13426#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13427 "win32unix",
13428#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013429#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013430 "win64",
13431#endif
13432#ifdef EBCDIC
13433 "ebcdic",
13434#endif
13435#ifndef CASE_INSENSITIVE_FILENAME
13436 "fname_case",
13437#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013438#ifdef HAVE_ACL
13439 "acl",
13440#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013441#ifdef FEAT_ARABIC
13442 "arabic",
13443#endif
13444#ifdef FEAT_AUTOCMD
13445 "autocmd",
13446#endif
13447#ifdef FEAT_BEVAL
13448 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013449# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13450 "balloon_multiline",
13451# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013452#endif
13453#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13454 "builtin_terms",
13455# ifdef ALL_BUILTIN_TCAPS
13456 "all_builtin_terms",
13457# endif
13458#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013459#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13460 || defined(FEAT_GUI_W32) \
13461 || defined(FEAT_GUI_MOTIF))
13462 "browsefilter",
13463#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013464#ifdef FEAT_BYTEOFF
13465 "byte_offset",
13466#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013467#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013468 "channel",
13469#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013470#ifdef FEAT_CINDENT
13471 "cindent",
13472#endif
13473#ifdef FEAT_CLIENTSERVER
13474 "clientserver",
13475#endif
13476#ifdef FEAT_CLIPBOARD
13477 "clipboard",
13478#endif
13479#ifdef FEAT_CMDL_COMPL
13480 "cmdline_compl",
13481#endif
13482#ifdef FEAT_CMDHIST
13483 "cmdline_hist",
13484#endif
13485#ifdef FEAT_COMMENTS
13486 "comments",
13487#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013488#ifdef FEAT_CONCEAL
13489 "conceal",
13490#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013491#ifdef FEAT_CRYPT
13492 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013493 "crypt-blowfish",
13494 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013495#endif
13496#ifdef FEAT_CSCOPE
13497 "cscope",
13498#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013499#ifdef FEAT_CURSORBIND
13500 "cursorbind",
13501#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013502#ifdef CURSOR_SHAPE
13503 "cursorshape",
13504#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013505#ifdef DEBUG
13506 "debug",
13507#endif
13508#ifdef FEAT_CON_DIALOG
13509 "dialog_con",
13510#endif
13511#ifdef FEAT_GUI_DIALOG
13512 "dialog_gui",
13513#endif
13514#ifdef FEAT_DIFF
13515 "diff",
13516#endif
13517#ifdef FEAT_DIGRAPHS
13518 "digraphs",
13519#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013520#ifdef FEAT_DIRECTX
13521 "directx",
13522#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013523#ifdef FEAT_DND
13524 "dnd",
13525#endif
13526#ifdef FEAT_EMACS_TAGS
13527 "emacs_tags",
13528#endif
13529 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013530 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531#ifdef FEAT_SEARCH_EXTRA
13532 "extra_search",
13533#endif
13534#ifdef FEAT_FKMAP
13535 "farsi",
13536#endif
13537#ifdef FEAT_SEARCHPATH
13538 "file_in_path",
13539#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013540#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013541 "filterpipe",
13542#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013543#ifdef FEAT_FIND_ID
13544 "find_in_path",
13545#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013546#ifdef FEAT_FLOAT
13547 "float",
13548#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013549#ifdef FEAT_FOLDING
13550 "folding",
13551#endif
13552#ifdef FEAT_FOOTER
13553 "footer",
13554#endif
13555#if !defined(USE_SYSTEM) && defined(UNIX)
13556 "fork",
13557#endif
13558#ifdef FEAT_GETTEXT
13559 "gettext",
13560#endif
13561#ifdef FEAT_GUI
13562 "gui",
13563#endif
13564#ifdef FEAT_GUI_ATHENA
13565# ifdef FEAT_GUI_NEXTAW
13566 "gui_neXtaw",
13567# else
13568 "gui_athena",
13569# endif
13570#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571#ifdef FEAT_GUI_GTK
13572 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013573# ifdef USE_GTK3
13574 "gui_gtk3",
13575# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013576 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013577# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013578#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013579#ifdef FEAT_GUI_GNOME
13580 "gui_gnome",
13581#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013582#ifdef FEAT_GUI_MAC
13583 "gui_mac",
13584#endif
13585#ifdef FEAT_GUI_MOTIF
13586 "gui_motif",
13587#endif
13588#ifdef FEAT_GUI_PHOTON
13589 "gui_photon",
13590#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013591#ifdef FEAT_GUI_W32
13592 "gui_win32",
13593#endif
13594#ifdef FEAT_HANGULIN
13595 "hangul_input",
13596#endif
13597#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13598 "iconv",
13599#endif
13600#ifdef FEAT_INS_EXPAND
13601 "insert_expand",
13602#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013603#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013604 "job",
13605#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013606#ifdef FEAT_JUMPLIST
13607 "jumplist",
13608#endif
13609#ifdef FEAT_KEYMAP
13610 "keymap",
13611#endif
13612#ifdef FEAT_LANGMAP
13613 "langmap",
13614#endif
13615#ifdef FEAT_LIBCALL
13616 "libcall",
13617#endif
13618#ifdef FEAT_LINEBREAK
13619 "linebreak",
13620#endif
13621#ifdef FEAT_LISP
13622 "lispindent",
13623#endif
13624#ifdef FEAT_LISTCMDS
13625 "listcmds",
13626#endif
13627#ifdef FEAT_LOCALMAP
13628 "localmap",
13629#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013630#ifdef FEAT_LUA
13631# ifndef DYNAMIC_LUA
13632 "lua",
13633# endif
13634#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013635#ifdef FEAT_MENU
13636 "menu",
13637#endif
13638#ifdef FEAT_SESSION
13639 "mksession",
13640#endif
13641#ifdef FEAT_MODIFY_FNAME
13642 "modify_fname",
13643#endif
13644#ifdef FEAT_MOUSE
13645 "mouse",
13646#endif
13647#ifdef FEAT_MOUSESHAPE
13648 "mouseshape",
13649#endif
13650#if defined(UNIX) || defined(VMS)
13651# ifdef FEAT_MOUSE_DEC
13652 "mouse_dec",
13653# endif
13654# ifdef FEAT_MOUSE_GPM
13655 "mouse_gpm",
13656# endif
13657# ifdef FEAT_MOUSE_JSB
13658 "mouse_jsbterm",
13659# endif
13660# ifdef FEAT_MOUSE_NET
13661 "mouse_netterm",
13662# endif
13663# ifdef FEAT_MOUSE_PTERM
13664 "mouse_pterm",
13665# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013666# ifdef FEAT_MOUSE_SGR
13667 "mouse_sgr",
13668# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013669# ifdef FEAT_SYSMOUSE
13670 "mouse_sysmouse",
13671# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013672# ifdef FEAT_MOUSE_URXVT
13673 "mouse_urxvt",
13674# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013675# ifdef FEAT_MOUSE_XTERM
13676 "mouse_xterm",
13677# endif
13678#endif
13679#ifdef FEAT_MBYTE
13680 "multi_byte",
13681#endif
13682#ifdef FEAT_MBYTE_IME
13683 "multi_byte_ime",
13684#endif
13685#ifdef FEAT_MULTI_LANG
13686 "multi_lang",
13687#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013688#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013689#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013690 "mzscheme",
13691#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013693#ifdef FEAT_OLE
13694 "ole",
13695#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013696 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013697#ifdef FEAT_PATH_EXTRA
13698 "path_extra",
13699#endif
13700#ifdef FEAT_PERL
13701#ifndef DYNAMIC_PERL
13702 "perl",
13703#endif
13704#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013705#ifdef FEAT_PERSISTENT_UNDO
13706 "persistent_undo",
13707#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013708#ifdef FEAT_PYTHON
13709#ifndef DYNAMIC_PYTHON
13710 "python",
13711#endif
13712#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013713#ifdef FEAT_PYTHON3
13714#ifndef DYNAMIC_PYTHON3
13715 "python3",
13716#endif
13717#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013718#ifdef FEAT_POSTSCRIPT
13719 "postscript",
13720#endif
13721#ifdef FEAT_PRINTER
13722 "printer",
13723#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013724#ifdef FEAT_PROFILE
13725 "profile",
13726#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013727#ifdef FEAT_RELTIME
13728 "reltime",
13729#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013730#ifdef FEAT_QUICKFIX
13731 "quickfix",
13732#endif
13733#ifdef FEAT_RIGHTLEFT
13734 "rightleft",
13735#endif
13736#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13737 "ruby",
13738#endif
13739#ifdef FEAT_SCROLLBIND
13740 "scrollbind",
13741#endif
13742#ifdef FEAT_CMDL_INFO
13743 "showcmd",
13744 "cmdline_info",
13745#endif
13746#ifdef FEAT_SIGNS
13747 "signs",
13748#endif
13749#ifdef FEAT_SMARTINDENT
13750 "smartindent",
13751#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013752#ifdef STARTUPTIME
13753 "startuptime",
13754#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013755#ifdef FEAT_STL_OPT
13756 "statusline",
13757#endif
13758#ifdef FEAT_SUN_WORKSHOP
13759 "sun_workshop",
13760#endif
13761#ifdef FEAT_NETBEANS_INTG
13762 "netbeans_intg",
13763#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013764#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013765 "spell",
13766#endif
13767#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013768 "syntax",
13769#endif
13770#if defined(USE_SYSTEM) || !defined(UNIX)
13771 "system",
13772#endif
13773#ifdef FEAT_TAG_BINS
13774 "tag_binary",
13775#endif
13776#ifdef FEAT_TAG_OLDSTATIC
13777 "tag_old_static",
13778#endif
13779#ifdef FEAT_TAG_ANYWHITE
13780 "tag_any_white",
13781#endif
13782#ifdef FEAT_TCL
13783# ifndef DYNAMIC_TCL
13784 "tcl",
13785# endif
13786#endif
13787#ifdef TERMINFO
13788 "terminfo",
13789#endif
13790#ifdef FEAT_TERMRESPONSE
13791 "termresponse",
13792#endif
13793#ifdef FEAT_TEXTOBJ
13794 "textobjects",
13795#endif
13796#ifdef HAVE_TGETENT
13797 "tgetent",
13798#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013799#ifdef FEAT_TIMERS
13800 "timers",
13801#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013802#ifdef FEAT_TITLE
13803 "title",
13804#endif
13805#ifdef FEAT_TOOLBAR
13806 "toolbar",
13807#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013808#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13809 "unnamedplus",
13810#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013811#ifdef FEAT_USR_CMDS
13812 "user-commands", /* was accidentally included in 5.4 */
13813 "user_commands",
13814#endif
13815#ifdef FEAT_VIMINFO
13816 "viminfo",
13817#endif
Bram Moolenaar44a2f922016-03-19 22:11:51 +010013818#ifdef FEAT_WINDOWS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013819 "vertsplit",
13820#endif
13821#ifdef FEAT_VIRTUALEDIT
13822 "virtualedit",
13823#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013824 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013825#ifdef FEAT_VISUALEXTRA
13826 "visualextra",
13827#endif
13828#ifdef FEAT_VREPLACE
13829 "vreplace",
13830#endif
13831#ifdef FEAT_WILDIGN
13832 "wildignore",
13833#endif
13834#ifdef FEAT_WILDMENU
13835 "wildmenu",
13836#endif
13837#ifdef FEAT_WINDOWS
13838 "windows",
13839#endif
13840#ifdef FEAT_WAK
13841 "winaltkeys",
13842#endif
13843#ifdef FEAT_WRITEBACKUP
13844 "writebackup",
13845#endif
13846#ifdef FEAT_XIM
13847 "xim",
13848#endif
13849#ifdef FEAT_XFONTSET
13850 "xfontset",
13851#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013852#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013853 "xpm",
13854 "xpm_w32", /* for backward compatibility */
13855#else
13856# if defined(HAVE_XPM)
13857 "xpm",
13858# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013859#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013860#ifdef USE_XSMP
13861 "xsmp",
13862#endif
13863#ifdef USE_XSMP_INTERACT
13864 "xsmp_interact",
13865#endif
13866#ifdef FEAT_XCLIPBOARD
13867 "xterm_clipboard",
13868#endif
13869#ifdef FEAT_XTERM_SAVE
13870 "xterm_save",
13871#endif
13872#if defined(UNIX) && defined(FEAT_X11)
13873 "X11",
13874#endif
13875 NULL
13876 };
13877
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013878 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013879 for (i = 0; has_list[i] != NULL; ++i)
13880 if (STRICMP(name, has_list[i]) == 0)
13881 {
13882 n = TRUE;
13883 break;
13884 }
13885
13886 if (n == FALSE)
13887 {
13888 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013889 {
13890 if (name[5] == '-'
Bram Moolenaar819821c2016-03-26 21:24:14 +010013891 && STRLEN(name) >= 11
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013892 && vim_isdigit(name[6])
13893 && vim_isdigit(name[8])
13894 && vim_isdigit(name[10]))
13895 {
13896 int major = atoi((char *)name + 6);
13897 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013898
13899 /* Expect "patch-9.9.01234". */
13900 n = (major < VIM_VERSION_MAJOR
13901 || (major == VIM_VERSION_MAJOR
13902 && (minor < VIM_VERSION_MINOR
13903 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013904 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013905 }
13906 else
13907 n = has_patch(atoi((char *)name + 5));
13908 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013909 else if (STRICMP(name, "vim_starting") == 0)
13910 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013911#ifdef FEAT_MBYTE
13912 else if (STRICMP(name, "multi_byte_encoding") == 0)
13913 n = has_mbyte;
13914#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013915#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13916 else if (STRICMP(name, "balloon_multiline") == 0)
13917 n = multiline_balloon_available();
13918#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013919#ifdef DYNAMIC_TCL
13920 else if (STRICMP(name, "tcl") == 0)
13921 n = tcl_enabled(FALSE);
13922#endif
13923#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13924 else if (STRICMP(name, "iconv") == 0)
13925 n = iconv_enabled(FALSE);
13926#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013927#ifdef DYNAMIC_LUA
13928 else if (STRICMP(name, "lua") == 0)
13929 n = lua_enabled(FALSE);
13930#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013931#ifdef DYNAMIC_MZSCHEME
13932 else if (STRICMP(name, "mzscheme") == 0)
13933 n = mzscheme_enabled(FALSE);
13934#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935#ifdef DYNAMIC_RUBY
13936 else if (STRICMP(name, "ruby") == 0)
13937 n = ruby_enabled(FALSE);
13938#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013939#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940#ifdef DYNAMIC_PYTHON
13941 else if (STRICMP(name, "python") == 0)
13942 n = python_enabled(FALSE);
13943#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013944#endif
13945#ifdef FEAT_PYTHON3
13946#ifdef DYNAMIC_PYTHON3
13947 else if (STRICMP(name, "python3") == 0)
13948 n = python3_enabled(FALSE);
13949#endif
13950#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013951#ifdef DYNAMIC_PERL
13952 else if (STRICMP(name, "perl") == 0)
13953 n = perl_enabled(FALSE);
13954#endif
13955#ifdef FEAT_GUI
13956 else if (STRICMP(name, "gui_running") == 0)
13957 n = (gui.in_use || gui.starting);
13958# ifdef FEAT_GUI_W32
13959 else if (STRICMP(name, "gui_win32s") == 0)
13960 n = gui_is_win32s();
13961# endif
13962# ifdef FEAT_BROWSE
13963 else if (STRICMP(name, "browse") == 0)
13964 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13965# endif
13966#endif
13967#ifdef FEAT_SYN_HL
13968 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013969 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013970#endif
13971#if defined(WIN3264)
13972 else if (STRICMP(name, "win95") == 0)
13973 n = mch_windows95();
13974#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013975#ifdef FEAT_NETBEANS_INTG
13976 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013977 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013978#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979 }
13980
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013981 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013982}
13983
13984/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013985 * "has_key()" function
13986 */
13987 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013988f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013989{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013990 if (argvars[0].v_type != VAR_DICT)
13991 {
13992 EMSG(_(e_dictreq));
13993 return;
13994 }
13995 if (argvars[0].vval.v_dict == NULL)
13996 return;
13997
13998 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013999 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014000}
14001
14002/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014003 * "haslocaldir()" function
14004 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014006f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014007{
Bram Moolenaarc9703302016-01-17 21:49:33 +010014008 win_T *wp = NULL;
14009
14010 wp = find_tabwin(&argvars[0], &argvars[1]);
14011 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000014012}
14013
14014/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014015 * "hasmapto()" function
14016 */
14017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014018f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014019{
14020 char_u *name;
14021 char_u *mode;
14022 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000014023 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014026 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014027 mode = (char_u *)"nvo";
14028 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000014029 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014030 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000014031 if (argvars[2].v_type != VAR_UNKNOWN)
14032 abbr = get_tv_number(&argvars[2]);
14033 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014034
Bram Moolenaar2c932302006-03-18 21:42:09 +000014035 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014036 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014037 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014038 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014039}
14040
14041/*
14042 * "histadd()" function
14043 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014045f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046{
14047#ifdef FEAT_CMDHIST
14048 int histype;
14049 char_u *str;
14050 char_u buf[NUMBUFLEN];
14051#endif
14052
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014053 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014054 if (check_restricted() || check_secure())
14055 return;
14056#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014057 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14058 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014059 if (histype >= 0)
14060 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014061 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014062 if (*str != NUL)
14063 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000014064 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000014065 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014066 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 return;
14068 }
14069 }
14070#endif
14071}
14072
14073/*
14074 * "histdel()" function
14075 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014076 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014077f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014078{
14079#ifdef FEAT_CMDHIST
14080 int n;
14081 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014082 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014083
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014084 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14085 if (str == NULL)
14086 n = 0;
14087 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014088 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014089 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014090 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014091 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014092 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014093 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094 else
14095 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014096 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014097 get_tv_string_buf(&argvars[1], buf));
14098 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099#endif
14100}
14101
14102/*
14103 * "histget()" function
14104 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014106f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107{
14108#ifdef FEAT_CMDHIST
14109 int type;
14110 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014111 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014112
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014113 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14114 if (str == NULL)
14115 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014116 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014117 {
14118 type = get_histtype(str);
14119 if (argvars[1].v_type == VAR_UNKNOWN)
14120 idx = get_history_idx(type);
14121 else
14122 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14123 /* -1 on type error */
14124 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14125 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014126#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014127 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014128#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014129 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014130}
14131
14132/*
14133 * "histnr()" function
14134 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014135 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014136f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014137{
14138 int i;
14139
14140#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014141 char_u *history = get_tv_string_chk(&argvars[0]);
14142
14143 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014144 if (i >= HIST_CMD && i < HIST_COUNT)
14145 i = get_history_idx(i);
14146 else
14147#endif
14148 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014149 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014150}
14151
14152/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014153 * "highlightID(name)" function
14154 */
14155 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014156f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014157{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014158 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014159}
14160
14161/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014162 * "highlight_exists()" function
14163 */
14164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014165f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014166{
14167 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14168}
14169
14170/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014171 * "hostname()" function
14172 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014174f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014175{
14176 char_u hostname[256];
14177
14178 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014179 rettv->v_type = VAR_STRING;
14180 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014181}
14182
14183/*
14184 * iconv() function
14185 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014186 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014187f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
14189#ifdef FEAT_MBYTE
14190 char_u buf1[NUMBUFLEN];
14191 char_u buf2[NUMBUFLEN];
14192 char_u *from, *to, *str;
14193 vimconv_T vimconv;
14194#endif
14195
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014196 rettv->v_type = VAR_STRING;
14197 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014198
14199#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014200 str = get_tv_string(&argvars[0]);
14201 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14202 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014203 vimconv.vc_type = CONV_NONE;
14204 convert_setup(&vimconv, from, to);
14205
14206 /* If the encodings are equal, no conversion needed. */
14207 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014208 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014209 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014210 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014211
14212 convert_setup(&vimconv, NULL, NULL);
14213 vim_free(from);
14214 vim_free(to);
14215#endif
14216}
14217
14218/*
14219 * "indent()" function
14220 */
14221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014222f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014223{
14224 linenr_T lnum;
14225
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014226 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014227 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014228 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014229 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014230 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014231}
14232
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014233/*
14234 * "index()" function
14235 */
14236 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014237f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014238{
Bram Moolenaar33570922005-01-25 22:26:29 +000014239 list_T *l;
14240 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014241 long idx = 0;
14242 int ic = FALSE;
14243
14244 rettv->vval.v_number = -1;
14245 if (argvars[0].v_type != VAR_LIST)
14246 {
14247 EMSG(_(e_listreq));
14248 return;
14249 }
14250 l = argvars[0].vval.v_list;
14251 if (l != NULL)
14252 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014253 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014254 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014255 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 int error = FALSE;
14257
Bram Moolenaar758711c2005-02-02 23:11:38 +000014258 /* Start at specified item. Use the cached index that list_find()
14259 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014261 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014262 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014263 ic = get_tv_number_chk(&argvars[3], &error);
14264 if (error)
14265 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014266 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014267
Bram Moolenaar758711c2005-02-02 23:11:38 +000014268 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014269 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014270 {
14271 rettv->vval.v_number = idx;
14272 break;
14273 }
14274 }
14275}
14276
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277static int inputsecret_flag = 0;
14278
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014279static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014280
Bram Moolenaar071d4272004-06-13 20:20:40 +000014281/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014282 * This function is used by f_input() and f_inputdialog() functions. The third
14283 * argument to f_input() specifies the type of completion to use at the
14284 * prompt. The third argument to f_inputdialog() specifies the value to return
14285 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014286 */
14287 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014288get_user_input(
14289 typval_T *argvars,
14290 typval_T *rettv,
14291 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014292{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014293 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014294 char_u *p = NULL;
14295 int c;
14296 char_u buf[NUMBUFLEN];
14297 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014298 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014299 int xp_type = EXPAND_NOTHING;
14300 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014301
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014302 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014303 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014304
14305#ifdef NO_CONSOLE_INPUT
14306 /* While starting up, there is no place to enter text. */
14307 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014309#endif
14310
14311 cmd_silent = FALSE; /* Want to see the prompt. */
14312 if (prompt != NULL)
14313 {
14314 /* Only the part of the message after the last NL is considered as
14315 * prompt for the command line */
14316 p = vim_strrchr(prompt, '\n');
14317 if (p == NULL)
14318 p = prompt;
14319 else
14320 {
14321 ++p;
14322 c = *p;
14323 *p = NUL;
14324 msg_start();
14325 msg_clr_eos();
14326 msg_puts_attr(prompt, echo_attr);
14327 msg_didout = FALSE;
14328 msg_starthere();
14329 *p = c;
14330 }
14331 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014332
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014333 if (argvars[1].v_type != VAR_UNKNOWN)
14334 {
14335 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14336 if (defstr != NULL)
14337 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014338
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014339 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014340 {
14341 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014342 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014343 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014344
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014345 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014346 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014347
Bram Moolenaar4463f292005-09-25 22:20:24 +000014348 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14349 if (xp_name == NULL)
14350 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014351
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014352 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014353
Bram Moolenaar4463f292005-09-25 22:20:24 +000014354 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14355 &xp_arg) == FAIL)
14356 return;
14357 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014358 }
14359
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014360 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014361 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014362 int save_ex_normal_busy = ex_normal_busy;
14363 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014364 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014365 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14366 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014367 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014368 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014369 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014370 && argvars[1].v_type != VAR_UNKNOWN
14371 && argvars[2].v_type != VAR_UNKNOWN)
14372 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14373 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014374
14375 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014376
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014377 /* since the user typed this, no need to wait for return */
14378 need_wait_return = FALSE;
14379 msg_didout = FALSE;
14380 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014381 cmd_silent = cmd_silent_save;
14382}
14383
14384/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014385 * "input()" function
14386 * Also handles inputsecret() when inputsecret is set.
14387 */
14388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014389f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014390{
14391 get_user_input(argvars, rettv, FALSE);
14392}
14393
14394/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014395 * "inputdialog()" function
14396 */
14397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014398f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014399{
14400#if defined(FEAT_GUI_TEXTDIALOG)
14401 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14402 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14403 {
14404 char_u *message;
14405 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014406 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014407
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014408 message = get_tv_string_chk(&argvars[0]);
14409 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014410 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014411 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014412 else
14413 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014414 if (message != NULL && defstr != NULL
14415 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014416 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014417 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418 else
14419 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014420 if (message != NULL && defstr != NULL
14421 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014422 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014423 rettv->vval.v_string = vim_strsave(
14424 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014425 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014426 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014427 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014428 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014429 }
14430 else
14431#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014432 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014433}
14434
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014435/*
14436 * "inputlist()" function
14437 */
14438 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014439f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014440{
14441 listitem_T *li;
14442 int selected;
14443 int mouse_used;
14444
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014445#ifdef NO_CONSOLE_INPUT
14446 /* While starting up, there is no place to enter text. */
14447 if (no_console_input())
14448 return;
14449#endif
14450 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14451 {
14452 EMSG2(_(e_listarg), "inputlist()");
14453 return;
14454 }
14455
14456 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014457 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014458 lines_left = Rows; /* avoid more prompt */
14459 msg_scroll = TRUE;
14460 msg_clr_eos();
14461
14462 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14463 {
14464 msg_puts(get_tv_string(&li->li_tv));
14465 msg_putchar('\n');
14466 }
14467
14468 /* Ask for choice. */
14469 selected = prompt_for_number(&mouse_used);
14470 if (mouse_used)
14471 selected -= lines_left;
14472
14473 rettv->vval.v_number = selected;
14474}
14475
14476
Bram Moolenaar071d4272004-06-13 20:20:40 +000014477static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14478
14479/*
14480 * "inputrestore()" function
14481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014483f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014484{
14485 if (ga_userinput.ga_len > 0)
14486 {
14487 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014488 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14489 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014490 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014491 }
14492 else if (p_verbose > 1)
14493 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014494 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014495 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014496 }
14497}
14498
14499/*
14500 * "inputsave()" function
14501 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014503f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014504{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014505 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014506 if (ga_grow(&ga_userinput, 1) == OK)
14507 {
14508 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14509 + ga_userinput.ga_len);
14510 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014511 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014512 }
14513 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014514 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014515}
14516
14517/*
14518 * "inputsecret()" function
14519 */
14520 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014521f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014522{
14523 ++cmdline_star;
14524 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014525 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014526 --cmdline_star;
14527 --inputsecret_flag;
14528}
14529
14530/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014531 * "insert()" function
14532 */
14533 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014534f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014535{
14536 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014537 listitem_T *item;
14538 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014539 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014540
14541 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014542 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014543 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014544 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014545 {
14546 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014547 before = get_tv_number_chk(&argvars[2], &error);
14548 if (error)
14549 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014550
Bram Moolenaar758711c2005-02-02 23:11:38 +000014551 if (before == l->lv_len)
14552 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014553 else
14554 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014555 item = list_find(l, before);
14556 if (item == NULL)
14557 {
14558 EMSGN(_(e_listidx), before);
14559 l = NULL;
14560 }
14561 }
14562 if (l != NULL)
14563 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014564 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014565 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014566 }
14567 }
14568}
14569
14570/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014571 * "invert(expr)" function
14572 */
14573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014574f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014575{
14576 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14577}
14578
14579/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014580 * "isdirectory()" function
14581 */
14582 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014583f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014584{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014585 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014586}
14587
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014588/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014589 * "islocked()" function
14590 */
14591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014592f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014593{
14594 lval_T lv;
14595 char_u *end;
14596 dictitem_T *di;
14597
14598 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014599 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14600 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014601 if (end != NULL && lv.ll_name != NULL)
14602 {
14603 if (*end != NUL)
14604 EMSG(_(e_trailing));
14605 else
14606 {
14607 if (lv.ll_tv == NULL)
14608 {
14609 if (check_changedtick(lv.ll_name))
14610 rettv->vval.v_number = 1; /* always locked */
14611 else
14612 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014613 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014614 if (di != NULL)
14615 {
14616 /* Consider a variable locked when:
14617 * 1. the variable itself is locked
14618 * 2. the value of the variable is locked.
14619 * 3. the List or Dict value is locked.
14620 */
14621 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14622 || tv_islocked(&di->di_tv));
14623 }
14624 }
14625 }
14626 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014627 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014628 else if (lv.ll_newkey != NULL)
14629 EMSG2(_(e_dictkey), lv.ll_newkey);
14630 else if (lv.ll_list != NULL)
14631 /* List item. */
14632 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14633 else
14634 /* Dictionary item. */
14635 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14636 }
14637 }
14638
14639 clear_lval(&lv);
14640}
14641
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014642#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14643/*
14644 * "isnan()" function
14645 */
14646 static void
14647f_isnan(typval_T *argvars, typval_T *rettv)
14648{
14649 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14650 && isnan(argvars[0].vval.v_float);
14651}
14652#endif
14653
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014654static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014655
14656/*
14657 * Turn a dict into a list:
14658 * "what" == 0: list of keys
14659 * "what" == 1: list of values
14660 * "what" == 2: list of items
14661 */
14662 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014663dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014664{
Bram Moolenaar33570922005-01-25 22:26:29 +000014665 list_T *l2;
14666 dictitem_T *di;
14667 hashitem_T *hi;
14668 listitem_T *li;
14669 listitem_T *li2;
14670 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014671 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014672
Bram Moolenaar8c711452005-01-14 21:53:12 +000014673 if (argvars[0].v_type != VAR_DICT)
14674 {
14675 EMSG(_(e_dictreq));
14676 return;
14677 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014678 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014679 return;
14680
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014681 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014682 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014683
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014684 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014685 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014686 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014687 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014688 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014689 --todo;
14690 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014691
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014692 li = listitem_alloc();
14693 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014694 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014695 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014696
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014697 if (what == 0)
14698 {
14699 /* keys() */
14700 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014701 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014702 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14703 }
14704 else if (what == 1)
14705 {
14706 /* values() */
14707 copy_tv(&di->di_tv, &li->li_tv);
14708 }
14709 else
14710 {
14711 /* items() */
14712 l2 = list_alloc();
14713 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014714 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014715 li->li_tv.vval.v_list = l2;
14716 if (l2 == NULL)
14717 break;
14718 ++l2->lv_refcount;
14719
14720 li2 = listitem_alloc();
14721 if (li2 == NULL)
14722 break;
14723 list_append(l2, li2);
14724 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014725 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014726 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14727
14728 li2 = listitem_alloc();
14729 if (li2 == NULL)
14730 break;
14731 list_append(l2, li2);
14732 copy_tv(&di->di_tv, &li2->li_tv);
14733 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014734 }
14735 }
14736}
14737
14738/*
14739 * "items(dict)" function
14740 */
14741 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014742f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014743{
14744 dict_list(argvars, rettv, 2);
14745}
14746
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014747#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014748/*
14749 * Get the job from the argument.
14750 * Returns NULL if the job is invalid.
14751 */
14752 static job_T *
14753get_job_arg(typval_T *tv)
14754{
14755 job_T *job;
14756
14757 if (tv->v_type != VAR_JOB)
14758 {
14759 EMSG2(_(e_invarg2), get_tv_string(tv));
14760 return NULL;
14761 }
14762 job = tv->vval.v_job;
14763
14764 if (job == NULL)
14765 EMSG(_("E916: not a valid job"));
14766 return job;
14767}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014768
Bram Moolenaar835dc632016-02-07 14:27:38 +010014769/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014770 * "job_getchannel()" function
14771 */
14772 static void
14773f_job_getchannel(typval_T *argvars, typval_T *rettv)
14774{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014775 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014776
Bram Moolenaar65edff82016-02-21 16:40:11 +010014777 if (job != NULL)
14778 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014779 rettv->v_type = VAR_CHANNEL;
14780 rettv->vval.v_channel = job->jv_channel;
14781 if (job->jv_channel != NULL)
14782 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014783 }
14784}
14785
14786/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014787 * "job_info()" function
14788 */
14789 static void
14790f_job_info(typval_T *argvars, typval_T *rettv)
14791{
14792 job_T *job = get_job_arg(&argvars[0]);
14793
14794 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14795 job_info(job, rettv->vval.v_dict);
14796}
14797
14798/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014799 * "job_setoptions()" function
14800 */
14801 static void
14802f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14803{
14804 job_T *job = get_job_arg(&argvars[0]);
14805 jobopt_T opt;
14806
14807 if (job == NULL)
14808 return;
14809 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014810 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014811 return;
14812 job_set_options(job, &opt);
14813}
14814
14815/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014816 * "job_start()" function
14817 */
14818 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014819f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014820{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014821 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014822 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014823}
14824
14825/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014826 * "job_status()" function
14827 */
14828 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014829f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014830{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014831 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014832
Bram Moolenaar65edff82016-02-21 16:40:11 +010014833 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014834 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014835 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014836 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014837 }
14838}
14839
14840/*
14841 * "job_stop()" function
14842 */
14843 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014844f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014845{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014846 job_T *job = get_job_arg(&argvars[0]);
14847
14848 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014849 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014850}
14851#endif
14852
Bram Moolenaar071d4272004-06-13 20:20:40 +000014853/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014854 * "join()" function
14855 */
14856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014857f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014858{
14859 garray_T ga;
14860 char_u *sep;
14861
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014862 if (argvars[0].v_type != VAR_LIST)
14863 {
14864 EMSG(_(e_listreq));
14865 return;
14866 }
14867 if (argvars[0].vval.v_list == NULL)
14868 return;
14869 if (argvars[1].v_type == VAR_UNKNOWN)
14870 sep = (char_u *)" ";
14871 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014872 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014873
14874 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014875
14876 if (sep != NULL)
14877 {
14878 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014879 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014880 ga_append(&ga, NUL);
14881 rettv->vval.v_string = (char_u *)ga.ga_data;
14882 }
14883 else
14884 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014885}
14886
14887/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014888 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014889 */
14890 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014891f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014892{
14893 js_read_T reader;
14894
14895 reader.js_buf = get_tv_string(&argvars[0]);
14896 reader.js_fill = NULL;
14897 reader.js_used = 0;
14898 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14899 EMSG(_(e_invarg));
14900}
14901
14902/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014903 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014904 */
14905 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014906f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014907{
14908 rettv->v_type = VAR_STRING;
14909 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14910}
14911
14912/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014913 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014914 */
14915 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014916f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014917{
14918 js_read_T reader;
14919
14920 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014921 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014922 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014923 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014924 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014925}
14926
14927/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014928 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014929 */
14930 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014931f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014932{
14933 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014934 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014935}
14936
14937/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014938 * "keys()" function
14939 */
14940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014941f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014942{
14943 dict_list(argvars, rettv, 0);
14944}
14945
14946/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014947 * "last_buffer_nr()" function.
14948 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014950f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014951{
14952 int n = 0;
14953 buf_T *buf;
14954
14955 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14956 if (n < buf->b_fnum)
14957 n = buf->b_fnum;
14958
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014959 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014960}
14961
14962/*
14963 * "len()" function
14964 */
14965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014966f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014967{
14968 switch (argvars[0].v_type)
14969 {
14970 case VAR_STRING:
14971 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014972 rettv->vval.v_number = (varnumber_T)STRLEN(
14973 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014974 break;
14975 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014976 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014977 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014978 case VAR_DICT:
14979 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14980 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014981 case VAR_UNKNOWN:
14982 case VAR_SPECIAL:
14983 case VAR_FLOAT:
14984 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014985 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014986 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014987 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014988 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014989 break;
14990 }
14991}
14992
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014993static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014994
14995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014996libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014997{
14998#ifdef FEAT_LIBCALL
14999 char_u *string_in;
15000 char_u **string_result;
15001 int nr_result;
15002#endif
15003
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015004 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000015005 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015006 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015007
15008 if (check_restricted() || check_secure())
15009 return;
15010
15011#ifdef FEAT_LIBCALL
15012 /* The first two args must be strings, otherwise its meaningless */
15013 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
15014 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000015015 string_in = NULL;
15016 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015017 string_in = argvars[2].vval.v_string;
15018 if (type == VAR_NUMBER)
15019 string_result = NULL;
15020 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015021 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015022 if (mch_libcall(argvars[0].vval.v_string,
15023 argvars[1].vval.v_string,
15024 string_in,
15025 argvars[2].vval.v_number,
15026 string_result,
15027 &nr_result) == OK
15028 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015029 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015030 }
15031#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032}
15033
15034/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015035 * "libcall()" function
15036 */
15037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015038f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015039{
15040 libcall_common(argvars, rettv, VAR_STRING);
15041}
15042
15043/*
15044 * "libcallnr()" function
15045 */
15046 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015047f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015048{
15049 libcall_common(argvars, rettv, VAR_NUMBER);
15050}
15051
15052/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015053 * "line(string)" function
15054 */
15055 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015056f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015057{
15058 linenr_T lnum = 0;
15059 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015060 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015061
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015062 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 if (fp != NULL)
15064 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015065 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015066}
15067
15068/*
15069 * "line2byte(lnum)" function
15070 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015071 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015072f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015073{
15074#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015075 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015076#else
15077 linenr_T lnum;
15078
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015079 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015080 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015081 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015082 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015083 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
15084 if (rettv->vval.v_number >= 0)
15085 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015086#endif
15087}
15088
15089/*
15090 * "lispindent(lnum)" function
15091 */
15092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015093f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015094{
15095#ifdef FEAT_LISP
15096 pos_T pos;
15097 linenr_T lnum;
15098
15099 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015100 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015101 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
15102 {
15103 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015104 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015105 curwin->w_cursor = pos;
15106 }
15107 else
15108#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015109 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015110}
15111
15112/*
15113 * "localtime()" function
15114 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015115 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015116f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015117{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015118 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015119}
15120
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015121static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015122
15123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015124get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015125{
15126 char_u *keys;
15127 char_u *which;
15128 char_u buf[NUMBUFLEN];
15129 char_u *keys_buf = NULL;
15130 char_u *rhs;
15131 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015132 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015133 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015134 mapblock_T *mp;
15135 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015136
15137 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015138 rettv->v_type = VAR_STRING;
15139 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015140
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015141 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015142 if (*keys == NUL)
15143 return;
15144
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015145 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015146 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015147 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015148 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015149 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015150 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015151 if (argvars[3].v_type != VAR_UNKNOWN)
15152 get_dict = get_tv_number(&argvars[3]);
15153 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015154 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015155 else
15156 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015157 if (which == NULL)
15158 return;
15159
Bram Moolenaar071d4272004-06-13 20:20:40 +000015160 mode = get_map_mode(&which, 0);
15161
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015162 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015163 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015164 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015165
15166 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015167 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015168 /* Return a string. */
15169 if (rhs != NULL)
15170 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015171
Bram Moolenaarbd743252010-10-20 21:23:33 +020015172 }
15173 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15174 {
15175 /* Return a dictionary. */
15176 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15177 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15178 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015179
Bram Moolenaarbd743252010-10-20 21:23:33 +020015180 dict_add_nr_str(dict, "lhs", 0L, lhs);
15181 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15182 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15183 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15184 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15185 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15186 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015187 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015188 dict_add_nr_str(dict, "mode", 0L, mapmode);
15189
15190 vim_free(lhs);
15191 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015192 }
15193}
15194
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015195#ifdef FEAT_FLOAT
15196/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015197 * "log()" function
15198 */
15199 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015200f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015201{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015202 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015203
15204 rettv->v_type = VAR_FLOAT;
15205 if (get_float_arg(argvars, &f) == OK)
15206 rettv->vval.v_float = log(f);
15207 else
15208 rettv->vval.v_float = 0.0;
15209}
15210
15211/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015212 * "log10()" function
15213 */
15214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015215f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015216{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015217 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015218
15219 rettv->v_type = VAR_FLOAT;
15220 if (get_float_arg(argvars, &f) == OK)
15221 rettv->vval.v_float = log10(f);
15222 else
15223 rettv->vval.v_float = 0.0;
15224}
15225#endif
15226
Bram Moolenaar1dced572012-04-05 16:54:08 +020015227#ifdef FEAT_LUA
15228/*
15229 * "luaeval()" function
15230 */
15231 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015232f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015233{
15234 char_u *str;
15235 char_u buf[NUMBUFLEN];
15236
15237 str = get_tv_string_buf(&argvars[0], buf);
15238 do_luaeval(str, argvars + 1, rettv);
15239}
15240#endif
15241
Bram Moolenaar071d4272004-06-13 20:20:40 +000015242/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015243 * "map()" function
15244 */
15245 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015246f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015247{
15248 filter_map(argvars, rettv, TRUE);
15249}
15250
15251/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015252 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015253 */
15254 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015255f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015257 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015258}
15259
15260/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015261 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015262 */
15263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015264f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015265{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015266 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015267}
15268
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015269static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015270
15271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015272find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015273{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015274 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015275 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015277 char_u *pat;
15278 regmatch_T regmatch;
15279 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015280 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015281 char_u *save_cpo;
15282 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015283 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015284 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015285 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015286 list_T *l = NULL;
15287 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015288 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015289 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015290
15291 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15292 save_cpo = p_cpo;
15293 p_cpo = (char_u *)"";
15294
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015295 rettv->vval.v_number = -1;
15296 if (type == 3)
15297 {
15298 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015299 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015300 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015301 }
15302 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015303 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015304 rettv->v_type = VAR_STRING;
15305 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015306 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015307
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015308 if (argvars[0].v_type == VAR_LIST)
15309 {
15310 if ((l = argvars[0].vval.v_list) == NULL)
15311 goto theend;
15312 li = l->lv_first;
15313 }
15314 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015315 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015316 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015317 len = (long)STRLEN(str);
15318 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015319
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015320 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15321 if (pat == NULL)
15322 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015323
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015324 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015325 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015326 int error = FALSE;
15327
15328 start = get_tv_number_chk(&argvars[2], &error);
15329 if (error)
15330 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015331 if (l != NULL)
15332 {
15333 li = list_find(l, start);
15334 if (li == NULL)
15335 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015336 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015337 }
15338 else
15339 {
15340 if (start < 0)
15341 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015342 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015343 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015344 /* When "count" argument is there ignore matches before "start",
15345 * otherwise skip part of the string. Differs when pattern is "^"
15346 * or "\<". */
15347 if (argvars[3].v_type != VAR_UNKNOWN)
15348 startcol = start;
15349 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015350 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015351 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015352 len -= start;
15353 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015354 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015355
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015356 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015357 nth = get_tv_number_chk(&argvars[3], &error);
15358 if (error)
15359 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015360 }
15361
15362 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15363 if (regmatch.regprog != NULL)
15364 {
15365 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015366
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015367 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015368 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015369 if (l != NULL)
15370 {
15371 if (li == NULL)
15372 {
15373 match = FALSE;
15374 break;
15375 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015376 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015377 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015378 if (str == NULL)
15379 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015380 }
15381
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015382 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015383
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015384 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015385 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015386 if (l == NULL && !match)
15387 break;
15388
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015389 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015390 if (l != NULL)
15391 {
15392 li = li->li_next;
15393 ++idx;
15394 }
15395 else
15396 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015397#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015398 startcol = (colnr_T)(regmatch.startp[0]
15399 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015400#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015401 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015402#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015403 if (startcol > (colnr_T)len
15404 || str + startcol <= regmatch.startp[0])
15405 {
15406 match = FALSE;
15407 break;
15408 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015409 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015410 }
15411
15412 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015413 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015414 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015415 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015416 int i;
15417
15418 /* return list with matched string and submatches */
15419 for (i = 0; i < NSUBEXP; ++i)
15420 {
15421 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015422 {
15423 if (list_append_string(rettv->vval.v_list,
15424 (char_u *)"", 0) == FAIL)
15425 break;
15426 }
15427 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015428 regmatch.startp[i],
15429 (int)(regmatch.endp[i] - regmatch.startp[i]))
15430 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015431 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015432 }
15433 }
15434 else if (type == 2)
15435 {
15436 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015437 if (l != NULL)
15438 copy_tv(&li->li_tv, rettv);
15439 else
15440 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015441 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015442 }
15443 else if (l != NULL)
15444 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015445 else
15446 {
15447 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015448 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015449 (varnumber_T)(regmatch.startp[0] - str);
15450 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015451 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015452 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015453 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015454 }
15455 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015456 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015457 }
15458
15459theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015460 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015461 p_cpo = save_cpo;
15462}
15463
15464/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015465 * "match()" function
15466 */
15467 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015468f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015469{
15470 find_some_match(argvars, rettv, 1);
15471}
15472
15473/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015474 * "matchadd()" function
15475 */
15476 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015477f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015478{
15479#ifdef FEAT_SEARCH_EXTRA
15480 char_u buf[NUMBUFLEN];
15481 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15482 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15483 int prio = 10; /* default priority */
15484 int id = -1;
15485 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015486 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015487
15488 rettv->vval.v_number = -1;
15489
15490 if (grp == NULL || pat == NULL)
15491 return;
15492 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015493 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015494 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015495 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015496 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015497 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015498 if (argvars[4].v_type != VAR_UNKNOWN)
15499 {
15500 if (argvars[4].v_type != VAR_DICT)
15501 {
15502 EMSG(_(e_dictreq));
15503 return;
15504 }
15505 if (dict_find(argvars[4].vval.v_dict,
15506 (char_u *)"conceal", -1) != NULL)
15507 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15508 (char_u *)"conceal", FALSE);
15509 }
15510 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015511 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015512 if (error == TRUE)
15513 return;
15514 if (id >= 1 && id <= 3)
15515 {
15516 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15517 return;
15518 }
15519
Bram Moolenaar6561d522015-07-21 15:48:27 +020015520 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15521 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015522#endif
15523}
15524
15525/*
15526 * "matchaddpos()" function
15527 */
15528 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015529f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015530{
15531#ifdef FEAT_SEARCH_EXTRA
15532 char_u buf[NUMBUFLEN];
15533 char_u *group;
15534 int prio = 10;
15535 int id = -1;
15536 int error = FALSE;
15537 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015538 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015539
15540 rettv->vval.v_number = -1;
15541
15542 group = get_tv_string_buf_chk(&argvars[0], buf);
15543 if (group == NULL)
15544 return;
15545
15546 if (argvars[1].v_type != VAR_LIST)
15547 {
15548 EMSG2(_(e_listarg), "matchaddpos()");
15549 return;
15550 }
15551 l = argvars[1].vval.v_list;
15552 if (l == NULL)
15553 return;
15554
15555 if (argvars[2].v_type != VAR_UNKNOWN)
15556 {
15557 prio = get_tv_number_chk(&argvars[2], &error);
15558 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015559 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015560 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015561 if (argvars[4].v_type != VAR_UNKNOWN)
15562 {
15563 if (argvars[4].v_type != VAR_DICT)
15564 {
15565 EMSG(_(e_dictreq));
15566 return;
15567 }
15568 if (dict_find(argvars[4].vval.v_dict,
15569 (char_u *)"conceal", -1) != NULL)
15570 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15571 (char_u *)"conceal", FALSE);
15572 }
15573 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015574 }
15575 if (error == TRUE)
15576 return;
15577
15578 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15579 if (id == 1 || id == 2)
15580 {
15581 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15582 return;
15583 }
15584
Bram Moolenaar6561d522015-07-21 15:48:27 +020015585 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15586 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015587#endif
15588}
15589
15590/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015591 * "matcharg()" function
15592 */
15593 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015594f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015595{
15596 if (rettv_list_alloc(rettv) == OK)
15597 {
15598#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015599 int id = get_tv_number(&argvars[0]);
15600 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015601
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015602 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015603 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015604 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15605 {
15606 list_append_string(rettv->vval.v_list,
15607 syn_id2name(m->hlg_id), -1);
15608 list_append_string(rettv->vval.v_list, m->pattern, -1);
15609 }
15610 else
15611 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015612 list_append_string(rettv->vval.v_list, NULL, -1);
15613 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015614 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015615 }
15616#endif
15617 }
15618}
15619
15620/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015621 * "matchdelete()" function
15622 */
15623 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015624f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015625{
15626#ifdef FEAT_SEARCH_EXTRA
15627 rettv->vval.v_number = match_delete(curwin,
15628 (int)get_tv_number(&argvars[0]), TRUE);
15629#endif
15630}
15631
15632/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015633 * "matchend()" function
15634 */
15635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015636f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015637{
15638 find_some_match(argvars, rettv, 0);
15639}
15640
15641/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015642 * "matchlist()" function
15643 */
15644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015645f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015646{
15647 find_some_match(argvars, rettv, 3);
15648}
15649
15650/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015651 * "matchstr()" function
15652 */
15653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015654f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015655{
15656 find_some_match(argvars, rettv, 2);
15657}
15658
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015659static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015660
15661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015662max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015663{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015664 long n = 0;
15665 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015666 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015667
15668 if (argvars[0].v_type == VAR_LIST)
15669 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015670 list_T *l;
15671 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015672
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015673 l = argvars[0].vval.v_list;
15674 if (l != NULL)
15675 {
15676 li = l->lv_first;
15677 if (li != NULL)
15678 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015679 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015680 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015681 {
15682 li = li->li_next;
15683 if (li == NULL)
15684 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015685 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015686 if (domax ? i > n : i < n)
15687 n = i;
15688 }
15689 }
15690 }
15691 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015692 else if (argvars[0].v_type == VAR_DICT)
15693 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015694 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015695 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015696 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015697 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015698
15699 d = argvars[0].vval.v_dict;
15700 if (d != NULL)
15701 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015702 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015703 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015704 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015705 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015706 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015707 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015708 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015709 if (first)
15710 {
15711 n = i;
15712 first = FALSE;
15713 }
15714 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015715 n = i;
15716 }
15717 }
15718 }
15719 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015720 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015721 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015722 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015723}
15724
15725/*
15726 * "max()" function
15727 */
15728 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015729f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015730{
15731 max_min(argvars, rettv, TRUE);
15732}
15733
15734/*
15735 * "min()" function
15736 */
15737 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015738f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015739{
15740 max_min(argvars, rettv, FALSE);
15741}
15742
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015743static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015744
15745/*
15746 * Create the directory in which "dir" is located, and higher levels when
15747 * needed.
15748 */
15749 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015750mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015751{
15752 char_u *p;
15753 char_u *updir;
15754 int r = FAIL;
15755
15756 /* Get end of directory name in "dir".
15757 * We're done when it's "/" or "c:/". */
15758 p = gettail_sep(dir);
15759 if (p <= get_past_head(dir))
15760 return OK;
15761
15762 /* If the directory exists we're done. Otherwise: create it.*/
15763 updir = vim_strnsave(dir, (int)(p - dir));
15764 if (updir == NULL)
15765 return FAIL;
15766 if (mch_isdir(updir))
15767 r = OK;
15768 else if (mkdir_recurse(updir, prot) == OK)
15769 r = vim_mkdir_emsg(updir, prot);
15770 vim_free(updir);
15771 return r;
15772}
15773
15774#ifdef vim_mkdir
15775/*
15776 * "mkdir()" function
15777 */
15778 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015779f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015780{
15781 char_u *dir;
15782 char_u buf[NUMBUFLEN];
15783 int prot = 0755;
15784
15785 rettv->vval.v_number = FAIL;
15786 if (check_restricted() || check_secure())
15787 return;
15788
15789 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015790 if (*dir == NUL)
15791 rettv->vval.v_number = FAIL;
15792 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015793 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015794 if (*gettail(dir) == NUL)
15795 /* remove trailing slashes */
15796 *gettail_sep(dir) = NUL;
15797
15798 if (argvars[1].v_type != VAR_UNKNOWN)
15799 {
15800 if (argvars[2].v_type != VAR_UNKNOWN)
15801 prot = get_tv_number_chk(&argvars[2], NULL);
15802 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15803 mkdir_recurse(dir, prot);
15804 }
15805 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015806 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015807}
15808#endif
15809
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015811 * "mode()" function
15812 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015813 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015814f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015815{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015816 char_u buf[3];
15817
15818 buf[1] = NUL;
15819 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015820
Bram Moolenaar071d4272004-06-13 20:20:40 +000015821 if (VIsual_active)
15822 {
15823 if (VIsual_select)
15824 buf[0] = VIsual_mode + 's' - 'v';
15825 else
15826 buf[0] = VIsual_mode;
15827 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015828 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015829 || State == CONFIRM)
15830 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015832 if (State == ASKMORE)
15833 buf[1] = 'm';
15834 else if (State == CONFIRM)
15835 buf[1] = '?';
15836 }
15837 else if (State == EXTERNCMD)
15838 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015839 else if (State & INSERT)
15840 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015841#ifdef FEAT_VREPLACE
15842 if (State & VREPLACE_FLAG)
15843 {
15844 buf[0] = 'R';
15845 buf[1] = 'v';
15846 }
15847 else
15848#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015849 if (State & REPLACE_FLAG)
15850 buf[0] = 'R';
15851 else
15852 buf[0] = 'i';
15853 }
15854 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015855 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015856 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015857 if (exmode_active)
15858 buf[1] = 'v';
15859 }
15860 else if (exmode_active)
15861 {
15862 buf[0] = 'c';
15863 buf[1] = 'e';
15864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015865 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015866 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015867 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015868 if (finish_op)
15869 buf[1] = 'o';
15870 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015871
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015872 /* Clear out the minor mode when the argument is not a non-zero number or
15873 * non-empty string. */
15874 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015875 buf[1] = NUL;
15876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015877 rettv->vval.v_string = vim_strsave(buf);
15878 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015879}
15880
Bram Moolenaar429fa852013-04-15 12:27:36 +020015881#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015882/*
15883 * "mzeval()" function
15884 */
15885 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015886f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015887{
15888 char_u *str;
15889 char_u buf[NUMBUFLEN];
15890
15891 str = get_tv_string_buf(&argvars[0], buf);
15892 do_mzeval(str, rettv);
15893}
Bram Moolenaar75676462013-01-30 14:55:42 +010015894
15895 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015896mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015897{
15898 typval_T argvars[3];
15899
15900 argvars[0].v_type = VAR_STRING;
15901 argvars[0].vval.v_string = name;
15902 copy_tv(args, &argvars[1]);
15903 argvars[2].v_type = VAR_UNKNOWN;
15904 f_call(argvars, rettv);
15905 clear_tv(&argvars[1]);
15906}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015907#endif
15908
Bram Moolenaar071d4272004-06-13 20:20:40 +000015909/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015910 * "nextnonblank()" function
15911 */
15912 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015913f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015914{
15915 linenr_T lnum;
15916
15917 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15918 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015919 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015920 {
15921 lnum = 0;
15922 break;
15923 }
15924 if (*skipwhite(ml_get(lnum)) != NUL)
15925 break;
15926 }
15927 rettv->vval.v_number = lnum;
15928}
15929
15930/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015931 * "nr2char()" function
15932 */
15933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015934f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015935{
15936 char_u buf[NUMBUFLEN];
15937
15938#ifdef FEAT_MBYTE
15939 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015940 {
15941 int utf8 = 0;
15942
15943 if (argvars[1].v_type != VAR_UNKNOWN)
15944 utf8 = get_tv_number_chk(&argvars[1], NULL);
15945 if (utf8)
15946 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15947 else
15948 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15949 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015950 else
15951#endif
15952 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015953 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015954 buf[1] = NUL;
15955 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015956 rettv->v_type = VAR_STRING;
15957 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015958}
15959
15960/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015961 * "or(expr, expr)" function
15962 */
15963 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015964f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015965{
15966 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15967 | get_tv_number_chk(&argvars[1], NULL);
15968}
15969
15970/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015971 * "pathshorten()" function
15972 */
15973 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015974f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015975{
15976 char_u *p;
15977
15978 rettv->v_type = VAR_STRING;
15979 p = get_tv_string_chk(&argvars[0]);
15980 if (p == NULL)
15981 rettv->vval.v_string = NULL;
15982 else
15983 {
15984 p = vim_strsave(p);
15985 rettv->vval.v_string = p;
15986 if (p != NULL)
15987 shorten_dir(p);
15988 }
15989}
15990
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015991#ifdef FEAT_PERL
15992/*
15993 * "perleval()" function
15994 */
15995 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015996f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015997{
15998 char_u *str;
15999 char_u buf[NUMBUFLEN];
16000
16001 str = get_tv_string_buf(&argvars[0], buf);
16002 do_perleval(str, rettv);
16003}
16004#endif
16005
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016006#ifdef FEAT_FLOAT
16007/*
16008 * "pow()" function
16009 */
16010 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016011f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016012{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010016013 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016014
16015 rettv->v_type = VAR_FLOAT;
16016 if (get_float_arg(argvars, &fx) == OK
16017 && get_float_arg(&argvars[1], &fy) == OK)
16018 rettv->vval.v_float = pow(fx, fy);
16019 else
16020 rettv->vval.v_float = 0.0;
16021}
16022#endif
16023
Bram Moolenaar910f66f2006-04-05 20:41:53 +000016024/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016025 * "prevnonblank()" function
16026 */
16027 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016028f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016029{
16030 linenr_T lnum;
16031
16032 lnum = get_tv_lnum(argvars);
16033 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
16034 lnum = 0;
16035 else
16036 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
16037 --lnum;
16038 rettv->vval.v_number = lnum;
16039}
16040
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016041/* This dummy va_list is here because:
16042 * - passing a NULL pointer doesn't work when va_list isn't a pointer
16043 * - locally in the function results in a "used before set" warning
16044 * - using va_start() to initialize it gives "function with fixed args" error */
16045static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000016046
Bram Moolenaar8c711452005-01-14 21:53:12 +000016047/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016048 * "printf()" function
16049 */
16050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016051f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016052{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016053 char_u buf[NUMBUFLEN];
16054 int len;
16055 char_u *s;
16056 int saved_did_emsg = did_emsg;
16057 char *fmt;
16058
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016059 rettv->v_type = VAR_STRING;
16060 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016061
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016062 /* Get the required length, allocate the buffer and do it for real. */
16063 did_emsg = FALSE;
16064 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
16065 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
16066 if (!did_emsg)
16067 {
16068 s = alloc(len + 1);
16069 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016070 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016071 rettv->vval.v_string = s;
16072 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016073 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016074 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010016075 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000016076}
16077
16078/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016079 * "pumvisible()" function
16080 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016081 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016082f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016083{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016084#ifdef FEAT_INS_EXPAND
16085 if (pum_visible())
16086 rettv->vval.v_number = 1;
16087#endif
16088}
16089
Bram Moolenaardb913952012-06-29 12:54:53 +020016090#ifdef FEAT_PYTHON3
16091/*
16092 * "py3eval()" function
16093 */
16094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016095f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016096{
16097 char_u *str;
16098 char_u buf[NUMBUFLEN];
16099
16100 str = get_tv_string_buf(&argvars[0], buf);
16101 do_py3eval(str, rettv);
16102}
16103#endif
16104
16105#ifdef FEAT_PYTHON
16106/*
16107 * "pyeval()" function
16108 */
16109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016110f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016111{
16112 char_u *str;
16113 char_u buf[NUMBUFLEN];
16114
16115 str = get_tv_string_buf(&argvars[0], buf);
16116 do_pyeval(str, rettv);
16117}
16118#endif
16119
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016120/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016121 * "range()" function
16122 */
16123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016124f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016125{
16126 long start;
16127 long end;
16128 long stride = 1;
16129 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016130 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016131
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016132 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016133 if (argvars[1].v_type == VAR_UNKNOWN)
16134 {
16135 end = start - 1;
16136 start = 0;
16137 }
16138 else
16139 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016140 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016141 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016142 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016143 }
16144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016145 if (error)
16146 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016147 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016148 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016149 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016150 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016151 else
16152 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016153 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016154 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016155 if (list_append_number(rettv->vval.v_list,
16156 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016157 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016158 }
16159}
16160
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016161/*
16162 * "readfile()" function
16163 */
16164 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016165f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016166{
16167 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016168 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016169 char_u *fname;
16170 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16172 int io_size = sizeof(buf);
16173 int readlen; /* size of last fread() */
16174 char_u *prev = NULL; /* previously read bytes, if any */
16175 long prevlen = 0; /* length of data in prev */
16176 long prevsize = 0; /* size of prev buffer */
16177 long maxline = MAXLNUM;
16178 long cnt = 0;
16179 char_u *p; /* position in buf */
16180 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016181
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016182 if (argvars[1].v_type != VAR_UNKNOWN)
16183 {
16184 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16185 binary = TRUE;
16186 if (argvars[2].v_type != VAR_UNKNOWN)
16187 maxline = get_tv_number(&argvars[2]);
16188 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016189
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016190 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016191 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016192
16193 /* Always open the file in binary mode, library functions have a mind of
16194 * their own about CR-LF conversion. */
16195 fname = get_tv_string(&argvars[0]);
16196 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16197 {
16198 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16199 return;
16200 }
16201
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016202 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016203 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016204 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016205
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016206 /* This for loop processes what was read, but is also entered at end
16207 * of file so that either:
16208 * - an incomplete line gets written
16209 * - a "binary" file gets an empty line at the end if it ends in a
16210 * newline. */
16211 for (p = buf, start = buf;
16212 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16213 ++p)
16214 {
16215 if (*p == '\n' || readlen <= 0)
16216 {
16217 listitem_T *li;
16218 char_u *s = NULL;
16219 long_u len = p - start;
16220
16221 /* Finished a line. Remove CRs before NL. */
16222 if (readlen > 0 && !binary)
16223 {
16224 while (len > 0 && start[len - 1] == '\r')
16225 --len;
16226 /* removal may cross back to the "prev" string */
16227 if (len == 0)
16228 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16229 --prevlen;
16230 }
16231 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016232 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016233 else
16234 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016235 /* Change "prev" buffer to be the right size. This way
16236 * the bytes are only copied once, and very long lines are
16237 * allocated only once. */
16238 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016239 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016240 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016241 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016242 prev = NULL; /* the list will own the string */
16243 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016244 }
16245 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016246 if (s == NULL)
16247 {
16248 do_outofmem_msg((long_u) prevlen + len + 1);
16249 failed = TRUE;
16250 break;
16251 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016252
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016253 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016254 {
16255 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016256 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016257 break;
16258 }
16259 li->li_tv.v_type = VAR_STRING;
16260 li->li_tv.v_lock = 0;
16261 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016262 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016263
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016264 start = p + 1; /* step over newline */
16265 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016266 break;
16267 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016268 else if (*p == NUL)
16269 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016270#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016271 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16272 * when finding the BF and check the previous two bytes. */
16273 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016274 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016275 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16276 * + 1, these may be in the "prev" string. */
16277 char_u back1 = p >= buf + 1 ? p[-1]
16278 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16279 char_u back2 = p >= buf + 2 ? p[-2]
16280 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16281 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016282
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016283 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016284 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016285 char_u *dest = p - 2;
16286
16287 /* Usually a BOM is at the beginning of a file, and so at
16288 * the beginning of a line; then we can just step over it.
16289 */
16290 if (start == dest)
16291 start = p + 1;
16292 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016293 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016294 /* have to shuffle buf to close gap */
16295 int adjust_prevlen = 0;
16296
16297 if (dest < buf)
16298 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016299 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016300 dest = buf;
16301 }
16302 if (readlen > p - buf + 1)
16303 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16304 readlen -= 3 - adjust_prevlen;
16305 prevlen -= adjust_prevlen;
16306 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016307 }
16308 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016309 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016310#endif
16311 } /* for */
16312
16313 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16314 break;
16315 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016316 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016317 /* There's part of a line in buf, store it in "prev". */
16318 if (p - start + prevlen >= prevsize)
16319 {
16320 /* need bigger "prev" buffer */
16321 char_u *newprev;
16322
16323 /* A common use case is ordinary text files and "prev" gets a
16324 * fragment of a line, so the first allocation is made
16325 * small, to avoid repeatedly 'allocing' large and
16326 * 'reallocing' small. */
16327 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016328 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016329 else
16330 {
16331 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016332 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016333 prevsize = grow50pc > growmin ? grow50pc : growmin;
16334 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016335 newprev = prev == NULL ? alloc(prevsize)
16336 : vim_realloc(prev, prevsize);
16337 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016338 {
16339 do_outofmem_msg((long_u)prevsize);
16340 failed = TRUE;
16341 break;
16342 }
16343 prev = newprev;
16344 }
16345 /* Add the line part to end of "prev". */
16346 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016347 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016348 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016349 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016350
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016351 /*
16352 * For a negative line count use only the lines at the end of the file,
16353 * free the rest.
16354 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016355 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016356 while (cnt > -maxline)
16357 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016358 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016359 --cnt;
16360 }
16361
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016362 if (failed)
16363 {
16364 list_free(rettv->vval.v_list, TRUE);
16365 /* readfile doc says an empty list is returned on error */
16366 rettv->vval.v_list = list_alloc();
16367 }
16368
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016369 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016370 fclose(fd);
16371}
16372
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016373#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016374static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016375
16376/*
16377 * Convert a List to proftime_T.
16378 * Return FAIL when there is something wrong.
16379 */
16380 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016381list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016382{
16383 long n1, n2;
16384 int error = FALSE;
16385
16386 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16387 || arg->vval.v_list->lv_len != 2)
16388 return FAIL;
16389 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16390 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16391# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016392 tm->HighPart = n1;
16393 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016394# else
16395 tm->tv_sec = n1;
16396 tm->tv_usec = n2;
16397# endif
16398 return error ? FAIL : OK;
16399}
16400#endif /* FEAT_RELTIME */
16401
16402/*
16403 * "reltime()" function
16404 */
16405 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016406f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016407{
16408#ifdef FEAT_RELTIME
16409 proftime_T res;
16410 proftime_T start;
16411
16412 if (argvars[0].v_type == VAR_UNKNOWN)
16413 {
16414 /* No arguments: get current time. */
16415 profile_start(&res);
16416 }
16417 else if (argvars[1].v_type == VAR_UNKNOWN)
16418 {
16419 if (list2proftime(&argvars[0], &res) == FAIL)
16420 return;
16421 profile_end(&res);
16422 }
16423 else
16424 {
16425 /* Two arguments: compute the difference. */
16426 if (list2proftime(&argvars[0], &start) == FAIL
16427 || list2proftime(&argvars[1], &res) == FAIL)
16428 return;
16429 profile_sub(&res, &start);
16430 }
16431
16432 if (rettv_list_alloc(rettv) == OK)
16433 {
16434 long n1, n2;
16435
16436# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016437 n1 = res.HighPart;
16438 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016439# else
16440 n1 = res.tv_sec;
16441 n2 = res.tv_usec;
16442# endif
16443 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16444 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16445 }
16446#endif
16447}
16448
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016449#ifdef FEAT_FLOAT
16450/*
16451 * "reltimefloat()" function
16452 */
16453 static void
16454f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16455{
16456# ifdef FEAT_RELTIME
16457 proftime_T tm;
16458# endif
16459
16460 rettv->v_type = VAR_FLOAT;
16461 rettv->vval.v_float = 0;
16462# ifdef FEAT_RELTIME
16463 if (list2proftime(&argvars[0], &tm) == OK)
16464 rettv->vval.v_float = profile_float(&tm);
16465# endif
16466}
16467#endif
16468
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016469/*
16470 * "reltimestr()" function
16471 */
16472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016473f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016474{
16475#ifdef FEAT_RELTIME
16476 proftime_T tm;
16477#endif
16478
16479 rettv->v_type = VAR_STRING;
16480 rettv->vval.v_string = NULL;
16481#ifdef FEAT_RELTIME
16482 if (list2proftime(&argvars[0], &tm) == OK)
16483 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16484#endif
16485}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016486
Bram Moolenaar0d660222005-01-07 21:51:51 +000016487#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016488static void make_connection(void);
16489static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016490
16491 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016492make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016493{
16494 if (X_DISPLAY == NULL
16495# ifdef FEAT_GUI
16496 && !gui.in_use
16497# endif
16498 )
16499 {
16500 x_force_connect = TRUE;
16501 setup_term_clip();
16502 x_force_connect = FALSE;
16503 }
16504}
16505
16506 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016507check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016508{
16509 make_connection();
16510 if (X_DISPLAY == NULL)
16511 {
16512 EMSG(_("E240: No connection to Vim server"));
16513 return FAIL;
16514 }
16515 return OK;
16516}
16517#endif
16518
16519#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016520static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016521
16522 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016523remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016524{
16525 char_u *server_name;
16526 char_u *keys;
16527 char_u *r = NULL;
16528 char_u buf[NUMBUFLEN];
16529# ifdef WIN32
16530 HWND w;
16531# else
16532 Window w;
16533# endif
16534
16535 if (check_restricted() || check_secure())
16536 return;
16537
16538# ifdef FEAT_X11
16539 if (check_connection() == FAIL)
16540 return;
16541# endif
16542
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016543 server_name = get_tv_string_chk(&argvars[0]);
16544 if (server_name == NULL)
16545 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016546 keys = get_tv_string_buf(&argvars[1], buf);
16547# ifdef WIN32
16548 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16549# else
16550 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16551 < 0)
16552# endif
16553 {
16554 if (r != NULL)
16555 EMSG(r); /* sending worked but evaluation failed */
16556 else
16557 EMSG2(_("E241: Unable to send to %s"), server_name);
16558 return;
16559 }
16560
16561 rettv->vval.v_string = r;
16562
16563 if (argvars[2].v_type != VAR_UNKNOWN)
16564 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016565 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016566 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016567 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016568
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016569 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016570 v.di_tv.v_type = VAR_STRING;
16571 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016572 idvar = get_tv_string_chk(&argvars[2]);
16573 if (idvar != NULL)
16574 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016575 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016576 }
16577}
16578#endif
16579
16580/*
16581 * "remote_expr()" function
16582 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016584f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585{
16586 rettv->v_type = VAR_STRING;
16587 rettv->vval.v_string = NULL;
16588#ifdef FEAT_CLIENTSERVER
16589 remote_common(argvars, rettv, TRUE);
16590#endif
16591}
16592
16593/*
16594 * "remote_foreground()" function
16595 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016596 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016597f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016598{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016599#ifdef FEAT_CLIENTSERVER
16600# ifdef WIN32
16601 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016602 {
16603 char_u *server_name = get_tv_string_chk(&argvars[0]);
16604
16605 if (server_name != NULL)
16606 serverForeground(server_name);
16607 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016608# else
16609 /* Send a foreground() expression to the server. */
16610 argvars[1].v_type = VAR_STRING;
16611 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16612 argvars[2].v_type = VAR_UNKNOWN;
16613 remote_common(argvars, rettv, TRUE);
16614 vim_free(argvars[1].vval.v_string);
16615# endif
16616#endif
16617}
16618
Bram Moolenaar0d660222005-01-07 21:51:51 +000016619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016620f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016621{
16622#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016623 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016624 char_u *s = NULL;
16625# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016626 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016627# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016628 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016629
16630 if (check_restricted() || check_secure())
16631 {
16632 rettv->vval.v_number = -1;
16633 return;
16634 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016635 serverid = get_tv_string_chk(&argvars[0]);
16636 if (serverid == NULL)
16637 {
16638 rettv->vval.v_number = -1;
16639 return; /* type error; errmsg already given */
16640 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016641# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016642 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016643 if (n == 0)
16644 rettv->vval.v_number = -1;
16645 else
16646 {
16647 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16648 rettv->vval.v_number = (s != NULL);
16649 }
16650# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016651 if (check_connection() == FAIL)
16652 return;
16653
16654 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016655 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016656# endif
16657
16658 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016660 char_u *retvar;
16661
Bram Moolenaar33570922005-01-25 22:26:29 +000016662 v.di_tv.v_type = VAR_STRING;
16663 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016664 retvar = get_tv_string_chk(&argvars[1]);
16665 if (retvar != NULL)
16666 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016667 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016668 }
16669#else
16670 rettv->vval.v_number = -1;
16671#endif
16672}
16673
Bram Moolenaar0d660222005-01-07 21:51:51 +000016674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016675f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016676{
16677 char_u *r = NULL;
16678
16679#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016680 char_u *serverid = get_tv_string_chk(&argvars[0]);
16681
16682 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016683 {
16684# ifdef WIN32
16685 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016686 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016687
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016688 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016689 if (n != 0)
16690 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16691 if (r == NULL)
16692# else
16693 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016694 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016695# endif
16696 EMSG(_("E277: Unable to read a server reply"));
16697 }
16698#endif
16699 rettv->v_type = VAR_STRING;
16700 rettv->vval.v_string = r;
16701}
16702
16703/*
16704 * "remote_send()" function
16705 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016706 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016707f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016708{
16709 rettv->v_type = VAR_STRING;
16710 rettv->vval.v_string = NULL;
16711#ifdef FEAT_CLIENTSERVER
16712 remote_common(argvars, rettv, FALSE);
16713#endif
16714}
16715
16716/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016717 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016718 */
16719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016720f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016721{
Bram Moolenaar33570922005-01-25 22:26:29 +000016722 list_T *l;
16723 listitem_T *item, *item2;
16724 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016725 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016726 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016727 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016728 dict_T *d;
16729 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016730 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016731
Bram Moolenaar8c711452005-01-14 21:53:12 +000016732 if (argvars[0].v_type == VAR_DICT)
16733 {
16734 if (argvars[2].v_type != VAR_UNKNOWN)
16735 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016736 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016737 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016738 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016739 key = get_tv_string_chk(&argvars[1]);
16740 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016741 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016742 di = dict_find(d, key, -1);
16743 if (di == NULL)
16744 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016745 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16746 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016747 {
16748 *rettv = di->di_tv;
16749 init_tv(&di->di_tv);
16750 dictitem_remove(d, di);
16751 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016752 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016753 }
16754 }
16755 else if (argvars[0].v_type != VAR_LIST)
16756 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016757 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016758 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016759 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016760 int error = FALSE;
16761
16762 idx = get_tv_number_chk(&argvars[1], &error);
16763 if (error)
16764 ; /* type error: do nothing, errmsg already given */
16765 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016766 EMSGN(_(e_listidx), idx);
16767 else
16768 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016769 if (argvars[2].v_type == VAR_UNKNOWN)
16770 {
16771 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016772 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016773 *rettv = item->li_tv;
16774 vim_free(item);
16775 }
16776 else
16777 {
16778 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016779 end = get_tv_number_chk(&argvars[2], &error);
16780 if (error)
16781 ; /* type error: do nothing */
16782 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016783 EMSGN(_(e_listidx), end);
16784 else
16785 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016786 int cnt = 0;
16787
16788 for (li = item; li != NULL; li = li->li_next)
16789 {
16790 ++cnt;
16791 if (li == item2)
16792 break;
16793 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016794 if (li == NULL) /* didn't find "item2" after "item" */
16795 EMSG(_(e_invrange));
16796 else
16797 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016798 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016799 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016800 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016801 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016802 l->lv_first = item;
16803 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016804 item->li_prev = NULL;
16805 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016806 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016807 }
16808 }
16809 }
16810 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016811 }
16812 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813}
16814
16815/*
16816 * "rename({from}, {to})" function
16817 */
16818 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016819f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016820{
16821 char_u buf[NUMBUFLEN];
16822
16823 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016824 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016825 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016826 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16827 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016828}
16829
16830/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016831 * "repeat()" function
16832 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016833 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016834f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016835{
16836 char_u *p;
16837 int n;
16838 int slen;
16839 int len;
16840 char_u *r;
16841 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016842
16843 n = get_tv_number(&argvars[1]);
16844 if (argvars[0].v_type == VAR_LIST)
16845 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016846 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016847 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016848 if (list_extend(rettv->vval.v_list,
16849 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016850 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016851 }
16852 else
16853 {
16854 p = get_tv_string(&argvars[0]);
16855 rettv->v_type = VAR_STRING;
16856 rettv->vval.v_string = NULL;
16857
16858 slen = (int)STRLEN(p);
16859 len = slen * n;
16860 if (len <= 0)
16861 return;
16862
16863 r = alloc(len + 1);
16864 if (r != NULL)
16865 {
16866 for (i = 0; i < n; i++)
16867 mch_memmove(r + i * slen, p, (size_t)slen);
16868 r[len] = NUL;
16869 }
16870
16871 rettv->vval.v_string = r;
16872 }
16873}
16874
16875/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016876 * "resolve()" function
16877 */
16878 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016879f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016880{
16881 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016882#ifdef HAVE_READLINK
16883 char_u *buf = NULL;
16884#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016885
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016886 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016887#ifdef FEAT_SHORTCUT
16888 {
16889 char_u *v = NULL;
16890
16891 v = mch_resolve_shortcut(p);
16892 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016893 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016894 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016895 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016896 }
16897#else
16898# ifdef HAVE_READLINK
16899 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016900 char_u *cpy;
16901 int len;
16902 char_u *remain = NULL;
16903 char_u *q;
16904 int is_relative_to_current = FALSE;
16905 int has_trailing_pathsep = FALSE;
16906 int limit = 100;
16907
16908 p = vim_strsave(p);
16909
16910 if (p[0] == '.' && (vim_ispathsep(p[1])
16911 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16912 is_relative_to_current = TRUE;
16913
16914 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016915 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016916 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016917 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016918 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16919 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016920
16921 q = getnextcomp(p);
16922 if (*q != NUL)
16923 {
16924 /* Separate the first path component in "p", and keep the
16925 * remainder (beginning with the path separator). */
16926 remain = vim_strsave(q - 1);
16927 q[-1] = NUL;
16928 }
16929
Bram Moolenaard9462e32011-04-11 21:35:11 +020016930 buf = alloc(MAXPATHL + 1);
16931 if (buf == NULL)
16932 goto fail;
16933
Bram Moolenaar071d4272004-06-13 20:20:40 +000016934 for (;;)
16935 {
16936 for (;;)
16937 {
16938 len = readlink((char *)p, (char *)buf, MAXPATHL);
16939 if (len <= 0)
16940 break;
16941 buf[len] = NUL;
16942
16943 if (limit-- == 0)
16944 {
16945 vim_free(p);
16946 vim_free(remain);
16947 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016948 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016949 goto fail;
16950 }
16951
16952 /* Ensure that the result will have a trailing path separator
16953 * if the argument has one. */
16954 if (remain == NULL && has_trailing_pathsep)
16955 add_pathsep(buf);
16956
16957 /* Separate the first path component in the link value and
16958 * concatenate the remainders. */
16959 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16960 if (*q != NUL)
16961 {
16962 if (remain == NULL)
16963 remain = vim_strsave(q - 1);
16964 else
16965 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016966 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016967 if (cpy != NULL)
16968 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016969 vim_free(remain);
16970 remain = cpy;
16971 }
16972 }
16973 q[-1] = NUL;
16974 }
16975
16976 q = gettail(p);
16977 if (q > p && *q == NUL)
16978 {
16979 /* Ignore trailing path separator. */
16980 q[-1] = NUL;
16981 q = gettail(p);
16982 }
16983 if (q > p && !mch_isFullName(buf))
16984 {
16985 /* symlink is relative to directory of argument */
16986 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16987 if (cpy != NULL)
16988 {
16989 STRCPY(cpy, p);
16990 STRCPY(gettail(cpy), buf);
16991 vim_free(p);
16992 p = cpy;
16993 }
16994 }
16995 else
16996 {
16997 vim_free(p);
16998 p = vim_strsave(buf);
16999 }
17000 }
17001
17002 if (remain == NULL)
17003 break;
17004
17005 /* Append the first path component of "remain" to "p". */
17006 q = getnextcomp(remain + 1);
17007 len = q - remain - (*q != NUL);
17008 cpy = vim_strnsave(p, STRLEN(p) + len);
17009 if (cpy != NULL)
17010 {
17011 STRNCAT(cpy, remain, len);
17012 vim_free(p);
17013 p = cpy;
17014 }
17015 /* Shorten "remain". */
17016 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017017 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017018 else
17019 {
17020 vim_free(remain);
17021 remain = NULL;
17022 }
17023 }
17024
17025 /* If the result is a relative path name, make it explicitly relative to
17026 * the current directory if and only if the argument had this form. */
17027 if (!vim_ispathsep(*p))
17028 {
17029 if (is_relative_to_current
17030 && *p != NUL
17031 && !(p[0] == '.'
17032 && (p[1] == NUL
17033 || vim_ispathsep(p[1])
17034 || (p[1] == '.'
17035 && (p[2] == NUL
17036 || vim_ispathsep(p[2]))))))
17037 {
17038 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000017039 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017040 if (cpy != NULL)
17041 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000017042 vim_free(p);
17043 p = cpy;
17044 }
17045 }
17046 else if (!is_relative_to_current)
17047 {
17048 /* Strip leading "./". */
17049 q = p;
17050 while (q[0] == '.' && vim_ispathsep(q[1]))
17051 q += 2;
17052 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017053 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017054 }
17055 }
17056
17057 /* Ensure that the result will have no trailing path separator
17058 * if the argument had none. But keep "/" or "//". */
17059 if (!has_trailing_pathsep)
17060 {
17061 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000017062 if (after_pathsep(p, q))
17063 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017064 }
17065
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017066 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017067 }
17068# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017069 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017070# endif
17071#endif
17072
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017073 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074
17075#ifdef HAVE_READLINK
17076fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020017077 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017078#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017079 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017080}
17081
17082/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000017083 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 */
17085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017086f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017087{
Bram Moolenaar33570922005-01-25 22:26:29 +000017088 list_T *l;
17089 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017090
Bram Moolenaar0d660222005-01-07 21:51:51 +000017091 if (argvars[0].v_type != VAR_LIST)
17092 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000017093 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020017094 && !tv_check_lock(l->lv_lock,
17095 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000017096 {
17097 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000017098 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000017099 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017100 while (li != NULL)
17101 {
17102 ni = li->li_prev;
17103 list_append(l, li);
17104 li = ni;
17105 }
17106 rettv->vval.v_list = l;
17107 rettv->v_type = VAR_LIST;
17108 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017109 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017110 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017111}
17112
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017113#define SP_NOMOVE 0x01 /* don't move cursor */
17114#define SP_REPEAT 0x02 /* repeat to find outer pair */
17115#define SP_RETCOUNT 0x04 /* return matchcount */
17116#define SP_SETPCMARK 0x08 /* set previous context mark */
17117#define SP_START 0x10 /* accept match at start position */
17118#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17119#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017120#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017121
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017122static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017123
17124/*
17125 * Get flags for a search function.
17126 * Possibly sets "p_ws".
17127 * Returns BACKWARD, FORWARD or zero (for an error).
17128 */
17129 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017130get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017131{
17132 int dir = FORWARD;
17133 char_u *flags;
17134 char_u nbuf[NUMBUFLEN];
17135 int mask;
17136
17137 if (varp->v_type != VAR_UNKNOWN)
17138 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017139 flags = get_tv_string_buf_chk(varp, nbuf);
17140 if (flags == NULL)
17141 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017142 while (*flags != NUL)
17143 {
17144 switch (*flags)
17145 {
17146 case 'b': dir = BACKWARD; break;
17147 case 'w': p_ws = TRUE; break;
17148 case 'W': p_ws = FALSE; break;
17149 default: mask = 0;
17150 if (flagsp != NULL)
17151 switch (*flags)
17152 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017153 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017154 case 'e': mask = SP_END; break;
17155 case 'm': mask = SP_RETCOUNT; break;
17156 case 'n': mask = SP_NOMOVE; break;
17157 case 'p': mask = SP_SUBPAT; break;
17158 case 'r': mask = SP_REPEAT; break;
17159 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017160 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017161 }
17162 if (mask == 0)
17163 {
17164 EMSG2(_(e_invarg2), flags);
17165 dir = 0;
17166 }
17167 else
17168 *flagsp |= mask;
17169 }
17170 if (dir == 0)
17171 break;
17172 ++flags;
17173 }
17174 }
17175 return dir;
17176}
17177
Bram Moolenaar071d4272004-06-13 20:20:40 +000017178/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017179 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017180 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017181 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017182search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017183{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017184 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017185 char_u *pat;
17186 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017187 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017188 int save_p_ws = p_ws;
17189 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017190 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017191 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017192 proftime_T tm;
17193#ifdef FEAT_RELTIME
17194 long time_limit = 0;
17195#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017196 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017197 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017198
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017199 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017200 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017201 if (dir == 0)
17202 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017203 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017204 if (flags & SP_START)
17205 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017206 if (flags & SP_END)
17207 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017208 if (flags & SP_COLUMN)
17209 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017210
Bram Moolenaar76929292008-01-06 19:07:36 +000017211 /* Optional arguments: line number to stop searching and timeout. */
17212 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017213 {
17214 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17215 if (lnum_stop < 0)
17216 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017217#ifdef FEAT_RELTIME
17218 if (argvars[3].v_type != VAR_UNKNOWN)
17219 {
17220 time_limit = get_tv_number_chk(&argvars[3], NULL);
17221 if (time_limit < 0)
17222 goto theend;
17223 }
17224#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017225 }
17226
Bram Moolenaar76929292008-01-06 19:07:36 +000017227#ifdef FEAT_RELTIME
17228 /* Set the time limit, if there is one. */
17229 profile_setlimit(time_limit, &tm);
17230#endif
17231
Bram Moolenaar231334e2005-07-25 20:46:57 +000017232 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017233 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017234 * Check to make sure only those flags are set.
17235 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17236 * flags cannot be set. Check for that condition also.
17237 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017238 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017239 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017240 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017241 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017242 goto theend;
17243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017244
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017245 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017246 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017247 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017248 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017249 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017250 if (flags & SP_SUBPAT)
17251 retval = subpatnum;
17252 else
17253 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017254 if (flags & SP_SETPCMARK)
17255 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017256 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017257 if (match_pos != NULL)
17258 {
17259 /* Store the match cursor position */
17260 match_pos->lnum = pos.lnum;
17261 match_pos->col = pos.col + 1;
17262 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017263 /* "/$" will put the cursor after the end of the line, may need to
17264 * correct that here */
17265 check_cursor();
17266 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017267
17268 /* If 'n' flag is used: restore cursor position. */
17269 if (flags & SP_NOMOVE)
17270 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017271 else
17272 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017273theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017274 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017275
17276 return retval;
17277}
17278
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017279#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017280
17281/*
17282 * round() is not in C90, use ceil() or floor() instead.
17283 */
17284 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017285vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017286{
17287 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17288}
17289
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017290/*
17291 * "round({float})" function
17292 */
17293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017294f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017295{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017296 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017297
17298 rettv->v_type = VAR_FLOAT;
17299 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017300 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017301 else
17302 rettv->vval.v_float = 0.0;
17303}
17304#endif
17305
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017306/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017307 * "screenattr()" function
17308 */
17309 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017310f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017311{
17312 int row;
17313 int col;
17314 int c;
17315
17316 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17317 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17318 if (row < 0 || row >= screen_Rows
17319 || col < 0 || col >= screen_Columns)
17320 c = -1;
17321 else
17322 c = ScreenAttrs[LineOffset[row] + col];
17323 rettv->vval.v_number = c;
17324}
17325
17326/*
17327 * "screenchar()" function
17328 */
17329 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017330f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017331{
17332 int row;
17333 int col;
17334 int off;
17335 int c;
17336
17337 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17338 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17339 if (row < 0 || row >= screen_Rows
17340 || col < 0 || col >= screen_Columns)
17341 c = -1;
17342 else
17343 {
17344 off = LineOffset[row] + col;
17345#ifdef FEAT_MBYTE
17346 if (enc_utf8 && ScreenLinesUC[off] != 0)
17347 c = ScreenLinesUC[off];
17348 else
17349#endif
17350 c = ScreenLines[off];
17351 }
17352 rettv->vval.v_number = c;
17353}
17354
17355/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017356 * "screencol()" function
17357 *
17358 * First column is 1 to be consistent with virtcol().
17359 */
17360 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017361f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017362{
17363 rettv->vval.v_number = screen_screencol() + 1;
17364}
17365
17366/*
17367 * "screenrow()" function
17368 */
17369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017370f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017371{
17372 rettv->vval.v_number = screen_screenrow() + 1;
17373}
17374
17375/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017376 * "search()" function
17377 */
17378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017379f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017380{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017381 int flags = 0;
17382
17383 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017384}
17385
Bram Moolenaar071d4272004-06-13 20:20:40 +000017386/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017387 * "searchdecl()" function
17388 */
17389 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017390f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017391{
17392 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017393 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017394 int error = FALSE;
17395 char_u *name;
17396
17397 rettv->vval.v_number = 1; /* default: FAIL */
17398
17399 name = get_tv_string_chk(&argvars[0]);
17400 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017401 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017402 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017403 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17404 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17405 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017406 if (!error && name != NULL)
17407 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017408 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017409}
17410
17411/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017412 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017413 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017414 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017415searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017416{
17417 char_u *spat, *mpat, *epat;
17418 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017419 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017420 int dir;
17421 int flags = 0;
17422 char_u nbuf1[NUMBUFLEN];
17423 char_u nbuf2[NUMBUFLEN];
17424 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017425 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017426 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017427 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017428
Bram Moolenaar071d4272004-06-13 20:20:40 +000017429 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017430 spat = get_tv_string_chk(&argvars[0]);
17431 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17432 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17433 if (spat == NULL || mpat == NULL || epat == NULL)
17434 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017435
Bram Moolenaar071d4272004-06-13 20:20:40 +000017436 /* Handle the optional fourth argument: flags */
17437 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017438 if (dir == 0)
17439 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017440
17441 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017442 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17443 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017444 if ((flags & (SP_END | SP_SUBPAT)) != 0
17445 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017446 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017447 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017448 goto theend;
17449 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017450
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017451 /* Using 'r' implies 'W', otherwise it doesn't work. */
17452 if (flags & SP_REPEAT)
17453 p_ws = FALSE;
17454
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017455 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017456 if (argvars[3].v_type == VAR_UNKNOWN
17457 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017458 skip = (char_u *)"";
17459 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017460 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017461 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017462 if (argvars[5].v_type != VAR_UNKNOWN)
17463 {
17464 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17465 if (lnum_stop < 0)
17466 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017467#ifdef FEAT_RELTIME
17468 if (argvars[6].v_type != VAR_UNKNOWN)
17469 {
17470 time_limit = get_tv_number_chk(&argvars[6], NULL);
17471 if (time_limit < 0)
17472 goto theend;
17473 }
17474#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017475 }
17476 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017477 if (skip == NULL)
17478 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017479
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017480 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017481 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017482
17483theend:
17484 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017485
17486 return retval;
17487}
17488
17489/*
17490 * "searchpair()" function
17491 */
17492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017493f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017494{
17495 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17496}
17497
17498/*
17499 * "searchpairpos()" function
17500 */
17501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017502f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017503{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017504 pos_T match_pos;
17505 int lnum = 0;
17506 int col = 0;
17507
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017508 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017509 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017510
17511 if (searchpair_cmn(argvars, &match_pos) > 0)
17512 {
17513 lnum = match_pos.lnum;
17514 col = match_pos.col;
17515 }
17516
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017517 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17518 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017519}
17520
17521/*
17522 * Search for a start/middle/end thing.
17523 * Used by searchpair(), see its documentation for the details.
17524 * Returns 0 or -1 for no match,
17525 */
17526 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017527do_searchpair(
17528 char_u *spat, /* start pattern */
17529 char_u *mpat, /* middle pattern */
17530 char_u *epat, /* end pattern */
17531 int dir, /* BACKWARD or FORWARD */
17532 char_u *skip, /* skip expression */
17533 int flags, /* SP_SETPCMARK and other SP_ values */
17534 pos_T *match_pos,
17535 linenr_T lnum_stop, /* stop at this line if not zero */
17536 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017537{
17538 char_u *save_cpo;
17539 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17540 long retval = 0;
17541 pos_T pos;
17542 pos_T firstpos;
17543 pos_T foundpos;
17544 pos_T save_cursor;
17545 pos_T save_pos;
17546 int n;
17547 int r;
17548 int nest = 1;
17549 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017550 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017551 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017552
17553 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17554 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017555 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017556
Bram Moolenaar76929292008-01-06 19:07:36 +000017557#ifdef FEAT_RELTIME
17558 /* Set the time limit, if there is one. */
17559 profile_setlimit(time_limit, &tm);
17560#endif
17561
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017562 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17563 * start/middle/end (pat3, for the top pair). */
17564 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17565 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17566 if (pat2 == NULL || pat3 == NULL)
17567 goto theend;
17568 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17569 if (*mpat == NUL)
17570 STRCPY(pat3, pat2);
17571 else
17572 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17573 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017574 if (flags & SP_START)
17575 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017576
Bram Moolenaar071d4272004-06-13 20:20:40 +000017577 save_cursor = curwin->w_cursor;
17578 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017579 clearpos(&firstpos);
17580 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017581 pat = pat3;
17582 for (;;)
17583 {
17584 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017585 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017586 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17587 /* didn't find it or found the first match again: FAIL */
17588 break;
17589
17590 if (firstpos.lnum == 0)
17591 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017592 if (equalpos(pos, foundpos))
17593 {
17594 /* Found the same position again. Can happen with a pattern that
17595 * has "\zs" at the end and searching backwards. Advance one
17596 * character and try again. */
17597 if (dir == BACKWARD)
17598 decl(&pos);
17599 else
17600 incl(&pos);
17601 }
17602 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017603
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017604 /* clear the start flag to avoid getting stuck here */
17605 options &= ~SEARCH_START;
17606
Bram Moolenaar071d4272004-06-13 20:20:40 +000017607 /* If the skip pattern matches, ignore this match. */
17608 if (*skip != NUL)
17609 {
17610 save_pos = curwin->w_cursor;
17611 curwin->w_cursor = pos;
17612 r = eval_to_bool(skip, &err, NULL, FALSE);
17613 curwin->w_cursor = save_pos;
17614 if (err)
17615 {
17616 /* Evaluating {skip} caused an error, break here. */
17617 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017618 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017619 break;
17620 }
17621 if (r)
17622 continue;
17623 }
17624
17625 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17626 {
17627 /* Found end when searching backwards or start when searching
17628 * forward: nested pair. */
17629 ++nest;
17630 pat = pat2; /* nested, don't search for middle */
17631 }
17632 else
17633 {
17634 /* Found end when searching forward or start when searching
17635 * backward: end of (nested) pair; or found middle in outer pair. */
17636 if (--nest == 1)
17637 pat = pat3; /* outer level, search for middle */
17638 }
17639
17640 if (nest == 0)
17641 {
17642 /* Found the match: return matchcount or line number. */
17643 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017644 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017645 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017646 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017647 if (flags & SP_SETPCMARK)
17648 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017649 curwin->w_cursor = pos;
17650 if (!(flags & SP_REPEAT))
17651 break;
17652 nest = 1; /* search for next unmatched */
17653 }
17654 }
17655
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017656 if (match_pos != NULL)
17657 {
17658 /* Store the match cursor position */
17659 match_pos->lnum = curwin->w_cursor.lnum;
17660 match_pos->col = curwin->w_cursor.col + 1;
17661 }
17662
Bram Moolenaar071d4272004-06-13 20:20:40 +000017663 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017664 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017665 curwin->w_cursor = save_cursor;
17666
17667theend:
17668 vim_free(pat2);
17669 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017670 if (p_cpo == empty_option)
17671 p_cpo = save_cpo;
17672 else
17673 /* Darn, evaluating the {skip} expression changed the value. */
17674 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017675
17676 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017677}
17678
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017679/*
17680 * "searchpos()" function
17681 */
17682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017683f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017684{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017685 pos_T match_pos;
17686 int lnum = 0;
17687 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017688 int n;
17689 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017690
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017691 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017692 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017693
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017694 n = search_cmn(argvars, &match_pos, &flags);
17695 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017696 {
17697 lnum = match_pos.lnum;
17698 col = match_pos.col;
17699 }
17700
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017701 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17702 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017703 if (flags & SP_SUBPAT)
17704 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017705}
17706
Bram Moolenaar0d660222005-01-07 21:51:51 +000017707 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017708f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017709{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017710#ifdef FEAT_CLIENTSERVER
17711 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017712 char_u *server = get_tv_string_chk(&argvars[0]);
17713 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017714
Bram Moolenaar0d660222005-01-07 21:51:51 +000017715 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017716 if (server == NULL || reply == NULL)
17717 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017718 if (check_restricted() || check_secure())
17719 return;
17720# ifdef FEAT_X11
17721 if (check_connection() == FAIL)
17722 return;
17723# endif
17724
17725 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017726 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017727 EMSG(_("E258: Unable to send to client"));
17728 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017729 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017730 rettv->vval.v_number = 0;
17731#else
17732 rettv->vval.v_number = -1;
17733#endif
17734}
17735
Bram Moolenaar0d660222005-01-07 21:51:51 +000017736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017737f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017738{
17739 char_u *r = NULL;
17740
17741#ifdef FEAT_CLIENTSERVER
17742# ifdef WIN32
17743 r = serverGetVimNames();
17744# else
17745 make_connection();
17746 if (X_DISPLAY != NULL)
17747 r = serverGetVimNames(X_DISPLAY);
17748# endif
17749#endif
17750 rettv->v_type = VAR_STRING;
17751 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017752}
17753
17754/*
17755 * "setbufvar()" function
17756 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017757 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017758f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017759{
17760 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017761 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017762 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017763 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017764 char_u nbuf[NUMBUFLEN];
17765
17766 if (check_restricted() || check_secure())
17767 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017768 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17769 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017770 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017771 varp = &argvars[2];
17772
17773 if (buf != NULL && varname != NULL && varp != NULL)
17774 {
17775 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017776 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017777
17778 if (*varname == '&')
17779 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017780 long numval;
17781 char_u *strval;
17782 int error = FALSE;
17783
Bram Moolenaar071d4272004-06-13 20:20:40 +000017784 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017785 numval = get_tv_number_chk(varp, &error);
17786 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017787 if (!error && strval != NULL)
17788 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017789 }
17790 else
17791 {
17792 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17793 if (bufvarname != NULL)
17794 {
17795 STRCPY(bufvarname, "b:");
17796 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017797 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017798 vim_free(bufvarname);
17799 }
17800 }
17801
17802 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017803 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017805}
17806
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017807 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017808f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017809{
17810 dict_T *d;
17811 dictitem_T *di;
17812 char_u *csearch;
17813
17814 if (argvars[0].v_type != VAR_DICT)
17815 {
17816 EMSG(_(e_dictreq));
17817 return;
17818 }
17819
17820 if ((d = argvars[0].vval.v_dict) != NULL)
17821 {
17822 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17823 if (csearch != NULL)
17824 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017825#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017826 if (enc_utf8)
17827 {
17828 int pcc[MAX_MCO];
17829 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017830
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017831 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17832 }
17833 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017834#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017835 set_last_csearch(PTR2CHAR(csearch),
17836 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017837 }
17838
17839 di = dict_find(d, (char_u *)"forward", -1);
17840 if (di != NULL)
17841 set_csearch_direction(get_tv_number(&di->di_tv)
17842 ? FORWARD : BACKWARD);
17843
17844 di = dict_find(d, (char_u *)"until", -1);
17845 if (di != NULL)
17846 set_csearch_until(!!get_tv_number(&di->di_tv));
17847 }
17848}
17849
Bram Moolenaar071d4272004-06-13 20:20:40 +000017850/*
17851 * "setcmdpos()" function
17852 */
17853 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017854f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017855{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017856 int pos = (int)get_tv_number(&argvars[0]) - 1;
17857
17858 if (pos >= 0)
17859 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017860}
17861
17862/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017863 * "setfperm({fname}, {mode})" function
17864 */
17865 static void
17866f_setfperm(typval_T *argvars, typval_T *rettv)
17867{
17868 char_u *fname;
17869 char_u modebuf[NUMBUFLEN];
17870 char_u *mode_str;
17871 int i;
17872 int mask;
17873 int mode = 0;
17874
17875 rettv->vval.v_number = 0;
17876 fname = get_tv_string_chk(&argvars[0]);
17877 if (fname == NULL)
17878 return;
17879 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17880 if (mode_str == NULL)
17881 return;
17882 if (STRLEN(mode_str) != 9)
17883 {
17884 EMSG2(_(e_invarg2), mode_str);
17885 return;
17886 }
17887
17888 mask = 1;
17889 for (i = 8; i >= 0; --i)
17890 {
17891 if (mode_str[i] != '-')
17892 mode |= mask;
17893 mask = mask << 1;
17894 }
17895 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17896}
17897
17898/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017899 * "setline()" function
17900 */
17901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017902f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017903{
17904 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017905 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017906 list_T *l = NULL;
17907 listitem_T *li = NULL;
17908 long added = 0;
17909 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017910
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017911 lnum = get_tv_lnum(&argvars[0]);
17912 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017913 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017914 l = argvars[1].vval.v_list;
17915 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017916 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017917 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017918 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017919
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017920 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017921 for (;;)
17922 {
17923 if (l != NULL)
17924 {
17925 /* list argument, get next string */
17926 if (li == NULL)
17927 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017928 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017929 li = li->li_next;
17930 }
17931
17932 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017933 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017934 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017935
17936 /* When coming here from Insert mode, sync undo, so that this can be
17937 * undone separately from what was previously inserted. */
17938 if (u_sync_once == 2)
17939 {
17940 u_sync_once = 1; /* notify that u_sync() was called */
17941 u_sync(TRUE);
17942 }
17943
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017944 if (lnum <= curbuf->b_ml.ml_line_count)
17945 {
17946 /* existing line, replace it */
17947 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17948 {
17949 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017950 if (lnum == curwin->w_cursor.lnum)
17951 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017952 rettv->vval.v_number = 0; /* OK */
17953 }
17954 }
17955 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17956 {
17957 /* lnum is one past the last line, append the line */
17958 ++added;
17959 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17960 rettv->vval.v_number = 0; /* OK */
17961 }
17962
17963 if (l == NULL) /* only one string argument */
17964 break;
17965 ++lnum;
17966 }
17967
17968 if (added > 0)
17969 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017970}
17971
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017972static 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 +000017973
Bram Moolenaar071d4272004-06-13 20:20:40 +000017974/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017975 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017976 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017978set_qf_ll_list(
17979 win_T *wp UNUSED,
17980 typval_T *list_arg UNUSED,
17981 typval_T *action_arg UNUSED,
17982 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017983{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017984#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017985 char_u *act;
17986 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017987#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017988
Bram Moolenaar2641f772005-03-25 21:58:17 +000017989 rettv->vval.v_number = -1;
17990
17991#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017992 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017993 EMSG(_(e_listreq));
17994 else
17995 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017996 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017997
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017998 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017999 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018000 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018001 if (act == NULL)
18002 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000018003 if (*act == 'a' || *act == 'r')
18004 action = *act;
18005 }
18006
Bram Moolenaar81484f42012-12-05 15:16:47 +010018007 if (l != NULL && set_errorlist(wp, l, action,
18008 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000018009 rettv->vval.v_number = 0;
18010 }
18011#endif
18012}
18013
18014/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018015 * "setloclist()" function
18016 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018017 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018018f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018019{
18020 win_T *win;
18021
18022 rettv->vval.v_number = -1;
18023
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018024 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018025 if (win != NULL)
18026 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
18027}
18028
18029/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018030 * "setmatches()" function
18031 */
18032 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018033f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018034{
18035#ifdef FEAT_SEARCH_EXTRA
18036 list_T *l;
18037 listitem_T *li;
18038 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018039 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018040
18041 rettv->vval.v_number = -1;
18042 if (argvars[0].v_type != VAR_LIST)
18043 {
18044 EMSG(_(e_listreq));
18045 return;
18046 }
18047 if ((l = argvars[0].vval.v_list) != NULL)
18048 {
18049
18050 /* To some extent make sure that we are dealing with a list from
18051 * "getmatches()". */
18052 li = l->lv_first;
18053 while (li != NULL)
18054 {
18055 if (li->li_tv.v_type != VAR_DICT
18056 || (d = li->li_tv.vval.v_dict) == NULL)
18057 {
18058 EMSG(_(e_invarg));
18059 return;
18060 }
18061 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018062 && (dict_find(d, (char_u *)"pattern", -1) != NULL
18063 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018064 && dict_find(d, (char_u *)"priority", -1) != NULL
18065 && dict_find(d, (char_u *)"id", -1) != NULL))
18066 {
18067 EMSG(_(e_invarg));
18068 return;
18069 }
18070 li = li->li_next;
18071 }
18072
18073 clear_matches(curwin);
18074 li = l->lv_first;
18075 while (li != NULL)
18076 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018077 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020018078 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018079 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020018080 char_u *group;
18081 int priority;
18082 int id;
18083 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018084
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018085 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018086 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
18087 {
18088 if (s == NULL)
18089 {
18090 s = list_alloc();
18091 if (s == NULL)
18092 return;
18093 }
18094
18095 /* match from matchaddpos() */
18096 for (i = 1; i < 9; i++)
18097 {
18098 sprintf((char *)buf, (char *)"pos%d", i);
18099 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
18100 {
18101 if (di->di_tv.v_type != VAR_LIST)
18102 return;
18103
18104 list_append_tv(s, &di->di_tv);
18105 s->lv_refcount++;
18106 }
18107 else
18108 break;
18109 }
18110 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018111
18112 group = get_dict_string(d, (char_u *)"group", FALSE);
18113 priority = (int)get_dict_number(d, (char_u *)"priority");
18114 id = (int)get_dict_number(d, (char_u *)"id");
18115 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18116 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18117 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018118 if (i == 0)
18119 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018120 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018121 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018122 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018123 }
18124 else
18125 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018126 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018127 list_unref(s);
18128 s = NULL;
18129 }
18130
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018131 li = li->li_next;
18132 }
18133 rettv->vval.v_number = 0;
18134 }
18135#endif
18136}
18137
18138/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018139 * "setpos()" function
18140 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018142f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018143{
18144 pos_T pos;
18145 int fnum;
18146 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018147 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018148
Bram Moolenaar08250432008-02-13 11:42:46 +000018149 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018150 name = get_tv_string_chk(argvars);
18151 if (name != NULL)
18152 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018153 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018154 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018155 if (--pos.col < 0)
18156 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018157 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018158 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018159 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018160 if (fnum == curbuf->b_fnum)
18161 {
18162 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018163 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018164 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018165 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018166 curwin->w_set_curswant = FALSE;
18167 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018168 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018169 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018170 }
18171 else
18172 EMSG(_(e_invarg));
18173 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018174 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18175 {
18176 /* set mark */
18177 if (setmark_pos(name[1], &pos, fnum) == OK)
18178 rettv->vval.v_number = 0;
18179 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018180 else
18181 EMSG(_(e_invarg));
18182 }
18183 }
18184}
18185
18186/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018187 * "setqflist()" function
18188 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018190f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018191{
18192 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18193}
18194
18195/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018196 * "setreg()" function
18197 */
18198 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018199f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018200{
18201 int regname;
18202 char_u *strregname;
18203 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018204 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018205 int append;
18206 char_u yank_type;
18207 long block_len;
18208
18209 block_len = -1;
18210 yank_type = MAUTO;
18211 append = FALSE;
18212
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018213 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018214 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018216 if (strregname == NULL)
18217 return; /* type error; errmsg already given */
18218 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018219 if (regname == 0 || regname == '@')
18220 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018221
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018222 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018223 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018224 stropt = get_tv_string_chk(&argvars[2]);
18225 if (stropt == NULL)
18226 return; /* type error */
18227 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018228 switch (*stropt)
18229 {
18230 case 'a': case 'A': /* append */
18231 append = TRUE;
18232 break;
18233 case 'v': case 'c': /* character-wise selection */
18234 yank_type = MCHAR;
18235 break;
18236 case 'V': case 'l': /* line-wise selection */
18237 yank_type = MLINE;
18238 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018239 case 'b': case Ctrl_V: /* block-wise selection */
18240 yank_type = MBLOCK;
18241 if (VIM_ISDIGIT(stropt[1]))
18242 {
18243 ++stropt;
18244 block_len = getdigits(&stropt) - 1;
18245 --stropt;
18246 }
18247 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018248 }
18249 }
18250
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018251 if (argvars[1].v_type == VAR_LIST)
18252 {
18253 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018254 char_u **allocval;
18255 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018256 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018257 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018258 int len = argvars[1].vval.v_list->lv_len;
18259 listitem_T *li;
18260
Bram Moolenaar7d647822014-04-05 21:28:56 +020018261 /* First half: use for pointers to result lines; second half: use for
18262 * pointers to allocated copies. */
18263 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018264 if (lstval == NULL)
18265 return;
18266 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018267 allocval = lstval + len + 2;
18268 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018269
18270 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18271 li = li->li_next)
18272 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018273 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018274 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018275 goto free_lstval;
18276 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018277 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018278 /* Need to make a copy, next get_tv_string_buf_chk() will
18279 * overwrite the string. */
18280 strval = vim_strsave(buf);
18281 if (strval == NULL)
18282 goto free_lstval;
18283 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018284 }
18285 *curval++ = strval;
18286 }
18287 *curval++ = NULL;
18288
18289 write_reg_contents_lst(regname, lstval, -1,
18290 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018291free_lstval:
18292 while (curallocval > allocval)
18293 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018294 vim_free(lstval);
18295 }
18296 else
18297 {
18298 strval = get_tv_string_chk(&argvars[1]);
18299 if (strval == NULL)
18300 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018301 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018302 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018303 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018304 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018305}
18306
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018307/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018308 * "settabvar()" function
18309 */
18310 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018311f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018312{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018313#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018314 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018315 tabpage_T *tp;
18316#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018317 char_u *varname, *tabvarname;
18318 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018319
18320 rettv->vval.v_number = 0;
18321
18322 if (check_restricted() || check_secure())
18323 return;
18324
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018325#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018326 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018327#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018328 varname = get_tv_string_chk(&argvars[1]);
18329 varp = &argvars[2];
18330
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018331 if (varname != NULL && varp != NULL
18332#ifdef FEAT_WINDOWS
18333 && tp != NULL
18334#endif
18335 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018336 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018337#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018338 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018339 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018340#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018341
18342 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18343 if (tabvarname != NULL)
18344 {
18345 STRCPY(tabvarname, "t:");
18346 STRCPY(tabvarname + 2, varname);
18347 set_var(tabvarname, varp, TRUE);
18348 vim_free(tabvarname);
18349 }
18350
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018351#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018352 /* Restore current tabpage */
18353 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018354 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018355#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018356 }
18357}
18358
18359/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018360 * "settabwinvar()" function
18361 */
18362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018363f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018364{
18365 setwinvar(argvars, rettv, 1);
18366}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018367
18368/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018369 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018370 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018371 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018372f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018373{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018374 setwinvar(argvars, rettv, 0);
18375}
18376
18377/*
18378 * "setwinvar()" and "settabwinvar()" functions
18379 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018380
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018381 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018382setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018383{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384 win_T *win;
18385#ifdef FEAT_WINDOWS
18386 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018387 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018388 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018389#endif
18390 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018391 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018392 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018393 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018394
18395 if (check_restricted() || check_secure())
18396 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018397
18398#ifdef FEAT_WINDOWS
18399 if (off == 1)
18400 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18401 else
18402 tp = curtab;
18403#endif
18404 win = find_win_by_nr(&argvars[off], tp);
18405 varname = get_tv_string_chk(&argvars[off + 1]);
18406 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018407
18408 if (win != NULL && varname != NULL && varp != NULL)
18409 {
18410#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018411 need_switch_win = !(tp == curtab && win == curwin);
18412 if (!need_switch_win
18413 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018414#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018415 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018416 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018417 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018418 long numval;
18419 char_u *strval;
18420 int error = FALSE;
18421
18422 ++varname;
18423 numval = get_tv_number_chk(varp, &error);
18424 strval = get_tv_string_buf_chk(varp, nbuf);
18425 if (!error && strval != NULL)
18426 set_option_value(varname, numval, strval, OPT_LOCAL);
18427 }
18428 else
18429 {
18430 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18431 if (winvarname != NULL)
18432 {
18433 STRCPY(winvarname, "w:");
18434 STRCPY(winvarname + 2, varname);
18435 set_var(winvarname, varp, TRUE);
18436 vim_free(winvarname);
18437 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018438 }
18439 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018440#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018441 if (need_switch_win)
18442 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018443#endif
18444 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018445}
18446
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018447#ifdef FEAT_CRYPT
18448/*
18449 * "sha256({string})" function
18450 */
18451 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018452f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018453{
18454 char_u *p;
18455
18456 p = get_tv_string(&argvars[0]);
18457 rettv->vval.v_string = vim_strsave(
18458 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18459 rettv->v_type = VAR_STRING;
18460}
18461#endif /* FEAT_CRYPT */
18462
Bram Moolenaar071d4272004-06-13 20:20:40 +000018463/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018464 * "shellescape({string})" function
18465 */
18466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018467f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018468{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018469 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018470 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018471 rettv->v_type = VAR_STRING;
18472}
18473
18474/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018475 * shiftwidth() function
18476 */
18477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018478f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018479{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018480 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018481}
18482
18483/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018484 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018485 */
18486 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018487f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018488{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018489 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018490
Bram Moolenaar0d660222005-01-07 21:51:51 +000018491 p = get_tv_string(&argvars[0]);
18492 rettv->vval.v_string = vim_strsave(p);
18493 simplify_filename(rettv->vval.v_string); /* simplify in place */
18494 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018495}
18496
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018497#ifdef FEAT_FLOAT
18498/*
18499 * "sin()" function
18500 */
18501 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018502f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018503{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018504 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018505
18506 rettv->v_type = VAR_FLOAT;
18507 if (get_float_arg(argvars, &f) == OK)
18508 rettv->vval.v_float = sin(f);
18509 else
18510 rettv->vval.v_float = 0.0;
18511}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018512
18513/*
18514 * "sinh()" function
18515 */
18516 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018517f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018518{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018519 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018520
18521 rettv->v_type = VAR_FLOAT;
18522 if (get_float_arg(argvars, &f) == OK)
18523 rettv->vval.v_float = sinh(f);
18524 else
18525 rettv->vval.v_float = 0.0;
18526}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018527#endif
18528
Bram Moolenaar0d660222005-01-07 21:51:51 +000018529static int
18530#ifdef __BORLANDC__
18531 _RTLENTRYF
18532#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018533 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018534static int
18535#ifdef __BORLANDC__
18536 _RTLENTRYF
18537#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018538 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018539
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018540/* struct used in the array that's given to qsort() */
18541typedef struct
18542{
18543 listitem_T *item;
18544 int idx;
18545} sortItem_T;
18546
Bram Moolenaar0b962472016-02-22 22:51:33 +010018547/* struct storing information about current sort */
18548typedef struct
18549{
18550 int item_compare_ic;
18551 int item_compare_numeric;
18552 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018553#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018554 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018555#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018556 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018557 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018558 dict_T *item_compare_selfdict;
18559 int item_compare_func_err;
18560 int item_compare_keep_zero;
18561} sortinfo_T;
18562static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018563static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018564#define ITEM_COMPARE_FAIL 999
18565
Bram Moolenaar071d4272004-06-13 20:20:40 +000018566/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018567 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018568 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018569 static int
18570#ifdef __BORLANDC__
18571_RTLENTRYF
18572#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018573item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018574{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018575 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018576 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018577 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018578 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018579 int res;
18580 char_u numbuf1[NUMBUFLEN];
18581 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018582
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018583 si1 = (sortItem_T *)s1;
18584 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018585 tv1 = &si1->item->li_tv;
18586 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018587
Bram Moolenaar0b962472016-02-22 22:51:33 +010018588 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018589 {
18590 long v1 = get_tv_number(tv1);
18591 long v2 = get_tv_number(tv2);
18592
18593 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18594 }
18595
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018596#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018597 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018598 {
18599 float_T v1 = get_tv_float(tv1);
18600 float_T v2 = get_tv_float(tv2);
18601
18602 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18603 }
18604#endif
18605
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018606 /* tv2string() puts quotes around a string and allocates memory. Don't do
18607 * that for string variables. Use a single quote when comparing with a
18608 * non-string to do what the docs promise. */
18609 if (tv1->v_type == VAR_STRING)
18610 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018611 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018612 p1 = (char_u *)"'";
18613 else
18614 p1 = tv1->vval.v_string;
18615 }
18616 else
18617 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18618 if (tv2->v_type == VAR_STRING)
18619 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018620 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018621 p2 = (char_u *)"'";
18622 else
18623 p2 = tv2->vval.v_string;
18624 }
18625 else
18626 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018627 if (p1 == NULL)
18628 p1 = (char_u *)"";
18629 if (p2 == NULL)
18630 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018631 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018632 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018633 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018634 res = STRICMP(p1, p2);
18635 else
18636 res = STRCMP(p1, p2);
18637 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018638 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018639 {
18640 double n1, n2;
18641 n1 = strtod((char *)p1, (char **)&p1);
18642 n2 = strtod((char *)p2, (char **)&p2);
18643 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18644 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018645
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018646 /* When the result would be zero, compare the item indexes. Makes the
18647 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018648 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018649 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018650
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 vim_free(tofree1);
18652 vim_free(tofree2);
18653 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018654}
18655
18656 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018657#ifdef __BORLANDC__
18658_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018659#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018660item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018661{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018662 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018663 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018664 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018665 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018666 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018667 char_u *func_name;
18668 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018669
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018670 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018671 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018672 return 0;
18673
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018674 si1 = (sortItem_T *)s1;
18675 si2 = (sortItem_T *)s2;
18676
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018677 if (partial == NULL)
18678 func_name = sortinfo->item_compare_func;
18679 else
18680 func_name = partial->pt_name;
18681
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018682 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018683 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018684 copy_tv(&si1->item->li_tv, &argv[0]);
18685 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018686
18687 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018688 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018689 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018690 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018691 clear_tv(&argv[0]);
18692 clear_tv(&argv[1]);
18693
18694 if (res == FAIL)
18695 res = ITEM_COMPARE_FAIL;
18696 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018697 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18698 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018699 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018700 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018701
18702 /* When the result would be zero, compare the pointers themselves. Makes
18703 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018704 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018705 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018706
Bram Moolenaar0d660222005-01-07 21:51:51 +000018707 return res;
18708}
18709
18710/*
18711 * "sort({list})" function
18712 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018714do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018715{
Bram Moolenaar33570922005-01-25 22:26:29 +000018716 list_T *l;
18717 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018718 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018719 sortinfo_T *old_sortinfo;
18720 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018721 long len;
18722 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018723
Bram Moolenaar0b962472016-02-22 22:51:33 +010018724 /* Pointer to current info struct used in compare function. Save and
18725 * restore the current one for nested calls. */
18726 old_sortinfo = sortinfo;
18727 sortinfo = &info;
18728
Bram Moolenaar0d660222005-01-07 21:51:51 +000018729 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018730 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018731 else
18732 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018733 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018734 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018735 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18736 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018737 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018738 rettv->vval.v_list = l;
18739 rettv->v_type = VAR_LIST;
18740 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018741
Bram Moolenaar0d660222005-01-07 21:51:51 +000018742 len = list_len(l);
18743 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018744 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018745
Bram Moolenaar0b962472016-02-22 22:51:33 +010018746 info.item_compare_ic = FALSE;
18747 info.item_compare_numeric = FALSE;
18748 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018749#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018750 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018751#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018752 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018753 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018754 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018755 if (argvars[1].v_type != VAR_UNKNOWN)
18756 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018757 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018758 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018759 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018760 else if (argvars[1].v_type == VAR_PARTIAL)
18761 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018762 else
18763 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018764 int error = FALSE;
18765
18766 i = get_tv_number_chk(&argvars[1], &error);
18767 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018768 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018769 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018770 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018771 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018772 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018773 else if (i != 0)
18774 {
18775 EMSG(_(e_invarg));
18776 goto theend;
18777 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018778 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018779 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018780 if (*info.item_compare_func == NUL)
18781 {
18782 /* empty string means default sort */
18783 info.item_compare_func = NULL;
18784 }
18785 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018786 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018787 info.item_compare_func = NULL;
18788 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018789 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018790 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018791 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018792 info.item_compare_func = NULL;
18793 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018794 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018795#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018796 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018797 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018798 info.item_compare_func = NULL;
18799 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018800 }
18801#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018802 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018803 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018804 info.item_compare_func = NULL;
18805 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018806 }
18807 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018808 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018809
18810 if (argvars[2].v_type != VAR_UNKNOWN)
18811 {
18812 /* optional third argument: {dict} */
18813 if (argvars[2].v_type != VAR_DICT)
18814 {
18815 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018816 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018817 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018818 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018819 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018820 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018821
Bram Moolenaar0d660222005-01-07 21:51:51 +000018822 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018823 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018824 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018825 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018826
Bram Moolenaar327aa022014-03-25 18:24:23 +010018827 i = 0;
18828 if (sort)
18829 {
18830 /* sort(): ptrs will be the list to sort */
18831 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018832 {
18833 ptrs[i].item = li;
18834 ptrs[i].idx = i;
18835 ++i;
18836 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018837
Bram Moolenaar0b962472016-02-22 22:51:33 +010018838 info.item_compare_func_err = FALSE;
18839 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018840 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018841 if ((info.item_compare_func != NULL
18842 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018843 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018844 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018845 EMSG(_("E702: Sort compare function failed"));
18846 else
18847 {
18848 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018849 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018850 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018851 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018852 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018853
Bram Moolenaar0b962472016-02-22 22:51:33 +010018854 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018855 {
18856 /* Clear the List and append the items in sorted order. */
18857 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18858 l->lv_len = 0;
18859 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018860 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018861 }
18862 }
18863 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018864 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018865 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018866 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018867
18868 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018869 info.item_compare_func_err = FALSE;
18870 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018871 item_compare_func_ptr = info.item_compare_func != NULL
18872 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018873 ? item_compare2 : item_compare;
18874
18875 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18876 li = li->li_next)
18877 {
18878 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18879 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018880 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018881 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018882 {
18883 EMSG(_("E882: Uniq compare function failed"));
18884 break;
18885 }
18886 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018887
Bram Moolenaar0b962472016-02-22 22:51:33 +010018888 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018889 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018890 while (--i >= 0)
18891 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018892 li = ptrs[i].item->li_next;
18893 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018894 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018895 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018896 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018897 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018898 list_fix_watch(l, li);
18899 listitem_free(li);
18900 l->lv_len--;
18901 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018902 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018903 }
18904
18905 vim_free(ptrs);
18906 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018907theend:
18908 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018909}
18910
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018912 * "sort({list})" function
18913 */
18914 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018915f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018916{
18917 do_sort_uniq(argvars, rettv, TRUE);
18918}
18919
18920/*
18921 * "uniq({list})" function
18922 */
18923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018924f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018925{
18926 do_sort_uniq(argvars, rettv, FALSE);
18927}
18928
18929/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018930 * "soundfold({word})" function
18931 */
18932 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018933f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018934{
18935 char_u *s;
18936
18937 rettv->v_type = VAR_STRING;
18938 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018939#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018940 rettv->vval.v_string = eval_soundfold(s);
18941#else
18942 rettv->vval.v_string = vim_strsave(s);
18943#endif
18944}
18945
18946/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018947 * "spellbadword()" function
18948 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018949 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018950f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018951{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018952 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018953 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018954 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018955
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018956 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018957 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018958
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018959#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018960 if (argvars[0].v_type == VAR_UNKNOWN)
18961 {
18962 /* Find the start and length of the badly spelled word. */
18963 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18964 if (len != 0)
18965 word = ml_get_cursor();
18966 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018967 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018968 {
18969 char_u *str = get_tv_string_chk(&argvars[0]);
18970 int capcol = -1;
18971
18972 if (str != NULL)
18973 {
18974 /* Check the argument for spelling. */
18975 while (*str != NUL)
18976 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018977 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018978 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018979 {
18980 word = str;
18981 break;
18982 }
18983 str += len;
18984 }
18985 }
18986 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018987#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018988
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018989 list_append_string(rettv->vval.v_list, word, len);
18990 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018991 attr == HLF_SPB ? "bad" :
18992 attr == HLF_SPR ? "rare" :
18993 attr == HLF_SPL ? "local" :
18994 attr == HLF_SPC ? "caps" :
18995 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018996}
18997
18998/*
18999 * "spellsuggest()" function
19000 */
19001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019002f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019003{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019004#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019005 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019006 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019007 int maxcount;
19008 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019009 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019010 listitem_T *li;
19011 int need_capital = FALSE;
19012#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019013
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019014 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019015 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019016
Bram Moolenaar3c56a962006-03-12 22:19:04 +000019017#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020019018 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019019 {
19020 str = get_tv_string(&argvars[0]);
19021 if (argvars[1].v_type != VAR_UNKNOWN)
19022 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019023 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019024 if (maxcount <= 0)
19025 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000019026 if (argvars[2].v_type != VAR_UNKNOWN)
19027 {
19028 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
19029 if (typeerr)
19030 return;
19031 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019032 }
19033 else
19034 maxcount = 25;
19035
Bram Moolenaar4770d092006-01-12 23:22:24 +000019036 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019037
19038 for (i = 0; i < ga.ga_len; ++i)
19039 {
19040 str = ((char_u **)ga.ga_data)[i];
19041
19042 li = listitem_alloc();
19043 if (li == NULL)
19044 vim_free(str);
19045 else
19046 {
19047 li->li_tv.v_type = VAR_STRING;
19048 li->li_tv.v_lock = 0;
19049 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019050 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000019051 }
19052 }
19053 ga_clear(&ga);
19054 }
19055#endif
19056}
19057
Bram Moolenaar0d660222005-01-07 21:51:51 +000019058 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019059f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019060{
19061 char_u *str;
19062 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019063 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019064 regmatch_T regmatch;
19065 char_u patbuf[NUMBUFLEN];
19066 char_u *save_cpo;
19067 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019068 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019069 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019070 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019071
19072 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
19073 save_cpo = p_cpo;
19074 p_cpo = (char_u *)"";
19075
19076 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019077 if (argvars[1].v_type != VAR_UNKNOWN)
19078 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019079 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19080 if (pat == NULL)
19081 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019082 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019083 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019084 }
19085 if (pat == NULL || *pat == NUL)
19086 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000019087
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019088 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019089 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019090 if (typeerr)
19091 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019092
Bram Moolenaar0d660222005-01-07 21:51:51 +000019093 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
19094 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019095 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019096 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019097 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019098 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000019099 if (*str == NUL)
19100 match = FALSE; /* empty item at the end */
19101 else
19102 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019103 if (match)
19104 end = regmatch.startp[0];
19105 else
19106 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019107 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19108 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019109 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019110 if (list_append_string(rettv->vval.v_list, str,
19111 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019112 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019113 }
19114 if (!match)
19115 break;
19116 /* Advance to just after the match. */
19117 if (regmatch.endp[0] > str)
19118 col = 0;
19119 else
19120 {
19121 /* Don't get stuck at the same match. */
19122#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019123 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019124#else
19125 col = 1;
19126#endif
19127 }
19128 str = regmatch.endp[0];
19129 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019130
Bram Moolenaar473de612013-06-08 18:19:48 +020019131 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019132 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019133
Bram Moolenaar0d660222005-01-07 21:51:51 +000019134 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019135}
19136
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019137#ifdef FEAT_FLOAT
19138/*
19139 * "sqrt()" function
19140 */
19141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019142f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019143{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019144 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019145
19146 rettv->v_type = VAR_FLOAT;
19147 if (get_float_arg(argvars, &f) == OK)
19148 rettv->vval.v_float = sqrt(f);
19149 else
19150 rettv->vval.v_float = 0.0;
19151}
19152
19153/*
19154 * "str2float()" function
19155 */
19156 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019157f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019158{
19159 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19160
19161 if (*p == '+')
19162 p = skipwhite(p + 1);
19163 (void)string2float(p, &rettv->vval.v_float);
19164 rettv->v_type = VAR_FLOAT;
19165}
19166#endif
19167
Bram Moolenaar2c932302006-03-18 21:42:09 +000019168/*
19169 * "str2nr()" function
19170 */
19171 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019172f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019173{
19174 int base = 10;
19175 char_u *p;
19176 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019177 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019178
19179 if (argvars[1].v_type != VAR_UNKNOWN)
19180 {
19181 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019182 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019183 {
19184 EMSG(_(e_invarg));
19185 return;
19186 }
19187 }
19188
19189 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019190 if (*p == '+')
19191 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019192 switch (base)
19193 {
19194 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19195 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19196 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19197 default: what = 0;
19198 }
19199 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019200 rettv->vval.v_number = n;
19201}
19202
Bram Moolenaar071d4272004-06-13 20:20:40 +000019203#ifdef HAVE_STRFTIME
19204/*
19205 * "strftime({format}[, {time}])" function
19206 */
19207 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019208f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019209{
19210 char_u result_buf[256];
19211 struct tm *curtime;
19212 time_t seconds;
19213 char_u *p;
19214
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019215 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019217 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019218 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019219 seconds = time(NULL);
19220 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019221 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019222 curtime = localtime(&seconds);
19223 /* MSVC returns NULL for an invalid value of seconds. */
19224 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019225 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019226 else
19227 {
19228# ifdef FEAT_MBYTE
19229 vimconv_T conv;
19230 char_u *enc;
19231
19232 conv.vc_type = CONV_NONE;
19233 enc = enc_locale();
19234 convert_setup(&conv, p_enc, enc);
19235 if (conv.vc_type != CONV_NONE)
19236 p = string_convert(&conv, p, NULL);
19237# endif
19238 if (p != NULL)
19239 (void)strftime((char *)result_buf, sizeof(result_buf),
19240 (char *)p, curtime);
19241 else
19242 result_buf[0] = NUL;
19243
19244# ifdef FEAT_MBYTE
19245 if (conv.vc_type != CONV_NONE)
19246 vim_free(p);
19247 convert_setup(&conv, enc, p_enc);
19248 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019249 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019250 else
19251# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019252 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019253
19254# ifdef FEAT_MBYTE
19255 /* Release conversion descriptors */
19256 convert_setup(&conv, NULL, NULL);
19257 vim_free(enc);
19258# endif
19259 }
19260}
19261#endif
19262
19263/*
19264 * "stridx()" function
19265 */
19266 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019267f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019268{
19269 char_u buf[NUMBUFLEN];
19270 char_u *needle;
19271 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019272 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019273 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019274 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019275
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019276 needle = get_tv_string_chk(&argvars[1]);
19277 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019278 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019279 if (needle == NULL || haystack == NULL)
19280 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019281
Bram Moolenaar33570922005-01-25 22:26:29 +000019282 if (argvars[2].v_type != VAR_UNKNOWN)
19283 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019284 int error = FALSE;
19285
19286 start_idx = get_tv_number_chk(&argvars[2], &error);
19287 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019288 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019289 if (start_idx >= 0)
19290 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019291 }
19292
19293 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19294 if (pos != NULL)
19295 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296}
19297
19298/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019299 * "string()" function
19300 */
19301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019302f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019303{
19304 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019305 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019306
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019307 rettv->v_type = VAR_STRING;
Bram Moolenaar24c77a12016-03-24 21:23:06 +010019308 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf,
19309 get_copyID());
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019310 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019311 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019312 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019313}
19314
19315/*
19316 * "strlen()" function
19317 */
19318 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019319f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019320{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019321 rettv->vval.v_number = (varnumber_T)(STRLEN(
19322 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019323}
19324
19325/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019326 * "strchars()" function
19327 */
19328 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019329f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019330{
19331 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019332 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019333#ifdef FEAT_MBYTE
19334 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019335 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019336#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019337
19338 if (argvars[1].v_type != VAR_UNKNOWN)
19339 skipcc = get_tv_number_chk(&argvars[1], NULL);
19340 if (skipcc < 0 || skipcc > 1)
19341 EMSG(_(e_invarg));
19342 else
19343 {
19344#ifdef FEAT_MBYTE
19345 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19346 while (*s != NUL)
19347 {
19348 func_mb_ptr2char_adv(&s);
19349 ++len;
19350 }
19351 rettv->vval.v_number = len;
19352#else
19353 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19354#endif
19355 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019356}
19357
19358/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019359 * "strdisplaywidth()" function
19360 */
19361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019362f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019363{
19364 char_u *s = get_tv_string(&argvars[0]);
19365 int col = 0;
19366
19367 if (argvars[1].v_type != VAR_UNKNOWN)
19368 col = get_tv_number(&argvars[1]);
19369
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019370 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019371}
19372
19373/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019374 * "strwidth()" function
19375 */
19376 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019377f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019378{
19379 char_u *s = get_tv_string(&argvars[0]);
19380
19381 rettv->vval.v_number = (varnumber_T)(
19382#ifdef FEAT_MBYTE
19383 mb_string2cells(s, -1)
19384#else
19385 STRLEN(s)
19386#endif
19387 );
19388}
19389
19390/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019391 * "strpart()" function
19392 */
19393 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019394f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019395{
19396 char_u *p;
19397 int n;
19398 int len;
19399 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019400 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019401
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019402 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019403 slen = (int)STRLEN(p);
19404
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019405 n = get_tv_number_chk(&argvars[1], &error);
19406 if (error)
19407 len = 0;
19408 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019409 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019410 else
19411 len = slen - n; /* default len: all bytes that are available. */
19412
19413 /*
19414 * Only return the overlap between the specified part and the actual
19415 * string.
19416 */
19417 if (n < 0)
19418 {
19419 len += n;
19420 n = 0;
19421 }
19422 else if (n > slen)
19423 n = slen;
19424 if (len < 0)
19425 len = 0;
19426 else if (n + len > slen)
19427 len = slen - n;
19428
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019429 rettv->v_type = VAR_STRING;
19430 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019431}
19432
19433/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019434 * "strridx()" function
19435 */
19436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019437f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019438{
19439 char_u buf[NUMBUFLEN];
19440 char_u *needle;
19441 char_u *haystack;
19442 char_u *rest;
19443 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019444 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019445
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019446 needle = get_tv_string_chk(&argvars[1]);
19447 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019448
19449 rettv->vval.v_number = -1;
19450 if (needle == NULL || haystack == NULL)
19451 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019452
19453 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019454 if (argvars[2].v_type != VAR_UNKNOWN)
19455 {
19456 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019458 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019459 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019460 }
19461 else
19462 end_idx = haystack_len;
19463
Bram Moolenaar0d660222005-01-07 21:51:51 +000019464 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019465 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019466 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019467 lastmatch = haystack + end_idx;
19468 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019469 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019470 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019471 for (rest = haystack; *rest != '\0'; ++rest)
19472 {
19473 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019474 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019475 break;
19476 lastmatch = rest;
19477 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019478 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019479
19480 if (lastmatch == NULL)
19481 rettv->vval.v_number = -1;
19482 else
19483 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19484}
19485
19486/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019487 * "strtrans()" function
19488 */
19489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019490f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019491{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019492 rettv->v_type = VAR_STRING;
19493 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019494}
19495
19496/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019497 * "submatch()" function
19498 */
19499 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019500f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019501{
Bram Moolenaar41571762014-04-02 19:00:58 +020019502 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019503 int no;
19504 int retList = 0;
19505
19506 no = (int)get_tv_number_chk(&argvars[0], &error);
19507 if (error)
19508 return;
19509 error = FALSE;
19510 if (argvars[1].v_type != VAR_UNKNOWN)
19511 retList = get_tv_number_chk(&argvars[1], &error);
19512 if (error)
19513 return;
19514
19515 if (retList == 0)
19516 {
19517 rettv->v_type = VAR_STRING;
19518 rettv->vval.v_string = reg_submatch(no);
19519 }
19520 else
19521 {
19522 rettv->v_type = VAR_LIST;
19523 rettv->vval.v_list = reg_submatch_list(no);
19524 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019525}
19526
19527/*
19528 * "substitute()" function
19529 */
19530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019531f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019532{
19533 char_u patbuf[NUMBUFLEN];
19534 char_u subbuf[NUMBUFLEN];
19535 char_u flagsbuf[NUMBUFLEN];
19536
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019537 char_u *str = get_tv_string_chk(&argvars[0]);
19538 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19539 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19540 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19541
Bram Moolenaar0d660222005-01-07 21:51:51 +000019542 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019543 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19544 rettv->vval.v_string = NULL;
19545 else
19546 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019547}
19548
19549/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019550 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019551 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019552 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019553f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019554{
19555 int id = 0;
19556#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019557 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019558 long col;
19559 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019560 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019562 lnum = get_tv_lnum(argvars); /* -1 on type error */
19563 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19564 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019566 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019567 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019568 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019569#endif
19570
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019571 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019572}
19573
19574/*
19575 * "synIDattr(id, what [, mode])" function
19576 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019577 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019578f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019579{
19580 char_u *p = NULL;
19581#ifdef FEAT_SYN_HL
19582 int id;
19583 char_u *what;
19584 char_u *mode;
19585 char_u modebuf[NUMBUFLEN];
19586 int modec;
19587
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019588 id = get_tv_number(&argvars[0]);
19589 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019590 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019591 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019592 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019593 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019594 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019595 modec = 0; /* replace invalid with current */
19596 }
19597 else
19598 {
19599#ifdef FEAT_GUI
19600 if (gui.in_use)
19601 modec = 'g';
19602 else
19603#endif
19604 if (t_colors > 1)
19605 modec = 'c';
19606 else
19607 modec = 't';
19608 }
19609
19610
19611 switch (TOLOWER_ASC(what[0]))
19612 {
19613 case 'b':
19614 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19615 p = highlight_color(id, what, modec);
19616 else /* bold */
19617 p = highlight_has_attr(id, HL_BOLD, modec);
19618 break;
19619
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019620 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019621 p = highlight_color(id, what, modec);
19622 break;
19623
19624 case 'i':
19625 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19626 p = highlight_has_attr(id, HL_INVERSE, modec);
19627 else /* italic */
19628 p = highlight_has_attr(id, HL_ITALIC, modec);
19629 break;
19630
19631 case 'n': /* name */
19632 p = get_highlight_name(NULL, id - 1);
19633 break;
19634
19635 case 'r': /* reverse */
19636 p = highlight_has_attr(id, HL_INVERSE, modec);
19637 break;
19638
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019639 case 's':
19640 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19641 p = highlight_color(id, what, modec);
19642 else /* standout */
19643 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019644 break;
19645
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019646 case 'u':
19647 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19648 /* underline */
19649 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19650 else
19651 /* undercurl */
19652 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019653 break;
19654 }
19655
19656 if (p != NULL)
19657 p = vim_strsave(p);
19658#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019659 rettv->v_type = VAR_STRING;
19660 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019661}
19662
19663/*
19664 * "synIDtrans(id)" function
19665 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019667f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019668{
19669 int id;
19670
19671#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019672 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019673
19674 if (id > 0)
19675 id = syn_get_final_id(id);
19676 else
19677#endif
19678 id = 0;
19679
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019680 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019681}
19682
19683/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019684 * "synconcealed(lnum, col)" function
19685 */
19686 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019687f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019688{
19689#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19690 long lnum;
19691 long col;
19692 int syntax_flags = 0;
19693 int cchar;
19694 int matchid = 0;
19695 char_u str[NUMBUFLEN];
19696#endif
19697
19698 rettv->v_type = VAR_LIST;
19699 rettv->vval.v_list = NULL;
19700
19701#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19702 lnum = get_tv_lnum(argvars); /* -1 on type error */
19703 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19704
19705 vim_memset(str, NUL, sizeof(str));
19706
19707 if (rettv_list_alloc(rettv) != FAIL)
19708 {
19709 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19710 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19711 && curwin->w_p_cole > 0)
19712 {
19713 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19714 syntax_flags = get_syntax_info(&matchid);
19715
19716 /* get the conceal character */
19717 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19718 {
19719 cchar = syn_get_sub_char();
19720 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19721 cchar = lcs_conceal;
19722 if (cchar != NUL)
19723 {
19724# ifdef FEAT_MBYTE
19725 if (has_mbyte)
19726 (*mb_char2bytes)(cchar, str);
19727 else
19728# endif
19729 str[0] = cchar;
19730 }
19731 }
19732 }
19733
19734 list_append_number(rettv->vval.v_list,
19735 (syntax_flags & HL_CONCEAL) != 0);
19736 /* -1 to auto-determine strlen */
19737 list_append_string(rettv->vval.v_list, str, -1);
19738 list_append_number(rettv->vval.v_list, matchid);
19739 }
19740#endif
19741}
19742
19743/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019744 * "synstack(lnum, col)" function
19745 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019747f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019748{
19749#ifdef FEAT_SYN_HL
19750 long lnum;
19751 long col;
19752 int i;
19753 int id;
19754#endif
19755
19756 rettv->v_type = VAR_LIST;
19757 rettv->vval.v_list = NULL;
19758
19759#ifdef FEAT_SYN_HL
19760 lnum = get_tv_lnum(argvars); /* -1 on type error */
19761 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19762
19763 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019764 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019765 && rettv_list_alloc(rettv) != FAIL)
19766 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019767 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019768 for (i = 0; ; ++i)
19769 {
19770 id = syn_get_stack_item(i);
19771 if (id < 0)
19772 break;
19773 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19774 break;
19775 }
19776 }
19777#endif
19778}
19779
Bram Moolenaar071d4272004-06-13 20:20:40 +000019780 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019781get_cmd_output_as_rettv(
19782 typval_T *argvars,
19783 typval_T *rettv,
19784 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019785{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019786 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019787 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019788 char_u *infile = NULL;
19789 char_u buf[NUMBUFLEN];
19790 int err = FALSE;
19791 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019792 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019793 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019795 rettv->v_type = VAR_STRING;
19796 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019797 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019798 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019799
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019800 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019801 {
19802 /*
19803 * Write the string to a temp file, to be used for input of the shell
19804 * command.
19805 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019806 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019807 {
19808 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019809 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019810 }
19811
19812 fd = mch_fopen((char *)infile, WRITEBIN);
19813 if (fd == NULL)
19814 {
19815 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019816 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019817 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019818 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019819 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019820 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19821 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019822 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019823 else
19824 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019825 size_t len;
19826
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019827 p = get_tv_string_buf_chk(&argvars[1], buf);
19828 if (p == NULL)
19829 {
19830 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019831 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019832 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019833 len = STRLEN(p);
19834 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019835 err = TRUE;
19836 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019837 if (fclose(fd) != 0)
19838 err = TRUE;
19839 if (err)
19840 {
19841 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019842 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019843 }
19844 }
19845
Bram Moolenaar52a72462014-08-29 15:53:52 +020019846 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19847 * echoes typeahead, that messes up the display. */
19848 if (!msg_silent)
19849 flags += SHELL_COOKED;
19850
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019851 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019852 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019853 int len;
19854 listitem_T *li;
19855 char_u *s = NULL;
19856 char_u *start;
19857 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019858 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019859
Bram Moolenaar52a72462014-08-29 15:53:52 +020019860 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019861 if (res == NULL)
19862 goto errret;
19863
19864 list = list_alloc();
19865 if (list == NULL)
19866 goto errret;
19867
19868 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019869 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019870 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019871 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019872 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019873 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019874
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019875 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019876 if (s == NULL)
19877 goto errret;
19878
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019879 for (p = s; start < end; ++p, ++start)
19880 *p = *start == NUL ? NL : *start;
19881 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019882
19883 li = listitem_alloc();
19884 if (li == NULL)
19885 {
19886 vim_free(s);
19887 goto errret;
19888 }
19889 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019890 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019891 li->li_tv.vval.v_string = s;
19892 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019893 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019894
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019895 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019896 rettv->v_type = VAR_LIST;
19897 rettv->vval.v_list = list;
19898 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019899 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019900 else
19901 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019902 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019903#ifdef USE_CR
19904 /* translate <CR> into <NL> */
19905 if (res != NULL)
19906 {
19907 char_u *s;
19908
19909 for (s = res; *s; ++s)
19910 {
19911 if (*s == CAR)
19912 *s = NL;
19913 }
19914 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019915#else
19916# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019917 /* translate <CR><NL> into <NL> */
19918 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019919 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019920 char_u *s, *d;
19921
19922 d = res;
19923 for (s = res; *s; ++s)
19924 {
19925 if (s[0] == CAR && s[1] == NL)
19926 ++s;
19927 *d++ = *s;
19928 }
19929 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019930 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019931# endif
19932#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019933 rettv->vval.v_string = res;
19934 res = NULL;
19935 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019936
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019937errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019938 if (infile != NULL)
19939 {
19940 mch_remove(infile);
19941 vim_free(infile);
19942 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019943 if (res != NULL)
19944 vim_free(res);
19945 if (list != NULL)
19946 list_free(list, TRUE);
19947}
19948
19949/*
19950 * "system()" function
19951 */
19952 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019953f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019954{
19955 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19956}
19957
19958/*
19959 * "systemlist()" function
19960 */
19961 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019962f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019963{
19964 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019965}
19966
19967/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019968 * "tabpagebuflist()" function
19969 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019970 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019971f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019972{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019973#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019974 tabpage_T *tp;
19975 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019976
19977 if (argvars[0].v_type == VAR_UNKNOWN)
19978 wp = firstwin;
19979 else
19980 {
19981 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19982 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019983 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019984 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019985 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019986 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019987 for (; wp != NULL; wp = wp->w_next)
19988 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019989 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019990 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019991 }
19992#endif
19993}
19994
19995
19996/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019997 * "tabpagenr()" function
19998 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019999 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020000f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020001{
20002 int nr = 1;
20003#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020004 char_u *arg;
20005
20006 if (argvars[0].v_type != VAR_UNKNOWN)
20007 {
20008 arg = get_tv_string_chk(&argvars[0]);
20009 nr = 0;
20010 if (arg != NULL)
20011 {
20012 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000020013 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020014 else
20015 EMSG2(_(e_invexpr2), arg);
20016 }
20017 }
20018 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020019 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020020#endif
20021 rettv->vval.v_number = nr;
20022}
20023
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020024
20025#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010020026static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020027
20028/*
20029 * Common code for tabpagewinnr() and winnr().
20030 */
20031 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020032get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020033{
20034 win_T *twin;
20035 int nr = 1;
20036 win_T *wp;
20037 char_u *arg;
20038
20039 twin = (tp == curtab) ? curwin : tp->tp_curwin;
20040 if (argvar->v_type != VAR_UNKNOWN)
20041 {
20042 arg = get_tv_string_chk(argvar);
20043 if (arg == NULL)
20044 nr = 0; /* type error; errmsg already given */
20045 else if (STRCMP(arg, "$") == 0)
20046 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
20047 else if (STRCMP(arg, "#") == 0)
20048 {
20049 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
20050 if (twin == NULL)
20051 nr = 0;
20052 }
20053 else
20054 {
20055 EMSG2(_(e_invexpr2), arg);
20056 nr = 0;
20057 }
20058 }
20059
20060 if (nr > 0)
20061 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
20062 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020063 {
20064 if (wp == NULL)
20065 {
20066 /* didn't find it in this tabpage */
20067 nr = 0;
20068 break;
20069 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020070 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000020071 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020072 return nr;
20073}
20074#endif
20075
20076/*
20077 * "tabpagewinnr()" function
20078 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020079 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020080f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020081{
20082 int nr = 1;
20083#ifdef FEAT_WINDOWS
20084 tabpage_T *tp;
20085
20086 tp = find_tabpage((int)get_tv_number(&argvars[0]));
20087 if (tp == NULL)
20088 nr = 0;
20089 else
20090 nr = get_winnr(tp, &argvars[1]);
20091#endif
20092 rettv->vval.v_number = nr;
20093}
20094
20095
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000020096/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020097 * "tagfiles()" function
20098 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020100f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020101{
Bram Moolenaard9462e32011-04-11 21:35:11 +020020102 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020103 tagname_T tn;
20104 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020105
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020106 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020107 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020108 fname = alloc(MAXPATHL);
20109 if (fname == NULL)
20110 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020111
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020112 for (first = TRUE; ; first = FALSE)
20113 if (get_tagfname(&tn, first, fname) == FAIL
20114 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020115 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020116 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020117 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020118}
20119
20120/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020121 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020122 */
20123 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020124f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020125{
20126 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020127
20128 tag_pattern = get_tv_string(&argvars[0]);
20129
20130 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020131 if (*tag_pattern == NUL)
20132 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020133
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020134 if (rettv_list_alloc(rettv) == OK)
20135 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020136}
20137
20138/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020139 * "tempname()" function
20140 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020142f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020143{
20144 static int x = 'A';
20145
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020146 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020147 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020148
20149 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20150 * names. Skip 'I' and 'O', they are used for shell redirection. */
20151 do
20152 {
20153 if (x == 'Z')
20154 x = '0';
20155 else if (x == '9')
20156 x = 'A';
20157 else
20158 {
20159#ifdef EBCDIC
20160 if (x == 'I')
20161 x = 'J';
20162 else if (x == 'R')
20163 x = 'S';
20164 else
20165#endif
20166 ++x;
20167 }
20168 } while (x == 'I' || x == 'O');
20169}
20170
20171/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020172 * "test(list)" function: Just checking the walls...
20173 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020174 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020175f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020176{
20177 /* Used for unit testing. Change the code below to your liking. */
20178#if 0
20179 listitem_T *li;
20180 list_T *l;
20181 char_u *bad, *good;
20182
20183 if (argvars[0].v_type != VAR_LIST)
20184 return;
20185 l = argvars[0].vval.v_list;
20186 if (l == NULL)
20187 return;
20188 li = l->lv_first;
20189 if (li == NULL)
20190 return;
20191 bad = get_tv_string(&li->li_tv);
20192 li = li->li_next;
20193 if (li == NULL)
20194 return;
20195 good = get_tv_string(&li->li_tv);
20196 rettv->vval.v_number = test_edit_score(bad, good);
20197#endif
20198}
20199
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020200#ifdef FEAT_FLOAT
20201/*
20202 * "tan()" function
20203 */
20204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020205f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020206{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020207 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020208
20209 rettv->v_type = VAR_FLOAT;
20210 if (get_float_arg(argvars, &f) == OK)
20211 rettv->vval.v_float = tan(f);
20212 else
20213 rettv->vval.v_float = 0.0;
20214}
20215
20216/*
20217 * "tanh()" function
20218 */
20219 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020220f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020221{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020222 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020223
20224 rettv->v_type = VAR_FLOAT;
20225 if (get_float_arg(argvars, &f) == OK)
20226 rettv->vval.v_float = tanh(f);
20227 else
20228 rettv->vval.v_float = 0.0;
20229}
20230#endif
20231
Bram Moolenaar975b5272016-03-15 23:10:59 +010020232#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20233/*
20234 * Get a callback from "arg". It can be a Funcref or a function name.
20235 * When "arg" is zero return an empty string.
20236 * Return NULL for an invalid argument.
20237 */
20238 char_u *
20239get_callback(typval_T *arg, partial_T **pp)
20240{
20241 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20242 {
20243 *pp = arg->vval.v_partial;
Bram Moolenaar92e35ef2016-03-26 18:20:41 +010020244 ++(*pp)->pt_refcount;
Bram Moolenaar975b5272016-03-15 23:10:59 +010020245 return (*pp)->pt_name;
20246 }
20247 *pp = NULL;
20248 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20249 return arg->vval.v_string;
20250 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20251 return (char_u *)"";
20252 EMSG(_("E921: Invalid callback argument"));
20253 return NULL;
20254}
20255#endif
20256
20257#ifdef FEAT_TIMERS
20258/*
20259 * "timer_start(time, callback [, options])" function
20260 */
20261 static void
20262f_timer_start(typval_T *argvars, typval_T *rettv)
20263{
20264 long msec = get_tv_number(&argvars[0]);
20265 timer_T *timer;
20266 int repeat = 0;
20267 char_u *callback;
20268 dict_T *dict;
20269
20270 if (argvars[2].v_type != VAR_UNKNOWN)
20271 {
20272 if (argvars[2].v_type != VAR_DICT
20273 || (dict = argvars[2].vval.v_dict) == NULL)
20274 {
20275 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20276 return;
20277 }
20278 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20279 repeat = get_dict_number(dict, (char_u *)"repeat");
20280 }
20281
20282 timer = create_timer(msec, repeat);
20283 callback = get_callback(&argvars[1], &timer->tr_partial);
20284 if (callback == NULL)
20285 {
20286 stop_timer(timer);
20287 rettv->vval.v_number = -1;
20288 }
20289 else
20290 {
20291 timer->tr_callback = vim_strsave(callback);
20292 rettv->vval.v_number = timer->tr_id;
20293 }
20294}
20295
20296/*
20297 * "timer_stop(timer)" function
20298 */
20299 static void
20300f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20301{
20302 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20303
20304 if (timer != NULL)
20305 stop_timer(timer);
20306}
20307#endif
20308
Bram Moolenaard52d9742005-08-21 22:20:28 +000020309/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020310 * "tolower(string)" function
20311 */
20312 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020313f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020314{
20315 char_u *p;
20316
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020317 p = vim_strsave(get_tv_string(&argvars[0]));
20318 rettv->v_type = VAR_STRING;
20319 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020320
20321 if (p != NULL)
20322 while (*p != NUL)
20323 {
20324#ifdef FEAT_MBYTE
20325 int l;
20326
20327 if (enc_utf8)
20328 {
20329 int c, lc;
20330
20331 c = utf_ptr2char(p);
20332 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020333 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020334 /* TODO: reallocate string when byte count changes. */
20335 if (utf_char2len(lc) == l)
20336 utf_char2bytes(lc, p);
20337 p += l;
20338 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020339 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020340 p += l; /* skip multi-byte character */
20341 else
20342#endif
20343 {
20344 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20345 ++p;
20346 }
20347 }
20348}
20349
20350/*
20351 * "toupper(string)" function
20352 */
20353 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020354f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020355{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020356 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020357 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020358}
20359
20360/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020361 * "tr(string, fromstr, tostr)" function
20362 */
20363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020364f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020365{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020366 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020367 char_u *fromstr;
20368 char_u *tostr;
20369 char_u *p;
20370#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020371 int inlen;
20372 int fromlen;
20373 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020374 int idx;
20375 char_u *cpstr;
20376 int cplen;
20377 int first = TRUE;
20378#endif
20379 char_u buf[NUMBUFLEN];
20380 char_u buf2[NUMBUFLEN];
20381 garray_T ga;
20382
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020383 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020384 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20385 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020386
20387 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020388 rettv->v_type = VAR_STRING;
20389 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020390 if (fromstr == NULL || tostr == NULL)
20391 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020392 ga_init2(&ga, (int)sizeof(char), 80);
20393
20394#ifdef FEAT_MBYTE
20395 if (!has_mbyte)
20396#endif
20397 /* not multi-byte: fromstr and tostr must be the same length */
20398 if (STRLEN(fromstr) != STRLEN(tostr))
20399 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020400#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020401error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020402#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020403 EMSG2(_(e_invarg2), fromstr);
20404 ga_clear(&ga);
20405 return;
20406 }
20407
20408 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020409 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020410 {
20411#ifdef FEAT_MBYTE
20412 if (has_mbyte)
20413 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020414 inlen = (*mb_ptr2len)(in_str);
20415 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020416 cplen = inlen;
20417 idx = 0;
20418 for (p = fromstr; *p != NUL; p += fromlen)
20419 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020420 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020421 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020422 {
20423 for (p = tostr; *p != NUL; p += tolen)
20424 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020425 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020426 if (idx-- == 0)
20427 {
20428 cplen = tolen;
20429 cpstr = p;
20430 break;
20431 }
20432 }
20433 if (*p == NUL) /* tostr is shorter than fromstr */
20434 goto error;
20435 break;
20436 }
20437 ++idx;
20438 }
20439
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020440 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020441 {
20442 /* Check that fromstr and tostr have the same number of
20443 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020444 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020445 first = FALSE;
20446 for (p = tostr; *p != NUL; p += tolen)
20447 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020448 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020449 --idx;
20450 }
20451 if (idx != 0)
20452 goto error;
20453 }
20454
Bram Moolenaarcde88542015-08-11 19:14:00 +020020455 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020456 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020457 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020458
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020459 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020460 }
20461 else
20462#endif
20463 {
20464 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020465 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020466 if (p != NULL)
20467 ga_append(&ga, tostr[p - fromstr]);
20468 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020469 ga_append(&ga, *in_str);
20470 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020471 }
20472 }
20473
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020474 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020475 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020476 ga_append(&ga, NUL);
20477
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020478 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020479}
20480
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020481#ifdef FEAT_FLOAT
20482/*
20483 * "trunc({float})" function
20484 */
20485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020486f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020487{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020488 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020489
20490 rettv->v_type = VAR_FLOAT;
20491 if (get_float_arg(argvars, &f) == OK)
20492 /* trunc() is not in C90, use floor() or ceil() instead. */
20493 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20494 else
20495 rettv->vval.v_float = 0.0;
20496}
20497#endif
20498
Bram Moolenaar8299df92004-07-10 09:47:34 +000020499/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020500 * "type(expr)" function
20501 */
20502 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020503f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020504{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020505 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020506
20507 switch (argvars[0].v_type)
20508 {
20509 case VAR_NUMBER: n = 0; break;
20510 case VAR_STRING: n = 1; break;
Bram Moolenaar953cc7f2016-03-19 18:52:29 +010020511 case VAR_PARTIAL:
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020512 case VAR_FUNC: n = 2; break;
20513 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020514 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020515 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020516 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020517 if (argvars[0].vval.v_number == VVAL_FALSE
20518 || argvars[0].vval.v_number == VVAL_TRUE)
20519 n = 6;
20520 else
20521 n = 7;
20522 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020523 case VAR_JOB: n = 8; break;
20524 case VAR_CHANNEL: n = 9; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020525 case VAR_UNKNOWN:
20526 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20527 n = -1;
20528 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020529 }
20530 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531}
20532
20533/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020534 * "undofile(name)" function
20535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020538{
20539 rettv->v_type = VAR_STRING;
20540#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020541 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020542 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020543
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020544 if (*fname == NUL)
20545 {
20546 /* If there is no file name there will be no undo file. */
20547 rettv->vval.v_string = NULL;
20548 }
20549 else
20550 {
20551 char_u *ffname = FullName_save(fname, FALSE);
20552
20553 if (ffname != NULL)
20554 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20555 vim_free(ffname);
20556 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020557 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020558#else
20559 rettv->vval.v_string = NULL;
20560#endif
20561}
20562
20563/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020564 * "undotree()" function
20565 */
20566 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020567f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020568{
20569 if (rettv_dict_alloc(rettv) == OK)
20570 {
20571 dict_T *dict = rettv->vval.v_dict;
20572 list_T *list;
20573
Bram Moolenaar730cde92010-06-27 05:18:54 +020020574 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020575 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020576 dict_add_nr_str(dict, "save_last",
20577 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020578 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20579 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020580 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020581
20582 list = list_alloc();
20583 if (list != NULL)
20584 {
20585 u_eval_tree(curbuf->b_u_oldhead, list);
20586 dict_add_list(dict, "entries", list);
20587 }
20588 }
20589}
20590
20591/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020592 * "values(dict)" function
20593 */
20594 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020595f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020596{
20597 dict_list(argvars, rettv, 1);
20598}
20599
20600/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020601 * "virtcol(string)" function
20602 */
20603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020604f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605{
20606 colnr_T vcol = 0;
20607 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020608 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020609
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020610 fp = var2fpos(&argvars[0], FALSE, &fnum);
20611 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20612 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020613 {
20614 getvvcol(curwin, fp, NULL, NULL, &vcol);
20615 ++vcol;
20616 }
20617
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020618 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020619}
20620
20621/*
20622 * "visualmode()" function
20623 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020624 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020625f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020626{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020627 char_u str[2];
20628
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020629 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020630 str[0] = curbuf->b_visual_mode_eval;
20631 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020632 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633
20634 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020635 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020636 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637}
20638
20639/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020640 * "wildmenumode()" function
20641 */
20642 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020643f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020644{
20645#ifdef FEAT_WILDMENU
20646 if (wild_menu_showing)
20647 rettv->vval.v_number = 1;
20648#endif
20649}
20650
20651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020652 * "winbufnr(nr)" function
20653 */
20654 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020655f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020656{
20657 win_T *wp;
20658
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020659 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020660 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020661 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020662 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020663 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020664}
20665
20666/*
20667 * "wincol()" function
20668 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020669 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020670f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020671{
20672 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020673 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020674}
20675
20676/*
20677 * "winheight(nr)" function
20678 */
20679 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020680f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020681{
20682 win_T *wp;
20683
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020684 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020685 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020686 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020687 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020688 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020689}
20690
20691/*
20692 * "winline()" function
20693 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020694 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020695f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020696{
20697 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020698 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020699}
20700
20701/*
20702 * "winnr()" function
20703 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020705f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020706{
20707 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020708
Bram Moolenaar071d4272004-06-13 20:20:40 +000020709#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020710 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020711#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020712 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020713}
20714
20715/*
20716 * "winrestcmd()" function
20717 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020718 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020719f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020720{
20721#ifdef FEAT_WINDOWS
20722 win_T *wp;
20723 int winnr = 1;
20724 garray_T ga;
20725 char_u buf[50];
20726
20727 ga_init2(&ga, (int)sizeof(char), 70);
20728 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20729 {
20730 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20731 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020732 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20733 ga_concat(&ga, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020734 ++winnr;
20735 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020736 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020737
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020738 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020739#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020740 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020741#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020742 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743}
20744
20745/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020746 * "winrestview()" function
20747 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020748 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020749f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020750{
20751 dict_T *dict;
20752
20753 if (argvars[0].v_type != VAR_DICT
20754 || (dict = argvars[0].vval.v_dict) == NULL)
20755 EMSG(_(e_invarg));
20756 else
20757 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020758 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20759 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20760 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20761 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020762#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020763 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20764 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020765#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020766 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20767 {
20768 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20769 curwin->w_set_curswant = FALSE;
20770 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020771
Bram Moolenaar82c25852014-05-28 16:47:16 +020020772 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20773 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020774#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020775 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20776 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020777#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020778 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20779 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20780 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20781 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020782
20783 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020784 win_new_height(curwin, curwin->w_height);
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020785# ifdef FEAT_WINDOWS
Bram Moolenaar6763c142012-07-19 18:05:44 +020020786 win_new_width(curwin, W_WIDTH(curwin));
20787# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020788 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020789
Bram Moolenaarb851a962014-10-31 15:45:52 +010020790 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020791 curwin->w_topline = 1;
20792 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20793 curwin->w_topline = curbuf->b_ml.ml_line_count;
20794#ifdef FEAT_DIFF
20795 check_topfill(curwin, TRUE);
20796#endif
20797 }
20798}
20799
20800/*
20801 * "winsaveview()" function
20802 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020803 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020804f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020805{
20806 dict_T *dict;
20807
Bram Moolenaara800b422010-06-27 01:15:55 +020020808 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020809 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020810 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020811
20812 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20813 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20814#ifdef FEAT_VIRTUALEDIT
20815 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20816#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020817 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020818 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20819
20820 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20821#ifdef FEAT_DIFF
20822 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20823#endif
20824 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20825 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20826}
20827
20828/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020829 * "winwidth(nr)" function
20830 */
20831 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020832f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020833{
20834 win_T *wp;
20835
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020836 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020837 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020839 else
Bram Moolenaar44a2f922016-03-19 22:11:51 +010020840#ifdef FEAT_WINDOWS
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020841 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020842#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020843 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020844#endif
20845}
20846
Bram Moolenaar071d4272004-06-13 20:20:40 +000020847/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020848 * "wordcount()" function
20849 */
20850 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020851f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020852{
20853 if (rettv_dict_alloc(rettv) == FAIL)
20854 return;
20855 cursor_pos_info(rettv->vval.v_dict);
20856}
20857
20858/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020859 * Write list of strings to file
20860 */
20861 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020862write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020863{
20864 listitem_T *li;
20865 int c;
20866 int ret = OK;
20867 char_u *s;
20868
20869 for (li = list->lv_first; li != NULL; li = li->li_next)
20870 {
20871 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20872 {
20873 if (*s == '\n')
20874 c = putc(NUL, fd);
20875 else
20876 c = putc(*s, fd);
20877 if (c == EOF)
20878 {
20879 ret = FAIL;
20880 break;
20881 }
20882 }
20883 if (!binary || li->li_next != NULL)
20884 if (putc('\n', fd) == EOF)
20885 {
20886 ret = FAIL;
20887 break;
20888 }
20889 if (ret == FAIL)
20890 {
20891 EMSG(_(e_write));
20892 break;
20893 }
20894 }
20895 return ret;
20896}
20897
20898/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020899 * "writefile()" function
20900 */
20901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020902f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020903{
20904 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020905 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020906 char_u *fname;
20907 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020908 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020909
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020910 if (check_restricted() || check_secure())
20911 return;
20912
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020913 if (argvars[0].v_type != VAR_LIST)
20914 {
20915 EMSG2(_(e_listarg), "writefile()");
20916 return;
20917 }
20918 if (argvars[0].vval.v_list == NULL)
20919 return;
20920
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020921 if (argvars[2].v_type != VAR_UNKNOWN)
20922 {
20923 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20924 binary = TRUE;
20925 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20926 append = TRUE;
20927 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020928
20929 /* Always open the file in binary mode, library functions have a mind of
20930 * their own about CR-LF conversion. */
20931 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020932 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20933 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020934 {
20935 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20936 ret = -1;
20937 }
20938 else
20939 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020940 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20941 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020942 fclose(fd);
20943 }
20944
20945 rettv->vval.v_number = ret;
20946}
20947
20948/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020949 * "xor(expr, expr)" function
20950 */
20951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020952f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020953{
20954 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20955 ^ get_tv_number_chk(&argvars[1], NULL);
20956}
20957
20958
20959/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020960 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020961 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020962 */
20963 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020964var2fpos(
20965 typval_T *varp,
20966 int dollar_lnum, /* TRUE when $ is last line */
20967 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020968{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020969 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020970 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020971 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020972
Bram Moolenaara5525202006-03-02 22:52:09 +000020973 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020974 if (varp->v_type == VAR_LIST)
20975 {
20976 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020977 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020978 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020979 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020980
20981 l = varp->vval.v_list;
20982 if (l == NULL)
20983 return NULL;
20984
20985 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020986 pos.lnum = list_find_nr(l, 0L, &error);
20987 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020988 return NULL; /* invalid line number */
20989
20990 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020991 pos.col = list_find_nr(l, 1L, &error);
20992 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020993 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020994 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020995
20996 /* We accept "$" for the column number: last column. */
20997 li = list_find(l, 1L);
20998 if (li != NULL && li->li_tv.v_type == VAR_STRING
20999 && li->li_tv.vval.v_string != NULL
21000 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
21001 pos.col = len + 1;
21002
Bram Moolenaara5525202006-03-02 22:52:09 +000021003 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000021004 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021005 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000021006 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021007
Bram Moolenaara5525202006-03-02 22:52:09 +000021008#ifdef FEAT_VIRTUALEDIT
21009 /* Get the virtual offset. Defaults to zero. */
21010 pos.coladd = list_find_nr(l, 2L, &error);
21011 if (error)
21012 pos.coladd = 0;
21013#endif
21014
Bram Moolenaar32466aa2006-02-24 23:53:04 +000021015 return &pos;
21016 }
21017
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021018 name = get_tv_string_chk(varp);
21019 if (name == NULL)
21020 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021021 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021022 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021023 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
21024 {
21025 if (VIsual_active)
21026 return &VIsual;
21027 return &curwin->w_cursor;
21028 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000021029 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021030 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010021031 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
21033 return NULL;
21034 return pp;
21035 }
Bram Moolenaara5525202006-03-02 22:52:09 +000021036
21037#ifdef FEAT_VIRTUALEDIT
21038 pos.coladd = 0;
21039#endif
21040
Bram Moolenaar477933c2007-07-17 14:32:23 +000021041 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021042 {
21043 pos.col = 0;
21044 if (name[1] == '0') /* "w0": first visible line */
21045 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021046 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021047 pos.lnum = curwin->w_topline;
21048 return &pos;
21049 }
21050 else if (name[1] == '$') /* "w$": last visible line */
21051 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000021052 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000021053 pos.lnum = curwin->w_botline - 1;
21054 return &pos;
21055 }
21056 }
21057 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021058 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000021059 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021060 {
21061 pos.lnum = curbuf->b_ml.ml_line_count;
21062 pos.col = 0;
21063 }
21064 else
21065 {
21066 pos.lnum = curwin->w_cursor.lnum;
21067 pos.col = (colnr_T)STRLEN(ml_get_curline());
21068 }
21069 return &pos;
21070 }
21071 return NULL;
21072}
21073
21074/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021075 * Convert list in "arg" into a position and optional file number.
21076 * When "fnump" is NULL there is no file number, only 3 items.
21077 * Note that the column is passed on as-is, the caller may want to decrement
21078 * it to use 1 for the first column.
21079 * Return FAIL when conversion is not possible, doesn't check the position for
21080 * validity.
21081 */
21082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021083list2fpos(
21084 typval_T *arg,
21085 pos_T *posp,
21086 int *fnump,
21087 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021088{
21089 list_T *l = arg->vval.v_list;
21090 long i = 0;
21091 long n;
21092
Bram Moolenaar493c1782014-05-28 14:34:46 +020021093 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
21094 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000021095 if (arg->v_type != VAR_LIST
21096 || l == NULL
21097 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020021098 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021099 return FAIL;
21100
21101 if (fnump != NULL)
21102 {
21103 n = list_find_nr(l, i++, NULL); /* fnum */
21104 if (n < 0)
21105 return FAIL;
21106 if (n == 0)
21107 n = curbuf->b_fnum; /* current buffer */
21108 *fnump = n;
21109 }
21110
21111 n = list_find_nr(l, i++, NULL); /* lnum */
21112 if (n < 0)
21113 return FAIL;
21114 posp->lnum = n;
21115
21116 n = list_find_nr(l, i++, NULL); /* col */
21117 if (n < 0)
21118 return FAIL;
21119 posp->col = n;
21120
21121#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021122 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021123 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021124 posp->coladd = 0;
21125 else
21126 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021127#endif
21128
Bram Moolenaar493c1782014-05-28 14:34:46 +020021129 if (curswantp != NULL)
21130 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21131
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021132 return OK;
21133}
21134
21135/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021136 * Get the length of an environment variable name.
21137 * Advance "arg" to the first character after the name.
21138 * Return 0 for error.
21139 */
21140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021141get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021142{
21143 char_u *p;
21144 int len;
21145
21146 for (p = *arg; vim_isIDc(*p); ++p)
21147 ;
21148 if (p == *arg) /* no name found */
21149 return 0;
21150
21151 len = (int)(p - *arg);
21152 *arg = p;
21153 return len;
21154}
21155
21156/*
21157 * Get the length of the name of a function or internal variable.
21158 * "arg" is advanced to the first non-white character after the name.
21159 * Return 0 if something is wrong.
21160 */
21161 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021162get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021163{
21164 char_u *p;
21165 int len;
21166
21167 /* Find the end of the name. */
21168 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021169 {
21170 if (*p == ':')
21171 {
21172 /* "s:" is start of "s:var", but "n:" is not and can be used in
21173 * slice "[n:]". Also "xx:" is not a namespace. */
21174 len = (int)(p - *arg);
21175 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21176 || len > 1)
21177 break;
21178 }
21179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021180 if (p == *arg) /* no name found */
21181 return 0;
21182
21183 len = (int)(p - *arg);
21184 *arg = skipwhite(p);
21185
21186 return len;
21187}
21188
21189/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021190 * Get the length of the name of a variable or function.
21191 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021192 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021193 * Return -1 if curly braces expansion failed.
21194 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021195 * If the name contains 'magic' {}'s, expand them and return the
21196 * expanded name in an allocated string via 'alias' - caller must free.
21197 */
21198 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021199get_name_len(
21200 char_u **arg,
21201 char_u **alias,
21202 int evaluate,
21203 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021204{
21205 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021206 char_u *p;
21207 char_u *expr_start;
21208 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021209
21210 *alias = NULL; /* default to no alias */
21211
21212 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21213 && (*arg)[2] == (int)KE_SNR)
21214 {
21215 /* hard coded <SNR>, already translated */
21216 *arg += 3;
21217 return get_id_len(arg) + 3;
21218 }
21219 len = eval_fname_script(*arg);
21220 if (len > 0)
21221 {
21222 /* literal "<SID>", "s:" or "<SNR>" */
21223 *arg += len;
21224 }
21225
Bram Moolenaar071d4272004-06-13 20:20:40 +000021226 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021227 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021228 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021229 p = find_name_end(*arg, &expr_start, &expr_end,
21230 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021231 if (expr_start != NULL)
21232 {
21233 char_u *temp_string;
21234
21235 if (!evaluate)
21236 {
21237 len += (int)(p - *arg);
21238 *arg = skipwhite(p);
21239 return len;
21240 }
21241
21242 /*
21243 * Include any <SID> etc in the expanded string:
21244 * Thus the -len here.
21245 */
21246 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21247 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021248 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021249 *alias = temp_string;
21250 *arg = skipwhite(p);
21251 return (int)STRLEN(temp_string);
21252 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021253
21254 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021255 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021256 EMSG2(_(e_invexpr2), *arg);
21257
21258 return len;
21259}
21260
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021261/*
21262 * Find the end of a variable or function name, taking care of magic braces.
21263 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21264 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021265 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021266 * Return a pointer to just after the name. Equal to "arg" if there is no
21267 * valid name.
21268 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021269 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021270find_name_end(
21271 char_u *arg,
21272 char_u **expr_start,
21273 char_u **expr_end,
21274 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021275{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021276 int mb_nest = 0;
21277 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021278 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021279 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021280
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021281 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021282 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021283 *expr_start = NULL;
21284 *expr_end = NULL;
21285 }
21286
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021287 /* Quick check for valid starting character. */
21288 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21289 return arg;
21290
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021291 for (p = arg; *p != NUL
21292 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021293 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021294 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021295 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021296 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021297 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021298 if (*p == '\'')
21299 {
21300 /* skip over 'string' to avoid counting [ and ] inside it. */
21301 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21302 ;
21303 if (*p == NUL)
21304 break;
21305 }
21306 else if (*p == '"')
21307 {
21308 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21309 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21310 if (*p == '\\' && p[1] != NUL)
21311 ++p;
21312 if (*p == NUL)
21313 break;
21314 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021315 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21316 {
21317 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021318 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021319 len = (int)(p - arg);
21320 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021321 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021322 break;
21323 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021325 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021326 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021327 if (*p == '[')
21328 ++br_nest;
21329 else if (*p == ']')
21330 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021331 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021332
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021333 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021334 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021335 if (*p == '{')
21336 {
21337 mb_nest++;
21338 if (expr_start != NULL && *expr_start == NULL)
21339 *expr_start = p;
21340 }
21341 else if (*p == '}')
21342 {
21343 mb_nest--;
21344 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21345 *expr_end = p;
21346 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021347 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021348 }
21349
21350 return p;
21351}
21352
21353/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021354 * Expands out the 'magic' {}'s in a variable/function name.
21355 * Note that this can call itself recursively, to deal with
21356 * constructs like foo{bar}{baz}{bam}
21357 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21358 * "in_start" ^
21359 * "expr_start" ^
21360 * "expr_end" ^
21361 * "in_end" ^
21362 *
21363 * Returns a new allocated string, which the caller must free.
21364 * Returns NULL for failure.
21365 */
21366 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367make_expanded_name(
21368 char_u *in_start,
21369 char_u *expr_start,
21370 char_u *expr_end,
21371 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021372{
21373 char_u c1;
21374 char_u *retval = NULL;
21375 char_u *temp_result;
21376 char_u *nextcmd = NULL;
21377
21378 if (expr_end == NULL || in_end == NULL)
21379 return NULL;
21380 *expr_start = NUL;
21381 *expr_end = NUL;
21382 c1 = *in_end;
21383 *in_end = NUL;
21384
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021385 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021386 if (temp_result != NULL && nextcmd == NULL)
21387 {
21388 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21389 + (in_end - expr_end) + 1));
21390 if (retval != NULL)
21391 {
21392 STRCPY(retval, in_start);
21393 STRCAT(retval, temp_result);
21394 STRCAT(retval, expr_end + 1);
21395 }
21396 }
21397 vim_free(temp_result);
21398
21399 *in_end = c1; /* put char back for error messages */
21400 *expr_start = '{';
21401 *expr_end = '}';
21402
21403 if (retval != NULL)
21404 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021405 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021406 if (expr_start != NULL)
21407 {
21408 /* Further expansion! */
21409 temp_result = make_expanded_name(retval, expr_start,
21410 expr_end, temp_result);
21411 vim_free(retval);
21412 retval = temp_result;
21413 }
21414 }
21415
21416 return retval;
21417}
21418
21419/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021420 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021421 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021422 */
21423 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021424eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021426 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21427}
21428
21429/*
21430 * Return TRUE if character "c" can be used as the first character in a
21431 * variable or function name (excluding '{' and '}').
21432 */
21433 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021434eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021435{
21436 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021437}
21438
21439/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021440 * Set number v: variable to "val".
21441 */
21442 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021443set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021444{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021445 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021446}
21447
21448/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021449 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021450 */
21451 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021452get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021453{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021454 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021455}
21456
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021457/*
21458 * Get string v: variable value. Uses a static buffer, can only be used once.
21459 */
21460 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021461get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021462{
21463 return get_tv_string(&vimvars[idx].vv_tv);
21464}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021465
Bram Moolenaar071d4272004-06-13 20:20:40 +000021466/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021467 * Get List v: variable value. Caller must take care of reference count when
21468 * needed.
21469 */
21470 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021471get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021472{
21473 return vimvars[idx].vv_list;
21474}
21475
21476/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021477 * Set v:char to character "c".
21478 */
21479 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021480set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021481{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021482 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021483
21484#ifdef FEAT_MBYTE
21485 if (has_mbyte)
21486 buf[(*mb_char2bytes)(c, buf)] = NUL;
21487 else
21488#endif
21489 {
21490 buf[0] = c;
21491 buf[1] = NUL;
21492 }
21493 set_vim_var_string(VV_CHAR, buf, -1);
21494}
21495
21496/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021497 * Set v:count to "count" and v:count1 to "count1".
21498 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021499 */
21500 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021501set_vcount(
21502 long count,
21503 long count1,
21504 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021505{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021506 if (set_prevcount)
21507 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021508 vimvars[VV_COUNT].vv_nr = count;
21509 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021510}
21511
21512/*
21513 * Set string v: variable to a copy of "val".
21514 */
21515 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021516set_vim_var_string(
21517 int idx,
21518 char_u *val,
21519 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021520{
Bram Moolenaara542c682016-01-31 16:28:04 +010021521 clear_tv(&vimvars[idx].vv_di.di_tv);
21522 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021523 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021524 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021525 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021526 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021527 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021528 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021529}
21530
21531/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021532 * Set List v: variable to "val".
21533 */
21534 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021535set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021536{
Bram Moolenaara542c682016-01-31 16:28:04 +010021537 clear_tv(&vimvars[idx].vv_di.di_tv);
21538 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021539 vimvars[idx].vv_list = val;
21540 if (val != NULL)
21541 ++val->lv_refcount;
21542}
21543
21544/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021545 * Set Dictionary v: variable to "val".
21546 */
21547 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021548set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021549{
21550 int todo;
21551 hashitem_T *hi;
21552
Bram Moolenaara542c682016-01-31 16:28:04 +010021553 clear_tv(&vimvars[idx].vv_di.di_tv);
21554 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021555 vimvars[idx].vv_dict = val;
21556 if (val != NULL)
21557 {
21558 ++val->dv_refcount;
21559
21560 /* Set readonly */
21561 todo = (int)val->dv_hashtab.ht_used;
21562 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21563 {
21564 if (HASHITEM_EMPTY(hi))
21565 continue;
21566 --todo;
21567 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21568 }
21569 }
21570}
21571
21572/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021573 * Set v:register if needed.
21574 */
21575 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021576set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021577{
21578 char_u regname;
21579
21580 if (c == 0 || c == ' ')
21581 regname = '"';
21582 else
21583 regname = c;
21584 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021585 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021586 set_vim_var_string(VV_REG, &regname, 1);
21587}
21588
21589/*
21590 * Get or set v:exception. If "oldval" == NULL, return the current value.
21591 * Otherwise, restore the value to "oldval" and return NULL.
21592 * Must always be called in pairs to save and restore v:exception! Does not
21593 * take care of memory allocations.
21594 */
21595 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021596v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021597{
21598 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021599 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021600
Bram Moolenaare9a41262005-01-15 22:18:47 +000021601 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021602 return NULL;
21603}
21604
21605/*
21606 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21607 * Otherwise, restore the value to "oldval" and return NULL.
21608 * Must always be called in pairs to save and restore v:throwpoint! Does not
21609 * take care of memory allocations.
21610 */
21611 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021612v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021613{
21614 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021615 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021616
Bram Moolenaare9a41262005-01-15 22:18:47 +000021617 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021618 return NULL;
21619}
21620
21621#if defined(FEAT_AUTOCMD) || defined(PROTO)
21622/*
21623 * Set v:cmdarg.
21624 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21625 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21626 * Must always be called in pairs!
21627 */
21628 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021629set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630{
21631 char_u *oldval;
21632 char_u *newval;
21633 unsigned len;
21634
Bram Moolenaare9a41262005-01-15 22:18:47 +000021635 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021636 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021637 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021638 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021639 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021640 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021641 }
21642
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021643 if (eap->force_bin == FORCE_BIN)
21644 len = 6;
21645 else if (eap->force_bin == FORCE_NOBIN)
21646 len = 8;
21647 else
21648 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021649
21650 if (eap->read_edit)
21651 len += 7;
21652
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021653 if (eap->force_ff != 0)
21654 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21655# ifdef FEAT_MBYTE
21656 if (eap->force_enc != 0)
21657 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021658 if (eap->bad_char != 0)
21659 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021660# endif
21661
21662 newval = alloc(len + 1);
21663 if (newval == NULL)
21664 return NULL;
21665
21666 if (eap->force_bin == FORCE_BIN)
21667 sprintf((char *)newval, " ++bin");
21668 else if (eap->force_bin == FORCE_NOBIN)
21669 sprintf((char *)newval, " ++nobin");
21670 else
21671 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021672
21673 if (eap->read_edit)
21674 STRCAT(newval, " ++edit");
21675
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021676 if (eap->force_ff != 0)
21677 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21678 eap->cmd + eap->force_ff);
21679# ifdef FEAT_MBYTE
21680 if (eap->force_enc != 0)
21681 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21682 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021683 if (eap->bad_char == BAD_KEEP)
21684 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21685 else if (eap->bad_char == BAD_DROP)
21686 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21687 else if (eap->bad_char != 0)
21688 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021689# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021690 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021691 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021692}
21693#endif
21694
21695/*
21696 * Get the value of internal variable "name".
21697 * Return OK or FAIL.
21698 */
21699 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021700get_var_tv(
21701 char_u *name,
21702 int len, /* length of "name" */
21703 typval_T *rettv, /* NULL when only checking existence */
21704 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21705 int verbose, /* may give error message */
21706 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021707{
21708 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021709 typval_T *tv = NULL;
21710 typval_T atv;
21711 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021712 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021713
21714 /* truncate the name, so that we can use strcmp() */
21715 cc = name[len];
21716 name[len] = NUL;
21717
21718 /*
21719 * Check for "b:changedtick".
21720 */
21721 if (STRCMP(name, "b:changedtick") == 0)
21722 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021723 atv.v_type = VAR_NUMBER;
21724 atv.vval.v_number = curbuf->b_changedtick;
21725 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021726 }
21727
21728 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021729 * Check for user-defined variables.
21730 */
21731 else
21732 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021733 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021734 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021735 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021736 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021737 if (dip != NULL)
21738 *dip = v;
21739 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021740 }
21741
Bram Moolenaare9a41262005-01-15 22:18:47 +000021742 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021743 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021744 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021745 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021746 ret = FAIL;
21747 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021748 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021749 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021750
21751 name[len] = cc;
21752
21753 return ret;
21754}
21755
21756/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021757 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21758 * Also handle function call with Funcref variable: func(expr)
21759 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21760 */
21761 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021762handle_subscript(
21763 char_u **arg,
21764 typval_T *rettv,
21765 int evaluate, /* do more than finding the end */
21766 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021767{
21768 int ret = OK;
21769 dict_T *selfdict = NULL;
21770 char_u *s;
21771 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021772 typval_T functv;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021773
21774 while (ret == OK
21775 && (**arg == '['
21776 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021777 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21778 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021779 && !vim_iswhite(*(*arg - 1)))
21780 {
21781 if (**arg == '(')
21782 {
Bram Moolenaar3f242a82016-03-18 19:39:25 +010021783 partial_T *pt = NULL;
21784
Bram Moolenaard9fba312005-06-26 22:34:35 +000021785 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021786 if (evaluate)
21787 {
21788 functv = *rettv;
21789 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021790
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021791 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021792 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021793 {
21794 pt = functv.vval.v_partial;
21795 s = pt->pt_name;
21796 }
21797 else
21798 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021799 }
21800 else
21801 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021802 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021803 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021804 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021805
21806 /* Clear the funcref afterwards, so that deleting it while
21807 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021808 if (evaluate)
21809 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021810
21811 /* Stop the expression evaluation when immediately aborting on
21812 * error, or when an interrupt occurred or an exception was thrown
21813 * but not caught. */
21814 if (aborting())
21815 {
21816 if (ret == OK)
21817 clear_tv(rettv);
21818 ret = FAIL;
21819 }
21820 dict_unref(selfdict);
21821 selfdict = NULL;
21822 }
21823 else /* **arg == '[' || **arg == '.' */
21824 {
21825 dict_unref(selfdict);
21826 if (rettv->v_type == VAR_DICT)
21827 {
21828 selfdict = rettv->vval.v_dict;
21829 if (selfdict != NULL)
21830 ++selfdict->dv_refcount;
21831 }
21832 else
21833 selfdict = NULL;
21834 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21835 {
21836 clear_tv(rettv);
21837 ret = FAIL;
21838 }
21839 }
21840 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021841
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021842 if ((rettv->v_type == VAR_FUNC || rettv->v_type == VAR_PARTIAL)
21843 && selfdict != NULL)
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021844 {
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021845 char_u *fname = rettv->v_type == VAR_FUNC ? rettv->vval.v_string
21846 : rettv->vval.v_partial->pt_name;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021847 char_u *tofree = NULL;
21848 ufunc_T *fp;
21849 char_u fname_buf[FLEN_FIXED + 1];
21850 int error;
21851
21852 /* Translate "s:func" to the stored function name. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021853 fname = fname_trans_sid(fname, fname_buf, &tofree, &error);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021854 fp = find_func(fname);
21855 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021856
21857 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021858 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021859 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021860 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21861
21862 if (pt != NULL)
21863 {
21864 pt->pt_refcount = 1;
21865 pt->pt_dict = selfdict;
21866 selfdict = NULL;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021867 if (rettv->v_type == VAR_FUNC)
21868 {
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021869 /* Just a function: Take over the function name and use
21870 * selfdict. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021871 pt->pt_name = rettv->vval.v_string;
21872 }
21873 else
21874 {
21875 partial_T *ret_pt = rettv->vval.v_partial;
21876 int i;
21877
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021878 /* Partial: copy the function name, use selfdict and copy
21879 * args. Can't take over name or args, the partial might
21880 * be referenced elsewhere. */
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021881 pt->pt_name = vim_strsave(ret_pt->pt_name);
Bram Moolenaare4eb6ff2016-03-22 21:00:09 +010021882 func_ref(pt->pt_name);
Bram Moolenaar9e63f612016-03-17 23:13:28 +010021883 if (ret_pt->pt_argc > 0)
21884 {
21885 pt->pt_argv = (typval_T *)alloc(
21886 sizeof(typval_T) * ret_pt->pt_argc);
21887 if (pt->pt_argv == NULL)
21888 /* out of memory: drop the arguments */
21889 pt->pt_argc = 0;
21890 else
21891 {
21892 pt->pt_argc = ret_pt->pt_argc;
21893 for (i = 0; i < pt->pt_argc; i++)
21894 copy_tv(&ret_pt->pt_argv[i], &pt->pt_argv[i]);
21895 }
21896 }
21897 partial_unref(ret_pt);
21898 }
Bram Moolenaar65639032016-03-16 21:40:30 +010021899 rettv->v_type = VAR_PARTIAL;
21900 rettv->vval.v_partial = pt;
21901 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021902 }
21903 }
21904
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021905 dict_unref(selfdict);
21906 return ret;
21907}
21908
21909/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021910 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021911 * value).
21912 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021913 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021914alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021915{
Bram Moolenaar33570922005-01-25 22:26:29 +000021916 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021917}
21918
21919/*
21920 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021921 * The string "s" must have been allocated, it is consumed.
21922 * Return NULL for out of memory, the variable otherwise.
21923 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021924 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021925alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021926{
Bram Moolenaar33570922005-01-25 22:26:29 +000021927 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021928
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021929 rettv = alloc_tv();
21930 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021931 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021932 rettv->v_type = VAR_STRING;
21933 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021934 }
21935 else
21936 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021937 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021938}
21939
21940/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021941 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021942 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021943 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021944free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021945{
21946 if (varp != NULL)
21947 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 switch (varp->v_type)
21949 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021950 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021951 func_unref(varp->vval.v_string);
21952 /*FALLTHROUGH*/
21953 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021954 vim_free(varp->vval.v_string);
21955 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021956 case VAR_PARTIAL:
21957 partial_unref(varp->vval.v_partial);
21958 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021959 case VAR_LIST:
21960 list_unref(varp->vval.v_list);
21961 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021962 case VAR_DICT:
21963 dict_unref(varp->vval.v_dict);
21964 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021965 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021966#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021967 job_unref(varp->vval.v_job);
21968 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021969#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021970 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021971#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021972 channel_unref(varp->vval.v_channel);
21973 break;
21974#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021975 case VAR_NUMBER:
21976 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021977 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021978 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021979 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021980 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021981 vim_free(varp);
21982 }
21983}
21984
21985/*
21986 * Free the memory for a variable value and set the value to NULL or 0.
21987 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021988 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021989clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021990{
21991 if (varp != NULL)
21992 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021993 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021994 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021995 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021996 func_unref(varp->vval.v_string);
21997 /*FALLTHROUGH*/
21998 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021999 vim_free(varp->vval.v_string);
22000 varp->vval.v_string = NULL;
22001 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022002 case VAR_PARTIAL:
22003 partial_unref(varp->vval.v_partial);
22004 varp->vval.v_partial = NULL;
22005 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022006 case VAR_LIST:
22007 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022008 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022009 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022010 case VAR_DICT:
22011 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000022012 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022013 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022014 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022015 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022016 varp->vval.v_number = 0;
22017 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022018 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022019#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022020 varp->vval.v_float = 0.0;
22021 break;
22022#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022023 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022024#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022025 job_unref(varp->vval.v_job);
22026 varp->vval.v_job = NULL;
22027#endif
22028 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022029 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022030#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022031 channel_unref(varp->vval.v_channel);
22032 varp->vval.v_channel = NULL;
22033#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022034 case VAR_UNKNOWN:
22035 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022036 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022037 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022038 }
22039}
22040
22041/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022042 * Set the value of a variable to NULL without freeing items.
22043 */
22044 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022045init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022046{
22047 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022048 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022049}
22050
22051/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022052 * Get the number value of a variable.
22053 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022054 * For incompatible types, return 0.
22055 * get_tv_number_chk() is similar to get_tv_number(), but informs the
22056 * caller of incompatible types: it sets *denote to TRUE if "denote"
22057 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022058 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022059 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022060get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022061{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022062 int error = FALSE;
22063
22064 return get_tv_number_chk(varp, &error); /* return 0L on error */
22065}
22066
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022067 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010022068get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022069{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022070 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022072 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022073 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022074 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022075 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022076 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022077#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000022078 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022079 break;
22080#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022081 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022082 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022083 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022084 break;
22085 case VAR_STRING:
22086 if (varp->vval.v_string != NULL)
22087 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010022088 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022089 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022090 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022091 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000022092 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022093 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000022094 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022095 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010022096 case VAR_SPECIAL:
22097 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
22098 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022099 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022100#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022101 EMSG(_("E910: Using a Job as a Number"));
22102 break;
22103#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022104 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022105#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022106 EMSG(_("E913: Using a Channel as a Number"));
22107 break;
22108#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022109 case VAR_UNKNOWN:
22110 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022111 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022112 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022113 if (denote == NULL) /* useful for values that must be unsigned */
22114 n = -1;
22115 else
22116 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022117 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022118}
22119
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022120#ifdef FEAT_FLOAT
22121 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022122get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022123{
22124 switch (varp->v_type)
22125 {
22126 case VAR_NUMBER:
22127 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022128 case VAR_FLOAT:
22129 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022130 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022131 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022132 EMSG(_("E891: Using a Funcref as a Float"));
22133 break;
22134 case VAR_STRING:
22135 EMSG(_("E892: Using a String as a Float"));
22136 break;
22137 case VAR_LIST:
22138 EMSG(_("E893: Using a List as a Float"));
22139 break;
22140 case VAR_DICT:
22141 EMSG(_("E894: Using a Dictionary as a Float"));
22142 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022143 case VAR_SPECIAL:
22144 EMSG(_("E907: Using a special value as a Float"));
22145 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022146 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022147# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022148 EMSG(_("E911: Using a Job as a Float"));
22149 break;
22150# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022151 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022152# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022153 EMSG(_("E914: Using a Channel as a Float"));
22154 break;
22155# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022156 case VAR_UNKNOWN:
22157 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022158 break;
22159 }
22160 return 0;
22161}
22162#endif
22163
Bram Moolenaar071d4272004-06-13 20:20:40 +000022164/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022165 * Get the lnum from the first argument.
22166 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022167 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022168 */
22169 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022170get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022171{
Bram Moolenaar33570922005-01-25 22:26:29 +000022172 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022173 linenr_T lnum;
22174
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022175 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022176 if (lnum == 0) /* no valid number, try using line() */
22177 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022178 rettv.v_type = VAR_NUMBER;
22179 f_line(argvars, &rettv);
22180 lnum = rettv.vval.v_number;
22181 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022182 }
22183 return lnum;
22184}
22185
22186/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022187 * Get the lnum from the first argument.
22188 * Also accepts "$", then "buf" is used.
22189 * Returns 0 on error.
22190 */
22191 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022192get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022193{
22194 if (argvars[0].v_type == VAR_STRING
22195 && argvars[0].vval.v_string != NULL
22196 && argvars[0].vval.v_string[0] == '$'
22197 && buf != NULL)
22198 return buf->b_ml.ml_line_count;
22199 return get_tv_number_chk(&argvars[0], NULL);
22200}
22201
22202/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022203 * Get the string value of a variable.
22204 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022205 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22206 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022207 * If the String variable has never been set, return an empty string.
22208 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022209 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22210 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022211 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022212 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022213get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022214{
22215 static char_u mybuf[NUMBUFLEN];
22216
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022217 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022218}
22219
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022220 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022221get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022222{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022223 char_u *res = get_tv_string_buf_chk(varp, buf);
22224
22225 return res != NULL ? res : (char_u *)"";
22226}
22227
Bram Moolenaar7d647822014-04-05 21:28:56 +020022228/*
22229 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22230 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022231 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022232get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022233{
22234 static char_u mybuf[NUMBUFLEN];
22235
22236 return get_tv_string_buf_chk(varp, mybuf);
22237}
22238
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022239 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022240get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022241{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022242 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022243 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022244 case VAR_NUMBER:
22245 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22246 return buf;
22247 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022248 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022249 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022250 break;
22251 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022252 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022253 break;
22254 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022255 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022256 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022257 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022258#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022259 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022260 break;
22261#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022262 case VAR_STRING:
22263 if (varp->vval.v_string != NULL)
22264 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022265 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022266 case VAR_SPECIAL:
22267 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22268 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022269 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022270#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022271 {
22272 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022273 char *status;
22274
22275 if (job == NULL)
22276 return (char_u *)"no process";
22277 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022278 : job->jv_status == JOB_ENDED ? "dead"
22279 : "run";
22280# ifdef UNIX
22281 vim_snprintf((char *)buf, NUMBUFLEN,
22282 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022283# elif defined(WIN32)
22284 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022285 "process %ld %s",
22286 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022287 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022288# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022289 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022290 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22291# endif
22292 return buf;
22293 }
22294#endif
22295 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022296 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022297#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022298 {
22299 channel_T *channel = varp->vval.v_channel;
22300 char *status = channel_status(channel);
22301
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022302 if (channel == NULL)
22303 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22304 else
22305 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022306 "channel %d %s", channel->ch_id, status);
22307 return buf;
22308 }
22309#endif
22310 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022311 case VAR_UNKNOWN:
22312 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022313 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022314 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022315 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022316}
22317
22318/*
22319 * Find variable "name" in the list of variables.
22320 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022321 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022322 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022323 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022324 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022325 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022326find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022327{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022328 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022329 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022330
Bram Moolenaara7043832005-01-21 11:56:39 +000022331 ht = find_var_ht(name, &varname);
22332 if (htp != NULL)
22333 *htp = ht;
22334 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022335 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022336 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022337}
22338
22339/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022340 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022341 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022342 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022343 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022344find_var_in_ht(
22345 hashtab_T *ht,
22346 int htname,
22347 char_u *varname,
22348 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022349{
Bram Moolenaar33570922005-01-25 22:26:29 +000022350 hashitem_T *hi;
22351
22352 if (*varname == NUL)
22353 {
22354 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022355 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022356 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022357 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022358 case 'g': return &globvars_var;
22359 case 'v': return &vimvars_var;
22360 case 'b': return &curbuf->b_bufvar;
22361 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022362#ifdef FEAT_WINDOWS
22363 case 't': return &curtab->tp_winvar;
22364#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022365 case 'l': return current_funccal == NULL
22366 ? NULL : &current_funccal->l_vars_var;
22367 case 'a': return current_funccal == NULL
22368 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022369 }
22370 return NULL;
22371 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022372
22373 hi = hash_find(ht, varname);
22374 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022375 {
22376 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022377 * worked find the variable again. Don't auto-load a script if it was
22378 * loaded already, otherwise it would be loaded every time when
22379 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022380 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022381 {
22382 /* Note: script_autoload() may make "hi" invalid. It must either
22383 * be obtained again or not used. */
22384 if (!script_autoload(varname, FALSE) || aborting())
22385 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022386 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022387 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022388 if (HASHITEM_EMPTY(hi))
22389 return NULL;
22390 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022391 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022392}
22393
22394/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022395 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022396 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022397 * Set "varname" to the start of name without ':'.
22398 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022399 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022400find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022401{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022402 hashitem_T *hi;
22403
Bram Moolenaar73627d02015-08-11 15:46:09 +020022404 if (name[0] == NUL)
22405 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022406 if (name[1] != ':')
22407 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022408 /* The name must not start with a colon or #. */
22409 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022410 return NULL;
22411 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022412
22413 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022414 hi = hash_find(&compat_hashtab, name);
22415 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022416 return &compat_hashtab;
22417
Bram Moolenaar071d4272004-06-13 20:20:40 +000022418 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022419 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022420 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022421 }
22422 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022423 if (*name == 'g') /* global variable */
22424 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022425 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22426 */
22427 if (vim_strchr(name + 2, ':') != NULL
22428 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022429 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022430 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022431 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022432 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022433 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022434#ifdef FEAT_WINDOWS
22435 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022436 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022437#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022438 if (*name == 'v') /* v: variable */
22439 return &vimvarht;
22440 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022441 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022442 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022443 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022444 if (*name == 's' /* script variable */
22445 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22446 return &SCRIPT_VARS(current_SID);
22447 return NULL;
22448}
22449
22450/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022451 * Get function call environment based on bactrace debug level
22452 */
22453 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022454get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022455{
22456 int i;
22457 funccall_T *funccal;
22458 funccall_T *temp_funccal;
22459
22460 funccal = current_funccal;
22461 if (debug_backtrace_level > 0)
22462 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022463 for (i = 0; i < debug_backtrace_level; i++)
22464 {
22465 temp_funccal = funccal->caller;
22466 if (temp_funccal)
22467 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022468 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022469 /* backtrace level overflow. reset to max */
22470 debug_backtrace_level = i;
22471 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022472 }
22473 return funccal;
22474}
22475
22476/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022478 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022479 * Returns NULL when it doesn't exist.
22480 */
22481 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022482get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022483{
Bram Moolenaar33570922005-01-25 22:26:29 +000022484 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022485
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022486 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022487 if (v == NULL)
22488 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022489 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022490}
22491
22492/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022493 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022494 * sourcing this script and when executing functions defined in the script.
22495 */
22496 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022497new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022498{
Bram Moolenaara7043832005-01-21 11:56:39 +000022499 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022500 hashtab_T *ht;
22501 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022502
Bram Moolenaar071d4272004-06-13 20:20:40 +000022503 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22504 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022505 /* Re-allocating ga_data means that an ht_array pointing to
22506 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022507 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022508 for (i = 1; i <= ga_scripts.ga_len; ++i)
22509 {
22510 ht = &SCRIPT_VARS(i);
22511 if (ht->ht_mask == HT_INIT_SIZE - 1)
22512 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022513 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022514 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022515 }
22516
Bram Moolenaar071d4272004-06-13 20:20:40 +000022517 while (ga_scripts.ga_len < id)
22518 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022519 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022520 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022521 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022522 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022523 }
22524 }
22525}
22526
22527/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022528 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22529 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022530 */
22531 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022532init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022533{
Bram Moolenaar33570922005-01-25 22:26:29 +000022534 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022535 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022536 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022537 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022538 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022539 dict_var->di_tv.vval.v_dict = dict;
22540 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022541 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022542 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22543 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022544}
22545
22546/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022547 * Unreference a dictionary initialized by init_var_dict().
22548 */
22549 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022550unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022551{
22552 /* Now the dict needs to be freed if no one else is using it, go back to
22553 * normal reference counting. */
22554 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22555 dict_unref(dict);
22556}
22557
22558/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022559 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022560 * Frees all allocated variables and the value they contain.
22561 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022562 */
22563 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022564vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022565{
22566 vars_clear_ext(ht, TRUE);
22567}
22568
22569/*
22570 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22571 */
22572 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022573vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022574{
Bram Moolenaara7043832005-01-21 11:56:39 +000022575 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022576 hashitem_T *hi;
22577 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022578
Bram Moolenaar33570922005-01-25 22:26:29 +000022579 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022580 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022581 for (hi = ht->ht_array; todo > 0; ++hi)
22582 {
22583 if (!HASHITEM_EMPTY(hi))
22584 {
22585 --todo;
22586
Bram Moolenaar33570922005-01-25 22:26:29 +000022587 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022588 * ht_array might change then. hash_clear() takes care of it
22589 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022590 v = HI2DI(hi);
22591 if (free_val)
22592 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022593 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022594 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022595 }
22596 }
22597 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022598 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022599}
22600
Bram Moolenaara7043832005-01-21 11:56:39 +000022601/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022602 * Delete a variable from hashtab "ht" at item "hi".
22603 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022604 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022605 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022606delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022607{
Bram Moolenaar33570922005-01-25 22:26:29 +000022608 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022609
22610 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022611 clear_tv(&di->di_tv);
22612 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022613}
22614
22615/*
22616 * List the value of one internal variable.
22617 */
22618 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022619list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022620{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022621 char_u *tofree;
22622 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022623 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022624
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022625 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022626 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022627 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022628 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022629}
22630
Bram Moolenaar071d4272004-06-13 20:20:40 +000022631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022632list_one_var_a(
22633 char_u *prefix,
22634 char_u *name,
22635 int type,
22636 char_u *string,
22637 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022638{
Bram Moolenaar31859182007-08-14 20:41:13 +000022639 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22640 msg_start();
22641 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022642 if (name != NULL) /* "a:" vars don't have a name stored */
22643 msg_puts(name);
22644 msg_putchar(' ');
22645 msg_advance(22);
22646 if (type == VAR_NUMBER)
22647 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022648 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649 msg_putchar('*');
22650 else if (type == VAR_LIST)
22651 {
22652 msg_putchar('[');
22653 if (*string == '[')
22654 ++string;
22655 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022656 else if (type == VAR_DICT)
22657 {
22658 msg_putchar('{');
22659 if (*string == '{')
22660 ++string;
22661 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022662 else
22663 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022664
Bram Moolenaar071d4272004-06-13 20:20:40 +000022665 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022666
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022667 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022668 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022669 if (*first)
22670 {
22671 msg_clr_eos();
22672 *first = FALSE;
22673 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022674}
22675
22676/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022677 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022678 * If the variable already exists, the value is updated.
22679 * Otherwise the variable is created.
22680 */
22681 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022682set_var(
22683 char_u *name,
22684 typval_T *tv,
22685 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022686{
Bram Moolenaar33570922005-01-25 22:26:29 +000022687 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022688 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022689 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022690
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022691 ht = find_var_ht(name, &varname);
22692 if (ht == NULL || *varname == NUL)
22693 {
22694 EMSG2(_(e_illvar), name);
22695 return;
22696 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022697 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022698
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022699 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22700 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022701 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022702
Bram Moolenaar33570922005-01-25 22:26:29 +000022703 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022704 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022705 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022706 if (var_check_ro(v->di_flags, name, FALSE)
22707 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022708 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022709
22710 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022711 * Handle setting internal v: variables separately where needed to
22712 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022713 */
22714 if (ht == &vimvarht)
22715 {
22716 if (v->di_tv.v_type == VAR_STRING)
22717 {
22718 vim_free(v->di_tv.vval.v_string);
22719 if (copy || tv->v_type != VAR_STRING)
22720 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22721 else
22722 {
22723 /* Take over the string to avoid an extra alloc/free. */
22724 v->di_tv.vval.v_string = tv->vval.v_string;
22725 tv->vval.v_string = NULL;
22726 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022727 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022728 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022729 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022730 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022731 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022732 if (STRCMP(varname, "searchforward") == 0)
22733 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022734#ifdef FEAT_SEARCH_EXTRA
22735 else if (STRCMP(varname, "hlsearch") == 0)
22736 {
22737 no_hlsearch = !v->di_tv.vval.v_number;
22738 redraw_all_later(SOME_VALID);
22739 }
22740#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022741 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022742 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022743 else if (v->di_tv.v_type != tv->v_type)
22744 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022745 }
22746
22747 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022748 }
22749 else /* add a new variable */
22750 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022751 /* Can't add "v:" variable. */
22752 if (ht == &vimvarht)
22753 {
22754 EMSG2(_(e_illvar), name);
22755 return;
22756 }
22757
Bram Moolenaar92124a32005-06-17 22:03:40 +000022758 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022759 if (!valid_varname(varname))
22760 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022761
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022762 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22763 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022764 if (v == NULL)
22765 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022766 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022767 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022768 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022769 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770 return;
22771 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022772 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022773 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022774
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022775 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022776 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022777 else
22778 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022779 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022780 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022781 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022782 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022783}
22784
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022785/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022786 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022787 * Also give an error message.
22788 */
22789 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022790var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022791{
22792 if (flags & DI_FLAGS_RO)
22793 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022794 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022795 return TRUE;
22796 }
22797 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22798 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022799 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022800 return TRUE;
22801 }
22802 return FALSE;
22803}
22804
22805/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022806 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22807 * Also give an error message.
22808 */
22809 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022810var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022811{
22812 if (flags & DI_FLAGS_FIX)
22813 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022814 EMSG2(_("E795: Cannot delete variable %s"),
22815 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022816 return TRUE;
22817 }
22818 return FALSE;
22819}
22820
22821/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022822 * Check if a funcref is assigned to a valid variable name.
22823 * Return TRUE and give an error if not.
22824 */
22825 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022826var_check_func_name(
22827 char_u *name, /* points to start of variable name */
22828 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022829{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022830 /* Allow for w: b: s: and t:. */
22831 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022832 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22833 ? name[2] : name[0]))
22834 {
22835 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22836 name);
22837 return TRUE;
22838 }
22839 /* Don't allow hiding a function. When "v" is not NULL we might be
22840 * assigning another function to the same var, the type is checked
22841 * below. */
22842 if (new_var && function_exists(name))
22843 {
22844 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22845 name);
22846 return TRUE;
22847 }
22848 return FALSE;
22849}
22850
22851/*
22852 * Check if a variable name is valid.
22853 * Return FALSE and give an error if not.
22854 */
22855 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022856valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022857{
22858 char_u *p;
22859
22860 for (p = varname; *p != NUL; ++p)
22861 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22862 && *p != AUTOLOAD_CHAR)
22863 {
22864 EMSG2(_(e_illvar), varname);
22865 return FALSE;
22866 }
22867 return TRUE;
22868}
22869
22870/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022871 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022872 * Also give an error message, using "name" or _("name") when use_gettext is
22873 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022874 */
22875 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022876tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022877{
22878 if (lock & VAR_LOCKED)
22879 {
22880 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022881 name == NULL ? (char_u *)_("Unknown")
22882 : use_gettext ? (char_u *)_(name)
22883 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022884 return TRUE;
22885 }
22886 if (lock & VAR_FIXED)
22887 {
22888 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022889 name == NULL ? (char_u *)_("Unknown")
22890 : use_gettext ? (char_u *)_(name)
22891 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022892 return TRUE;
22893 }
22894 return FALSE;
22895}
22896
22897/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022898 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022899 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022900 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022901 * It is OK for "from" and "to" to point to the same item. This is used to
22902 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022903 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022904 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022905copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022906{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022907 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022908 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022909 switch (from->v_type)
22910 {
22911 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022912 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022913 to->vval.v_number = from->vval.v_number;
22914 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022915 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022916#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022917 to->vval.v_float = from->vval.v_float;
22918 break;
22919#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022920 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022921#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022922 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022923 if (to->vval.v_job != NULL)
22924 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022925 break;
22926#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022927 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022928#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022929 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022930 if (to->vval.v_channel != NULL)
22931 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022932 break;
22933#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022934 case VAR_STRING:
22935 case VAR_FUNC:
22936 if (from->vval.v_string == NULL)
22937 to->vval.v_string = NULL;
22938 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022939 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022940 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022941 if (from->v_type == VAR_FUNC)
22942 func_ref(to->vval.v_string);
22943 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022944 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022945 case VAR_PARTIAL:
22946 if (from->vval.v_partial == NULL)
22947 to->vval.v_partial = NULL;
22948 else
22949 {
22950 to->vval.v_partial = from->vval.v_partial;
22951 ++to->vval.v_partial->pt_refcount;
22952 }
22953 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022954 case VAR_LIST:
22955 if (from->vval.v_list == NULL)
22956 to->vval.v_list = NULL;
22957 else
22958 {
22959 to->vval.v_list = from->vval.v_list;
22960 ++to->vval.v_list->lv_refcount;
22961 }
22962 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022963 case VAR_DICT:
22964 if (from->vval.v_dict == NULL)
22965 to->vval.v_dict = NULL;
22966 else
22967 {
22968 to->vval.v_dict = from->vval.v_dict;
22969 ++to->vval.v_dict->dv_refcount;
22970 }
22971 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022972 case VAR_UNKNOWN:
22973 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022974 break;
22975 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022976}
22977
22978/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022979 * Make a copy of an item.
22980 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022981 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22982 * reference to an already copied list/dict can be used.
22983 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022984 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022985 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022986item_copy(
22987 typval_T *from,
22988 typval_T *to,
22989 int deep,
22990 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022991{
22992 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022993 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022994
Bram Moolenaar33570922005-01-25 22:26:29 +000022995 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022996 {
22997 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022998 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022999 }
23000 ++recurse;
23001
23002 switch (from->v_type)
23003 {
23004 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023005 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023006 case VAR_STRING:
23007 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023008 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010023009 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010023010 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010023011 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000023012 copy_tv(from, to);
23013 break;
23014 case VAR_LIST:
23015 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023016 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023017 if (from->vval.v_list == NULL)
23018 to->vval.v_list = NULL;
23019 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
23020 {
23021 /* use the copy made earlier */
23022 to->vval.v_list = from->vval.v_list->lv_copylist;
23023 ++to->vval.v_list->lv_refcount;
23024 }
23025 else
23026 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
23027 if (to->vval.v_list == NULL)
23028 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023029 break;
23030 case VAR_DICT:
23031 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023032 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023033 if (from->vval.v_dict == NULL)
23034 to->vval.v_dict = NULL;
23035 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
23036 {
23037 /* use the copy made earlier */
23038 to->vval.v_dict = from->vval.v_dict->dv_copydict;
23039 ++to->vval.v_dict->dv_refcount;
23040 }
23041 else
23042 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
23043 if (to->vval.v_dict == NULL)
23044 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023045 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010023046 case VAR_UNKNOWN:
23047 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023048 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023049 }
23050 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023051 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000023052}
23053
23054/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000023055 * ":echo expr1 ..." print each argument separated with a space, add a
23056 * newline at the end.
23057 * ":echon expr1 ..." print each argument plain.
23058 */
23059 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023060ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023061{
23062 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023063 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023064 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023065 char_u *p;
23066 int needclr = TRUE;
23067 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000023068 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023069
23070 if (eap->skip)
23071 ++emsg_skip;
23072 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
23073 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023074 /* If eval1() causes an error message the text from the command may
23075 * still need to be cleared. E.g., "echo 22,44". */
23076 need_clr_eos = needclr;
23077
Bram Moolenaar071d4272004-06-13 20:20:40 +000023078 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023079 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023080 {
23081 /*
23082 * Report the invalid expression unless the expression evaluation
23083 * has been cancelled due to an aborting error, an interrupt, or an
23084 * exception.
23085 */
23086 if (!aborting())
23087 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023088 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 break;
23090 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000023091 need_clr_eos = FALSE;
23092
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 if (!eap->skip)
23094 {
23095 if (atstart)
23096 {
23097 atstart = FALSE;
23098 /* Call msg_start() after eval1(), evaluating the expression
23099 * may cause a message to appear. */
23100 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010023101 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020023102 /* Mark the saved text as finishing the line, so that what
23103 * follows is displayed on a new line when scrolling back
23104 * at the more prompt. */
23105 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000023106 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010023107 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023108 }
23109 else if (eap->cmdidx == CMD_echo)
23110 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010023111 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023112 if (p != NULL)
23113 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023114 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023115 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023116 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023117 if (*p != TAB && needclr)
23118 {
23119 /* remove any text still there from the command */
23120 msg_clr_eos();
23121 needclr = FALSE;
23122 }
23123 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023124 }
23125 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023126 {
23127#ifdef FEAT_MBYTE
23128 if (has_mbyte)
23129 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000023130 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023131
23132 (void)msg_outtrans_len_attr(p, i, echo_attr);
23133 p += i - 1;
23134 }
23135 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023136#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023137 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23138 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023139 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023140 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023141 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023142 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023143 arg = skipwhite(arg);
23144 }
23145 eap->nextcmd = check_nextcmd(arg);
23146
23147 if (eap->skip)
23148 --emsg_skip;
23149 else
23150 {
23151 /* remove text that may still be there from the command */
23152 if (needclr)
23153 msg_clr_eos();
23154 if (eap->cmdidx == CMD_echo)
23155 msg_end();
23156 }
23157}
23158
23159/*
23160 * ":echohl {name}".
23161 */
23162 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023163ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023164{
23165 int id;
23166
23167 id = syn_name2id(eap->arg);
23168 if (id == 0)
23169 echo_attr = 0;
23170 else
23171 echo_attr = syn_id2attr(id);
23172}
23173
23174/*
23175 * ":execute expr1 ..." execute the result of an expression.
23176 * ":echomsg expr1 ..." Print a message
23177 * ":echoerr expr1 ..." Print an error
23178 * Each gets spaces around each argument and a newline at the end for
23179 * echo commands
23180 */
23181 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023182ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023183{
23184 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023185 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023186 int ret = OK;
23187 char_u *p;
23188 garray_T ga;
23189 int len;
23190 int save_did_emsg;
23191
23192 ga_init2(&ga, 1, 80);
23193
23194 if (eap->skip)
23195 ++emsg_skip;
23196 while (*arg != NUL && *arg != '|' && *arg != '\n')
23197 {
23198 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023199 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023200 {
23201 /*
23202 * Report the invalid expression unless the expression evaluation
23203 * has been cancelled due to an aborting error, an interrupt, or an
23204 * exception.
23205 */
23206 if (!aborting())
23207 EMSG2(_(e_invexpr2), p);
23208 ret = FAIL;
23209 break;
23210 }
23211
23212 if (!eap->skip)
23213 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023214 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023215 len = (int)STRLEN(p);
23216 if (ga_grow(&ga, len + 2) == FAIL)
23217 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023218 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023219 ret = FAIL;
23220 break;
23221 }
23222 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023223 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023224 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023225 ga.ga_len += len;
23226 }
23227
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023228 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023229 arg = skipwhite(arg);
23230 }
23231
23232 if (ret != FAIL && ga.ga_data != NULL)
23233 {
23234 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023235 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023236 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023237 out_flush();
23238 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023239 else if (eap->cmdidx == CMD_echoerr)
23240 {
23241 /* We don't want to abort following commands, restore did_emsg. */
23242 save_did_emsg = did_emsg;
23243 EMSG((char_u *)ga.ga_data);
23244 if (!force_abort)
23245 did_emsg = save_did_emsg;
23246 }
23247 else if (eap->cmdidx == CMD_execute)
23248 do_cmdline((char_u *)ga.ga_data,
23249 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23250 }
23251
23252 ga_clear(&ga);
23253
23254 if (eap->skip)
23255 --emsg_skip;
23256
23257 eap->nextcmd = check_nextcmd(arg);
23258}
23259
23260/*
23261 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23262 * "arg" points to the "&" or '+' when called, to "option" when returning.
23263 * Returns NULL when no option name found. Otherwise pointer to the char
23264 * after the option name.
23265 */
23266 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023267find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023268{
23269 char_u *p = *arg;
23270
23271 ++p;
23272 if (*p == 'g' && p[1] == ':')
23273 {
23274 *opt_flags = OPT_GLOBAL;
23275 p += 2;
23276 }
23277 else if (*p == 'l' && p[1] == ':')
23278 {
23279 *opt_flags = OPT_LOCAL;
23280 p += 2;
23281 }
23282 else
23283 *opt_flags = 0;
23284
23285 if (!ASCII_ISALPHA(*p))
23286 return NULL;
23287 *arg = p;
23288
23289 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23290 p += 4; /* termcap option */
23291 else
23292 while (ASCII_ISALPHA(*p))
23293 ++p;
23294 return p;
23295}
23296
23297/*
23298 * ":function"
23299 */
23300 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023301ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023302{
23303 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023304 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023305 int j;
23306 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023307 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023308 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023309 char_u *name = NULL;
23310 char_u *p;
23311 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023312 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023313 garray_T newargs;
23314 garray_T newlines;
23315 int varargs = FALSE;
23316 int mustend = FALSE;
23317 int flags = 0;
23318 ufunc_T *fp;
23319 int indent;
23320 int nesting;
23321 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023322 dictitem_T *v;
23323 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023324 static int func_nr = 0; /* number for nameless function */
23325 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023326 hashtab_T *ht;
23327 int todo;
23328 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023329 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023330
23331 /*
23332 * ":function" without argument: list functions.
23333 */
23334 if (ends_excmd(*eap->arg))
23335 {
23336 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023337 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023338 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023339 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023340 {
23341 if (!HASHITEM_EMPTY(hi))
23342 {
23343 --todo;
23344 fp = HI2UF(hi);
23345 if (!isdigit(*fp->uf_name))
23346 list_func_head(fp, FALSE);
23347 }
23348 }
23349 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023350 eap->nextcmd = check_nextcmd(eap->arg);
23351 return;
23352 }
23353
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023354 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023355 * ":function /pat": list functions matching pattern.
23356 */
23357 if (*eap->arg == '/')
23358 {
23359 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23360 if (!eap->skip)
23361 {
23362 regmatch_T regmatch;
23363
23364 c = *p;
23365 *p = NUL;
23366 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23367 *p = c;
23368 if (regmatch.regprog != NULL)
23369 {
23370 regmatch.rm_ic = p_ic;
23371
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023372 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023373 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23374 {
23375 if (!HASHITEM_EMPTY(hi))
23376 {
23377 --todo;
23378 fp = HI2UF(hi);
23379 if (!isdigit(*fp->uf_name)
23380 && vim_regexec(&regmatch, fp->uf_name, 0))
23381 list_func_head(fp, FALSE);
23382 }
23383 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023384 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023385 }
23386 }
23387 if (*p == '/')
23388 ++p;
23389 eap->nextcmd = check_nextcmd(p);
23390 return;
23391 }
23392
23393 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023394 * Get the function name. There are these situations:
23395 * func normal function name
23396 * "name" == func, "fudi.fd_dict" == NULL
23397 * dict.func new dictionary entry
23398 * "name" == NULL, "fudi.fd_dict" set,
23399 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23400 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023401 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023402 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23403 * dict.func existing dict entry that's not a Funcref
23404 * "name" == NULL, "fudi.fd_dict" set,
23405 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023406 * s:func script-local function name
23407 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023408 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023409 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023410 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023411 paren = (vim_strchr(p, '(') != NULL);
23412 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023413 {
23414 /*
23415 * Return on an invalid expression in braces, unless the expression
23416 * evaluation has been cancelled due to an aborting error, an
23417 * interrupt, or an exception.
23418 */
23419 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023420 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023421 if (!eap->skip && fudi.fd_newkey != NULL)
23422 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023423 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023424 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023425 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023426 else
23427 eap->skip = TRUE;
23428 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023429
Bram Moolenaar071d4272004-06-13 20:20:40 +000023430 /* An error in a function call during evaluation of an expression in magic
23431 * braces should not cause the function not to be defined. */
23432 saved_did_emsg = did_emsg;
23433 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434
23435 /*
23436 * ":function func" with only function name: list function.
23437 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023438 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023439 {
23440 if (!ends_excmd(*skipwhite(p)))
23441 {
23442 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023443 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023444 }
23445 eap->nextcmd = check_nextcmd(p);
23446 if (eap->nextcmd != NULL)
23447 *p = NUL;
23448 if (!eap->skip && !got_int)
23449 {
23450 fp = find_func(name);
23451 if (fp != NULL)
23452 {
23453 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023454 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023455 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023456 if (FUNCLINE(fp, j) == NULL)
23457 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023458 msg_putchar('\n');
23459 msg_outnum((long)(j + 1));
23460 if (j < 9)
23461 msg_putchar(' ');
23462 if (j < 99)
23463 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023464 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 out_flush(); /* show a line at a time */
23466 ui_breakcheck();
23467 }
23468 if (!got_int)
23469 {
23470 msg_putchar('\n');
23471 msg_puts((char_u *)" endfunction");
23472 }
23473 }
23474 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023475 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023476 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023477 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023478 }
23479
23480 /*
23481 * ":function name(arg1, arg2)" Define function.
23482 */
23483 p = skipwhite(p);
23484 if (*p != '(')
23485 {
23486 if (!eap->skip)
23487 {
23488 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023490 }
23491 /* attempt to continue by skipping some text */
23492 if (vim_strchr(p, '(') != NULL)
23493 p = vim_strchr(p, '(');
23494 }
23495 p = skipwhite(p + 1);
23496
23497 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23498 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23499
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023500 if (!eap->skip)
23501 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023502 /* Check the name of the function. Unless it's a dictionary function
23503 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023504 if (name != NULL)
23505 arg = name;
23506 else
23507 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023508 if (arg != NULL && (fudi.fd_di == NULL
Bram Moolenaarc5fbe8a2016-03-24 21:42:09 +010023509 || (fudi.fd_di->di_tv.v_type != VAR_FUNC
23510 && fudi.fd_di->di_tv.v_type != VAR_PARTIAL)))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023511 {
23512 if (*arg == K_SPECIAL)
23513 j = 3;
23514 else
23515 j = 0;
23516 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23517 : eval_isnamec(arg[j])))
23518 ++j;
23519 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023520 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023521 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023522 /* Disallow using the g: dict. */
23523 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23524 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023525 }
23526
Bram Moolenaar071d4272004-06-13 20:20:40 +000023527 /*
23528 * Isolate the arguments: "arg1, arg2, ...)"
23529 */
23530 while (*p != ')')
23531 {
23532 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23533 {
23534 varargs = TRUE;
23535 p += 3;
23536 mustend = TRUE;
23537 }
23538 else
23539 {
23540 arg = p;
23541 while (ASCII_ISALNUM(*p) || *p == '_')
23542 ++p;
23543 if (arg == p || isdigit(*arg)
23544 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23545 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23546 {
23547 if (!eap->skip)
23548 EMSG2(_("E125: Illegal argument: %s"), arg);
23549 break;
23550 }
23551 if (ga_grow(&newargs, 1) == FAIL)
23552 goto erret;
23553 c = *p;
23554 *p = NUL;
23555 arg = vim_strsave(arg);
23556 if (arg == NULL)
23557 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023558
23559 /* Check for duplicate argument name. */
23560 for (i = 0; i < newargs.ga_len; ++i)
23561 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23562 {
23563 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023564 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023565 goto erret;
23566 }
23567
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23569 *p = c;
23570 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023571 if (*p == ',')
23572 ++p;
23573 else
23574 mustend = TRUE;
23575 }
23576 p = skipwhite(p);
23577 if (mustend && *p != ')')
23578 {
23579 if (!eap->skip)
23580 EMSG2(_(e_invarg2), eap->arg);
23581 break;
23582 }
23583 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023584 if (*p != ')')
23585 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 ++p; /* skip the ')' */
23587
Bram Moolenaare9a41262005-01-15 22:18:47 +000023588 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 for (;;)
23590 {
23591 p = skipwhite(p);
23592 if (STRNCMP(p, "range", 5) == 0)
23593 {
23594 flags |= FC_RANGE;
23595 p += 5;
23596 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023597 else if (STRNCMP(p, "dict", 4) == 0)
23598 {
23599 flags |= FC_DICT;
23600 p += 4;
23601 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023602 else if (STRNCMP(p, "abort", 5) == 0)
23603 {
23604 flags |= FC_ABORT;
23605 p += 5;
23606 }
23607 else
23608 break;
23609 }
23610
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023611 /* When there is a line break use what follows for the function body.
23612 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23613 if (*p == '\n')
23614 line_arg = p + 1;
23615 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023616 EMSG(_(e_trailing));
23617
23618 /*
23619 * Read the body of the function, until ":endfunction" is found.
23620 */
23621 if (KeyTyped)
23622 {
23623 /* Check if the function already exists, don't let the user type the
23624 * whole function before telling him it doesn't work! For a script we
23625 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023626 if (!eap->skip && !eap->forceit)
23627 {
23628 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23629 EMSG(_(e_funcdict));
23630 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023631 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023632 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023633
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023634 if (!eap->skip && did_emsg)
23635 goto erret;
23636
Bram Moolenaar071d4272004-06-13 20:20:40 +000023637 msg_putchar('\n'); /* don't overwrite the function name */
23638 cmdline_row = msg_row;
23639 }
23640
23641 indent = 2;
23642 nesting = 0;
23643 for (;;)
23644 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023645 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023646 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023647 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023648 saved_wait_return = FALSE;
23649 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023650 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023651 sourcing_lnum_off = sourcing_lnum;
23652
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023653 if (line_arg != NULL)
23654 {
23655 /* Use eap->arg, split up in parts by line breaks. */
23656 theline = line_arg;
23657 p = vim_strchr(theline, '\n');
23658 if (p == NULL)
23659 line_arg += STRLEN(line_arg);
23660 else
23661 {
23662 *p = NUL;
23663 line_arg = p + 1;
23664 }
23665 }
23666 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023667 theline = getcmdline(':', 0L, indent);
23668 else
23669 theline = eap->getline(':', eap->cookie, indent);
23670 if (KeyTyped)
23671 lines_left = Rows - 1;
23672 if (theline == NULL)
23673 {
23674 EMSG(_("E126: Missing :endfunction"));
23675 goto erret;
23676 }
23677
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023678 /* Detect line continuation: sourcing_lnum increased more than one. */
23679 if (sourcing_lnum > sourcing_lnum_off + 1)
23680 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23681 else
23682 sourcing_lnum_off = 0;
23683
Bram Moolenaar071d4272004-06-13 20:20:40 +000023684 if (skip_until != NULL)
23685 {
23686 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23687 * don't check for ":endfunc". */
23688 if (STRCMP(theline, skip_until) == 0)
23689 {
23690 vim_free(skip_until);
23691 skip_until = NULL;
23692 }
23693 }
23694 else
23695 {
23696 /* skip ':' and blanks*/
23697 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23698 ;
23699
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023700 /* Check for "endfunction". */
23701 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023702 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023703 if (line_arg == NULL)
23704 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023705 break;
23706 }
23707
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023708 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023709 * at "end". */
23710 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23711 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023712 else if (STRNCMP(p, "if", 2) == 0
23713 || STRNCMP(p, "wh", 2) == 0
23714 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023715 || STRNCMP(p, "try", 3) == 0)
23716 indent += 2;
23717
23718 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023719 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023720 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023721 if (*p == '!')
23722 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023723 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023724 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023725 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023726 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023727 ++nesting;
23728 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023729 }
23730 }
23731
23732 /* Check for ":append" or ":insert". */
23733 p = skip_range(p, NULL);
23734 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23735 || (p[0] == 'i'
23736 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23737 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23738 skip_until = vim_strsave((char_u *)".");
23739
23740 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23741 arg = skipwhite(skiptowhite(p));
23742 if (arg[0] == '<' && arg[1] =='<'
23743 && ((p[0] == 'p' && p[1] == 'y'
23744 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23745 || (p[0] == 'p' && p[1] == 'e'
23746 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23747 || (p[0] == 't' && p[1] == 'c'
23748 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023749 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23750 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023751 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23752 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023753 || (p[0] == 'm' && p[1] == 'z'
23754 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023755 ))
23756 {
23757 /* ":python <<" continues until a dot, like ":append" */
23758 p = skipwhite(arg + 2);
23759 if (*p == NUL)
23760 skip_until = vim_strsave((char_u *)".");
23761 else
23762 skip_until = vim_strsave(p);
23763 }
23764 }
23765
23766 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023767 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023768 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023769 if (line_arg == NULL)
23770 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023771 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023772 }
23773
23774 /* Copy the line to newly allocated memory. get_one_sourceline()
23775 * allocates 250 bytes per line, this saves 80% on average. The cost
23776 * is an extra alloc/free. */
23777 p = vim_strsave(theline);
23778 if (p != NULL)
23779 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023780 if (line_arg == NULL)
23781 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023782 theline = p;
23783 }
23784
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023785 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23786
23787 /* Add NULL lines for continuation lines, so that the line count is
23788 * equal to the index in the growarray. */
23789 while (sourcing_lnum_off-- > 0)
23790 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023791
23792 /* Check for end of eap->arg. */
23793 if (line_arg != NULL && *line_arg == NUL)
23794 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023795 }
23796
23797 /* Don't define the function when skipping commands or when an error was
23798 * detected. */
23799 if (eap->skip || did_emsg)
23800 goto erret;
23801
23802 /*
23803 * If there are no errors, add the function
23804 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023805 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023806 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023807 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023808 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023809 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023810 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023811 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023812 goto erret;
23813 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023814
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023815 fp = find_func(name);
23816 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023818 if (!eap->forceit)
23819 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023820 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023821 goto erret;
23822 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023823 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023824 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023825 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023826 name);
23827 goto erret;
23828 }
23829 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023830 ga_clear_strings(&(fp->uf_args));
23831 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023832 vim_free(name);
23833 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023834 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 }
23836 else
23837 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023838 char numbuf[20];
23839
23840 fp = NULL;
23841 if (fudi.fd_newkey == NULL && !eap->forceit)
23842 {
23843 EMSG(_(e_funcdict));
23844 goto erret;
23845 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023846 if (fudi.fd_di == NULL)
23847 {
23848 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023849 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023850 goto erret;
23851 }
23852 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023853 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023854 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023855
23856 /* Give the function a sequential number. Can only be used with a
23857 * Funcref! */
23858 vim_free(name);
23859 sprintf(numbuf, "%d", ++func_nr);
23860 name = vim_strsave((char_u *)numbuf);
23861 if (name == NULL)
23862 goto erret;
23863 }
23864
23865 if (fp == NULL)
23866 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023867 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023868 {
23869 int slen, plen;
23870 char_u *scriptname;
23871
23872 /* Check that the autoload name matches the script name. */
23873 j = FAIL;
23874 if (sourcing_name != NULL)
23875 {
23876 scriptname = autoload_name(name);
23877 if (scriptname != NULL)
23878 {
23879 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023880 plen = (int)STRLEN(p);
23881 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023882 if (slen > plen && fnamecmp(p,
23883 sourcing_name + slen - plen) == 0)
23884 j = OK;
23885 vim_free(scriptname);
23886 }
23887 }
23888 if (j == FAIL)
23889 {
23890 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23891 goto erret;
23892 }
23893 }
23894
23895 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023896 if (fp == NULL)
23897 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023898
23899 if (fudi.fd_dict != NULL)
23900 {
23901 if (fudi.fd_di == NULL)
23902 {
23903 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023904 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023905 if (fudi.fd_di == NULL)
23906 {
23907 vim_free(fp);
23908 goto erret;
23909 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023910 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23911 {
23912 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023913 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023914 goto erret;
23915 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023916 }
23917 else
23918 /* overwrite existing dict entry */
23919 clear_tv(&fudi.fd_di->di_tv);
23920 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023921 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023922 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023923 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023924
23925 /* behave like "dict" was used */
23926 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023927 }
23928
Bram Moolenaar071d4272004-06-13 20:20:40 +000023929 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023930 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023931 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23932 {
23933 vim_free(fp);
23934 goto erret;
23935 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023936 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023937 fp->uf_args = newargs;
23938 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023939#ifdef FEAT_PROFILE
23940 fp->uf_tml_count = NULL;
23941 fp->uf_tml_total = NULL;
23942 fp->uf_tml_self = NULL;
23943 fp->uf_profiling = FALSE;
23944 if (prof_def_func())
23945 func_do_profile(fp);
23946#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023947 fp->uf_varargs = varargs;
23948 fp->uf_flags = flags;
23949 fp->uf_calls = 0;
23950 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023951 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023952
23953erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023954 ga_clear_strings(&newargs);
23955 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023956ret_free:
23957 vim_free(skip_until);
23958 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023959 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023960 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023961 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023962}
23963
23964/*
23965 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023966 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023967 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023968 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023969 * TFN_INT: internal function name OK
23970 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023971 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023972 * Advances "pp" to just after the function name (if no error).
23973 */
23974 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023975trans_function_name(
23976 char_u **pp,
23977 int skip, /* only find the end, don't evaluate */
23978 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023979 funcdict_T *fdp, /* return: info about dictionary used */
23980 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023981{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023982 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023983 char_u *start;
23984 char_u *end;
23985 int lead;
23986 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023987 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023988 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023989
23990 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023991 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023992 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023993
23994 /* Check for hard coded <SNR>: already translated function ID (from a user
23995 * command). */
23996 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23997 && (*pp)[2] == (int)KE_SNR)
23998 {
23999 *pp += 3;
24000 len = get_id_len(pp) + 3;
24001 return vim_strnsave(start, len);
24002 }
24003
24004 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
24005 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024006 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000024007 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024008 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024009
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024010 /* Note that TFN_ flags use the same values as GLV_ flags. */
24011 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024012 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024013 if (end == start)
24014 {
24015 if (!skip)
24016 EMSG(_("E129: Function name required"));
24017 goto theend;
24018 }
Bram Moolenaara7043832005-01-21 11:56:39 +000024019 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024020 {
24021 /*
24022 * Report an invalid expression in braces, unless the expression
24023 * evaluation has been cancelled due to an aborting error, an
24024 * interrupt, or an exception.
24025 */
24026 if (!aborting())
24027 {
24028 if (end != NULL)
24029 EMSG2(_(e_invarg2), start);
24030 }
24031 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024032 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024033 goto theend;
24034 }
24035
24036 if (lv.ll_tv != NULL)
24037 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024038 if (fdp != NULL)
24039 {
24040 fdp->fd_dict = lv.ll_dict;
24041 fdp->fd_newkey = lv.ll_newkey;
24042 lv.ll_newkey = NULL;
24043 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024044 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024045 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
24046 {
24047 name = vim_strsave(lv.ll_tv->vval.v_string);
24048 *pp = end;
24049 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010024050 else if (lv.ll_tv->v_type == VAR_PARTIAL
24051 && lv.ll_tv->vval.v_partial != NULL)
24052 {
24053 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
24054 *pp = end;
Bram Moolenaar9e63f612016-03-17 23:13:28 +010024055 if (partial != NULL)
24056 *partial = lv.ll_tv->vval.v_partial;
Bram Moolenaard22a1892016-03-17 20:50:47 +010024057 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024058 else
24059 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024060 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
24061 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024062 EMSG(_(e_funcref));
24063 else
24064 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024065 name = NULL;
24066 }
24067 goto theend;
24068 }
24069
24070 if (lv.ll_name == NULL)
24071 {
24072 /* Error found, but continue after the function name. */
24073 *pp = end;
24074 goto theend;
24075 }
24076
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024077 /* Check if the name is a Funcref. If so, use the value. */
24078 if (lv.ll_exp_name != NULL)
24079 {
24080 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010024081 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010024082 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024083 if (name == lv.ll_exp_name)
24084 name = NULL;
24085 }
24086 else
24087 {
24088 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010024089 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024090 if (name == *pp)
24091 name = NULL;
24092 }
24093 if (name != NULL)
24094 {
24095 name = vim_strsave(name);
24096 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020024097 if (STRNCMP(name, "<SNR>", 5) == 0)
24098 {
24099 /* Change "<SNR>" to the byte sequence. */
24100 name[0] = K_SPECIAL;
24101 name[1] = KS_EXTRA;
24102 name[2] = (int)KE_SNR;
24103 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
24104 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000024105 goto theend;
24106 }
24107
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024108 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024109 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024110 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000024111 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
24112 && STRNCMP(lv.ll_name, "s:", 2) == 0)
24113 {
24114 /* When there was "s:" already or the name expanded to get a
24115 * leading "s:" then remove it. */
24116 lv.ll_name += 2;
24117 len -= 2;
24118 lead = 2;
24119 }
24120 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024121 else
Bram Moolenaara7043832005-01-21 11:56:39 +000024122 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024123 /* skip over "s:" and "g:" */
24124 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000024125 lv.ll_name += 2;
24126 len = (int)(end - lv.ll_name);
24127 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024128
24129 /*
24130 * Copy the function name to allocated memory.
24131 * Accept <SID>name() inside a script, translate into <SNR>123_name().
24132 * Accept <SNR>123_name() outside a script.
24133 */
24134 if (skip)
24135 lead = 0; /* do nothing */
24136 else if (lead > 0)
24137 {
24138 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024139 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24140 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024141 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024142 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024143 if (current_SID <= 0)
24144 {
24145 EMSG(_(e_usingsid));
24146 goto theend;
24147 }
24148 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24149 lead += (int)STRLEN(sid_buf);
24150 }
24151 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024152 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024153 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024154 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024155 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024156 goto theend;
24157 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024158 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024159 {
24160 char_u *cp = vim_strchr(lv.ll_name, ':');
24161
24162 if (cp != NULL && cp < end)
24163 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024164 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024165 goto theend;
24166 }
24167 }
24168
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024169 name = alloc((unsigned)(len + lead + 1));
24170 if (name != NULL)
24171 {
24172 if (lead > 0)
24173 {
24174 name[0] = K_SPECIAL;
24175 name[1] = KS_EXTRA;
24176 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024177 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024178 STRCPY(name + 3, sid_buf);
24179 }
24180 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024181 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024182 }
24183 *pp = end;
24184
24185theend:
24186 clear_lval(&lv);
24187 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024188}
24189
24190/*
24191 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24192 * Return 2 if "p" starts with "s:".
24193 * Return 0 otherwise.
24194 */
24195 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024196eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024197{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024198 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24199 * the standard library function. */
24200 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24201 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024202 return 5;
24203 if (p[0] == 's' && p[1] == ':')
24204 return 2;
24205 return 0;
24206}
24207
24208/*
24209 * Return TRUE if "p" starts with "<SID>" or "s:".
24210 * Only works if eval_fname_script() returned non-zero for "p"!
24211 */
24212 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024213eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024214{
24215 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24216}
24217
24218/*
24219 * List the head of the function: "name(arg1, arg2)".
24220 */
24221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024222list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024223{
24224 int j;
24225
24226 msg_start();
24227 if (indent)
24228 MSG_PUTS(" ");
24229 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024230 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024231 {
24232 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024233 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024234 }
24235 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024236 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024237 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024238 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024239 {
24240 if (j)
24241 MSG_PUTS(", ");
24242 msg_puts(FUNCARG(fp, j));
24243 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024244 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024245 {
24246 if (j)
24247 MSG_PUTS(", ");
24248 MSG_PUTS("...");
24249 }
24250 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024251 if (fp->uf_flags & FC_ABORT)
24252 MSG_PUTS(" abort");
24253 if (fp->uf_flags & FC_RANGE)
24254 MSG_PUTS(" range");
24255 if (fp->uf_flags & FC_DICT)
24256 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024257 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024258 if (p_verbose > 0)
24259 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024260}
24261
24262/*
24263 * Find a function by name, return pointer to it in ufuncs.
24264 * Return NULL for unknown function.
24265 */
24266 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024267find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024268{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024269 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024270
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024271 hi = hash_find(&func_hashtab, name);
24272 if (!HASHITEM_EMPTY(hi))
24273 return HI2UF(hi);
24274 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024275}
24276
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024277#if defined(EXITFREE) || defined(PROTO)
24278 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024279free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024280{
24281 hashitem_T *hi;
24282
24283 /* Need to start all over every time, because func_free() may change the
24284 * hash table. */
24285 while (func_hashtab.ht_used > 0)
24286 for (hi = func_hashtab.ht_array; ; ++hi)
24287 if (!HASHITEM_EMPTY(hi))
24288 {
24289 func_free(HI2UF(hi));
24290 break;
24291 }
24292}
24293#endif
24294
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024295 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024296translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024297{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024298 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024299 return find_internal_func(name) >= 0;
24300 return find_func(name) != NULL;
24301}
24302
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024303/*
24304 * Return TRUE if a function "name" exists.
24305 */
24306 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024307function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024308{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024309 char_u *nm = name;
24310 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024311 int n = FALSE;
24312
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024313 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024314 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024315 nm = skipwhite(nm);
24316
24317 /* Only accept "funcname", "funcname ", "funcname (..." and
24318 * "funcname(...", not "funcname!...". */
24319 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024320 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024321 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024322 return n;
24323}
24324
Bram Moolenaara1544c02013-05-30 12:35:52 +020024325 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024326get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024327{
24328 char_u *nm = name;
24329 char_u *p;
24330
Bram Moolenaar65639032016-03-16 21:40:30 +010024331 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024332
24333 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024334 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024335 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024336
Bram Moolenaara1544c02013-05-30 12:35:52 +020024337 vim_free(p);
24338 return NULL;
24339}
24340
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024341/*
24342 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024343 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24344 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024345 */
24346 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024347builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024348{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024349 char_u *p;
24350
24351 if (!ASCII_ISLOWER(name[0]))
24352 return FALSE;
24353 p = vim_strchr(name, AUTOLOAD_CHAR);
24354 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024355}
24356
Bram Moolenaar05159a02005-02-26 23:04:13 +000024357#if defined(FEAT_PROFILE) || defined(PROTO)
24358/*
24359 * Start profiling function "fp".
24360 */
24361 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024362func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024363{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024364 int len = fp->uf_lines.ga_len;
24365
24366 if (len == 0)
24367 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024368 fp->uf_tm_count = 0;
24369 profile_zero(&fp->uf_tm_self);
24370 profile_zero(&fp->uf_tm_total);
24371 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024372 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024373 if (fp->uf_tml_total == NULL)
24374 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024375 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024376 if (fp->uf_tml_self == NULL)
24377 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024378 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024379 fp->uf_tml_idx = -1;
24380 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24381 || fp->uf_tml_self == NULL)
24382 return; /* out of memory */
24383
24384 fp->uf_profiling = TRUE;
24385}
24386
24387/*
24388 * Dump the profiling results for all functions in file "fd".
24389 */
24390 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024391func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024392{
24393 hashitem_T *hi;
24394 int todo;
24395 ufunc_T *fp;
24396 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024397 ufunc_T **sorttab;
24398 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024399
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024400 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024401 if (todo == 0)
24402 return; /* nothing to dump */
24403
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024404 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024405
Bram Moolenaar05159a02005-02-26 23:04:13 +000024406 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24407 {
24408 if (!HASHITEM_EMPTY(hi))
24409 {
24410 --todo;
24411 fp = HI2UF(hi);
24412 if (fp->uf_profiling)
24413 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024414 if (sorttab != NULL)
24415 sorttab[st_len++] = fp;
24416
Bram Moolenaar05159a02005-02-26 23:04:13 +000024417 if (fp->uf_name[0] == K_SPECIAL)
24418 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24419 else
24420 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24421 if (fp->uf_tm_count == 1)
24422 fprintf(fd, "Called 1 time\n");
24423 else
24424 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24425 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24426 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24427 fprintf(fd, "\n");
24428 fprintf(fd, "count total (s) self (s)\n");
24429
24430 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24431 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024432 if (FUNCLINE(fp, i) == NULL)
24433 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024434 prof_func_line(fd, fp->uf_tml_count[i],
24435 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024436 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24437 }
24438 fprintf(fd, "\n");
24439 }
24440 }
24441 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024442
24443 if (sorttab != NULL && st_len > 0)
24444 {
24445 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24446 prof_total_cmp);
24447 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24448 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24449 prof_self_cmp);
24450 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24451 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024452
24453 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024454}
Bram Moolenaar73830342005-02-28 22:48:19 +000024455
24456 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024457prof_sort_list(
24458 FILE *fd,
24459 ufunc_T **sorttab,
24460 int st_len,
24461 char *title,
24462 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024463{
24464 int i;
24465 ufunc_T *fp;
24466
24467 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24468 fprintf(fd, "count total (s) self (s) function\n");
24469 for (i = 0; i < 20 && i < st_len; ++i)
24470 {
24471 fp = sorttab[i];
24472 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24473 prefer_self);
24474 if (fp->uf_name[0] == K_SPECIAL)
24475 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24476 else
24477 fprintf(fd, " %s()\n", fp->uf_name);
24478 }
24479 fprintf(fd, "\n");
24480}
24481
24482/*
24483 * Print the count and times for one function or function line.
24484 */
24485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024486prof_func_line(
24487 FILE *fd,
24488 int count,
24489 proftime_T *total,
24490 proftime_T *self,
24491 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024492{
24493 if (count > 0)
24494 {
24495 fprintf(fd, "%5d ", count);
24496 if (prefer_self && profile_equal(total, self))
24497 fprintf(fd, " ");
24498 else
24499 fprintf(fd, "%s ", profile_msg(total));
24500 if (!prefer_self && profile_equal(total, self))
24501 fprintf(fd, " ");
24502 else
24503 fprintf(fd, "%s ", profile_msg(self));
24504 }
24505 else
24506 fprintf(fd, " ");
24507}
24508
24509/*
24510 * Compare function for total time sorting.
24511 */
24512 static int
24513#ifdef __BORLANDC__
24514_RTLENTRYF
24515#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024516prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024517{
24518 ufunc_T *p1, *p2;
24519
24520 p1 = *(ufunc_T **)s1;
24521 p2 = *(ufunc_T **)s2;
24522 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24523}
24524
24525/*
24526 * Compare function for self time sorting.
24527 */
24528 static int
24529#ifdef __BORLANDC__
24530_RTLENTRYF
24531#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024532prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024533{
24534 ufunc_T *p1, *p2;
24535
24536 p1 = *(ufunc_T **)s1;
24537 p2 = *(ufunc_T **)s2;
24538 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24539}
24540
Bram Moolenaar05159a02005-02-26 23:04:13 +000024541#endif
24542
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024543/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024544 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024545 * Return TRUE if a package was loaded.
24546 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024547 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024548script_autoload(
24549 char_u *name,
24550 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024551{
24552 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024553 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024554 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024555 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024556
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024557 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024558 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024559 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024560 return FALSE;
24561
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024562 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024563
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024564 /* Find the name in the list of previously loaded package names. Skip
24565 * "autoload/", it's always the same. */
24566 for (i = 0; i < ga_loaded.ga_len; ++i)
24567 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24568 break;
24569 if (!reload && i < ga_loaded.ga_len)
24570 ret = FALSE; /* was loaded already */
24571 else
24572 {
24573 /* Remember the name if it wasn't loaded already. */
24574 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24575 {
24576 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24577 tofree = NULL;
24578 }
24579
24580 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024581 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024582 ret = TRUE;
24583 }
24584
24585 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024586 return ret;
24587}
24588
24589/*
24590 * Return the autoload script name for a function or variable name.
24591 * Returns NULL when out of memory.
24592 */
24593 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024594autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024595{
24596 char_u *p;
24597 char_u *scriptname;
24598
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024599 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024600 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24601 if (scriptname == NULL)
24602 return FALSE;
24603 STRCPY(scriptname, "autoload/");
24604 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024605 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024606 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024607 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024608 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024609 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024610}
24611
Bram Moolenaar071d4272004-06-13 20:20:40 +000024612#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24613
24614/*
24615 * Function given to ExpandGeneric() to obtain the list of user defined
24616 * function names.
24617 */
24618 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024619get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024620{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024621 static long_u done;
24622 static hashitem_T *hi;
24623 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024624
24625 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024626 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024627 done = 0;
24628 hi = func_hashtab.ht_array;
24629 }
24630 if (done < func_hashtab.ht_used)
24631 {
24632 if (done++ > 0)
24633 ++hi;
24634 while (HASHITEM_EMPTY(hi))
24635 ++hi;
24636 fp = HI2UF(hi);
24637
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024638 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024639 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024640
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024641 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24642 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024643
24644 cat_func_name(IObuff, fp);
24645 if (xp->xp_context != EXPAND_USER_FUNC)
24646 {
24647 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024648 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024649 STRCAT(IObuff, ")");
24650 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024651 return IObuff;
24652 }
24653 return NULL;
24654}
24655
24656#endif /* FEAT_CMDL_COMPL */
24657
24658/*
24659 * Copy the function name of "fp" to buffer "buf".
24660 * "buf" must be able to hold the function name plus three bytes.
24661 * Takes care of script-local function names.
24662 */
24663 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024664cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024665{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024666 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024667 {
24668 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024669 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024670 }
24671 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024672 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024673}
24674
24675/*
24676 * ":delfunction {name}"
24677 */
24678 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024679ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024680{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024681 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024682 char_u *p;
24683 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024684 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024685
24686 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024687 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024688 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024689 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024690 {
24691 if (fudi.fd_dict != NULL && !eap->skip)
24692 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024694 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024695 if (!ends_excmd(*skipwhite(p)))
24696 {
24697 vim_free(name);
24698 EMSG(_(e_trailing));
24699 return;
24700 }
24701 eap->nextcmd = check_nextcmd(p);
24702 if (eap->nextcmd != NULL)
24703 *p = NUL;
24704
24705 if (!eap->skip)
24706 fp = find_func(name);
24707 vim_free(name);
24708
24709 if (!eap->skip)
24710 {
24711 if (fp == NULL)
24712 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024713 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024714 return;
24715 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024716 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024717 {
24718 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24719 return;
24720 }
24721
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024722 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024723 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024724 /* Delete the dict item that refers to the function, it will
24725 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024726 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024727 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024728 else
24729 func_free(fp);
24730 }
24731}
24732
24733/*
24734 * Free a function and remove it from the list of functions.
24735 */
24736 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024737func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024738{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024739 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024740
24741 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024742 ga_clear_strings(&(fp->uf_args));
24743 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024744#ifdef FEAT_PROFILE
24745 vim_free(fp->uf_tml_count);
24746 vim_free(fp->uf_tml_total);
24747 vim_free(fp->uf_tml_self);
24748#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024749
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024750 /* remove the function from the function hashtable */
24751 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24752 if (HASHITEM_EMPTY(hi))
24753 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024754 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024755 hash_remove(&func_hashtab, hi);
24756
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024757 vim_free(fp);
24758}
24759
24760/*
24761 * Unreference a Function: decrement the reference count and free it when it
24762 * becomes zero. Only for numbered functions.
24763 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024764 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024765func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024766{
24767 ufunc_T *fp;
24768
24769 if (name != NULL && isdigit(*name))
24770 {
24771 fp = find_func(name);
24772 if (fp == NULL)
24773 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024774 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024775 {
24776 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024777 * when "uf_calls" becomes zero. */
24778 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024779 func_free(fp);
24780 }
24781 }
24782}
24783
24784/*
24785 * Count a reference to a Function.
24786 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024787 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024788func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024789{
24790 ufunc_T *fp;
24791
24792 if (name != NULL && isdigit(*name))
24793 {
24794 fp = find_func(name);
24795 if (fp == NULL)
24796 EMSG2(_(e_intern2), "func_ref()");
24797 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024798 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024799 }
24800}
24801
24802/*
24803 * Call a user function.
24804 */
24805 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024806call_user_func(
24807 ufunc_T *fp, /* pointer to function */
24808 int argcount, /* nr of args */
24809 typval_T *argvars, /* arguments */
24810 typval_T *rettv, /* return value */
24811 linenr_T firstline, /* first line of range */
24812 linenr_T lastline, /* last line of range */
24813 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024814{
Bram Moolenaar33570922005-01-25 22:26:29 +000024815 char_u *save_sourcing_name;
24816 linenr_T save_sourcing_lnum;
24817 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024818 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024819 int save_did_emsg;
24820 static int depth = 0;
24821 dictitem_T *v;
24822 int fixvar_idx = 0; /* index in fixvar[] */
24823 int i;
24824 int ai;
24825 char_u numbuf[NUMBUFLEN];
24826 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024827 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024828#ifdef FEAT_PROFILE
24829 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024830 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024831#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024832
24833 /* If depth of calling is getting too high, don't execute the function */
24834 if (depth >= p_mfd)
24835 {
24836 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024837 rettv->v_type = VAR_NUMBER;
24838 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024839 return;
24840 }
24841 ++depth;
24842
24843 line_breakcheck(); /* check for CTRL-C hit */
24844
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024845 fc = (funccall_T *)alloc(sizeof(funccall_T));
24846 fc->caller = current_funccal;
24847 current_funccal = fc;
24848 fc->func = fp;
24849 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024850 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024851 fc->linenr = 0;
24852 fc->returned = FALSE;
24853 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024855 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24856 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024857
Bram Moolenaar33570922005-01-25 22:26:29 +000024858 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024859 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024860 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24861 * each argument variable and saves a lot of time.
24862 */
24863 /*
24864 * Init l: variables.
24865 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024866 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024867 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024868 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024869 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24870 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024871 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024872 name = v->di_key;
24873 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024874 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024875 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024876 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024877 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024878 v->di_tv.vval.v_dict = selfdict;
24879 ++selfdict->dv_refcount;
24880 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024881
Bram Moolenaar33570922005-01-25 22:26:29 +000024882 /*
24883 * Init a: variables.
24884 * Set a:0 to "argcount".
24885 * Set a:000 to a list with room for the "..." arguments.
24886 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024887 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024888 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024889 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024890 /* Use "name" to avoid a warning from some compiler that checks the
24891 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024892 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024893 name = v->di_key;
24894 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024895 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024896 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024897 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024898 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024899 v->di_tv.vval.v_list = &fc->l_varlist;
24900 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24901 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24902 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024903
24904 /*
24905 * Set a:firstline to "firstline" and a:lastline to "lastline".
24906 * Set a:name to named arguments.
24907 * Set a:N to the "..." arguments.
24908 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024909 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024910 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024911 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024912 (varnumber_T)lastline);
24913 for (i = 0; i < argcount; ++i)
24914 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024915 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024916 if (ai < 0)
24917 /* named argument a:name */
24918 name = FUNCARG(fp, i);
24919 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024920 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024921 /* "..." argument a:1, a:2, etc. */
24922 sprintf((char *)numbuf, "%d", ai + 1);
24923 name = numbuf;
24924 }
24925 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24926 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024927 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024928 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24929 }
24930 else
24931 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024932 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24933 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024934 if (v == NULL)
24935 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024936 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024937 }
24938 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024939 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024940
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024941 /* Note: the values are copied directly to avoid alloc/free.
24942 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024943 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024944 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024945
24946 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24947 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024948 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24949 fc->l_listitems[ai].li_tv = argvars[i];
24950 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024951 }
24952 }
24953
Bram Moolenaar071d4272004-06-13 20:20:40 +000024954 /* Don't redraw while executing the function. */
24955 ++RedrawingDisabled;
24956 save_sourcing_name = sourcing_name;
24957 save_sourcing_lnum = sourcing_lnum;
24958 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024959 /* need space for function name + ("function " + 3) or "[number]" */
24960 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24961 + STRLEN(fp->uf_name) + 20;
24962 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024963 if (sourcing_name != NULL)
24964 {
24965 if (save_sourcing_name != NULL
24966 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024967 sprintf((char *)sourcing_name, "%s[%d]..",
24968 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 else
24970 STRCPY(sourcing_name, "function ");
24971 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24972
24973 if (p_verbose >= 12)
24974 {
24975 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024976 verbose_enter_scroll();
24977
Bram Moolenaar555b2802005-05-19 21:08:39 +000024978 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024979 if (p_verbose >= 14)
24980 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024981 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024982 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024983 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024984 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024985
24986 msg_puts((char_u *)"(");
24987 for (i = 0; i < argcount; ++i)
24988 {
24989 if (i > 0)
24990 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024991 if (argvars[i].v_type == VAR_NUMBER)
24992 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024993 else
24994 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024995 /* Do not want errors such as E724 here. */
24996 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024997 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024998 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024999 if (s != NULL)
25000 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025001 if (vim_strsize(s) > MSG_BUF_CLEN)
25002 {
25003 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25004 s = buf;
25005 }
25006 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025007 vim_free(tofree);
25008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025009 }
25010 }
25011 msg_puts((char_u *)")");
25012 }
25013 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025014
25015 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025016 --no_wait_return;
25017 }
25018 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025019#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025020 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025021 {
25022 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
25023 func_do_profile(fp);
25024 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025025 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025026 {
25027 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025028 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025029 profile_zero(&fp->uf_tm_children);
25030 }
25031 script_prof_save(&wait_start);
25032 }
25033#endif
25034
Bram Moolenaar071d4272004-06-13 20:20:40 +000025035 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025036 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025037 save_did_emsg = did_emsg;
25038 did_emsg = FALSE;
25039
25040 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025041 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025042 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
25043
25044 --RedrawingDisabled;
25045
25046 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025047 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025048 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025049 clear_tv(rettv);
25050 rettv->v_type = VAR_NUMBER;
25051 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025052 }
25053
Bram Moolenaar05159a02005-02-26 23:04:13 +000025054#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025055 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025056 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000025057 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000025058 profile_end(&call_start);
25059 profile_sub_wait(&wait_start, &call_start);
25060 profile_add(&fp->uf_tm_total, &call_start);
25061 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025062 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025063 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025064 profile_add(&fc->caller->func->uf_tm_children, &call_start);
25065 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025066 }
25067 }
25068#endif
25069
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070 /* when being verbose, mention the return value */
25071 if (p_verbose >= 12)
25072 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000025073 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025074 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025075
Bram Moolenaar071d4272004-06-13 20:20:40 +000025076 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000025077 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025078 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000025079 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025080 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000025081 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000025082 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000025083 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000025084 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000025085 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025086 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000025087
Bram Moolenaar555b2802005-05-19 21:08:39 +000025088 /* The value may be very long. Skip the middle part, so that we
25089 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020025090 * truncate it at the end. Don't want errors such as E724 here. */
25091 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025092 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020025093 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025094 if (s != NULL)
25095 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010025096 if (vim_strsize(s) > MSG_BUF_CLEN)
25097 {
25098 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
25099 s = buf;
25100 }
25101 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000025102 vim_free(tofree);
25103 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025104 }
25105 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025106
25107 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025108 --no_wait_return;
25109 }
25110
25111 vim_free(sourcing_name);
25112 sourcing_name = save_sourcing_name;
25113 sourcing_lnum = save_sourcing_lnum;
25114 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025115#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025116 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025117 script_prof_restore(&wait_start);
25118#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025119
25120 if (p_verbose >= 12 && sourcing_name != NULL)
25121 {
25122 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025123 verbose_enter_scroll();
25124
Bram Moolenaar555b2802005-05-19 21:08:39 +000025125 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025126 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000025127
25128 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000025129 --no_wait_return;
25130 }
25131
25132 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025133 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025134 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025135
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025136 /* If the a:000 list and the l: and a: dicts are not referenced we can
25137 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025138 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25139 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25140 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25141 {
25142 free_funccal(fc, FALSE);
25143 }
25144 else
25145 {
25146 hashitem_T *hi;
25147 listitem_T *li;
25148 int todo;
25149
25150 /* "fc" is still in use. This can happen when returning "a:000" or
25151 * assigning "l:" to a global variable.
25152 * Link "fc" in the list for garbage collection later. */
25153 fc->caller = previous_funccal;
25154 previous_funccal = fc;
25155
25156 /* Make a copy of the a: variables, since we didn't do that above. */
25157 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25158 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25159 {
25160 if (!HASHITEM_EMPTY(hi))
25161 {
25162 --todo;
25163 v = HI2DI(hi);
25164 copy_tv(&v->di_tv, &v->di_tv);
25165 }
25166 }
25167
25168 /* Make a copy of the a:000 items, since we didn't do that above. */
25169 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25170 copy_tv(&li->li_tv, &li->li_tv);
25171 }
25172}
25173
25174/*
25175 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025176 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025177 */
25178 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025179can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025180{
25181 return (fc->l_varlist.lv_copyID != copyID
25182 && fc->l_vars.dv_copyID != copyID
25183 && fc->l_avars.dv_copyID != copyID);
25184}
25185
25186/*
25187 * Free "fc" and what it contains.
25188 */
25189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025190free_funccal(
25191 funccall_T *fc,
25192 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025193{
25194 listitem_T *li;
25195
25196 /* The a: variables typevals may not have been allocated, only free the
25197 * allocated variables. */
25198 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25199
25200 /* free all l: variables */
25201 vars_clear(&fc->l_vars.dv_hashtab);
25202
25203 /* Free the a:000 variables if they were allocated. */
25204 if (free_val)
25205 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25206 clear_tv(&li->li_tv);
25207
25208 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025209}
25210
25211/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025212 * Add a number variable "name" to dict "dp" with value "nr".
25213 */
25214 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025215add_nr_var(
25216 dict_T *dp,
25217 dictitem_T *v,
25218 char *name,
25219 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025220{
25221 STRCPY(v->di_key, name);
25222 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25223 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25224 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025225 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025226 v->di_tv.vval.v_number = nr;
25227}
25228
25229/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025230 * ":return [expr]"
25231 */
25232 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025233ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234{
25235 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025236 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025237 int returning = FALSE;
25238
25239 if (current_funccal == NULL)
25240 {
25241 EMSG(_("E133: :return not inside a function"));
25242 return;
25243 }
25244
25245 if (eap->skip)
25246 ++emsg_skip;
25247
25248 eap->nextcmd = NULL;
25249 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025250 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025251 {
25252 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025253 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025254 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025255 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025256 }
25257 /* It's safer to return also on error. */
25258 else if (!eap->skip)
25259 {
25260 /*
25261 * Return unless the expression evaluation has been cancelled due to an
25262 * aborting error, an interrupt, or an exception.
25263 */
25264 if (!aborting())
25265 returning = do_return(eap, FALSE, TRUE, NULL);
25266 }
25267
25268 /* When skipping or the return gets pending, advance to the next command
25269 * in this line (!returning). Otherwise, ignore the rest of the line.
25270 * Following lines will be ignored by get_func_line(). */
25271 if (returning)
25272 eap->nextcmd = NULL;
25273 else if (eap->nextcmd == NULL) /* no argument */
25274 eap->nextcmd = check_nextcmd(arg);
25275
25276 if (eap->skip)
25277 --emsg_skip;
25278}
25279
25280/*
25281 * Return from a function. Possibly makes the return pending. Also called
25282 * for a pending return at the ":endtry" or after returning from an extra
25283 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025284 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025285 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025286 * FALSE when the return gets pending.
25287 */
25288 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025289do_return(
25290 exarg_T *eap,
25291 int reanimate,
25292 int is_cmd,
25293 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025294{
25295 int idx;
25296 struct condstack *cstack = eap->cstack;
25297
25298 if (reanimate)
25299 /* Undo the return. */
25300 current_funccal->returned = FALSE;
25301
25302 /*
25303 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25304 * not in its finally clause (which then is to be executed next) is found.
25305 * In this case, make the ":return" pending for execution at the ":endtry".
25306 * Otherwise, return normally.
25307 */
25308 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25309 if (idx >= 0)
25310 {
25311 cstack->cs_pending[idx] = CSTP_RETURN;
25312
25313 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025314 /* A pending return again gets pending. "rettv" points to an
25315 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025316 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025317 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025318 else
25319 {
25320 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025321 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025322 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025323 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025324
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025325 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025326 {
25327 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025328 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025329 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025330 else
25331 EMSG(_(e_outofmem));
25332 }
25333 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025334 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025335
25336 if (reanimate)
25337 {
25338 /* The pending return value could be overwritten by a ":return"
25339 * without argument in a finally clause; reset the default
25340 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025341 current_funccal->rettv->v_type = VAR_NUMBER;
25342 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025343 }
25344 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025345 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025346 }
25347 else
25348 {
25349 current_funccal->returned = TRUE;
25350
25351 /* If the return is carried out now, store the return value. For
25352 * a return immediately after reanimation, the value is already
25353 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025354 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025355 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025356 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025357 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025358 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025359 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025360 }
25361 }
25362
25363 return idx < 0;
25364}
25365
25366/*
25367 * Free the variable with a pending return value.
25368 */
25369 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025370discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025371{
Bram Moolenaar33570922005-01-25 22:26:29 +000025372 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025373}
25374
25375/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025376 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025377 * is an allocated string. Used by report_pending() for verbose messages.
25378 */
25379 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025380get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025381{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025382 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025383 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025384 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025385
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025386 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025387 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025388 if (s == NULL)
25389 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025390
25391 STRCPY(IObuff, ":return ");
25392 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25393 if (STRLEN(s) + 8 >= IOSIZE)
25394 STRCPY(IObuff + IOSIZE - 4, "...");
25395 vim_free(tofree);
25396 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397}
25398
25399/*
25400 * Get next function line.
25401 * Called by do_cmdline() to get the next line.
25402 * Returns allocated string, or NULL for end of function.
25403 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025404 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025405get_func_line(
25406 int c UNUSED,
25407 void *cookie,
25408 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025409{
Bram Moolenaar33570922005-01-25 22:26:29 +000025410 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025411 ufunc_T *fp = fcp->func;
25412 char_u *retval;
25413 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025414
25415 /* If breakpoints have been added/deleted need to check for it. */
25416 if (fcp->dbg_tick != debug_tick)
25417 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025418 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025419 sourcing_lnum);
25420 fcp->dbg_tick = debug_tick;
25421 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025422#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025423 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025424 func_line_end(cookie);
25425#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426
Bram Moolenaar05159a02005-02-26 23:04:13 +000025427 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025428 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25429 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025430 retval = NULL;
25431 else
25432 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025433 /* Skip NULL lines (continuation lines). */
25434 while (fcp->linenr < gap->ga_len
25435 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25436 ++fcp->linenr;
25437 if (fcp->linenr >= gap->ga_len)
25438 retval = NULL;
25439 else
25440 {
25441 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25442 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025443#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025444 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025445 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025446#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025447 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025448 }
25449
25450 /* Did we encounter a breakpoint? */
25451 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25452 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025453 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025454 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025455 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025456 sourcing_lnum);
25457 fcp->dbg_tick = debug_tick;
25458 }
25459
25460 return retval;
25461}
25462
Bram Moolenaar05159a02005-02-26 23:04:13 +000025463#if defined(FEAT_PROFILE) || defined(PROTO)
25464/*
25465 * Called when starting to read a function line.
25466 * "sourcing_lnum" must be correct!
25467 * When skipping lines it may not actually be executed, but we won't find out
25468 * until later and we need to store the time now.
25469 */
25470 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025471func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025472{
25473 funccall_T *fcp = (funccall_T *)cookie;
25474 ufunc_T *fp = fcp->func;
25475
25476 if (fp->uf_profiling && sourcing_lnum >= 1
25477 && sourcing_lnum <= fp->uf_lines.ga_len)
25478 {
25479 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025480 /* Skip continuation lines. */
25481 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25482 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025483 fp->uf_tml_execed = FALSE;
25484 profile_start(&fp->uf_tml_start);
25485 profile_zero(&fp->uf_tml_children);
25486 profile_get_wait(&fp->uf_tml_wait);
25487 }
25488}
25489
25490/*
25491 * Called when actually executing a function line.
25492 */
25493 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025494func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025495{
25496 funccall_T *fcp = (funccall_T *)cookie;
25497 ufunc_T *fp = fcp->func;
25498
25499 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25500 fp->uf_tml_execed = TRUE;
25501}
25502
25503/*
25504 * Called when done with a function line.
25505 */
25506 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025507func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025508{
25509 funccall_T *fcp = (funccall_T *)cookie;
25510 ufunc_T *fp = fcp->func;
25511
25512 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25513 {
25514 if (fp->uf_tml_execed)
25515 {
25516 ++fp->uf_tml_count[fp->uf_tml_idx];
25517 profile_end(&fp->uf_tml_start);
25518 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025519 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025520 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25521 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025522 }
25523 fp->uf_tml_idx = -1;
25524 }
25525}
25526#endif
25527
Bram Moolenaar071d4272004-06-13 20:20:40 +000025528/*
25529 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025530 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025531 */
25532 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025533func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025534{
Bram Moolenaar33570922005-01-25 22:26:29 +000025535 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025536
25537 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25538 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025539 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025540 || fcp->returned);
25541}
25542
25543/*
25544 * return TRUE if cookie indicates a function which "abort"s on errors.
25545 */
25546 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025547func_has_abort(
25548 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025549{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025550 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025551}
25552
25553#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25554typedef enum
25555{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025556 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25557 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25558 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025559} var_flavour_T;
25560
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025561static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025562
25563 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025564var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025565{
25566 char_u *p = varname;
25567
25568 if (ASCII_ISUPPER(*p))
25569 {
25570 while (*(++p))
25571 if (ASCII_ISLOWER(*p))
25572 return VAR_FLAVOUR_SESSION;
25573 return VAR_FLAVOUR_VIMINFO;
25574 }
25575 else
25576 return VAR_FLAVOUR_DEFAULT;
25577}
25578#endif
25579
25580#if defined(FEAT_VIMINFO) || defined(PROTO)
25581/*
25582 * Restore global vars that start with a capital from the viminfo file
25583 */
25584 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025585read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025586{
25587 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025588 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025589 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025590 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025591
25592 if (!writing && (find_viminfo_parameter('!') != NULL))
25593 {
25594 tab = vim_strchr(virp->vir_line + 1, '\t');
25595 if (tab != NULL)
25596 {
25597 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025598 switch (*tab)
25599 {
25600 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025601#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025602 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025603#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025604 case 'D': type = VAR_DICT; break;
25605 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025606 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025607 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025608
25609 tab = vim_strchr(tab, '\t');
25610 if (tab != NULL)
25611 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025612 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025613 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025614 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025615 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025616#ifdef FEAT_FLOAT
25617 else if (type == VAR_FLOAT)
25618 (void)string2float(tab + 1, &tv.vval.v_float);
25619#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025620 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025621 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025622 if (type == VAR_DICT || type == VAR_LIST)
25623 {
25624 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25625
25626 if (etv == NULL)
25627 /* Failed to parse back the dict or list, use it as a
25628 * string. */
25629 tv.v_type = VAR_STRING;
25630 else
25631 {
25632 vim_free(tv.vval.v_string);
25633 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025634 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025635 }
25636 }
25637
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025638 /* when in a function use global variables */
25639 save_funccal = current_funccal;
25640 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025641 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025642 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025643
25644 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025645 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025646 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25647 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025648 }
25649 }
25650 }
25651
25652 return viminfo_readline(virp);
25653}
25654
25655/*
25656 * Write global vars that start with a capital to the viminfo file
25657 */
25658 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025659write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025660{
Bram Moolenaar33570922005-01-25 22:26:29 +000025661 hashitem_T *hi;
25662 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025663 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025664 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025665 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025666 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025667 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025668
25669 if (find_viminfo_parameter('!') == NULL)
25670 return;
25671
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025672 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025673
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025674 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025675 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025676 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025677 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025678 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025679 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025680 this_var = HI2DI(hi);
25681 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025682 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025683 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025684 {
25685 case VAR_STRING: s = "STR"; break;
25686 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025687 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025688 case VAR_DICT: s = "DIC"; break;
25689 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025690 case VAR_SPECIAL: s = "XPL"; break;
25691
25692 case VAR_UNKNOWN:
25693 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025694 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025695 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025696 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025697 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025698 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025699 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025700 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025701 if (p != NULL)
25702 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025703 vim_free(tofree);
25704 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025705 }
25706 }
25707}
25708#endif
25709
25710#if defined(FEAT_SESSION) || defined(PROTO)
25711 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025712store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025713{
Bram Moolenaar33570922005-01-25 22:26:29 +000025714 hashitem_T *hi;
25715 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025716 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025717 char_u *p, *t;
25718
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025719 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025720 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025721 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025722 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025723 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025724 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025725 this_var = HI2DI(hi);
25726 if ((this_var->di_tv.v_type == VAR_NUMBER
25727 || this_var->di_tv.v_type == VAR_STRING)
25728 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025729 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025730 /* Escape special characters with a backslash. Turn a LF and
25731 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025732 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025733 (char_u *)"\\\"\n\r");
25734 if (p == NULL) /* out of memory */
25735 break;
25736 for (t = p; *t != NUL; ++t)
25737 if (*t == '\n')
25738 *t = 'n';
25739 else if (*t == '\r')
25740 *t = 'r';
25741 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025742 this_var->di_key,
25743 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25744 : ' ',
25745 p,
25746 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25747 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025748 || put_eol(fd) == FAIL)
25749 {
25750 vim_free(p);
25751 return FAIL;
25752 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025753 vim_free(p);
25754 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025755#ifdef FEAT_FLOAT
25756 else if (this_var->di_tv.v_type == VAR_FLOAT
25757 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25758 {
25759 float_T f = this_var->di_tv.vval.v_float;
25760 int sign = ' ';
25761
25762 if (f < 0)
25763 {
25764 f = -f;
25765 sign = '-';
25766 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025767 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025768 this_var->di_key, sign, f) < 0)
25769 || put_eol(fd) == FAIL)
25770 return FAIL;
25771 }
25772#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773 }
25774 }
25775 return OK;
25776}
25777#endif
25778
Bram Moolenaar661b1822005-07-28 22:36:45 +000025779/*
25780 * Display script name where an item was last set.
25781 * Should only be invoked when 'verbose' is non-zero.
25782 */
25783 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025784last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025785{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025786 char_u *p;
25787
Bram Moolenaar661b1822005-07-28 22:36:45 +000025788 if (scriptID != 0)
25789 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025790 p = home_replace_save(NULL, get_scriptname(scriptID));
25791 if (p != NULL)
25792 {
25793 verbose_enter();
25794 MSG_PUTS(_("\n\tLast set from "));
25795 MSG_PUTS(p);
25796 vim_free(p);
25797 verbose_leave();
25798 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025799 }
25800}
25801
Bram Moolenaard812df62008-11-09 12:46:09 +000025802/*
25803 * List v:oldfiles in a nice way.
25804 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025805 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025806ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025807{
25808 list_T *l = vimvars[VV_OLDFILES].vv_list;
25809 listitem_T *li;
25810 int nr = 0;
25811
25812 if (l == NULL)
25813 msg((char_u *)_("No old files"));
25814 else
25815 {
25816 msg_start();
25817 msg_scroll = TRUE;
25818 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25819 {
25820 msg_outnum((long)++nr);
25821 MSG_PUTS(": ");
25822 msg_outtrans(get_tv_string(&li->li_tv));
25823 msg_putchar('\n');
25824 out_flush(); /* output one line at a time */
25825 ui_breakcheck();
25826 }
25827 /* Assume "got_int" was set to truncate the listing. */
25828 got_int = FALSE;
25829
25830#ifdef FEAT_BROWSE_CMD
25831 if (cmdmod.browse)
25832 {
25833 quit_more = FALSE;
25834 nr = prompt_for_number(FALSE);
25835 msg_starthere();
25836 if (nr > 0)
25837 {
25838 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25839 (long)nr);
25840
25841 if (p != NULL)
25842 {
25843 p = expand_env_save(p);
25844 eap->arg = p;
25845 eap->cmdidx = CMD_edit;
25846 cmdmod.browse = FALSE;
25847 do_exedit(eap, NULL);
25848 vim_free(p);
25849 }
25850 }
25851 }
25852#endif
25853 }
25854}
25855
Bram Moolenaar53744302015-07-17 17:38:22 +020025856/* reset v:option_new, v:option_old and v:option_type */
25857 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025858reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025859{
25860 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25861 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25862 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25863}
25864
25865
Bram Moolenaar071d4272004-06-13 20:20:40 +000025866#endif /* FEAT_EVAL */
25867
Bram Moolenaar071d4272004-06-13 20:20:40 +000025868
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025869#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870
25871#ifdef WIN3264
25872/*
25873 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25874 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025875static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25876static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25877static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025878
25879/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025880 * Get the short path (8.3) for the filename in "fnamep".
25881 * Only works for a valid file name.
25882 * When the path gets longer "fnamep" is changed and the allocated buffer
25883 * is put in "bufp".
25884 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25885 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025886 */
25887 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025888get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025889{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025890 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891 char_u *newbuf;
25892
25893 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025894 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025895 if (l > len - 1)
25896 {
25897 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025898 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025899 newbuf = vim_strnsave(*fnamep, l);
25900 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025901 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902
25903 vim_free(*bufp);
25904 *fnamep = *bufp = newbuf;
25905
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025906 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025907 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025908 }
25909
25910 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025911 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912}
25913
25914/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025915 * Get the short path (8.3) for the filename in "fname". The converted
25916 * path is returned in "bufp".
25917 *
25918 * Some of the directories specified in "fname" may not exist. This function
25919 * will shorten the existing directories at the beginning of the path and then
25920 * append the remaining non-existing path.
25921 *
25922 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025923 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025924 * bufp - Pointer to an allocated buffer for the filename.
25925 * fnamelen - Length of the filename pointed to by fname
25926 *
25927 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025928 */
25929 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025930shortpath_for_invalid_fname(
25931 char_u **fname,
25932 char_u **bufp,
25933 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025934{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025935 char_u *short_fname, *save_fname, *pbuf_unused;
25936 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025937 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025938 int old_len, len;
25939 int new_len, sfx_len;
25940 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025941
25942 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025943 old_len = *fnamelen;
25944 save_fname = vim_strnsave(*fname, old_len);
25945 pbuf_unused = NULL;
25946 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025947
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025948 endp = save_fname + old_len - 1; /* Find the end of the copy */
25949 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025950
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025951 /*
25952 * Try shortening the supplied path till it succeeds by removing one
25953 * directory at a time from the tail of the path.
25954 */
25955 len = 0;
25956 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025957 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025958 /* go back one path-separator */
25959 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25960 --endp;
25961 if (endp <= save_fname)
25962 break; /* processed the complete path */
25963
25964 /*
25965 * Replace the path separator with a NUL and try to shorten the
25966 * resulting path.
25967 */
25968 ch = *endp;
25969 *endp = 0;
25970 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025971 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025972 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25973 {
25974 retval = FAIL;
25975 goto theend;
25976 }
25977 *endp = ch; /* preserve the string */
25978
25979 if (len > 0)
25980 break; /* successfully shortened the path */
25981
25982 /* failed to shorten the path. Skip the path separator */
25983 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025984 }
25985
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025986 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025987 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025988 /*
25989 * Succeeded in shortening the path. Now concatenate the shortened
25990 * path with the remaining path at the tail.
25991 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025992
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025993 /* Compute the length of the new path. */
25994 sfx_len = (int)(save_endp - endp) + 1;
25995 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025996
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025997 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025998 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025999 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026000 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026001 /* There is not enough space in the currently allocated string,
26002 * copy it to a buffer big enough. */
26003 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026004 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026005 {
26006 retval = FAIL;
26007 goto theend;
26008 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026009 }
26010 else
26011 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026012 /* Transfer short_fname to the main buffer (it's big enough),
26013 * unless get_short_pathname() did its work in-place. */
26014 *fname = *bufp = save_fname;
26015 if (short_fname != save_fname)
26016 vim_strncpy(save_fname, short_fname, len);
26017 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026018 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026019
26020 /* concat the not-shortened part of the path */
26021 vim_strncpy(*fname + len, endp, sfx_len);
26022 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026023 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026024
26025theend:
26026 vim_free(pbuf_unused);
26027 vim_free(save_fname);
26028
26029 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030}
26031
26032/*
26033 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026034 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026035 */
26036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026037shortpath_for_partial(
26038 char_u **fnamep,
26039 char_u **bufp,
26040 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026041{
26042 int sepcount, len, tflen;
26043 char_u *p;
26044 char_u *pbuf, *tfname;
26045 int hasTilde;
26046
Bram Moolenaar8c8de832008-06-24 22:58:06 +000026047 /* Count up the path separators from the RHS.. so we know which part
26048 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026049 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026050 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026051 if (vim_ispathsep(*p))
26052 ++sepcount;
26053
26054 /* Need full path first (use expand_env() to remove a "~/") */
26055 hasTilde = (**fnamep == '~');
26056 if (hasTilde)
26057 pbuf = tfname = expand_env_save(*fnamep);
26058 else
26059 pbuf = tfname = FullName_save(*fnamep, FALSE);
26060
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000026061 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026062
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026063 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
26064 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026065
26066 if (len == 0)
26067 {
26068 /* Don't have a valid filename, so shorten the rest of the
26069 * path if we can. This CAN give us invalid 8.3 filenames, but
26070 * there's not a lot of point in guessing what it might be.
26071 */
26072 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026073 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
26074 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026075 }
26076
26077 /* Count the paths backward to find the beginning of the desired string. */
26078 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026079 {
26080#ifdef FEAT_MBYTE
26081 if (has_mbyte)
26082 p -= mb_head_off(tfname, p);
26083#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026084 if (vim_ispathsep(*p))
26085 {
26086 if (sepcount == 0 || (hasTilde && sepcount == 1))
26087 break;
26088 else
26089 sepcount --;
26090 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026091 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026092 if (hasTilde)
26093 {
26094 --p;
26095 if (p >= tfname)
26096 *p = '~';
26097 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026098 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026099 }
26100 else
26101 ++p;
26102
26103 /* Copy in the string - p indexes into tfname - allocated at pbuf */
26104 vim_free(*bufp);
26105 *fnamelen = (int)STRLEN(p);
26106 *bufp = pbuf;
26107 *fnamep = p;
26108
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026109 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026110}
26111#endif /* WIN3264 */
26112
26113/*
26114 * Adjust a filename, according to a string of modifiers.
26115 * *fnamep must be NUL terminated when called. When returning, the length is
26116 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026117 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026118 * When there is an error, *fnamep is set to NULL.
26119 */
26120 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010026121modify_fname(
26122 char_u *src, /* string with modifiers */
26123 int *usedlen, /* characters after src that are used */
26124 char_u **fnamep, /* file name so far */
26125 char_u **bufp, /* buffer for allocated file name or NULL */
26126 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026127{
26128 int valid = 0;
26129 char_u *tail;
26130 char_u *s, *p, *pbuf;
26131 char_u dirname[MAXPATHL];
26132 int c;
26133 int has_fullname = 0;
26134#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026135 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026136 int has_shortname = 0;
26137#endif
26138
26139repeat:
26140 /* ":p" - full path/file_name */
26141 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26142 {
26143 has_fullname = 1;
26144
26145 valid |= VALID_PATH;
26146 *usedlen += 2;
26147
26148 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26149 if ((*fnamep)[0] == '~'
26150#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26151 && ((*fnamep)[1] == '/'
26152# ifdef BACKSLASH_IN_FILENAME
26153 || (*fnamep)[1] == '\\'
26154# endif
26155 || (*fnamep)[1] == NUL)
26156
26157#endif
26158 )
26159 {
26160 *fnamep = expand_env_save(*fnamep);
26161 vim_free(*bufp); /* free any allocated file name */
26162 *bufp = *fnamep;
26163 if (*fnamep == NULL)
26164 return -1;
26165 }
26166
26167 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026168 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026169 {
26170 if (vim_ispathsep(*p)
26171 && p[1] == '.'
26172 && (p[2] == NUL
26173 || vim_ispathsep(p[2])
26174 || (p[2] == '.'
26175 && (p[3] == NUL || vim_ispathsep(p[3])))))
26176 break;
26177 }
26178
26179 /* FullName_save() is slow, don't use it when not needed. */
26180 if (*p != NUL || !vim_isAbsName(*fnamep))
26181 {
26182 *fnamep = FullName_save(*fnamep, *p != NUL);
26183 vim_free(*bufp); /* free any allocated file name */
26184 *bufp = *fnamep;
26185 if (*fnamep == NULL)
26186 return -1;
26187 }
26188
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026189#ifdef WIN3264
26190# if _WIN32_WINNT >= 0x0500
26191 if (vim_strchr(*fnamep, '~') != NULL)
26192 {
26193 /* Expand 8.3 filename to full path. Needed to make sure the same
26194 * file does not have two different names.
26195 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26196 p = alloc(_MAX_PATH + 1);
26197 if (p != NULL)
26198 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026199 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026200 {
26201 vim_free(*bufp);
26202 *bufp = *fnamep = p;
26203 }
26204 else
26205 vim_free(p);
26206 }
26207 }
26208# endif
26209#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026210 /* Append a path separator to a directory. */
26211 if (mch_isdir(*fnamep))
26212 {
26213 /* Make room for one or two extra characters. */
26214 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26215 vim_free(*bufp); /* free any allocated file name */
26216 *bufp = *fnamep;
26217 if (*fnamep == NULL)
26218 return -1;
26219 add_pathsep(*fnamep);
26220 }
26221 }
26222
26223 /* ":." - path relative to the current directory */
26224 /* ":~" - path relative to the home directory */
26225 /* ":8" - shortname path - postponed till after */
26226 while (src[*usedlen] == ':'
26227 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26228 {
26229 *usedlen += 2;
26230 if (c == '8')
26231 {
26232#ifdef WIN3264
26233 has_shortname = 1; /* Postpone this. */
26234#endif
26235 continue;
26236 }
26237 pbuf = NULL;
26238 /* Need full path first (use expand_env() to remove a "~/") */
26239 if (!has_fullname)
26240 {
26241 if (c == '.' && **fnamep == '~')
26242 p = pbuf = expand_env_save(*fnamep);
26243 else
26244 p = pbuf = FullName_save(*fnamep, FALSE);
26245 }
26246 else
26247 p = *fnamep;
26248
26249 has_fullname = 0;
26250
26251 if (p != NULL)
26252 {
26253 if (c == '.')
26254 {
26255 mch_dirname(dirname, MAXPATHL);
26256 s = shorten_fname(p, dirname);
26257 if (s != NULL)
26258 {
26259 *fnamep = s;
26260 if (pbuf != NULL)
26261 {
26262 vim_free(*bufp); /* free any allocated file name */
26263 *bufp = pbuf;
26264 pbuf = NULL;
26265 }
26266 }
26267 }
26268 else
26269 {
26270 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26271 /* Only replace it when it starts with '~' */
26272 if (*dirname == '~')
26273 {
26274 s = vim_strsave(dirname);
26275 if (s != NULL)
26276 {
26277 *fnamep = s;
26278 vim_free(*bufp);
26279 *bufp = s;
26280 }
26281 }
26282 }
26283 vim_free(pbuf);
26284 }
26285 }
26286
26287 tail = gettail(*fnamep);
26288 *fnamelen = (int)STRLEN(*fnamep);
26289
26290 /* ":h" - head, remove "/file_name", can be repeated */
26291 /* Don't remove the first "/" or "c:\" */
26292 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26293 {
26294 valid |= VALID_HEAD;
26295 *usedlen += 2;
26296 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026297 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026298 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026299 *fnamelen = (int)(tail - *fnamep);
26300#ifdef VMS
26301 if (*fnamelen > 0)
26302 *fnamelen += 1; /* the path separator is part of the path */
26303#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026304 if (*fnamelen == 0)
26305 {
26306 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26307 p = vim_strsave((char_u *)".");
26308 if (p == NULL)
26309 return -1;
26310 vim_free(*bufp);
26311 *bufp = *fnamep = tail = p;
26312 *fnamelen = 1;
26313 }
26314 else
26315 {
26316 while (tail > s && !after_pathsep(s, tail))
26317 mb_ptr_back(*fnamep, tail);
26318 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026319 }
26320
26321 /* ":8" - shortname */
26322 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26323 {
26324 *usedlen += 2;
26325#ifdef WIN3264
26326 has_shortname = 1;
26327#endif
26328 }
26329
26330#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026331 /*
26332 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026333 */
26334 if (has_shortname)
26335 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026336 /* Copy the string if it is shortened by :h and when it wasn't copied
26337 * yet, because we are going to change it in place. Avoids changing
26338 * the buffer name for "%:8". */
26339 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026340 {
26341 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026342 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026343 return -1;
26344 vim_free(*bufp);
26345 *bufp = *fnamep = p;
26346 }
26347
26348 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026349 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026350 if (!has_fullname && !vim_isAbsName(*fnamep))
26351 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026352 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026353 return -1;
26354 }
26355 else
26356 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026357 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026358
Bram Moolenaardc935552011-08-17 15:23:23 +020026359 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026360 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026361 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 return -1;
26363
26364 if (l == 0)
26365 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026366 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026367 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026368 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026369 return -1;
26370 }
26371 *fnamelen = l;
26372 }
26373 }
26374#endif /* WIN3264 */
26375
26376 /* ":t" - tail, just the basename */
26377 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26378 {
26379 *usedlen += 2;
26380 *fnamelen -= (int)(tail - *fnamep);
26381 *fnamep = tail;
26382 }
26383
26384 /* ":e" - extension, can be repeated */
26385 /* ":r" - root, without extension, can be repeated */
26386 while (src[*usedlen] == ':'
26387 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26388 {
26389 /* find a '.' in the tail:
26390 * - for second :e: before the current fname
26391 * - otherwise: The last '.'
26392 */
26393 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26394 s = *fnamep - 2;
26395 else
26396 s = *fnamep + *fnamelen - 1;
26397 for ( ; s > tail; --s)
26398 if (s[0] == '.')
26399 break;
26400 if (src[*usedlen + 1] == 'e') /* :e */
26401 {
26402 if (s > tail)
26403 {
26404 *fnamelen += (int)(*fnamep - (s + 1));
26405 *fnamep = s + 1;
26406#ifdef VMS
26407 /* cut version from the extension */
26408 s = *fnamep + *fnamelen - 1;
26409 for ( ; s > *fnamep; --s)
26410 if (s[0] == ';')
26411 break;
26412 if (s > *fnamep)
26413 *fnamelen = s - *fnamep;
26414#endif
26415 }
26416 else if (*fnamep <= tail)
26417 *fnamelen = 0;
26418 }
26419 else /* :r */
26420 {
26421 if (s > tail) /* remove one extension */
26422 *fnamelen = (int)(s - *fnamep);
26423 }
26424 *usedlen += 2;
26425 }
26426
26427 /* ":s?pat?foo?" - substitute */
26428 /* ":gs?pat?foo?" - global substitute */
26429 if (src[*usedlen] == ':'
26430 && (src[*usedlen + 1] == 's'
26431 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26432 {
26433 char_u *str;
26434 char_u *pat;
26435 char_u *sub;
26436 int sep;
26437 char_u *flags;
26438 int didit = FALSE;
26439
26440 flags = (char_u *)"";
26441 s = src + *usedlen + 2;
26442 if (src[*usedlen + 1] == 'g')
26443 {
26444 flags = (char_u *)"g";
26445 ++s;
26446 }
26447
26448 sep = *s++;
26449 if (sep)
26450 {
26451 /* find end of pattern */
26452 p = vim_strchr(s, sep);
26453 if (p != NULL)
26454 {
26455 pat = vim_strnsave(s, (int)(p - s));
26456 if (pat != NULL)
26457 {
26458 s = p + 1;
26459 /* find end of substitution */
26460 p = vim_strchr(s, sep);
26461 if (p != NULL)
26462 {
26463 sub = vim_strnsave(s, (int)(p - s));
26464 str = vim_strnsave(*fnamep, *fnamelen);
26465 if (sub != NULL && str != NULL)
26466 {
26467 *usedlen = (int)(p + 1 - src);
26468 s = do_string_sub(str, pat, sub, flags);
26469 if (s != NULL)
26470 {
26471 *fnamep = s;
26472 *fnamelen = (int)STRLEN(s);
26473 vim_free(*bufp);
26474 *bufp = s;
26475 didit = TRUE;
26476 }
26477 }
26478 vim_free(sub);
26479 vim_free(str);
26480 }
26481 vim_free(pat);
26482 }
26483 }
26484 /* after using ":s", repeat all the modifiers */
26485 if (didit)
26486 goto repeat;
26487 }
26488 }
26489
Bram Moolenaar26df0922014-02-23 23:39:13 +010026490 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26491 {
Bram Moolenaar5ca84ce2016-03-23 22:28:25 +010026492 /* vim_strsave_shellescape() needs a NUL terminated string. */
Bram Moolenaard4caf5c2016-03-24 19:14:35 +010026493 c = (*fnamep)[*fnamelen];
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026494 if (c != NUL)
26495 (*fnamep)[*fnamelen] = NUL;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026496 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
Bram Moolenaar52c6eaf2016-03-25 18:42:46 +010026497 if (c != NUL)
26498 (*fnamep)[*fnamelen] = c;
Bram Moolenaar26df0922014-02-23 23:39:13 +010026499 if (p == NULL)
26500 return -1;
26501 vim_free(*bufp);
26502 *bufp = *fnamep = p;
26503 *fnamelen = (int)STRLEN(p);
26504 *usedlen += 2;
26505 }
26506
Bram Moolenaar071d4272004-06-13 20:20:40 +000026507 return valid;
26508}
26509
26510/*
26511 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26512 * "flags" can be "g" to do a global substitute.
26513 * Returns an allocated string, NULL for error.
26514 */
26515 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026516do_string_sub(
26517 char_u *str,
26518 char_u *pat,
26519 char_u *sub,
26520 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026521{
26522 int sublen;
26523 regmatch_T regmatch;
26524 int i;
26525 int do_all;
26526 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026527 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026528 garray_T ga;
26529 char_u *ret;
26530 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026531 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026532
26533 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26534 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026535 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026536
26537 ga_init2(&ga, 1, 200);
26538
26539 do_all = (flags[0] == 'g');
26540
26541 regmatch.rm_ic = p_ic;
26542 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26543 if (regmatch.regprog != NULL)
26544 {
26545 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026546 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026547 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26548 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026549 /* Skip empty match except for first match. */
26550 if (regmatch.startp[0] == regmatch.endp[0])
26551 {
26552 if (zero_width == regmatch.startp[0])
26553 {
26554 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026555 i = MB_PTR2LEN(tail);
26556 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26557 (size_t)i);
26558 ga.ga_len += i;
26559 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026560 continue;
26561 }
26562 zero_width = regmatch.startp[0];
26563 }
26564
Bram Moolenaar071d4272004-06-13 20:20:40 +000026565 /*
26566 * Get some space for a temporary buffer to do the substitution
26567 * into. It will contain:
26568 * - The text up to where the match is.
26569 * - The substituted text.
26570 * - The text after the match.
26571 */
26572 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026573 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026574 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26575 {
26576 ga_clear(&ga);
26577 break;
26578 }
26579
26580 /* copy the text up to where the match is */
26581 i = (int)(regmatch.startp[0] - tail);
26582 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26583 /* add the substituted text */
26584 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26585 + ga.ga_len + i, TRUE, TRUE, FALSE);
26586 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026587 tail = regmatch.endp[0];
26588 if (*tail == NUL)
26589 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026590 if (!do_all)
26591 break;
26592 }
26593
26594 if (ga.ga_data != NULL)
26595 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26596
Bram Moolenaar473de612013-06-08 18:19:48 +020026597 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026598 }
26599
26600 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26601 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026602 if (p_cpo == empty_option)
26603 p_cpo = save_cpo;
26604 else
26605 /* Darn, evaluating {sub} expression changed the value. */
26606 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026607
26608 return ret;
26609}
26610
26611#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */