blob: cc23de490c14d6cfde666454d6bc55be8676cb45 [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 Moolenaar9a50b1b2005-06-27 22:48:21 +0000212/* list heads for garbage collection */
213static dict_T *first_dict = NULL; /* list of all dicts */
214static list_T *first_list = NULL; /* list of all lists */
215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000216/* From user function to hashitem and back. */
217static ufunc_T dumuf;
218#define UF2HIKEY(fp) ((fp)->uf_name)
219#define HIKEY2UF(p) ((ufunc_T *)(p - (dumuf.uf_name - (char_u *)&dumuf)))
220#define HI2UF(hi) HIKEY2UF((hi)->hi_key)
221
222#define FUNCARG(fp, j) ((char_u **)(fp->uf_args.ga_data))[j]
223#define FUNCLINE(fp, j) ((char_u **)(fp->uf_lines.ga_data))[j]
Bram Moolenaar071d4272004-06-13 20:20:40 +0000224
Bram Moolenaar33570922005-01-25 22:26:29 +0000225#define MAX_FUNC_ARGS 20 /* maximum number of function arguments */
226#define VAR_SHORT_LEN 20 /* short variable name length */
227#define FIXVAR_CNT 12 /* number of fixed variables */
228
Bram Moolenaar071d4272004-06-13 20:20:40 +0000229/* structure to hold info for a function that is currently being executed. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000230typedef struct funccall_S funccall_T;
231
232struct funccall_S
Bram Moolenaar071d4272004-06-13 20:20:40 +0000233{
234 ufunc_T *func; /* function being called */
235 int linenr; /* next line to be executed */
236 int returned; /* ":return" used */
Bram Moolenaar33570922005-01-25 22:26:29 +0000237 struct /* fixed variables for arguments */
238 {
239 dictitem_T var; /* variable (without room for name) */
240 char_u room[VAR_SHORT_LEN]; /* room for the name */
241 } fixvar[FIXVAR_CNT];
242 dict_T l_vars; /* l: local function variables */
243 dictitem_T l_vars_var; /* variable for l: scope */
244 dict_T l_avars; /* a: argument variables */
245 dictitem_T l_avars_var; /* variable for a: scope */
246 list_T l_varlist; /* list for a:000 */
247 listitem_T l_listitems[MAX_FUNC_ARGS]; /* listitems for a:000 */
248 typval_T *rettv; /* return value */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000249 linenr_T breakpoint; /* next line with breakpoint or zero */
250 int dbg_tick; /* debug_tick when breakpoint was set */
251 int level; /* top nesting level of executed function */
Bram Moolenaar05159a02005-02-26 23:04:13 +0000252#ifdef FEAT_PROFILE
253 proftime_T prof_child; /* time spent in a child */
254#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +0000255 funccall_T *caller; /* calling function or NULL */
256};
Bram Moolenaar071d4272004-06-13 20:20:40 +0000257
258/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000259 * Info used by a ":for" loop.
260 */
Bram Moolenaar33570922005-01-25 22:26:29 +0000261typedef struct
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000262{
263 int fi_semicolon; /* TRUE if ending in '; var]' */
264 int fi_varcount; /* nr of variables in the list */
Bram Moolenaar33570922005-01-25 22:26:29 +0000265 listwatch_T fi_lw; /* keep an eye on the item used. */
266 list_T *fi_list; /* list being used */
267} forinfo_T;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000268
Bram Moolenaar3d60ec22005-01-05 22:19:46 +0000269/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000270 * Struct used by trans_function_name()
271 */
272typedef struct
273{
Bram Moolenaar33570922005-01-25 22:26:29 +0000274 dict_T *fd_dict; /* Dictionary used */
Bram Moolenaar532c7802005-01-27 14:44:31 +0000275 char_u *fd_newkey; /* new key in "dict" in allocated memory */
Bram Moolenaar33570922005-01-25 22:26:29 +0000276 dictitem_T *fd_di; /* Dictionary item used */
277} funcdict_T;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +0000278
Bram Moolenaara7043832005-01-21 11:56:39 +0000279
280/*
Bram Moolenaar33570922005-01-25 22:26:29 +0000281 * Array to hold the value of v: variables.
282 * The value is in a dictitem, so that it can also be used in the v: scope.
283 * The reason to use this table anyway is for very quick access to the
284 * variables with the VV_ defines.
285 */
286#include "version.h"
287
288/* values for vv_flags: */
289#define VV_COMPAT 1 /* compatible, also used without "v:" */
290#define VV_RO 2 /* read-only */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000291#define VV_RO_SBX 4 /* read-only in the sandbox */
Bram Moolenaar33570922005-01-25 22:26:29 +0000292
Bram Moolenaarcdb92af2009-06-03 12:26:06 +0000293#define VV_NAME(s, t) s, {{t, 0, {0}}, 0, {0}}, {0}
Bram Moolenaar33570922005-01-25 22:26:29 +0000294
295static struct vimvar
296{
297 char *vv_name; /* name of variable, without v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000298 dictitem_T vv_di; /* value and name for key */
299 char vv_filler[16]; /* space for LONGEST name below!!! */
300 char vv_flags; /* VV_COMPAT, VV_RO, VV_RO_SBX */
301} vimvars[VV_LEN] =
302{
303 /*
304 * The order here must match the VV_ defines in vim.h!
305 * Initializing a union does not work, leave tv.vval empty to get zero's.
306 */
307 {VV_NAME("count", VAR_NUMBER), VV_COMPAT+VV_RO},
308 {VV_NAME("count1", VAR_NUMBER), VV_RO},
309 {VV_NAME("prevcount", VAR_NUMBER), VV_RO},
310 {VV_NAME("errmsg", VAR_STRING), VV_COMPAT},
311 {VV_NAME("warningmsg", VAR_STRING), 0},
312 {VV_NAME("statusmsg", VAR_STRING), 0},
313 {VV_NAME("shell_error", VAR_NUMBER), VV_COMPAT+VV_RO},
314 {VV_NAME("this_session", VAR_STRING), VV_COMPAT},
315 {VV_NAME("version", VAR_NUMBER), VV_COMPAT+VV_RO},
316 {VV_NAME("lnum", VAR_NUMBER), VV_RO_SBX},
317 {VV_NAME("termresponse", VAR_STRING), VV_RO},
318 {VV_NAME("fname", VAR_STRING), VV_RO},
319 {VV_NAME("lang", VAR_STRING), VV_RO},
320 {VV_NAME("lc_time", VAR_STRING), VV_RO},
321 {VV_NAME("ctype", VAR_STRING), VV_RO},
322 {VV_NAME("charconvert_from", VAR_STRING), VV_RO},
323 {VV_NAME("charconvert_to", VAR_STRING), VV_RO},
324 {VV_NAME("fname_in", VAR_STRING), VV_RO},
325 {VV_NAME("fname_out", VAR_STRING), VV_RO},
326 {VV_NAME("fname_new", VAR_STRING), VV_RO},
327 {VV_NAME("fname_diff", VAR_STRING), VV_RO},
328 {VV_NAME("cmdarg", VAR_STRING), VV_RO},
329 {VV_NAME("foldstart", VAR_NUMBER), VV_RO_SBX},
330 {VV_NAME("foldend", VAR_NUMBER), VV_RO_SBX},
331 {VV_NAME("folddashes", VAR_STRING), VV_RO_SBX},
332 {VV_NAME("foldlevel", VAR_NUMBER), VV_RO_SBX},
333 {VV_NAME("progname", VAR_STRING), VV_RO},
334 {VV_NAME("servername", VAR_STRING), VV_RO},
335 {VV_NAME("dying", VAR_NUMBER), VV_RO},
336 {VV_NAME("exception", VAR_STRING), VV_RO},
337 {VV_NAME("throwpoint", VAR_STRING), VV_RO},
338 {VV_NAME("register", VAR_STRING), VV_RO},
339 {VV_NAME("cmdbang", VAR_NUMBER), VV_RO},
340 {VV_NAME("insertmode", VAR_STRING), VV_RO},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000341 {VV_NAME("val", VAR_UNKNOWN), VV_RO},
342 {VV_NAME("key", VAR_UNKNOWN), VV_RO},
Bram Moolenaar05159a02005-02-26 23:04:13 +0000343 {VV_NAME("profiling", VAR_NUMBER), VV_RO},
Bram Moolenaar19a09a12005-03-04 23:39:37 +0000344 {VV_NAME("fcs_reason", VAR_STRING), VV_RO},
345 {VV_NAME("fcs_choice", VAR_STRING), 0},
Bram Moolenaare2ac10d2005-03-07 23:26:06 +0000346 {VV_NAME("beval_bufnr", VAR_NUMBER), VV_RO},
347 {VV_NAME("beval_winnr", VAR_NUMBER), VV_RO},
348 {VV_NAME("beval_lnum", VAR_NUMBER), VV_RO},
349 {VV_NAME("beval_col", VAR_NUMBER), VV_RO},
350 {VV_NAME("beval_text", VAR_STRING), VV_RO},
Bram Moolenaar761b1132005-10-03 22:05:45 +0000351 {VV_NAME("scrollstart", VAR_STRING), 0},
Bram Moolenaard5bc83f2005-12-07 21:07:59 +0000352 {VV_NAME("swapname", VAR_STRING), VV_RO},
353 {VV_NAME("swapchoice", VAR_STRING), 0},
Bram Moolenaar63a121b2005-12-11 21:36:39 +0000354 {VV_NAME("swapcommand", VAR_STRING), VV_RO},
Bram Moolenaare659c952011-05-19 17:25:41 +0200355 {VV_NAME("char", VAR_STRING), 0},
Bram Moolenaar219b8702006-11-01 14:32:36 +0000356 {VV_NAME("mouse_win", VAR_NUMBER), 0},
357 {VV_NAME("mouse_lnum", VAR_NUMBER), 0},
358 {VV_NAME("mouse_col", VAR_NUMBER), 0},
Bram Moolenaar8af1fbf2008-01-05 12:35:21 +0000359 {VV_NAME("operator", VAR_STRING), VV_RO},
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000360 {VV_NAME("searchforward", VAR_NUMBER), 0},
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100361 {VV_NAME("hlsearch", VAR_NUMBER), 0},
Bram Moolenaard812df62008-11-09 12:46:09 +0000362 {VV_NAME("oldfiles", VAR_LIST), 0},
Bram Moolenaar727c8762010-10-20 19:17:48 +0200363 {VV_NAME("windowid", VAR_NUMBER), VV_RO},
Bram Moolenaara1706c92014-04-01 19:55:49 +0200364 {VV_NAME("progpath", VAR_STRING), VV_RO},
Bram Moolenaar42a45122015-07-10 17:56:23 +0200365 {VV_NAME("completed_item", VAR_DICT), VV_RO},
Bram Moolenaar53744302015-07-17 17:38:22 +0200366 {VV_NAME("option_new", VAR_STRING), VV_RO},
367 {VV_NAME("option_old", VAR_STRING), VV_RO},
368 {VV_NAME("option_type", VAR_STRING), VV_RO},
Bram Moolenaar43345542015-11-29 17:35:35 +0100369 {VV_NAME("errors", VAR_LIST), 0},
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100370 {VV_NAME("false", VAR_SPECIAL), VV_RO},
371 {VV_NAME("true", VAR_SPECIAL), VV_RO},
372 {VV_NAME("null", VAR_SPECIAL), VV_RO},
373 {VV_NAME("none", VAR_SPECIAL), VV_RO},
Bram Moolenaar33570922005-01-25 22:26:29 +0000374};
375
376/* shorthand */
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000377#define vv_type vv_di.di_tv.v_type
378#define vv_nr vv_di.di_tv.vval.v_number
379#define vv_float vv_di.di_tv.vval.v_float
380#define vv_str vv_di.di_tv.vval.v_string
Bram Moolenaard812df62008-11-09 12:46:09 +0000381#define vv_list vv_di.di_tv.vval.v_list
Bram Moolenaar42a45122015-07-10 17:56:23 +0200382#define vv_dict vv_di.di_tv.vval.v_dict
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000383#define vv_tv vv_di.di_tv
Bram Moolenaar33570922005-01-25 22:26:29 +0000384
Bram Moolenaar230bb3f2013-04-24 14:07:45 +0200385static dictitem_T vimvars_var; /* variable used for v: */
Bram Moolenaar33570922005-01-25 22:26:29 +0000386#define vimvarht vimvardict.dv_hashtab
387
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100388static void prepare_vimvar(int idx, typval_T *save_tv);
389static void restore_vimvar(int idx, typval_T *save_tv);
390static int ex_let_vars(char_u *arg, typval_T *tv, int copy, int semicolon, int var_count, char_u *nextchars);
391static char_u *skip_var_list(char_u *arg, int *var_count, int *semicolon);
392static char_u *skip_var_one(char_u *arg);
393static void list_hashtable_vars(hashtab_T *ht, char_u *prefix, int empty, int *first);
394static void list_glob_vars(int *first);
395static void list_buf_vars(int *first);
396static void list_win_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000397#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100398static void list_tab_vars(int *first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000399#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100400static void list_vim_vars(int *first);
401static void list_script_vars(int *first);
402static void list_func_vars(int *first);
403static char_u *list_arg_vars(exarg_T *eap, char_u *arg, int *first);
404static char_u *ex_let_one(char_u *arg, typval_T *tv, int copy, char_u *endchars, char_u *op);
405static int check_changedtick(char_u *arg);
406static char_u *get_lval(char_u *name, typval_T *rettv, lval_T *lp, int unlet, int skip, int flags, int fne_flags);
407static void clear_lval(lval_T *lp);
408static void set_var_lval(lval_T *lp, char_u *endp, typval_T *rettv, int copy, char_u *op);
409static int tv_op(typval_T *tv1, typval_T *tv2, char_u *op);
410static void list_fix_watch(list_T *l, listitem_T *item);
411static void ex_unletlock(exarg_T *eap, char_u *argstart, int deep);
412static int do_unlet_var(lval_T *lp, char_u *name_end, int forceit);
413static int do_lock_var(lval_T *lp, char_u *name_end, int deep, int lock);
414static void item_lock(typval_T *tv, int deep, int lock);
415static int tv_islocked(typval_T *tv);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000416
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100417static int eval0(char_u *arg, typval_T *rettv, char_u **nextcmd, int evaluate);
418static int eval1(char_u **arg, typval_T *rettv, int evaluate);
419static int eval2(char_u **arg, typval_T *rettv, int evaluate);
420static int eval3(char_u **arg, typval_T *rettv, int evaluate);
421static int eval4(char_u **arg, typval_T *rettv, int evaluate);
422static int eval5(char_u **arg, typval_T *rettv, int evaluate);
423static int eval6(char_u **arg, typval_T *rettv, int evaluate, int want_string);
424static int eval7(char_u **arg, typval_T *rettv, int evaluate, int want_string);
Bram Moolenaara40058a2005-07-11 22:42:07 +0000425
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100426static int eval_index(char_u **arg, typval_T *rettv, int evaluate, int verbose);
427static int get_option_tv(char_u **arg, typval_T *rettv, int evaluate);
428static int get_string_tv(char_u **arg, typval_T *rettv, int evaluate);
429static int get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate);
430static int get_list_tv(char_u **arg, typval_T *rettv, int evaluate);
431static long list_len(list_T *l);
432static int list_equal(list_T *l1, list_T *l2, int ic, int recursive);
433static int dict_equal(dict_T *d1, dict_T *d2, int ic, int recursive);
434static int tv_equal(typval_T *tv1, typval_T *tv2, int ic, int recursive);
435static long list_find_nr(list_T *l, long idx, int *errorp);
436static long list_idx_of_item(list_T *l, listitem_T *item);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100437static int list_extend(list_T *l1, list_T *l2, listitem_T *bef);
438static int list_concat(list_T *l1, list_T *l2, typval_T *tv);
439static list_T *list_copy(list_T *orig, int deep, int copyID);
440static char_u *list2string(typval_T *tv, int copyID);
441static int list_join_inner(garray_T *gap, list_T *l, char_u *sep, int echo_style, int copyID, garray_T *join_gap);
442static int list_join(garray_T *gap, list_T *l, char_u *sep, int echo, int copyID);
443static int free_unref_items(int copyID);
444static dictitem_T *dictitem_copy(dictitem_T *org);
445static void dictitem_remove(dict_T *dict, dictitem_T *item);
446static dict_T *dict_copy(dict_T *orig, int deep, int copyID);
447static long dict_len(dict_T *d);
448static char_u *dict2string(typval_T *tv, int copyID);
449static int get_dict_tv(char_u **arg, typval_T *rettv, int evaluate);
450static char_u *echo_string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
451static char_u *tv2string(typval_T *tv, char_u **tofree, char_u *numbuf, int copyID);
452static char_u *string_quote(char_u *str, int function);
453static int get_env_tv(char_u **arg, typval_T *rettv, int evaluate);
454static int find_internal_func(char_u *name);
Bram Moolenaar1735bc92016-03-14 23:05:14 +0100455static char_u *deref_func_name(char_u *name, int *lenp, partial_T **partial, int no_autoload);
456static 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 +0100457static void emsg_funcname(char *ermsg, char_u *name);
458static int non_zero_arg(typval_T *argvars);
Bram Moolenaar33570922005-01-25 22:26:29 +0000459
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000460#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100461static void f_abs(typval_T *argvars, typval_T *rettv);
462static void f_acos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000463#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100464static void f_add(typval_T *argvars, typval_T *rettv);
465static void f_alloc_fail(typval_T *argvars, typval_T *rettv);
466static void f_and(typval_T *argvars, typval_T *rettv);
467static void f_append(typval_T *argvars, typval_T *rettv);
468static void f_argc(typval_T *argvars, typval_T *rettv);
469static void f_argidx(typval_T *argvars, typval_T *rettv);
470static void f_arglistid(typval_T *argvars, typval_T *rettv);
471static void f_argv(typval_T *argvars, typval_T *rettv);
472static void f_assert_equal(typval_T *argvars, typval_T *rettv);
473static void f_assert_exception(typval_T *argvars, typval_T *rettv);
474static void f_assert_fails(typval_T *argvars, typval_T *rettv);
475static void f_assert_false(typval_T *argvars, typval_T *rettv);
476static void f_assert_true(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000477#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100478static void f_asin(typval_T *argvars, typval_T *rettv);
479static void f_atan(typval_T *argvars, typval_T *rettv);
480static void f_atan2(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000481#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100482static void f_browse(typval_T *argvars, typval_T *rettv);
483static void f_browsedir(typval_T *argvars, typval_T *rettv);
484static void f_bufexists(typval_T *argvars, typval_T *rettv);
485static void f_buflisted(typval_T *argvars, typval_T *rettv);
486static void f_bufloaded(typval_T *argvars, typval_T *rettv);
487static void f_bufname(typval_T *argvars, typval_T *rettv);
488static void f_bufnr(typval_T *argvars, typval_T *rettv);
489static void f_bufwinnr(typval_T *argvars, typval_T *rettv);
490static void f_byte2line(typval_T *argvars, typval_T *rettv);
491static void byteidx(typval_T *argvars, typval_T *rettv, int comp);
492static void f_byteidx(typval_T *argvars, typval_T *rettv);
493static void f_byteidxcomp(typval_T *argvars, typval_T *rettv);
494static void f_call(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000495#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100496static void f_ceil(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000497#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100498#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100499static void f_ch_close(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8b1862a2016-02-27 19:21:24 +0100500static void f_ch_evalexpr(typval_T *argvars, typval_T *rettv);
501static void f_ch_evalraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +0100502static void f_ch_getbufnr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar02e83b42016-02-21 20:10:26 +0100503static void f_ch_getjob(typval_T *argvars, typval_T *rettv);
Bram Moolenaar81661fb2016-02-18 22:23:34 +0100504static void f_ch_log(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100505static void f_ch_logfile(typval_T *argvars, typval_T *rettv);
506static void f_ch_open(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6f3a5442016-02-20 19:56:13 +0100507static void f_ch_read(typval_T *argvars, typval_T *rettv);
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100508static void f_ch_readraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100509static void f_ch_sendexpr(typval_T *argvars, typval_T *rettv);
510static void f_ch_sendraw(typval_T *argvars, typval_T *rettv);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +0100511static void f_ch_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar77073442016-02-13 23:23:53 +0100512static void f_ch_status(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf57969a2016-02-02 20:47:49 +0100513#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100514static void f_changenr(typval_T *argvars, typval_T *rettv);
515static void f_char2nr(typval_T *argvars, typval_T *rettv);
516static void f_cindent(typval_T *argvars, typval_T *rettv);
517static void f_clearmatches(typval_T *argvars, typval_T *rettv);
518static void f_col(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000519#if defined(FEAT_INS_EXPAND)
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100520static void f_complete(typval_T *argvars, typval_T *rettv);
521static void f_complete_add(typval_T *argvars, typval_T *rettv);
522static void f_complete_check(typval_T *argvars, typval_T *rettv);
Bram Moolenaar572cb562005-08-05 21:35:02 +0000523#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100524static void f_confirm(typval_T *argvars, typval_T *rettv);
525static void f_copy(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000526#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100527static void f_cos(typval_T *argvars, typval_T *rettv);
528static void f_cosh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000529#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100530static void f_count(typval_T *argvars, typval_T *rettv);
531static void f_cscope_connection(typval_T *argvars, typval_T *rettv);
532static void f_cursor(typval_T *argsvars, typval_T *rettv);
533static void f_deepcopy(typval_T *argvars, typval_T *rettv);
534static void f_delete(typval_T *argvars, typval_T *rettv);
535static void f_did_filetype(typval_T *argvars, typval_T *rettv);
536static void f_diff_filler(typval_T *argvars, typval_T *rettv);
537static void f_diff_hlID(typval_T *argvars, typval_T *rettv);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +0100538static void f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100539static void f_empty(typval_T *argvars, typval_T *rettv);
540static void f_escape(typval_T *argvars, typval_T *rettv);
541static void f_eval(typval_T *argvars, typval_T *rettv);
542static void f_eventhandler(typval_T *argvars, typval_T *rettv);
543static void f_executable(typval_T *argvars, typval_T *rettv);
544static void f_exepath(typval_T *argvars, typval_T *rettv);
545static void f_exists(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200546#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100547static void f_exp(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200548#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100549static void f_expand(typval_T *argvars, typval_T *rettv);
550static void f_extend(typval_T *argvars, typval_T *rettv);
551static void f_feedkeys(typval_T *argvars, typval_T *rettv);
552static void f_filereadable(typval_T *argvars, typval_T *rettv);
553static void f_filewritable(typval_T *argvars, typval_T *rettv);
554static void f_filter(typval_T *argvars, typval_T *rettv);
555static void f_finddir(typval_T *argvars, typval_T *rettv);
556static void f_findfile(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000557#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100558static void f_float2nr(typval_T *argvars, typval_T *rettv);
559static void f_floor(typval_T *argvars, typval_T *rettv);
560static void f_fmod(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000561#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100562static void f_fnameescape(typval_T *argvars, typval_T *rettv);
563static void f_fnamemodify(typval_T *argvars, typval_T *rettv);
564static void f_foldclosed(typval_T *argvars, typval_T *rettv);
565static void f_foldclosedend(typval_T *argvars, typval_T *rettv);
566static void f_foldlevel(typval_T *argvars, typval_T *rettv);
567static void f_foldtext(typval_T *argvars, typval_T *rettv);
568static void f_foldtextresult(typval_T *argvars, typval_T *rettv);
569static void f_foreground(typval_T *argvars, typval_T *rettv);
570static void f_function(typval_T *argvars, typval_T *rettv);
571static void f_garbagecollect(typval_T *argvars, typval_T *rettv);
572static void f_get(typval_T *argvars, typval_T *rettv);
573static void f_getbufline(typval_T *argvars, typval_T *rettv);
574static void f_getbufvar(typval_T *argvars, typval_T *rettv);
575static void f_getchar(typval_T *argvars, typval_T *rettv);
576static void f_getcharmod(typval_T *argvars, typval_T *rettv);
577static void f_getcharsearch(typval_T *argvars, typval_T *rettv);
578static void f_getcmdline(typval_T *argvars, typval_T *rettv);
579static void f_getcmdpos(typval_T *argvars, typval_T *rettv);
580static void f_getcmdtype(typval_T *argvars, typval_T *rettv);
581static void f_getcmdwintype(typval_T *argvars, typval_T *rettv);
582static void f_getcwd(typval_T *argvars, typval_T *rettv);
583static void f_getfontname(typval_T *argvars, typval_T *rettv);
584static void f_getfperm(typval_T *argvars, typval_T *rettv);
585static void f_getfsize(typval_T *argvars, typval_T *rettv);
586static void f_getftime(typval_T *argvars, typval_T *rettv);
587static void f_getftype(typval_T *argvars, typval_T *rettv);
588static void f_getline(typval_T *argvars, typval_T *rettv);
589static void f_getmatches(typval_T *argvars, typval_T *rettv);
590static void f_getpid(typval_T *argvars, typval_T *rettv);
591static void f_getcurpos(typval_T *argvars, typval_T *rettv);
592static void f_getpos(typval_T *argvars, typval_T *rettv);
593static void f_getqflist(typval_T *argvars, typval_T *rettv);
594static void f_getreg(typval_T *argvars, typval_T *rettv);
595static void f_getregtype(typval_T *argvars, typval_T *rettv);
596static void f_gettabvar(typval_T *argvars, typval_T *rettv);
597static void f_gettabwinvar(typval_T *argvars, typval_T *rettv);
598static void f_getwinposx(typval_T *argvars, typval_T *rettv);
599static void f_getwinposy(typval_T *argvars, typval_T *rettv);
600static void f_getwinvar(typval_T *argvars, typval_T *rettv);
601static void f_glob(typval_T *argvars, typval_T *rettv);
602static void f_globpath(typval_T *argvars, typval_T *rettv);
603static void f_glob2regpat(typval_T *argvars, typval_T *rettv);
604static void f_has(typval_T *argvars, typval_T *rettv);
605static void f_has_key(typval_T *argvars, typval_T *rettv);
606static void f_haslocaldir(typval_T *argvars, typval_T *rettv);
607static void f_hasmapto(typval_T *argvars, typval_T *rettv);
608static void f_histadd(typval_T *argvars, typval_T *rettv);
609static void f_histdel(typval_T *argvars, typval_T *rettv);
610static void f_histget(typval_T *argvars, typval_T *rettv);
611static void f_histnr(typval_T *argvars, typval_T *rettv);
612static void f_hlID(typval_T *argvars, typval_T *rettv);
613static void f_hlexists(typval_T *argvars, typval_T *rettv);
614static void f_hostname(typval_T *argvars, typval_T *rettv);
615static void f_iconv(typval_T *argvars, typval_T *rettv);
616static void f_indent(typval_T *argvars, typval_T *rettv);
617static void f_index(typval_T *argvars, typval_T *rettv);
618static void f_input(typval_T *argvars, typval_T *rettv);
619static void f_inputdialog(typval_T *argvars, typval_T *rettv);
620static void f_inputlist(typval_T *argvars, typval_T *rettv);
621static void f_inputrestore(typval_T *argvars, typval_T *rettv);
622static void f_inputsave(typval_T *argvars, typval_T *rettv);
623static void f_inputsecret(typval_T *argvars, typval_T *rettv);
624static void f_insert(typval_T *argvars, typval_T *rettv);
625static void f_invert(typval_T *argvars, typval_T *rettv);
626static void f_isdirectory(typval_T *argvars, typval_T *rettv);
627static void f_islocked(typval_T *argvars, typval_T *rettv);
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +0100628#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
629static void f_isnan(typval_T *argvars, typval_T *rettv);
630#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100631static void f_items(typval_T *argvars, typval_T *rettv);
Bram Moolenaar509ce2a2016-03-11 22:52:15 +0100632#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +0100633static void f_job_getchannel(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8950a562016-03-12 15:22:55 +0100634static void f_job_info(typval_T *argvars, typval_T *rettv);
Bram Moolenaar65edff82016-02-21 16:40:11 +0100635static void f_job_setoptions(typval_T *argvars, typval_T *rettv);
Bram Moolenaar835dc632016-02-07 14:27:38 +0100636static void f_job_start(typval_T *argvars, typval_T *rettv);
637static void f_job_stop(typval_T *argvars, typval_T *rettv);
638static void f_job_status(typval_T *argvars, typval_T *rettv);
639#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100640static void f_join(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7823a3b2016-02-11 21:08:32 +0100641static void f_js_decode(typval_T *argvars, typval_T *rettv);
642static void f_js_encode(typval_T *argvars, typval_T *rettv);
643static void f_json_decode(typval_T *argvars, typval_T *rettv);
644static void f_json_encode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100645static void f_keys(typval_T *argvars, typval_T *rettv);
646static void f_last_buffer_nr(typval_T *argvars, typval_T *rettv);
647static void f_len(typval_T *argvars, typval_T *rettv);
648static void f_libcall(typval_T *argvars, typval_T *rettv);
649static void f_libcallnr(typval_T *argvars, typval_T *rettv);
650static void f_line(typval_T *argvars, typval_T *rettv);
651static void f_line2byte(typval_T *argvars, typval_T *rettv);
652static void f_lispindent(typval_T *argvars, typval_T *rettv);
653static void f_localtime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000654#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100655static void f_log(typval_T *argvars, typval_T *rettv);
656static void f_log10(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000657#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +0200658#ifdef FEAT_LUA
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100659static void f_luaeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar1dced572012-04-05 16:54:08 +0200660#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100661static void f_map(typval_T *argvars, typval_T *rettv);
662static void f_maparg(typval_T *argvars, typval_T *rettv);
663static void f_mapcheck(typval_T *argvars, typval_T *rettv);
664static void f_match(typval_T *argvars, typval_T *rettv);
665static void f_matchadd(typval_T *argvars, typval_T *rettv);
666static void f_matchaddpos(typval_T *argvars, typval_T *rettv);
667static void f_matcharg(typval_T *argvars, typval_T *rettv);
668static void f_matchdelete(typval_T *argvars, typval_T *rettv);
669static void f_matchend(typval_T *argvars, typval_T *rettv);
670static void f_matchlist(typval_T *argvars, typval_T *rettv);
671static void f_matchstr(typval_T *argvars, typval_T *rettv);
672static void f_max(typval_T *argvars, typval_T *rettv);
673static void f_min(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000674#ifdef vim_mkdir
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100675static void f_mkdir(typval_T *argvars, typval_T *rettv);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000676#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100677static void f_mode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100678#ifdef FEAT_MZSCHEME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100679static void f_mzeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaar7e506b62010-01-19 15:55:06 +0100680#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100681static void f_nextnonblank(typval_T *argvars, typval_T *rettv);
682static void f_nr2char(typval_T *argvars, typval_T *rettv);
683static void f_or(typval_T *argvars, typval_T *rettv);
684static void f_pathshorten(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100685#ifdef FEAT_PERL
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100686static void f_perleval(typval_T *argvars, typval_T *rettv);
Bram Moolenaare9b892e2016-01-17 21:15:58 +0100687#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000688#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100689static void f_pow(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000690#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100691static void f_prevnonblank(typval_T *argvars, typval_T *rettv);
692static void f_printf(typval_T *argvars, typval_T *rettv);
693static void f_pumvisible(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200694#ifdef FEAT_PYTHON3
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100695static void f_py3eval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200696#endif
697#ifdef FEAT_PYTHON
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100698static void f_pyeval(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb913952012-06-29 12:54:53 +0200699#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100700static void f_range(typval_T *argvars, typval_T *rettv);
701static void f_readfile(typval_T *argvars, typval_T *rettv);
702static void f_reltime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar79c2c882016-02-07 21:19:28 +0100703#ifdef FEAT_FLOAT
704static void f_reltimefloat(typval_T *argvars, typval_T *rettv);
705#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100706static void f_reltimestr(typval_T *argvars, typval_T *rettv);
707static void f_remote_expr(typval_T *argvars, typval_T *rettv);
708static void f_remote_foreground(typval_T *argvars, typval_T *rettv);
709static void f_remote_peek(typval_T *argvars, typval_T *rettv);
710static void f_remote_read(typval_T *argvars, typval_T *rettv);
711static void f_remote_send(typval_T *argvars, typval_T *rettv);
712static void f_remove(typval_T *argvars, typval_T *rettv);
713static void f_rename(typval_T *argvars, typval_T *rettv);
714static void f_repeat(typval_T *argvars, typval_T *rettv);
715static void f_resolve(typval_T *argvars, typval_T *rettv);
716static void f_reverse(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000717#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100718static void f_round(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000719#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100720static void f_screenattr(typval_T *argvars, typval_T *rettv);
721static void f_screenchar(typval_T *argvars, typval_T *rettv);
722static void f_screencol(typval_T *argvars, typval_T *rettv);
723static void f_screenrow(typval_T *argvars, typval_T *rettv);
724static void f_search(typval_T *argvars, typval_T *rettv);
725static void f_searchdecl(typval_T *argvars, typval_T *rettv);
726static void f_searchpair(typval_T *argvars, typval_T *rettv);
727static void f_searchpairpos(typval_T *argvars, typval_T *rettv);
728static void f_searchpos(typval_T *argvars, typval_T *rettv);
729static void f_server2client(typval_T *argvars, typval_T *rettv);
730static void f_serverlist(typval_T *argvars, typval_T *rettv);
731static void f_setbufvar(typval_T *argvars, typval_T *rettv);
732static void f_setcharsearch(typval_T *argvars, typval_T *rettv);
733static void f_setcmdpos(typval_T *argvars, typval_T *rettv);
Bram Moolenaar80492532016-03-08 17:08:53 +0100734static void f_setfperm(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100735static void f_setline(typval_T *argvars, typval_T *rettv);
736static void f_setloclist(typval_T *argvars, typval_T *rettv);
737static void f_setmatches(typval_T *argvars, typval_T *rettv);
738static void f_setpos(typval_T *argvars, typval_T *rettv);
739static void f_setqflist(typval_T *argvars, typval_T *rettv);
740static void f_setreg(typval_T *argvars, typval_T *rettv);
741static void f_settabvar(typval_T *argvars, typval_T *rettv);
742static void f_settabwinvar(typval_T *argvars, typval_T *rettv);
743static void f_setwinvar(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100744#ifdef FEAT_CRYPT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100745static void f_sha256(typval_T *argvars, typval_T *rettv);
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +0100746#endif /* FEAT_CRYPT */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100747static void f_shellescape(typval_T *argvars, typval_T *rettv);
748static void f_shiftwidth(typval_T *argvars, typval_T *rettv);
749static void f_simplify(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000750#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100751static void f_sin(typval_T *argvars, typval_T *rettv);
752static void f_sinh(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000753#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100754static void f_sort(typval_T *argvars, typval_T *rettv);
755static void f_soundfold(typval_T *argvars, typval_T *rettv);
756static void f_spellbadword(typval_T *argvars, typval_T *rettv);
757static void f_spellsuggest(typval_T *argvars, typval_T *rettv);
758static void f_split(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000759#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100760static void f_sqrt(typval_T *argvars, typval_T *rettv);
761static void f_str2float(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000762#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100763static void f_str2nr(typval_T *argvars, typval_T *rettv);
764static void f_strchars(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000765#ifdef HAVE_STRFTIME
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100766static void f_strftime(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000767#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100768static void f_stridx(typval_T *argvars, typval_T *rettv);
769static void f_string(typval_T *argvars, typval_T *rettv);
770static void f_strlen(typval_T *argvars, typval_T *rettv);
771static void f_strpart(typval_T *argvars, typval_T *rettv);
772static void f_strridx(typval_T *argvars, typval_T *rettv);
773static void f_strtrans(typval_T *argvars, typval_T *rettv);
774static void f_strdisplaywidth(typval_T *argvars, typval_T *rettv);
775static void f_strwidth(typval_T *argvars, typval_T *rettv);
776static void f_submatch(typval_T *argvars, typval_T *rettv);
777static void f_substitute(typval_T *argvars, typval_T *rettv);
778static void f_synID(typval_T *argvars, typval_T *rettv);
779static void f_synIDattr(typval_T *argvars, typval_T *rettv);
780static void f_synIDtrans(typval_T *argvars, typval_T *rettv);
781static void f_synstack(typval_T *argvars, typval_T *rettv);
782static void f_synconcealed(typval_T *argvars, typval_T *rettv);
783static void f_system(typval_T *argvars, typval_T *rettv);
784static void f_systemlist(typval_T *argvars, typval_T *rettv);
785static void f_tabpagebuflist(typval_T *argvars, typval_T *rettv);
786static void f_tabpagenr(typval_T *argvars, typval_T *rettv);
787static void f_tabpagewinnr(typval_T *argvars, typval_T *rettv);
788static void f_taglist(typval_T *argvars, typval_T *rettv);
789static void f_tagfiles(typval_T *argvars, typval_T *rettv);
790static void f_tempname(typval_T *argvars, typval_T *rettv);
791static void f_test(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200792#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100793static void f_tan(typval_T *argvars, typval_T *rettv);
794static void f_tanh(typval_T *argvars, typval_T *rettv);
Bram Moolenaardb7c6862010-05-21 16:33:48 +0200795#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +0100796#ifdef FEAT_TIMERS
797static void f_timer_start(typval_T *argvars, typval_T *rettv);
798static void f_timer_stop(typval_T *argvars, typval_T *rettv);
799#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100800static void f_tolower(typval_T *argvars, typval_T *rettv);
801static void f_toupper(typval_T *argvars, typval_T *rettv);
802static void f_tr(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000803#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100804static void f_trunc(typval_T *argvars, typval_T *rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000805#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100806static void f_type(typval_T *argvars, typval_T *rettv);
807static void f_undofile(typval_T *argvars, typval_T *rettv);
808static void f_undotree(typval_T *argvars, typval_T *rettv);
809static void f_uniq(typval_T *argvars, typval_T *rettv);
810static void f_values(typval_T *argvars, typval_T *rettv);
811static void f_virtcol(typval_T *argvars, typval_T *rettv);
812static void f_visualmode(typval_T *argvars, typval_T *rettv);
813static void f_wildmenumode(typval_T *argvars, typval_T *rettv);
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +0100814static void f_win_findbuf(typval_T *argvars, typval_T *rettv);
Bram Moolenaar86edef62016-03-13 18:07:30 +0100815static void f_win_getid(typval_T *argvars, typval_T *rettv);
816static void f_win_gotoid(typval_T *argvars, typval_T *rettv);
817static void f_win_id2tabwin(typval_T *argvars, typval_T *rettv);
818static void f_win_id2win(typval_T *argvars, typval_T *rettv);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100819static void f_winbufnr(typval_T *argvars, typval_T *rettv);
820static void f_wincol(typval_T *argvars, typval_T *rettv);
821static void f_winheight(typval_T *argvars, typval_T *rettv);
822static void f_winline(typval_T *argvars, typval_T *rettv);
823static void f_winnr(typval_T *argvars, typval_T *rettv);
824static void f_winrestcmd(typval_T *argvars, typval_T *rettv);
825static void f_winrestview(typval_T *argvars, typval_T *rettv);
826static void f_winsaveview(typval_T *argvars, typval_T *rettv);
827static void f_winwidth(typval_T *argvars, typval_T *rettv);
828static void f_writefile(typval_T *argvars, typval_T *rettv);
829static void f_wordcount(typval_T *argvars, typval_T *rettv);
830static void f_xor(typval_T *argvars, typval_T *rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +0000831
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100832static int list2fpos(typval_T *arg, pos_T *posp, int *fnump, colnr_T *curswantp);
833static pos_T *var2fpos(typval_T *varp, int dollar_lnum, int *fnum);
834static int get_env_len(char_u **arg);
835static int get_id_len(char_u **arg);
836static int get_name_len(char_u **arg, char_u **alias, int evaluate, int verbose);
837static char_u *find_name_end(char_u *arg, char_u **expr_start, char_u **expr_end, int flags);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +0000838#define FNE_INCL_BR 1 /* find_name_end(): include [] in name */
839#define FNE_CHECK_START 2 /* find_name_end(): check name starts with
840 valid character */
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100841static char_u * make_expanded_name(char_u *in_start, char_u *expr_start, char_u *expr_end, char_u *in_end);
842static int eval_isnamec(int c);
843static int eval_isnamec1(int c);
844static int get_var_tv(char_u *name, int len, typval_T *rettv, dictitem_T **dip, int verbose, int no_autoload);
845static int handle_subscript(char_u **arg, typval_T *rettv, int evaluate, int verbose);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100846static typval_T *alloc_string_tv(char_u *string);
847static void init_tv(typval_T *varp);
Bram Moolenaarf7edf402016-01-19 23:36:15 +0100848#ifdef FEAT_FLOAT
849static float_T get_tv_float(typval_T *varp);
850#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100851static linenr_T get_tv_lnum(typval_T *argvars);
852static linenr_T get_tv_lnum_buf(typval_T *argvars, buf_T *buf);
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100853static dictitem_T *find_var(char_u *name, hashtab_T **htp, int no_autoload);
854static dictitem_T *find_var_in_ht(hashtab_T *ht, int htname, char_u *varname, int no_autoload);
855static hashtab_T *find_var_ht(char_u *name, char_u **varname);
856static funccall_T *get_funccal(void);
857static void vars_clear_ext(hashtab_T *ht, int free_val);
858static void delete_var(hashtab_T *ht, hashitem_T *hi);
859static void list_one_var(dictitem_T *v, char_u *prefix, int *first);
860static void list_one_var_a(char_u *prefix, char_u *name, int type, char_u *string, int *first);
861static void set_var(char_u *name, typval_T *varp, int copy);
862static int var_check_ro(int flags, char_u *name, int use_gettext);
863static int var_check_fixed(int flags, char_u *name, int use_gettext);
864static int var_check_func_name(char_u *name, int new_var);
865static int valid_varname(char_u *varname);
866static int tv_check_lock(int lock, char_u *name, int use_gettext);
867static int item_copy(typval_T *from, typval_T *to, int deep, int copyID);
868static char_u *find_option_end(char_u **arg, int *opt_flags);
Bram Moolenaar65639032016-03-16 21:40:30 +0100869static 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 +0100870static int eval_fname_script(char_u *p);
871static int eval_fname_sid(char_u *p);
872static void list_func_head(ufunc_T *fp, int indent);
873static ufunc_T *find_func(char_u *name);
874static int function_exists(char_u *name);
875static int builtin_function(char_u *name, int len);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000876#ifdef FEAT_PROFILE
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100877static void func_do_profile(ufunc_T *fp);
878static void prof_sort_list(FILE *fd, ufunc_T **sorttab, int st_len, char *title, int prefer_self);
879static void prof_func_line(FILE *fd, int count, proftime_T *total, proftime_T *self, int prefer_self);
Bram Moolenaar73830342005-02-28 22:48:19 +0000880static int
881# ifdef __BORLANDC__
882 _RTLENTRYF
883# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100884 prof_total_cmp(const void *s1, const void *s2);
Bram Moolenaar73830342005-02-28 22:48:19 +0000885static int
886# ifdef __BORLANDC__
887 _RTLENTRYF
888# endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100889 prof_self_cmp(const void *s1, const void *s2);
Bram Moolenaar05159a02005-02-26 23:04:13 +0000890#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100891static int script_autoload(char_u *name, int reload);
892static char_u *autoload_name(char_u *name);
893static void cat_func_name(char_u *buf, ufunc_T *fp);
894static void func_free(ufunc_T *fp);
895static void call_user_func(ufunc_T *fp, int argcount, typval_T *argvars, typval_T *rettv, linenr_T firstline, linenr_T lastline, dict_T *selfdict);
896static int can_free_funccal(funccall_T *fc, int copyID) ;
897static void free_funccal(funccall_T *fc, int free_val);
898static void add_nr_var(dict_T *dp, dictitem_T *v, char *name, varnumber_T nr);
899static win_T *find_win_by_nr(typval_T *vp, tabpage_T *tp);
900static win_T *find_tabwin(typval_T *wvp, typval_T *tvp);
901static void getwinvar(typval_T *argvars, typval_T *rettv, int off);
902static int searchpair_cmn(typval_T *argvars, pos_T *match_pos);
903static int search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp);
904static void setwinvar(typval_T *argvars, typval_T *rettv, int off);
905static int write_list(FILE *fd, list_T *list, int binary);
906static void get_cmd_output_as_rettv(typval_T *argvars, typval_T *rettv, int retlist);
Bram Moolenaar33570922005-01-25 22:26:29 +0000907
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200908
909#ifdef EBCDIC
Bram Moolenaar48e697e2016-01-23 22:17:30 +0100910static int compare_func_name(const void *s1, const void *s2);
911static void sortFunctions();
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200912#endif
913
Bram Moolenaar33570922005-01-25 22:26:29 +0000914/*
915 * Initialize the global and v: variables.
Bram Moolenaara7043832005-01-21 11:56:39 +0000916 */
917 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100918eval_init(void)
Bram Moolenaara7043832005-01-21 11:56:39 +0000919{
Bram Moolenaar33570922005-01-25 22:26:29 +0000920 int i;
921 struct vimvar *p;
922
Bram Moolenaarbdb62052012-07-16 17:31:53 +0200923 init_var_dict(&globvardict, &globvars_var, VAR_DEF_SCOPE);
924 init_var_dict(&vimvardict, &vimvars_var, VAR_SCOPE);
Bram Moolenaar32f649e2011-04-11 13:46:13 +0200925 vimvardict.dv_lock = VAR_FIXED;
Bram Moolenaar532c7802005-01-27 14:44:31 +0000926 hash_init(&compat_hashtab);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +0000927 hash_init(&func_hashtab);
Bram Moolenaar33570922005-01-25 22:26:29 +0000928
929 for (i = 0; i < VV_LEN; ++i)
930 {
931 p = &vimvars[i];
932 STRCPY(p->vv_di.di_key, p->vv_name);
933 if (p->vv_flags & VV_RO)
934 p->vv_di.di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
935 else if (p->vv_flags & VV_RO_SBX)
936 p->vv_di.di_flags = DI_FLAGS_RO_SBX | DI_FLAGS_FIX;
937 else
938 p->vv_di.di_flags = DI_FLAGS_FIX;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +0000939
940 /* add to v: scope dict, unless the value is not always available */
941 if (p->vv_type != VAR_UNKNOWN)
942 hash_add(&vimvarht, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000943 if (p->vv_flags & VV_COMPAT)
Bram Moolenaar532c7802005-01-27 14:44:31 +0000944 /* add to compat scope dict */
945 hash_add(&compat_hashtab, p->vv_di.di_key);
Bram Moolenaar33570922005-01-25 22:26:29 +0000946 }
Bram Moolenaara542c682016-01-31 16:28:04 +0100947 vimvars[VV_VERSION].vv_nr = VIM_VERSION_100;
948
Bram Moolenaar8c8de832008-06-24 22:58:06 +0000949 set_vim_var_nr(VV_SEARCHFORWARD, 1L);
Bram Moolenaar8050efa2013-11-08 04:30:20 +0100950 set_vim_var_nr(VV_HLSEARCH, 1L);
Bram Moolenaar42a45122015-07-10 17:56:23 +0200951 set_vim_var_dict(VV_COMPLETED_ITEM, dict_alloc());
Bram Moolenaar4649ded2015-12-03 14:55:55 +0100952 set_vim_var_list(VV_ERRORS, list_alloc());
Bram Moolenaar520e1e42016-01-23 19:46:28 +0100953
954 set_vim_var_nr(VV_FALSE, VVAL_FALSE);
955 set_vim_var_nr(VV_TRUE, VVAL_TRUE);
956 set_vim_var_nr(VV_NONE, VVAL_NONE);
957 set_vim_var_nr(VV_NULL, VVAL_NULL);
958
Bram Moolenaarb429cde2012-04-25 18:24:29 +0200959 set_reg_var(0); /* default for v:register is not 0 but '"' */
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200960
961#ifdef EBCDIC
962 /*
Bram Moolenaar195ea0f2011-11-30 14:57:31 +0100963 * Sort the function table, to enable binary search.
Bram Moolenaar2c704a72010-06-03 21:17:25 +0200964 */
965 sortFunctions();
966#endif
Bram Moolenaara7043832005-01-21 11:56:39 +0000967}
968
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000969#if defined(EXITFREE) || defined(PROTO)
970 void
Bram Moolenaar7454a062016-01-30 15:14:10 +0100971eval_clear(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000972{
973 int i;
974 struct vimvar *p;
975
976 for (i = 0; i < VV_LEN; ++i)
977 {
978 p = &vimvars[i];
979 if (p->vv_di.di_tv.v_type == VAR_STRING)
Bram Moolenaard9fba312005-06-26 22:34:35 +0000980 {
Bram Moolenaar12193212008-11-09 16:22:01 +0000981 vim_free(p->vv_str);
982 p->vv_str = NULL;
Bram Moolenaard812df62008-11-09 12:46:09 +0000983 }
984 else if (p->vv_di.di_tv.v_type == VAR_LIST)
985 {
986 list_unref(p->vv_list);
987 p->vv_list = NULL;
Bram Moolenaard9fba312005-06-26 22:34:35 +0000988 }
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000989 }
990 hash_clear(&vimvarht);
Bram Moolenaar0f71c6d2008-11-12 14:29:28 +0000991 hash_init(&vimvarht); /* garbage_collect() will access it */
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000992 hash_clear(&compat_hashtab);
993
Bram Moolenaard9fba312005-06-26 22:34:35 +0000994 free_scriptnames();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100995# if defined(FEAT_CMDL_COMPL)
Bram Moolenaar9b486ca2011-05-19 18:26:40 +0200996 free_locales();
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +0100997# endif
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +0000998
999 /* global variables */
1000 vars_clear(&globvarht);
Bram Moolenaard9fba312005-06-26 22:34:35 +00001001
Bram Moolenaaraa35dd12006-04-29 22:03:41 +00001002 /* autoloaded script names */
1003 ga_clear_strings(&ga_loaded);
1004
Bram Moolenaarcca74132013-09-25 21:00:28 +02001005 /* Script-local variables. First clear all the variables and in a second
1006 * loop free the scriptvar_T, because a variable in one script might hold
1007 * a reference to the whole scope of another script. */
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001008 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001009 vars_clear(&SCRIPT_VARS(i));
Bram Moolenaarcca74132013-09-25 21:00:28 +02001010 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001011 vim_free(SCRIPT_SV(i));
Bram Moolenaar9577c3e2010-05-14 12:16:25 +02001012 ga_clear(&ga_scripts);
1013
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00001014 /* unreferenced lists and dicts */
1015 (void)garbage_collect();
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001016
1017 /* functions */
1018 free_all_functions();
1019 hash_clear(&func_hashtab);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +00001020}
1021#endif
1022
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001023/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001024 * Return the name of the executed function.
1025 */
1026 char_u *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001027func_name(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001028{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001029 return ((funccall_T *)cookie)->func->uf_name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001030}
1031
1032/*
1033 * Return the address holding the next breakpoint line for a funccall cookie.
1034 */
1035 linenr_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001036func_breakpoint(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001037{
Bram Moolenaar33570922005-01-25 22:26:29 +00001038 return &((funccall_T *)cookie)->breakpoint;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001039}
1040
1041/*
1042 * Return the address holding the debug tick for a funccall cookie.
1043 */
1044 int *
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001045func_dbg_tick(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001046{
Bram Moolenaar33570922005-01-25 22:26:29 +00001047 return &((funccall_T *)cookie)->dbg_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001048}
1049
1050/*
1051 * Return the nesting level for a funccall cookie.
1052 */
1053 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001054func_level(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001055{
Bram Moolenaar33570922005-01-25 22:26:29 +00001056 return ((funccall_T *)cookie)->level;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057}
1058
1059/* pointer to funccal for currently active function */
Bram Moolenaar33570922005-01-25 22:26:29 +00001060funccall_T *current_funccal = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001061
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00001062/* pointer to list of previously used funccal, still around because some
1063 * item in it is still being used. */
1064funccall_T *previous_funccal = NULL;
1065
Bram Moolenaar071d4272004-06-13 20:20:40 +00001066/*
1067 * Return TRUE when a function was ended by a ":return" command.
1068 */
1069 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001070current_func_returned(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001071{
1072 return current_funccal->returned;
1073}
1074
1075
1076/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001077 * Set an internal variable to a string value. Creates the variable if it does
1078 * not already exist.
1079 */
1080 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001081set_internal_string_var(char_u *name, char_u *value)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001082{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001083 char_u *val;
Bram Moolenaar33570922005-01-25 22:26:29 +00001084 typval_T *tvp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001085
1086 val = vim_strsave(value);
1087 if (val != NULL)
1088 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001089 tvp = alloc_string_tv(val);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001090 if (tvp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001091 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001092 set_var(name, tvp, FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001093 free_tv(tvp);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001094 }
1095 }
1096}
1097
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001098static lval_T *redir_lval = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001099static garray_T redir_ga; /* only valid when redir_lval is not NULL */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001100static char_u *redir_endp = NULL;
1101static char_u *redir_varname = NULL;
1102
1103/*
1104 * Start recording command output to a variable
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001105 * When "append" is TRUE append to an existing variable.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001106 * Returns OK if successfully completed the setup. FAIL otherwise.
1107 */
1108 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001109var_redir_start(char_u *name, int append)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001110{
1111 int save_emsg;
1112 int err;
1113 typval_T tv;
1114
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001115 /* Catch a bad name early. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001116 if (!eval_isnamec1(*name))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001117 {
1118 EMSG(_(e_invarg));
1119 return FAIL;
1120 }
1121
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001122 /* Make a copy of the name, it is used in redir_lval until redir ends. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001123 redir_varname = vim_strsave(name);
1124 if (redir_varname == NULL)
1125 return FAIL;
1126
1127 redir_lval = (lval_T *)alloc_clear((unsigned)sizeof(lval_T));
1128 if (redir_lval == NULL)
1129 {
1130 var_redir_stop();
1131 return FAIL;
1132 }
1133
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001134 /* The output is stored in growarray "redir_ga" until redirection ends. */
1135 ga_init2(&redir_ga, (int)sizeof(char), 500);
1136
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001137 /* Parse the variable name (can be a dict or list entry). */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001138 redir_endp = get_lval(redir_varname, NULL, redir_lval, FALSE, FALSE, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00001139 FNE_CHECK_START);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001140 if (redir_endp == NULL || redir_lval->ll_name == NULL || *redir_endp != NUL)
1141 {
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001142 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001143 if (redir_endp != NULL && *redir_endp != NUL)
1144 /* Trailing characters are present after the variable name */
1145 EMSG(_(e_trailing));
1146 else
1147 EMSG(_(e_invarg));
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001148 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001149 var_redir_stop();
1150 return FAIL;
1151 }
1152
1153 /* check if we can write to the variable: set it to or append an empty
1154 * string */
1155 save_emsg = did_emsg;
1156 did_emsg = FALSE;
1157 tv.v_type = VAR_STRING;
1158 tv.vval.v_string = (char_u *)"";
1159 if (append)
1160 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)".");
1161 else
1162 set_var_lval(redir_lval, redir_endp, &tv, TRUE, (char_u *)"=");
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001163 clear_lval(redir_lval);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001164 err = did_emsg;
Bram Moolenaar1f35bf92006-03-07 22:38:47 +00001165 did_emsg |= save_emsg;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001166 if (err)
1167 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001168 redir_endp = NULL; /* don't store a value, only cleanup */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001169 var_redir_stop();
1170 return FAIL;
1171 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001172
1173 return OK;
1174}
1175
1176/*
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001177 * Append "value[value_len]" to the variable set by var_redir_start().
1178 * The actual appending is postponed until redirection ends, because the value
1179 * appended may in fact be the string we write to, changing it may cause freed
1180 * memory to be used:
1181 * :redir => foo
1182 * :let foo
1183 * :redir END
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001184 */
1185 void
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001186var_redir_str(char_u *value, int value_len)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001187{
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001188 int len;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001189
1190 if (redir_lval == NULL)
1191 return;
1192
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001193 if (value_len == -1)
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001194 len = (int)STRLEN(value); /* Append the entire string */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001195 else
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001196 len = value_len; /* Append only "value_len" characters */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001197
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001198 if (ga_grow(&redir_ga, len) == OK)
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001199 {
1200 mch_memmove((char *)redir_ga.ga_data + redir_ga.ga_len, value, len);
Bram Moolenaar5fdec472007-07-24 08:45:13 +00001201 redir_ga.ga_len += len;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001202 }
1203 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001204 var_redir_stop();
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001205}
1206
1207/*
1208 * Stop redirecting command output to a variable.
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001209 * Frees the allocated memory.
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001210 */
1211 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001212var_redir_stop(void)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001213{
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001214 typval_T tv;
1215
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001216 if (redir_lval != NULL)
1217 {
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001218 /* If there was no error: assign the text to the variable. */
1219 if (redir_endp != NULL)
1220 {
1221 ga_append(&redir_ga, NUL); /* Append the trailing NUL. */
1222 tv.v_type = VAR_STRING;
1223 tv.vval.v_string = redir_ga.ga_data;
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001224 /* Call get_lval() again, if it's inside a Dict or List it may
1225 * have changed. */
1226 redir_endp = get_lval(redir_varname, NULL, redir_lval,
Bram Moolenaar6d977d62014-01-14 15:24:39 +01001227 FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar1dba0fb2010-07-28 18:55:02 +02001228 if (redir_endp != NULL && redir_lval->ll_name != NULL)
1229 set_var_lval(redir_lval, redir_endp, &tv, FALSE, (char_u *)".");
1230 clear_lval(redir_lval);
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001231 }
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001232
Bram Moolenaar2f59b5c2009-11-03 13:26:55 +00001233 /* free the collected output */
1234 vim_free(redir_ga.ga_data);
1235 redir_ga.ga_data = NULL;
Bram Moolenaar863b53b2007-01-14 14:28:34 +00001236
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00001237 vim_free(redir_lval);
1238 redir_lval = NULL;
1239 }
1240 vim_free(redir_varname);
1241 redir_varname = NULL;
1242}
1243
Bram Moolenaar071d4272004-06-13 20:20:40 +00001244# if defined(FEAT_MBYTE) || defined(PROTO)
1245 int
Bram Moolenaarb7604cc2016-01-15 21:23:22 +01001246eval_charconvert(
1247 char_u *enc_from,
1248 char_u *enc_to,
1249 char_u *fname_from,
1250 char_u *fname_to)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001251{
1252 int err = FALSE;
1253
1254 set_vim_var_string(VV_CC_FROM, enc_from, -1);
1255 set_vim_var_string(VV_CC_TO, enc_to, -1);
1256 set_vim_var_string(VV_FNAME_IN, fname_from, -1);
1257 set_vim_var_string(VV_FNAME_OUT, fname_to, -1);
1258 if (eval_to_bool(p_ccv, &err, NULL, FALSE))
1259 err = TRUE;
1260 set_vim_var_string(VV_CC_FROM, NULL, -1);
1261 set_vim_var_string(VV_CC_TO, NULL, -1);
1262 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1263 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1264
1265 if (err)
1266 return FAIL;
1267 return OK;
1268}
1269# endif
1270
1271# if defined(FEAT_POSTSCRIPT) || defined(PROTO)
1272 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001273eval_printexpr(char_u *fname, char_u *args)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001274{
1275 int err = FALSE;
1276
1277 set_vim_var_string(VV_FNAME_IN, fname, -1);
1278 set_vim_var_string(VV_CMDARG, args, -1);
1279 if (eval_to_bool(p_pexpr, &err, NULL, FALSE))
1280 err = TRUE;
1281 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1282 set_vim_var_string(VV_CMDARG, NULL, -1);
1283
1284 if (err)
1285 {
1286 mch_remove(fname);
1287 return FAIL;
1288 }
1289 return OK;
1290}
1291# endif
1292
1293# if defined(FEAT_DIFF) || defined(PROTO)
1294 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001295eval_diff(
1296 char_u *origfile,
1297 char_u *newfile,
1298 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001299{
1300 int err = FALSE;
1301
1302 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1303 set_vim_var_string(VV_FNAME_NEW, newfile, -1);
1304 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1305 (void)eval_to_bool(p_dex, &err, NULL, FALSE);
1306 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1307 set_vim_var_string(VV_FNAME_NEW, NULL, -1);
1308 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1309}
1310
1311 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001312eval_patch(
1313 char_u *origfile,
1314 char_u *difffile,
1315 char_u *outfile)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001316{
1317 int err;
1318
1319 set_vim_var_string(VV_FNAME_IN, origfile, -1);
1320 set_vim_var_string(VV_FNAME_DIFF, difffile, -1);
1321 set_vim_var_string(VV_FNAME_OUT, outfile, -1);
1322 (void)eval_to_bool(p_pex, &err, NULL, FALSE);
1323 set_vim_var_string(VV_FNAME_IN, NULL, -1);
1324 set_vim_var_string(VV_FNAME_DIFF, NULL, -1);
1325 set_vim_var_string(VV_FNAME_OUT, NULL, -1);
1326}
1327# endif
1328
1329/*
1330 * Top level evaluation function, returning a boolean.
1331 * Sets "error" to TRUE if there was an error.
1332 * Return TRUE or FALSE.
1333 */
1334 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001335eval_to_bool(
1336 char_u *arg,
1337 int *error,
1338 char_u **nextcmd,
1339 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001340{
Bram Moolenaar33570922005-01-25 22:26:29 +00001341 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001342 int retval = FALSE;
1343
1344 if (skip)
1345 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001346 if (eval0(arg, &tv, nextcmd, !skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001347 *error = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001348 else
1349 {
1350 *error = FALSE;
1351 if (!skip)
1352 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001353 retval = (get_tv_number_chk(&tv, error) != 0);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001354 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001355 }
1356 }
1357 if (skip)
1358 --emsg_skip;
1359
1360 return retval;
1361}
1362
1363/*
1364 * Top level evaluation function, returning a string. If "skip" is TRUE,
1365 * only parsing to "nextcmd" is done, without reporting errors. Return
1366 * pointer to allocated memory, or NULL for failure or when "skip" is TRUE.
1367 */
1368 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001369eval_to_string_skip(
1370 char_u *arg,
1371 char_u **nextcmd,
1372 int skip) /* only parse, don't execute */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001373{
Bram Moolenaar33570922005-01-25 22:26:29 +00001374 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001375 char_u *retval;
1376
1377 if (skip)
1378 ++emsg_skip;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001379 if (eval0(arg, &tv, nextcmd, !skip) == FAIL || skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001380 retval = NULL;
1381 else
1382 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001383 retval = vim_strsave(get_tv_string(&tv));
1384 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001385 }
1386 if (skip)
1387 --emsg_skip;
1388
1389 return retval;
1390}
1391
1392/*
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001393 * Skip over an expression at "*pp".
1394 * Return FAIL for an error, OK otherwise.
1395 */
1396 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001397skip_expr(char_u **pp)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001398{
Bram Moolenaar33570922005-01-25 22:26:29 +00001399 typval_T rettv;
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001400
1401 *pp = skipwhite(*pp);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001402 return eval1(pp, &rettv, FALSE);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00001403}
1404
1405/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00001406 * Top level evaluation function, returning a string.
Bram Moolenaara85fb752008-09-07 11:55:43 +00001407 * When "convert" is TRUE convert a List into a sequence of lines and convert
1408 * a Float to a String.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001409 * Return pointer to allocated memory, or NULL for failure.
1410 */
1411 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001412eval_to_string(
1413 char_u *arg,
1414 char_u **nextcmd,
1415 int convert)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001416{
Bram Moolenaar33570922005-01-25 22:26:29 +00001417 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001418 char_u *retval;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001419 garray_T ga;
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001420#ifdef FEAT_FLOAT
Bram Moolenaara85fb752008-09-07 11:55:43 +00001421 char_u numbuf[NUMBUFLEN];
Bram Moolenaar798b30b2009-04-22 10:56:16 +00001422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001423
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001424 if (eval0(arg, &tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001425 retval = NULL;
1426 else
1427 {
Bram Moolenaara85fb752008-09-07 11:55:43 +00001428 if (convert && tv.v_type == VAR_LIST)
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001429 {
1430 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001431 if (tv.vval.v_list != NULL)
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001432 {
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001433 list_join(&ga, tv.vval.v_list, (char_u *)"\n", TRUE, 0);
Bram Moolenaar213b10a2011-08-10 12:38:08 +02001434 if (tv.vval.v_list->lv_len > 0)
1435 ga_append(&ga, NL);
1436 }
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001437 ga_append(&ga, NUL);
1438 retval = (char_u *)ga.ga_data;
1439 }
Bram Moolenaara85fb752008-09-07 11:55:43 +00001440#ifdef FEAT_FLOAT
1441 else if (convert && tv.v_type == VAR_FLOAT)
1442 {
1443 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv.vval.v_float);
1444 retval = vim_strsave(numbuf);
1445 }
1446#endif
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001447 else
1448 retval = vim_strsave(get_tv_string(&tv));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001449 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001450 }
1451
1452 return retval;
1453}
1454
1455/*
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001456 * Call eval_to_string() without using current local variables and using
1457 * textlock. When "use_sandbox" is TRUE use the sandbox.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001458 */
1459 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001460eval_to_string_safe(
1461 char_u *arg,
1462 char_u **nextcmd,
1463 int use_sandbox)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464{
1465 char_u *retval;
1466 void *save_funccalp;
1467
1468 save_funccalp = save_funccal();
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001469 if (use_sandbox)
1470 ++sandbox;
1471 ++textlock;
Bram Moolenaar362e1a32006-03-06 23:29:24 +00001472 retval = eval_to_string(arg, nextcmd, FALSE);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001473 if (use_sandbox)
1474 --sandbox;
1475 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001476 restore_funccal(save_funccalp);
1477 return retval;
1478}
1479
Bram Moolenaar071d4272004-06-13 20:20:40 +00001480/*
1481 * Top level evaluation function, returning a number.
1482 * Evaluates "expr" silently.
1483 * Returns -1 for an error.
1484 */
1485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001486eval_to_number(char_u *expr)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001487{
Bram Moolenaar33570922005-01-25 22:26:29 +00001488 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001489 int retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00001490 char_u *p = skipwhite(expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001491
1492 ++emsg_off;
1493
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001494 if (eval1(&p, &rettv, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001495 retval = -1;
1496 else
1497 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00001498 retval = get_tv_number_chk(&rettv, NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001499 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001500 }
1501 --emsg_off;
1502
1503 return retval;
1504}
1505
Bram Moolenaara40058a2005-07-11 22:42:07 +00001506/*
1507 * Prepare v: variable "idx" to be used.
1508 * Save the current typeval in "save_tv".
1509 * When not used yet add the variable to the v: hashtable.
1510 */
1511 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001512prepare_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001513{
1514 *save_tv = vimvars[idx].vv_tv;
1515 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1516 hash_add(&vimvarht, vimvars[idx].vv_di.di_key);
1517}
1518
1519/*
1520 * Restore v: variable "idx" to typeval "save_tv".
1521 * When no longer defined, remove the variable from the v: hashtable.
1522 */
1523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001524restore_vimvar(int idx, typval_T *save_tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00001525{
1526 hashitem_T *hi;
1527
Bram Moolenaara40058a2005-07-11 22:42:07 +00001528 vimvars[idx].vv_tv = *save_tv;
1529 if (vimvars[idx].vv_type == VAR_UNKNOWN)
1530 {
1531 hi = hash_find(&vimvarht, vimvars[idx].vv_di.di_key);
1532 if (HASHITEM_EMPTY(hi))
1533 EMSG2(_(e_intern2), "restore_vimvar()");
1534 else
1535 hash_remove(&vimvarht, hi);
1536 }
1537}
1538
Bram Moolenaar3c56a962006-03-12 22:19:04 +00001539#if defined(FEAT_SPELL) || defined(PROTO)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001540/*
1541 * Evaluate an expression to a list with suggestions.
1542 * For the "expr:" part of 'spellsuggest'.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001543 * Returns NULL when there is an error.
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001544 */
1545 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001546eval_spell_expr(char_u *badword, char_u *expr)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001547{
1548 typval_T save_val;
1549 typval_T rettv;
1550 list_T *list = NULL;
1551 char_u *p = skipwhite(expr);
1552
1553 /* Set "v:val" to the bad word. */
1554 prepare_vimvar(VV_VAL, &save_val);
1555 vimvars[VV_VAL].vv_type = VAR_STRING;
1556 vimvars[VV_VAL].vv_str = badword;
1557 if (p_verbose == 0)
1558 ++emsg_off;
1559
1560 if (eval1(&p, &rettv, TRUE) == OK)
1561 {
1562 if (rettv.v_type != VAR_LIST)
1563 clear_tv(&rettv);
1564 else
1565 list = rettv.vval.v_list;
1566 }
1567
1568 if (p_verbose == 0)
1569 --emsg_off;
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001570 restore_vimvar(VV_VAL, &save_val);
1571
1572 return list;
1573}
1574
1575/*
1576 * "list" is supposed to contain two items: a word and a number. Return the
1577 * word in "pp" and the number as the return value.
1578 * Return -1 if anything isn't right.
1579 * Used to get the good word and score from the eval_spell_expr() result.
1580 */
1581 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001582get_spellword(list_T *list, char_u **pp)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00001583{
1584 listitem_T *li;
1585
1586 li = list->lv_first;
1587 if (li == NULL)
1588 return -1;
1589 *pp = get_tv_string(&li->li_tv);
1590
1591 li = li->li_next;
1592 if (li == NULL)
1593 return -1;
1594 return get_tv_number(&li->li_tv);
1595}
1596#endif
1597
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001598/*
Bram Moolenaar4770d092006-01-12 23:22:24 +00001599 * Top level evaluation function.
1600 * Returns an allocated typval_T with the result.
1601 * Returns NULL when there is an error.
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001602 */
1603 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001604eval_expr(char_u *arg, char_u **nextcmd)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001605{
1606 typval_T *tv;
1607
1608 tv = (typval_T *)alloc(sizeof(typval_T));
Bram Moolenaar4770d092006-01-12 23:22:24 +00001609 if (tv != NULL && eval0(arg, tv, nextcmd, TRUE) == FAIL)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001610 {
1611 vim_free(tv);
Bram Moolenaar4770d092006-01-12 23:22:24 +00001612 tv = NULL;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +00001613 }
1614
1615 return tv;
1616}
1617
1618
Bram Moolenaar071d4272004-06-13 20:20:40 +00001619/*
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001620 * Call some vimL function and return the result in "*rettv".
Bram Moolenaar8c8de832008-06-24 22:58:06 +00001621 * Uses argv[argc] for the function arguments. Only Number and String
1622 * arguments are currently supported.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001623 * Returns OK or FAIL.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001624 */
Bram Moolenaar82139082011-09-14 16:52:09 +02001625 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001626call_vim_function(
1627 char_u *func,
1628 int argc,
1629 char_u **argv,
1630 int safe, /* use the sandbox */
1631 int str_arg_only, /* all arguments are strings */
1632 typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001633{
Bram Moolenaar33570922005-01-25 22:26:29 +00001634 typval_T *argvars;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001635 long n;
1636 int len;
1637 int i;
1638 int doesrange;
1639 void *save_funccalp = NULL;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001640 int ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001641
Bram Moolenaareb3593b2006-04-22 22:33:57 +00001642 argvars = (typval_T *)alloc((unsigned)((argc + 1) * sizeof(typval_T)));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001643 if (argvars == NULL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001644 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001645
1646 for (i = 0; i < argc; i++)
1647 {
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001648 /* Pass a NULL or empty argument as an empty string */
1649 if (argv[i] == NULL || *argv[i] == NUL)
1650 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001651 argvars[i].v_type = VAR_STRING;
1652 argvars[i].vval.v_string = (char_u *)"";
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00001653 continue;
1654 }
1655
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001656 if (str_arg_only)
1657 len = 0;
1658 else
1659 /* Recognize a number argument, the others must be strings. */
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01001660 vim_str2nr(argv[i], NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001661 if (len != 0 && len == (int)STRLEN(argv[i]))
1662 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001663 argvars[i].v_type = VAR_NUMBER;
1664 argvars[i].vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001665 }
1666 else
1667 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00001668 argvars[i].v_type = VAR_STRING;
1669 argvars[i].vval.v_string = argv[i];
Bram Moolenaar071d4272004-06-13 20:20:40 +00001670 }
1671 }
1672
1673 if (safe)
1674 {
1675 save_funccalp = save_funccal();
1676 ++sandbox;
1677 }
1678
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001679 rettv->v_type = VAR_UNKNOWN; /* clear_tv() uses this */
1680 ret = call_func(func, (int)STRLEN(func), rettv, argc, argvars,
Bram Moolenaar071d4272004-06-13 20:20:40 +00001681 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01001682 &doesrange, TRUE, NULL, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001683 if (safe)
1684 {
1685 --sandbox;
1686 restore_funccal(save_funccalp);
1687 }
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001688 vim_free(argvars);
1689
1690 if (ret == FAIL)
1691 clear_tv(rettv);
1692
1693 return ret;
1694}
1695
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001696/*
1697 * Call vimL function "func" and return the result as a number.
1698 * Returns -1 when calling the function fails.
1699 * Uses argv[argc] for the function arguments.
1700 */
1701 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01001702call_func_retnr(
1703 char_u *func,
1704 int argc,
1705 char_u **argv,
1706 int safe) /* use the sandbox */
Bram Moolenaarb2c5a5a2013-02-14 22:11:39 +01001707{
1708 typval_T rettv;
1709 long retval;
1710
1711 /* All arguments are passed as strings, no conversion to number. */
1712 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
1713 return -1;
1714
1715 retval = get_tv_number_chk(&rettv, NULL);
1716 clear_tv(&rettv);
1717 return retval;
1718}
1719
1720#if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) \
1721 || defined(FEAT_COMPL_FUNC) || defined(PROTO)
1722
Bram Moolenaar4f688582007-07-24 12:34:30 +00001723# if (defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL)) || defined(PROTO)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001724/*
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001725 * Call vimL function "func" and return the result as a string.
1726 * Returns NULL when calling the function fails.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001727 * Uses argv[argc] for the function arguments.
1728 */
1729 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001730call_func_retstr(
1731 char_u *func,
1732 int argc,
1733 char_u **argv,
1734 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001735{
1736 typval_T rettv;
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001737 char_u *retval;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001738
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001739 /* All arguments are passed as strings, no conversion to number. */
1740 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001741 return NULL;
1742
1743 retval = vim_strsave(get_tv_string(&rettv));
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001744 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001745 return retval;
1746}
Bram Moolenaar4f688582007-07-24 12:34:30 +00001747# endif
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001748
Bram Moolenaar25ceb222005-07-30 22:45:36 +00001749/*
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001750 * Call vimL function "func" and return the result as a List.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001751 * Uses argv[argc] for the function arguments.
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00001752 * Returns NULL when there is something wrong.
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001753 */
1754 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001755call_func_retlist(
1756 char_u *func,
1757 int argc,
1758 char_u **argv,
1759 int safe) /* use the sandbox */
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001760{
1761 typval_T rettv;
1762
Bram Moolenaar0cbba942012-07-25 16:47:03 +02001763 /* All arguments are passed as strings, no conversion to number. */
1764 if (call_vim_function(func, argc, argv, safe, TRUE, &rettv) == FAIL)
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00001765 return NULL;
1766
1767 if (rettv.v_type != VAR_LIST)
1768 {
1769 clear_tv(&rettv);
1770 return NULL;
1771 }
1772
1773 return rettv.vval.v_list;
1774}
Bram Moolenaar071d4272004-06-13 20:20:40 +00001775#endif
1776
1777/*
1778 * Save the current function call pointer, and set it to NULL.
1779 * Used when executing autocommands and for ":source".
1780 */
1781 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01001782save_funccal(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001783{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001784 funccall_T *fc = current_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001785
Bram Moolenaar071d4272004-06-13 20:20:40 +00001786 current_funccal = NULL;
1787 return (void *)fc;
1788}
1789
1790 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001791restore_funccal(void *vfc)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001792{
Bram Moolenaar05159a02005-02-26 23:04:13 +00001793 funccall_T *fc = (funccall_T *)vfc;
1794
1795 current_funccal = fc;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001796}
1797
Bram Moolenaar05159a02005-02-26 23:04:13 +00001798#if defined(FEAT_PROFILE) || defined(PROTO)
1799/*
1800 * Prepare profiling for entering a child or something else that is not
1801 * counted for the script/function itself.
1802 * Should always be called in pair with prof_child_exit().
1803 */
1804 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001805prof_child_enter(
1806 proftime_T *tm) /* place to store waittime */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001807{
1808 funccall_T *fc = current_funccal;
1809
1810 if (fc != NULL && fc->func->uf_profiling)
1811 profile_start(&fc->prof_child);
1812 script_prof_save(tm);
1813}
1814
1815/*
1816 * Take care of time spent in a child.
1817 * Should always be called after prof_child_enter().
1818 */
1819 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001820prof_child_exit(
1821 proftime_T *tm) /* where waittime was stored */
Bram Moolenaar05159a02005-02-26 23:04:13 +00001822{
1823 funccall_T *fc = current_funccal;
1824
1825 if (fc != NULL && fc->func->uf_profiling)
1826 {
1827 profile_end(&fc->prof_child);
1828 profile_sub_wait(tm, &fc->prof_child); /* don't count waiting time */
1829 profile_add(&fc->func->uf_tm_children, &fc->prof_child);
1830 profile_add(&fc->func->uf_tml_children, &fc->prof_child);
1831 }
1832 script_prof_restore(tm);
1833}
1834#endif
1835
1836
Bram Moolenaar071d4272004-06-13 20:20:40 +00001837#ifdef FEAT_FOLDING
1838/*
1839 * Evaluate 'foldexpr'. Returns the foldlevel, and any character preceding
1840 * it in "*cp". Doesn't give error messages.
1841 */
1842 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001843eval_foldexpr(char_u *arg, int *cp)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001844{
Bram Moolenaar33570922005-01-25 22:26:29 +00001845 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001846 int retval;
1847 char_u *s;
Bram Moolenaard1f56e62006-02-22 21:25:37 +00001848 int use_sandbox = was_set_insecurely((char_u *)"foldexpr",
1849 OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001850
1851 ++emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001852 if (use_sandbox)
1853 ++sandbox;
1854 ++textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001855 *cp = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001856 if (eval0(arg, &tv, NULL, TRUE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001857 retval = 0;
1858 else
1859 {
1860 /* If the result is a number, just return the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001861 if (tv.v_type == VAR_NUMBER)
1862 retval = tv.vval.v_number;
Bram Moolenaar758711c2005-02-02 23:11:38 +00001863 else if (tv.v_type != VAR_STRING || tv.vval.v_string == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001864 retval = 0;
1865 else
1866 {
1867 /* If the result is a string, check if there is a non-digit before
1868 * the number. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001869 s = tv.vval.v_string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001870 if (!VIM_ISDIGIT(*s) && *s != '-')
1871 *cp = *s++;
1872 retval = atol((char *)s);
1873 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001874 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001875 }
1876 --emsg_off;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00001877 if (use_sandbox)
1878 --sandbox;
1879 --textlock;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001880
1881 return retval;
1882}
1883#endif
1884
Bram Moolenaar071d4272004-06-13 20:20:40 +00001885/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001886 * ":let" list all variable values
1887 * ":let var1 var2" list variable values
1888 * ":let var = expr" assignment command.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001889 * ":let var += expr" assignment command.
1890 * ":let var -= expr" assignment command.
1891 * ":let var .= expr" assignment command.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001892 * ":let [var1, var2] = expr" unpack list.
Bram Moolenaar071d4272004-06-13 20:20:40 +00001893 */
1894 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01001895ex_let(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001896{
1897 char_u *arg = eap->arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001898 char_u *expr = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00001899 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 int i;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001901 int var_count = 0;
1902 int semicolon = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001903 char_u op[2];
Bram Moolenaardb552d602006-03-23 22:59:57 +00001904 char_u *argend;
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001905 int first = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00001906
Bram Moolenaardb552d602006-03-23 22:59:57 +00001907 argend = skip_var_list(arg, &var_count, &semicolon);
1908 if (argend == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001909 return;
Bram Moolenaar76b92b22006-03-24 22:46:53 +00001910 if (argend > arg && argend[-1] == '.') /* for var.='str' */
1911 --argend;
Bram Moolenaara3920382014-03-30 16:49:09 +02001912 expr = skipwhite(argend);
1913 if (*expr != '=' && !(vim_strchr((char_u *)"+-.", *expr) != NULL
1914 && expr[1] == '='))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00001916 /*
1917 * ":let" without "=": list variables
1918 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001919 if (*arg == '[')
1920 EMSG(_(e_invarg));
1921 else if (!ends_excmd(*arg))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001922 /* ":let var1 var2" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001923 arg = list_arg_vars(eap, arg, &first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001924 else if (!eap->skip)
Bram Moolenaara7043832005-01-21 11:56:39 +00001925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001926 /* ":let" */
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001927 list_glob_vars(&first);
1928 list_buf_vars(&first);
1929 list_win_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001930#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001931 list_tab_vars(&first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00001932#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00001933 list_script_vars(&first);
1934 list_func_vars(&first);
1935 list_vim_vars(&first);
Bram Moolenaara7043832005-01-21 11:56:39 +00001936 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00001937 eap->nextcmd = check_nextcmd(arg);
1938 }
1939 else
1940 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001941 op[0] = '=';
1942 op[1] = NUL;
Bram Moolenaara3920382014-03-30 16:49:09 +02001943 if (*expr != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001944 {
Bram Moolenaara3920382014-03-30 16:49:09 +02001945 if (vim_strchr((char_u *)"+-.", *expr) != NULL)
1946 op[0] = *expr; /* +=, -= or .= */
1947 expr = skipwhite(expr + 2);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001948 }
Bram Moolenaara3920382014-03-30 16:49:09 +02001949 else
1950 expr = skipwhite(expr + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001951
Bram Moolenaar071d4272004-06-13 20:20:40 +00001952 if (eap->skip)
1953 ++emsg_skip;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001954 i = eval0(expr, &rettv, &eap->nextcmd, !eap->skip);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001955 if (eap->skip)
1956 {
1957 if (i != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001958 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001959 --emsg_skip;
1960 }
1961 else if (i != FAIL)
1962 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001963 (void)ex_let_vars(eap->arg, &rettv, FALSE, semicolon, var_count,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001964 op);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00001965 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001966 }
1967 }
1968}
1969
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001970/*
1971 * Assign the typevalue "tv" to the variable or variables at "arg_start".
1972 * Handles both "var" with any type and "[var, var; var]" with a list type.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001973 * When "nextchars" is not NULL it points to a string with characters that
1974 * must appear after the variable(s). Use "+", "-" or "." for add, subtract
1975 * or concatenate.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001976 * Returns OK or FAIL;
1977 */
1978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01001979ex_let_vars(
1980 char_u *arg_start,
1981 typval_T *tv,
1982 int copy, /* copy values from "tv", don't move */
1983 int semicolon, /* from skip_var_list() */
1984 int var_count, /* from skip_var_list() */
1985 char_u *nextchars)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001986{
1987 char_u *arg = arg_start;
Bram Moolenaar33570922005-01-25 22:26:29 +00001988 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001989 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +00001990 listitem_T *item;
1991 typval_T ltv;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001992
1993 if (*arg != '[')
1994 {
1995 /*
1996 * ":let var = expr" or ":for var in list"
1997 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00001998 if (ex_let_one(arg, tv, copy, nextchars, nextchars) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00001999 return FAIL;
2000 return OK;
2001 }
2002
2003 /*
2004 * ":let [v1, v2] = list" or ":for [v1, v2] in listlist"
2005 */
Bram Moolenaar758711c2005-02-02 23:11:38 +00002006 if (tv->v_type != VAR_LIST || (l = tv->vval.v_list) == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002007 {
2008 EMSG(_(e_listreq));
2009 return FAIL;
2010 }
2011
2012 i = list_len(l);
2013 if (semicolon == 0 && var_count < i)
2014 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002015 EMSG(_("E687: Less targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002016 return FAIL;
2017 }
2018 if (var_count - semicolon > i)
2019 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00002020 EMSG(_("E688: More targets than List items"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002021 return FAIL;
2022 }
2023
2024 item = l->lv_first;
2025 while (*arg != ']')
2026 {
2027 arg = skipwhite(arg + 1);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002028 arg = ex_let_one(arg, &item->li_tv, TRUE, (char_u *)",;]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002029 item = item->li_next;
2030 if (arg == NULL)
2031 return FAIL;
2032
2033 arg = skipwhite(arg);
2034 if (*arg == ';')
2035 {
2036 /* Put the rest of the list (may be empty) in the var after ';'.
2037 * Create a new list for this. */
2038 l = list_alloc();
2039 if (l == NULL)
2040 return FAIL;
2041 while (item != NULL)
2042 {
2043 list_append_tv(l, &item->li_tv);
2044 item = item->li_next;
2045 }
2046
2047 ltv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002048 ltv.v_lock = 0;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002049 ltv.vval.v_list = l;
2050 l->lv_refcount = 1;
2051
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002052 arg = ex_let_one(skipwhite(arg + 1), &ltv, FALSE,
2053 (char_u *)"]", nextchars);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002054 clear_tv(&ltv);
2055 if (arg == NULL)
2056 return FAIL;
2057 break;
2058 }
2059 else if (*arg != ',' && *arg != ']')
2060 {
2061 EMSG2(_(e_intern2), "ex_let_vars()");
2062 return FAIL;
2063 }
2064 }
2065
2066 return OK;
2067}
2068
2069/*
2070 * Skip over assignable variable "var" or list of variables "[var, var]".
2071 * Used for ":let varvar = expr" and ":for varvar in expr".
2072 * For "[var, var]" increment "*var_count" for each variable.
2073 * for "[var, var; var]" set "semicolon".
2074 * Return NULL for an error.
2075 */
2076 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002077skip_var_list(
2078 char_u *arg,
2079 int *var_count,
2080 int *semicolon)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002081{
2082 char_u *p, *s;
2083
2084 if (*arg == '[')
2085 {
2086 /* "[var, var]": find the matching ']'. */
2087 p = arg;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00002088 for (;;)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002089 {
2090 p = skipwhite(p + 1); /* skip whites after '[', ';' or ',' */
2091 s = skip_var_one(p);
2092 if (s == p)
2093 {
2094 EMSG2(_(e_invarg2), p);
2095 return NULL;
2096 }
2097 ++*var_count;
2098
2099 p = skipwhite(s);
2100 if (*p == ']')
2101 break;
2102 else if (*p == ';')
2103 {
2104 if (*semicolon == 1)
2105 {
2106 EMSG(_("Double ; in list of variables"));
2107 return NULL;
2108 }
2109 *semicolon = 1;
2110 }
2111 else if (*p != ',')
2112 {
2113 EMSG2(_(e_invarg2), p);
2114 return NULL;
2115 }
2116 }
2117 return p + 1;
2118 }
2119 else
2120 return skip_var_one(arg);
2121}
2122
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002123/*
Bram Moolenaarbae0c162007-05-10 19:30:25 +00002124 * Skip one (assignable) variable name, including @r, $VAR, &option, d.key,
Bram Moolenaar92124a32005-06-17 22:03:40 +00002125 * l[idx].
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002126 */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002127 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002128skip_var_one(char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002129{
Bram Moolenaar92124a32005-06-17 22:03:40 +00002130 if (*arg == '@' && arg[1] != NUL)
2131 return arg + 2;
2132 return find_name_end(*arg == '$' || *arg == '&' ? arg + 1 : arg,
2133 NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002134}
2135
Bram Moolenaara7043832005-01-21 11:56:39 +00002136/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002137 * List variables for hashtab "ht" with prefix "prefix".
2138 * If "empty" is TRUE also list NULL strings as empty strings.
Bram Moolenaara7043832005-01-21 11:56:39 +00002139 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002140 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002141list_hashtable_vars(
2142 hashtab_T *ht,
2143 char_u *prefix,
2144 int empty,
2145 int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002146{
Bram Moolenaar33570922005-01-25 22:26:29 +00002147 hashitem_T *hi;
2148 dictitem_T *di;
Bram Moolenaara7043832005-01-21 11:56:39 +00002149 int todo;
2150
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002151 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +00002152 for (hi = ht->ht_array; todo > 0 && !got_int; ++hi)
2153 {
2154 if (!HASHITEM_EMPTY(hi))
2155 {
2156 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00002157 di = HI2DI(hi);
2158 if (empty || di->di_tv.v_type != VAR_STRING
2159 || di->di_tv.vval.v_string != NULL)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002160 list_one_var(di, prefix, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002161 }
2162 }
2163}
2164
2165/*
2166 * List global variables.
2167 */
2168 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002169list_glob_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002170{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002171 list_hashtable_vars(&globvarht, (char_u *)"", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002172}
2173
2174/*
2175 * List buffer variables.
2176 */
2177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002178list_buf_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002179{
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002180 char_u numbuf[NUMBUFLEN];
2181
Bram Moolenaar429fa852013-04-15 12:27:36 +02002182 list_hashtable_vars(&curbuf->b_vars->dv_hashtab, (char_u *)"b:",
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002183 TRUE, first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002184
2185 sprintf((char *)numbuf, "%ld", (long)curbuf->b_changedtick);
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002186 list_one_var_a((char_u *)"b:", (char_u *)"changedtick", VAR_NUMBER,
2187 numbuf, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002188}
2189
2190/*
2191 * List window variables.
2192 */
2193 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002194list_win_vars(int *first)
Bram Moolenaara7043832005-01-21 11:56:39 +00002195{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002196 list_hashtable_vars(&curwin->w_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002197 (char_u *)"w:", TRUE, first);
Bram Moolenaara7043832005-01-21 11:56:39 +00002198}
2199
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002200#ifdef FEAT_WINDOWS
2201/*
2202 * List tab page variables.
2203 */
2204 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002205list_tab_vars(int *first)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002206{
Bram Moolenaar429fa852013-04-15 12:27:36 +02002207 list_hashtable_vars(&curtab->tp_vars->dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002208 (char_u *)"t:", TRUE, first);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002209}
2210#endif
2211
Bram Moolenaara7043832005-01-21 11:56:39 +00002212/*
2213 * List Vim variables.
2214 */
2215 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002216list_vim_vars(int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002217{
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002218 list_hashtable_vars(&vimvarht, (char_u *)"v:", FALSE, first);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002219}
2220
2221/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002222 * List script-local variables, if there is a script.
2223 */
2224 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002225list_script_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002226{
2227 if (current_SID > 0 && current_SID <= ga_scripts.ga_len)
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002228 list_hashtable_vars(&SCRIPT_VARS(current_SID),
2229 (char_u *)"s:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002230}
2231
2232/*
2233 * List function variables, if there is a function.
2234 */
2235 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002236list_func_vars(int *first)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002237{
2238 if (current_funccal != NULL)
2239 list_hashtable_vars(&current_funccal->l_vars.dv_hashtab,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002240 (char_u *)"l:", FALSE, first);
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00002241}
2242
2243/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002244 * List variables in "arg".
2245 */
2246 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002247list_arg_vars(exarg_T *eap, char_u *arg, int *first)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002248{
2249 int error = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002250 int len;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002251 char_u *name;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002252 char_u *name_start;
2253 char_u *arg_subsc;
2254 char_u *tofree;
2255 typval_T tv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002256
2257 while (!ends_excmd(*arg) && !got_int)
2258 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002259 if (error || eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002260 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002261 arg = find_name_end(arg, NULL, NULL, FNE_INCL_BR | FNE_CHECK_START);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002262 if (!vim_iswhite(*arg) && !ends_excmd(*arg))
2263 {
2264 emsg_severe = TRUE;
2265 EMSG(_(e_trailing));
2266 break;
2267 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002268 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002269 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002270 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002271 /* get_name_len() takes care of expanding curly braces */
2272 name_start = name = arg;
2273 len = get_name_len(&arg, &tofree, TRUE, TRUE);
2274 if (len <= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002275 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002276 /* This is mainly to keep test 49 working: when expanding
2277 * curly braces fails overrule the exception error message. */
2278 if (len < 0 && !aborting())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002279 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002280 emsg_severe = TRUE;
2281 EMSG2(_(e_invarg2), arg);
2282 break;
2283 }
2284 error = TRUE;
2285 }
2286 else
2287 {
2288 if (tofree != NULL)
2289 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002290 if (get_var_tv(name, len, &tv, NULL, TRUE, FALSE) == FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002291 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002292 else
2293 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002294 /* handle d.key, l[idx], f(expr) */
2295 arg_subsc = arg;
2296 if (handle_subscript(&arg, &tv, TRUE, TRUE) == FAIL)
Bram Moolenaara7043832005-01-21 11:56:39 +00002297 error = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002298 else
Bram Moolenaara7043832005-01-21 11:56:39 +00002299 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002300 if (arg == arg_subsc && len == 2 && name[1] == ':')
Bram Moolenaara7043832005-01-21 11:56:39 +00002301 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002302 switch (*name)
Bram Moolenaara7043832005-01-21 11:56:39 +00002303 {
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002304 case 'g': list_glob_vars(first); break;
2305 case 'b': list_buf_vars(first); break;
2306 case 'w': list_win_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002307#ifdef FEAT_WINDOWS
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002308 case 't': list_tab_vars(first); break;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00002309#endif
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002310 case 'v': list_vim_vars(first); break;
2311 case 's': list_script_vars(first); break;
2312 case 'l': list_func_vars(first); break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002313 default:
2314 EMSG2(_("E738: Can't list variables for %s"), name);
Bram Moolenaara7043832005-01-21 11:56:39 +00002315 }
Bram Moolenaara7043832005-01-21 11:56:39 +00002316 }
2317 else
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002318 {
2319 char_u numbuf[NUMBUFLEN];
2320 char_u *tf;
2321 int c;
2322 char_u *s;
2323
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00002324 s = echo_string(&tv, &tf, numbuf, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002325 c = *arg;
2326 *arg = NUL;
2327 list_one_var_a((char_u *)"",
2328 arg == arg_subsc ? name : name_start,
Bram Moolenaar7d61a922007-08-30 09:12:23 +00002329 tv.v_type,
2330 s == NULL ? (char_u *)"" : s,
2331 first);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002332 *arg = c;
2333 vim_free(tf);
2334 }
2335 clear_tv(&tv);
Bram Moolenaara7043832005-01-21 11:56:39 +00002336 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002337 }
2338 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002339
2340 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002341 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00002342
2343 arg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002344 }
2345
2346 return arg;
2347}
2348
2349/*
2350 * Set one item of ":let var = expr" or ":let [v1, v2] = list" to its value.
2351 * Returns a pointer to the char just after the var name.
2352 * Returns NULL if there is an error.
2353 */
2354 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002355ex_let_one(
2356 char_u *arg, /* points to variable name */
2357 typval_T *tv, /* value to assign to variable */
2358 int copy, /* copy value from "tv" */
2359 char_u *endchars, /* valid chars after variable name or NULL */
2360 char_u *op) /* "+", "-", "." or NULL*/
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002361{
2362 int c1;
2363 char_u *name;
2364 char_u *p;
2365 char_u *arg_end = NULL;
2366 int len;
2367 int opt_flags;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002368 char_u *tofree = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002369
2370 /*
2371 * ":let $VAR = expr": Set environment variable.
2372 */
2373 if (*arg == '$')
2374 {
2375 /* Find the end of the name. */
2376 ++arg;
2377 name = arg;
2378 len = get_env_len(&arg);
2379 if (len == 0)
2380 EMSG2(_(e_invarg2), name - 1);
2381 else
2382 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002383 if (op != NULL && (*op == '+' || *op == '-'))
2384 EMSG2(_(e_letwrong), op);
2385 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002386 && vim_strchr(endchars, *skipwhite(arg)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002387 EMSG(_(e_letunexp));
Bram Moolenaard4ddfaf2010-12-02 14:48:14 +01002388 else if (!check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002389 {
2390 c1 = name[len];
2391 name[len] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002392 p = get_tv_string_chk(tv);
2393 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002394 {
2395 int mustfree = FALSE;
2396 char_u *s = vim_getenv(name, &mustfree);
2397
2398 if (s != NULL)
2399 {
2400 p = tofree = concat_str(s, p);
2401 if (mustfree)
2402 vim_free(s);
2403 }
2404 }
2405 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002406 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002407 vim_setenv(name, p);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002408 if (STRICMP(name, "HOME") == 0)
2409 init_homedir();
2410 else if (didset_vim && STRICMP(name, "VIM") == 0)
2411 didset_vim = FALSE;
2412 else if (didset_vimruntime
2413 && STRICMP(name, "VIMRUNTIME") == 0)
2414 didset_vimruntime = FALSE;
2415 arg_end = arg;
2416 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002417 name[len] = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002418 vim_free(tofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002419 }
2420 }
2421 }
2422
2423 /*
2424 * ":let &option = expr": Set option value.
2425 * ":let &l:option = expr": Set local option value.
2426 * ":let &g:option = expr": Set global option value.
2427 */
2428 else if (*arg == '&')
2429 {
2430 /* Find the end of the name. */
2431 p = find_option_end(&arg, &opt_flags);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002432 if (p == NULL || (endchars != NULL
2433 && vim_strchr(endchars, *skipwhite(p)) == NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002434 EMSG(_(e_letunexp));
2435 else
2436 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002437 long n;
2438 int opt_type;
2439 long numval;
2440 char_u *stringval = NULL;
2441 char_u *s;
2442
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002443 c1 = *p;
2444 *p = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002445
2446 n = get_tv_number(tv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002447 s = get_tv_string_chk(tv); /* != NULL if number or string */
2448 if (s != NULL && op != NULL && *op != '=')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002449 {
2450 opt_type = get_option_value(arg, &numval,
2451 &stringval, opt_flags);
2452 if ((opt_type == 1 && *op == '.')
2453 || (opt_type == 0 && *op != '.'))
2454 EMSG2(_(e_letwrong), op);
2455 else
2456 {
2457 if (opt_type == 1) /* number */
2458 {
2459 if (*op == '+')
2460 n = numval + n;
2461 else
2462 n = numval - n;
2463 }
2464 else if (opt_type == 0 && stringval != NULL) /* string */
2465 {
2466 s = concat_str(stringval, s);
2467 vim_free(stringval);
2468 stringval = s;
2469 }
2470 }
2471 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002472 if (s != NULL)
2473 {
2474 set_option_value(arg, n, s, opt_flags);
2475 arg_end = p;
2476 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002477 *p = c1;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002478 vim_free(stringval);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002479 }
2480 }
2481
2482 /*
2483 * ":let @r = expr": Set register contents.
2484 */
2485 else if (*arg == '@')
2486 {
2487 ++arg;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002488 if (op != NULL && (*op == '+' || *op == '-'))
2489 EMSG2(_(e_letwrong), op);
2490 else if (endchars != NULL
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00002491 && vim_strchr(endchars, *skipwhite(arg + 1)) == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002492 EMSG(_(e_letunexp));
2493 else
2494 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002495 char_u *ptofree = NULL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002496 char_u *s;
2497
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002498 p = get_tv_string_chk(tv);
2499 if (p != NULL && op != NULL && *op == '.')
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002500 {
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02002501 s = get_reg_contents(*arg == '@' ? '"' : *arg, GREG_EXPR_SRC);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002502 if (s != NULL)
2503 {
Bram Moolenaar89d40322006-08-29 15:30:07 +00002504 p = ptofree = concat_str(s, p);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002505 vim_free(s);
2506 }
2507 }
2508 if (p != NULL)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002509 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002510 write_reg_contents(*arg == '@' ? '"' : *arg, p, -1, FALSE);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002511 arg_end = arg + 1;
2512 }
Bram Moolenaar89d40322006-08-29 15:30:07 +00002513 vim_free(ptofree);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002514 }
2515 }
2516
2517 /*
2518 * ":let var = expr": Set internal variable.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002519 * ":let {expr} = expr": Idem, name made with curly braces
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002520 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002521 else if (eval_isnamec1(*arg) || *arg == '{')
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002522 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002523 lval_T lv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002524
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002525 p = get_lval(arg, tv, &lv, FALSE, FALSE, 0, FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002526 if (p != NULL && lv.ll_name != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002527 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002528 if (endchars != NULL && vim_strchr(endchars, *skipwhite(p)) == NULL)
2529 EMSG(_(e_letunexp));
2530 else
2531 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002532 set_var_lval(&lv, p, tv, copy, op);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002533 arg_end = p;
2534 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002535 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002536 clear_lval(&lv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002537 }
2538
2539 else
2540 EMSG2(_(e_invarg2), arg);
2541
2542 return arg_end;
2543}
2544
2545/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00002546 * If "arg" is equal to "b:changedtick" give an error and return TRUE.
2547 */
2548 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01002549check_changedtick(char_u *arg)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002550{
2551 if (STRNCMP(arg, "b:changedtick", 13) == 0 && !eval_isnamec(arg[13]))
2552 {
2553 EMSG2(_(e_readonlyvar), arg);
2554 return TRUE;
2555 }
2556 return FALSE;
2557}
2558
2559/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002560 * Get an lval: variable, Dict item or List item that can be assigned a value
2561 * to: "name", "na{me}", "name[expr]", "name[expr:expr]", "name[expr][expr]",
2562 * "name.key", "name.key[expr]" etc.
2563 * Indexing only works if "name" is an existing List or Dictionary.
2564 * "name" points to the start of the name.
2565 * If "rettv" is not NULL it points to the value to be assigned.
2566 * "unlet" is TRUE for ":unlet": slightly different behavior when something is
2567 * wrong; must end in space or cmd separator.
2568 *
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002569 * flags:
2570 * GLV_QUIET: do not give error messages
2571 * GLV_NO_AUTOLOAD: do not use script autoloading
2572 *
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002573 * Returns a pointer to just after the name, including indexes.
Bram Moolenaara7043832005-01-21 11:56:39 +00002574 * When an evaluation error occurs "lp->ll_name" is NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002575 * Returns NULL for a parsing error. Still need to free items in "lp"!
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002576 */
2577 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01002578get_lval(
2579 char_u *name,
2580 typval_T *rettv,
2581 lval_T *lp,
2582 int unlet,
2583 int skip,
2584 int flags, /* GLV_ values */
2585 int fne_flags) /* flags for find_name_end() */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002586{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002587 char_u *p;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002588 char_u *expr_start, *expr_end;
2589 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002590 dictitem_T *v;
2591 typval_T var1;
2592 typval_T var2;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002593 int empty1 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00002594 listitem_T *ni;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002595 char_u *key = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002596 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00002597 hashtab_T *ht;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002598 int quiet = flags & GLV_QUIET;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002599
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002600 /* Clear everything in "lp". */
Bram Moolenaar33570922005-01-25 22:26:29 +00002601 vim_memset(lp, 0, sizeof(lval_T));
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002602
2603 if (skip)
2604 {
2605 /* When skipping just find the end of the name. */
2606 lp->ll_name = name;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002607 return find_name_end(name, NULL, NULL, FNE_INCL_BR | fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002608 }
2609
2610 /* Find the end of the name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00002611 p = find_name_end(name, &expr_start, &expr_end, fne_flags);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002612 if (expr_start != NULL)
2613 {
2614 /* Don't expand the name when we already know there is an error. */
2615 if (unlet && !vim_iswhite(*p) && !ends_excmd(*p)
2616 && *p != '[' && *p != '.')
2617 {
2618 EMSG(_(e_trailing));
2619 return NULL;
2620 }
2621
2622 lp->ll_exp_name = make_expanded_name(name, expr_start, expr_end, p);
2623 if (lp->ll_exp_name == NULL)
2624 {
2625 /* Report an invalid expression in braces, unless the
2626 * expression evaluation has been cancelled due to an
2627 * aborting error, an interrupt, or an exception. */
2628 if (!aborting() && !quiet)
2629 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002630 emsg_severe = TRUE;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002631 EMSG2(_(e_invarg2), name);
2632 return NULL;
2633 }
2634 }
2635 lp->ll_name = lp->ll_exp_name;
2636 }
2637 else
2638 lp->ll_name = name;
2639
2640 /* Without [idx] or .key we are done. */
2641 if ((*p != '[' && *p != '.') || lp->ll_name == NULL)
2642 return p;
2643
2644 cc = *p;
2645 *p = NUL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +01002646 v = find_var(lp->ll_name, &ht, flags & GLV_NO_AUTOLOAD);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002647 if (v == NULL && !quiet)
2648 EMSG2(_(e_undefvar), lp->ll_name);
2649 *p = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002650 if (v == NULL)
2651 return NULL;
2652
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002653 /*
2654 * Loop until no more [idx] or .key is following.
2655 */
Bram Moolenaar33570922005-01-25 22:26:29 +00002656 lp->ll_tv = &v->di_tv;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002657 while (*p == '[' || (*p == '.' && lp->ll_tv->v_type == VAR_DICT))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002658 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002659 if (!(lp->ll_tv->v_type == VAR_LIST && lp->ll_tv->vval.v_list != NULL)
2660 && !(lp->ll_tv->v_type == VAR_DICT
2661 && lp->ll_tv->vval.v_dict != NULL))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002662 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002663 if (!quiet)
2664 EMSG(_("E689: Can only index a List or Dictionary"));
2665 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002666 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002667 if (lp->ll_range)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002668 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002669 if (!quiet)
2670 EMSG(_("E708: [:] must come last"));
2671 return NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002672 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002673
Bram Moolenaar8c711452005-01-14 21:53:12 +00002674 len = -1;
2675 if (*p == '.')
2676 {
2677 key = p + 1;
2678 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
2679 ;
2680 if (len == 0)
2681 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002682 if (!quiet)
2683 EMSG(_(e_emptykey));
2684 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002685 }
2686 p = key + len;
2687 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002688 else
2689 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002690 /* Get the index [expr] or the first index [expr: ]. */
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002691 p = skipwhite(p + 1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002692 if (*p == ':')
2693 empty1 = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002694 else
2695 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002696 empty1 = FALSE;
2697 if (eval1(&p, &var1, TRUE) == FAIL) /* recursive! */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002698 return NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002699 if (get_tv_string_chk(&var1) == NULL)
2700 {
2701 /* not a number or string */
2702 clear_tv(&var1);
2703 return NULL;
2704 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002705 }
2706
2707 /* Optionally get the second index [ :expr]. */
2708 if (*p == ':')
2709 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002710 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002711 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002712 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002713 EMSG(_(e_dictrange));
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002714 if (!empty1)
2715 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002716 return NULL;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002717 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002718 if (rettv != NULL && (rettv->v_type != VAR_LIST
2719 || rettv->vval.v_list == NULL))
Bram Moolenaar8c711452005-01-14 21:53:12 +00002720 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002721 if (!quiet)
2722 EMSG(_("E709: [:] requires a List value"));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002723 if (!empty1)
2724 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002725 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002726 }
2727 p = skipwhite(p + 1);
2728 if (*p == ']')
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002729 lp->ll_empty2 = TRUE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002730 else
2731 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002732 lp->ll_empty2 = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002733 if (eval1(&p, &var2, TRUE) == FAIL) /* recursive! */
2734 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00002735 if (!empty1)
2736 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002737 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002738 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002739 if (get_tv_string_chk(&var2) == NULL)
2740 {
2741 /* not a number or string */
2742 if (!empty1)
2743 clear_tv(&var1);
2744 clear_tv(&var2);
2745 return NULL;
2746 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002747 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002748 lp->ll_range = TRUE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002749 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00002750 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002751 lp->ll_range = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002752
Bram Moolenaar8c711452005-01-14 21:53:12 +00002753 if (*p != ']')
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002754 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002755 if (!quiet)
2756 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002757 if (!empty1)
2758 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002759 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002760 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002761 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002762 }
2763
2764 /* Skip to past ']'. */
2765 ++p;
2766 }
2767
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002768 if (lp->ll_tv->v_type == VAR_DICT)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002769 {
2770 if (len == -1)
2771 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002772 /* "[key]": get key from "var1" */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002773 key = get_tv_string(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002774 if (*key == NUL)
2775 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002776 if (!quiet)
2777 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00002778 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002779 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002780 }
2781 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002782 lp->ll_list = NULL;
2783 lp->ll_dict = lp->ll_tv->vval.v_dict;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002784 lp->ll_di = dict_find(lp->ll_dict, key, len);
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002785
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002786 /* When assigning to a scope dictionary check that a function and
2787 * variable name is valid (only variable name unless it is l: or
2788 * g: dictionary). Disallow overwriting a builtin function. */
2789 if (rettv != NULL && lp->ll_dict->dv_scope != 0)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002790 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002791 int prevval;
2792 int wrong;
2793
2794 if (len != -1)
2795 {
2796 prevval = key[len];
2797 key[len] = NUL;
2798 }
Bram Moolenaar4380d1e2013-06-09 20:51:00 +02002799 else
2800 prevval = 0; /* avoid compiler warning */
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002801 wrong = (lp->ll_dict->dv_scope == VAR_DEF_SCOPE
2802 && rettv->v_type == VAR_FUNC
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002803 && var_check_func_name(key, lp->ll_di == NULL))
Bram Moolenaarbdb62052012-07-16 17:31:53 +02002804 || !valid_varname(key);
2805 if (len != -1)
2806 key[len] = prevval;
2807 if (wrong)
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002808 return NULL;
2809 }
2810
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002811 if (lp->ll_di == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002812 {
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002813 /* Can't add "v:" variable. */
2814 if (lp->ll_dict == &vimvardict)
2815 {
2816 EMSG2(_(e_illvar), name);
2817 return NULL;
2818 }
2819
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00002820 /* Key does not exist in dict: may need to add it. */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002821 if (*p == '[' || *p == '.' || unlet)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002822 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002823 if (!quiet)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002824 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002825 if (len == -1)
2826 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002827 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002828 }
2829 if (len == -1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002830 lp->ll_newkey = vim_strsave(key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002831 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002832 lp->ll_newkey = vim_strnsave(key, len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002833 if (len == -1)
2834 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002835 if (lp->ll_newkey == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002836 p = NULL;
2837 break;
2838 }
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002839 /* existing variable, need to check if it can be changed */
Bram Moolenaar77354e72015-04-21 16:49:05 +02002840 else if (var_check_ro(lp->ll_di->di_flags, name, FALSE))
Bram Moolenaar4228bec2011-03-27 16:03:15 +02002841 return NULL;
2842
Bram Moolenaar8c711452005-01-14 21:53:12 +00002843 if (len == -1)
2844 clear_tv(&var1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002845 lp->ll_tv = &lp->ll_di->di_tv;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002846 }
2847 else
2848 {
2849 /*
2850 * Get the number and item for the only or first index of the List.
2851 */
2852 if (empty1)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002853 lp->ll_n1 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002854 else
2855 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002856 lp->ll_n1 = get_tv_number(&var1); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002857 clear_tv(&var1);
2858 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002859 lp->ll_dict = NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002860 lp->ll_list = lp->ll_tv->vval.v_list;
2861 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2862 if (lp->ll_li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002863 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00002864 if (lp->ll_n1 < 0)
2865 {
2866 lp->ll_n1 = 0;
2867 lp->ll_li = list_find(lp->ll_list, lp->ll_n1);
2868 }
2869 }
2870 if (lp->ll_li == NULL)
2871 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002872 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002873 clear_tv(&var2);
Bram Moolenaare9623882011-04-21 14:27:28 +02002874 if (!quiet)
2875 EMSGN(_(e_listidx), lp->ll_n1);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002876 return NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00002877 }
2878
2879 /*
2880 * May need to find the item or absolute index for the second
2881 * index of a range.
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002882 * When no index given: "lp->ll_empty2" is TRUE.
2883 * Otherwise "lp->ll_n2" is set to the second index.
Bram Moolenaar8c711452005-01-14 21:53:12 +00002884 */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002885 if (lp->ll_range && !lp->ll_empty2)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002886 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00002887 lp->ll_n2 = get_tv_number(&var2); /* is number or string */
Bram Moolenaar8c711452005-01-14 21:53:12 +00002888 clear_tv(&var2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002889 if (lp->ll_n2 < 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00002890 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002891 ni = list_find(lp->ll_list, lp->ll_n2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002892 if (ni == NULL)
Bram Moolenaare9623882011-04-21 14:27:28 +02002893 {
2894 if (!quiet)
2895 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002896 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002897 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002898 lp->ll_n2 = list_idx_of_item(lp->ll_list, ni);
Bram Moolenaar8c711452005-01-14 21:53:12 +00002899 }
2900
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002901 /* Check that lp->ll_n2 isn't before lp->ll_n1. */
2902 if (lp->ll_n1 < 0)
2903 lp->ll_n1 = list_idx_of_item(lp->ll_list, lp->ll_li);
2904 if (lp->ll_n2 < lp->ll_n1)
Bram Moolenaare9623882011-04-21 14:27:28 +02002905 {
2906 if (!quiet)
2907 EMSGN(_(e_listidx), lp->ll_n2);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002908 return NULL;
Bram Moolenaare9623882011-04-21 14:27:28 +02002909 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002910 }
2911
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002912 lp->ll_tv = &lp->ll_li->li_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00002913 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002914 }
2915
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002916 return p;
2917}
2918
2919/*
Bram Moolenaar33570922005-01-25 22:26:29 +00002920 * Clear lval "lp" that was filled by get_lval().
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002921 */
2922 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002923clear_lval(lval_T *lp)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002924{
2925 vim_free(lp->ll_exp_name);
2926 vim_free(lp->ll_newkey);
2927}
2928
2929/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002930 * Set a variable that was parsed by get_lval() to "rettv".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002931 * "endp" points to just after the parsed name.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002932 * "op" is NULL, "+" for "+=", "-" for "-=", "." for ".=" or "=" for "=".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002933 */
2934 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01002935set_var_lval(
2936 lval_T *lp,
2937 char_u *endp,
2938 typval_T *rettv,
2939 int copy,
2940 char_u *op)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002941{
2942 int cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00002943 listitem_T *ri;
2944 dictitem_T *di;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002945
2946 if (lp->ll_tv == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002947 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002948 if (!check_changedtick(lp->ll_name))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002949 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002950 cc = *endp;
2951 *endp = NUL;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002952 if (op != NULL && *op != '=')
2953 {
Bram Moolenaar33570922005-01-25 22:26:29 +00002954 typval_T tv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002955
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002956 /* handle +=, -= and .= */
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002957 di = NULL;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00002958 if (get_var_tv(lp->ll_name, (int)STRLEN(lp->ll_name),
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002959 &tv, &di, TRUE, FALSE) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002960 {
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02002961 if ((di == NULL
2962 || (!var_check_ro(di->di_flags, lp->ll_name, FALSE)
2963 && !tv_check_lock(di->di_tv.v_lock, lp->ll_name,
2964 FALSE)))
2965 && tv_op(&tv, rettv, op) == OK)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00002966 set_var(lp->ll_name, &tv, FALSE);
2967 clear_tv(&tv);
2968 }
2969 }
2970 else
2971 set_var(lp->ll_name, rettv, copy);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002972 *endp = cc;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00002973 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002974 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002975 else if (tv_check_lock(lp->ll_newkey == NULL
2976 ? lp->ll_tv->v_lock
Bram Moolenaar77354e72015-04-21 16:49:05 +02002977 : lp->ll_tv->vval.v_dict->dv_lock, lp->ll_name, FALSE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00002978 ;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002979 else if (lp->ll_range)
2980 {
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002981 listitem_T *ll_li = lp->ll_li;
2982 int ll_n1 = lp->ll_n1;
2983
2984 /*
2985 * Check whether any of the list items is locked
2986 */
Bram Moolenaarb2a851f2014-12-07 00:18:33 +01002987 for (ri = rettv->vval.v_list->lv_first; ri != NULL && ll_li != NULL; )
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002988 {
Bram Moolenaar77354e72015-04-21 16:49:05 +02002989 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02002990 return;
2991 ri = ri->li_next;
2992 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == ll_n1))
2993 break;
2994 ll_li = ll_li->li_next;
2995 ++ll_n1;
2996 }
2997
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00002998 /*
2999 * Assign the List values to the list items.
3000 */
3001 for (ri = rettv->vval.v_list->lv_first; ri != NULL; )
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003002 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003003 if (op != NULL && *op != '=')
3004 tv_op(&lp->ll_li->li_tv, &ri->li_tv, op);
3005 else
3006 {
3007 clear_tv(&lp->ll_li->li_tv);
3008 copy_tv(&ri->li_tv, &lp->ll_li->li_tv);
3009 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003010 ri = ri->li_next;
3011 if (ri == NULL || (!lp->ll_empty2 && lp->ll_n2 == lp->ll_n1))
3012 break;
3013 if (lp->ll_li->li_next == NULL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003014 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003015 /* Need to add an empty item. */
Bram Moolenaar4463f292005-09-25 22:20:24 +00003016 if (list_append_number(lp->ll_list, 0) == FAIL)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003017 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003018 ri = NULL;
3019 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003020 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003021 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003022 lp->ll_li = lp->ll_li->li_next;
3023 ++lp->ll_n1;
3024 }
3025 if (ri != NULL)
3026 EMSG(_("E710: List value has more items than target"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003027 else if (lp->ll_empty2
3028 ? (lp->ll_li != NULL && lp->ll_li->li_next != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003029 : lp->ll_n1 != lp->ll_n2)
3030 EMSG(_("E711: List value has not enough items"));
3031 }
3032 else
3033 {
3034 /*
3035 * Assign to a List or Dictionary item.
3036 */
3037 if (lp->ll_newkey != NULL)
3038 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003039 if (op != NULL && *op != '=')
3040 {
3041 EMSG2(_(e_letwrong), op);
3042 return;
3043 }
3044
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003045 /* Need to add an item to the Dictionary. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003046 di = dictitem_alloc(lp->ll_newkey);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003047 if (di == NULL)
3048 return;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003049 if (dict_add(lp->ll_tv->vval.v_dict, di) == FAIL)
3050 {
3051 vim_free(di);
3052 return;
3053 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003054 lp->ll_tv = &di->di_tv;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00003055 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003056 else if (op != NULL && *op != '=')
3057 {
3058 tv_op(lp->ll_tv, rettv, op);
3059 return;
3060 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003061 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003062 clear_tv(lp->ll_tv);
Bram Moolenaar8c711452005-01-14 21:53:12 +00003063
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003064 /*
3065 * Assign the value to the variable or list item.
3066 */
3067 if (copy)
3068 copy_tv(rettv, lp->ll_tv);
3069 else
3070 {
3071 *lp->ll_tv = *rettv;
Bram Moolenaar758711c2005-02-02 23:11:38 +00003072 lp->ll_tv->v_lock = 0;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003073 init_tv(rettv);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003074 }
3075 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003076}
3077
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003078/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003079 * Handle "tv1 += tv2", "tv1 -= tv2" and "tv1 .= tv2"
3080 * Returns OK or FAIL.
3081 */
3082 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003083tv_op(typval_T *tv1, typval_T *tv2, char_u *op)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003084{
3085 long n;
3086 char_u numbuf[NUMBUFLEN];
3087 char_u *s;
3088
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003089 /* Can't do anything with a Funcref, Dict, v:true on the right. */
3090 if (tv2->v_type != VAR_FUNC && tv2->v_type != VAR_DICT
3091 && tv2->v_type != VAR_SPECIAL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003092 {
3093 switch (tv1->v_type)
3094 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01003095 case VAR_UNKNOWN:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003096 case VAR_DICT:
3097 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003098 case VAR_PARTIAL:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01003099 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003100 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003101 case VAR_CHANNEL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003102 break;
3103
3104 case VAR_LIST:
3105 if (*op != '+' || tv2->v_type != VAR_LIST)
3106 break;
3107 /* List += List */
3108 if (tv1->vval.v_list != NULL && tv2->vval.v_list != NULL)
3109 list_extend(tv1->vval.v_list, tv2->vval.v_list, NULL);
3110 return OK;
3111
3112 case VAR_NUMBER:
3113 case VAR_STRING:
3114 if (tv2->v_type == VAR_LIST)
3115 break;
3116 if (*op == '+' || *op == '-')
3117 {
3118 /* nr += nr or nr -= nr*/
3119 n = get_tv_number(tv1);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003120#ifdef FEAT_FLOAT
3121 if (tv2->v_type == VAR_FLOAT)
3122 {
3123 float_T f = n;
3124
3125 if (*op == '+')
3126 f += tv2->vval.v_float;
3127 else
3128 f -= tv2->vval.v_float;
3129 clear_tv(tv1);
3130 tv1->v_type = VAR_FLOAT;
3131 tv1->vval.v_float = f;
3132 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003133 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003134#endif
3135 {
3136 if (*op == '+')
3137 n += get_tv_number(tv2);
3138 else
3139 n -= get_tv_number(tv2);
3140 clear_tv(tv1);
3141 tv1->v_type = VAR_NUMBER;
3142 tv1->vval.v_number = n;
3143 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003144 }
3145 else
3146 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003147 if (tv2->v_type == VAR_FLOAT)
3148 break;
3149
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003150 /* str .= str */
3151 s = get_tv_string(tv1);
3152 s = concat_str(s, get_tv_string_buf(tv2, numbuf));
3153 clear_tv(tv1);
3154 tv1->v_type = VAR_STRING;
3155 tv1->vval.v_string = s;
3156 }
3157 return OK;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003158
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003159 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003160#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003161 {
3162 float_T f;
3163
3164 if (*op == '.' || (tv2->v_type != VAR_FLOAT
3165 && tv2->v_type != VAR_NUMBER
3166 && tv2->v_type != VAR_STRING))
3167 break;
3168 if (tv2->v_type == VAR_FLOAT)
3169 f = tv2->vval.v_float;
3170 else
3171 f = get_tv_number(tv2);
3172 if (*op == '+')
3173 tv1->vval.v_float += f;
3174 else
3175 tv1->vval.v_float -= f;
3176 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00003177#endif
Bram Moolenaar5fac4672016-03-02 22:16:32 +01003178 return OK;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003179 }
3180 }
3181
3182 EMSG2(_(e_letwrong), op);
3183 return FAIL;
3184}
3185
3186/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003187 * Add a watcher to a list.
3188 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003189 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003190list_add_watch(list_T *l, listwatch_T *lw)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003191{
3192 lw->lw_next = l->lv_watch;
3193 l->lv_watch = lw;
3194}
3195
3196/*
Bram Moolenaar758711c2005-02-02 23:11:38 +00003197 * Remove a watcher from a list.
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003198 * No warning when it isn't found...
3199 */
Bram Moolenaarb6c589a2013-05-15 14:39:52 +02003200 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003201list_rem_watch(list_T *l, listwatch_T *lwrem)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003202{
Bram Moolenaar33570922005-01-25 22:26:29 +00003203 listwatch_T *lw, **lwp;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003204
3205 lwp = &l->lv_watch;
3206 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3207 {
3208 if (lw == lwrem)
3209 {
3210 *lwp = lw->lw_next;
3211 break;
3212 }
3213 lwp = &lw->lw_next;
3214 }
3215}
3216
3217/*
3218 * Just before removing an item from a list: advance watchers to the next
3219 * item.
3220 */
3221 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003222list_fix_watch(list_T *l, listitem_T *item)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003223{
Bram Moolenaar33570922005-01-25 22:26:29 +00003224 listwatch_T *lw;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003225
3226 for (lw = l->lv_watch; lw != NULL; lw = lw->lw_next)
3227 if (lw->lw_item == item)
3228 lw->lw_item = item->li_next;
3229}
3230
3231/*
3232 * Evaluate the expression used in a ":for var in expr" command.
3233 * "arg" points to "var".
3234 * Set "*errp" to TRUE for an error, FALSE otherwise;
3235 * Return a pointer that holds the info. Null when there is an error.
3236 */
3237 void *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003238eval_for_line(
3239 char_u *arg,
3240 int *errp,
3241 char_u **nextcmdp,
3242 int skip)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003243{
Bram Moolenaar33570922005-01-25 22:26:29 +00003244 forinfo_T *fi;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003245 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +00003246 typval_T tv;
3247 list_T *l;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003248
3249 *errp = TRUE; /* default: there is an error */
3250
Bram Moolenaar33570922005-01-25 22:26:29 +00003251 fi = (forinfo_T *)alloc_clear(sizeof(forinfo_T));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003252 if (fi == NULL)
3253 return NULL;
3254
3255 expr = skip_var_list(arg, &fi->fi_varcount, &fi->fi_semicolon);
3256 if (expr == NULL)
3257 return fi;
3258
3259 expr = skipwhite(expr);
3260 if (expr[0] != 'i' || expr[1] != 'n' || !vim_iswhite(expr[2]))
3261 {
Bram Moolenaare49b69a2005-01-08 16:11:57 +00003262 EMSG(_("E690: Missing \"in\" after :for"));
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003263 return fi;
3264 }
3265
3266 if (skip)
3267 ++emsg_skip;
3268 if (eval0(skipwhite(expr + 2), &tv, nextcmdp, !skip) == OK)
3269 {
3270 *errp = FALSE;
3271 if (!skip)
3272 {
3273 l = tv.vval.v_list;
3274 if (tv.v_type != VAR_LIST || l == NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003275 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003276 EMSG(_(e_listreq));
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003277 clear_tv(&tv);
3278 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003279 else
3280 {
Bram Moolenaar7bb4c6e2005-09-07 21:22:27 +00003281 /* No need to increment the refcount, it's already set for the
3282 * list being used in "tv". */
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003283 fi->fi_list = l;
3284 list_add_watch(l, &fi->fi_lw);
3285 fi->fi_lw.lw_item = l->lv_first;
3286 }
3287 }
3288 }
3289 if (skip)
3290 --emsg_skip;
3291
3292 return fi;
3293}
3294
3295/*
3296 * Use the first item in a ":for" list. Advance to the next.
3297 * Assign the values to the variable (list). "arg" points to the first one.
3298 * Return TRUE when a valid item was found, FALSE when at end of list or
3299 * something wrong.
3300 */
3301 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003302next_for_item(void *fi_void, char_u *arg)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003303{
Bram Moolenaarc4b99e02013-06-23 14:30:47 +02003304 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003305 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00003306 listitem_T *item;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003307
3308 item = fi->fi_lw.lw_item;
3309 if (item == NULL)
3310 result = FALSE;
3311 else
3312 {
3313 fi->fi_lw.lw_item = item->li_next;
3314 result = (ex_let_vars(arg, &item->li_tv, TRUE,
3315 fi->fi_semicolon, fi->fi_varcount, NULL) == OK);
3316 }
3317 return result;
3318}
3319
3320/*
3321 * Free the structure used to store info used by ":for".
3322 */
3323 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003324free_for_info(void *fi_void)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003325{
Bram Moolenaar33570922005-01-25 22:26:29 +00003326 forinfo_T *fi = (forinfo_T *)fi_void;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003327
Bram Moolenaarab7013c2005-01-09 21:23:56 +00003328 if (fi != NULL && fi->fi_list != NULL)
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003329 {
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003330 list_rem_watch(fi->fi_list, &fi->fi_lw);
Bram Moolenaarf461c8e2005-06-25 23:04:51 +00003331 list_unref(fi->fi_list);
3332 }
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003333 vim_free(fi);
3334}
3335
Bram Moolenaar071d4272004-06-13 20:20:40 +00003336#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3337
3338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003339set_context_for_expression(
3340 expand_T *xp,
3341 char_u *arg,
3342 cmdidx_T cmdidx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003343{
3344 int got_eq = FALSE;
3345 int c;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003346 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003347
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003348 if (cmdidx == CMD_let)
3349 {
3350 xp->xp_context = EXPAND_USER_VARS;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003351 if (vim_strpbrk(arg, (char_u *)"\"'+-*/%.=!?~|&$([<>,#") == NULL)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003352 {
3353 /* ":let var1 var2 ...": find last space. */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00003354 for (p = arg + STRLEN(arg); p >= arg; )
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003355 {
3356 xp->xp_pattern = p;
Bram Moolenaar33570922005-01-25 22:26:29 +00003357 mb_ptr_back(arg, p);
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003358 if (vim_iswhite(*p))
3359 break;
3360 }
3361 return;
3362 }
3363 }
3364 else
3365 xp->xp_context = cmdidx == CMD_call ? EXPAND_FUNCTIONS
3366 : EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003367 while ((xp->xp_pattern = vim_strpbrk(arg,
3368 (char_u *)"\"'+-*/%.=!?~|&$([<>,#")) != NULL)
3369 {
3370 c = *xp->xp_pattern;
3371 if (c == '&')
3372 {
3373 c = xp->xp_pattern[1];
3374 if (c == '&')
3375 {
3376 ++xp->xp_pattern;
3377 xp->xp_context = cmdidx != CMD_let || got_eq
3378 ? EXPAND_EXPRESSION : EXPAND_NOTHING;
3379 }
3380 else if (c != ' ')
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003381 {
Bram Moolenaar071d4272004-06-13 20:20:40 +00003382 xp->xp_context = EXPAND_SETTINGS;
Bram Moolenaar11cbeb12005-03-11 22:51:16 +00003383 if ((c == 'l' || c == 'g') && xp->xp_pattern[2] == ':')
3384 xp->xp_pattern += 2;
3385
3386 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003387 }
3388 else if (c == '$')
3389 {
3390 /* environment variable */
3391 xp->xp_context = EXPAND_ENV_VARS;
3392 }
3393 else if (c == '=')
3394 {
3395 got_eq = TRUE;
3396 xp->xp_context = EXPAND_EXPRESSION;
3397 }
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003398 else if ((c == '<' || c == '#')
Bram Moolenaar071d4272004-06-13 20:20:40 +00003399 && xp->xp_context == EXPAND_FUNCTIONS
3400 && vim_strchr(xp->xp_pattern, '(') == NULL)
3401 {
Bram Moolenaar8a349ff2014-11-12 20:09:06 +01003402 /* Function name can start with "<SNR>" and contain '#'. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003403 break;
3404 }
3405 else if (cmdidx != CMD_let || got_eq)
3406 {
3407 if (c == '"') /* string */
3408 {
3409 while ((c = *++xp->xp_pattern) != NUL && c != '"')
3410 if (c == '\\' && xp->xp_pattern[1] != NUL)
3411 ++xp->xp_pattern;
3412 xp->xp_context = EXPAND_NOTHING;
3413 }
3414 else if (c == '\'') /* literal string */
3415 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00003416 /* Trick: '' is like stopping and starting a literal string. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003417 while ((c = *++xp->xp_pattern) != NUL && c != '\'')
3418 /* skip */ ;
3419 xp->xp_context = EXPAND_NOTHING;
3420 }
3421 else if (c == '|')
3422 {
3423 if (xp->xp_pattern[1] == '|')
3424 {
3425 ++xp->xp_pattern;
3426 xp->xp_context = EXPAND_EXPRESSION;
3427 }
3428 else
3429 xp->xp_context = EXPAND_COMMANDS;
3430 }
3431 else
3432 xp->xp_context = EXPAND_EXPRESSION;
3433 }
3434 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00003435 /* Doesn't look like something valid, expand as an expression
3436 * anyway. */
3437 xp->xp_context = EXPAND_EXPRESSION;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003438 arg = xp->xp_pattern;
3439 if (*arg != NUL)
3440 while ((c = *++arg) != NUL && (c == ' ' || c == '\t'))
3441 /* skip */ ;
3442 }
3443 xp->xp_pattern = arg;
3444}
3445
3446#endif /* FEAT_CMDL_COMPL */
3447
3448/*
3449 * ":1,25call func(arg1, arg2)" function call.
3450 */
3451 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003452ex_call(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453{
3454 char_u *arg = eap->arg;
3455 char_u *startarg;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003456 char_u *name;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003457 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +00003459 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460 linenr_T lnum;
3461 int doesrange;
3462 int failed = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00003463 funcdict_T fudi;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003464 partial_T *partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003465
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003466 if (eap->skip)
3467 {
3468 /* trans_function_name() doesn't work well when skipping, use eval0()
3469 * instead to skip to any following command, e.g. for:
3470 * :if 0 | call dict.foo().bar() | endif */
Bram Moolenaar25091292011-09-30 18:35:57 +02003471 ++emsg_skip;
3472 if (eval0(eap->arg, &rettv, &eap->nextcmd, FALSE) != FAIL)
3473 clear_tv(&rettv);
3474 --emsg_skip;
Bram Moolenaar6d0efda2011-01-04 19:03:27 +01003475 return;
3476 }
3477
Bram Moolenaar65639032016-03-16 21:40:30 +01003478 tofree = trans_function_name(&arg, eap->skip, TFN_INT, &fudi, &partial);
Bram Moolenaara2a31752006-10-24 11:49:25 +00003479 if (fudi.fd_newkey != NULL)
3480 {
3481 /* Still need to give an error message for missing key. */
3482 EMSG2(_(e_dictkey), fudi.fd_newkey);
3483 vim_free(fudi.fd_newkey);
3484 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003485 if (tofree == NULL)
3486 return;
3487
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003488 /* Increase refcount on dictionary, it could get deleted when evaluating
3489 * the arguments. */
3490 if (fudi.fd_dict != NULL)
3491 ++fudi.fd_dict->dv_refcount;
3492
Bram Moolenaar65639032016-03-16 21:40:30 +01003493 /* If it is the name of a variable of type VAR_FUNC or VAR_PARTIAL use its
3494 * contents. For VAR_PARTIAL get its partial, unless we already have one
3495 * from trans_function_name(). */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003496 len = (int)STRLEN(tofree);
Bram Moolenaar65639032016-03-16 21:40:30 +01003497 name = deref_func_name(tofree, &len,
3498 partial != NULL ? NULL : &partial, FALSE);
3499
3500 /* When calling fdict.func(), where "func" is a partial, use "fdict"
3501 * instead of the dict in the partial, for backwards compatibility.
3502 * TODO: Do use the arguments in the partial? */
3503 if (fudi.fd_dict != NULL)
3504 partial = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003505
Bram Moolenaar532c7802005-01-27 14:44:31 +00003506 /* Skip white space to allow ":call func ()". Not good, but required for
3507 * backward compatibility. */
3508 startarg = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003509 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003510
3511 if (*startarg != '(')
3512 {
Bram Moolenaar8dd9ac52008-11-06 10:05:42 +00003513 EMSG2(_("E107: Missing parentheses: %s"), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003514 goto end;
3515 }
3516
3517 /*
3518 * When skipping, evaluate the function once, to find the end of the
3519 * arguments.
3520 * When the function takes a range, this is discovered after the first
3521 * call, and the loop is broken.
3522 */
3523 if (eap->skip)
3524 {
3525 ++emsg_skip;
3526 lnum = eap->line2; /* do it once, also with an invalid range */
3527 }
3528 else
3529 lnum = eap->line1;
3530 for ( ; lnum <= eap->line2; ++lnum)
3531 {
3532 if (!eap->skip && eap->addr_count > 0)
3533 {
3534 curwin->w_cursor.lnum = lnum;
3535 curwin->w_cursor.col = 0;
Bram Moolenaar0acc5612011-07-15 21:24:11 +02003536#ifdef FEAT_VIRTUALEDIT
3537 curwin->w_cursor.coladd = 0;
3538#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00003539 }
3540 arg = startarg;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003541 if (get_func_tv(name, (int)STRLEN(name), &rettv, &arg,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003542 eap->line1, eap->line2, &doesrange,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003543 !eap->skip, partial, fudi.fd_dict) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003544 {
3545 failed = TRUE;
3546 break;
3547 }
Bram Moolenaarf2789872006-11-28 19:54:04 +00003548
3549 /* Handle a function returning a Funcref, Dictionary or List. */
3550 if (handle_subscript(&arg, &rettv, !eap->skip, TRUE) == FAIL)
3551 {
3552 failed = TRUE;
3553 break;
3554 }
3555
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003556 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003557 if (doesrange || eap->skip)
3558 break;
Bram Moolenaarf2789872006-11-28 19:54:04 +00003559
Bram Moolenaar071d4272004-06-13 20:20:40 +00003560 /* Stop when immediately aborting on error, or when an interrupt
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003561 * occurred or an exception was thrown but not caught.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00003562 * get_func_tv() returned OK, so that the check for trailing
Bram Moolenaar49cd9572005-01-03 21:06:01 +00003563 * characters below is executed. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003564 if (aborting())
3565 break;
3566 }
3567 if (eap->skip)
3568 --emsg_skip;
3569
3570 if (!failed)
3571 {
3572 /* Check for trailing illegal characters and a following command. */
3573 if (!ends_excmd(*arg))
3574 {
3575 emsg_severe = TRUE;
3576 EMSG(_(e_trailing));
3577 }
3578 else
3579 eap->nextcmd = check_nextcmd(arg);
3580 }
3581
3582end:
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003583 dict_unref(fudi.fd_dict);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00003584 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003585}
3586
3587/*
3588 * ":unlet[!] var1 ... " command.
3589 */
3590 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003591ex_unlet(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003592{
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003593 ex_unletlock(eap, eap->arg, 0);
3594}
3595
3596/*
3597 * ":lockvar" and ":unlockvar" commands
3598 */
3599 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003600ex_lockvar(exarg_T *eap)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003601{
Bram Moolenaar071d4272004-06-13 20:20:40 +00003602 char_u *arg = eap->arg;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003603 int deep = 2;
3604
3605 if (eap->forceit)
3606 deep = -1;
3607 else if (vim_isdigit(*arg))
3608 {
3609 deep = getdigits(&arg);
3610 arg = skipwhite(arg);
3611 }
3612
3613 ex_unletlock(eap, arg, deep);
3614}
3615
3616/*
3617 * ":unlet", ":lockvar" and ":unlockvar" are quite similar.
3618 */
3619 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003620ex_unletlock(
3621 exarg_T *eap,
3622 char_u *argstart,
3623 int deep)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003624{
3625 char_u *arg = argstart;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 char_u *name_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003627 int error = FALSE;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003628 lval_T lv;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003629
3630 do
3631 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003632 /* Parse the name and find the end. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003633 name_end = get_lval(arg, NULL, &lv, TRUE, eap->skip || error, 0,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +00003634 FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003635 if (lv.ll_name == NULL)
3636 error = TRUE; /* error but continue parsing */
3637 if (name_end == NULL || (!vim_iswhite(*name_end)
3638 && !ends_excmd(*name_end)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003640 if (name_end != NULL)
3641 {
3642 emsg_severe = TRUE;
3643 EMSG(_(e_trailing));
3644 }
3645 if (!(eap->skip || error))
3646 clear_lval(&lv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 break;
3648 }
3649
3650 if (!error && !eap->skip)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003651 {
3652 if (eap->cmdidx == CMD_unlet)
3653 {
3654 if (do_unlet_var(&lv, name_end, eap->forceit) == FAIL)
3655 error = TRUE;
3656 }
3657 else
3658 {
3659 if (do_lock_var(&lv, name_end, deep,
3660 eap->cmdidx == CMD_lockvar) == FAIL)
3661 error = TRUE;
3662 }
3663 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003664
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003665 if (!eap->skip)
3666 clear_lval(&lv);
3667
Bram Moolenaar071d4272004-06-13 20:20:40 +00003668 arg = skipwhite(name_end);
3669 } while (!ends_excmd(*arg));
3670
3671 eap->nextcmd = check_nextcmd(arg);
3672}
3673
Bram Moolenaar8c711452005-01-14 21:53:12 +00003674 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003675do_unlet_var(
3676 lval_T *lp,
3677 char_u *name_end,
3678 int forceit)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003679{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003680 int ret = OK;
3681 int cc;
3682
3683 if (lp->ll_tv == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00003684 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003685 cc = *name_end;
3686 *name_end = NUL;
3687
3688 /* Normal name or expanded name. */
3689 if (check_changedtick(lp->ll_name))
3690 ret = FAIL;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003691 else if (do_unlet(lp->ll_name, forceit) == FAIL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003692 ret = FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003693 *name_end = cc;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003694 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003695 else if ((lp->ll_list != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003696 && tv_check_lock(lp->ll_list->lv_lock, lp->ll_name, FALSE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003697 || (lp->ll_dict != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02003698 && tv_check_lock(lp->ll_dict->dv_lock, lp->ll_name, FALSE)))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003699 return FAIL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003700 else if (lp->ll_range)
3701 {
Bram Moolenaar33570922005-01-25 22:26:29 +00003702 listitem_T *li;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003703 listitem_T *ll_li = lp->ll_li;
Bram Moolenaarc9703302016-01-17 21:49:33 +01003704 int ll_n1 = lp->ll_n1;
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003705
3706 while (ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= ll_n1))
3707 {
3708 li = ll_li->li_next;
Bram Moolenaar77354e72015-04-21 16:49:05 +02003709 if (tv_check_lock(ll_li->li_tv.v_lock, lp->ll_name, FALSE))
Bram Moolenaarf2d912e2014-08-29 09:46:10 +02003710 return FAIL;
3711 ll_li = li;
3712 ++ll_n1;
3713 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003714
3715 /* Delete a range of List items. */
3716 while (lp->ll_li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3717 {
3718 li = lp->ll_li->li_next;
3719 listitem_remove(lp->ll_list, lp->ll_li);
3720 lp->ll_li = li;
3721 ++lp->ll_n1;
3722 }
3723 }
3724 else
3725 {
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003726 if (lp->ll_list != NULL)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003727 /* unlet a List item. */
3728 listitem_remove(lp->ll_list, lp->ll_li);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003729 else
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003730 /* unlet a Dictionary item. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00003731 dictitem_remove(lp->ll_dict, lp->ll_di);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00003732 }
3733
3734 return ret;
Bram Moolenaar8c711452005-01-14 21:53:12 +00003735}
3736
Bram Moolenaar071d4272004-06-13 20:20:40 +00003737/*
3738 * "unlet" a variable. Return OK if it existed, FAIL if not.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003739 * When "forceit" is TRUE don't complain if the variable doesn't exist.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003740 */
3741 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003742do_unlet(char_u *name, int forceit)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003743{
Bram Moolenaar33570922005-01-25 22:26:29 +00003744 hashtab_T *ht;
3745 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003746 char_u *varname;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02003747 dict_T *d;
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003748 dictitem_T *di;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003749
Bram Moolenaar33570922005-01-25 22:26:29 +00003750 ht = find_var_ht(name, &varname);
3751 if (ht != NULL && *varname != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003752 {
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003753 if (ht == &globvarht)
3754 d = &globvardict;
3755 else if (current_funccal != NULL
3756 && ht == &current_funccal->l_vars.dv_hashtab)
3757 d = &current_funccal->l_vars;
3758 else if (ht == &compat_hashtab)
3759 d = &vimvardict;
3760 else
3761 {
3762 di = find_var_in_ht(ht, *name, (char_u *)"", FALSE);
3763 d = di == NULL ? NULL : di->di_tv.vval.v_dict;
3764 }
3765 if (d == NULL)
3766 {
3767 EMSG2(_(e_intern2), "do_unlet()");
3768 return FAIL;
3769 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003770 hi = hash_find(ht, varname);
3771 if (!HASHITEM_EMPTY(hi))
Bram Moolenaara7043832005-01-21 11:56:39 +00003772 {
Bram Moolenaarafbdeb82008-01-05 21:16:31 +00003773 di = HI2DI(hi);
Bram Moolenaar77354e72015-04-21 16:49:05 +02003774 if (var_check_fixed(di->di_flags, name, FALSE)
Bram Moolenaar71bcfdf2016-01-09 18:20:46 +01003775 || var_check_ro(di->di_flags, name, FALSE)
3776 || tv_check_lock(d->dv_lock, name, FALSE))
Bram Moolenaaraf8af8b2016-01-04 22:05:24 +01003777 return FAIL;
3778
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003779 delete_var(ht, hi);
3780 return OK;
Bram Moolenaara7043832005-01-21 11:56:39 +00003781 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00003782 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003783 if (forceit)
3784 return OK;
3785 EMSG2(_("E108: No such variable: \"%s\""), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003786 return FAIL;
3787}
3788
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003789/*
3790 * Lock or unlock variable indicated by "lp".
3791 * "deep" is the levels to go (-1 for unlimited);
3792 * "lock" is TRUE for ":lockvar", FALSE for ":unlockvar".
3793 */
3794 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003795do_lock_var(
3796 lval_T *lp,
3797 char_u *name_end,
3798 int deep,
3799 int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003800{
3801 int ret = OK;
3802 int cc;
3803 dictitem_T *di;
3804
3805 if (deep == 0) /* nothing to do */
3806 return OK;
3807
3808 if (lp->ll_tv == NULL)
3809 {
3810 cc = *name_end;
3811 *name_end = NUL;
3812
3813 /* Normal name or expanded name. */
3814 if (check_changedtick(lp->ll_name))
3815 ret = FAIL;
3816 else
3817 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +01003818 di = find_var(lp->ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003819 if (di == NULL)
3820 ret = FAIL;
3821 else
3822 {
3823 if (lock)
3824 di->di_flags |= DI_FLAGS_LOCK;
3825 else
3826 di->di_flags &= ~DI_FLAGS_LOCK;
3827 item_lock(&di->di_tv, deep, lock);
3828 }
3829 }
3830 *name_end = cc;
3831 }
3832 else if (lp->ll_range)
3833 {
3834 listitem_T *li = lp->ll_li;
3835
3836 /* (un)lock a range of List items. */
3837 while (li != NULL && (lp->ll_empty2 || lp->ll_n2 >= lp->ll_n1))
3838 {
3839 item_lock(&li->li_tv, deep, lock);
3840 li = li->li_next;
3841 ++lp->ll_n1;
3842 }
3843 }
3844 else if (lp->ll_list != NULL)
3845 /* (un)lock a List item. */
3846 item_lock(&lp->ll_li->li_tv, deep, lock);
3847 else
Bram Moolenaar641e48c2015-06-25 16:09:26 +02003848 /* (un)lock a Dictionary item. */
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003849 item_lock(&lp->ll_di->di_tv, deep, lock);
3850
3851 return ret;
3852}
3853
3854/*
3855 * Lock or unlock an item. "deep" is nr of levels to go.
3856 */
3857 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003858item_lock(typval_T *tv, int deep, int lock)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003859{
3860 static int recurse = 0;
3861 list_T *l;
3862 listitem_T *li;
3863 dict_T *d;
3864 hashitem_T *hi;
3865 int todo;
3866
3867 if (recurse >= DICT_MAXNEST)
3868 {
3869 EMSG(_("E743: variable nested too deep for (un)lock"));
3870 return;
3871 }
3872 if (deep == 0)
3873 return;
3874 ++recurse;
3875
3876 /* lock/unlock the item itself */
3877 if (lock)
3878 tv->v_lock |= VAR_LOCKED;
3879 else
3880 tv->v_lock &= ~VAR_LOCKED;
3881
3882 switch (tv->v_type)
3883 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01003884 case VAR_UNKNOWN:
3885 case VAR_NUMBER:
3886 case VAR_STRING:
3887 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01003888 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003889 case VAR_FLOAT:
3890 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01003891 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01003892 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01003893 break;
3894
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003895 case VAR_LIST:
3896 if ((l = tv->vval.v_list) != NULL)
3897 {
3898 if (lock)
3899 l->lv_lock |= VAR_LOCKED;
3900 else
3901 l->lv_lock &= ~VAR_LOCKED;
3902 if (deep < 0 || deep > 1)
3903 /* recursive: lock/unlock the items the List contains */
3904 for (li = l->lv_first; li != NULL; li = li->li_next)
3905 item_lock(&li->li_tv, deep - 1, lock);
3906 }
3907 break;
3908 case VAR_DICT:
3909 if ((d = tv->vval.v_dict) != NULL)
3910 {
3911 if (lock)
3912 d->dv_lock |= VAR_LOCKED;
3913 else
3914 d->dv_lock &= ~VAR_LOCKED;
3915 if (deep < 0 || deep > 1)
3916 {
3917 /* recursive: lock/unlock the items the List contains */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003918 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00003919 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
3920 {
3921 if (!HASHITEM_EMPTY(hi))
3922 {
3923 --todo;
3924 item_lock(&HI2DI(hi)->di_tv, deep - 1, lock);
3925 }
3926 }
3927 }
3928 }
3929 }
3930 --recurse;
3931}
3932
Bram Moolenaara40058a2005-07-11 22:42:07 +00003933/*
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +00003934 * Return TRUE if typeval "tv" is locked: Either that value is locked itself
3935 * or it refers to a List or Dictionary that is locked.
Bram Moolenaara40058a2005-07-11 22:42:07 +00003936 */
3937 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01003938tv_islocked(typval_T *tv)
Bram Moolenaara40058a2005-07-11 22:42:07 +00003939{
3940 return (tv->v_lock & VAR_LOCKED)
3941 || (tv->v_type == VAR_LIST
3942 && tv->vval.v_list != NULL
3943 && (tv->vval.v_list->lv_lock & VAR_LOCKED))
3944 || (tv->v_type == VAR_DICT
3945 && tv->vval.v_dict != NULL
3946 && (tv->vval.v_dict->dv_lock & VAR_LOCKED));
3947}
3948
Bram Moolenaar071d4272004-06-13 20:20:40 +00003949#if (defined(FEAT_MENU) && defined(FEAT_MULTI_LANG)) || defined(PROTO)
3950/*
3951 * Delete all "menutrans_" variables.
3952 */
3953 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01003954del_menutrans_vars(void)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003955{
Bram Moolenaar33570922005-01-25 22:26:29 +00003956 hashitem_T *hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00003957 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003958
Bram Moolenaar33570922005-01-25 22:26:29 +00003959 hash_lock(&globvarht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003960 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00003961 for (hi = globvarht.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaara7043832005-01-21 11:56:39 +00003962 {
3963 if (!HASHITEM_EMPTY(hi))
3964 {
3965 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00003966 if (STRNCMP(HI2DI(hi)->di_key, "menutrans_", 10) == 0)
3967 delete_var(&globvarht, hi);
Bram Moolenaara7043832005-01-21 11:56:39 +00003968 }
3969 }
Bram Moolenaar33570922005-01-25 22:26:29 +00003970 hash_unlock(&globvarht);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003971}
3972#endif
3973
3974#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
3975
3976/*
3977 * Local string buffer for the next two functions to store a variable name
3978 * with its prefix. Allocated in cat_prefix_varname(), freed later in
3979 * get_user_var_name().
3980 */
3981
Bram Moolenaar48e697e2016-01-23 22:17:30 +01003982static char_u *cat_prefix_varname(int prefix, char_u *name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003983
3984static char_u *varnamebuf = NULL;
3985static int varnamebuflen = 0;
3986
3987/*
3988 * Function to concatenate a prefix and a variable name.
3989 */
3990 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01003991cat_prefix_varname(int prefix, char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003992{
3993 int len;
3994
3995 len = (int)STRLEN(name) + 3;
3996 if (len > varnamebuflen)
3997 {
3998 vim_free(varnamebuf);
3999 len += 10; /* some additional space */
4000 varnamebuf = alloc(len);
4001 if (varnamebuf == NULL)
4002 {
4003 varnamebuflen = 0;
4004 return NULL;
4005 }
4006 varnamebuflen = len;
4007 }
4008 *varnamebuf = prefix;
4009 varnamebuf[1] = ':';
4010 STRCPY(varnamebuf + 2, name);
4011 return varnamebuf;
4012}
4013
4014/*
4015 * Function given to ExpandGeneric() to obtain the list of user defined
4016 * (global/buffer/window/built-in) variable names.
4017 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01004019get_user_var_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004020{
Bram Moolenaar532c7802005-01-27 14:44:31 +00004021 static long_u gdone;
4022 static long_u bdone;
4023 static long_u wdone;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004024#ifdef FEAT_WINDOWS
4025 static long_u tdone;
4026#endif
Bram Moolenaar532c7802005-01-27 14:44:31 +00004027 static int vidx;
4028 static hashitem_T *hi;
4029 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004030
4031 if (idx == 0)
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004032 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004033 gdone = bdone = wdone = vidx = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004034#ifdef FEAT_WINDOWS
4035 tdone = 0;
4036#endif
4037 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004038
4039 /* Global variables */
4040 if (gdone < globvarht.ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004041 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004042 if (gdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004043 hi = globvarht.ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004044 else
4045 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004046 while (HASHITEM_EMPTY(hi))
4047 ++hi;
4048 if (STRNCMP("g:", xp->xp_pattern, 2) == 0)
4049 return cat_prefix_varname('g', hi->hi_key);
4050 return hi->hi_key;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004051 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004052
4053 /* b: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004054 ht = &curbuf->b_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004055 if (bdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004056 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004057 if (bdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004058 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004059 else
4060 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004061 while (HASHITEM_EMPTY(hi))
4062 ++hi;
4063 return cat_prefix_varname('b', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004064 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004065 if (bdone == ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004066 {
Bram Moolenaara7043832005-01-21 11:56:39 +00004067 ++bdone;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004068 return (char_u *)"b:changedtick";
4069 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004070
4071 /* w: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004072 ht = &curwin->w_vars->dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +00004073 if (wdone < ht->ht_used)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004074 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00004075 if (wdone++ == 0)
Bram Moolenaar33570922005-01-25 22:26:29 +00004076 hi = ht->ht_array;
Bram Moolenaar532c7802005-01-27 14:44:31 +00004077 else
4078 ++hi;
Bram Moolenaara7043832005-01-21 11:56:39 +00004079 while (HASHITEM_EMPTY(hi))
4080 ++hi;
4081 return cat_prefix_varname('w', hi->hi_key);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004082 }
Bram Moolenaar33570922005-01-25 22:26:29 +00004083
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004084#ifdef FEAT_WINDOWS
4085 /* t: variables */
Bram Moolenaar429fa852013-04-15 12:27:36 +02004086 ht = &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00004087 if (tdone < ht->ht_used)
4088 {
4089 if (tdone++ == 0)
4090 hi = ht->ht_array;
4091 else
4092 ++hi;
4093 while (HASHITEM_EMPTY(hi))
4094 ++hi;
4095 return cat_prefix_varname('t', hi->hi_key);
4096 }
4097#endif
4098
Bram Moolenaar33570922005-01-25 22:26:29 +00004099 /* v: variables */
4100 if (vidx < VV_LEN)
4101 return cat_prefix_varname('v', (char_u *)vimvars[vidx++].vv_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004102
4103 vim_free(varnamebuf);
4104 varnamebuf = NULL;
4105 varnamebuflen = 0;
4106 return NULL;
4107}
4108
4109#endif /* FEAT_CMDL_COMPL */
4110
4111/*
4112 * types for expressions.
4113 */
4114typedef enum
4115{
4116 TYPE_UNKNOWN = 0
4117 , TYPE_EQUAL /* == */
4118 , TYPE_NEQUAL /* != */
4119 , TYPE_GREATER /* > */
4120 , TYPE_GEQUAL /* >= */
4121 , TYPE_SMALLER /* < */
4122 , TYPE_SEQUAL /* <= */
4123 , TYPE_MATCH /* =~ */
4124 , TYPE_NOMATCH /* !~ */
4125} exptype_T;
4126
4127/*
4128 * The "evaluate" argument: When FALSE, the argument is only parsed but not
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004129 * executed. The function may return OK, but the rettv will be of type
Bram Moolenaar071d4272004-06-13 20:20:40 +00004130 * VAR_UNKNOWN. The function still returns FAIL for a syntax error.
4131 */
4132
4133/*
4134 * Handle zero level expression.
4135 * This calls eval1() and handles error message and nextcmd.
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004136 * Put the result in "rettv" when returning OK and "evaluate" is TRUE.
Bram Moolenaar4463f292005-09-25 22:20:24 +00004137 * Note: "rettv.v_lock" is not set.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138 * Return OK or FAIL.
4139 */
4140 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004141eval0(
4142 char_u *arg,
4143 typval_T *rettv,
4144 char_u **nextcmd,
4145 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004146{
4147 int ret;
4148 char_u *p;
4149
4150 p = skipwhite(arg);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004151 ret = eval1(&p, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004152 if (ret == FAIL || !ends_excmd(*p))
4153 {
4154 if (ret != FAIL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004155 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004156 /*
4157 * Report the invalid expression unless the expression evaluation has
4158 * been cancelled due to an aborting error, an interrupt, or an
4159 * exception.
4160 */
4161 if (!aborting())
4162 EMSG2(_(e_invexpr2), arg);
4163 ret = FAIL;
4164 }
4165 if (nextcmd != NULL)
4166 *nextcmd = check_nextcmd(p);
4167
4168 return ret;
4169}
4170
4171/*
4172 * Handle top level expression:
Bram Moolenaarb67cc162009-02-04 15:27:06 +00004173 * expr2 ? expr1 : expr1
Bram Moolenaar071d4272004-06-13 20:20:40 +00004174 *
4175 * "arg" must point to the first non-white of the expression.
4176 * "arg" is advanced to the next non-white after the recognized expression.
4177 *
Bram Moolenaar4463f292005-09-25 22:20:24 +00004178 * Note: "rettv.v_lock" is not set.
4179 *
Bram Moolenaar071d4272004-06-13 20:20:40 +00004180 * Return OK or FAIL.
4181 */
4182 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004183eval1(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004184{
4185 int result;
Bram Moolenaar33570922005-01-25 22:26:29 +00004186 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004187
4188 /*
4189 * Get the first variable.
4190 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004191 if (eval2(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004192 return FAIL;
4193
4194 if ((*arg)[0] == '?')
4195 {
4196 result = FALSE;
4197 if (evaluate)
4198 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004199 int error = FALSE;
4200
4201 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004202 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004203 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004204 if (error)
4205 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004206 }
4207
4208 /*
4209 * Get the second variable.
4210 */
4211 *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004212 if (eval1(arg, rettv, evaluate && result) == FAIL) /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004213 return FAIL;
4214
4215 /*
4216 * Check for the ":".
4217 */
4218 if ((*arg)[0] != ':')
4219 {
4220 EMSG(_("E109: Missing ':' after '?'"));
4221 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004222 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004223 return FAIL;
4224 }
4225
4226 /*
4227 * Get the third variable.
4228 */
4229 *arg = skipwhite(*arg + 1);
4230 if (eval1(arg, &var2, evaluate && !result) == FAIL) /* recursive! */
4231 {
4232 if (evaluate && result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004233 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004234 return FAIL;
4235 }
4236 if (evaluate && !result)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004237 *rettv = var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004238 }
4239
4240 return OK;
4241}
4242
4243/*
4244 * Handle first level expression:
4245 * expr2 || expr2 || expr2 logical OR
4246 *
4247 * "arg" must point to the first non-white of the expression.
4248 * "arg" is advanced to the next non-white after the recognized expression.
4249 *
4250 * Return OK or FAIL.
4251 */
4252 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004253eval2(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004254{
Bram Moolenaar33570922005-01-25 22:26:29 +00004255 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004256 long result;
4257 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004258 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004259
4260 /*
4261 * Get the first variable.
4262 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004263 if (eval3(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004264 return FAIL;
4265
4266 /*
4267 * Repeat until there is no following "||".
4268 */
4269 first = TRUE;
4270 result = FALSE;
4271 while ((*arg)[0] == '|' && (*arg)[1] == '|')
4272 {
4273 if (evaluate && first)
4274 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004275 if (get_tv_number_chk(rettv, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004276 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004277 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004278 if (error)
4279 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004280 first = FALSE;
4281 }
4282
4283 /*
4284 * Get the second variable.
4285 */
4286 *arg = skipwhite(*arg + 2);
4287 if (eval3(arg, &var2, evaluate && !result) == FAIL)
4288 return FAIL;
4289
4290 /*
4291 * Compute the result.
4292 */
4293 if (evaluate && !result)
4294 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004295 if (get_tv_number_chk(&var2, &error) != 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 result = TRUE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004297 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004298 if (error)
4299 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004300 }
4301 if (evaluate)
4302 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004303 rettv->v_type = VAR_NUMBER;
4304 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004305 }
4306 }
4307
4308 return OK;
4309}
4310
4311/*
4312 * Handle second level expression:
4313 * expr3 && expr3 && expr3 logical AND
4314 *
4315 * "arg" must point to the first non-white of the expression.
4316 * "arg" is advanced to the next non-white after the recognized expression.
4317 *
4318 * Return OK or FAIL.
4319 */
4320 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004321eval3(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004322{
Bram Moolenaar33570922005-01-25 22:26:29 +00004323 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004324 long result;
4325 int first;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004326 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004327
4328 /*
4329 * Get the first variable.
4330 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004331 if (eval4(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004332 return FAIL;
4333
4334 /*
4335 * Repeat until there is no following "&&".
4336 */
4337 first = TRUE;
4338 result = TRUE;
4339 while ((*arg)[0] == '&' && (*arg)[1] == '&')
4340 {
4341 if (evaluate && first)
4342 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004343 if (get_tv_number_chk(rettv, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004344 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004345 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004346 if (error)
4347 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004348 first = FALSE;
4349 }
4350
4351 /*
4352 * Get the second variable.
4353 */
4354 *arg = skipwhite(*arg + 2);
4355 if (eval4(arg, &var2, evaluate && result) == FAIL)
4356 return FAIL;
4357
4358 /*
4359 * Compute the result.
4360 */
4361 if (evaluate && result)
4362 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004363 if (get_tv_number_chk(&var2, &error) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004364 result = FALSE;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004365 clear_tv(&var2);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004366 if (error)
4367 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004368 }
4369 if (evaluate)
4370 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004371 rettv->v_type = VAR_NUMBER;
4372 rettv->vval.v_number = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373 }
4374 }
4375
4376 return OK;
4377}
4378
4379/*
4380 * Handle third level expression:
4381 * var1 == var2
4382 * var1 =~ var2
4383 * var1 != var2
4384 * var1 !~ var2
4385 * var1 > var2
4386 * var1 >= var2
4387 * var1 < var2
4388 * var1 <= var2
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004389 * var1 is var2
4390 * var1 isnot var2
Bram Moolenaar071d4272004-06-13 20:20:40 +00004391 *
4392 * "arg" must point to the first non-white of the expression.
4393 * "arg" is advanced to the next non-white after the recognized expression.
4394 *
4395 * Return OK or FAIL.
4396 */
4397 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004398eval4(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004399{
Bram Moolenaar33570922005-01-25 22:26:29 +00004400 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004401 char_u *p;
4402 int i;
4403 exptype_T type = TYPE_UNKNOWN;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004404 int type_is = FALSE; /* TRUE for "is" and "isnot" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004405 int len = 2;
4406 long n1, n2;
4407 char_u *s1, *s2;
4408 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4409 regmatch_T regmatch;
4410 int ic;
4411 char_u *save_cpo;
4412
4413 /*
4414 * Get the first variable.
4415 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004416 if (eval5(arg, rettv, evaluate) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417 return FAIL;
4418
4419 p = *arg;
4420 switch (p[0])
4421 {
4422 case '=': if (p[1] == '=')
4423 type = TYPE_EQUAL;
4424 else if (p[1] == '~')
4425 type = TYPE_MATCH;
4426 break;
4427 case '!': if (p[1] == '=')
4428 type = TYPE_NEQUAL;
4429 else if (p[1] == '~')
4430 type = TYPE_NOMATCH;
4431 break;
4432 case '>': if (p[1] != '=')
4433 {
4434 type = TYPE_GREATER;
4435 len = 1;
4436 }
4437 else
4438 type = TYPE_GEQUAL;
4439 break;
4440 case '<': if (p[1] != '=')
4441 {
4442 type = TYPE_SMALLER;
4443 len = 1;
4444 }
4445 else
4446 type = TYPE_SEQUAL;
4447 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004448 case 'i': if (p[1] == 's')
4449 {
4450 if (p[2] == 'n' && p[3] == 'o' && p[4] == 't')
4451 len = 5;
Bram Moolenaar37a8de12015-09-01 16:05:00 +02004452 i = p[len];
4453 if (!isalnum(i) && i != '_')
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004454 {
4455 type = len == 2 ? TYPE_EQUAL : TYPE_NEQUAL;
4456 type_is = TRUE;
4457 }
4458 }
4459 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004460 }
4461
4462 /*
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004463 * If there is a comparative operator, use it.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004464 */
4465 if (type != TYPE_UNKNOWN)
4466 {
4467 /* extra question mark appended: ignore case */
4468 if (p[len] == '?')
4469 {
4470 ic = TRUE;
4471 ++len;
4472 }
4473 /* extra '#' appended: match case */
4474 else if (p[len] == '#')
4475 {
4476 ic = FALSE;
4477 ++len;
4478 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004479 /* nothing appended: use 'ignorecase' */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004480 else
4481 ic = p_ic;
4482
4483 /*
4484 * Get the second variable.
4485 */
4486 *arg = skipwhite(p + len);
4487 if (eval5(arg, &var2, evaluate) == FAIL)
4488 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004489 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004490 return FAIL;
4491 }
4492
4493 if (evaluate)
4494 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004495 if (type_is && rettv->v_type != var2.v_type)
4496 {
4497 /* For "is" a different type always means FALSE, for "notis"
4498 * it means TRUE. */
4499 n1 = (type == TYPE_NEQUAL);
4500 }
4501 else if (rettv->v_type == VAR_LIST || var2.v_type == VAR_LIST)
4502 {
4503 if (type_is)
4504 {
4505 n1 = (rettv->v_type == var2.v_type
4506 && rettv->vval.v_list == var2.vval.v_list);
4507 if (type == TYPE_NEQUAL)
4508 n1 = !n1;
4509 }
4510 else if (rettv->v_type != var2.v_type
4511 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4512 {
4513 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004514 EMSG(_("E691: Can only compare List with List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004515 else
Bram Moolenaar59838522014-05-13 13:46:33 +02004516 EMSG(_("E692: Invalid operation for List"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004517 clear_tv(rettv);
4518 clear_tv(&var2);
4519 return FAIL;
4520 }
4521 else
4522 {
4523 /* Compare two Lists for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004524 n1 = list_equal(rettv->vval.v_list, var2.vval.v_list,
4525 ic, FALSE);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004526 if (type == TYPE_NEQUAL)
4527 n1 = !n1;
4528 }
4529 }
4530
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004531 else if (rettv->v_type == VAR_DICT || var2.v_type == VAR_DICT)
4532 {
4533 if (type_is)
4534 {
4535 n1 = (rettv->v_type == var2.v_type
4536 && rettv->vval.v_dict == var2.vval.v_dict);
4537 if (type == TYPE_NEQUAL)
4538 n1 = !n1;
4539 }
4540 else if (rettv->v_type != var2.v_type
4541 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4542 {
4543 if (rettv->v_type != var2.v_type)
4544 EMSG(_("E735: Can only compare Dictionary with Dictionary"));
4545 else
4546 EMSG(_("E736: Invalid operation for Dictionary"));
4547 clear_tv(rettv);
4548 clear_tv(&var2);
4549 return FAIL;
4550 }
4551 else
4552 {
4553 /* Compare two Dictionaries for being equal or unequal. */
Bram Moolenaar67b3f992010-11-10 20:41:57 +01004554 n1 = dict_equal(rettv->vval.v_dict, var2.vval.v_dict,
4555 ic, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004556 if (type == TYPE_NEQUAL)
4557 n1 = !n1;
4558 }
4559 }
4560
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004561 else if (rettv->v_type == VAR_FUNC || var2.v_type == VAR_FUNC
4562 || rettv->v_type == VAR_PARTIAL || var2.v_type == VAR_PARTIAL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004563 {
4564 if (rettv->v_type != var2.v_type
4565 || (type != TYPE_EQUAL && type != TYPE_NEQUAL))
4566 {
4567 if (rettv->v_type != var2.v_type)
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004568 EMSG(_("E693: Can only compare Funcref with Funcref"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004569 else
Bram Moolenaare49b69a2005-01-08 16:11:57 +00004570 EMSG(_("E694: Invalid operation for Funcrefs"));
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004571 clear_tv(rettv);
4572 clear_tv(&var2);
4573 return FAIL;
4574 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004575 else if (rettv->v_type == VAR_PARTIAL)
4576 {
4577 /* Partials are only equal when identical. */
4578 n1 = rettv->vval.v_partial != NULL
4579 && rettv->vval.v_partial == var2.vval.v_partial;
4580 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004581 else
4582 {
4583 /* Compare two Funcrefs for being equal or unequal. */
4584 if (rettv->vval.v_string == NULL
4585 || var2.vval.v_string == NULL)
4586 n1 = FALSE;
4587 else
4588 n1 = STRCMP(rettv->vval.v_string,
4589 var2.vval.v_string) == 0;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004590 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +01004591 if (type == TYPE_NEQUAL)
4592 n1 = !n1;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004593 }
4594
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004595#ifdef FEAT_FLOAT
4596 /*
4597 * If one of the two variables is a float, compare as a float.
4598 * When using "=~" or "!~", always compare as string.
4599 */
4600 else if ((rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4601 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4602 {
4603 float_T f1, f2;
4604
4605 if (rettv->v_type == VAR_FLOAT)
4606 f1 = rettv->vval.v_float;
4607 else
4608 f1 = get_tv_number(rettv);
4609 if (var2.v_type == VAR_FLOAT)
4610 f2 = var2.vval.v_float;
4611 else
4612 f2 = get_tv_number(&var2);
4613 n1 = FALSE;
4614 switch (type)
4615 {
4616 case TYPE_EQUAL: n1 = (f1 == f2); break;
4617 case TYPE_NEQUAL: n1 = (f1 != f2); break;
4618 case TYPE_GREATER: n1 = (f1 > f2); break;
4619 case TYPE_GEQUAL: n1 = (f1 >= f2); break;
4620 case TYPE_SMALLER: n1 = (f1 < f2); break;
4621 case TYPE_SEQUAL: n1 = (f1 <= f2); break;
4622 case TYPE_UNKNOWN:
4623 case TYPE_MATCH:
4624 case TYPE_NOMATCH: break; /* avoid gcc warning */
4625 }
4626 }
4627#endif
4628
Bram Moolenaar071d4272004-06-13 20:20:40 +00004629 /*
4630 * If one of the two variables is a number, compare as a number.
4631 * When using "=~" or "!~", always compare as string.
4632 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004633 else if ((rettv->v_type == VAR_NUMBER || var2.v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004634 && type != TYPE_MATCH && type != TYPE_NOMATCH)
4635 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004636 n1 = get_tv_number(rettv);
4637 n2 = get_tv_number(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004638 switch (type)
4639 {
4640 case TYPE_EQUAL: n1 = (n1 == n2); break;
4641 case TYPE_NEQUAL: n1 = (n1 != n2); break;
4642 case TYPE_GREATER: n1 = (n1 > n2); break;
4643 case TYPE_GEQUAL: n1 = (n1 >= n2); break;
4644 case TYPE_SMALLER: n1 = (n1 < n2); break;
4645 case TYPE_SEQUAL: n1 = (n1 <= n2); break;
4646 case TYPE_UNKNOWN:
4647 case TYPE_MATCH:
4648 case TYPE_NOMATCH: break; /* avoid gcc warning */
4649 }
4650 }
4651 else
4652 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004653 s1 = get_tv_string_buf(rettv, buf1);
4654 s2 = get_tv_string_buf(&var2, buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004655 if (type != TYPE_MATCH && type != TYPE_NOMATCH)
4656 i = ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2);
4657 else
4658 i = 0;
4659 n1 = FALSE;
4660 switch (type)
4661 {
4662 case TYPE_EQUAL: n1 = (i == 0); break;
4663 case TYPE_NEQUAL: n1 = (i != 0); break;
4664 case TYPE_GREATER: n1 = (i > 0); break;
4665 case TYPE_GEQUAL: n1 = (i >= 0); break;
4666 case TYPE_SMALLER: n1 = (i < 0); break;
4667 case TYPE_SEQUAL: n1 = (i <= 0); break;
4668
4669 case TYPE_MATCH:
4670 case TYPE_NOMATCH:
4671 /* avoid 'l' flag in 'cpoptions' */
4672 save_cpo = p_cpo;
4673 p_cpo = (char_u *)"";
4674 regmatch.regprog = vim_regcomp(s2,
4675 RE_MAGIC + RE_STRING);
4676 regmatch.rm_ic = ic;
4677 if (regmatch.regprog != NULL)
4678 {
4679 n1 = vim_regexec_nl(&regmatch, s1, (colnr_T)0);
Bram Moolenaar473de612013-06-08 18:19:48 +02004680 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681 if (type == TYPE_NOMATCH)
4682 n1 = !n1;
4683 }
4684 p_cpo = save_cpo;
4685 break;
4686
4687 case TYPE_UNKNOWN: break; /* avoid gcc warning */
4688 }
4689 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004690 clear_tv(rettv);
4691 clear_tv(&var2);
4692 rettv->v_type = VAR_NUMBER;
4693 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004694 }
4695 }
4696
4697 return OK;
4698}
4699
4700/*
4701 * Handle fourth level expression:
4702 * + number addition
4703 * - number subtraction
4704 * . string concatenation
4705 *
4706 * "arg" must point to the first non-white of the expression.
4707 * "arg" is advanced to the next non-white after the recognized expression.
4708 *
4709 * Return OK or FAIL.
4710 */
4711 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004712eval5(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004713{
Bram Moolenaar33570922005-01-25 22:26:29 +00004714 typval_T var2;
4715 typval_T var3;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004716 int op;
4717 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004718#ifdef FEAT_FLOAT
4719 float_T f1 = 0, f2 = 0;
4720#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721 char_u *s1, *s2;
4722 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
4723 char_u *p;
4724
4725 /*
4726 * Get the first variable.
4727 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004728 if (eval6(arg, rettv, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 return FAIL;
4730
4731 /*
4732 * Repeat computing, until no '+', '-' or '.' is following.
4733 */
4734 for (;;)
4735 {
4736 op = **arg;
4737 if (op != '+' && op != '-' && op != '.')
4738 break;
4739
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004740 if ((op != '+' || rettv->v_type != VAR_LIST)
4741#ifdef FEAT_FLOAT
4742 && (op == '.' || rettv->v_type != VAR_FLOAT)
4743#endif
4744 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004745 {
4746 /* For "list + ...", an illegal use of the first operand as
4747 * a number cannot be determined before evaluating the 2nd
4748 * operand: if this is also a list, all is ok.
4749 * For "something . ...", "something - ..." or "non-list + ...",
4750 * we know that the first operand needs to be a string or number
4751 * without evaluating the 2nd operand. So check before to avoid
4752 * side effects after an error. */
4753 if (evaluate && get_tv_string_chk(rettv) == NULL)
4754 {
4755 clear_tv(rettv);
4756 return FAIL;
4757 }
4758 }
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 /*
4761 * Get the second variable.
4762 */
4763 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004764 if (eval6(arg, &var2, evaluate, op == '.') == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004765 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004766 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004767 return FAIL;
4768 }
4769
4770 if (evaluate)
4771 {
4772 /*
4773 * Compute the result.
4774 */
4775 if (op == '.')
4776 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004777 s1 = get_tv_string_buf(rettv, buf1); /* already checked */
4778 s2 = get_tv_string_buf_chk(&var2, buf2);
4779 if (s2 == NULL) /* type error ? */
4780 {
4781 clear_tv(rettv);
4782 clear_tv(&var2);
4783 return FAIL;
4784 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00004785 p = concat_str(s1, s2);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004786 clear_tv(rettv);
4787 rettv->v_type = VAR_STRING;
4788 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00004790 else if (op == '+' && rettv->v_type == VAR_LIST
4791 && var2.v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00004792 {
4793 /* concatenate Lists */
4794 if (list_concat(rettv->vval.v_list, var2.vval.v_list,
4795 &var3) == FAIL)
4796 {
4797 clear_tv(rettv);
4798 clear_tv(&var2);
4799 return FAIL;
4800 }
4801 clear_tv(rettv);
4802 *rettv = var3;
4803 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004804 else
4805 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004806 int error = FALSE;
4807
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004808#ifdef FEAT_FLOAT
4809 if (rettv->v_type == VAR_FLOAT)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004810 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004811 f1 = rettv->vval.v_float;
4812 n1 = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004813 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004814 else
4815#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004816 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004817 n1 = get_tv_number_chk(rettv, &error);
4818 if (error)
4819 {
4820 /* This can only happen for "list + non-list". For
4821 * "non-list + ..." or "something - ...", we returned
4822 * before evaluating the 2nd operand. */
4823 clear_tv(rettv);
4824 return FAIL;
4825 }
4826#ifdef FEAT_FLOAT
4827 if (var2.v_type == VAR_FLOAT)
4828 f1 = n1;
4829#endif
4830 }
4831#ifdef FEAT_FLOAT
4832 if (var2.v_type == VAR_FLOAT)
4833 {
4834 f2 = var2.vval.v_float;
4835 n2 = 0;
4836 }
4837 else
4838#endif
4839 {
4840 n2 = get_tv_number_chk(&var2, &error);
4841 if (error)
4842 {
4843 clear_tv(rettv);
4844 clear_tv(&var2);
4845 return FAIL;
4846 }
4847#ifdef FEAT_FLOAT
4848 if (rettv->v_type == VAR_FLOAT)
4849 f2 = n2;
4850#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004852 clear_tv(rettv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004853
4854#ifdef FEAT_FLOAT
4855 /* If there is a float on either side the result is a float. */
4856 if (rettv->v_type == VAR_FLOAT || var2.v_type == VAR_FLOAT)
4857 {
4858 if (op == '+')
4859 f1 = f1 + f2;
4860 else
4861 f1 = f1 - f2;
4862 rettv->v_type = VAR_FLOAT;
4863 rettv->vval.v_float = f1;
4864 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004866#endif
4867 {
4868 if (op == '+')
4869 n1 = n1 + n2;
4870 else
4871 n1 = n1 - n2;
4872 rettv->v_type = VAR_NUMBER;
4873 rettv->vval.v_number = n1;
4874 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004876 clear_tv(&var2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004877 }
4878 }
4879 return OK;
4880}
4881
4882/*
4883 * Handle fifth level expression:
4884 * * number multiplication
4885 * / number division
4886 * % number modulo
4887 *
4888 * "arg" must point to the first non-white of the expression.
4889 * "arg" is advanced to the next non-white after the recognized expression.
4890 *
4891 * Return OK or FAIL.
4892 */
4893 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01004894eval6(
4895 char_u **arg,
4896 typval_T *rettv,
4897 int evaluate,
4898 int want_string) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004899{
Bram Moolenaar33570922005-01-25 22:26:29 +00004900 typval_T var2;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004901 int op;
4902 long n1, n2;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004903#ifdef FEAT_FLOAT
4904 int use_float = FALSE;
4905 float_T f1 = 0, f2;
4906#endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004907 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004908
4909 /*
4910 * Get the first variable.
4911 */
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004912 if (eval7(arg, rettv, evaluate, want_string) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004913 return FAIL;
4914
4915 /*
4916 * Repeat computing, until no '*', '/' or '%' is following.
4917 */
4918 for (;;)
4919 {
4920 op = **arg;
4921 if (op != '*' && op != '/' && op != '%')
4922 break;
4923
4924 if (evaluate)
4925 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004926#ifdef FEAT_FLOAT
4927 if (rettv->v_type == VAR_FLOAT)
4928 {
4929 f1 = rettv->vval.v_float;
4930 use_float = TRUE;
4931 n1 = 0;
4932 }
4933 else
4934#endif
4935 n1 = get_tv_number_chk(rettv, &error);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00004936 clear_tv(rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00004937 if (error)
4938 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004939 }
4940 else
4941 n1 = 0;
4942
4943 /*
4944 * Get the second variable.
4945 */
4946 *arg = skipwhite(*arg + 1);
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00004947 if (eval7(arg, &var2, evaluate, FALSE) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004948 return FAIL;
4949
4950 if (evaluate)
4951 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004952#ifdef FEAT_FLOAT
4953 if (var2.v_type == VAR_FLOAT)
4954 {
4955 if (!use_float)
4956 {
4957 f1 = n1;
4958 use_float = TRUE;
4959 }
4960 f2 = var2.vval.v_float;
4961 n2 = 0;
4962 }
4963 else
4964#endif
4965 {
4966 n2 = get_tv_number_chk(&var2, &error);
4967 clear_tv(&var2);
4968 if (error)
4969 return FAIL;
4970#ifdef FEAT_FLOAT
4971 if (use_float)
4972 f2 = n2;
4973#endif
4974 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00004975
4976 /*
4977 * Compute the result.
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004978 * When either side is a float the result is a float.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004979 */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004980#ifdef FEAT_FLOAT
4981 if (use_float)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004982 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00004983 if (op == '*')
4984 f1 = f1 * f2;
4985 else if (op == '/')
4986 {
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004987# ifdef VMS
4988 /* VMS crashes on divide by zero, work around it */
4989 if (f2 == 0.0)
4990 {
4991 if (f1 == 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004992 f1 = -1 * __F_FLT_MAX - 1L; /* similar to NaN */
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004993 else if (f1 < 0)
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004994 f1 = -1 * __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004995 else
Bram Moolenaar314f11d2010-08-09 22:07:08 +02004996 f1 = __F_FLT_MAX;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02004997 }
4998 else
4999 f1 = f1 / f2;
5000# else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005001 /* We rely on the floating point library to handle divide
5002 * by zero to result in "inf" and not a crash. */
5003 f1 = f1 / f2;
Bram Moolenaarf878bcf2010-07-30 22:29:41 +02005004# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005005 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005006 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005007 {
Bram Moolenaar1378fca2008-07-04 16:51:55 +00005008 EMSG(_("E804: Cannot use '%' with Float"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005009 return FAIL;
5010 }
5011 rettv->v_type = VAR_FLOAT;
5012 rettv->vval.v_float = f1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005013 }
5014 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005015#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005017 if (op == '*')
5018 n1 = n1 * n2;
5019 else if (op == '/')
5020 {
5021 if (n2 == 0) /* give an error message? */
5022 {
5023 if (n1 == 0)
5024 n1 = -0x7fffffffL - 1L; /* similar to NaN */
5025 else if (n1 < 0)
5026 n1 = -0x7fffffffL;
5027 else
5028 n1 = 0x7fffffffL;
5029 }
5030 else
5031 n1 = n1 / n2;
5032 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005033 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005034 {
5035 if (n2 == 0) /* give an error message? */
5036 n1 = 0;
5037 else
5038 n1 = n1 % n2;
5039 }
5040 rettv->v_type = VAR_NUMBER;
5041 rettv->vval.v_number = n1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005042 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 }
5044 }
5045
5046 return OK;
5047}
5048
5049/*
5050 * Handle sixth level expression:
5051 * number number constant
Bram Moolenaarbae0c162007-05-10 19:30:25 +00005052 * "string" string constant
5053 * 'string' literal string constant
Bram Moolenaar071d4272004-06-13 20:20:40 +00005054 * &option-name option value
5055 * @r register contents
5056 * identifier variable value
5057 * function() function call
5058 * $VAR environment variable
5059 * (expression) nested expression
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005060 * [expr, expr] List
5061 * {key: val, key: val} Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062 *
5063 * Also handle:
5064 * ! in front logical NOT
5065 * - in front unary minus
5066 * + in front unary plus (ignored)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005067 * trailing [] subscript in String or List
5068 * trailing .name entry in Dictionary
Bram Moolenaar071d4272004-06-13 20:20:40 +00005069 *
5070 * "arg" must point to the first non-white of the expression.
5071 * "arg" is advanced to the next non-white after the recognized expression.
5072 *
5073 * Return OK or FAIL.
5074 */
5075 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005076eval7(
5077 char_u **arg,
5078 typval_T *rettv,
5079 int evaluate,
5080 int want_string UNUSED) /* after "." operator */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005081{
Bram Moolenaar071d4272004-06-13 20:20:40 +00005082 long n;
5083 int len;
5084 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005085 char_u *start_leader, *end_leader;
5086 int ret = OK;
5087 char_u *alias;
5088
5089 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005090 * Initialise variable so that clear_tv() can't mistake this for a
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005091 * string and free a string that isn't there.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005092 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005093 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005094
5095 /*
5096 * Skip '!' and '-' characters. They are handled later.
5097 */
5098 start_leader = *arg;
5099 while (**arg == '!' || **arg == '-' || **arg == '+')
5100 *arg = skipwhite(*arg + 1);
5101 end_leader = *arg;
5102
5103 switch (**arg)
5104 {
5105 /*
5106 * Number constant.
5107 */
5108 case '0':
5109 case '1':
5110 case '2':
5111 case '3':
5112 case '4':
5113 case '5':
5114 case '6':
5115 case '7':
5116 case '8':
5117 case '9':
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005118 {
5119#ifdef FEAT_FLOAT
5120 char_u *p = skipdigits(*arg + 1);
5121 int get_float = FALSE;
5122
5123 /* We accept a float when the format matches
5124 * "[0-9]\+\.[0-9]\+\([eE][+-]\?[0-9]\+\)\?". This is very
Bram Moolenaar8d00f9c2008-06-28 13:09:56 +00005125 * strict to avoid backwards compatibility problems.
5126 * Don't look for a float after the "." operator, so that
5127 * ":let vers = 1.2.3" doesn't fail. */
5128 if (!want_string && p[0] == '.' && vim_isdigit(p[1]))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005129 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005130 get_float = TRUE;
5131 p = skipdigits(p + 2);
5132 if (*p == 'e' || *p == 'E')
5133 {
5134 ++p;
5135 if (*p == '-' || *p == '+')
5136 ++p;
5137 if (!vim_isdigit(*p))
5138 get_float = FALSE;
5139 else
5140 p = skipdigits(p + 1);
5141 }
5142 if (ASCII_ISALPHA(*p) || *p == '.')
5143 get_float = FALSE;
5144 }
5145 if (get_float)
5146 {
5147 float_T f;
5148
5149 *arg += string2float(*arg, &f);
5150 if (evaluate)
5151 {
5152 rettv->v_type = VAR_FLOAT;
5153 rettv->vval.v_float = f;
5154 }
5155 }
5156 else
5157#endif
5158 {
Bram Moolenaar887c1fe2016-01-02 17:56:35 +01005159 vim_str2nr(*arg, NULL, &len, STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005160 *arg += len;
5161 if (evaluate)
5162 {
5163 rettv->v_type = VAR_NUMBER;
5164 rettv->vval.v_number = n;
5165 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005166 }
5167 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005169
5170 /*
5171 * String constant: "string".
5172 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005173 case '"': ret = get_string_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005174 break;
5175
5176 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005177 * Literal string constant: 'str''ing'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005179 case '\'': ret = get_lit_string_tv(arg, rettv, evaluate);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005180 break;
5181
5182 /*
5183 * List: [expr, expr]
5184 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005185 case '[': ret = get_list_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005186 break;
5187
5188 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005189 * Dictionary: {key: val, key: val}
5190 */
5191 case '{': ret = get_dict_tv(arg, rettv, evaluate);
5192 break;
5193
5194 /*
Bram Moolenaare9a41262005-01-15 22:18:47 +00005195 * Option value: &name
Bram Moolenaar071d4272004-06-13 20:20:40 +00005196 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00005197 case '&': ret = get_option_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 break;
5199
5200 /*
5201 * Environment variable: $VAR.
5202 */
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005203 case '$': ret = get_env_tv(arg, rettv, evaluate);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005204 break;
5205
5206 /*
5207 * Register contents: @r.
5208 */
5209 case '@': ++*arg;
5210 if (evaluate)
5211 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005212 rettv->v_type = VAR_STRING;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02005213 rettv->vval.v_string = get_reg_contents(**arg,
5214 GREG_EXPR_SRC);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005215 }
5216 if (**arg != NUL)
5217 ++*arg;
5218 break;
5219
5220 /*
5221 * nested expression: (expression).
5222 */
5223 case '(': *arg = skipwhite(*arg + 1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005224 ret = eval1(arg, rettv, evaluate); /* recursive! */
Bram Moolenaar071d4272004-06-13 20:20:40 +00005225 if (**arg == ')')
5226 ++*arg;
5227 else if (ret == OK)
5228 {
5229 EMSG(_("E110: Missing ')'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005230 clear_tv(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005231 ret = FAIL;
5232 }
5233 break;
5234
Bram Moolenaar8c711452005-01-14 21:53:12 +00005235 default: ret = NOTDONE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005236 break;
5237 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005238
5239 if (ret == NOTDONE)
5240 {
5241 /*
5242 * Must be a variable or function name.
5243 * Can also be a curly-braces kind of name: {expr}.
5244 */
5245 s = *arg;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005246 len = get_name_len(arg, &alias, evaluate, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005247 if (alias != NULL)
5248 s = alias;
5249
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005250 if (len <= 0)
Bram Moolenaar8c711452005-01-14 21:53:12 +00005251 ret = FAIL;
5252 else
5253 {
5254 if (**arg == '(') /* recursive! */
5255 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005256 partial_T *partial;
5257
Bram Moolenaar8c711452005-01-14 21:53:12 +00005258 /* If "s" is the name of a variable of type VAR_FUNC
5259 * use its contents. */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005260 s = deref_func_name(s, &len, &partial, !evaluate);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005261
5262 /* Invoke the function. */
5263 ret = get_func_tv(s, len, rettv, arg,
5264 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005265 &len, evaluate, partial, NULL);
Bram Moolenaare17c2602013-02-26 19:36:15 +01005266
5267 /* If evaluate is FALSE rettv->v_type was not set in
5268 * get_func_tv, but it's needed in handle_subscript() to parse
5269 * what follows. So set it here. */
5270 if (rettv->v_type == VAR_UNKNOWN && !evaluate && **arg == '(')
5271 {
Bram Moolenaar988232f2013-02-26 21:43:32 +01005272 rettv->vval.v_string = vim_strsave((char_u *)"");
Bram Moolenaare17c2602013-02-26 19:36:15 +01005273 rettv->v_type = VAR_FUNC;
5274 }
5275
Bram Moolenaar8c711452005-01-14 21:53:12 +00005276 /* Stop the expression evaluation when immediately
5277 * aborting on error, or when an interrupt occurred or
5278 * an exception was thrown but not caught. */
5279 if (aborting())
5280 {
5281 if (ret == OK)
5282 clear_tv(rettv);
5283 ret = FAIL;
5284 }
5285 }
5286 else if (evaluate)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +02005287 ret = get_var_tv(s, len, rettv, NULL, TRUE, FALSE);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005288 else
5289 ret = OK;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005290 }
Bram Moolenaar3c2d6532011-02-01 13:48:53 +01005291 vim_free(alias);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005292 }
5293
Bram Moolenaar071d4272004-06-13 20:20:40 +00005294 *arg = skipwhite(*arg);
5295
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005296 /* Handle following '[', '(' and '.' for expr[expr], expr.name,
5297 * expr(expr). */
5298 if (ret == OK)
5299 ret = handle_subscript(arg, rettv, evaluate, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005300
5301 /*
5302 * Apply logical NOT and unary '-', from right to left, ignore '+'.
5303 */
5304 if (ret == OK && evaluate && end_leader > start_leader)
5305 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005306 int error = FALSE;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005307 int val = 0;
5308#ifdef FEAT_FLOAT
5309 float_T f = 0.0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005310
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005311 if (rettv->v_type == VAR_FLOAT)
5312 f = rettv->vval.v_float;
5313 else
5314#endif
5315 val = get_tv_number_chk(rettv, &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005316 if (error)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005317 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005318 clear_tv(rettv);
5319 ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005320 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005321 else
5322 {
5323 while (end_leader > start_leader)
5324 {
5325 --end_leader;
5326 if (*end_leader == '!')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005327 {
5328#ifdef FEAT_FLOAT
5329 if (rettv->v_type == VAR_FLOAT)
5330 f = !f;
5331 else
5332#endif
5333 val = !val;
5334 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005335 else if (*end_leader == '-')
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005336 {
5337#ifdef FEAT_FLOAT
5338 if (rettv->v_type == VAR_FLOAT)
5339 f = -f;
5340 else
5341#endif
5342 val = -val;
5343 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005344 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +00005345#ifdef FEAT_FLOAT
5346 if (rettv->v_type == VAR_FLOAT)
5347 {
5348 clear_tv(rettv);
5349 rettv->vval.v_float = f;
5350 }
5351 else
5352#endif
5353 {
5354 clear_tv(rettv);
5355 rettv->v_type = VAR_NUMBER;
5356 rettv->vval.v_number = val;
5357 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005358 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005359 }
5360
5361 return ret;
5362}
5363
5364/*
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005365 * Evaluate an "[expr]" or "[expr:expr]" index. Also "dict.key".
5366 * "*arg" points to the '[' or '.'.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005367 * Returns FAIL or OK. "*arg" is advanced to after the ']'.
5368 */
5369 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005370eval_index(
5371 char_u **arg,
5372 typval_T *rettv,
5373 int evaluate,
5374 int verbose) /* give error messages */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005375{
5376 int empty1 = FALSE, empty2 = FALSE;
Bram Moolenaar33570922005-01-25 22:26:29 +00005377 typval_T var1, var2;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005378 long n1, n2 = 0;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005379 long len = -1;
5380 int range = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005381 char_u *s;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005382 char_u *key = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005383
Bram Moolenaara03f2332016-02-06 18:09:59 +01005384 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005385 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01005386 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005387 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005388 if (verbose)
5389 EMSG(_("E695: Cannot index a Funcref"));
5390 return FAIL;
5391 case VAR_FLOAT:
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005392#ifdef FEAT_FLOAT
Bram Moolenaara03f2332016-02-06 18:09:59 +01005393 if (verbose)
5394 EMSG(_(e_float_as_string));
5395 return FAIL;
Bram Moolenaar2a876e42013-06-12 22:08:58 +02005396#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +01005397 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005398 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005399 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005400 if (verbose)
5401 EMSG(_("E909: Cannot index a special variable"));
5402 return FAIL;
5403 case VAR_UNKNOWN:
5404 if (evaluate)
5405 return FAIL;
5406 /* FALLTHROUGH */
5407
5408 case VAR_STRING:
5409 case VAR_NUMBER:
5410 case VAR_LIST:
5411 case VAR_DICT:
5412 break;
Bram Moolenaar520e1e42016-01-23 19:46:28 +01005413 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005414
Bram Moolenaar0a38dd22015-08-25 16:49:01 +02005415 init_tv(&var1);
5416 init_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005417 if (**arg == '.')
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005418 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005419 /*
5420 * dict.name
5421 */
5422 key = *arg + 1;
5423 for (len = 0; ASCII_ISALNUM(key[len]) || key[len] == '_'; ++len)
5424 ;
5425 if (len == 0)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005426 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005427 *arg = skipwhite(key + len);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005428 }
5429 else
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005430 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005431 /*
5432 * something[idx]
5433 *
5434 * Get the (first) variable from inside the [].
5435 */
5436 *arg = skipwhite(*arg + 1);
5437 if (**arg == ':')
5438 empty1 = TRUE;
5439 else if (eval1(arg, &var1, evaluate) == FAIL) /* recursive! */
5440 return FAIL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005441 else if (evaluate && get_tv_string_chk(&var1) == NULL)
5442 {
5443 /* not a number or string */
5444 clear_tv(&var1);
5445 return FAIL;
5446 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005447
5448 /*
5449 * Get the second variable from inside the [:].
5450 */
5451 if (**arg == ':')
5452 {
5453 range = TRUE;
5454 *arg = skipwhite(*arg + 1);
5455 if (**arg == ']')
5456 empty2 = TRUE;
5457 else if (eval1(arg, &var2, evaluate) == FAIL) /* recursive! */
5458 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00005459 if (!empty1)
5460 clear_tv(&var1);
5461 return FAIL;
5462 }
5463 else if (evaluate && get_tv_string_chk(&var2) == NULL)
5464 {
5465 /* not a number or string */
5466 if (!empty1)
5467 clear_tv(&var1);
5468 clear_tv(&var2);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005469 return FAIL;
5470 }
5471 }
5472
5473 /* Check for the ']'. */
5474 if (**arg != ']')
5475 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005476 if (verbose)
5477 EMSG(_(e_missbrac));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005478 clear_tv(&var1);
5479 if (range)
5480 clear_tv(&var2);
5481 return FAIL;
5482 }
5483 *arg = skipwhite(*arg + 1); /* skip the ']' */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005484 }
5485
5486 if (evaluate)
5487 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005488 n1 = 0;
5489 if (!empty1 && rettv->v_type != VAR_DICT)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005490 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005491 n1 = get_tv_number(&var1);
5492 clear_tv(&var1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005493 }
5494 if (range)
5495 {
5496 if (empty2)
5497 n2 = -1;
5498 else
5499 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005500 n2 = get_tv_number(&var2);
5501 clear_tv(&var2);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005502 }
5503 }
5504
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005505 switch (rettv->v_type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005506 {
Bram Moolenaar835dc632016-02-07 14:27:38 +01005507 case VAR_UNKNOWN:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005508 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +01005509 case VAR_PARTIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005510 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01005511 case VAR_SPECIAL:
5512 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01005513 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01005514 break; /* not evaluating, skipping over subscript */
5515
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005516 case VAR_NUMBER:
5517 case VAR_STRING:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005518 s = get_tv_string(rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005519 len = (long)STRLEN(s);
5520 if (range)
5521 {
5522 /* The resulting variable is a substring. If the indexes
5523 * are out of range the result is empty. */
5524 if (n1 < 0)
5525 {
5526 n1 = len + n1;
5527 if (n1 < 0)
5528 n1 = 0;
5529 }
5530 if (n2 < 0)
5531 n2 = len + n2;
5532 else if (n2 >= len)
5533 n2 = len;
5534 if (n1 >= len || n2 < 0 || n1 > n2)
5535 s = NULL;
5536 else
5537 s = vim_strnsave(s + n1, (int)(n2 - n1 + 1));
5538 }
5539 else
5540 {
5541 /* The resulting variable is a string of a single
5542 * character. If the index is too big or negative the
5543 * result is empty. */
5544 if (n1 >= len || n1 < 0)
5545 s = NULL;
5546 else
5547 s = vim_strnsave(s + n1, 1);
5548 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005549 clear_tv(rettv);
5550 rettv->v_type = VAR_STRING;
5551 rettv->vval.v_string = s;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005552 break;
5553
5554 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005555 len = list_len(rettv->vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005556 if (n1 < 0)
5557 n1 = len + n1;
5558 if (!empty1 && (n1 < 0 || n1 >= len))
5559 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005560 /* For a range we allow invalid values and return an empty
5561 * list. A list index out of range is an error. */
5562 if (!range)
5563 {
5564 if (verbose)
5565 EMSGN(_(e_listidx), n1);
5566 return FAIL;
5567 }
5568 n1 = len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005569 }
5570 if (range)
5571 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005572 list_T *l;
5573 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005574
5575 if (n2 < 0)
5576 n2 = len + n2;
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00005577 else if (n2 >= len)
5578 n2 = len - 1;
5579 if (!empty2 && (n2 < 0 || n2 + 1 < n1))
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005580 n2 = -1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005581 l = list_alloc();
5582 if (l == NULL)
5583 return FAIL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005584 for (item = list_find(rettv->vval.v_list, n1);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005585 n1 <= n2; ++n1)
5586 {
5587 if (list_append_tv(l, &item->li_tv) == FAIL)
5588 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00005589 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005590 return FAIL;
5591 }
5592 item = item->li_next;
5593 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005594 clear_tv(rettv);
5595 rettv->v_type = VAR_LIST;
5596 rettv->vval.v_list = l;
Bram Moolenaar0d660222005-01-07 21:51:51 +00005597 ++l->lv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005598 }
5599 else
5600 {
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00005601 copy_tv(&list_find(rettv->vval.v_list, n1)->li_tv, &var1);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005602 clear_tv(rettv);
5603 *rettv = var1;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005604 }
5605 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005606
5607 case VAR_DICT:
5608 if (range)
5609 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005610 if (verbose)
5611 EMSG(_(e_dictrange));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005612 if (len == -1)
5613 clear_tv(&var1);
5614 return FAIL;
5615 }
5616 {
Bram Moolenaar33570922005-01-25 22:26:29 +00005617 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005618
5619 if (len == -1)
5620 {
5621 key = get_tv_string(&var1);
5622 if (*key == NUL)
5623 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005624 if (verbose)
5625 EMSG(_(e_emptykey));
Bram Moolenaar8c711452005-01-14 21:53:12 +00005626 clear_tv(&var1);
5627 return FAIL;
5628 }
5629 }
5630
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005631 item = dict_find(rettv->vval.v_dict, key, (int)len);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005632
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00005633 if (item == NULL && verbose)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00005634 EMSG2(_(e_dictkey), key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00005635 if (len == -1)
5636 clear_tv(&var1);
5637 if (item == NULL)
5638 return FAIL;
5639
5640 copy_tv(&item->di_tv, &var1);
5641 clear_tv(rettv);
5642 *rettv = var1;
5643 }
5644 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005645 }
5646 }
5647
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005648 return OK;
5649}
5650
5651/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00005652 * Get an option value.
5653 * "arg" points to the '&' or '+' before the option name.
5654 * "arg" is advanced to character after the option name.
5655 * Return OK or FAIL.
5656 */
5657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005658get_option_tv(
5659 char_u **arg,
5660 typval_T *rettv, /* when NULL, only check if option exists */
5661 int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005662{
5663 char_u *option_end;
5664 long numval;
5665 char_u *stringval;
5666 int opt_type;
5667 int c;
5668 int working = (**arg == '+'); /* has("+option") */
5669 int ret = OK;
5670 int opt_flags;
5671
5672 /*
5673 * Isolate the option name and find its value.
5674 */
5675 option_end = find_option_end(arg, &opt_flags);
5676 if (option_end == NULL)
5677 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005678 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005679 EMSG2(_("E112: Option name missing: %s"), *arg);
5680 return FAIL;
5681 }
5682
5683 if (!evaluate)
5684 {
5685 *arg = option_end;
5686 return OK;
5687 }
5688
5689 c = *option_end;
5690 *option_end = NUL;
5691 opt_type = get_option_value(*arg, &numval,
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005692 rettv == NULL ? NULL : &stringval, opt_flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005693
5694 if (opt_type == -3) /* invalid name */
5695 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005696 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005697 EMSG2(_("E113: Unknown option: %s"), *arg);
5698 ret = FAIL;
5699 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005700 else if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005701 {
5702 if (opt_type == -2) /* hidden string option */
5703 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005704 rettv->v_type = VAR_STRING;
5705 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005706 }
5707 else if (opt_type == -1) /* hidden number option */
5708 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005709 rettv->v_type = VAR_NUMBER;
5710 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005711 }
5712 else if (opt_type == 1) /* number option */
5713 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005714 rettv->v_type = VAR_NUMBER;
5715 rettv->vval.v_number = numval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005716 }
5717 else /* string option */
5718 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005719 rettv->v_type = VAR_STRING;
5720 rettv->vval.v_string = stringval;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005721 }
5722 }
5723 else if (working && (opt_type == -2 || opt_type == -1))
5724 ret = FAIL;
5725
5726 *option_end = c; /* put back for error messages */
5727 *arg = option_end;
5728
5729 return ret;
5730}
5731
5732/*
5733 * Allocate a variable for a string constant.
5734 * Return OK or FAIL.
5735 */
5736 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005737get_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005738{
5739 char_u *p;
5740 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005741 int extra = 0;
5742
5743 /*
5744 * Find the end of the string, skipping backslashed characters.
5745 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005746 for (p = *arg + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005747 {
5748 if (*p == '\\' && p[1] != NUL)
5749 {
5750 ++p;
5751 /* A "\<x>" form occupies at least 4 characters, and produces up
5752 * to 6 characters: reserve space for 2 extra */
5753 if (*p == '<')
5754 extra += 2;
5755 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005756 }
5757
5758 if (*p != '"')
5759 {
5760 EMSG2(_("E114: Missing quote: %s"), *arg);
5761 return FAIL;
5762 }
5763
5764 /* If only parsing, set *arg and return here */
5765 if (!evaluate)
5766 {
5767 *arg = p + 1;
5768 return OK;
5769 }
5770
5771 /*
5772 * Copy the string into allocated memory, handling backslashed
5773 * characters.
5774 */
5775 name = alloc((unsigned)(p - *arg + extra));
5776 if (name == NULL)
5777 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005778 rettv->v_type = VAR_STRING;
5779 rettv->vval.v_string = name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005780
Bram Moolenaar8c711452005-01-14 21:53:12 +00005781 for (p = *arg + 1; *p != NUL && *p != '"'; )
Bram Moolenaar071d4272004-06-13 20:20:40 +00005782 {
5783 if (*p == '\\')
5784 {
5785 switch (*++p)
5786 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005787 case 'b': *name++ = BS; ++p; break;
5788 case 'e': *name++ = ESC; ++p; break;
5789 case 'f': *name++ = FF; ++p; break;
5790 case 'n': *name++ = NL; ++p; break;
5791 case 'r': *name++ = CAR; ++p; break;
5792 case 't': *name++ = TAB; ++p; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005793
5794 case 'X': /* hex: "\x1", "\x12" */
5795 case 'x':
5796 case 'u': /* Unicode: "\u0023" */
5797 case 'U':
5798 if (vim_isxdigit(p[1]))
5799 {
5800 int n, nr;
5801 int c = toupper(*p);
5802
5803 if (c == 'X')
5804 n = 2;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005805 else if (*p == 'u')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005806 n = 4;
Bram Moolenaaracc39882015-06-19 12:08:13 +02005807 else
5808 n = 8;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005809 nr = 0;
5810 while (--n >= 0 && vim_isxdigit(p[1]))
5811 {
5812 ++p;
5813 nr = (nr << 4) + hex2nr(*p);
5814 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005815 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005816#ifdef FEAT_MBYTE
5817 /* For "\u" store the number according to
5818 * 'encoding'. */
5819 if (c != 'X')
Bram Moolenaar8c711452005-01-14 21:53:12 +00005820 name += (*mb_char2bytes)(nr, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005821 else
5822#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00005823 *name++ = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005824 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00005825 break;
5826
5827 /* octal: "\1", "\12", "\123" */
5828 case '0':
5829 case '1':
5830 case '2':
5831 case '3':
5832 case '4':
5833 case '5':
5834 case '6':
Bram Moolenaar8c711452005-01-14 21:53:12 +00005835 case '7': *name = *p++ - '0';
5836 if (*p >= '0' && *p <= '7')
Bram Moolenaar071d4272004-06-13 20:20:40 +00005837 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005838 *name = (*name << 3) + *p++ - '0';
5839 if (*p >= '0' && *p <= '7')
5840 *name = (*name << 3) + *p++ - '0';
Bram Moolenaar071d4272004-06-13 20:20:40 +00005841 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005842 ++name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005843 break;
5844
5845 /* Special key, e.g.: "\<C-W>" */
Bram Moolenaar8c711452005-01-14 21:53:12 +00005846 case '<': extra = trans_special(&p, name, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005847 if (extra != 0)
5848 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005849 name += extra;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005850 break;
5851 }
5852 /* FALLTHROUGH */
5853
Bram Moolenaar8c711452005-01-14 21:53:12 +00005854 default: MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005855 break;
5856 }
5857 }
5858 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00005859 MB_COPY_CHAR(p, name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005860
Bram Moolenaar071d4272004-06-13 20:20:40 +00005861 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005862 *name = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005863 *arg = p + 1;
5864
Bram Moolenaar071d4272004-06-13 20:20:40 +00005865 return OK;
5866}
5867
5868/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005869 * Allocate a variable for a 'str''ing' constant.
Bram Moolenaar071d4272004-06-13 20:20:40 +00005870 * Return OK or FAIL.
5871 */
5872 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005873get_lit_string_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005874{
5875 char_u *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005876 char_u *str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005877 int reduce = 0;
5878
5879 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005880 * Find the end of the string, skipping ''.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005881 */
5882 for (p = *arg + 1; *p != NUL; mb_ptr_adv(p))
5883 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005884 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005885 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005886 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005887 break;
5888 ++reduce;
5889 ++p;
5890 }
5891 }
5892
Bram Moolenaar8c711452005-01-14 21:53:12 +00005893 if (*p != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005894 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005895 EMSG2(_("E115: Missing quote: %s"), *arg);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005896 return FAIL;
5897 }
5898
Bram Moolenaar8c711452005-01-14 21:53:12 +00005899 /* If only parsing return after setting "*arg" */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005900 if (!evaluate)
5901 {
5902 *arg = p + 1;
5903 return OK;
5904 }
5905
5906 /*
Bram Moolenaar8c711452005-01-14 21:53:12 +00005907 * Copy the string into allocated memory, handling '' to ' reduction.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005908 */
5909 str = alloc((unsigned)((p - *arg) - reduce));
5910 if (str == NULL)
5911 return FAIL;
Bram Moolenaar8c711452005-01-14 21:53:12 +00005912 rettv->v_type = VAR_STRING;
5913 rettv->vval.v_string = str;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005914
Bram Moolenaar8c711452005-01-14 21:53:12 +00005915 for (p = *arg + 1; *p != NUL; )
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005916 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005917 if (*p == '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005918 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005919 if (p[1] != '\'')
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005920 break;
5921 ++p;
5922 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005923 MB_COPY_CHAR(p, str);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005924 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00005925 *str = NUL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005926 *arg = p + 1;
5927
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00005928 return OK;
5929}
5930
5931/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005932 * Allocate a variable for a List and fill it from "*arg".
5933 * Return OK or FAIL.
5934 */
5935 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01005936get_list_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005937{
Bram Moolenaar33570922005-01-25 22:26:29 +00005938 list_T *l = NULL;
5939 typval_T tv;
5940 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005941
5942 if (evaluate)
5943 {
5944 l = list_alloc();
5945 if (l == NULL)
5946 return FAIL;
5947 }
5948
5949 *arg = skipwhite(*arg + 1);
5950 while (**arg != ']' && **arg != NUL)
5951 {
5952 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
5953 goto failret;
5954 if (evaluate)
5955 {
5956 item = listitem_alloc();
5957 if (item != NULL)
5958 {
5959 item->li_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00005960 item->li_tv.v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005961 list_append(l, item);
5962 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00005963 else
5964 clear_tv(&tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005965 }
5966
5967 if (**arg == ']')
5968 break;
5969 if (**arg != ',')
5970 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005971 EMSG2(_("E696: Missing comma in List: %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005972 goto failret;
5973 }
5974 *arg = skipwhite(*arg + 1);
5975 }
5976
5977 if (**arg != ']')
5978 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00005979 EMSG2(_("E697: Missing end of List ']': %s"), *arg);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005980failret:
5981 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00005982 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005983 return FAIL;
5984 }
5985
5986 *arg = skipwhite(*arg + 1);
5987 if (evaluate)
5988 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +00005989 rettv->v_type = VAR_LIST;
5990 rettv->vval.v_list = l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00005991 ++l->lv_refcount;
5992 }
5993
5994 return OK;
5995}
5996
5997/*
5998 * Allocate an empty header for a list.
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00005999 * Caller should take care of the reference count.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006000 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006001 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006002list_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006003{
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006004 list_T *l;
6005
6006 l = (list_T *)alloc_clear(sizeof(list_T));
6007 if (l != NULL)
6008 {
6009 /* Prepend the list to the list of lists for garbage collection. */
6010 if (first_list != NULL)
6011 first_list->lv_used_prev = l;
6012 l->lv_used_prev = NULL;
6013 l->lv_used_next = first_list;
6014 first_list = l;
6015 }
6016 return l;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006017}
6018
6019/*
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006020 * Allocate an empty list for a return value.
6021 * Returns OK or FAIL.
6022 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006023 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006024rettv_list_alloc(typval_T *rettv)
Bram Moolenaareddf53b2006-02-27 00:11:10 +00006025{
6026 list_T *l = list_alloc();
6027
6028 if (l == NULL)
6029 return FAIL;
6030
6031 rettv->vval.v_list = l;
6032 rettv->v_type = VAR_LIST;
6033 ++l->lv_refcount;
6034 return OK;
6035}
6036
6037/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006038 * Unreference a list: decrement the reference count and free it when it
6039 * becomes zero.
6040 */
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00006041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006042list_unref(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006043{
Bram Moolenaar685295c2006-10-15 20:37:38 +00006044 if (l != NULL && --l->lv_refcount <= 0)
6045 list_free(l, TRUE);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006046}
6047
6048/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01006049 * Free a list, including all non-container items it points to.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006050 * Ignores the reference count.
6051 */
Bram Moolenaar1ef15e32006-02-01 21:56:25 +00006052 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006053list_free(
6054 list_T *l,
6055 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006056{
Bram Moolenaar33570922005-01-25 22:26:29 +00006057 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006058
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006059 /* Remove the list from the list of lists for garbage collection. */
6060 if (l->lv_used_prev == NULL)
6061 first_list = l->lv_used_next;
6062 else
6063 l->lv_used_prev->lv_used_next = l->lv_used_next;
6064 if (l->lv_used_next != NULL)
6065 l->lv_used_next->lv_used_prev = l->lv_used_prev;
6066
Bram Moolenaard9fba312005-06-26 22:34:35 +00006067 for (item = l->lv_first; item != NULL; item = l->lv_first)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006068 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00006069 /* Remove the item before deleting it. */
6070 l->lv_first = item->li_next;
Bram Moolenaar685295c2006-10-15 20:37:38 +00006071 if (recurse || (item->li_tv.v_type != VAR_LIST
6072 && item->li_tv.v_type != VAR_DICT))
6073 clear_tv(&item->li_tv);
6074 vim_free(item);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006075 }
6076 vim_free(l);
6077}
6078
6079/*
6080 * Allocate a list item.
Bram Moolenaar9a492d42015-01-27 13:49:31 +01006081 * It is not initialized, don't forget to set v_lock.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006082 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006083 listitem_T *
Bram Moolenaard14e00e2016-01-31 17:30:51 +01006084listitem_alloc(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006085{
Bram Moolenaar33570922005-01-25 22:26:29 +00006086 return (listitem_T *)alloc(sizeof(listitem_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006087}
6088
6089/*
Bram Moolenaar3d60ec22005-01-05 22:19:46 +00006090 * Free a list item. Also clears the value. Does not notify watchers.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006091 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02006092 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006093listitem_free(listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006094{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006095 clear_tv(&item->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006096 vim_free(item);
6097}
6098
6099/*
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006100 * Remove a list item from a List and free it. Also clears the value.
6101 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006102 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006103listitem_remove(list_T *l, listitem_T *item)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006104{
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006105 vimlist_remove(l, item, item);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006106 listitem_free(item);
6107}
6108
6109/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006110 * Get the number of items in a list.
6111 */
6112 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006113list_len(list_T *l)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006114{
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006115 if (l == NULL)
6116 return 0L;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006117 return l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006118}
6119
6120/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006121 * Return TRUE when two lists have exactly the same values.
6122 */
6123 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006124list_equal(
6125 list_T *l1,
6126 list_T *l2,
6127 int ic, /* ignore case for strings */
6128 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006129{
Bram Moolenaar33570922005-01-25 22:26:29 +00006130 listitem_T *item1, *item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006131
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006132 if (l1 == NULL || l2 == NULL)
6133 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006134 if (l1 == l2)
6135 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006136 if (list_len(l1) != list_len(l2))
6137 return FALSE;
6138
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006139 for (item1 = l1->lv_first, item2 = l2->lv_first;
6140 item1 != NULL && item2 != NULL;
6141 item1 = item1->li_next, item2 = item2->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006142 if (!tv_equal(&item1->li_tv, &item2->li_tv, ic, recursive))
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006143 return FALSE;
6144 return item1 == NULL && item2 == NULL;
6145}
6146
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006147/*
6148 * Return the dictitem that an entry in a hashtable points to.
6149 */
6150 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006151dict_lookup(hashitem_T *hi)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006152{
6153 return HI2DI(hi);
6154}
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006155
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006156/*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006157 * Return TRUE when two dictionaries have exactly the same key/values.
6158 */
6159 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006160dict_equal(
6161 dict_T *d1,
6162 dict_T *d2,
6163 int ic, /* ignore case for strings */
6164 int recursive) /* TRUE when used recursively */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006165{
Bram Moolenaar33570922005-01-25 22:26:29 +00006166 hashitem_T *hi;
6167 dictitem_T *item2;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006168 int todo;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006169
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006170 if (d1 == NULL || d2 == NULL)
6171 return FALSE;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006172 if (d1 == d2)
6173 return TRUE;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006174 if (dict_len(d1) != dict_len(d2))
6175 return FALSE;
6176
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00006177 todo = (int)d1->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00006178 for (hi = d1->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006179 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006180 if (!HASHITEM_EMPTY(hi))
6181 {
6182 item2 = dict_find(d2, hi->hi_key, -1);
6183 if (item2 == NULL)
6184 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006185 if (!tv_equal(&HI2DI(hi)->di_tv, &item2->di_tv, ic, recursive))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00006186 return FALSE;
6187 --todo;
6188 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006189 }
6190 return TRUE;
6191}
6192
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006193static int tv_equal_recurse_limit;
6194
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00006195/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006196 * Return TRUE if "tv1" and "tv2" have the same value.
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006197 * Compares the items just like "==" would compare them, but strings and
Bram Moolenaar8c8de832008-06-24 22:58:06 +00006198 * numbers are different. Floats and numbers are also different.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006199 */
6200 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006201tv_equal(
6202 typval_T *tv1,
6203 typval_T *tv2,
6204 int ic, /* ignore case */
6205 int recursive) /* TRUE when used recursively */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006206{
6207 char_u buf1[NUMBUFLEN], buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00006208 char_u *s1, *s2;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006209 static int recursive_cnt = 0; /* catch recursive loops */
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006210 int r;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006211
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006212 if (tv1->v_type != tv2->v_type)
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006213 return FALSE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006214
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006215 /* Catch lists and dicts that have an endless loop by limiting
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006216 * recursiveness to a limit. We guess they are equal then.
6217 * A fixed limit has the problem of still taking an awful long time.
6218 * Reduce the limit every time running into it. That should work fine for
6219 * deeply linked structures that are not recursively linked and catch
6220 * recursiveness quickly. */
6221 if (!recursive)
6222 tv_equal_recurse_limit = 1000;
6223 if (recursive_cnt >= tv_equal_recurse_limit)
6224 {
6225 --tv_equal_recurse_limit;
Bram Moolenaar8b402a02006-10-17 13:16:39 +00006226 return TRUE;
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006227 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006228
6229 switch (tv1->v_type)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006230 {
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006231 case VAR_LIST:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006232 ++recursive_cnt;
6233 r = list_equal(tv1->vval.v_list, tv2->vval.v_list, ic, TRUE);
6234 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006235 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006236
6237 case VAR_DICT:
Bram Moolenaar67b3f992010-11-10 20:41:57 +01006238 ++recursive_cnt;
6239 r = dict_equal(tv1->vval.v_dict, tv2->vval.v_dict, ic, TRUE);
6240 --recursive_cnt;
Bram Moolenaarb47a2402006-10-15 13:09:12 +00006241 return r;
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006242
6243 case VAR_FUNC:
6244 return (tv1->vval.v_string != NULL
6245 && tv2->vval.v_string != NULL
6246 && STRCMP(tv1->vval.v_string, tv2->vval.v_string) == 0);
6247
Bram Moolenaar1735bc92016-03-14 23:05:14 +01006248 case VAR_PARTIAL:
6249 return tv1->vval.v_partial != NULL
6250 && tv1->vval.v_partial == tv2->vval.v_partial;
6251
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006252 case VAR_NUMBER:
6253 return tv1->vval.v_number == tv2->vval.v_number;
6254
6255 case VAR_STRING:
6256 s1 = get_tv_string_buf(tv1, buf1);
6257 s2 = get_tv_string_buf(tv2, buf2);
6258 return ((ic ? MB_STRICMP(s1, s2) : STRCMP(s1, s2)) == 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006259
6260 case VAR_SPECIAL:
6261 return tv1->vval.v_number == tv2->vval.v_number;
Bram Moolenaar835dc632016-02-07 14:27:38 +01006262
6263 case VAR_FLOAT:
6264#ifdef FEAT_FLOAT
6265 return tv1->vval.v_float == tv2->vval.v_float;
6266#endif
6267 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006268#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +01006269 return tv1->vval.v_job == tv2->vval.v_job;
6270#endif
Bram Moolenaar77073442016-02-13 23:23:53 +01006271 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006272#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +01006273 return tv1->vval.v_channel == tv2->vval.v_channel;
6274#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +01006275 case VAR_UNKNOWN:
6276 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006277 }
Bram Moolenaar7d1f5db2005-07-03 21:39:27 +00006278
Bram Moolenaara03f2332016-02-06 18:09:59 +01006279 /* VAR_UNKNOWN can be the result of a invalid expression, let's say it
6280 * does not equal anything, not even itself. */
6281 return FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006282}
6283
6284/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006285 * Locate item with index "n" in list "l" and return it.
6286 * A negative index is counted from the end; -1 is the last item.
6287 * Returns NULL when "n" is out of range.
6288 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006289 listitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006290list_find(list_T *l, long n)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006291{
Bram Moolenaar33570922005-01-25 22:26:29 +00006292 listitem_T *item;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006293 long idx;
6294
6295 if (l == NULL)
6296 return NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006297
6298 /* Negative index is relative to the end. */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006299 if (n < 0)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006300 n = l->lv_len + n;
6301
6302 /* Check for index out of range. */
6303 if (n < 0 || n >= l->lv_len)
6304 return NULL;
6305
6306 /* When there is a cached index may start search from there. */
6307 if (l->lv_idx_item != NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006308 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006309 if (n < l->lv_idx / 2)
6310 {
6311 /* closest to the start of the list */
6312 item = l->lv_first;
6313 idx = 0;
6314 }
6315 else if (n > (l->lv_idx + l->lv_len) / 2)
6316 {
6317 /* closest to the end of the list */
6318 item = l->lv_last;
6319 idx = l->lv_len - 1;
6320 }
6321 else
6322 {
6323 /* closest to the cached index */
6324 item = l->lv_idx_item;
6325 idx = l->lv_idx;
6326 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006327 }
6328 else
6329 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006330 if (n < l->lv_len / 2)
6331 {
6332 /* closest to the start of the list */
6333 item = l->lv_first;
6334 idx = 0;
6335 }
6336 else
6337 {
6338 /* closest to the end of the list */
6339 item = l->lv_last;
6340 idx = l->lv_len - 1;
6341 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006342 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006343
6344 while (n > idx)
6345 {
6346 /* search forward */
6347 item = item->li_next;
6348 ++idx;
6349 }
6350 while (n < idx)
6351 {
6352 /* search backward */
6353 item = item->li_prev;
6354 --idx;
6355 }
6356
6357 /* cache the used index */
6358 l->lv_idx = idx;
6359 l->lv_idx_item = item;
6360
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006361 return item;
6362}
6363
6364/*
Bram Moolenaara5525202006-03-02 22:52:09 +00006365 * Get list item "l[idx]" as a number.
6366 */
6367 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006368list_find_nr(
6369 list_T *l,
6370 long idx,
6371 int *errorp) /* set to TRUE when something wrong */
Bram Moolenaara5525202006-03-02 22:52:09 +00006372{
6373 listitem_T *li;
6374
6375 li = list_find(l, idx);
6376 if (li == NULL)
6377 {
6378 if (errorp != NULL)
6379 *errorp = TRUE;
6380 return -1L;
6381 }
6382 return get_tv_number_chk(&li->li_tv, errorp);
6383}
6384
6385/*
Bram Moolenaard812df62008-11-09 12:46:09 +00006386 * Get list item "l[idx - 1]" as a string. Returns NULL for failure.
6387 */
6388 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006389list_find_str(list_T *l, long idx)
Bram Moolenaard812df62008-11-09 12:46:09 +00006390{
6391 listitem_T *li;
6392
6393 li = list_find(l, idx - 1);
6394 if (li == NULL)
6395 {
6396 EMSGN(_(e_listidx), idx);
6397 return NULL;
6398 }
6399 return get_tv_string(&li->li_tv);
6400}
6401
6402/*
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006403 * Locate "item" list "l" and return its index.
6404 * Returns -1 when "item" is not in the list.
6405 */
6406 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01006407list_idx_of_item(list_T *l, listitem_T *item)
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006408{
6409 long idx = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +00006410 listitem_T *li;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006411
6412 if (l == NULL)
6413 return -1;
6414 idx = 0;
6415 for (li = l->lv_first; li != NULL && li != item; li = li->li_next)
6416 ++idx;
6417 if (li == NULL)
6418 return -1;
Bram Moolenaar75c50c42005-06-04 22:06:24 +00006419 return idx;
Bram Moolenaar6cc16192005-01-08 21:49:45 +00006420}
6421
6422/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006423 * Append item "item" to the end of list "l".
6424 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006425 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006426list_append(list_T *l, listitem_T *item)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006427{
6428 if (l->lv_last == NULL)
6429 {
6430 /* empty list */
6431 l->lv_first = item;
6432 l->lv_last = item;
6433 item->li_prev = NULL;
6434 }
6435 else
6436 {
6437 l->lv_last->li_next = item;
6438 item->li_prev = l->lv_last;
6439 l->lv_last = item;
6440 }
Bram Moolenaar758711c2005-02-02 23:11:38 +00006441 ++l->lv_len;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006442 item->li_next = NULL;
6443}
6444
6445/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006446 * Append typval_T "tv" to the end of list "l".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006447 * Return FAIL when out of memory.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006448 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01006449 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006450list_append_tv(list_T *l, typval_T *tv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006451{
Bram Moolenaar05159a02005-02-26 23:04:13 +00006452 listitem_T *li = listitem_alloc();
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006453
Bram Moolenaar05159a02005-02-26 23:04:13 +00006454 if (li == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006455 return FAIL;
Bram Moolenaar05159a02005-02-26 23:04:13 +00006456 copy_tv(tv, &li->li_tv);
6457 list_append(l, li);
6458 return OK;
6459}
6460
6461/*
Bram Moolenaar2641f772005-03-25 21:58:17 +00006462 * Add a dictionary to a list. Used by getqflist().
Bram Moolenaar05159a02005-02-26 23:04:13 +00006463 * Return FAIL when out of memory.
6464 */
6465 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006466list_append_dict(list_T *list, dict_T *dict)
Bram Moolenaar05159a02005-02-26 23:04:13 +00006467{
6468 listitem_T *li = listitem_alloc();
6469
6470 if (li == NULL)
6471 return FAIL;
6472 li->li_tv.v_type = VAR_DICT;
6473 li->li_tv.v_lock = 0;
6474 li->li_tv.vval.v_dict = dict;
6475 list_append(list, li);
6476 ++dict->dv_refcount;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006477 return OK;
6478}
6479
6480/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006481 * Make a copy of "str" and append it as an item to list "l".
Bram Moolenaar4463f292005-09-25 22:20:24 +00006482 * When "len" >= 0 use "str[len]".
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006483 * Returns FAIL when out of memory.
6484 */
Bram Moolenaard812df62008-11-09 12:46:09 +00006485 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006486list_append_string(list_T *l, char_u *str, int len)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006487{
6488 listitem_T *li = listitem_alloc();
6489
6490 if (li == NULL)
6491 return FAIL;
6492 list_append(l, li);
6493 li->li_tv.v_type = VAR_STRING;
6494 li->li_tv.v_lock = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006495 if (str == NULL)
6496 li->li_tv.vval.v_string = NULL;
6497 else if ((li->li_tv.vval.v_string = (len >= 0 ? vim_strnsave(str, len)
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00006498 : vim_strsave(str))) == NULL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00006499 return FAIL;
6500 return OK;
6501}
6502
6503/*
Bram Moolenaar4463f292005-09-25 22:20:24 +00006504 * Append "n" to list "l".
6505 * Returns FAIL when out of memory.
6506 */
Bram Moolenaar86edef62016-03-13 18:07:30 +01006507 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006508list_append_number(list_T *l, varnumber_T n)
Bram Moolenaar4463f292005-09-25 22:20:24 +00006509{
6510 listitem_T *li;
6511
6512 li = listitem_alloc();
6513 if (li == NULL)
6514 return FAIL;
6515 li->li_tv.v_type = VAR_NUMBER;
6516 li->li_tv.v_lock = 0;
6517 li->li_tv.vval.v_number = n;
6518 list_append(l, li);
6519 return OK;
6520}
6521
6522/*
Bram Moolenaar33570922005-01-25 22:26:29 +00006523 * Insert typval_T "tv" in list "l" before "item".
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006524 * If "item" is NULL append at the end.
6525 * Return FAIL when out of memory.
6526 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006527 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006528list_insert_tv(list_T *l, typval_T *tv, listitem_T *item)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006529{
Bram Moolenaar33570922005-01-25 22:26:29 +00006530 listitem_T *ni = listitem_alloc();
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006531
6532 if (ni == NULL)
6533 return FAIL;
6534 copy_tv(tv, &ni->li_tv);
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006535 list_insert(l, ni, item);
6536 return OK;
6537}
6538
6539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006540list_insert(list_T *l, listitem_T *ni, listitem_T *item)
Bram Moolenaar063a46b2014-01-14 16:36:51 +01006541{
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006542 if (item == NULL)
6543 /* Append new item at end of list. */
6544 list_append(l, ni);
6545 else
6546 {
6547 /* Insert new item before existing item. */
6548 ni->li_prev = item->li_prev;
6549 ni->li_next = item;
6550 if (item->li_prev == NULL)
Bram Moolenaar758711c2005-02-02 23:11:38 +00006551 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006552 l->lv_first = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006553 ++l->lv_idx;
6554 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006555 else
Bram Moolenaar758711c2005-02-02 23:11:38 +00006556 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006557 item->li_prev->li_next = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006558 l->lv_idx_item = NULL;
6559 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006560 item->li_prev = ni;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006561 ++l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006562 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006563}
6564
6565/*
6566 * Extend "l1" with "l2".
6567 * If "bef" is NULL append at the end, otherwise insert before this item.
6568 * Returns FAIL when out of memory.
6569 */
6570 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006571list_extend(list_T *l1, list_T *l2, listitem_T *bef)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006572{
Bram Moolenaar33570922005-01-25 22:26:29 +00006573 listitem_T *item;
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006574 int todo = l2->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006575
Bram Moolenaardc9cf9c2008-08-08 10:36:31 +00006576 /* We also quit the loop when we have inserted the original item count of
6577 * the list, avoid a hang when we extend a list with itself. */
6578 for (item = l2->lv_first; item != NULL && --todo >= 0; item = item->li_next)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006579 if (list_insert_tv(l1, &item->li_tv, bef) == FAIL)
6580 return FAIL;
6581 return OK;
6582}
6583
6584/*
6585 * Concatenate lists "l1" and "l2" into a new list, stored in "tv".
6586 * Return FAIL when out of memory.
6587 */
6588 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006589list_concat(list_T *l1, list_T *l2, typval_T *tv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006590{
Bram Moolenaar33570922005-01-25 22:26:29 +00006591 list_T *l;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006592
Bram Moolenaar9bf749b2008-07-27 13:57:29 +00006593 if (l1 == NULL || l2 == NULL)
6594 return FAIL;
6595
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006596 /* make a copy of the first list. */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006597 l = list_copy(l1, FALSE, 0);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006598 if (l == NULL)
6599 return FAIL;
6600 tv->v_type = VAR_LIST;
6601 tv->vval.v_list = l;
6602
6603 /* append all items from the second list */
6604 return list_extend(l, l2, NULL);
6605}
6606
6607/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00006608 * Make a copy of list "orig". Shallow if "deep" is FALSE.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006609 * The refcount of the new list is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006610 * See item_copy() for "copyID".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006611 * Returns NULL when out of memory.
6612 */
Bram Moolenaar33570922005-01-25 22:26:29 +00006613 static list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006614list_copy(list_T *orig, int deep, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006615{
Bram Moolenaar33570922005-01-25 22:26:29 +00006616 list_T *copy;
6617 listitem_T *item;
6618 listitem_T *ni;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006619
6620 if (orig == NULL)
6621 return NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006622
6623 copy = list_alloc();
6624 if (copy != NULL)
6625 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006626 if (copyID != 0)
6627 {
6628 /* Do this before adding the items, because one of the items may
6629 * refer back to this list. */
6630 orig->lv_copyID = copyID;
6631 orig->lv_copylist = copy;
6632 }
6633 for (item = orig->lv_first; item != NULL && !got_int;
6634 item = item->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006635 {
6636 ni = listitem_alloc();
6637 if (ni == NULL)
6638 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00006639 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006640 {
6641 if (item_copy(&item->li_tv, &ni->li_tv, deep, copyID) == FAIL)
6642 {
6643 vim_free(ni);
6644 break;
6645 }
6646 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006647 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00006648 copy_tv(&item->li_tv, &ni->li_tv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006649 list_append(copy, ni);
6650 }
6651 ++copy->lv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006652 if (item != NULL)
6653 {
6654 list_unref(copy);
6655 copy = NULL;
6656 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006657 }
6658
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006659 return copy;
6660}
6661
6662/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006663 * Remove items "item" to "item2" from list "l".
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00006664 * Does not free the listitem or the value!
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +02006665 * This used to be called list_remove, but that conflicts with a Sun header
6666 * file.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006667 */
Bram Moolenaardb913952012-06-29 12:54:53 +02006668 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01006669vimlist_remove(list_T *l, listitem_T *item, listitem_T *item2)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006670{
Bram Moolenaar33570922005-01-25 22:26:29 +00006671 listitem_T *ip;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006672
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006673 /* notify watchers */
6674 for (ip = item; ip != NULL; ip = ip->li_next)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006675 {
Bram Moolenaar758711c2005-02-02 23:11:38 +00006676 --l->lv_len;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006677 list_fix_watch(l, ip);
6678 if (ip == item2)
6679 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006680 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00006681
6682 if (item2->li_next == NULL)
6683 l->lv_last = item->li_prev;
6684 else
6685 item2->li_next->li_prev = item->li_prev;
6686 if (item->li_prev == NULL)
6687 l->lv_first = item2->li_next;
6688 else
6689 item->li_prev->li_next = item2->li_next;
Bram Moolenaar758711c2005-02-02 23:11:38 +00006690 l->lv_idx_item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006691}
6692
6693/*
6694 * Return an allocated string with the string representation of a list.
6695 * May return NULL.
6696 */
6697 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01006698list2string(typval_T *tv, int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006699{
6700 garray_T ga;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006701
6702 if (tv->vval.v_list == NULL)
6703 return NULL;
6704 ga_init2(&ga, (int)sizeof(char), 80);
6705 ga_append(&ga, '[');
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00006706 if (list_join(&ga, tv->vval.v_list, (char_u *)", ", FALSE, copyID) == FAIL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006707 {
6708 vim_free(ga.ga_data);
6709 return NULL;
6710 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006711 ga_append(&ga, ']');
6712 ga_append(&ga, NUL);
6713 return (char_u *)ga.ga_data;
6714}
6715
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006716typedef struct join_S {
6717 char_u *s;
6718 char_u *tofree;
6719} join_T;
6720
6721 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006722list_join_inner(
6723 garray_T *gap, /* to store the result in */
6724 list_T *l,
6725 char_u *sep,
6726 int echo_style,
6727 int copyID,
6728 garray_T *join_gap) /* to keep each list item string */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006729{
6730 int i;
6731 join_T *p;
6732 int len;
6733 int sumlen = 0;
6734 int first = TRUE;
6735 char_u *tofree;
6736 char_u numbuf[NUMBUFLEN];
6737 listitem_T *item;
6738 char_u *s;
6739
6740 /* Stringify each item in the list. */
6741 for (item = l->lv_first; item != NULL && !got_int; item = item->li_next)
6742 {
6743 if (echo_style)
6744 s = echo_string(&item->li_tv, &tofree, numbuf, copyID);
6745 else
6746 s = tv2string(&item->li_tv, &tofree, numbuf, copyID);
6747 if (s == NULL)
6748 return FAIL;
6749
6750 len = (int)STRLEN(s);
6751 sumlen += len;
6752
Bram Moolenaarcde88542015-08-11 19:14:00 +02006753 (void)ga_grow(join_gap, 1);
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006754 p = ((join_T *)join_gap->ga_data) + (join_gap->ga_len++);
6755 if (tofree != NULL || s != numbuf)
6756 {
6757 p->s = s;
6758 p->tofree = tofree;
6759 }
6760 else
6761 {
6762 p->s = vim_strnsave(s, len);
6763 p->tofree = p->s;
6764 }
6765
6766 line_breakcheck();
Bram Moolenaar8502c702014-06-17 12:51:16 +02006767 if (did_echo_string_emsg) /* recursion error, bail out */
6768 break;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006769 }
6770
6771 /* Allocate result buffer with its total size, avoid re-allocation and
6772 * multiple copy operations. Add 2 for a tailing ']' and NUL. */
6773 if (join_gap->ga_len >= 2)
6774 sumlen += (int)STRLEN(sep) * (join_gap->ga_len - 1);
6775 if (ga_grow(gap, sumlen + 2) == FAIL)
6776 return FAIL;
6777
6778 for (i = 0; i < join_gap->ga_len && !got_int; ++i)
6779 {
6780 if (first)
6781 first = FALSE;
6782 else
6783 ga_concat(gap, sep);
6784 p = ((join_T *)join_gap->ga_data) + i;
6785
6786 if (p->s != NULL)
6787 ga_concat(gap, p->s);
6788 line_breakcheck();
6789 }
6790
6791 return OK;
6792}
6793
Bram Moolenaar49cd9572005-01-03 21:06:01 +00006794/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006795 * Join list "l" into a string in "*gap", using separator "sep".
Bram Moolenaar70b2a562012-01-10 22:26:17 +01006796 * When "echo_style" is TRUE use String as echoed, otherwise as inside a List.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006797 * Return FAIL or OK.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006798 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +00006799 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006800list_join(
6801 garray_T *gap,
6802 list_T *l,
6803 char_u *sep,
6804 int echo_style,
6805 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006806{
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006807 garray_T join_ga;
6808 int retval;
6809 join_T *p;
6810 int i;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006811
Bram Moolenaard39a7512015-04-16 22:51:22 +02006812 if (l->lv_len < 1)
6813 return OK; /* nothing to do */
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006814 ga_init2(&join_ga, (int)sizeof(join_T), l->lv_len);
6815 retval = list_join_inner(gap, l, sep, echo_style, copyID, &join_ga);
6816
6817 /* Dispose each item in join_ga. */
6818 if (join_ga.ga_data != NULL)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006819 {
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006820 p = (join_T *)join_ga.ga_data;
6821 for (i = 0; i < join_ga.ga_len; ++i)
6822 {
6823 vim_free(p->tofree);
6824 ++p;
6825 }
6826 ga_clear(&join_ga);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006827 }
Bram Moolenaar3fe37d62012-02-06 00:13:22 +01006828
6829 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00006830}
6831
6832/*
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006833 * Return the next (unique) copy ID.
6834 * Used for serializing nested structures.
6835 */
6836 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006837get_copyID(void)
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006838{
6839 current_copyID += COPYID_INC;
6840 return current_copyID;
6841}
6842
6843/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006844 * Garbage collection for lists and dictionaries.
6845 *
6846 * We use reference counts to be able to free most items right away when they
6847 * are no longer used. But for composite items it's possible that it becomes
6848 * unused while the reference count is > 0: When there is a recursive
6849 * reference. Example:
6850 * :let l = [1, 2, 3]
6851 * :let d = {9: l}
6852 * :let l[1] = d
6853 *
6854 * Since this is quite unusual we handle this with garbage collection: every
6855 * once in a while find out which lists and dicts are not referenced from any
6856 * variable.
6857 *
6858 * Here is a good reference text about garbage collection (refers to Python
6859 * but it applies to all reference-counting mechanisms):
6860 * http://python.ca/nas/python/gc/
Bram Moolenaard9fba312005-06-26 22:34:35 +00006861 */
Bram Moolenaard9fba312005-06-26 22:34:35 +00006862
6863/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006864 * Do garbage collection for lists and dicts.
6865 * Return TRUE if some memory was freed.
Bram Moolenaard9fba312005-06-26 22:34:35 +00006866 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006867 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01006868garbage_collect(void)
Bram Moolenaard9fba312005-06-26 22:34:35 +00006869{
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006870 int copyID;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006871 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006872 buf_T *buf;
6873 win_T *wp;
6874 int i;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +00006875 funccall_T *fc, **pfc;
Bram Moolenaar934b1362015-02-04 23:06:45 +01006876 int did_free = FALSE;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006877 int did_free_funccal = FALSE;
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006878#ifdef FEAT_WINDOWS
6879 tabpage_T *tp;
6880#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006881
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006882 /* Only do this once. */
6883 want_garbage_collect = FALSE;
6884 may_garbage_collect = FALSE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00006885 garbage_collect_at_exit = FALSE;
Bram Moolenaar9fecb462006-09-05 10:59:47 +00006886
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006887 /* We advance by two because we add one for items referenced through
6888 * previous_funccal. */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01006889 copyID = get_copyID();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006890
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006891 /*
6892 * 1. Go through all accessible variables and mark all lists and dicts
6893 * with copyID.
6894 */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006895
6896 /* Don't free variables in the previous_funccal list unless they are only
6897 * referenced through previous_funccal. This must be first, because if
Bram Moolenaar2c2398c2009-06-03 11:22:45 +00006898 * the item is referenced elsewhere the funccal must not be freed. */
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006899 for (fc = previous_funccal; fc != NULL; fc = fc->caller)
6900 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006901 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID + 1,
6902 NULL);
6903 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID + 1,
6904 NULL);
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006905 }
6906
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006907 /* script-local variables */
6908 for (i = 1; i <= ga_scripts.ga_len; ++i)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006909 abort = abort || set_ref_in_ht(&SCRIPT_VARS(i), copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006910
6911 /* buffer-local variables */
6912 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006913 abort = abort || set_ref_in_item(&buf->b_bufvar.di_tv, copyID,
6914 NULL, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006915
6916 /* window-local variables */
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006917 FOR_ALL_TAB_WINDOWS(tp, wp)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006918 abort = abort || set_ref_in_item(&wp->w_winvar.di_tv, copyID,
6919 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006920#ifdef FEAT_AUTOCMD
6921 if (aucmd_win != NULL)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006922 abort = abort || set_ref_in_item(&aucmd_win->w_winvar.di_tv, copyID,
6923 NULL, NULL);
Bram Moolenaar3bb28552013-04-15 18:25:59 +02006924#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006925
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006926#ifdef FEAT_WINDOWS
6927 /* tabpage-local variables */
6928 for (tp = first_tabpage; tp != NULL; tp = tp->tp_next)
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006929 abort = abort || set_ref_in_item(&tp->tp_winvar.di_tv, copyID,
6930 NULL, NULL);
Bram Moolenaar910f66f2006-04-05 20:41:53 +00006931#endif
6932
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006933 /* global variables */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006934 abort = abort || set_ref_in_ht(&globvarht, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006935
6936 /* function-local variables */
6937 for (fc = current_funccal; fc != NULL; fc = fc->caller)
6938 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006939 abort = abort || set_ref_in_ht(&fc->l_vars.dv_hashtab, copyID, NULL);
6940 abort = abort || set_ref_in_ht(&fc->l_avars.dv_hashtab, copyID, NULL);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00006941 }
6942
Bram Moolenaard812df62008-11-09 12:46:09 +00006943 /* v: vars */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006944 abort = abort || set_ref_in_ht(&vimvarht, copyID, NULL);
Bram Moolenaard812df62008-11-09 12:46:09 +00006945
Bram Moolenaar1dced572012-04-05 16:54:08 +02006946#ifdef FEAT_LUA
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006947 abort = abort || set_ref_in_lua(copyID);
Bram Moolenaar1dced572012-04-05 16:54:08 +02006948#endif
6949
Bram Moolenaardb913952012-06-29 12:54:53 +02006950#ifdef FEAT_PYTHON
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006951 abort = abort || set_ref_in_python(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006952#endif
6953
6954#ifdef FEAT_PYTHON3
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006955 abort = abort || set_ref_in_python3(copyID);
Bram Moolenaardb913952012-06-29 12:54:53 +02006956#endif
6957
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01006958#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar4b6a6dc2016-02-04 22:49:49 +01006959 abort = abort || set_ref_in_channel(copyID);
6960#endif
6961
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006962 if (!abort)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006963 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006964 /*
6965 * 2. Free lists and dictionaries that are not referenced.
6966 */
6967 did_free = free_unref_items(copyID);
6968
6969 /*
6970 * 3. Check if any funccal can be freed now.
6971 */
6972 for (pfc = &previous_funccal; *pfc != NULL; )
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006973 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006974 if (can_free_funccal(*pfc, copyID))
6975 {
6976 fc = *pfc;
6977 *pfc = fc->caller;
6978 free_funccal(fc, TRUE);
6979 did_free = TRUE;
6980 did_free_funccal = TRUE;
6981 }
6982 else
6983 pfc = &(*pfc)->caller;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006984 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006985 if (did_free_funccal)
6986 /* When a funccal was freed some more items might be garbage
6987 * collected, so run again. */
6988 (void)garbage_collect();
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006989 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01006990 else if (p_verbose > 0)
6991 {
6992 verb_msg((char_u *)_("Not enough memory to set references, garbage collection aborted!"));
6993 }
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00006994
6995 return did_free;
6996}
6997
6998/*
Bram Moolenaar835dc632016-02-07 14:27:38 +01006999 * Free lists, dictionaries and jobs that are no longer referenced.
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007000 */
7001 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007002free_unref_items(int copyID)
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007003{
Bram Moolenaare71eea82015-02-03 17:10:06 +01007004 dict_T *dd, *dd_next;
7005 list_T *ll, *ll_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007006 int did_free = FALSE;
7007
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007008 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007009 * Go through the list of dicts and free items without the copyID.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007010 */
7011 for (dd = first_dict; dd != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007012 {
7013 dd_next = dd->dv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007014 if ((dd->dv_copyID & COPYID_MASK) != (copyID & COPYID_MASK))
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007015 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007016 /* Free the Dictionary and ordinary items it contains, but don't
7017 * recurse into Lists and Dictionaries, they will be in the list
7018 * of dicts or list of lists. */
7019 dict_free(dd, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007020 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007021 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007022 dd = dd_next;
7023 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007024
7025 /*
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007026 * Go through the list of lists and free items without the copyID.
7027 * But don't free a list that has a watcher (used in a for loop), these
7028 * are not referenced anywhere.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007029 */
7030 for (ll = first_list; ll != NULL; )
Bram Moolenaare71eea82015-02-03 17:10:06 +01007031 {
7032 ll_next = ll->lv_used_next;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +00007033 if ((ll->lv_copyID & COPYID_MASK) != (copyID & COPYID_MASK)
7034 && ll->lv_watch == NULL)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007035 {
Bram Moolenaar685295c2006-10-15 20:37:38 +00007036 /* Free the List and ordinary items it contains, but don't recurse
7037 * into Lists and Dictionaries, they will be in the list of dicts
7038 * or list of lists. */
7039 list_free(ll, FALSE);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007040 did_free = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007041 }
Bram Moolenaare71eea82015-02-03 17:10:06 +01007042 ll = ll_next;
7043 }
Bram Moolenaar835dc632016-02-07 14:27:38 +01007044
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007045 return did_free;
7046}
7047
7048/*
7049 * Mark all lists and dicts referenced through hashtab "ht" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007050 * "list_stack" is used to add lists to be marked. Can be NULL.
7051 *
7052 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007053 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007054 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007055set_ref_in_ht(hashtab_T *ht, int copyID, list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007056{
7057 int todo;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007058 int abort = FALSE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007059 hashitem_T *hi;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007060 hashtab_T *cur_ht;
7061 ht_stack_T *ht_stack = NULL;
7062 ht_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007063
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007064 cur_ht = ht;
7065 for (;;)
7066 {
7067 if (!abort)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007068 {
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007069 /* Mark each item in the hashtab. If the item contains a hashtab
7070 * it is added to ht_stack, if it contains a list it is added to
7071 * list_stack. */
7072 todo = (int)cur_ht->ht_used;
7073 for (hi = cur_ht->ht_array; todo > 0; ++hi)
7074 if (!HASHITEM_EMPTY(hi))
7075 {
7076 --todo;
7077 abort = abort || set_ref_in_item(&HI2DI(hi)->di_tv, copyID,
7078 &ht_stack, list_stack);
7079 }
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007080 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007081
7082 if (ht_stack == NULL)
7083 break;
7084
7085 /* take an item from the stack */
7086 cur_ht = ht_stack->ht;
7087 tempitem = ht_stack;
7088 ht_stack = ht_stack->prev;
7089 free(tempitem);
7090 }
7091
7092 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007093}
7094
7095/*
7096 * Mark all lists and dicts referenced through list "l" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007097 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7098 *
7099 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007100 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007101 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007102set_ref_in_list(list_T *l, int copyID, ht_stack_T **ht_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007103{
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007104 listitem_T *li;
7105 int abort = FALSE;
7106 list_T *cur_l;
7107 list_stack_T *list_stack = NULL;
7108 list_stack_T *tempitem;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007109
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007110 cur_l = l;
7111 for (;;)
7112 {
7113 if (!abort)
7114 /* Mark each item in the list. If the item contains a hashtab
7115 * it is added to ht_stack, if it contains a list it is added to
7116 * list_stack. */
7117 for (li = cur_l->lv_first; !abort && li != NULL; li = li->li_next)
7118 abort = abort || set_ref_in_item(&li->li_tv, copyID,
7119 ht_stack, &list_stack);
7120 if (list_stack == NULL)
7121 break;
7122
7123 /* take an item from the stack */
7124 cur_l = list_stack->list;
7125 tempitem = list_stack;
7126 list_stack = list_stack->prev;
7127 free(tempitem);
7128 }
7129
7130 return abort;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007131}
7132
7133/*
7134 * Mark all lists and dicts referenced through typval "tv" with "copyID".
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007135 * "list_stack" is used to add lists to be marked. Can be NULL.
7136 * "ht_stack" is used to add hashtabs to be marked. Can be NULL.
7137 *
7138 * Returns TRUE if setting references failed somehow.
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007139 */
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007140 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007141set_ref_in_item(
7142 typval_T *tv,
7143 int copyID,
7144 ht_stack_T **ht_stack,
7145 list_stack_T **list_stack)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007146{
7147 dict_T *dd;
7148 list_T *ll;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007149 int abort = FALSE;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007150
Bram Moolenaara03f2332016-02-06 18:09:59 +01007151 if (tv->v_type == VAR_DICT)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007152 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007153 dd = tv->vval.v_dict;
7154 if (dd != NULL && dd->dv_copyID != copyID)
7155 {
7156 /* Didn't see this dict yet. */
7157 dd->dv_copyID = copyID;
7158 if (ht_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007159 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007160 abort = set_ref_in_ht(&dd->dv_hashtab, copyID, list_stack);
7161 }
7162 else
7163 {
7164 ht_stack_T *newitem = (ht_stack_T*)malloc(sizeof(ht_stack_T));
7165 if (newitem == NULL)
7166 abort = TRUE;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007167 else
7168 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007169 newitem->ht = &dd->dv_hashtab;
7170 newitem->prev = *ht_stack;
7171 *ht_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007172 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007173 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007174 }
7175 }
7176 else if (tv->v_type == VAR_LIST)
7177 {
7178 ll = tv->vval.v_list;
7179 if (ll != NULL && ll->lv_copyID != copyID)
7180 {
7181 /* Didn't see this list yet. */
7182 ll->lv_copyID = copyID;
7183 if (list_stack == NULL)
Bram Moolenaard9fba312005-06-26 22:34:35 +00007184 {
Bram Moolenaara03f2332016-02-06 18:09:59 +01007185 abort = set_ref_in_list(ll, copyID, ht_stack);
7186 }
7187 else
7188 {
7189 list_stack_T *newitem = (list_stack_T*)malloc(
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007190 sizeof(list_stack_T));
Bram Moolenaara03f2332016-02-06 18:09:59 +01007191 if (newitem == NULL)
7192 abort = TRUE;
7193 else
7194 {
7195 newitem->list = ll;
7196 newitem->prev = *list_stack;
7197 *list_stack = newitem;
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007198 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007199 }
Bram Moolenaara03f2332016-02-06 18:09:59 +01007200 }
Bram Moolenaard9fba312005-06-26 22:34:35 +00007201 }
Bram Moolenaar2459a5e2015-02-03 12:55:18 +01007202 return abort;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007203}
7204
7205/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007206 * Allocate an empty header for a dictionary.
7207 */
Bram Moolenaar05159a02005-02-26 23:04:13 +00007208 dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007209dict_alloc(void)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007210{
Bram Moolenaar33570922005-01-25 22:26:29 +00007211 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007212
Bram Moolenaar33570922005-01-25 22:26:29 +00007213 d = (dict_T *)alloc(sizeof(dict_T));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007214 if (d != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007215 {
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007216 /* Add the dict to the list of dicts for garbage collection. */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007217 if (first_dict != NULL)
7218 first_dict->dv_used_prev = d;
7219 d->dv_used_next = first_dict;
7220 d->dv_used_prev = NULL;
Bram Moolenaar685295c2006-10-15 20:37:38 +00007221 first_dict = d;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007222
Bram Moolenaar33570922005-01-25 22:26:29 +00007223 hash_init(&d->dv_hashtab);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007224 d->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +02007225 d->dv_scope = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007226 d->dv_refcount = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007227 d->dv_copyID = 0;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007228 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007229 return d;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007230}
7231
7232/*
Bram Moolenaara800b422010-06-27 01:15:55 +02007233 * Allocate an empty dict for a return value.
7234 * Returns OK or FAIL.
7235 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007236 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007237rettv_dict_alloc(typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +02007238{
7239 dict_T *d = dict_alloc();
7240
7241 if (d == NULL)
7242 return FAIL;
7243
7244 rettv->vval.v_dict = d;
7245 rettv->v_type = VAR_DICT;
7246 ++d->dv_refcount;
7247 return OK;
7248}
7249
7250
7251/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007252 * Unreference a Dictionary: decrement the reference count and free it when it
7253 * becomes zero.
7254 */
Bram Moolenaar82139082011-09-14 16:52:09 +02007255 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007256dict_unref(dict_T *d)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007257{
Bram Moolenaar685295c2006-10-15 20:37:38 +00007258 if (d != NULL && --d->dv_refcount <= 0)
7259 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007260}
7261
7262/*
Bram Moolenaare71eea82015-02-03 17:10:06 +01007263 * Free a Dictionary, including all non-container items it contains.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007264 * Ignores the reference count.
7265 */
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02007266 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007267dict_free(
7268 dict_T *d,
7269 int recurse) /* Free Lists and Dictionaries recursively. */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007270{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007271 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007272 hashitem_T *hi;
Bram Moolenaard9fba312005-06-26 22:34:35 +00007273 dictitem_T *di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007274
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +00007275 /* Remove the dict from the list of dicts for garbage collection. */
7276 if (d->dv_used_prev == NULL)
7277 first_dict = d->dv_used_next;
7278 else
7279 d->dv_used_prev->dv_used_next = d->dv_used_next;
7280 if (d->dv_used_next != NULL)
7281 d->dv_used_next->dv_used_prev = d->dv_used_prev;
7282
7283 /* Lock the hashtab, we don't want it to resize while freeing items. */
Bram Moolenaard9fba312005-06-26 22:34:35 +00007284 hash_lock(&d->dv_hashtab);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007285 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +00007286 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007287 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007288 if (!HASHITEM_EMPTY(hi))
7289 {
Bram Moolenaard9fba312005-06-26 22:34:35 +00007290 /* Remove the item before deleting it, just in case there is
7291 * something recursive causing trouble. */
7292 di = HI2DI(hi);
7293 hash_remove(&d->dv_hashtab, hi);
Bram Moolenaar685295c2006-10-15 20:37:38 +00007294 if (recurse || (di->di_tv.v_type != VAR_LIST
7295 && di->di_tv.v_type != VAR_DICT))
7296 clear_tv(&di->di_tv);
7297 vim_free(di);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007298 --todo;
7299 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007300 }
Bram Moolenaar33570922005-01-25 22:26:29 +00007301 hash_clear(&d->dv_hashtab);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007302 vim_free(d);
7303}
7304
7305/*
7306 * Allocate a Dictionary item.
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007307 * The "key" is copied to the new item.
7308 * Note that the value of the item "di_tv" still needs to be initialized!
7309 * Returns NULL when out of memory.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007310 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007311 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007312dictitem_alloc(char_u *key)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007313{
Bram Moolenaar33570922005-01-25 22:26:29 +00007314 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007315
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007316 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T) + STRLEN(key)));
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007317 if (di != NULL)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007318 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007319 STRCPY(di->di_key, key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007320 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007321 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007322 return di;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007323}
7324
7325/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007326 * Make a copy of a Dictionary item.
7327 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007328 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007329dictitem_copy(dictitem_T *org)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007330{
Bram Moolenaar33570922005-01-25 22:26:29 +00007331 dictitem_T *di;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007332
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007333 di = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
7334 + STRLEN(org->di_key)));
Bram Moolenaare9a41262005-01-15 22:18:47 +00007335 if (di != NULL)
7336 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007337 STRCPY(di->di_key, org->di_key);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007338 di->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007339 copy_tv(&org->di_tv, &di->di_tv);
7340 }
7341 return di;
7342}
7343
7344/*
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007345 * Remove item "item" from Dictionary "dict" and free it.
7346 */
7347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007348dictitem_remove(dict_T *dict, dictitem_T *item)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007349{
Bram Moolenaar33570922005-01-25 22:26:29 +00007350 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007351
Bram Moolenaar33570922005-01-25 22:26:29 +00007352 hi = hash_find(&dict->dv_hashtab, item->di_key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007353 if (HASHITEM_EMPTY(hi))
7354 EMSG2(_(e_intern2), "dictitem_remove()");
7355 else
Bram Moolenaar33570922005-01-25 22:26:29 +00007356 hash_remove(&dict->dv_hashtab, hi);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007357 dictitem_free(item);
7358}
7359
7360/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007361 * Free a dict item. Also clears the value.
7362 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007363 void
Bram Moolenaar7454a062016-01-30 15:14:10 +01007364dictitem_free(dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007365{
Bram Moolenaar8c711452005-01-14 21:53:12 +00007366 clear_tv(&item->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +02007367 if (item->di_flags & DI_FLAGS_ALLOC)
7368 vim_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007369}
7370
7371/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007372 * Make a copy of dict "d". Shallow if "deep" is FALSE.
7373 * The refcount of the new dict is set to 1.
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007374 * See item_copy() for "copyID".
Bram Moolenaare9a41262005-01-15 22:18:47 +00007375 * Returns NULL when out of memory.
7376 */
Bram Moolenaar33570922005-01-25 22:26:29 +00007377 static dict_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007378dict_copy(dict_T *orig, int deep, int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007379{
Bram Moolenaar33570922005-01-25 22:26:29 +00007380 dict_T *copy;
7381 dictitem_T *di;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007382 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +00007383 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007384
7385 if (orig == NULL)
7386 return NULL;
7387
7388 copy = dict_alloc();
7389 if (copy != NULL)
7390 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007391 if (copyID != 0)
7392 {
7393 orig->dv_copyID = copyID;
7394 orig->dv_copydict = copy;
7395 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007396 todo = (int)orig->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007397 for (hi = orig->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007398 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007399 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +00007400 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007401 --todo;
7402
7403 di = dictitem_alloc(hi->hi_key);
7404 if (di == NULL)
7405 break;
7406 if (deep)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007407 {
7408 if (item_copy(&HI2DI(hi)->di_tv, &di->di_tv, deep,
7409 copyID) == FAIL)
7410 {
7411 vim_free(di);
7412 break;
7413 }
7414 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007415 else
7416 copy_tv(&HI2DI(hi)->di_tv, &di->di_tv);
7417 if (dict_add(copy, di) == FAIL)
7418 {
7419 dictitem_free(di);
7420 break;
7421 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007422 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007423 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007424
Bram Moolenaare9a41262005-01-15 22:18:47 +00007425 ++copy->dv_refcount;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007426 if (todo > 0)
7427 {
7428 dict_unref(copy);
7429 copy = NULL;
7430 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007431 }
7432
7433 return copy;
7434}
7435
7436/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007437 * Add item "item" to Dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007438 * Returns FAIL when out of memory and when key already exists.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007439 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +01007440 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007441dict_add(dict_T *d, dictitem_T *item)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007442{
Bram Moolenaar33570922005-01-25 22:26:29 +00007443 return hash_add(&d->dv_hashtab, item->di_key);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007444}
7445
Bram Moolenaar8c711452005-01-14 21:53:12 +00007446/*
Bram Moolenaar05159a02005-02-26 23:04:13 +00007447 * Add a number or string entry to dictionary "d".
7448 * When "str" is NULL use number "nr", otherwise use "str".
7449 * Returns FAIL when out of memory and when key already exists.
7450 */
7451 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007452dict_add_nr_str(
7453 dict_T *d,
7454 char *key,
7455 long nr,
7456 char_u *str)
Bram Moolenaar05159a02005-02-26 23:04:13 +00007457{
7458 dictitem_T *item;
7459
7460 item = dictitem_alloc((char_u *)key);
7461 if (item == NULL)
7462 return FAIL;
7463 item->di_tv.v_lock = 0;
7464 if (str == NULL)
7465 {
7466 item->di_tv.v_type = VAR_NUMBER;
7467 item->di_tv.vval.v_number = nr;
7468 }
7469 else
7470 {
7471 item->di_tv.v_type = VAR_STRING;
7472 item->di_tv.vval.v_string = vim_strsave(str);
7473 }
7474 if (dict_add(d, item) == FAIL)
7475 {
7476 dictitem_free(item);
7477 return FAIL;
7478 }
7479 return OK;
7480}
7481
7482/*
Bram Moolenaar217d2852010-09-14 12:47:37 +02007483 * Add a list entry to dictionary "d".
Bram Moolenaara800b422010-06-27 01:15:55 +02007484 * Returns FAIL when out of memory and when key already exists.
7485 */
7486 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007487dict_add_list(dict_T *d, char *key, list_T *list)
Bram Moolenaara800b422010-06-27 01:15:55 +02007488{
7489 dictitem_T *item;
7490
7491 item = dictitem_alloc((char_u *)key);
7492 if (item == NULL)
7493 return FAIL;
7494 item->di_tv.v_lock = 0;
7495 item->di_tv.v_type = VAR_LIST;
7496 item->di_tv.vval.v_list = list;
7497 if (dict_add(d, item) == FAIL)
7498 {
7499 dictitem_free(item);
7500 return FAIL;
7501 }
Bram Moolenaar217d2852010-09-14 12:47:37 +02007502 ++list->lv_refcount;
Bram Moolenaara800b422010-06-27 01:15:55 +02007503 return OK;
7504}
7505
7506/*
Bram Moolenaare9a41262005-01-15 22:18:47 +00007507 * Get the number of items in a Dictionary.
7508 */
7509 static long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007510dict_len(dict_T *d)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007511{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007512 if (d == NULL)
7513 return 0L;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007514 return (long)d->dv_hashtab.ht_used;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007515}
7516
7517/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007518 * Find item "key[len]" in Dictionary "d".
7519 * If "len" is negative use strlen(key).
7520 * Returns NULL when not found.
7521 */
Bram Moolenaar8bcf9652010-06-12 20:12:02 +02007522 dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007523dict_find(dict_T *d, char_u *key, int len)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007524{
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007525#define AKEYLEN 200
7526 char_u buf[AKEYLEN];
7527 char_u *akey;
7528 char_u *tofree = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007529 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007530
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007531 if (len < 0)
7532 akey = key;
7533 else if (len >= AKEYLEN)
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007534 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007535 tofree = akey = vim_strnsave(key, len);
7536 if (akey == NULL)
7537 return NULL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +00007538 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007539 else
7540 {
7541 /* Avoid a malloc/free by using buf[]. */
Bram Moolenaarce0842a2005-07-18 21:58:11 +00007542 vim_strncpy(buf, key, len);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007543 akey = buf;
7544 }
7545
Bram Moolenaar33570922005-01-25 22:26:29 +00007546 hi = hash_find(&d->dv_hashtab, akey);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007547 vim_free(tofree);
7548 if (HASHITEM_EMPTY(hi))
7549 return NULL;
7550 return HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007551}
7552
7553/*
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007554 * Get a string item from a dictionary.
7555 * When "save" is TRUE allocate memory for it.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007556 * Returns NULL if the entry doesn't exist or out of memory.
7557 */
7558 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007559get_dict_string(dict_T *d, char_u *key, int save)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007560{
7561 dictitem_T *di;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007562 char_u *s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007563
7564 di = dict_find(d, key, -1);
7565 if (di == NULL)
7566 return NULL;
Bram Moolenaar8b6144b2006-02-08 09:20:24 +00007567 s = get_tv_string(&di->di_tv);
7568 if (save && s != NULL)
7569 s = vim_strsave(s);
7570 return s;
Bram Moolenaar2641f772005-03-25 21:58:17 +00007571}
7572
7573/*
7574 * Get a number item from a dictionary.
Bram Moolenaarba093bc2016-02-16 19:37:29 +01007575 * Returns 0 if the entry doesn't exist.
Bram Moolenaar2641f772005-03-25 21:58:17 +00007576 */
7577 long
Bram Moolenaar7454a062016-01-30 15:14:10 +01007578get_dict_number(dict_T *d, char_u *key)
Bram Moolenaar2641f772005-03-25 21:58:17 +00007579{
7580 dictitem_T *di;
7581
7582 di = dict_find(d, key, -1);
7583 if (di == NULL)
7584 return 0;
7585 return get_tv_number(&di->di_tv);
7586}
7587
7588/*
Bram Moolenaar8c711452005-01-14 21:53:12 +00007589 * Return an allocated string with the string representation of a Dictionary.
7590 * May return NULL.
7591 */
7592 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007593dict2string(typval_T *tv, int copyID)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007594{
7595 garray_T ga;
7596 int first = TRUE;
7597 char_u *tofree;
7598 char_u numbuf[NUMBUFLEN];
Bram Moolenaar33570922005-01-25 22:26:29 +00007599 hashitem_T *hi;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007600 char_u *s;
Bram Moolenaar33570922005-01-25 22:26:29 +00007601 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007602 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007603
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007604 if ((d = tv->vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007605 return NULL;
7606 ga_init2(&ga, (int)sizeof(char), 80);
7607 ga_append(&ga, '{');
7608
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007609 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007610 for (hi = d->dv_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007611 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007612 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +00007613 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007614 --todo;
7615
7616 if (first)
7617 first = FALSE;
7618 else
7619 ga_concat(&ga, (char_u *)", ");
7620
7621 tofree = string_quote(hi->hi_key, FALSE);
7622 if (tofree != NULL)
7623 {
7624 ga_concat(&ga, tofree);
7625 vim_free(tofree);
7626 }
7627 ga_concat(&ga, (char_u *)": ");
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007628 s = tv2string(&HI2DI(hi)->di_tv, &tofree, numbuf, copyID);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007629 if (s != NULL)
7630 ga_concat(&ga, s);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007631 vim_free(tofree);
Bram Moolenaar8502c702014-06-17 12:51:16 +02007632 if (s == NULL || did_echo_string_emsg)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007633 break;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007634 line_breakcheck();
7635
Bram Moolenaar8c711452005-01-14 21:53:12 +00007636 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007637 }
Bram Moolenaar81bf7082005-02-12 14:31:42 +00007638 if (todo > 0)
7639 {
7640 vim_free(ga.ga_data);
7641 return NULL;
7642 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007643
7644 ga_append(&ga, '}');
7645 ga_append(&ga, NUL);
7646 return (char_u *)ga.ga_data;
7647}
7648
7649/*
7650 * Allocate a variable for a Dictionary and fill it from "*arg".
7651 * Return OK or FAIL. Returns NOTDONE for {expr}.
7652 */
7653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007654get_dict_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007655{
Bram Moolenaar33570922005-01-25 22:26:29 +00007656 dict_T *d = NULL;
7657 typval_T tvkey;
7658 typval_T tv;
Bram Moolenaarad6c2272007-09-17 20:21:33 +00007659 char_u *key = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00007660 dictitem_T *item;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007661 char_u *start = skipwhite(*arg + 1);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007662 char_u buf[NUMBUFLEN];
Bram Moolenaar8c711452005-01-14 21:53:12 +00007663
7664 /*
7665 * First check if it's not a curly-braces thing: {expr}.
7666 * Must do this without evaluating, otherwise a function may be called
7667 * twice. Unfortunately this means we need to call eval1() twice for the
7668 * first item.
Bram Moolenaare9a41262005-01-15 22:18:47 +00007669 * But {} is an empty Dictionary.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007670 */
Bram Moolenaare9a41262005-01-15 22:18:47 +00007671 if (*start != '}')
7672 {
7673 if (eval1(&start, &tv, FALSE) == FAIL) /* recursive! */
7674 return FAIL;
7675 if (*start == '}')
7676 return NOTDONE;
7677 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007678
7679 if (evaluate)
7680 {
7681 d = dict_alloc();
7682 if (d == NULL)
7683 return FAIL;
7684 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007685 tvkey.v_type = VAR_UNKNOWN;
7686 tv.v_type = VAR_UNKNOWN;
Bram Moolenaar8c711452005-01-14 21:53:12 +00007687
7688 *arg = skipwhite(*arg + 1);
7689 while (**arg != '}' && **arg != NUL)
7690 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007691 if (eval1(arg, &tvkey, evaluate) == FAIL) /* recursive! */
Bram Moolenaar8c711452005-01-14 21:53:12 +00007692 goto failret;
7693 if (**arg != ':')
7694 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007695 EMSG2(_("E720: Missing colon in Dictionary: %s"), *arg);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007696 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007697 goto failret;
7698 }
Bram Moolenaar037cc642007-09-13 18:40:54 +00007699 if (evaluate)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007700 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007701 key = get_tv_string_buf_chk(&tvkey, buf);
7702 if (key == NULL || *key == NUL)
7703 {
7704 /* "key" is NULL when get_tv_string_buf_chk() gave an errmsg */
7705 if (key != NULL)
7706 EMSG(_(e_emptykey));
7707 clear_tv(&tvkey);
7708 goto failret;
7709 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007710 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007711
7712 *arg = skipwhite(*arg + 1);
7713 if (eval1(arg, &tv, evaluate) == FAIL) /* recursive! */
7714 {
Bram Moolenaar037cc642007-09-13 18:40:54 +00007715 if (evaluate)
7716 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007717 goto failret;
7718 }
7719 if (evaluate)
7720 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007721 item = dict_find(d, key, -1);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007722 if (item != NULL)
7723 {
Bram Moolenaarb982ca52005-03-28 21:02:15 +00007724 EMSG2(_("E721: Duplicate key in Dictionary: \"%s\""), key);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007725 clear_tv(&tvkey);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007726 clear_tv(&tv);
7727 goto failret;
7728 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007729 item = dictitem_alloc(key);
7730 clear_tv(&tvkey);
7731 if (item != NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +00007732 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007733 item->di_tv = tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00007734 item->di_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +00007735 if (dict_add(d, item) == FAIL)
7736 dictitem_free(item);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007737 }
7738 }
7739
7740 if (**arg == '}')
7741 break;
7742 if (**arg != ',')
7743 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007744 EMSG2(_("E722: Missing comma in Dictionary: %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007745 goto failret;
7746 }
7747 *arg = skipwhite(*arg + 1);
7748 }
7749
7750 if (**arg != '}')
7751 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00007752 EMSG2(_("E723: Missing end of Dictionary '}': %s"), *arg);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007753failret:
7754 if (evaluate)
Bram Moolenaar685295c2006-10-15 20:37:38 +00007755 dict_free(d, TRUE);
Bram Moolenaar8c711452005-01-14 21:53:12 +00007756 return FAIL;
7757 }
7758
7759 *arg = skipwhite(*arg + 1);
7760 if (evaluate)
7761 {
7762 rettv->v_type = VAR_DICT;
7763 rettv->vval.v_dict = d;
7764 ++d->dv_refcount;
7765 }
7766
7767 return OK;
7768}
7769
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01007770#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar835dc632016-02-07 14:27:38 +01007771#endif
7772
Bram Moolenaar17a13432016-01-24 14:22:10 +01007773 static char *
7774get_var_special_name(int nr)
7775{
7776 switch (nr)
7777 {
Bram Moolenaarf48aa162016-01-24 17:54:24 +01007778 case VVAL_FALSE: return "v:false";
Bram Moolenaar65edff82016-02-21 16:40:11 +01007779 case VVAL_TRUE: return "v:true";
7780 case VVAL_NONE: return "v:none";
7781 case VVAL_NULL: return "v:null";
Bram Moolenaar17a13432016-01-24 14:22:10 +01007782 }
7783 EMSG2(_(e_intern2), "get_var_special_name()");
7784 return "42";
7785}
7786
Bram Moolenaar8c711452005-01-14 21:53:12 +00007787/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007788 * Return a string with the string representation of a variable.
7789 * If the memory is allocated "tofree" is set to it, otherwise NULL.
Bram Moolenaar8a283e52005-01-06 23:28:25 +00007790 * "numbuf" is used for a number.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007791 * Does not put quotes around strings, as ":echo" displays values.
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007792 * When "copyID" is not NULL replace recursive lists and dicts with "...".
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007793 * May return NULL.
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007794 */
7795 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007796echo_string(
7797 typval_T *tv,
7798 char_u **tofree,
7799 char_u *numbuf,
7800 int copyID)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007801{
Bram Moolenaare9a41262005-01-15 22:18:47 +00007802 static int recurse = 0;
7803 char_u *r = NULL;
7804
Bram Moolenaar33570922005-01-25 22:26:29 +00007805 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +00007806 {
Bram Moolenaar8502c702014-06-17 12:51:16 +02007807 if (!did_echo_string_emsg)
7808 {
7809 /* Only give this message once for a recursive call to avoid
7810 * flooding the user with errors. And stop iterating over lists
7811 * and dicts. */
7812 did_echo_string_emsg = TRUE;
7813 EMSG(_("E724: variable nested too deep for displaying"));
7814 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007815 *tofree = NULL;
Bram Moolenaar8502c702014-06-17 12:51:16 +02007816 return (char_u *)"{E724}";
Bram Moolenaare9a41262005-01-15 22:18:47 +00007817 }
7818 ++recurse;
7819
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007820 switch (tv->v_type)
7821 {
7822 case VAR_FUNC:
7823 *tofree = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007824 r = tv->vval.v_string;
7825 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007826
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007827 case VAR_PARTIAL:
7828 *tofree = NULL;
7829 /* TODO: arguments */
7830 r = tv->vval.v_partial == NULL ? NULL : tv->vval.v_partial->pt_name;
7831 break;
7832
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007833 case VAR_LIST:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007834 if (tv->vval.v_list == NULL)
7835 {
7836 *tofree = NULL;
7837 r = NULL;
7838 }
7839 else if (copyID != 0 && tv->vval.v_list->lv_copyID == copyID)
7840 {
7841 *tofree = NULL;
7842 r = (char_u *)"[...]";
7843 }
7844 else
7845 {
7846 tv->vval.v_list->lv_copyID = copyID;
7847 *tofree = list2string(tv, copyID);
7848 r = *tofree;
7849 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007850 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007851
Bram Moolenaar8c711452005-01-14 21:53:12 +00007852 case VAR_DICT:
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007853 if (tv->vval.v_dict == NULL)
7854 {
7855 *tofree = NULL;
7856 r = NULL;
7857 }
7858 else if (copyID != 0 && tv->vval.v_dict->dv_copyID == copyID)
7859 {
7860 *tofree = NULL;
7861 r = (char_u *)"{...}";
7862 }
7863 else
7864 {
7865 tv->vval.v_dict->dv_copyID = copyID;
7866 *tofree = dict2string(tv, copyID);
7867 r = *tofree;
7868 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007869 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007870
Bram Moolenaarc70646c2005-01-04 21:52:38 +00007871 case VAR_STRING:
7872 case VAR_NUMBER:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007873 case VAR_UNKNOWN:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007875 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007876 *tofree = NULL;
7877 r = get_tv_string_buf(tv, numbuf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007878 break;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007879
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007880 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007881#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007882 *tofree = NULL;
7883 vim_snprintf((char *)numbuf, NUMBUFLEN, "%g", tv->vval.v_float);
7884 r = numbuf;
7885 break;
7886#endif
7887
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007888 case VAR_SPECIAL:
7889 *tofree = NULL;
Bram Moolenaar17a13432016-01-24 14:22:10 +01007890 r = (char_u *)get_var_special_name(tv->vval.v_number);
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007891 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007892 }
Bram Moolenaare9a41262005-01-15 22:18:47 +00007893
Bram Moolenaar8502c702014-06-17 12:51:16 +02007894 if (--recurse == 0)
7895 did_echo_string_emsg = FALSE;
Bram Moolenaare9a41262005-01-15 22:18:47 +00007896 return r;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007897}
7898
7899/*
7900 * Return a string with the string representation of a variable.
7901 * If the memory is allocated "tofree" is set to it, otherwise NULL.
7902 * "numbuf" is used for a number.
7903 * Puts quotes around strings, so that they can be parsed back by eval().
Bram Moolenaar92c5aba2007-08-14 20:29:31 +00007904 * May return NULL.
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007905 */
7906 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007907tv2string(
7908 typval_T *tv,
7909 char_u **tofree,
7910 char_u *numbuf,
7911 int copyID)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007912{
7913 switch (tv->v_type)
7914 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007915 case VAR_FUNC:
7916 *tofree = string_quote(tv->vval.v_string, TRUE);
7917 return *tofree;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01007918 case VAR_PARTIAL:
7919 *tofree = string_quote(tv->vval.v_partial == NULL ? NULL
7920 : tv->vval.v_partial->pt_name, TRUE);
7921 return *tofree;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007922 case VAR_STRING:
7923 *tofree = string_quote(tv->vval.v_string, FALSE);
7924 return *tofree;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007925 case VAR_FLOAT:
Bram Moolenaar5fac4672016-03-02 22:16:32 +01007926#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007927 *tofree = NULL;
7928 vim_snprintf((char *)numbuf, NUMBUFLEN - 1, "%g", tv->vval.v_float);
7929 return numbuf;
7930#endif
Bram Moolenaare9a41262005-01-15 22:18:47 +00007931 case VAR_NUMBER:
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007932 case VAR_LIST:
Bram Moolenaar8c711452005-01-14 21:53:12 +00007933 case VAR_DICT:
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007934 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +01007935 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +01007936 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +01007937 case VAR_UNKNOWN:
Bram Moolenaare9a41262005-01-15 22:18:47 +00007938 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007939 }
Bram Moolenaarb71eaae2006-01-20 23:10:18 +00007940 return echo_string(tv, tofree, numbuf, copyID);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00007941}
7942
7943/*
Bram Moolenaar33570922005-01-25 22:26:29 +00007944 * Return string "str" in ' quotes, doubling ' characters.
7945 * If "str" is NULL an empty string is assumed.
Bram Moolenaar8c711452005-01-14 21:53:12 +00007946 * If "function" is TRUE make it function('string').
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007947 */
7948 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01007949string_quote(char_u *str, int function)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007950{
Bram Moolenaar33570922005-01-25 22:26:29 +00007951 unsigned len;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007952 char_u *p, *r, *s;
7953
Bram Moolenaar33570922005-01-25 22:26:29 +00007954 len = (function ? 13 : 3);
7955 if (str != NULL)
7956 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00007957 len += (unsigned)STRLEN(str);
Bram Moolenaar33570922005-01-25 22:26:29 +00007958 for (p = str; *p != NUL; mb_ptr_adv(p))
7959 if (*p == '\'')
7960 ++len;
7961 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007962 s = r = alloc(len);
7963 if (r != NULL)
7964 {
7965 if (function)
7966 {
Bram Moolenaar8c711452005-01-14 21:53:12 +00007967 STRCPY(r, "function('");
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007968 r += 10;
7969 }
7970 else
Bram Moolenaar8c711452005-01-14 21:53:12 +00007971 *r++ = '\'';
Bram Moolenaar33570922005-01-25 22:26:29 +00007972 if (str != NULL)
7973 for (p = str; *p != NUL; )
7974 {
7975 if (*p == '\'')
7976 *r++ = '\'';
7977 MB_COPY_CHAR(p, r);
7978 }
Bram Moolenaar8c711452005-01-14 21:53:12 +00007979 *r++ = '\'';
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00007980 if (function)
7981 *r++ = ')';
7982 *r++ = NUL;
7983 }
7984 return s;
7985}
7986
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007987#if defined(FEAT_FLOAT) || defined(PROTO)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007988/*
7989 * Convert the string "text" to a floating point number.
7990 * This uses strtod(). setlocale(LC_NUMERIC, "C") has been used to make sure
7991 * this always uses a decimal point.
7992 * Returns the length of the text that was consumed.
7993 */
Bram Moolenaar520e1e42016-01-23 19:46:28 +01007994 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01007995string2float(
7996 char_u *text,
7997 float_T *value) /* result stored here */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00007998{
7999 char *s = (char *)text;
8000 float_T f;
8001
8002 f = strtod(s, &s);
8003 *value = f;
8004 return (int)((char_u *)s - text);
8005}
8006#endif
8007
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008008/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008009 * Get the value of an environment variable.
8010 * "arg" is pointing to the '$'. It is advanced to after the name.
8011 * If the environment variable was not set, silently assume it is empty.
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008012 * Return FAIL if the name is invalid.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008013 */
8014 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008015get_env_tv(char_u **arg, typval_T *rettv, int evaluate)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008016{
8017 char_u *string = NULL;
8018 int len;
8019 int cc;
8020 char_u *name;
Bram Moolenaar05159a02005-02-26 23:04:13 +00008021 int mustfree = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008022
8023 ++*arg;
8024 name = *arg;
8025 len = get_env_len(arg);
8026 if (evaluate)
8027 {
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008028 if (len == 0)
Bram Moolenaar615b9972015-01-14 17:15:05 +01008029 return FAIL; /* invalid empty name */
Bram Moolenaar05159a02005-02-26 23:04:13 +00008030
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008031 cc = name[len];
8032 name[len] = NUL;
8033 /* first try vim_getenv(), fast for normal environment vars */
8034 string = vim_getenv(name, &mustfree);
8035 if (string != NULL && *string != NUL)
8036 {
8037 if (!mustfree)
8038 string = vim_strsave(string);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008039 }
Bram Moolenaare512c8c2014-04-29 17:41:22 +02008040 else
8041 {
8042 if (mustfree)
8043 vim_free(string);
8044
8045 /* next try expanding things like $VIM and ${HOME} */
8046 string = expand_env_save(name - 1);
8047 if (string != NULL && *string == '$')
8048 {
8049 vim_free(string);
8050 string = NULL;
8051 }
8052 }
8053 name[len] = cc;
8054
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008055 rettv->v_type = VAR_STRING;
8056 rettv->vval.v_string = string;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008057 }
8058
8059 return OK;
8060}
8061
8062/*
8063 * Array with names and number of arguments of all internal functions
8064 * MUST BE KEPT SORTED IN strcmp() ORDER FOR BINARY SEARCH!
8065 */
8066static struct fst
8067{
8068 char *f_name; /* function name */
8069 char f_min_argc; /* minimal number of arguments */
8070 char f_max_argc; /* maximal number of arguments */
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008071 void (*f_func)(typval_T *args, typval_T *rvar);
Bram Moolenaarbae0c162007-05-10 19:30:25 +00008072 /* implementation of function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008073} functions[] =
8074{
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008075#ifdef FEAT_FLOAT
8076 {"abs", 1, 1, f_abs},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008077 {"acos", 1, 1, f_acos}, /* WJMc */
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008078#endif
Bram Moolenaar0d660222005-01-07 21:51:51 +00008079 {"add", 2, 2, f_add},
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01008080 {"alloc_fail", 3, 3, f_alloc_fail},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008081 {"and", 2, 2, f_and},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008082 {"append", 2, 2, f_append},
8083 {"argc", 0, 0, f_argc},
8084 {"argidx", 0, 0, f_argidx},
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02008085 {"arglistid", 0, 2, f_arglistid},
Bram Moolenaare2f98b92006-03-29 21:18:24 +00008086 {"argv", 0, 1, f_argv},
Bram Moolenaar099fdde2015-12-13 14:45:21 +01008087#ifdef FEAT_FLOAT
8088 {"asin", 1, 1, f_asin}, /* WJMc */
8089#endif
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008090 {"assert_equal", 2, 3, f_assert_equal},
Bram Moolenaara803c7f2016-01-15 15:31:39 +01008091 {"assert_exception", 1, 2, f_assert_exception},
Bram Moolenaara260b872016-01-15 20:48:22 +01008092 {"assert_fails", 1, 2, f_assert_fails},
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01008093 {"assert_false", 1, 2, f_assert_false},
8094 {"assert_true", 1, 2, f_assert_true},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008095#ifdef FEAT_FLOAT
8096 {"atan", 1, 1, f_atan},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008097 {"atan2", 2, 2, f_atan2},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008098#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008099 {"browse", 4, 4, f_browse},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008100 {"browsedir", 2, 2, f_browsedir},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008101 {"bufexists", 1, 1, f_bufexists},
8102 {"buffer_exists", 1, 1, f_bufexists}, /* obsolete */
8103 {"buffer_name", 1, 1, f_bufname}, /* obsolete */
8104 {"buffer_number", 1, 1, f_bufnr}, /* obsolete */
8105 {"buflisted", 1, 1, f_buflisted},
8106 {"bufloaded", 1, 1, f_bufloaded},
8107 {"bufname", 1, 1, f_bufname},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008108 {"bufnr", 1, 2, f_bufnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008109 {"bufwinnr", 1, 1, f_bufwinnr},
8110 {"byte2line", 1, 1, f_byte2line},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008111 {"byteidx", 2, 2, f_byteidx},
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01008112 {"byteidxcomp", 2, 2, f_byteidxcomp},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008113 {"call", 2, 3, f_call},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008114#ifdef FEAT_FLOAT
8115 {"ceil", 1, 1, f_ceil},
8116#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008117#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008118 {"ch_close", 1, 1, f_ch_close},
Bram Moolenaar8b1862a2016-02-27 19:21:24 +01008119 {"ch_evalexpr", 2, 3, f_ch_evalexpr},
8120 {"ch_evalraw", 2, 3, f_ch_evalraw},
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01008121 {"ch_getbufnr", 2, 2, f_ch_getbufnr},
Bram Moolenaar02e83b42016-02-21 20:10:26 +01008122 {"ch_getjob", 1, 1, f_ch_getjob},
Bram Moolenaar81661fb2016-02-18 22:23:34 +01008123 {"ch_log", 1, 2, f_ch_log},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008124 {"ch_logfile", 1, 2, f_ch_logfile},
Bram Moolenaar4d919d72016-02-05 22:36:41 +01008125 {"ch_open", 1, 2, f_ch_open},
Bram Moolenaar6f3a5442016-02-20 19:56:13 +01008126 {"ch_read", 1, 2, f_ch_read},
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008127 {"ch_readraw", 1, 2, f_ch_readraw},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008128 {"ch_sendexpr", 2, 3, f_ch_sendexpr},
8129 {"ch_sendraw", 2, 3, f_ch_sendraw},
Bram Moolenaar40ea1da2016-02-19 22:33:35 +01008130 {"ch_setoptions", 2, 2, f_ch_setoptions},
Bram Moolenaar77073442016-02-13 23:23:53 +01008131 {"ch_status", 1, 1, f_ch_status},
Bram Moolenaarf57969a2016-02-02 20:47:49 +01008132#endif
Bram Moolenaarf0acfce2006-03-17 23:21:19 +00008133 {"changenr", 0, 0, f_changenr},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008134 {"char2nr", 1, 2, f_char2nr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008135 {"cindent", 1, 1, f_cindent},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008136 {"clearmatches", 0, 0, f_clearmatches},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008137 {"col", 1, 1, f_col},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008138#if defined(FEAT_INS_EXPAND)
Bram Moolenaarade00832006-03-10 21:46:58 +00008139 {"complete", 2, 2, f_complete},
Bram Moolenaar572cb562005-08-05 21:35:02 +00008140 {"complete_add", 1, 1, f_complete_add},
8141 {"complete_check", 0, 0, f_complete_check},
8142#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008143 {"confirm", 1, 4, f_confirm},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008144 {"copy", 1, 1, f_copy},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008145#ifdef FEAT_FLOAT
8146 {"cos", 1, 1, f_cos},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008147 {"cosh", 1, 1, f_cosh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008148#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008149 {"count", 2, 4, f_count},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008150 {"cscope_connection",0,3, f_cscope_connection},
Bram Moolenaara5525202006-03-02 22:52:09 +00008151 {"cursor", 1, 3, f_cursor},
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008152 {"deepcopy", 1, 2, f_deepcopy},
Bram Moolenaarda440d22016-01-16 21:27:23 +01008153 {"delete", 1, 2, f_delete},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008154 {"did_filetype", 0, 0, f_did_filetype},
Bram Moolenaar47136d72004-10-12 20:02:24 +00008155 {"diff_filler", 1, 1, f_diff_filler},
8156 {"diff_hlID", 2, 2, f_diff_hlID},
Bram Moolenaar2ab375e2016-02-10 22:23:06 +01008157 {"disable_char_avail_for_testing", 1, 1, f_disable_char_avail_for_testing},
Bram Moolenaare49b69a2005-01-08 16:11:57 +00008158 {"empty", 1, 1, f_empty},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008159 {"escape", 2, 2, f_escape},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008160 {"eval", 1, 1, f_eval},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008161 {"eventhandler", 0, 0, f_eventhandler},
8162 {"executable", 1, 1, f_executable},
Bram Moolenaarc7f02552014-04-01 21:00:59 +02008163 {"exepath", 1, 1, f_exepath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008164 {"exists", 1, 1, f_exists},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008165#ifdef FEAT_FLOAT
8166 {"exp", 1, 1, f_exp},
8167#endif
Bram Moolenaar146e9c32012-03-07 19:18:23 +01008168 {"expand", 1, 3, f_expand},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008169 {"extend", 2, 3, f_extend},
Bram Moolenaarf9393ef2006-04-24 19:47:27 +00008170 {"feedkeys", 1, 2, f_feedkeys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008171 {"file_readable", 1, 1, f_filereadable}, /* obsolete */
8172 {"filereadable", 1, 1, f_filereadable},
8173 {"filewritable", 1, 1, f_filewritable},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008174 {"filter", 2, 2, f_filter},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008175 {"finddir", 1, 3, f_finddir},
8176 {"findfile", 1, 3, f_findfile},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008177#ifdef FEAT_FLOAT
8178 {"float2nr", 1, 1, f_float2nr},
8179 {"floor", 1, 1, f_floor},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008180 {"fmod", 2, 2, f_fmod},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008181#endif
Bram Moolenaaraebaf892008-05-28 14:49:58 +00008182 {"fnameescape", 1, 1, f_fnameescape},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008183 {"fnamemodify", 2, 2, f_fnamemodify},
8184 {"foldclosed", 1, 1, f_foldclosed},
8185 {"foldclosedend", 1, 1, f_foldclosedend},
8186 {"foldlevel", 1, 1, f_foldlevel},
8187 {"foldtext", 0, 0, f_foldtext},
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00008188 {"foldtextresult", 1, 1, f_foldtextresult},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008189 {"foreground", 0, 0, f_foreground},
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008190 {"function", 1, 3, f_function},
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +00008191 {"garbagecollect", 0, 1, f_garbagecollect},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008192 {"get", 2, 3, f_get},
Bram Moolenaar80fc0432005-07-20 22:06:07 +00008193 {"getbufline", 2, 3, f_getbufline},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008194 {"getbufvar", 2, 3, f_getbufvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008195 {"getchar", 0, 1, f_getchar},
8196 {"getcharmod", 0, 0, f_getcharmod},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008197 {"getcharsearch", 0, 0, f_getcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008198 {"getcmdline", 0, 0, f_getcmdline},
8199 {"getcmdpos", 0, 0, f_getcmdpos},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008200 {"getcmdtype", 0, 0, f_getcmdtype},
Bram Moolenaar8c1329c2014-08-06 13:36:59 +02008201 {"getcmdwintype", 0, 0, f_getcmdwintype},
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +02008202 {"getcurpos", 0, 0, f_getcurpos},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008203 {"getcwd", 0, 2, f_getcwd},
Bram Moolenaar46c9c732004-12-12 11:37:09 +00008204 {"getfontname", 0, 1, f_getfontname},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008205 {"getfperm", 1, 1, f_getfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008206 {"getfsize", 1, 1, f_getfsize},
8207 {"getftime", 1, 1, f_getftime},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008208 {"getftype", 1, 1, f_getftype},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008209 {"getline", 1, 2, f_getline},
Bram Moolenaar280f1262006-01-30 00:14:18 +00008210 {"getloclist", 1, 1, f_getqflist},
Bram Moolenaar2240aeb2007-07-27 19:33:14 +00008211 {"getmatches", 0, 0, f_getmatches},
Bram Moolenaar18081e32008-02-20 19:11:07 +00008212 {"getpid", 0, 0, f_getpid},
Bram Moolenaara5525202006-03-02 22:52:09 +00008213 {"getpos", 1, 1, f_getpos},
Bram Moolenaar2641f772005-03-25 21:58:17 +00008214 {"getqflist", 0, 0, f_getqflist},
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +02008215 {"getreg", 0, 3, f_getreg},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008216 {"getregtype", 0, 1, f_getregtype},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008217 {"gettabvar", 2, 3, f_gettabvar},
8218 {"gettabwinvar", 3, 4, f_gettabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008219 {"getwinposx", 0, 0, f_getwinposx},
8220 {"getwinposy", 0, 0, f_getwinposy},
Bram Moolenaar63dbda12013-02-20 21:12:10 +01008221 {"getwinvar", 2, 3, f_getwinvar},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008222 {"glob", 1, 4, f_glob},
Bram Moolenaar825e7ab2015-03-20 17:36:42 +01008223 {"glob2regpat", 1, 1, f_glob2regpat},
Bram Moolenaara245bc72015-03-05 19:35:25 +01008224 {"globpath", 2, 5, f_globpath},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008225 {"has", 1, 1, f_has},
Bram Moolenaare9a41262005-01-15 22:18:47 +00008226 {"has_key", 2, 2, f_has_key},
Bram Moolenaarc9703302016-01-17 21:49:33 +01008227 {"haslocaldir", 0, 2, f_haslocaldir},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008228 {"hasmapto", 1, 3, f_hasmapto},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008229 {"highlightID", 1, 1, f_hlID}, /* obsolete */
8230 {"highlight_exists",1, 1, f_hlexists}, /* obsolete */
8231 {"histadd", 2, 2, f_histadd},
8232 {"histdel", 1, 2, f_histdel},
8233 {"histget", 1, 2, f_histget},
8234 {"histnr", 1, 1, f_histnr},
8235 {"hlID", 1, 1, f_hlID},
8236 {"hlexists", 1, 1, f_hlexists},
8237 {"hostname", 0, 0, f_hostname},
8238 {"iconv", 3, 3, f_iconv},
8239 {"indent", 1, 1, f_indent},
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008240 {"index", 2, 4, f_index},
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +00008241 {"input", 1, 3, f_input},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008242 {"inputdialog", 1, 3, f_inputdialog},
Bram Moolenaar6efa2b32005-09-10 19:26:26 +00008243 {"inputlist", 1, 1, f_inputlist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008244 {"inputrestore", 0, 0, f_inputrestore},
8245 {"inputsave", 0, 0, f_inputsave},
8246 {"inputsecret", 1, 2, f_inputsecret},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008247 {"insert", 2, 3, f_insert},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008248 {"invert", 1, 1, f_invert},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008249 {"isdirectory", 1, 1, f_isdirectory},
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00008250 {"islocked", 1, 1, f_islocked},
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +01008251#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
8252 {"isnan", 1, 1, f_isnan},
8253#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008254 {"items", 1, 1, f_items},
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01008255#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar6463ca22016-02-13 17:04:46 +01008256 {"job_getchannel", 1, 1, f_job_getchannel},
Bram Moolenaar8950a562016-03-12 15:22:55 +01008257 {"job_info", 1, 1, f_job_info},
Bram Moolenaar65edff82016-02-21 16:40:11 +01008258 {"job_setoptions", 2, 2, f_job_setoptions},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008259 {"job_start", 1, 2, f_job_start},
8260 {"job_status", 1, 1, f_job_status},
Bram Moolenaar942d6b22016-02-07 19:57:16 +01008261 {"job_stop", 1, 2, f_job_stop},
Bram Moolenaar835dc632016-02-07 14:27:38 +01008262#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008263 {"join", 1, 2, f_join},
Bram Moolenaar7823a3b2016-02-11 21:08:32 +01008264 {"js_decode", 1, 1, f_js_decode},
8265 {"js_encode", 1, 1, f_js_encode},
8266 {"json_decode", 1, 1, f_json_decode},
8267 {"json_encode", 1, 1, f_json_encode},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008268 {"keys", 1, 1, f_keys},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008269 {"last_buffer_nr", 0, 0, f_last_buffer_nr},/* obsolete */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008270 {"len", 1, 1, f_len},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008271 {"libcall", 3, 3, f_libcall},
8272 {"libcallnr", 3, 3, f_libcallnr},
8273 {"line", 1, 1, f_line},
8274 {"line2byte", 1, 1, f_line2byte},
8275 {"lispindent", 1, 1, f_lispindent},
8276 {"localtime", 0, 0, f_localtime},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008277#ifdef FEAT_FLOAT
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008278 {"log", 1, 1, f_log},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008279 {"log10", 1, 1, f_log10},
8280#endif
Bram Moolenaar1dced572012-04-05 16:54:08 +02008281#ifdef FEAT_LUA
Bram Moolenaar9feaf622014-02-22 22:18:47 +01008282 {"luaeval", 1, 2, f_luaeval},
Bram Moolenaar1dced572012-04-05 16:54:08 +02008283#endif
Bram Moolenaar31c67ef2005-01-11 21:34:41 +00008284 {"map", 2, 2, f_map},
Bram Moolenaarbd743252010-10-20 21:23:33 +02008285 {"maparg", 1, 4, f_maparg},
Bram Moolenaar2c932302006-03-18 21:42:09 +00008286 {"mapcheck", 1, 3, f_mapcheck},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008287 {"match", 2, 4, f_match},
Bram Moolenaar6561d522015-07-21 15:48:27 +02008288 {"matchadd", 2, 5, f_matchadd},
8289 {"matchaddpos", 2, 5, f_matchaddpos},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008290 {"matcharg", 1, 1, f_matcharg},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008291 {"matchdelete", 1, 1, f_matchdelete},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008292 {"matchend", 2, 4, f_matchend},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008293 {"matchlist", 2, 4, f_matchlist},
Bram Moolenaar89cb5e02004-07-19 20:55:54 +00008294 {"matchstr", 2, 4, f_matchstr},
Bram Moolenaar6cc16192005-01-08 21:49:45 +00008295 {"max", 1, 1, f_max},
8296 {"min", 1, 1, f_min},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008297#ifdef vim_mkdir
8298 {"mkdir", 1, 3, f_mkdir},
8299#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008300 {"mode", 0, 1, f_mode},
Bram Moolenaar7e506b62010-01-19 15:55:06 +01008301#ifdef FEAT_MZSCHEME
8302 {"mzeval", 1, 1, f_mzeval},
8303#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008304 {"nextnonblank", 1, 1, f_nextnonblank},
Bram Moolenaard35d7842013-01-23 17:17:10 +01008305 {"nr2char", 1, 2, f_nr2char},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008306 {"or", 2, 2, f_or},
Bram Moolenaar910f66f2006-04-05 20:41:53 +00008307 {"pathshorten", 1, 1, f_pathshorten},
Bram Moolenaare9b892e2016-01-17 21:15:58 +01008308#ifdef FEAT_PERL
8309 {"perleval", 1, 1, f_perleval},
8310#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008311#ifdef FEAT_FLOAT
8312 {"pow", 2, 2, f_pow},
8313#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008314 {"prevnonblank", 1, 1, f_prevnonblank},
Bram Moolenaar4be06f92005-07-29 22:36:03 +00008315 {"printf", 2, 19, f_printf},
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00008316 {"pumvisible", 0, 0, f_pumvisible},
Bram Moolenaardb913952012-06-29 12:54:53 +02008317#ifdef FEAT_PYTHON3
8318 {"py3eval", 1, 1, f_py3eval},
8319#endif
8320#ifdef FEAT_PYTHON
8321 {"pyeval", 1, 1, f_pyeval},
8322#endif
Bram Moolenaar8c711452005-01-14 21:53:12 +00008323 {"range", 1, 3, f_range},
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008324 {"readfile", 1, 3, f_readfile},
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008325 {"reltime", 0, 2, f_reltime},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008326#ifdef FEAT_FLOAT
Bram Moolenaar79c2c882016-02-07 21:19:28 +01008327 {"reltimefloat", 1, 1, f_reltimefloat},
Bram Moolenaar10b369f2016-02-29 23:12:49 +01008328#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +00008329 {"reltimestr", 1, 1, f_reltimestr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008330 {"remote_expr", 2, 3, f_remote_expr},
8331 {"remote_foreground", 1, 1, f_remote_foreground},
8332 {"remote_peek", 1, 2, f_remote_peek},
8333 {"remote_read", 1, 1, f_remote_read},
8334 {"remote_send", 2, 3, f_remote_send},
Bram Moolenaar8a283e52005-01-06 23:28:25 +00008335 {"remove", 2, 3, f_remove},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008336 {"rename", 2, 2, f_rename},
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00008337 {"repeat", 2, 2, f_repeat},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008338 {"resolve", 1, 1, f_resolve},
Bram Moolenaar0d660222005-01-07 21:51:51 +00008339 {"reverse", 1, 1, f_reverse},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008340#ifdef FEAT_FLOAT
8341 {"round", 1, 1, f_round},
8342#endif
Bram Moolenaar9a773482013-06-11 18:40:13 +02008343 {"screenattr", 2, 2, f_screenattr},
8344 {"screenchar", 2, 2, f_screenchar},
Bram Moolenaar9750bb12012-12-05 16:10:42 +01008345 {"screencol", 0, 0, f_screencol},
8346 {"screenrow", 0, 0, f_screenrow},
Bram Moolenaar76929292008-01-06 19:07:36 +00008347 {"search", 1, 4, f_search},
Bram Moolenaare6facf92005-09-13 21:22:27 +00008348 {"searchdecl", 1, 3, f_searchdecl},
Bram Moolenaar76929292008-01-06 19:07:36 +00008349 {"searchpair", 3, 7, f_searchpair},
8350 {"searchpairpos", 3, 7, f_searchpairpos},
8351 {"searchpos", 1, 4, f_searchpos},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008352 {"server2client", 2, 2, f_server2client},
8353 {"serverlist", 0, 0, f_serverlist},
8354 {"setbufvar", 3, 3, f_setbufvar},
Bram Moolenaardbd24b52015-08-11 14:26:19 +02008355 {"setcharsearch", 1, 1, f_setcharsearch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008356 {"setcmdpos", 1, 1, f_setcmdpos},
Bram Moolenaar80492532016-03-08 17:08:53 +01008357 {"setfperm", 2, 2, f_setfperm},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008358 {"setline", 2, 2, f_setline},
Bram Moolenaar17c7c012006-01-26 22:25:15 +00008359 {"setloclist", 2, 3, f_setloclist},
Bram Moolenaar6ee10162007-07-26 20:58:42 +00008360 {"setmatches", 1, 1, f_setmatches},
Bram Moolenaar0e34f622006-03-03 23:00:03 +00008361 {"setpos", 2, 2, f_setpos},
Bram Moolenaarf4630b62005-05-20 21:31:17 +00008362 {"setqflist", 1, 2, f_setqflist},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008363 {"setreg", 2, 3, f_setreg},
Bram Moolenaar06b5d512010-05-22 15:37:44 +02008364 {"settabvar", 3, 3, f_settabvar},
Bram Moolenaar99ebf042006-04-15 20:28:54 +00008365 {"settabwinvar", 4, 4, f_settabwinvar},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008366 {"setwinvar", 3, 3, f_setwinvar},
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +01008367#ifdef FEAT_CRYPT
8368 {"sha256", 1, 1, f_sha256},
8369#endif
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008370 {"shellescape", 1, 2, f_shellescape},
Bram Moolenaar2d17fa32012-10-21 00:45:18 +02008371 {"shiftwidth", 0, 0, f_shiftwidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008372 {"simplify", 1, 1, f_simplify},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008373#ifdef FEAT_FLOAT
8374 {"sin", 1, 1, f_sin},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008375 {"sinh", 1, 1, f_sinh},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008376#endif
Bram Moolenaar5f894962011-06-19 02:55:37 +02008377 {"sort", 1, 3, f_sort},
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +00008378 {"soundfold", 1, 1, f_soundfold},
Bram Moolenaar4463f292005-09-25 22:20:24 +00008379 {"spellbadword", 0, 1, f_spellbadword},
Bram Moolenaar69e0ff92005-09-30 21:23:56 +00008380 {"spellsuggest", 1, 3, f_spellsuggest},
Bram Moolenaar67fe1a12005-05-22 22:12:58 +00008381 {"split", 1, 3, f_split},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008382#ifdef FEAT_FLOAT
8383 {"sqrt", 1, 1, f_sqrt},
8384 {"str2float", 1, 1, f_str2float},
8385#endif
Bram Moolenaar2c932302006-03-18 21:42:09 +00008386 {"str2nr", 1, 2, f_str2nr},
Bram Moolenaar641e48c2015-06-25 16:09:26 +02008387 {"strchars", 1, 2, f_strchars},
Bram Moolenaardc536092010-07-18 15:45:49 +02008388 {"strdisplaywidth", 1, 2, f_strdisplaywidth},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008389#ifdef HAVE_STRFTIME
8390 {"strftime", 1, 2, f_strftime},
8391#endif
Bram Moolenaar33570922005-01-25 22:26:29 +00008392 {"stridx", 2, 3, f_stridx},
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008393 {"string", 1, 1, f_string},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008394 {"strlen", 1, 1, f_strlen},
8395 {"strpart", 2, 3, f_strpart},
Bram Moolenaar532c7802005-01-27 14:44:31 +00008396 {"strridx", 2, 3, f_strridx},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008397 {"strtrans", 1, 1, f_strtrans},
Bram Moolenaar72597a52010-07-18 15:31:08 +02008398 {"strwidth", 1, 1, f_strwidth},
Bram Moolenaar41571762014-04-02 19:00:58 +02008399 {"submatch", 1, 2, f_submatch},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008400 {"substitute", 4, 4, f_substitute},
8401 {"synID", 3, 3, f_synID},
8402 {"synIDattr", 2, 3, f_synIDattr},
8403 {"synIDtrans", 1, 1, f_synIDtrans},
Bram Moolenaar7510fe72010-07-25 12:46:44 +02008404 {"synconcealed", 2, 2, f_synconcealed},
Bram Moolenaar9d188ab2008-01-10 21:24:39 +00008405 {"synstack", 2, 2, f_synstack},
Bram Moolenaarc0197e22004-09-13 20:26:32 +00008406 {"system", 1, 2, f_system},
Bram Moolenaar39c29ed2014-04-05 19:44:40 +02008407 {"systemlist", 1, 2, f_systemlist},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008408 {"tabpagebuflist", 0, 1, f_tabpagebuflist},
Bram Moolenaar7e8fd632006-02-18 22:14:51 +00008409 {"tabpagenr", 0, 1, f_tabpagenr},
Bram Moolenaarfaa959a2006-02-20 21:37:40 +00008410 {"tabpagewinnr", 1, 2, f_tabpagewinnr},
Bram Moolenaard43b6cf2005-09-09 19:53:42 +00008411 {"tagfiles", 0, 0, f_tagfiles},
Bram Moolenaar19a09a12005-03-04 23:39:37 +00008412 {"taglist", 1, 1, f_taglist},
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008413#ifdef FEAT_FLOAT
8414 {"tan", 1, 1, f_tan},
8415 {"tanh", 1, 1, f_tanh},
8416#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008417 {"tempname", 0, 0, f_tempname},
Bram Moolenaard52d9742005-08-21 22:20:28 +00008418 {"test", 1, 1, f_test},
Bram Moolenaar975b5272016-03-15 23:10:59 +01008419#ifdef FEAT_TIMERS
8420 {"timer_start", 2, 3, f_timer_start},
8421 {"timer_stop", 1, 1, f_timer_stop},
8422#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008423 {"tolower", 1, 1, f_tolower},
8424 {"toupper", 1, 1, f_toupper},
Bram Moolenaar8299df92004-07-10 09:47:34 +00008425 {"tr", 3, 3, f_tr},
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008426#ifdef FEAT_FLOAT
8427 {"trunc", 1, 1, f_trunc},
8428#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00008429 {"type", 1, 1, f_type},
Bram Moolenaara17d4c12010-05-30 18:30:36 +02008430 {"undofile", 1, 1, f_undofile},
Bram Moolenaara800b422010-06-27 01:15:55 +02008431 {"undotree", 0, 0, f_undotree},
Bram Moolenaar327aa022014-03-25 18:24:23 +01008432 {"uniq", 1, 3, f_uniq},
Bram Moolenaar8c711452005-01-14 21:53:12 +00008433 {"values", 1, 1, f_values},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008434 {"virtcol", 1, 1, f_virtcol},
8435 {"visualmode", 0, 1, f_visualmode},
Bram Moolenaar8738fc12013-02-20 17:59:11 +01008436 {"wildmenumode", 0, 0, f_wildmenumode},
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +01008437 {"win_findbuf", 1, 1, f_win_findbuf},
Bram Moolenaar86edef62016-03-13 18:07:30 +01008438 {"win_getid", 0, 2, f_win_getid},
8439 {"win_gotoid", 1, 1, f_win_gotoid},
8440 {"win_id2tabwin", 1, 1, f_win_id2tabwin},
8441 {"win_id2win", 1, 1, f_win_id2win},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008442 {"winbufnr", 1, 1, f_winbufnr},
8443 {"wincol", 0, 0, f_wincol},
8444 {"winheight", 1, 1, f_winheight},
8445 {"winline", 0, 0, f_winline},
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00008446 {"winnr", 0, 1, f_winnr},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008447 {"winrestcmd", 0, 0, f_winrestcmd},
Bram Moolenaar768b8c42006-03-04 21:58:33 +00008448 {"winrestview", 1, 1, f_winrestview},
8449 {"winsaveview", 0, 0, f_winsaveview},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008450 {"winwidth", 1, 1, f_winwidth},
Bram Moolenaared767a22016-01-03 22:49:16 +01008451 {"wordcount", 0, 0, f_wordcount},
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +00008452 {"writefile", 2, 3, f_writefile},
Bram Moolenaard6e256c2011-12-14 15:32:50 +01008453 {"xor", 2, 2, f_xor},
Bram Moolenaar071d4272004-06-13 20:20:40 +00008454};
8455
8456#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
8457
8458/*
8459 * Function given to ExpandGeneric() to obtain the list of internal
8460 * or user defined function names.
8461 */
8462 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008463get_function_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008464{
8465 static int intidx = -1;
8466 char_u *name;
8467
8468 if (idx == 0)
8469 intidx = -1;
8470 if (intidx < 0)
8471 {
8472 name = get_user_func_name(xp, idx);
8473 if (name != NULL)
8474 return name;
8475 }
8476 if (++intidx < (int)(sizeof(functions) / sizeof(struct fst)))
8477 {
8478 STRCPY(IObuff, functions[intidx].f_name);
8479 STRCAT(IObuff, "(");
8480 if (functions[intidx].f_max_argc == 0)
8481 STRCAT(IObuff, ")");
8482 return IObuff;
8483 }
8484
8485 return NULL;
8486}
8487
8488/*
8489 * Function given to ExpandGeneric() to obtain the list of internal or
8490 * user defined variable or function names.
8491 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008492 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +01008493get_expr_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008494{
8495 static int intidx = -1;
8496 char_u *name;
8497
8498 if (idx == 0)
8499 intidx = -1;
8500 if (intidx < 0)
8501 {
8502 name = get_function_name(xp, idx);
8503 if (name != NULL)
8504 return name;
8505 }
8506 return get_user_var_name(xp, ++intidx);
8507}
8508
8509#endif /* FEAT_CMDL_COMPL */
8510
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008511#if defined(EBCDIC) || defined(PROTO)
8512/*
8513 * Compare struct fst by function name.
8514 */
8515 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008516compare_func_name(const void *s1, const void *s2)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008517{
8518 struct fst *p1 = (struct fst *)s1;
8519 struct fst *p2 = (struct fst *)s2;
8520
8521 return STRCMP(p1->f_name, p2->f_name);
8522}
8523
8524/*
8525 * Sort the function table by function name.
8526 * The sorting of the table above is ASCII dependant.
8527 * On machines using EBCDIC we have to sort it.
8528 */
8529 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008530sortFunctions(void)
Bram Moolenaar2c704a72010-06-03 21:17:25 +02008531{
8532 int funcCnt = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8533
8534 qsort(functions, (size_t)funcCnt, sizeof(struct fst), compare_func_name);
8535}
8536#endif
8537
8538
Bram Moolenaar071d4272004-06-13 20:20:40 +00008539/*
8540 * Find internal function in table above.
8541 * Return index, or -1 if not found
8542 */
8543 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008544find_internal_func(
8545 char_u *name) /* name of the function */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008546{
8547 int first = 0;
8548 int last = (int)(sizeof(functions) / sizeof(struct fst)) - 1;
8549 int cmp;
8550 int x;
8551
8552 /*
8553 * Find the function name in the table. Binary search.
8554 */
8555 while (first <= last)
8556 {
8557 x = first + ((unsigned)(last - first) >> 1);
8558 cmp = STRCMP(name, functions[x].f_name);
8559 if (cmp < 0)
8560 last = x - 1;
8561 else if (cmp > 0)
8562 first = x + 1;
8563 else
8564 return x;
8565 }
8566 return -1;
8567}
8568
8569/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008570 * Check if "name" is a variable of type VAR_FUNC. If so, return the function
8571 * name it contains, otherwise return "name".
Bram Moolenaar65639032016-03-16 21:40:30 +01008572 * If "partialp" is not NULL, and "name" is of type VAR_PARTIAL also set
8573 * "partialp".
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008574 */
8575 static char_u *
Bram Moolenaar65639032016-03-16 21:40:30 +01008576deref_func_name(char_u *name, int *lenp, partial_T **partialp, int no_autoload)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008577{
Bram Moolenaar33570922005-01-25 22:26:29 +00008578 dictitem_T *v;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008579 int cc;
8580
Bram Moolenaar65639032016-03-16 21:40:30 +01008581 if (partialp != NULL)
8582 *partialp = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008583
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008584 cc = name[*lenp];
8585 name[*lenp] = NUL;
Bram Moolenaar8822a9c2014-01-14 19:44:34 +01008586 v = find_var(name, NULL, no_autoload);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008587 name[*lenp] = cc;
Bram Moolenaar33570922005-01-25 22:26:29 +00008588 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008589 {
Bram Moolenaar33570922005-01-25 22:26:29 +00008590 if (v->di_tv.vval.v_string == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008591 {
8592 *lenp = 0;
8593 return (char_u *)""; /* just in case */
8594 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008595 *lenp = (int)STRLEN(v->di_tv.vval.v_string);
Bram Moolenaar33570922005-01-25 22:26:29 +00008596 return v->di_tv.vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008597 }
8598
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008599 if (v != NULL && v->di_tv.v_type == VAR_PARTIAL)
8600 {
Bram Moolenaar65639032016-03-16 21:40:30 +01008601 partial_T *pt = v->di_tv.vval.v_partial;
8602
8603 if (pt == NULL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008604 {
8605 *lenp = 0;
8606 return (char_u *)""; /* just in case */
8607 }
Bram Moolenaar65639032016-03-16 21:40:30 +01008608 if (partialp != NULL)
8609 *partialp = pt;
8610 *lenp = (int)STRLEN(pt->pt_name);
8611 return pt->pt_name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008612 }
8613
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008614 return name;
8615}
8616
8617/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00008618 * Allocate a variable for the result of a function.
8619 * Return OK or FAIL.
8620 */
8621 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008622get_func_tv(
8623 char_u *name, /* name of the function */
8624 int len, /* length of "name" */
8625 typval_T *rettv,
8626 char_u **arg, /* argument, pointing to the '(' */
8627 linenr_T firstline, /* first line of range */
8628 linenr_T lastline, /* last line of range */
8629 int *doesrange, /* return: function handled range */
8630 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008631 partial_T *partial, /* for extra arguments */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008632 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008633{
8634 char_u *argp;
8635 int ret = OK;
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008636 typval_T argvars[MAX_FUNC_ARGS + 1]; /* vars for arguments */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008637 int argcount = 0; /* number of arguments found */
8638
8639 /*
8640 * Get the arguments.
8641 */
8642 argp = *arg;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008643 while (argcount < MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008644 {
8645 argp = skipwhite(argp + 1); /* skip the '(' or ',' */
8646 if (*argp == ')' || *argp == ',' || *argp == NUL)
8647 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008648 if (eval1(&argp, &argvars[argcount], evaluate) == FAIL)
8649 {
8650 ret = FAIL;
8651 break;
8652 }
8653 ++argcount;
8654 if (*argp != ',')
8655 break;
8656 }
8657 if (*argp == ')')
8658 ++argp;
8659 else
8660 ret = FAIL;
8661
8662 if (ret == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008663 ret = call_func(name, len, rettv, argcount, argvars,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008664 firstline, lastline, doesrange, evaluate, partial, selfdict);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008665 else if (!aborting())
Bram Moolenaar33570922005-01-25 22:26:29 +00008666 {
8667 if (argcount == MAX_FUNC_ARGS)
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008668 emsg_funcname(N_("E740: Too many arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008669 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008670 emsg_funcname(N_("E116: Invalid arguments for function %s"), name);
Bram Moolenaar33570922005-01-25 22:26:29 +00008671 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008672
8673 while (--argcount >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008674 clear_tv(&argvars[argcount]);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008675
8676 *arg = skipwhite(argp);
8677 return ret;
8678}
8679
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008680#define ERROR_UNKNOWN 0
8681#define ERROR_TOOMANY 1
8682#define ERROR_TOOFEW 2
8683#define ERROR_SCRIPT 3
8684#define ERROR_DICT 4
8685#define ERROR_NONE 5
8686#define ERROR_OTHER 6
8687#define FLEN_FIXED 40
8688
8689/*
8690 * In a script change <SID>name() and s:name() to K_SNR 123_name().
8691 * Change <SNR>123_name() to K_SNR 123_name().
8692 * Use "fname_buf[FLEN_FIXED + 1]" when it fits, otherwise allocate memory
8693 * (slow).
8694 */
8695 static char_u *
8696fname_trans_sid(char_u *name, char_u *fname_buf, char_u **tofree, int *error)
8697{
8698 int llen;
8699 char_u *fname;
8700 int i;
8701
8702 llen = eval_fname_script(name);
8703 if (llen > 0)
8704 {
8705 fname_buf[0] = K_SPECIAL;
8706 fname_buf[1] = KS_EXTRA;
8707 fname_buf[2] = (int)KE_SNR;
8708 i = 3;
8709 if (eval_fname_sid(name)) /* "<SID>" or "s:" */
8710 {
8711 if (current_SID <= 0)
8712 *error = ERROR_SCRIPT;
8713 else
8714 {
8715 sprintf((char *)fname_buf + 3, "%ld_", (long)current_SID);
8716 i = (int)STRLEN(fname_buf);
8717 }
8718 }
8719 if (i + STRLEN(name + llen) < FLEN_FIXED)
8720 {
8721 STRCPY(fname_buf + i, name + llen);
8722 fname = fname_buf;
8723 }
8724 else
8725 {
8726 fname = alloc((unsigned)(i + STRLEN(name + llen) + 1));
8727 if (fname == NULL)
8728 *error = ERROR_OTHER;
8729 else
8730 {
8731 *tofree = fname;
8732 mch_memmove(fname, fname_buf, (size_t)i);
8733 STRCPY(fname + i, name + llen);
8734 }
8735 }
8736 }
8737 else
8738 fname = name;
8739 return fname;
8740}
Bram Moolenaar071d4272004-06-13 20:20:40 +00008741
8742/*
8743 * Call a function with its resolved parameters
Bram Moolenaar9bb77d62013-05-30 12:14:49 +02008744 * Return FAIL when the function can't be called, OK otherwise.
Bram Moolenaar280f1262006-01-30 00:14:18 +00008745 * Also returns OK when an error was encountered while executing the function.
Bram Moolenaar071d4272004-06-13 20:20:40 +00008746 */
Bram Moolenaar3b5f9292016-01-28 22:37:01 +01008747 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008748call_func(
8749 char_u *funcname, /* name of the function */
8750 int len, /* length of "name" */
8751 typval_T *rettv, /* return value goes here */
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008752 int argcount_in, /* number of "argvars" */
8753 typval_T *argvars_in, /* vars for arguments, must have "argcount"
Bram Moolenaareb3593b2006-04-22 22:33:57 +00008754 PLUS ONE elements! */
Bram Moolenaar7454a062016-01-30 15:14:10 +01008755 linenr_T firstline, /* first line of range */
8756 linenr_T lastline, /* last line of range */
8757 int *doesrange, /* return: function handled range */
8758 int evaluate,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008759 partial_T *partial, /* optional, can be NULL */
8760 dict_T *selfdict_in) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +00008761{
8762 int ret = FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008763 int error = ERROR_NONE;
8764 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008765 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008766 char_u fname_buf[FLEN_FIXED + 1];
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008767 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008768 char_u *fname;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008769 char_u *name;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008770 int argcount = argcount_in;
8771 typval_T *argvars = argvars_in;
8772 dict_T *selfdict = selfdict_in;
8773 typval_T argv[MAX_FUNC_ARGS + 1]; /* used when "partial" is not NULL */
8774 int argv_clear = 0;
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008775
8776 /* Make a copy of the name, if it comes from a funcref variable it could
8777 * be changed or deleted in the called function. */
Bram Moolenaarf506c5b2010-06-22 06:28:58 +02008778 name = vim_strnsave(funcname, len);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008779 if (name == NULL)
8780 return ret;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008781
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008782 fname = fname_trans_sid(name, fname_buf, &tofree, &error);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008783
8784 *doesrange = FALSE;
8785
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008786 if (partial != NULL)
8787 {
8788 if (partial->pt_dict != NULL)
8789 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008790 /* When the function has a partial with a dict and there is a dict
8791 * argument, use the dict argument. That is backwards compatible.
8792 */
8793 if (selfdict_in == NULL)
8794 selfdict = partial->pt_dict;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008795 }
8796 if (error == ERROR_NONE && partial->pt_argc > 0)
8797 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008798 for (argv_clear = 0; argv_clear < partial->pt_argc; ++argv_clear)
8799 copy_tv(&partial->pt_argv[argv_clear], &argv[argv_clear]);
8800 for (i = 0; i < argcount_in; ++i)
8801 argv[i + argv_clear] = argvars_in[i];
8802 argvars = argv;
8803 argcount = partial->pt_argc + argcount_in;
8804 }
8805 }
8806
Bram Moolenaar071d4272004-06-13 20:20:40 +00008807
8808 /* execute the function if no errors detected and executing */
8809 if (evaluate && error == ERROR_NONE)
8810 {
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008811 char_u *rfname = fname;
8812
8813 /* Ignore "g:" before a function name. */
8814 if (fname[0] == 'g' && fname[1] == ':')
8815 rfname = fname + 2;
8816
Bram Moolenaar798b30b2009-04-22 10:56:16 +00008817 rettv->v_type = VAR_NUMBER; /* default rettv is number zero */
8818 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008819 error = ERROR_UNKNOWN;
8820
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008821 if (!builtin_function(rfname, -1))
Bram Moolenaar071d4272004-06-13 20:20:40 +00008822 {
8823 /*
8824 * User defined function.
8825 */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008826 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008827
Bram Moolenaar071d4272004-06-13 20:20:40 +00008828#ifdef FEAT_AUTOCMD
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008829 /* Trigger FuncUndefined event, may load the function. */
8830 if (fp == NULL
8831 && apply_autocmds(EVENT_FUNCUNDEFINED,
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008832 rfname, rfname, TRUE, NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008833 && !aborting())
Bram Moolenaar071d4272004-06-13 20:20:40 +00008834 {
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008835 /* executed an autocommand, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008836 fp = find_func(rfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008837 }
8838#endif
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008839 /* Try loading a package. */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008840 if (fp == NULL && script_autoload(rfname, TRUE) && !aborting())
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008841 {
8842 /* loaded a package, search for the function again */
Bram Moolenaara4f317d2014-04-24 17:12:33 +02008843 fp = find_func(rfname);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +00008844 }
8845
Bram Moolenaar071d4272004-06-13 20:20:40 +00008846 if (fp != NULL)
8847 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008848 if (fp->uf_flags & FC_RANGE)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008849 *doesrange = TRUE;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008850 if (argcount < fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008851 error = ERROR_TOOFEW;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008852 else if (!fp->uf_varargs && argcount > fp->uf_args.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +00008853 error = ERROR_TOOMANY;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008854 else if ((fp->uf_flags & FC_DICT) && selfdict == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +00008855 error = ERROR_DICT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00008856 else
8857 {
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008858 int did_save_redo = FALSE;
8859
Bram Moolenaar071d4272004-06-13 20:20:40 +00008860 /*
8861 * Call the user function.
8862 * Save and restore search patterns, script variables and
8863 * redo buffer.
8864 */
8865 save_search_patterns();
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008866#ifdef FEAT_INS_EXPAND
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008867 if (!ins_compl_active())
Bram Moolenaar20ad69c2015-12-03 13:52:52 +01008868#endif
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008869 {
8870 saveRedobuff();
8871 did_save_redo = TRUE;
8872 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008873 ++fp->uf_calls;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008874 call_user_func(fp, argcount, argvars, rettv,
Bram Moolenaare9a41262005-01-15 22:18:47 +00008875 firstline, lastline,
Bram Moolenaar5313dcb2005-02-22 08:56:13 +00008876 (fp->uf_flags & FC_DICT) ? selfdict : NULL);
8877 if (--fp->uf_calls <= 0 && isdigit(*fp->uf_name)
8878 && fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +00008879 /* Function was unreferenced while being used, free it
8880 * now. */
8881 func_free(fp);
Bram Moolenaarbe20f9f2015-02-17 12:44:09 +01008882 if (did_save_redo)
8883 restoreRedobuff();
Bram Moolenaar071d4272004-06-13 20:20:40 +00008884 restore_search_patterns();
8885 error = ERROR_NONE;
8886 }
8887 }
8888 }
8889 else
8890 {
8891 /*
8892 * Find the function name in the table, call its implementation.
8893 */
8894 i = find_internal_func(fname);
8895 if (i >= 0)
8896 {
8897 if (argcount < functions[i].f_min_argc)
8898 error = ERROR_TOOFEW;
8899 else if (argcount > functions[i].f_max_argc)
8900 error = ERROR_TOOMANY;
8901 else
8902 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00008903 argvars[argcount].v_type = VAR_UNKNOWN;
Bram Moolenaarc70646c2005-01-04 21:52:38 +00008904 functions[i].f_func(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008905 error = ERROR_NONE;
8906 }
8907 }
8908 }
8909 /*
8910 * The function call (or "FuncUndefined" autocommand sequence) might
8911 * have been aborted by an error, an interrupt, or an explicitly thrown
8912 * exception that has not been caught so far. This situation can be
8913 * tested for by calling aborting(). For an error in an internal
8914 * function or for the "E132" error in call_user_func(), however, the
8915 * throw point at which the "force_abort" flag (temporarily reset by
8916 * emsg()) is normally updated has not been reached yet. We need to
8917 * update that flag first to make aborting() reliable.
8918 */
8919 update_force_abort();
8920 }
8921 if (error == ERROR_NONE)
8922 ret = OK;
8923
8924 /*
8925 * Report an error unless the argument evaluation or function call has been
8926 * cancelled due to an aborting error, an interrupt, or an exception.
8927 */
Bram Moolenaar8c711452005-01-14 21:53:12 +00008928 if (!aborting())
8929 {
8930 switch (error)
8931 {
8932 case ERROR_UNKNOWN:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008933 emsg_funcname(N_("E117: Unknown function: %s"), name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008934 break;
8935 case ERROR_TOOMANY:
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008936 emsg_funcname(e_toomanyarg, name);
Bram Moolenaar8c711452005-01-14 21:53:12 +00008937 break;
8938 case ERROR_TOOFEW:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008939 emsg_funcname(N_("E119: Not enough arguments for function: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008940 name);
8941 break;
8942 case ERROR_SCRIPT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008943 emsg_funcname(N_("E120: Using <SID> not in a script context: %s"),
Bram Moolenaar8c711452005-01-14 21:53:12 +00008944 name);
8945 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +00008946 case ERROR_DICT:
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00008947 emsg_funcname(N_("E725: Calling dict function without Dictionary: %s"),
Bram Moolenaare9a41262005-01-15 22:18:47 +00008948 name);
8949 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +00008950 }
8951 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00008952
Bram Moolenaar1735bc92016-03-14 23:05:14 +01008953 while (argv_clear > 0)
8954 clear_tv(&argv[--argv_clear]);
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +01008955 vim_free(tofree);
Bram Moolenaarbc42c1e2010-05-28 22:06:46 +02008956 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +00008957
8958 return ret;
8959}
8960
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008961/*
8962 * Give an error message with a function name. Handle <SNR> things.
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +00008963 * "ermsg" is to be passed without translation, use N_() instead of _().
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008964 */
8965 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01008966emsg_funcname(char *ermsg, char_u *name)
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008967{
8968 char_u *p;
8969
8970 if (*name == K_SPECIAL)
8971 p = concat_str((char_u *)"<SNR>", name + 3);
8972 else
8973 p = name;
Bram Moolenaar89d40322006-08-29 15:30:07 +00008974 EMSG2(_(ermsg), p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +00008975 if (p != name)
8976 vim_free(p);
8977}
8978
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008979/*
8980 * Return TRUE for a non-zero Number and a non-empty String.
8981 */
8982 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01008983non_zero_arg(typval_T *argvars)
Bram Moolenaar05bb9532008-07-04 09:44:11 +00008984{
8985 return ((argvars[0].v_type == VAR_NUMBER
8986 && argvars[0].vval.v_number != 0)
8987 || (argvars[0].v_type == VAR_STRING
8988 && argvars[0].vval.v_string != NULL
8989 && *argvars[0].vval.v_string != NUL));
8990}
8991
Bram Moolenaar071d4272004-06-13 20:20:40 +00008992/*********************************************
8993 * Implementation of the built-in functions
8994 */
8995
Bram Moolenaar8c8de832008-06-24 22:58:06 +00008996#ifdef FEAT_FLOAT
Bram Moolenaar48e697e2016-01-23 22:17:30 +01008997static int get_float_arg(typval_T *argvars, float_T *f);
Bram Moolenaardb7c6862010-05-21 16:33:48 +02008998
8999/*
9000 * Get the float value of "argvars[0]" into "f".
9001 * Returns FAIL when the argument is not a Number or Float.
9002 */
9003 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009004get_float_arg(typval_T *argvars, float_T *f)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009005{
9006 if (argvars[0].v_type == VAR_FLOAT)
9007 {
9008 *f = argvars[0].vval.v_float;
9009 return OK;
9010 }
9011 if (argvars[0].v_type == VAR_NUMBER)
9012 {
9013 *f = (float_T)argvars[0].vval.v_number;
9014 return OK;
9015 }
9016 EMSG(_("E808: Number or Float required"));
9017 return FAIL;
9018}
9019
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009020/*
9021 * "abs(expr)" function
9022 */
9023 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009024f_abs(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009025{
9026 if (argvars[0].v_type == VAR_FLOAT)
9027 {
9028 rettv->v_type = VAR_FLOAT;
9029 rettv->vval.v_float = fabs(argvars[0].vval.v_float);
9030 }
9031 else
9032 {
9033 varnumber_T n;
9034 int error = FALSE;
9035
9036 n = get_tv_number_chk(&argvars[0], &error);
9037 if (error)
9038 rettv->vval.v_number = -1;
9039 else if (n > 0)
9040 rettv->vval.v_number = n;
9041 else
9042 rettv->vval.v_number = -n;
9043 }
9044}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009045
9046/*
9047 * "acos()" function
9048 */
9049 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009050f_acos(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009051{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009052 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009053
9054 rettv->v_type = VAR_FLOAT;
9055 if (get_float_arg(argvars, &f) == OK)
9056 rettv->vval.v_float = acos(f);
9057 else
9058 rettv->vval.v_float = 0.0;
9059}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009060#endif
9061
Bram Moolenaar071d4272004-06-13 20:20:40 +00009062/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009063 * "add(list, item)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +00009064 */
9065 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009066f_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009067{
Bram Moolenaar33570922005-01-25 22:26:29 +00009068 list_T *l;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009069
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009070 rettv->vval.v_number = 1; /* Default: Failed */
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009071 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009072 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009073 if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +02009074 && !tv_check_lock(l->lv_lock,
9075 (char_u *)N_("add() argument"), TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +00009076 && list_append_tv(l, &argvars[1]) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009077 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009078 }
9079 else
Bram Moolenaar0d660222005-01-07 21:51:51 +00009080 EMSG(_(e_listreq));
9081}
9082
9083/*
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009084 * "alloc_fail(id, countdown, repeat)" function
9085 */
9086 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009087f_alloc_fail(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009088{
9089 if (argvars[0].v_type != VAR_NUMBER
9090 || argvars[0].vval.v_number <= 0
9091 || argvars[1].v_type != VAR_NUMBER
9092 || argvars[1].vval.v_number < 0
9093 || argvars[2].v_type != VAR_NUMBER)
9094 EMSG(_(e_invarg));
9095 else
9096 {
9097 alloc_fail_id = argvars[0].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009098 if (alloc_fail_id >= aid_last)
9099 EMSG(_(e_invarg));
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009100 alloc_fail_countdown = argvars[1].vval.v_number;
9101 alloc_fail_repeat = argvars[2].vval.v_number;
Bram Moolenaara260b872016-01-15 20:48:22 +01009102 did_outofmem_msg = FALSE;
Bram Moolenaar75bdf6a2016-01-07 21:25:08 +01009103 }
9104}
9105
9106/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009107 * "and(expr, expr)" function
9108 */
9109 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009110f_and(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +01009111{
9112 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
9113 & get_tv_number_chk(&argvars[1], NULL);
9114}
9115
9116/*
Bram Moolenaar0d660222005-01-07 21:51:51 +00009117 * "append(lnum, string/list)" function
9118 */
9119 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009120f_append(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +00009121{
9122 long lnum;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009123 char_u *line;
Bram Moolenaar33570922005-01-25 22:26:29 +00009124 list_T *l = NULL;
9125 listitem_T *li = NULL;
9126 typval_T *tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +00009127 long added = 0;
9128
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +02009129 /* When coming here from Insert mode, sync undo, so that this can be
9130 * undone separately from what was previously inserted. */
9131 if (u_sync_once == 2)
9132 {
9133 u_sync_once = 1; /* notify that u_sync() was called */
9134 u_sync(TRUE);
9135 }
9136
Bram Moolenaar0d660222005-01-07 21:51:51 +00009137 lnum = get_tv_lnum(argvars);
9138 if (lnum >= 0
9139 && lnum <= curbuf->b_ml.ml_line_count
9140 && u_save(lnum, lnum + 1) == OK)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009141 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009142 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009143 {
Bram Moolenaar0d660222005-01-07 21:51:51 +00009144 l = argvars[1].vval.v_list;
9145 if (l == NULL)
9146 return;
9147 li = l->lv_first;
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009148 }
Bram Moolenaar0d660222005-01-07 21:51:51 +00009149 for (;;)
9150 {
9151 if (l == NULL)
9152 tv = &argvars[1]; /* append a string */
9153 else if (li == NULL)
9154 break; /* end of list */
9155 else
9156 tv = &li->li_tv; /* append item from list */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009157 line = get_tv_string_chk(tv);
9158 if (line == NULL) /* type error */
9159 {
9160 rettv->vval.v_number = 1; /* Failed */
9161 break;
9162 }
9163 ml_append(lnum + added, line, (colnr_T)0, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009164 ++added;
9165 if (l == NULL)
9166 break;
9167 li = li->li_next;
9168 }
9169
9170 appended_lines_mark(lnum, added);
9171 if (curwin->w_cursor.lnum > lnum)
9172 curwin->w_cursor.lnum += added;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009173 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009174 else
9175 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009176}
9177
9178/*
9179 * "argc()" function
9180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009182f_argc(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009183{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009184 rettv->vval.v_number = ARGCOUNT;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009185}
9186
9187/*
9188 * "argidx()" function
9189 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009190 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009191f_argidx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009192{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009193 rettv->vval.v_number = curwin->w_arg_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009194}
9195
9196/*
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009197 * "arglistid()" function
9198 */
9199 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +01009200f_arglistid(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009201{
9202 win_T *wp;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009203
9204 rettv->vval.v_number = -1;
Bram Moolenaarc9703302016-01-17 21:49:33 +01009205 wp = find_tabwin(&argvars[0], &argvars[1]);
9206 if (wp != NULL)
9207 rettv->vval.v_number = wp->w_alist->id;
Bram Moolenaar2d1fe052014-05-28 18:22:57 +02009208}
9209
9210/*
Bram Moolenaar071d4272004-06-13 20:20:40 +00009211 * "argv(nr)" function
9212 */
9213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009214f_argv(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009215{
9216 int idx;
9217
Bram Moolenaare2f98b92006-03-29 21:18:24 +00009218 if (argvars[0].v_type != VAR_UNKNOWN)
9219 {
9220 idx = get_tv_number_chk(&argvars[0], NULL);
9221 if (idx >= 0 && idx < ARGCOUNT)
9222 rettv->vval.v_string = vim_strsave(alist_name(&ARGLIST[idx]));
9223 else
9224 rettv->vval.v_string = NULL;
9225 rettv->v_type = VAR_STRING;
9226 }
9227 else if (rettv_list_alloc(rettv) == OK)
9228 for (idx = 0; idx < ARGCOUNT; ++idx)
9229 list_append_string(rettv->vval.v_list,
9230 alist_name(&ARGLIST[idx]), -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009231}
9232
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009233static void prepare_assert_error(garray_T*gap);
9234static void fill_assert_error(garray_T *gap, typval_T *opt_msg_tv, char_u *exp_str, typval_T *exp_tv, typval_T *got_tv);
9235static void assert_error(garray_T *gap);
9236static void assert_bool(typval_T *argvars, int isTrue);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009237
9238/*
9239 * Prepare "gap" for an assert error and add the sourcing position.
9240 */
9241 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009242prepare_assert_error(garray_T *gap)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009243{
9244 char buf[NUMBUFLEN];
9245
9246 ga_init2(gap, 1, 100);
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009247 if (sourcing_name != NULL)
9248 {
9249 ga_concat(gap, sourcing_name);
9250 if (sourcing_lnum > 0)
9251 ga_concat(gap, (char_u *)" ");
9252 }
9253 if (sourcing_lnum > 0)
9254 {
9255 sprintf(buf, "line %ld", (long)sourcing_lnum);
9256 ga_concat(gap, (char_u *)buf);
9257 }
9258 if (sourcing_name != NULL || sourcing_lnum > 0)
9259 ga_concat(gap, (char_u *)": ");
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009260}
9261
9262/*
Bram Moolenaar23689172016-02-15 22:37:37 +01009263 * Append "str" to "gap", escaping unprintable characters.
9264 * Changes NL to \n, CR to \r, etc.
9265 */
9266 static void
9267ga_concat_esc(garray_T *gap, char_u *str)
9268{
9269 char_u *p;
9270 char_u buf[NUMBUFLEN];
9271
Bram Moolenaarf1551962016-03-15 12:55:58 +01009272 if (str == NULL)
9273 {
9274 ga_concat(gap, (char_u *)"NULL");
9275 return;
9276 }
9277
Bram Moolenaar23689172016-02-15 22:37:37 +01009278 for (p = str; *p != NUL; ++p)
9279 switch (*p)
9280 {
9281 case BS: ga_concat(gap, (char_u *)"\\b"); break;
9282 case ESC: ga_concat(gap, (char_u *)"\\e"); break;
9283 case FF: ga_concat(gap, (char_u *)"\\f"); break;
9284 case NL: ga_concat(gap, (char_u *)"\\n"); break;
9285 case TAB: ga_concat(gap, (char_u *)"\\t"); break;
9286 case CAR: ga_concat(gap, (char_u *)"\\r"); break;
9287 case '\\': ga_concat(gap, (char_u *)"\\\\"); break;
9288 default:
9289 if (*p < ' ')
9290 {
9291 vim_snprintf((char *)buf, NUMBUFLEN, "\\x%02x", *p);
9292 ga_concat(gap, buf);
9293 }
9294 else
9295 ga_append(gap, *p);
9296 break;
9297 }
9298}
9299
9300/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009301 * Fill "gap" with information about an assert error.
9302 */
9303 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009304fill_assert_error(
9305 garray_T *gap,
9306 typval_T *opt_msg_tv,
9307 char_u *exp_str,
9308 typval_T *exp_tv,
9309 typval_T *got_tv)
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009310{
9311 char_u numbuf[NUMBUFLEN];
9312 char_u *tofree;
9313
9314 if (opt_msg_tv->v_type != VAR_UNKNOWN)
9315 {
9316 ga_concat(gap, tv2string(opt_msg_tv, &tofree, numbuf, 0));
9317 vim_free(tofree);
9318 }
9319 else
9320 {
9321 ga_concat(gap, (char_u *)"Expected ");
9322 if (exp_str == NULL)
9323 {
Bram Moolenaar23689172016-02-15 22:37:37 +01009324 ga_concat_esc(gap, tv2string(exp_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009325 vim_free(tofree);
9326 }
9327 else
Bram Moolenaar23689172016-02-15 22:37:37 +01009328 ga_concat_esc(gap, exp_str);
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009329 ga_concat(gap, (char_u *)" but got ");
Bram Moolenaar23689172016-02-15 22:37:37 +01009330 ga_concat_esc(gap, tv2string(got_tv, &tofree, numbuf, 0));
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009331 vim_free(tofree);
9332 }
9333}
Bram Moolenaar43345542015-11-29 17:35:35 +01009334
9335/*
9336 * Add an assert error to v:errors.
9337 */
9338 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009339assert_error(garray_T *gap)
Bram Moolenaar43345542015-11-29 17:35:35 +01009340{
9341 struct vimvar *vp = &vimvars[VV_ERRORS];
9342
9343 if (vp->vv_type != VAR_LIST || vimvars[VV_ERRORS].vv_list == NULL)
9344 /* Make sure v:errors is a list. */
9345 set_vim_var_list(VV_ERRORS, list_alloc());
9346 list_append_string(vimvars[VV_ERRORS].vv_list, gap->ga_data, gap->ga_len);
9347}
9348
Bram Moolenaar43345542015-11-29 17:35:35 +01009349/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009350 * "assert_equal(expected, actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009351 */
9352 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009353f_assert_equal(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009354{
9355 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009356
9357 if (!tv_equal(&argvars[0], &argvars[1], FALSE, FALSE))
9358 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009359 prepare_assert_error(&ga);
9360 fill_assert_error(&ga, &argvars[2], NULL, &argvars[0], &argvars[1]);
9361 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009362 ga_clear(&ga);
9363 }
9364}
9365
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009366/*
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009367 * "assert_exception(string[, msg])" function
9368 */
9369 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009370f_assert_exception(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009371{
9372 garray_T ga;
9373 char *error;
9374
9375 error = (char *)get_tv_string_chk(&argvars[0]);
9376 if (vimvars[VV_EXCEPTION].vv_str == NULL)
9377 {
9378 prepare_assert_error(&ga);
9379 ga_concat(&ga, (char_u *)"v:exception is not set");
9380 assert_error(&ga);
9381 ga_clear(&ga);
9382 }
Bram Moolenaarda5dcd92016-01-19 14:31:20 +01009383 else if (error != NULL
9384 && strstr((char *)vimvars[VV_EXCEPTION].vv_str, error) == NULL)
Bram Moolenaara803c7f2016-01-15 15:31:39 +01009385 {
9386 prepare_assert_error(&ga);
9387 fill_assert_error(&ga, &argvars[1], NULL, &argvars[0],
9388 &vimvars[VV_EXCEPTION].vv_tv);
9389 assert_error(&ga);
9390 ga_clear(&ga);
9391 }
9392}
9393
9394/*
Bram Moolenaara260b872016-01-15 20:48:22 +01009395 * "assert_fails(cmd [, error])" function
9396 */
9397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009398f_assert_fails(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaara260b872016-01-15 20:48:22 +01009399{
9400 char_u *cmd = get_tv_string_chk(&argvars[0]);
9401 garray_T ga;
9402
9403 called_emsg = FALSE;
9404 suppress_errthrow = TRUE;
9405 emsg_silent = TRUE;
9406 do_cmdline_cmd(cmd);
9407 if (!called_emsg)
9408 {
9409 prepare_assert_error(&ga);
9410 ga_concat(&ga, (char_u *)"command did not fail: ");
9411 ga_concat(&ga, cmd);
9412 assert_error(&ga);
9413 ga_clear(&ga);
9414 }
9415 else if (argvars[1].v_type != VAR_UNKNOWN)
9416 {
9417 char_u buf[NUMBUFLEN];
9418 char *error = (char *)get_tv_string_buf_chk(&argvars[1], buf);
9419
Bram Moolenaar1abb5022016-03-15 13:33:55 +01009420 if (error == NULL
9421 || strstr((char *)vimvars[VV_ERRMSG].vv_str, error) == NULL)
Bram Moolenaara260b872016-01-15 20:48:22 +01009422 {
9423 prepare_assert_error(&ga);
9424 fill_assert_error(&ga, &argvars[2], NULL, &argvars[1],
9425 &vimvars[VV_ERRMSG].vv_tv);
9426 assert_error(&ga);
9427 ga_clear(&ga);
9428 }
9429 }
9430
9431 called_emsg = FALSE;
9432 suppress_errthrow = FALSE;
9433 emsg_silent = FALSE;
9434 emsg_on_display = FALSE;
9435 set_vim_var_string(VV_ERRMSG, NULL, 0);
9436}
9437
9438/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009439 * Common for assert_true() and assert_false().
9440 */
Bram Moolenaar43345542015-11-29 17:35:35 +01009441 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009442assert_bool(typval_T *argvars, int isTrue)
Bram Moolenaar43345542015-11-29 17:35:35 +01009443{
9444 int error = FALSE;
9445 garray_T ga;
Bram Moolenaar43345542015-11-29 17:35:35 +01009446
Bram Moolenaar37127922016-02-06 20:29:28 +01009447 if (argvars[0].v_type == VAR_SPECIAL
Bram Moolenaarc5f98ee2016-02-07 00:00:35 +01009448 && argvars[0].vval.v_number == (isTrue ? VVAL_TRUE : VVAL_FALSE))
Bram Moolenaar37127922016-02-06 20:29:28 +01009449 return;
Bram Moolenaar43345542015-11-29 17:35:35 +01009450 if (argvars[0].v_type != VAR_NUMBER
9451 || (get_tv_number_chk(&argvars[0], &error) == 0) == isTrue
9452 || error)
9453 {
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009454 prepare_assert_error(&ga);
9455 fill_assert_error(&ga, &argvars[1],
Bram Moolenaarcbfe3292016-01-02 20:59:10 +01009456 (char_u *)(isTrue ? "True" : "False"),
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009457 NULL, &argvars[0]);
9458 assert_error(&ga);
Bram Moolenaar43345542015-11-29 17:35:35 +01009459 ga_clear(&ga);
9460 }
9461}
9462
9463/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009464 * "assert_false(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009465 */
9466 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009467f_assert_false(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009468{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009469 assert_bool(argvars, FALSE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009470}
9471
9472/*
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009473 * "assert_true(actual[, msg])" function
Bram Moolenaar43345542015-11-29 17:35:35 +01009474 */
9475 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009476f_assert_true(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar43345542015-11-29 17:35:35 +01009477{
Bram Moolenaarbbfbaf92015-12-01 15:32:56 +01009478 assert_bool(argvars, TRUE);
Bram Moolenaar43345542015-11-29 17:35:35 +01009479}
9480
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009481#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009482/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009483 * "asin()" function
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009484 */
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009485 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009486f_asin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009487{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009488 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009489
9490 rettv->v_type = VAR_FLOAT;
9491 if (get_float_arg(argvars, &f) == OK)
9492 rettv->vval.v_float = asin(f);
9493 else
9494 rettv->vval.v_float = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009495}
9496
9497/*
9498 * "atan()" function
9499 */
9500 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009501f_atan(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009502{
Bram Moolenaar4db20ab2016-02-22 21:48:30 +01009503 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009504
9505 rettv->v_type = VAR_FLOAT;
9506 if (get_float_arg(argvars, &f) == OK)
9507 rettv->vval.v_float = atan(f);
9508 else
9509 rettv->vval.v_float = 0.0;
9510}
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009511
9512/*
9513 * "atan2()" function
9514 */
9515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009516f_atan2(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009517{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009518 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +02009519
9520 rettv->v_type = VAR_FLOAT;
9521 if (get_float_arg(argvars, &fx) == OK
9522 && get_float_arg(&argvars[1], &fy) == OK)
9523 rettv->vval.v_float = atan2(fx, fy);
9524 else
9525 rettv->vval.v_float = 0.0;
9526}
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009527#endif
9528
Bram Moolenaar071d4272004-06-13 20:20:40 +00009529/*
9530 * "browse(save, title, initdir, default)" function
9531 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009532 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009533f_browse(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009534{
9535#ifdef FEAT_BROWSE
9536 int save;
9537 char_u *title;
9538 char_u *initdir;
9539 char_u *defname;
9540 char_u buf[NUMBUFLEN];
9541 char_u buf2[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009542 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009543
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009544 save = get_tv_number_chk(&argvars[0], &error);
9545 title = get_tv_string_chk(&argvars[1]);
9546 initdir = get_tv_string_buf_chk(&argvars[2], buf);
9547 defname = get_tv_string_buf_chk(&argvars[3], buf2);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009548
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009549 if (error || title == NULL || initdir == NULL || defname == NULL)
9550 rettv->vval.v_string = NULL;
9551 else
9552 rettv->vval.v_string =
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009553 do_browse(save ? BROWSE_SAVE : 0,
9554 title, defname, NULL, initdir, NULL, curbuf);
9555#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009556 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009557#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009558 rettv->v_type = VAR_STRING;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009559}
9560
9561/*
9562 * "browsedir(title, initdir)" function
9563 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009564 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009565f_browsedir(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009566{
9567#ifdef FEAT_BROWSE
9568 char_u *title;
9569 char_u *initdir;
9570 char_u buf[NUMBUFLEN];
9571
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009572 title = get_tv_string_chk(&argvars[0]);
9573 initdir = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009574
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009575 if (title == NULL || initdir == NULL)
9576 rettv->vval.v_string = NULL;
9577 else
9578 rettv->vval.v_string = do_browse(BROWSE_DIR,
Bram Moolenaar7b0294c2004-10-11 10:16:09 +00009579 title, NULL, NULL, initdir, NULL, curbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009580#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009581 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009582#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009583 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009584}
9585
Bram Moolenaar48e697e2016-01-23 22:17:30 +01009586static buf_T *find_buffer(typval_T *avar);
Bram Moolenaar0d660222005-01-07 21:51:51 +00009587
Bram Moolenaar071d4272004-06-13 20:20:40 +00009588/*
9589 * Find a buffer by number or exact name.
9590 */
9591 static buf_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +01009592find_buffer(typval_T *avar)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009593{
9594 buf_T *buf = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009595
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009596 if (avar->v_type == VAR_NUMBER)
9597 buf = buflist_findnr((int)avar->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +00009598 else if (avar->v_type == VAR_STRING && avar->vval.v_string != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009599 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009600 buf = buflist_findname_exp(avar->vval.v_string);
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009601 if (buf == NULL)
9602 {
9603 /* No full path name match, try a match with a URL or a "nofile"
9604 * buffer, these don't use the full path. */
9605 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
9606 if (buf->b_fname != NULL
9607 && (path_with_url(buf->b_fname)
9608#ifdef FEAT_QUICKFIX
9609 || bt_nofile(buf)
9610#endif
9611 )
Bram Moolenaar49cd9572005-01-03 21:06:01 +00009612 && STRCMP(buf->b_fname, avar->vval.v_string) == 0)
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00009613 break;
9614 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00009615 }
9616 return buf;
9617}
9618
9619/*
9620 * "bufexists(expr)" function
9621 */
9622 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009623f_bufexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009624{
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009625 rettv->vval.v_number = (find_buffer(&argvars[0]) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009626}
9627
9628/*
9629 * "buflisted(expr)" function
9630 */
9631 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009632f_buflisted(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009633{
9634 buf_T *buf;
9635
9636 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009637 rettv->vval.v_number = (buf != NULL && buf->b_p_bl);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009638}
9639
9640/*
9641 * "bufloaded(expr)" function
9642 */
9643 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009644f_bufloaded(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009645{
9646 buf_T *buf;
9647
9648 buf = find_buffer(&argvars[0]);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009649 rettv->vval.v_number = (buf != NULL && buf->b_ml.ml_mfp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009650}
9651
Bram Moolenaar8e2c9422016-03-12 13:43:33 +01009652 buf_T *
Bram Moolenaar014069a2016-03-03 22:51:40 +01009653buflist_find_by_name(char_u *name, int curtab_only)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009654{
Bram Moolenaar071d4272004-06-13 20:20:40 +00009655 int save_magic;
9656 char_u *save_cpo;
9657 buf_T *buf;
9658
Bram Moolenaar071d4272004-06-13 20:20:40 +00009659 /* Ignore 'magic' and 'cpoptions' here to make scripts portable */
9660 save_magic = p_magic;
9661 p_magic = TRUE;
9662 save_cpo = p_cpo;
9663 p_cpo = (char_u *)"";
9664
9665 buf = buflist_findnr(buflist_findpat(name, name + STRLEN(name),
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009666 TRUE, FALSE, curtab_only));
Bram Moolenaar071d4272004-06-13 20:20:40 +00009667
9668 p_magic = save_magic;
9669 p_cpo = save_cpo;
Bram Moolenaar014069a2016-03-03 22:51:40 +01009670 return buf;
9671}
9672
9673/*
9674 * Get buffer by number or pattern.
9675 */
9676 static buf_T *
9677get_buf_tv(typval_T *tv, int curtab_only)
9678{
9679 char_u *name = tv->vval.v_string;
9680 buf_T *buf;
9681
9682 if (tv->v_type == VAR_NUMBER)
9683 return buflist_findnr((int)tv->vval.v_number);
9684 if (tv->v_type != VAR_STRING)
9685 return NULL;
9686 if (name == NULL || *name == NUL)
9687 return curbuf;
9688 if (name[0] == '$' && name[1] == NUL)
9689 return lastbuf;
9690
9691 buf = buflist_find_by_name(name, curtab_only);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009692
9693 /* If not found, try expanding the name, like done for bufexists(). */
9694 if (buf == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009695 buf = find_buffer(tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009696
9697 return buf;
9698}
9699
9700/*
9701 * "bufname(expr)" function
9702 */
9703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009704f_bufname(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009705{
9706 buf_T *buf;
9707
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009708 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009709 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009710 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009711 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009712 if (buf != NULL && buf->b_fname != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009713 rettv->vval.v_string = vim_strsave(buf->b_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009714 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009715 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009716 --emsg_off;
9717}
9718
9719/*
9720 * "bufnr(expr)" function
9721 */
9722 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009723f_bufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009724{
9725 buf_T *buf;
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009726 int error = FALSE;
9727 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009728
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009729 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009730 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009731 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0e34f622006-03-03 23:00:03 +00009732 --emsg_off;
9733
9734 /* If the buffer isn't found and the second argument is not zero create a
9735 * new buffer. */
9736 if (buf == NULL
9737 && argvars[1].v_type != VAR_UNKNOWN
9738 && get_tv_number_chk(&argvars[1], &error) != 0
9739 && !error
9740 && (name = get_tv_string_chk(&argvars[0])) != NULL
9741 && !error)
9742 buf = buflist_new(name, NULL, (linenr_T)1, 0);
9743
Bram Moolenaar071d4272004-06-13 20:20:40 +00009744 if (buf != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009745 rettv->vval.v_number = buf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009746 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009747 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009748}
9749
9750/*
9751 * "bufwinnr(nr)" function
9752 */
9753 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009754f_bufwinnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009755{
9756#ifdef FEAT_WINDOWS
9757 win_T *wp;
9758 int winnr = 0;
9759#endif
9760 buf_T *buf;
9761
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009762 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009763 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +01009764 buf = get_buf_tv(&argvars[0], TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009765#ifdef FEAT_WINDOWS
9766 for (wp = firstwin; wp; wp = wp->w_next)
9767 {
9768 ++winnr;
9769 if (wp->w_buffer == buf)
9770 break;
9771 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009772 rettv->vval.v_number = (wp != NULL ? winnr : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009773#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009774 rettv->vval.v_number = (curwin->w_buffer == buf ? 1 : -1);
Bram Moolenaar071d4272004-06-13 20:20:40 +00009775#endif
9776 --emsg_off;
9777}
9778
9779/*
9780 * "byte2line(byte)" function
9781 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009782 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009783f_byte2line(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +00009784{
9785#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009786 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009787#else
9788 long boff = 0;
9789
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009790 boff = get_tv_number(&argvars[0]) - 1; /* boff gets -1 on type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +00009791 if (boff < 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009792 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +00009793 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009794 rettv->vval.v_number = ml_find_line_or_offset(curbuf,
Bram Moolenaar071d4272004-06-13 20:20:40 +00009795 (linenr_T)0, &boff);
9796#endif
9797}
9798
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009799 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009800byteidx(typval_T *argvars, typval_T *rettv, int comp UNUSED)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009801{
9802#ifdef FEAT_MBYTE
9803 char_u *t;
9804#endif
9805 char_u *str;
9806 long idx;
9807
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009808 str = get_tv_string_chk(&argvars[0]);
9809 idx = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009810 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009811 if (str == NULL || idx < 0)
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009812 return;
9813
9814#ifdef FEAT_MBYTE
9815 t = str;
9816 for ( ; idx > 0; idx--)
9817 {
9818 if (*t == NUL) /* EOL reached */
9819 return;
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009820 if (enc_utf8 && comp)
9821 t += utf_ptr2len(t);
9822 else
9823 t += (*mb_ptr2len)(t);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009824 }
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00009825 rettv->vval.v_number = (varnumber_T)(t - str);
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009826#else
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009827 if ((size_t)idx <= STRLEN(str))
Bram Moolenaarc70646c2005-01-04 21:52:38 +00009828 rettv->vval.v_number = idx;
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009829#endif
9830}
9831
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009832/*
9833 * "byteidx()" function
9834 */
9835 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009836f_byteidx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009837{
9838 byteidx(argvars, rettv, FALSE);
9839}
9840
9841/*
9842 * "byteidxcomp()" function
9843 */
9844 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009845f_byteidxcomp(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0ffbbf92013-11-02 23:29:26 +01009846{
9847 byteidx(argvars, rettv, TRUE);
9848}
9849
Bram Moolenaardb913952012-06-29 12:54:53 +02009850 int
Bram Moolenaar7454a062016-01-30 15:14:10 +01009851func_call(
9852 char_u *name,
9853 typval_T *args,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009854 partial_T *partial,
Bram Moolenaar7454a062016-01-30 15:14:10 +01009855 dict_T *selfdict,
9856 typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +02009857{
9858 listitem_T *item;
9859 typval_T argv[MAX_FUNC_ARGS + 1];
9860 int argc = 0;
9861 int dummy;
9862 int r = 0;
9863
9864 for (item = args->vval.v_list->lv_first; item != NULL;
9865 item = item->li_next)
9866 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009867 if (argc == MAX_FUNC_ARGS - (partial == NULL ? 0 : partial->pt_argc))
Bram Moolenaardb913952012-06-29 12:54:53 +02009868 {
9869 EMSG(_("E699: Too many arguments"));
9870 break;
9871 }
9872 /* Make a copy of each argument. This is needed to be able to set
9873 * v_lock to VAR_FIXED in the copy without changing the original list.
9874 */
9875 copy_tv(&item->li_tv, &argv[argc++]);
9876 }
9877
9878 if (item == NULL)
9879 r = call_func(name, (int)STRLEN(name), rettv, argc, argv,
9880 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009881 &dummy, TRUE, partial, selfdict);
Bram Moolenaardb913952012-06-29 12:54:53 +02009882
9883 /* Free the arguments. */
9884 while (argc > 0)
9885 clear_tv(&argv[--argc]);
9886
9887 return r;
9888}
9889
Bram Moolenaarab79bcb2004-07-18 21:34:53 +00009890/*
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009891 * "call(func, arglist [, dict])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009892 */
9893 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009894f_call(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009895{
9896 char_u *func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009897 partial_T *partial = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +00009898 dict_T *selfdict = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009899
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009900 if (argvars[1].v_type != VAR_LIST)
9901 {
9902 EMSG(_(e_listreq));
9903 return;
9904 }
9905 if (argvars[1].vval.v_list == NULL)
9906 return;
9907
9908 if (argvars[0].v_type == VAR_FUNC)
9909 func = argvars[0].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009910 else if (argvars[0].v_type == VAR_PARTIAL)
9911 {
9912 partial = argvars[0].vval.v_partial;
9913 func = partial->pt_name;
9914 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009915 else
9916 func = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +00009917 if (*func == NUL)
9918 return; /* type error or empty name */
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009919
Bram Moolenaare9a41262005-01-15 22:18:47 +00009920 if (argvars[2].v_type != VAR_UNKNOWN)
9921 {
9922 if (argvars[2].v_type != VAR_DICT)
9923 {
9924 EMSG(_(e_dictreq));
9925 return;
9926 }
9927 selfdict = argvars[2].vval.v_dict;
9928 }
9929
Bram Moolenaar1735bc92016-03-14 23:05:14 +01009930 (void)func_call(func, &argvars[1], partial, selfdict, rettv);
Bram Moolenaar8a283e52005-01-06 23:28:25 +00009931}
9932
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009933#ifdef FEAT_FLOAT
9934/*
9935 * "ceil({float})" function
9936 */
9937 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +01009938f_ceil(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009939{
Bram Moolenaara1e24b92016-02-18 20:18:09 +01009940 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +00009941
9942 rettv->v_type = VAR_FLOAT;
9943 if (get_float_arg(argvars, &f) == OK)
9944 rettv->vval.v_float = ceil(f);
9945 else
9946 rettv->vval.v_float = 0.0;
9947}
9948#endif
9949
Bram Moolenaar509ce2a2016-03-11 22:52:15 +01009950#ifdef FEAT_JOB_CHANNEL
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009951/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009952 * "ch_close()" function
9953 */
9954 static void
9955f_ch_close(typval_T *argvars, typval_T *rettv UNUSED)
9956{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009957 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009958
9959 if (channel != NULL)
Bram Moolenaar187db502016-02-27 14:44:26 +01009960 {
Bram Moolenaar8b374212016-02-24 20:43:06 +01009961 channel_close(channel, FALSE);
Bram Moolenaar187db502016-02-27 14:44:26 +01009962 channel_clear(channel);
9963 }
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +01009964}
9965
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009966/*
9967 * "ch_getbufnr()" function
9968 */
9969 static void
9970f_ch_getbufnr(typval_T *argvars, typval_T *rettv)
9971{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009972 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaarc7f0ebc2016-02-27 21:10:09 +01009973
9974 rettv->vval.v_number = -1;
9975 if (channel != NULL)
9976 {
9977 char_u *what = get_tv_string(&argvars[1]);
9978 int part;
9979
9980 if (STRCMP(what, "err") == 0)
9981 part = PART_ERR;
9982 else if (STRCMP(what, "out") == 0)
9983 part = PART_OUT;
9984 else if (STRCMP(what, "in") == 0)
9985 part = PART_IN;
9986 else
9987 part = PART_SOCK;
9988 if (channel->ch_part[part].ch_buffer != NULL)
9989 rettv->vval.v_number = channel->ch_part[part].ch_buffer->b_fnum;
9990 }
9991}
9992
Bram Moolenaar02e83b42016-02-21 20:10:26 +01009993/*
9994 * "ch_getjob()" function
9995 */
9996 static void
9997f_ch_getjob(typval_T *argvars, typval_T *rettv)
9998{
Bram Moolenaarf65333c2016-03-08 18:27:21 +01009999 channel_T *channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010000
10001 if (channel != NULL)
10002 {
10003 rettv->v_type = VAR_JOB;
10004 rettv->vval.v_job = channel->ch_job;
10005 if (channel->ch_job != NULL)
10006 ++channel->ch_job->jv_refcount;
10007 }
10008}
Bram Moolenaar02e83b42016-02-21 20:10:26 +010010009
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010010/*
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010011 * "ch_log()" function
10012 */
10013 static void
10014f_ch_log(typval_T *argvars, typval_T *rettv UNUSED)
10015{
10016 char_u *msg = get_tv_string(&argvars[0]);
10017 channel_T *channel = NULL;
10018
10019 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010020 channel = get_channel_arg(&argvars[1], TRUE);
Bram Moolenaar81661fb2016-02-18 22:23:34 +010010021
10022 ch_log(channel, (char *)msg);
10023}
10024
10025/*
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010026 * "ch_logfile()" function
10027 */
10028 static void
10029f_ch_logfile(typval_T *argvars, typval_T *rettv UNUSED)
10030{
10031 char_u *fname;
10032 char_u *opt = (char_u *)"";
10033 char_u buf[NUMBUFLEN];
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010034
10035 fname = get_tv_string(&argvars[0]);
10036 if (argvars[1].v_type == VAR_STRING)
10037 opt = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010038 ch_logfile(fname, opt);
Bram Moolenaarcd39bbc2016-02-17 10:05:42 +010010039}
Bram Moolenaarba093bc2016-02-16 19:37:29 +010010040
10041/*
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010042 * "ch_open()" function
10043 */
10044 static void
10045f_ch_open(typval_T *argvars, typval_T *rettv)
10046{
Bram Moolenaar77073442016-02-13 23:23:53 +010010047 rettv->v_type = VAR_CHANNEL;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010010048 rettv->vval.v_channel = channel_open_func(argvars);
Bram Moolenaar77073442016-02-13 23:23:53 +010010049}
10050
10051/*
Bram Moolenaar6f3a5442016-02-20 19:56:13 +010010052 * "ch_read()" function
10053 */
10054 static void
10055f_ch_read(typval_T *argvars, typval_T *rettv)
10056{
10057 common_channel_read(argvars, rettv, FALSE);
10058}
10059
10060/*
10061 * "ch_readraw()" function
10062 */
10063 static void
10064f_ch_readraw(typval_T *argvars, typval_T *rettv)
10065{
10066 common_channel_read(argvars, rettv, TRUE);
10067}
10068
10069/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010070 * "ch_evalexpr()" function
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010071 */
10072 static void
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010073f_ch_evalexpr(typval_T *argvars, typval_T *rettv)
10074{
10075 ch_expr_common(argvars, rettv, TRUE);
10076}
10077
10078/*
10079 * "ch_sendexpr()" function
10080 */
10081 static void
10082f_ch_sendexpr(typval_T *argvars, typval_T *rettv)
10083{
10084 ch_expr_common(argvars, rettv, FALSE);
10085}
10086
10087/*
Bram Moolenaar8b1862a2016-02-27 19:21:24 +010010088 * "ch_evalraw()" function
10089 */
10090 static void
10091f_ch_evalraw(typval_T *argvars, typval_T *rettv)
10092{
10093 ch_raw_common(argvars, rettv, TRUE);
10094}
10095
10096/*
10097 * "ch_sendraw()" function
10098 */
10099 static void
10100f_ch_sendraw(typval_T *argvars, typval_T *rettv)
10101{
10102 ch_raw_common(argvars, rettv, FALSE);
10103}
10104
10105/*
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010106 * "ch_setoptions()" function
10107 */
10108 static void
10109f_ch_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
10110{
10111 channel_T *channel;
10112 jobopt_T opt;
10113
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010114 channel = get_channel_arg(&argvars[0], TRUE);
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010115 if (channel == NULL)
10116 return;
Bram Moolenaarb6b52522016-02-20 23:30:07 +010010117 clear_job_options(&opt);
10118 if (get_job_options(&argvars[1], &opt,
10119 JO_CB_ALL + JO_TIMEOUT_ALL + JO_MODE_ALL) == FAIL)
Bram Moolenaar132006c2016-02-19 22:38:15 +010010120 return;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010121 channel_set_options(channel, &opt);
10122}
10123
10124/*
10125 * "ch_status()" function
10126 */
10127 static void
10128f_ch_status(typval_T *argvars, typval_T *rettv)
10129{
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010130 channel_T *channel;
10131
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010132 /* return an empty string by default */
10133 rettv->v_type = VAR_STRING;
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010134 rettv->vval.v_string = NULL;
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010135
Bram Moolenaarf65333c2016-03-08 18:27:21 +010010136 channel = get_channel_arg(&argvars[0], FALSE);
10137 rettv->vval.v_string = vim_strsave((char_u *)channel_status(channel));
Bram Moolenaar40ea1da2016-02-19 22:33:35 +010010138}
Bram Moolenaarf57969a2016-02-02 20:47:49 +010010139#endif
10140
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010141/*
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010142 * "changenr()" function
10143 */
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010144 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010145f_changenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarf0acfce2006-03-17 23:21:19 +000010146{
10147 rettv->vval.v_number = curbuf->b_u_seq_cur;
10148}
10149
10150/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010151 * "char2nr(string)" function
10152 */
10153 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010154f_char2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010155{
10156#ifdef FEAT_MBYTE
10157 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010010158 {
10159 int utf8 = 0;
10160
10161 if (argvars[1].v_type != VAR_UNKNOWN)
10162 utf8 = get_tv_number_chk(&argvars[1], NULL);
10163
10164 if (utf8)
10165 rettv->vval.v_number = (*utf_ptr2char)(get_tv_string(&argvars[0]));
10166 else
10167 rettv->vval.v_number = (*mb_ptr2char)(get_tv_string(&argvars[0]));
10168 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010169 else
10170#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010171 rettv->vval.v_number = get_tv_string(&argvars[0])[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +000010172}
10173
10174/*
10175 * "cindent(lnum)" function
10176 */
10177 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010178f_cindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010179{
10180#ifdef FEAT_CINDENT
10181 pos_T pos;
10182 linenr_T lnum;
10183
10184 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010185 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010186 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
10187 {
10188 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010189 rettv->vval.v_number = get_c_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000010190 curwin->w_cursor = pos;
10191 }
10192 else
10193#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010194 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010195}
10196
10197/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010198 * "clearmatches()" function
10199 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010200 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010201f_clearmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000010202{
10203#ifdef FEAT_SEARCH_EXTRA
10204 clear_matches(curwin);
10205#endif
10206}
10207
10208/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010209 * "col(string)" function
10210 */
10211 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010212f_col(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010213{
10214 colnr_T col = 0;
10215 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010216 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010217
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010218 fp = var2fpos(&argvars[0], FALSE, &fnum);
10219 if (fp != NULL && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010220 {
10221 if (fp->col == MAXCOL)
10222 {
10223 /* '> can be MAXCOL, get the length of the line then */
10224 if (fp->lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010225 col = (colnr_T)STRLEN(ml_get(fp->lnum)) + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010226 else
10227 col = MAXCOL;
10228 }
10229 else
10230 {
10231 col = fp->col + 1;
10232#ifdef FEAT_VIRTUALEDIT
10233 /* col(".") when the cursor is on the NUL at the end of the line
10234 * because of "coladd" can be seen as an extra column. */
10235 if (virtual_active() && fp == &curwin->w_cursor)
10236 {
10237 char_u *p = ml_get_cursor();
10238
10239 if (curwin->w_cursor.coladd >= (colnr_T)chartabsize(p,
10240 curwin->w_virtcol - curwin->w_cursor.coladd))
10241 {
10242# ifdef FEAT_MBYTE
10243 int l;
10244
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000010245 if (*p != NUL && p[(l = (*mb_ptr2len)(p))] == NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010246 col += l;
10247# else
10248 if (*p != NUL && p[1] == NUL)
10249 ++col;
10250# endif
10251 }
10252 }
10253#endif
10254 }
10255 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010256 rettv->vval.v_number = col;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010257}
10258
Bram Moolenaar572cb562005-08-05 21:35:02 +000010259#if defined(FEAT_INS_EXPAND)
10260/*
Bram Moolenaarade00832006-03-10 21:46:58 +000010261 * "complete()" function
10262 */
Bram Moolenaarade00832006-03-10 21:46:58 +000010263 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010264f_complete(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarade00832006-03-10 21:46:58 +000010265{
10266 int startcol;
10267
10268 if ((State & INSERT) == 0)
10269 {
10270 EMSG(_("E785: complete() can only be used in Insert mode"));
10271 return;
10272 }
Bram Moolenaarce6ef252006-07-12 19:49:41 +000010273
10274 /* Check for undo allowed here, because if something was already inserted
10275 * the line was already saved for undo and this check isn't done. */
10276 if (!undo_allowed())
10277 return;
10278
Bram Moolenaarade00832006-03-10 21:46:58 +000010279 if (argvars[1].v_type != VAR_LIST || argvars[1].vval.v_list == NULL)
10280 {
10281 EMSG(_(e_invarg));
10282 return;
10283 }
10284
10285 startcol = get_tv_number_chk(&argvars[0], NULL);
10286 if (startcol <= 0)
10287 return;
10288
10289 set_completion(startcol - 1, argvars[1].vval.v_list);
10290}
10291
10292/*
Bram Moolenaar572cb562005-08-05 21:35:02 +000010293 * "complete_add()" function
10294 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010295 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010296f_complete_add(typval_T *argvars, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010297{
Bram Moolenaarceaf7b82006-03-19 22:18:55 +000010298 rettv->vval.v_number = ins_compl_add_tv(&argvars[0], 0);
Bram Moolenaar572cb562005-08-05 21:35:02 +000010299}
10300
10301/*
10302 * "complete_check()" function
10303 */
Bram Moolenaar572cb562005-08-05 21:35:02 +000010304 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010305f_complete_check(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar572cb562005-08-05 21:35:02 +000010306{
10307 int saved = RedrawingDisabled;
10308
10309 RedrawingDisabled = 0;
10310 ins_compl_check_keys(0);
10311 rettv->vval.v_number = compl_interrupted;
10312 RedrawingDisabled = saved;
10313}
10314#endif
10315
Bram Moolenaar071d4272004-06-13 20:20:40 +000010316/*
10317 * "confirm(message, buttons[, default [, type]])" function
10318 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010319 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010320f_confirm(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010321{
10322#if defined(FEAT_GUI_DIALOG) || defined(FEAT_CON_DIALOG)
10323 char_u *message;
10324 char_u *buttons = NULL;
10325 char_u buf[NUMBUFLEN];
10326 char_u buf2[NUMBUFLEN];
10327 int def = 1;
10328 int type = VIM_GENERIC;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010329 char_u *typestr;
10330 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010331
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010332 message = get_tv_string_chk(&argvars[0]);
10333 if (message == NULL)
10334 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010335 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010336 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010337 buttons = get_tv_string_buf_chk(&argvars[1], buf);
10338 if (buttons == NULL)
10339 error = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010340 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010341 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010342 def = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010343 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010344 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010345 typestr = get_tv_string_buf_chk(&argvars[3], buf2);
10346 if (typestr == NULL)
10347 error = TRUE;
10348 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000010349 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010350 switch (TOUPPER_ASC(*typestr))
10351 {
10352 case 'E': type = VIM_ERROR; break;
10353 case 'Q': type = VIM_QUESTION; break;
10354 case 'I': type = VIM_INFO; break;
10355 case 'W': type = VIM_WARNING; break;
10356 case 'G': type = VIM_GENERIC; break;
10357 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010358 }
10359 }
10360 }
10361 }
10362
10363 if (buttons == NULL || *buttons == NUL)
10364 buttons = (char_u *)_("&Ok");
10365
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010366 if (!error)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010367 rettv->vval.v_number = do_dialog(type, NULL, message, buttons,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010010368 def, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010369#endif
10370}
10371
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010372/*
10373 * "copy()" function
10374 */
10375 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010376f_copy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010377{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010378 item_copy(&argvars[0], rettv, FALSE, 0);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010379}
Bram Moolenaar071d4272004-06-13 20:20:40 +000010380
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010381#ifdef FEAT_FLOAT
10382/*
10383 * "cos()" function
10384 */
10385 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010386f_cos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010387{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010388 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010389
10390 rettv->v_type = VAR_FLOAT;
10391 if (get_float_arg(argvars, &f) == OK)
10392 rettv->vval.v_float = cos(f);
10393 else
10394 rettv->vval.v_float = 0.0;
10395}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010396
10397/*
10398 * "cosh()" function
10399 */
10400 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010401f_cosh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010402{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010403 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010404
10405 rettv->v_type = VAR_FLOAT;
10406 if (get_float_arg(argvars, &f) == OK)
10407 rettv->vval.v_float = cosh(f);
10408 else
10409 rettv->vval.v_float = 0.0;
10410}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010411#endif
10412
Bram Moolenaar071d4272004-06-13 20:20:40 +000010413/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010414 * "count()" function
10415 */
10416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010417f_count(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010418{
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010419 long n = 0;
10420 int ic = FALSE;
10421
Bram Moolenaare9a41262005-01-15 22:18:47 +000010422 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010423 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010424 listitem_T *li;
10425 list_T *l;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010426 long idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010427
Bram Moolenaare9a41262005-01-15 22:18:47 +000010428 if ((l = argvars[0].vval.v_list) != NULL)
10429 {
10430 li = l->lv_first;
10431 if (argvars[2].v_type != VAR_UNKNOWN)
10432 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010433 int error = FALSE;
10434
10435 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010436 if (argvars[3].v_type != VAR_UNKNOWN)
10437 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010438 idx = get_tv_number_chk(&argvars[3], &error);
10439 if (!error)
10440 {
10441 li = list_find(l, idx);
10442 if (li == NULL)
10443 EMSGN(_(e_listidx), idx);
10444 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010445 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010446 if (error)
10447 li = NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010448 }
10449
10450 for ( ; li != NULL; li = li->li_next)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010451 if (tv_equal(&li->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaare9a41262005-01-15 22:18:47 +000010452 ++n;
10453 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010454 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010455 else if (argvars[0].v_type == VAR_DICT)
10456 {
Bram Moolenaar33570922005-01-25 22:26:29 +000010457 int todo;
10458 dict_T *d;
10459 hashitem_T *hi;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010460
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010461 if ((d = argvars[0].vval.v_dict) != NULL)
10462 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010463 int error = FALSE;
10464
Bram Moolenaare9a41262005-01-15 22:18:47 +000010465 if (argvars[2].v_type != VAR_UNKNOWN)
10466 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010467 ic = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaare9a41262005-01-15 22:18:47 +000010468 if (argvars[3].v_type != VAR_UNKNOWN)
10469 EMSG(_(e_invarg));
10470 }
10471
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000010472 todo = error ? 0 : (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000010473 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010474 {
10475 if (!HASHITEM_EMPTY(hi))
10476 {
10477 --todo;
Bram Moolenaar67b3f992010-11-10 20:41:57 +010010478 if (tv_equal(&HI2DI(hi)->di_tv, &argvars[1], ic, FALSE))
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000010479 ++n;
10480 }
10481 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000010482 }
10483 }
10484 else
10485 EMSG2(_(e_listdictarg), "count()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000010486 rettv->vval.v_number = n;
10487}
10488
10489/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010490 * "cscope_connection([{num} , {dbpath} [, {prepend}]])" function
10491 *
10492 * Checks the existence of a cscope connection.
10493 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010494 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010495f_cscope_connection(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010496{
10497#ifdef FEAT_CSCOPE
10498 int num = 0;
10499 char_u *dbpath = NULL;
10500 char_u *prepend = NULL;
10501 char_u buf[NUMBUFLEN];
10502
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010503 if (argvars[0].v_type != VAR_UNKNOWN
10504 && argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010505 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010506 num = (int)get_tv_number(&argvars[0]);
10507 dbpath = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010508 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010509 prepend = get_tv_string_buf(&argvars[2], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010510 }
10511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010512 rettv->vval.v_number = cs_connection(num, dbpath, prepend);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010513#endif
10514}
10515
10516/*
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010517 * "cursor(lnum, col)" function, or
10518 * "cursor(list)"
Bram Moolenaar071d4272004-06-13 20:20:40 +000010519 *
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010520 * Moves the cursor to the specified line and column.
10521 * Returns 0 when the position could be set, -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000010522 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010523 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010524f_cursor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010525{
10526 long line, col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010527#ifdef FEAT_VIRTUALEDIT
10528 long coladd = 0;
10529#endif
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010530 int set_curswant = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010531
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010532 rettv->vval.v_number = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010533 if (argvars[1].v_type == VAR_UNKNOWN)
10534 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010535 pos_T pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020010536 colnr_T curswant = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000010537
Bram Moolenaar493c1782014-05-28 14:34:46 +020010538 if (list2fpos(argvars, &pos, NULL, &curswant) == FAIL)
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010539 {
10540 EMSG(_(e_invarg));
Bram Moolenaara5525202006-03-02 22:52:09 +000010541 return;
Bram Moolenaar24c4d532016-01-15 15:37:20 +010010542 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010543 line = pos.lnum;
10544 col = pos.col;
Bram Moolenaara5525202006-03-02 22:52:09 +000010545#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar0e34f622006-03-03 23:00:03 +000010546 coladd = pos.coladd;
Bram Moolenaara5525202006-03-02 22:52:09 +000010547#endif
Bram Moolenaar493c1782014-05-28 14:34:46 +020010548 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010549 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020010550 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010551 set_curswant = FALSE;
10552 }
Bram Moolenaara5525202006-03-02 22:52:09 +000010553 }
10554 else
10555 {
10556 line = get_tv_lnum(argvars);
10557 col = get_tv_number_chk(&argvars[1], NULL);
10558#ifdef FEAT_VIRTUALEDIT
10559 if (argvars[2].v_type != VAR_UNKNOWN)
10560 coladd = get_tv_number_chk(&argvars[2], NULL);
10561#endif
10562 }
10563 if (line < 0 || col < 0
10564#ifdef FEAT_VIRTUALEDIT
10565 || coladd < 0
10566#endif
10567 )
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010568 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010569 if (line > 0)
10570 curwin->w_cursor.lnum = line;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010571 if (col > 0)
10572 curwin->w_cursor.col = col - 1;
10573#ifdef FEAT_VIRTUALEDIT
Bram Moolenaara5525202006-03-02 22:52:09 +000010574 curwin->w_cursor.coladd = coladd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010575#endif
10576
10577 /* Make sure the cursor is in a valid position. */
10578 check_cursor();
10579#ifdef FEAT_MBYTE
10580 /* Correct cursor for multi-byte character. */
10581 if (has_mbyte)
10582 mb_adjust_cursor();
10583#endif
Bram Moolenaarf4b8e572004-06-24 15:53:16 +000010584
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010010585 curwin->w_set_curswant = set_curswant;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000010586 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010587}
10588
10589/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010590 * "deepcopy()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000010591 */
10592 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010593f_deepcopy(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010594{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010595 int noref = 0;
10596
10597 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010598 noref = get_tv_number_chk(&argvars[1], NULL);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000010599 if (noref < 0 || noref > 1)
10600 EMSG(_(e_invarg));
10601 else
Bram Moolenaar2c2398c2009-06-03 11:22:45 +000010602 {
10603 current_copyID += COPYID_INC;
10604 item_copy(&argvars[0], rettv, TRUE, noref == 0 ? current_copyID : 0);
10605 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010606}
10607
10608/*
10609 * "delete()" function
10610 */
10611 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010612f_delete(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010613{
Bram Moolenaarda440d22016-01-16 21:27:23 +010010614 char_u nbuf[NUMBUFLEN];
10615 char_u *name;
10616 char_u *flags;
10617
10618 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010619 if (check_restricted() || check_secure())
Bram Moolenaarda440d22016-01-16 21:27:23 +010010620 return;
10621
10622 name = get_tv_string(&argvars[0]);
10623 if (name == NULL || *name == NUL)
10624 {
10625 EMSG(_(e_invarg));
10626 return;
10627 }
10628
10629 if (argvars[1].v_type != VAR_UNKNOWN)
10630 flags = get_tv_string_buf(&argvars[1], nbuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010631 else
Bram Moolenaarda440d22016-01-16 21:27:23 +010010632 flags = (char_u *)"";
10633
10634 if (*flags == NUL)
10635 /* delete a file */
10636 rettv->vval.v_number = mch_remove(name) == 0 ? 0 : -1;
10637 else if (STRCMP(flags, "d") == 0)
10638 /* delete an empty directory */
10639 rettv->vval.v_number = mch_rmdir(name) == 0 ? 0 : -1;
10640 else if (STRCMP(flags, "rf") == 0)
Bram Moolenaar43a34f92016-01-17 15:56:34 +010010641 /* delete a directory recursively */
Bram Moolenaarda440d22016-01-16 21:27:23 +010010642 rettv->vval.v_number = delete_recursive(name);
10643 else
10644 EMSG2(_(e_invexpr2), flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010645}
10646
10647/*
10648 * "did_filetype()" function
10649 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010650 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010651f_did_filetype(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010652{
10653#ifdef FEAT_AUTOCMD
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010654 rettv->vval.v_number = did_filetype;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010655#endif
10656}
10657
10658/*
Bram Moolenaar47136d72004-10-12 20:02:24 +000010659 * "diff_filler()" function
10660 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010661 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010662f_diff_filler(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010663{
10664#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010665 rettv->vval.v_number = diff_check_fill(curwin, get_tv_lnum(argvars));
Bram Moolenaar47136d72004-10-12 20:02:24 +000010666#endif
10667}
10668
10669/*
10670 * "diff_hlID()" function
10671 */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010672 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010673f_diff_hlID(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar47136d72004-10-12 20:02:24 +000010674{
10675#ifdef FEAT_DIFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010676 linenr_T lnum = get_tv_lnum(argvars);
Bram Moolenaar47136d72004-10-12 20:02:24 +000010677 static linenr_T prev_lnum = 0;
10678 static int changedtick = 0;
10679 static int fnum = 0;
10680 static int change_start = 0;
10681 static int change_end = 0;
Bram Moolenaar6f192452007-11-08 19:49:02 +000010682 static hlf_T hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010683 int filler_lines;
10684 int col;
10685
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010686 if (lnum < 0) /* ignore type error in {lnum} arg */
10687 lnum = 0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010688 if (lnum != prev_lnum
10689 || changedtick != curbuf->b_changedtick
10690 || fnum != curbuf->b_fnum)
10691 {
10692 /* New line, buffer, change: need to get the values. */
10693 filler_lines = diff_check(curwin, lnum);
10694 if (filler_lines < 0)
10695 {
10696 if (filler_lines == -1)
10697 {
10698 change_start = MAXCOL;
10699 change_end = -1;
10700 if (diff_find_change(curwin, lnum, &change_start, &change_end))
10701 hlID = HLF_ADD; /* added line */
10702 else
10703 hlID = HLF_CHD; /* changed line */
10704 }
10705 else
10706 hlID = HLF_ADD; /* added line */
10707 }
10708 else
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010709 hlID = (hlf_T)0;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010710 prev_lnum = lnum;
10711 changedtick = curbuf->b_changedtick;
10712 fnum = curbuf->b_fnum;
10713 }
10714
10715 if (hlID == HLF_CHD || hlID == HLF_TXD)
10716 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010717 col = get_tv_number(&argvars[1]) - 1; /* ignore type error in {col} */
Bram Moolenaar47136d72004-10-12 20:02:24 +000010718 if (col >= change_start && col <= change_end)
10719 hlID = HLF_TXD; /* changed text */
10720 else
10721 hlID = HLF_CHD; /* changed line */
10722 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000010723 rettv->vval.v_number = hlID == (hlf_T)0 ? 0 : (int)hlID;
Bram Moolenaar47136d72004-10-12 20:02:24 +000010724#endif
10725}
10726
10727/*
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010010728 * "disable_char_avail_for_testing({expr})" function
10729 */
10730 static void
10731f_disable_char_avail_for_testing(typval_T *argvars, typval_T *rettv UNUSED)
10732{
10733 disable_char_avail_for_testing = get_tv_number(&argvars[0]);
10734}
10735
10736/*
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010737 * "empty({expr})" function
10738 */
10739 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010740f_empty(typval_T *argvars, typval_T *rettv)
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010741{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010010742 int n = FALSE;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010743
10744 switch (argvars[0].v_type)
10745 {
10746 case VAR_STRING:
10747 case VAR_FUNC:
10748 n = argvars[0].vval.v_string == NULL
10749 || *argvars[0].vval.v_string == NUL;
10750 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010010751 case VAR_PARTIAL:
10752 n = FALSE;
10753 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010754 case VAR_NUMBER:
10755 n = argvars[0].vval.v_number == 0;
10756 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010757 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010010758#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000010759 n = argvars[0].vval.v_float == 0.0;
10760 break;
10761#endif
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010762 case VAR_LIST:
10763 n = argvars[0].vval.v_list == NULL
10764 || argvars[0].vval.v_list->lv_first == NULL;
10765 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010766 case VAR_DICT:
10767 n = argvars[0].vval.v_dict == NULL
Bram Moolenaar33570922005-01-25 22:26:29 +000010768 || argvars[0].vval.v_dict->dv_hashtab.ht_used == 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000010769 break;
Bram Moolenaar767d8c12016-01-25 20:22:54 +010010770 case VAR_SPECIAL:
10771 n = argvars[0].vval.v_number != VVAL_TRUE;
10772 break;
10773
Bram Moolenaar835dc632016-02-07 14:27:38 +010010774 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010775#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010776 n = argvars[0].vval.v_job == NULL
10777 || argvars[0].vval.v_job->jv_status != JOB_STARTED;
10778 break;
10779#endif
10780 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010010781#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010010782 n = argvars[0].vval.v_channel == NULL
10783 || !channel_is_open(argvars[0].vval.v_channel);
Bram Moolenaar835dc632016-02-07 14:27:38 +010010784 break;
10785#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010010786 case VAR_UNKNOWN:
10787 EMSG2(_(e_intern2), "f_empty(UNKNOWN)");
10788 n = TRUE;
10789 break;
Bram Moolenaare49b69a2005-01-08 16:11:57 +000010790 }
10791
10792 rettv->vval.v_number = n;
10793}
10794
10795/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010796 * "escape({string}, {chars})" function
10797 */
10798 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010799f_escape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010800{
10801 char_u buf[NUMBUFLEN];
10802
Bram Moolenaar758711c2005-02-02 23:11:38 +000010803 rettv->vval.v_string = vim_strsave_escaped(get_tv_string(&argvars[0]),
10804 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010805 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010806}
10807
10808/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010809 * "eval()" function
10810 */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010812f_eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010813{
Bram Moolenaar615b9972015-01-14 17:15:05 +010010814 char_u *s, *p;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010815
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010816 s = get_tv_string_chk(&argvars[0]);
10817 if (s != NULL)
10818 s = skipwhite(s);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010819
Bram Moolenaar615b9972015-01-14 17:15:05 +010010820 p = s;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010821 if (s == NULL || eval1(&s, rettv, TRUE) == FAIL)
10822 {
Bram Moolenaar615b9972015-01-14 17:15:05 +010010823 if (p != NULL && !aborting())
10824 EMSG2(_(e_invexpr2), p);
10825 need_clr_eos = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010826 rettv->v_type = VAR_NUMBER;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010827 rettv->vval.v_number = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010828 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000010829 else if (*s != NUL)
10830 EMSG(_(e_trailing));
10831}
10832
10833/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000010834 * "eventhandler()" function
10835 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000010836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010837f_eventhandler(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010838{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010839 rettv->vval.v_number = vgetc_busy;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010840}
10841
10842/*
10843 * "executable()" function
10844 */
10845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010846f_executable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010847{
Bram Moolenaarb5971142015-03-21 17:32:19 +010010848 char_u *name = get_tv_string(&argvars[0]);
10849
10850 /* Check in $PATH and also check directly if there is a directory name. */
10851 rettv->vval.v_number = mch_can_exe(name, NULL, TRUE)
10852 || (gettail(name) != name && mch_can_exe(name, NULL, FALSE));
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010853}
10854
10855/*
10856 * "exepath()" function
10857 */
10858 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010859f_exepath(typval_T *argvars, typval_T *rettv)
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010860{
10861 char_u *p = NULL;
10862
Bram Moolenaarb5971142015-03-21 17:32:19 +010010863 (void)mch_can_exe(get_tv_string(&argvars[0]), &p, TRUE);
Bram Moolenaarc7f02552014-04-01 21:00:59 +020010864 rettv->v_type = VAR_STRING;
10865 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010866}
10867
10868/*
10869 * "exists()" function
10870 */
10871 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010872f_exists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010873{
10874 char_u *p;
10875 char_u *name;
10876 int n = FALSE;
10877 int len = 0;
10878
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010879 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010880 if (*p == '$') /* environment variable */
10881 {
10882 /* first try "normal" environment variables (fast) */
10883 if (mch_getenv(p + 1) != NULL)
10884 n = TRUE;
10885 else
10886 {
10887 /* try expanding things like $VIM and ${HOME} */
10888 p = expand_env_save(p);
10889 if (p != NULL && *p != '$')
10890 n = TRUE;
10891 vim_free(p);
10892 }
10893 }
10894 else if (*p == '&' || *p == '+') /* option */
Bram Moolenaar79783442006-05-05 21:18:03 +000010895 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010896 n = (get_option_tv(&p, NULL, TRUE) == OK);
Bram Moolenaar79783442006-05-05 21:18:03 +000010897 if (*skipwhite(p) != NUL)
10898 n = FALSE; /* trailing garbage */
10899 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000010900 else if (*p == '*') /* internal or user defined function */
10901 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000010902 n = function_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010903 }
10904 else if (*p == ':')
10905 {
10906 n = cmd_exists(p + 1);
10907 }
10908 else if (*p == '#')
10909 {
10910#ifdef FEAT_AUTOCMD
Bram Moolenaarf4cd3e82005-12-22 22:47:02 +000010911 if (p[1] == '#')
10912 n = autocmd_supported(p + 2);
10913 else
10914 n = au_exists(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010915#endif
10916 }
10917 else /* internal variable */
10918 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010919 char_u *tofree;
10920 typval_T tv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010921
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010922 /* get_name_len() takes care of expanding curly braces */
10923 name = p;
10924 len = get_name_len(&p, &tofree, TRUE, FALSE);
10925 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010926 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010927 if (tofree != NULL)
10928 name = tofree;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020010929 n = (get_var_tv(name, len, &tv, NULL, FALSE, TRUE) == OK);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010930 if (n)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010931 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010932 /* handle d.key, l[idx], f(expr) */
10933 n = (handle_subscript(&p, &tv, TRUE, FALSE) == OK);
10934 if (n)
10935 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010936 }
10937 }
Bram Moolenaar79783442006-05-05 21:18:03 +000010938 if (*p != NUL)
10939 n = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010940
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000010941 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010942 }
10943
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010944 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010945}
10946
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010947#ifdef FEAT_FLOAT
10948/*
10949 * "exp()" function
10950 */
10951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010952f_exp(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010953{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010010954 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020010955
10956 rettv->v_type = VAR_FLOAT;
10957 if (get_float_arg(argvars, &f) == OK)
10958 rettv->vval.v_float = exp(f);
10959 else
10960 rettv->vval.v_float = 0.0;
10961}
10962#endif
10963
Bram Moolenaar071d4272004-06-13 20:20:40 +000010964/*
10965 * "expand()" function
10966 */
10967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010010968f_expand(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000010969{
10970 char_u *s;
10971 int len;
10972 char_u *errormsg;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010010973 int options = WILD_SILENT|WILD_USE_NL|WILD_LIST_NOTFOUND;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010974 expand_T xpc;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000010975 int error = FALSE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010976 char_u *result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000010977
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010978 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010979 if (argvars[1].v_type != VAR_UNKNOWN
10980 && argvars[2].v_type != VAR_UNKNOWN
10981 && get_tv_number_chk(&argvars[2], &error)
10982 && !error)
10983 {
10984 rettv->v_type = VAR_LIST;
10985 rettv->vval.v_list = NULL;
10986 }
10987
Bram Moolenaarc70646c2005-01-04 21:52:38 +000010988 s = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010989 if (*s == '%' || *s == '#' || *s == '<')
10990 {
10991 ++emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010992 result = eval_vars(s, s, &len, NULL, &errormsg, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000010993 --emsg_off;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010010994 if (rettv->v_type == VAR_LIST)
10995 {
10996 if (rettv_list_alloc(rettv) != FAIL && result != NULL)
10997 list_append_string(rettv->vval.v_list, result, -1);
10998 else
10999 vim_free(result);
11000 }
11001 else
11002 rettv->vval.v_string = result;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011003 }
11004 else
11005 {
11006 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000011007 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011008 if (argvars[1].v_type != VAR_UNKNOWN
11009 && get_tv_number_chk(&argvars[1], &error))
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011010 options |= WILD_KEEP_ALL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011011 if (!error)
11012 {
11013 ExpandInit(&xpc);
11014 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010011015 if (p_wic)
11016 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010011017 if (rettv->v_type == VAR_STRING)
11018 rettv->vval.v_string = ExpandOne(&xpc, s, NULL,
11019 options, WILD_ALL);
11020 else if (rettv_list_alloc(rettv) != FAIL)
11021 {
11022 int i;
11023
11024 ExpandOne(&xpc, s, NULL, options, WILD_ALL_KEEP);
11025 for (i = 0; i < xpc.xp_numfiles; i++)
11026 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
11027 ExpandCleanup(&xpc);
11028 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011029 }
11030 else
11031 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011032 }
11033}
11034
11035/*
Bram Moolenaara9922d62013-05-30 13:01:18 +020011036 * Go over all entries in "d2" and add them to "d1".
11037 * When "action" is "error" then a duplicate key is an error.
11038 * When "action" is "force" then a duplicate key is overwritten.
11039 * Otherwise duplicate keys are ignored ("action" is "keep").
11040 */
11041 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011042dict_extend(dict_T *d1, dict_T *d2, char_u *action)
Bram Moolenaara9922d62013-05-30 13:01:18 +020011043{
11044 dictitem_T *di1;
11045 hashitem_T *hi2;
11046 int todo;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011047 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaara9922d62013-05-30 13:01:18 +020011048
11049 todo = (int)d2->dv_hashtab.ht_used;
11050 for (hi2 = d2->dv_hashtab.ht_array; todo > 0; ++hi2)
11051 {
11052 if (!HASHITEM_EMPTY(hi2))
11053 {
11054 --todo;
11055 di1 = dict_find(d1, hi2->hi_key, -1);
11056 if (d1->dv_scope != 0)
11057 {
11058 /* Disallow replacing a builtin function in l: and g:.
11059 * Check the key to be valid when adding to any
11060 * scope. */
11061 if (d1->dv_scope == VAR_DEF_SCOPE
11062 && HI2DI(hi2)->di_tv.v_type == VAR_FUNC
11063 && var_check_func_name(hi2->hi_key,
11064 di1 == NULL))
11065 break;
11066 if (!valid_varname(hi2->hi_key))
11067 break;
11068 }
11069 if (di1 == NULL)
11070 {
11071 di1 = dictitem_copy(HI2DI(hi2));
11072 if (di1 != NULL && dict_add(d1, di1) == FAIL)
11073 dictitem_free(di1);
11074 }
11075 else if (*action == 'e')
11076 {
11077 EMSG2(_("E737: Key already exists: %s"), hi2->hi_key);
11078 break;
11079 }
11080 else if (*action == 'f' && HI2DI(hi2) != di1)
11081 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011082 if (tv_check_lock(di1->di_tv.v_lock, arg_errmsg, TRUE)
11083 || var_check_ro(di1->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011084 break;
Bram Moolenaara9922d62013-05-30 13:01:18 +020011085 clear_tv(&di1->di_tv);
11086 copy_tv(&HI2DI(hi2)->di_tv, &di1->di_tv);
11087 }
11088 }
11089 }
11090}
11091
11092/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011093 * "extend(list, list [, idx])" function
Bram Moolenaare9a41262005-01-15 22:18:47 +000011094 * "extend(dict, dict [, action])" function
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011095 */
11096 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011097f_extend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011098{
Bram Moolenaar77354e72015-04-21 16:49:05 +020011099 char_u *arg_errmsg = (char_u *)N_("extend() argument");
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011100
Bram Moolenaare9a41262005-01-15 22:18:47 +000011101 if (argvars[0].v_type == VAR_LIST && argvars[1].v_type == VAR_LIST)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011102 {
Bram Moolenaar33570922005-01-25 22:26:29 +000011103 list_T *l1, *l2;
11104 listitem_T *item;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011105 long before;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011106 int error = FALSE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011107
Bram Moolenaare9a41262005-01-15 22:18:47 +000011108 l1 = argvars[0].vval.v_list;
11109 l2 = argvars[1].vval.v_list;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011110 if (l1 != NULL && !tv_check_lock(l1->lv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011111 && l2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011112 {
11113 if (argvars[2].v_type != VAR_UNKNOWN)
11114 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011115 before = get_tv_number_chk(&argvars[2], &error);
11116 if (error)
11117 return; /* type error; errmsg already given */
11118
Bram Moolenaar758711c2005-02-02 23:11:38 +000011119 if (before == l1->lv_len)
11120 item = NULL;
11121 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000011122 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000011123 item = list_find(l1, before);
11124 if (item == NULL)
11125 {
11126 EMSGN(_(e_listidx), before);
11127 return;
11128 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011129 }
11130 }
11131 else
11132 item = NULL;
11133 list_extend(l1, l2, item);
11134
Bram Moolenaare9a41262005-01-15 22:18:47 +000011135 copy_tv(&argvars[0], rettv);
11136 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011137 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011138 else if (argvars[0].v_type == VAR_DICT && argvars[1].v_type == VAR_DICT)
11139 {
Bram Moolenaara9922d62013-05-30 13:01:18 +020011140 dict_T *d1, *d2;
11141 char_u *action;
11142 int i;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011143
11144 d1 = argvars[0].vval.v_dict;
11145 d2 = argvars[1].vval.v_dict;
Bram Moolenaar77354e72015-04-21 16:49:05 +020011146 if (d1 != NULL && !tv_check_lock(d1->dv_lock, arg_errmsg, TRUE)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011147 && d2 != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011148 {
11149 /* Check the third argument. */
11150 if (argvars[2].v_type != VAR_UNKNOWN)
11151 {
11152 static char *(av[]) = {"keep", "force", "error"};
11153
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011154 action = get_tv_string_chk(&argvars[2]);
11155 if (action == NULL)
11156 return; /* type error; errmsg already given */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011157 for (i = 0; i < 3; ++i)
11158 if (STRCMP(action, av[i]) == 0)
11159 break;
11160 if (i == 3)
11161 {
Bram Moolenaareb3593b2006-04-22 22:33:57 +000011162 EMSG2(_(e_invarg2), action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011163 return;
11164 }
11165 }
11166 else
11167 action = (char_u *)"force";
11168
Bram Moolenaara9922d62013-05-30 13:01:18 +020011169 dict_extend(d1, d2, action);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011170
Bram Moolenaare9a41262005-01-15 22:18:47 +000011171 copy_tv(&argvars[0], rettv);
11172 }
11173 }
11174 else
11175 EMSG2(_(e_listdictarg), "extend()");
Bram Moolenaar8a283e52005-01-06 23:28:25 +000011176}
11177
11178/*
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011179 * "feedkeys()" function
11180 */
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011181 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011182f_feedkeys(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011183{
11184 int remap = TRUE;
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011185 int insert = FALSE;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011186 char_u *keys, *flags;
11187 char_u nbuf[NUMBUFLEN];
11188 int typed = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011189 int execute = FALSE;
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011190 char_u *keys_esc;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011191
Bram Moolenaar3d43a662007-04-27 20:15:55 +000011192 /* This is not allowed in the sandbox. If the commands would still be
11193 * executed in the sandbox it would be OK, but it probably happens later,
11194 * when "sandbox" is no longer set. */
11195 if (check_secure())
11196 return;
11197
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011198 keys = get_tv_string(&argvars[0]);
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011199
11200 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011201 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011202 flags = get_tv_string_buf(&argvars[1], nbuf);
11203 for ( ; *flags != NUL; ++flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011204 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011205 switch (*flags)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011206 {
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011207 case 'n': remap = FALSE; break;
11208 case 'm': remap = TRUE; break;
11209 case 't': typed = TRUE; break;
11210 case 'i': insert = TRUE; break;
11211 case 'x': execute = TRUE; break;
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011212 }
11213 }
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011214 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011215
Bram Moolenaar74c5bbf2016-03-10 22:19:53 +010011216 if (*keys != NUL || execute)
11217 {
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011218 /* Need to escape K_SPECIAL and CSI before putting the string in the
11219 * typeahead buffer. */
11220 keys_esc = vim_strsave_escape_csi(keys);
11221 if (keys_esc != NULL)
11222 {
11223 ins_typebuf(keys_esc, (remap ? REMAP_YES : REMAP_NONE),
Bram Moolenaar0a988df2015-01-27 15:19:24 +010011224 insert ? 0 : typebuf.tb_len, !typed, FALSE);
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011225 vim_free(keys_esc);
Bram Moolenaar437df8f2006-04-27 21:47:44 +000011226 if (vgetc_busy)
11227 typebuf_was_filled = TRUE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011228 if (execute)
Bram Moolenaar9e496852016-03-11 19:31:47 +010011229 {
11230 int save_msg_scroll = msg_scroll;
11231
11232 /* Avoid a 1 second delay when the keys start Insert mode. */
11233 msg_scroll = FALSE;
Bram Moolenaar5f8a14b2016-01-21 23:34:58 +010011234 exec_normal(TRUE);
Bram Moolenaar9e496852016-03-11 19:31:47 +010011235 msg_scroll |= save_msg_scroll;
11236 }
Bram Moolenaarf193fff2006-04-27 00:02:13 +000011237 }
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000011238 }
11239}
11240
11241/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011242 * "filereadable()" function
11243 */
11244 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011245f_filereadable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011246{
Bram Moolenaarc236c162008-07-13 17:41:49 +000011247 int fd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011248 char_u *p;
11249 int n;
11250
Bram Moolenaarc236c162008-07-13 17:41:49 +000011251#ifndef O_NONBLOCK
11252# define O_NONBLOCK 0
11253#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011254 p = get_tv_string(&argvars[0]);
Bram Moolenaarc236c162008-07-13 17:41:49 +000011255 if (*p && !mch_isdir(p) && (fd = mch_open((char *)p,
11256 O_RDONLY | O_NONBLOCK, 0)) >= 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011257 {
11258 n = TRUE;
Bram Moolenaarc236c162008-07-13 17:41:49 +000011259 close(fd);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011260 }
11261 else
11262 n = FALSE;
11263
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011264 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011265}
11266
11267/*
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011268 * Return 0 for not writable, 1 for writable file, 2 for a dir which we have
Bram Moolenaar071d4272004-06-13 20:20:40 +000011269 * rights to write into.
11270 */
11271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011272f_filewritable(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011273{
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000011274 rettv->vval.v_number = filewritable(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011275}
11276
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011277 static void
Bram Moolenaard14e00e2016-01-31 17:30:51 +010011278findfilendir(
11279 typval_T *argvars UNUSED,
11280 typval_T *rettv,
11281 int find_what UNUSED)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011282{
11283#ifdef FEAT_SEARCHPATH
11284 char_u *fname;
11285 char_u *fresult = NULL;
11286 char_u *path = *curbuf->b_p_path == NUL ? p_path : curbuf->b_p_path;
11287 char_u *p;
11288 char_u pathbuf[NUMBUFLEN];
11289 int count = 1;
11290 int first = TRUE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011291 int error = FALSE;
11292#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011293
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011294 rettv->vval.v_string = NULL;
11295 rettv->v_type = VAR_STRING;
11296
11297#ifdef FEAT_SEARCHPATH
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011298 fname = get_tv_string(&argvars[0]);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011299
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011300 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011301 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011302 p = get_tv_string_buf_chk(&argvars[1], pathbuf);
11303 if (p == NULL)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011304 error = TRUE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011305 else
11306 {
11307 if (*p != NUL)
11308 path = p;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011309
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011310 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011311 count = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011312 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011313 }
11314
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011315 if (count < 0 && rettv_list_alloc(rettv) == FAIL)
11316 error = TRUE;
11317
11318 if (*fname != NUL && !error)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011319 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011320 do
11321 {
Bram Moolenaardf2bc272013-06-24 22:17:32 +020011322 if (rettv->v_type == VAR_STRING || rettv->v_type == VAR_LIST)
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011323 vim_free(fresult);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011324 fresult = find_file_in_path_option(first ? fname : NULL,
11325 first ? (int)STRLEN(fname) : 0,
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011326 0, first, path,
11327 find_what,
11328 curbuf->b_ffname,
11329 find_what == FINDFILE_DIR
11330 ? (char_u *)"" : curbuf->b_p_sua);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011331 first = FALSE;
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011332
11333 if (fresult != NULL && rettv->v_type == VAR_LIST)
11334 list_append_string(rettv->vval.v_list, fresult, -1);
11335
11336 } while ((rettv->v_type == VAR_LIST || --count > 0) && fresult != NULL);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011337 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011338
Bram Moolenaar899dddf2006-03-26 21:06:50 +000011339 if (rettv->v_type == VAR_STRING)
11340 rettv->vval.v_string = fresult;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011341#endif
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011342}
11343
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011344static void filter_map(typval_T *argvars, typval_T *rettv, int map);
11345static int filter_map_one(typval_T *tv, char_u *expr, int map, int *remp);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011346
11347/*
11348 * Implementation of map() and filter().
11349 */
11350 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011351filter_map(typval_T *argvars, typval_T *rettv, int map)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011352{
11353 char_u buf[NUMBUFLEN];
Bram Moolenaare9a41262005-01-15 22:18:47 +000011354 char_u *expr;
Bram Moolenaar33570922005-01-25 22:26:29 +000011355 listitem_T *li, *nli;
11356 list_T *l = NULL;
11357 dictitem_T *di;
11358 hashtab_T *ht;
11359 hashitem_T *hi;
11360 dict_T *d = NULL;
11361 typval_T save_val;
11362 typval_T save_key;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011363 int rem;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011364 int todo;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011365 char_u *ermsg = (char_u *)(map ? "map()" : "filter()");
Bram Moolenaar77354e72015-04-21 16:49:05 +020011366 char_u *arg_errmsg = (char_u *)(map ? N_("map() argument")
Bram Moolenaar32f649e2011-04-11 13:46:13 +020011367 : N_("filter() argument"));
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011368 int save_did_emsg;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011369 int idx = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011370
Bram Moolenaare9a41262005-01-15 22:18:47 +000011371 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011372 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011373 if ((l = argvars[0].vval.v_list) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011374 || (!map && tv_check_lock(l->lv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011375 return;
11376 }
11377 else if (argvars[0].v_type == VAR_DICT)
11378 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011379 if ((d = argvars[0].vval.v_dict) == NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020011380 || (!map && tv_check_lock(d->dv_lock, arg_errmsg, TRUE)))
Bram Moolenaare9a41262005-01-15 22:18:47 +000011381 return;
11382 }
11383 else
11384 {
Bram Moolenaar89d40322006-08-29 15:30:07 +000011385 EMSG2(_(e_listdictarg), ermsg);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011386 return;
11387 }
11388
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011389 expr = get_tv_string_buf_chk(&argvars[1], buf);
11390 /* On type errors, the preceding call has already displayed an error
11391 * message. Avoid a misleading error message for an empty string that
11392 * was not passed as argument. */
11393 if (expr != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011394 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011395 prepare_vimvar(VV_VAL, &save_val);
11396 expr = skipwhite(expr);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011397
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011398 /* We reset "did_emsg" to be able to detect whether an error
11399 * occurred during evaluation of the expression. */
11400 save_did_emsg = did_emsg;
11401 did_emsg = FALSE;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011402
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011403 prepare_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011404 if (argvars[0].v_type == VAR_DICT)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011405 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011406 vimvars[VV_KEY].vv_type = VAR_STRING;
11407
11408 ht = &d->dv_hashtab;
11409 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000011410 todo = (int)ht->ht_used;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011411 for (hi = ht->ht_array; todo > 0; ++hi)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011412 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011413 if (!HASHITEM_EMPTY(hi))
11414 {
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011415 int r;
11416
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011417 --todo;
11418 di = HI2DI(hi);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011419 if (map &&
Bram Moolenaar77354e72015-04-21 16:49:05 +020011420 (tv_check_lock(di->di_tv.v_lock, arg_errmsg, TRUE)
11421 || var_check_ro(di->di_flags, arg_errmsg, TRUE)))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011422 break;
11423 vimvars[VV_KEY].vv_str = vim_strsave(di->di_key);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011424 r = filter_map_one(&di->di_tv, expr, map, &rem);
11425 clear_tv(&vimvars[VV_KEY].vv_tv);
11426 if (r == FAIL || did_emsg)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011427 break;
11428 if (!map && rem)
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011429 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011430 if (var_check_fixed(di->di_flags, arg_errmsg, TRUE)
11431 || var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011432 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011433 dictitem_remove(d, di);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020011434 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011435 }
11436 }
11437 hash_unlock(ht);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011438 }
11439 else
11440 {
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011441 vimvars[VV_KEY].vv_type = VAR_NUMBER;
11442
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011443 for (li = l->lv_first; li != NULL; li = nli)
11444 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020011445 if (map && tv_check_lock(li->li_tv.v_lock, arg_errmsg, TRUE))
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000011446 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011447 nli = li->li_next;
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011448 vimvars[VV_KEY].vv_nr = idx;
Bram Moolenaar280f1262006-01-30 00:14:18 +000011449 if (filter_map_one(&li->li_tv, expr, map, &rem) == FAIL
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011450 || did_emsg)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011451 break;
11452 if (!map && rem)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011453 listitem_remove(l, li);
Bram Moolenaarf506c5b2010-06-22 06:28:58 +020011454 ++idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011455 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011456 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000011457
Bram Moolenaar627b1d32009-11-17 11:20:35 +000011458 restore_vimvar(VV_KEY, &save_key);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011459 restore_vimvar(VV_VAL, &save_val);
Bram Moolenaar280f1262006-01-30 00:14:18 +000011460
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000011461 did_emsg |= save_did_emsg;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011462 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000011463
11464 copy_tv(&argvars[0], rettv);
11465}
11466
11467 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010011468filter_map_one(typval_T *tv, char_u *expr, int map, int *remp)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011469{
Bram Moolenaar33570922005-01-25 22:26:29 +000011470 typval_T rettv;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011471 char_u *s;
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011472 int retval = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011473
Bram Moolenaar33570922005-01-25 22:26:29 +000011474 copy_tv(tv, &vimvars[VV_VAL].vv_tv);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011475 s = expr;
11476 if (eval1(&s, &rettv, TRUE) == FAIL)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011477 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011478 if (*s != NUL) /* check for trailing chars after expr */
11479 {
11480 EMSG2(_(e_invexpr2), s);
Bram Moolenaarb738c9a2014-11-19 20:04:48 +010011481 clear_tv(&rettv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011482 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011483 }
11484 if (map)
11485 {
11486 /* map(): replace the list item value */
11487 clear_tv(tv);
Bram Moolenaar4463f292005-09-25 22:20:24 +000011488 rettv.v_lock = 0;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011489 *tv = rettv;
11490 }
11491 else
11492 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011493 int error = FALSE;
11494
Bram Moolenaare9a41262005-01-15 22:18:47 +000011495 /* filter(): when expr is zero remove the item */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011496 *remp = (get_tv_number_chk(&rettv, &error) == 0);
Bram Moolenaare9a41262005-01-15 22:18:47 +000011497 clear_tv(&rettv);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011498 /* On type error, nothing has been removed; return FAIL to stop the
11499 * loop. The error message was given by get_tv_number_chk(). */
11500 if (error)
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011501 goto theend;
Bram Moolenaare9a41262005-01-15 22:18:47 +000011502 }
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011503 retval = OK;
11504theend:
Bram Moolenaar33570922005-01-25 22:26:29 +000011505 clear_tv(&vimvars[VV_VAL].vv_tv);
Bram Moolenaarb4066a12007-09-17 19:38:08 +000011506 return retval;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011507}
11508
11509/*
11510 * "filter()" function
11511 */
11512 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011513f_filter(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000011514{
11515 filter_map(argvars, rettv, FALSE);
11516}
11517
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000011518/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011519 * "finddir({fname}[, {path}[, {count}]])" function
11520 */
11521 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011522f_finddir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011523{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011524 findfilendir(argvars, rettv, FINDFILE_DIR);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011525}
11526
11527/*
11528 * "findfile({fname}[, {path}[, {count}]])" function
11529 */
11530 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011531f_findfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011532{
Bram Moolenaar4d0ec162008-02-20 11:24:52 +000011533 findfilendir(argvars, rettv, FINDFILE_FILE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000011534}
11535
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011536#ifdef FEAT_FLOAT
11537/*
11538 * "float2nr({float})" function
11539 */
11540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011541f_float2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011542{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011543 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011544
11545 if (get_float_arg(argvars, &f) == OK)
11546 {
11547 if (f < -0x7fffffff)
11548 rettv->vval.v_number = -0x7fffffff;
11549 else if (f > 0x7fffffff)
11550 rettv->vval.v_number = 0x7fffffff;
11551 else
11552 rettv->vval.v_number = (varnumber_T)f;
11553 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011554}
11555
11556/*
11557 * "floor({float})" function
11558 */
11559 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011560f_floor(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011561{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011562 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011563
11564 rettv->v_type = VAR_FLOAT;
11565 if (get_float_arg(argvars, &f) == OK)
11566 rettv->vval.v_float = floor(f);
11567 else
11568 rettv->vval.v_float = 0.0;
11569}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011570
11571/*
11572 * "fmod()" function
11573 */
11574 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011575f_fmod(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011576{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010011577 float_T fx = 0.0, fy = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020011578
11579 rettv->v_type = VAR_FLOAT;
11580 if (get_float_arg(argvars, &fx) == OK
11581 && get_float_arg(&argvars[1], &fy) == OK)
11582 rettv->vval.v_float = fmod(fx, fy);
11583 else
11584 rettv->vval.v_float = 0.0;
11585}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000011586#endif
11587
Bram Moolenaar0d660222005-01-07 21:51:51 +000011588/*
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011589 * "fnameescape({string})" function
11590 */
11591 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011592f_fnameescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraebaf892008-05-28 14:49:58 +000011593{
11594 rettv->vval.v_string = vim_strsave_fnameescape(
11595 get_tv_string(&argvars[0]), FALSE);
11596 rettv->v_type = VAR_STRING;
11597}
11598
11599/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011600 * "fnamemodify({fname}, {mods})" function
11601 */
11602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011603f_fnamemodify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011604{
11605 char_u *fname;
11606 char_u *mods;
11607 int usedlen = 0;
11608 int len;
11609 char_u *fbuf = NULL;
11610 char_u buf[NUMBUFLEN];
11611
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011612 fname = get_tv_string_chk(&argvars[0]);
11613 mods = get_tv_string_buf_chk(&argvars[1], buf);
11614 if (fname == NULL || mods == NULL)
11615 fname = NULL;
11616 else
11617 {
11618 len = (int)STRLEN(fname);
11619 (void)modify_fname(mods, &usedlen, &fname, &fbuf, &len);
11620 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011621
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011622 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011623 if (fname == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011624 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011625 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011626 rettv->vval.v_string = vim_strnsave(fname, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011627 vim_free(fbuf);
11628}
11629
Bram Moolenaar48e697e2016-01-23 22:17:30 +010011630static void foldclosed_both(typval_T *argvars, typval_T *rettv, int end);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011631
11632/*
11633 * "foldclosed()" function
11634 */
11635 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011636foldclosed_both(
11637 typval_T *argvars UNUSED,
11638 typval_T *rettv,
11639 int end UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011640{
11641#ifdef FEAT_FOLDING
11642 linenr_T lnum;
11643 linenr_T first, last;
11644
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011645 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011646 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
11647 {
11648 if (hasFoldingWin(curwin, lnum, &first, &last, FALSE, NULL))
11649 {
11650 if (end)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011651 rettv->vval.v_number = (varnumber_T)last;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011652 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011653 rettv->vval.v_number = (varnumber_T)first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011654 return;
11655 }
11656 }
11657#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011658 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011659}
11660
11661/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000011662 * "foldclosed()" function
11663 */
11664 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011665f_foldclosed(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011666{
11667 foldclosed_both(argvars, rettv, FALSE);
11668}
11669
11670/*
11671 * "foldclosedend()" function
11672 */
11673 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011674f_foldclosedend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000011675{
11676 foldclosed_both(argvars, rettv, TRUE);
11677}
11678
11679/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011680 * "foldlevel()" function
11681 */
11682 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011683f_foldlevel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011684{
11685#ifdef FEAT_FOLDING
11686 linenr_T lnum;
11687
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011688 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011689 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011690 rettv->vval.v_number = foldLevel(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000011691#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000011692}
11693
11694/*
11695 * "foldtext()" function
11696 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011697 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011698f_foldtext(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011699{
11700#ifdef FEAT_FOLDING
11701 linenr_T lnum;
11702 char_u *s;
11703 char_u *r;
11704 int len;
11705 char *txt;
11706#endif
11707
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011708 rettv->v_type = VAR_STRING;
11709 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011710#ifdef FEAT_FOLDING
Bram Moolenaare9a41262005-01-15 22:18:47 +000011711 if ((linenr_T)vimvars[VV_FOLDSTART].vv_nr > 0
11712 && (linenr_T)vimvars[VV_FOLDEND].vv_nr
11713 <= curbuf->b_ml.ml_line_count
11714 && vimvars[VV_FOLDDASHES].vv_str != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011715 {
11716 /* Find first non-empty line in the fold. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000011717 lnum = (linenr_T)vimvars[VV_FOLDSTART].vv_nr;
11718 while (lnum < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011719 {
11720 if (!linewhite(lnum))
11721 break;
11722 ++lnum;
11723 }
11724
11725 /* Find interesting text in this line. */
11726 s = skipwhite(ml_get(lnum));
11727 /* skip C comment-start */
11728 if (s[0] == '/' && (s[1] == '*' || s[1] == '/'))
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011729 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000011730 s = skipwhite(s + 2);
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011731 if (*skipwhite(s) == NUL
Bram Moolenaare9a41262005-01-15 22:18:47 +000011732 && lnum + 1 < (linenr_T)vimvars[VV_FOLDEND].vv_nr)
Bram Moolenaar293ee4d2004-12-09 21:34:53 +000011733 {
11734 s = skipwhite(ml_get(lnum + 1));
11735 if (*s == '*')
11736 s = skipwhite(s + 1);
11737 }
11738 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000011739 txt = _("+-%s%3ld lines: ");
11740 r = alloc((unsigned)(STRLEN(txt)
Bram Moolenaare9a41262005-01-15 22:18:47 +000011741 + STRLEN(vimvars[VV_FOLDDASHES].vv_str) /* for %s */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011742 + 20 /* for %3ld */
11743 + STRLEN(s))); /* concatenated */
11744 if (r != NULL)
11745 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000011746 sprintf((char *)r, txt, vimvars[VV_FOLDDASHES].vv_str,
11747 (long)((linenr_T)vimvars[VV_FOLDEND].vv_nr
11748 - (linenr_T)vimvars[VV_FOLDSTART].vv_nr + 1));
Bram Moolenaar071d4272004-06-13 20:20:40 +000011749 len = (int)STRLEN(r);
11750 STRCAT(r, s);
11751 /* remove 'foldmarker' and 'commentstring' */
11752 foldtext_cleanup(r + len);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011753 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000011754 }
11755 }
11756#endif
11757}
11758
11759/*
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011760 * "foldtextresult(lnum)" function
11761 */
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011762 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011763f_foldtextresult(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011764{
11765#ifdef FEAT_FOLDING
11766 linenr_T lnum;
11767 char_u *text;
11768 char_u buf[51];
11769 foldinfo_T foldinfo;
11770 int fold_count;
11771#endif
11772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011773 rettv->v_type = VAR_STRING;
11774 rettv->vval.v_string = NULL;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011775#ifdef FEAT_FOLDING
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011776 lnum = get_tv_lnum(argvars);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000011777 /* treat illegal types and illegal string values for {lnum} the same */
11778 if (lnum < 0)
11779 lnum = 0;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011780 fold_count = foldedCount(curwin, lnum, &foldinfo);
11781 if (fold_count > 0)
11782 {
11783 text = get_foldtext(curwin, lnum, lnum + fold_count - 1,
11784 &foldinfo, buf);
11785 if (text == buf)
11786 text = vim_strsave(text);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000011787 rettv->vval.v_string = text;
Bram Moolenaar7b0294c2004-10-11 10:16:09 +000011788 }
11789#endif
11790}
11791
11792/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000011793 * "foreground()" function
11794 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000011795 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011796f_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000011797{
Bram Moolenaar071d4272004-06-13 20:20:40 +000011798#ifdef FEAT_GUI
11799 if (gui.in_use)
11800 gui_mch_set_foreground();
11801#else
11802# ifdef WIN32
11803 win32_set_foreground();
11804# endif
11805#endif
11806}
11807
11808/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011809 * "function()" function
11810 */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011811 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010011812f_function(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011813{
11814 char_u *s;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011815 char_u *name;
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011816 int use_string = FALSE;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011817 partial_T *arg_pt = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011818
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011819 if (argvars[0].v_type == VAR_FUNC)
11820 {
11821 /* function(MyFunc, [arg], dict) */
11822 s = argvars[0].vval.v_string;
11823 }
11824 else if (argvars[0].v_type == VAR_PARTIAL
11825 && argvars[0].vval.v_partial != NULL)
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011826 {
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011827 /* function(dict.MyFunc, [arg]) */
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011828 arg_pt = argvars[0].vval.v_partial;
11829 s = arg_pt->pt_name;
11830 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011831 else
11832 {
11833 /* function('MyFunc', [arg], dict) */
11834 s = get_tv_string(&argvars[0]);
11835 use_string = TRUE;
11836 }
11837
11838 if (s == NULL || *s == NUL || (use_string && VIM_ISDIGIT(*s)))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011839 EMSG2(_(e_invarg2), s);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011840 /* Don't check an autoload name for existence here. */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010011841 else if (use_string && vim_strchr(s, AUTOLOAD_CHAR) == NULL
11842 && !function_exists(s))
Bram Moolenaare49b69a2005-01-08 16:11:57 +000011843 EMSG2(_("E700: Unknown function: %s"), s);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011844 else
11845 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011846 int dict_idx = 0;
11847 int arg_idx = 0;
11848 list_T *list = NULL;
11849
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011850 if (STRNCMP(s, "s:", 2) == 0 || STRNCMP(s, "<SID>", 5) == 0)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011851 {
11852 char sid_buf[25];
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011853 int off = *s == 's' ? 2 : 5;
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011854
Bram Moolenaar0c6633a2013-06-13 21:24:06 +020011855 /* Expand s: and <SID> into <SNR>nr_, so that the function can
11856 * also be called from another script. Using trans_function_name()
11857 * would also work, but some plugins depend on the name being
11858 * printable text. */
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011859 sprintf(sid_buf, "<SNR>%ld_", (long)current_SID);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011860 name = alloc((int)(STRLEN(sid_buf) + STRLEN(s + off) + 1));
11861 if (name != NULL)
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011862 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011863 STRCPY(name, sid_buf);
11864 STRCAT(name, s + off);
Bram Moolenaar60bf1f52013-06-12 13:37:43 +020011865 }
11866 }
Bram Moolenaara1544c02013-05-30 12:35:52 +020011867 else
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011868 name = vim_strsave(s);
11869
11870 if (argvars[1].v_type != VAR_UNKNOWN)
11871 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011872 if (argvars[2].v_type != VAR_UNKNOWN)
11873 {
11874 /* function(name, [args], dict) */
11875 arg_idx = 1;
11876 dict_idx = 2;
11877 }
11878 else if (argvars[1].v_type == VAR_DICT)
11879 /* function(name, dict) */
11880 dict_idx = 1;
11881 else
11882 /* function(name, [args]) */
11883 arg_idx = 1;
Bram Moolenaar346418c2016-03-15 12:36:08 +010011884 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011885 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011886 if (argvars[dict_idx].v_type != VAR_DICT)
11887 {
11888 EMSG(_("E922: expected a dict"));
11889 vim_free(name);
11890 return;
11891 }
11892 if (argvars[dict_idx].vval.v_dict == NULL)
11893 dict_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011894 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011895 if (arg_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011896 {
Bram Moolenaar346418c2016-03-15 12:36:08 +010011897 if (argvars[arg_idx].v_type != VAR_LIST)
11898 {
11899 EMSG(_("E923: Second argument of function() must be a list or a dict"));
11900 vim_free(name);
11901 return;
11902 }
11903 list = argvars[arg_idx].vval.v_list;
11904 if (list == NULL || list->lv_len == 0)
11905 arg_idx = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011906 }
Bram Moolenaar346418c2016-03-15 12:36:08 +010011907 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011908 if (dict_idx > 0 || arg_idx > 0 || arg_pt != NULL)
Bram Moolenaar346418c2016-03-15 12:36:08 +010011909 {
11910 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011911
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011912 /* result is a VAR_PARTIAL */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011913 if (pt != NULL)
11914 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011915 if (arg_idx > 0 || (arg_pt != NULL && arg_pt->pt_argc > 0))
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011916 {
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011917 listitem_T *li;
11918 int i = 0;
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011919 int arg_len = 0;
11920 int lv_len = 0;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011921
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011922 if (arg_pt != NULL)
11923 arg_len = arg_pt->pt_argc;
11924 if (list != NULL)
11925 lv_len = list->lv_len;
11926 pt->pt_argc = arg_len + lv_len;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011927 pt->pt_argv = (typval_T *)alloc(
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011928 sizeof(typval_T) * pt->pt_argc);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011929 if (pt->pt_argv == NULL)
11930 {
11931 vim_free(pt);
11932 vim_free(name);
11933 return;
11934 }
11935 else
11936 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011937 for (i = 0; i < arg_len; i++)
11938 copy_tv(&arg_pt->pt_argv[i], &pt->pt_argv[i]);
11939 if (lv_len > 0)
11940 for (li = list->lv_first; li != NULL;
11941 li = li->li_next)
11942 copy_tv(&li->li_tv, &pt->pt_argv[i++]);
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011943 }
11944 }
11945
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011946 /* For "function(dict.func, [], dict)" and "func" is a partial
11947 * use "dict". That is backwards compatible. */
11948 if (dict_idx > 0)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011949 {
11950 pt->pt_dict = argvars[dict_idx].vval.v_dict;
11951 ++pt->pt_dict->dv_refcount;
11952 }
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011953 else if (arg_pt != NULL)
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011954 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011955 pt->pt_dict = arg_pt->pt_dict;
11956 if (pt->pt_dict != NULL)
11957 ++pt->pt_dict->dv_refcount;
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010011958 }
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011959
11960 pt->pt_refcount = 1;
11961 pt->pt_name = name;
11962 func_ref(pt->pt_name);
11963 }
11964 rettv->v_type = VAR_PARTIAL;
11965 rettv->vval.v_partial = pt;
11966 }
11967 else
11968 {
Bram Moolenaar8a1bb042016-03-17 21:11:53 +010011969 /* result is a VAR_FUNC */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011970 rettv->v_type = VAR_FUNC;
11971 rettv->vval.v_string = name;
11972 func_ref(name);
11973 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000011974 }
11975}
11976
Bram Moolenaar1735bc92016-03-14 23:05:14 +010011977 static void
11978partial_free(partial_T *pt)
11979{
11980 int i;
11981
11982 for (i = 0; i < pt->pt_argc; ++i)
11983 clear_tv(&pt->pt_argv[i]);
11984 vim_free(pt->pt_argv);
11985 func_unref(pt->pt_name);
11986 vim_free(pt->pt_name);
11987 vim_free(pt);
11988}
11989
11990/*
11991 * Unreference a closure: decrement the reference count and free it when it
11992 * becomes zero.
11993 */
11994 void
11995partial_unref(partial_T *pt)
11996{
11997 if (pt != NULL && --pt->pt_refcount <= 0)
11998 partial_free(pt);
11999}
12000
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012001/*
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012002 * "garbagecollect()" function
12003 */
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012004 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012005f_garbagecollect(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012006{
Bram Moolenaar9fecb462006-09-05 10:59:47 +000012007 /* This is postponed until we are back at the toplevel, because we may be
12008 * using Lists and Dicts internally. E.g.: ":echo [garbagecollect()]". */
12009 want_garbage_collect = TRUE;
Bram Moolenaar9d2c8c12007-09-25 16:00:00 +000012010
12011 if (argvars[0].v_type != VAR_UNKNOWN && get_tv_number(&argvars[0]) == 1)
12012 garbage_collect_at_exit = TRUE;
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000012013}
12014
12015/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000012016 * "get()" function
12017 */
12018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012019f_get(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012020{
Bram Moolenaar33570922005-01-25 22:26:29 +000012021 listitem_T *li;
12022 list_T *l;
12023 dictitem_T *di;
12024 dict_T *d;
12025 typval_T *tv = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012026
Bram Moolenaare9a41262005-01-15 22:18:47 +000012027 if (argvars[0].v_type == VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012028 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000012029 if ((l = argvars[0].vval.v_list) != NULL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012030 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012031 int error = FALSE;
12032
12033 li = list_find(l, get_tv_number_chk(&argvars[1], &error));
12034 if (!error && li != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012035 tv = &li->li_tv;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012036 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012037 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000012038 else if (argvars[0].v_type == VAR_DICT)
12039 {
12040 if ((d = argvars[0].vval.v_dict) != NULL)
12041 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000012042 di = dict_find(d, get_tv_string(&argvars[1]), -1);
Bram Moolenaare9a41262005-01-15 22:18:47 +000012043 if (di != NULL)
12044 tv = &di->di_tv;
12045 }
12046 }
12047 else
12048 EMSG2(_(e_listdictarg), "get()");
12049
12050 if (tv == NULL)
12051 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012052 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaare9a41262005-01-15 22:18:47 +000012053 copy_tv(&argvars[2], rettv);
12054 }
12055 else
12056 copy_tv(tv, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012057}
12058
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012059static 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 +000012060
12061/*
12062 * Get line or list of lines from buffer "buf" into "rettv".
Bram Moolenaar342337a2005-07-21 21:11:17 +000012063 * Return a range (from start to end) of lines in rettv from the specified
12064 * buffer.
12065 * If 'retlist' is TRUE, then the lines are returned as a Vim List.
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012066 */
12067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012068get_buffer_lines(
12069 buf_T *buf,
12070 linenr_T start,
12071 linenr_T end,
12072 int retlist,
12073 typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012074{
12075 char_u *p;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012076
Bram Moolenaar959a1432013-12-14 12:17:38 +010012077 rettv->v_type = VAR_STRING;
12078 rettv->vval.v_string = NULL;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000012079 if (retlist && rettv_list_alloc(rettv) == FAIL)
12080 return;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012081
12082 if (buf == NULL || buf->b_ml.ml_mfp == NULL || start < 0)
12083 return;
12084
12085 if (!retlist)
12086 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012087 if (start >= 1 && start <= buf->b_ml.ml_line_count)
12088 p = ml_get_buf(buf, start, FALSE);
12089 else
12090 p = (char_u *)"";
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012091 rettv->vval.v_string = vim_strsave(p);
12092 }
12093 else
12094 {
12095 if (end < start)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012096 return;
12097
12098 if (start < 1)
12099 start = 1;
12100 if (end > buf->b_ml.ml_line_count)
12101 end = buf->b_ml.ml_line_count;
12102 while (start <= end)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012103 if (list_append_string(rettv->vval.v_list,
12104 ml_get_buf(buf, start++, FALSE), -1) == FAIL)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012105 break;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012106 }
12107}
12108
12109/*
12110 * "getbufline()" function
12111 */
12112 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012113f_getbufline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012114{
12115 linenr_T lnum;
12116 linenr_T end;
12117 buf_T *buf;
12118
12119 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12120 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012121 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012122 --emsg_off;
12123
Bram Moolenaar661b1822005-07-28 22:36:45 +000012124 lnum = get_tv_lnum_buf(&argvars[1], buf);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012125 if (argvars[2].v_type == VAR_UNKNOWN)
12126 end = lnum;
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012127 else
Bram Moolenaar661b1822005-07-28 22:36:45 +000012128 end = get_tv_lnum_buf(&argvars[2], buf);
12129
Bram Moolenaar342337a2005-07-21 21:11:17 +000012130 get_buffer_lines(buf, lnum, end, TRUE, rettv);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012131}
12132
Bram Moolenaar0d660222005-01-07 21:51:51 +000012133/*
12134 * "getbufvar()" function
12135 */
12136 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012137f_getbufvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012138{
12139 buf_T *buf;
12140 buf_T *save_curbuf;
12141 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000012142 dictitem_T *v;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012143 int done = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012144
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012145 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
12146 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012147 ++emsg_off;
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010012148 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012149
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012150 rettv->v_type = VAR_STRING;
12151 rettv->vval.v_string = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012152
12153 if (buf != NULL && varname != NULL)
12154 {
Bram Moolenaar632deed2008-06-27 18:26:11 +000012155 /* set curbuf to be our buf, temporarily */
12156 save_curbuf = curbuf;
12157 curbuf = buf;
12158
Bram Moolenaar0d660222005-01-07 21:51:51 +000012159 if (*varname == '&') /* buffer-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012160 {
12161 if (get_option_tv(&varname, rettv, TRUE) == OK)
12162 done = TRUE;
12163 }
Bram Moolenaar445edda2011-01-22 01:13:39 +010012164 else if (STRCMP(varname, "changedtick") == 0)
12165 {
12166 rettv->v_type = VAR_NUMBER;
12167 rettv->vval.v_number = curbuf->b_changedtick;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012168 done = TRUE;
Bram Moolenaar445edda2011-01-22 01:13:39 +010012169 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012170 else
12171 {
Bram Moolenaar332ac062013-04-15 13:06:21 +020012172 /* Look up the variable. */
12173 /* Let getbufvar({nr}, "") return the "b:" dictionary. */
12174 v = find_var_in_ht(&curbuf->b_vars->dv_hashtab,
12175 'b', varname, FALSE);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012176 if (v != NULL)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012177 {
Bram Moolenaar33570922005-01-25 22:26:29 +000012178 copy_tv(&v->di_tv, rettv);
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012179 done = TRUE;
12180 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012181 }
Bram Moolenaar632deed2008-06-27 18:26:11 +000012182
12183 /* restore previous notion of curbuf */
12184 curbuf = save_curbuf;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012185 }
12186
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012187 if (!done && argvars[2].v_type != VAR_UNKNOWN)
12188 /* use the default value */
12189 copy_tv(&argvars[2], rettv);
12190
Bram Moolenaar0d660222005-01-07 21:51:51 +000012191 --emsg_off;
12192}
12193
12194/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012195 * "getchar()" function
12196 */
12197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012198f_getchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012199{
12200 varnumber_T n;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012201 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012202
Bram Moolenaar4015b2c2006-06-22 19:01:34 +000012203 /* Position the cursor. Needed after a message that ends in a space. */
12204 windgoto(msg_row, msg_col);
12205
Bram Moolenaar071d4272004-06-13 20:20:40 +000012206 ++no_mapping;
12207 ++allow_keys;
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012208 for (;;)
12209 {
12210 if (argvars[0].v_type == VAR_UNKNOWN)
12211 /* getchar(): blocking wait. */
12212 n = safe_vgetc();
12213 else if (get_tv_number_chk(&argvars[0], &error) == 1)
12214 /* getchar(1): only check if char avail */
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012215 n = vpeekc_any();
12216 else if (error || vpeekc_any() == NUL)
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012217 /* illegal argument or getchar(0) and no char avail: return zero */
12218 n = 0;
12219 else
12220 /* getchar(0) and char avail: return char */
12221 n = safe_vgetc();
Bram Moolenaar9a665ba2014-05-22 18:59:58 +020012222
Bram Moolenaar9c8791f2007-09-05 19:47:23 +000012223 if (n == K_IGNORE)
12224 continue;
12225 break;
12226 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012227 --no_mapping;
12228 --allow_keys;
12229
Bram Moolenaar219b8702006-11-01 14:32:36 +000012230 vimvars[VV_MOUSE_WIN].vv_nr = 0;
12231 vimvars[VV_MOUSE_LNUM].vv_nr = 0;
12232 vimvars[VV_MOUSE_COL].vv_nr = 0;
12233
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012234 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012235 if (IS_SPECIAL(n) || mod_mask != 0)
12236 {
12237 char_u temp[10]; /* modifier: 3, mbyte-char: 6, NUL: 1 */
12238 int i = 0;
12239
12240 /* Turn a special key into three bytes, plus modifier. */
12241 if (mod_mask != 0)
12242 {
12243 temp[i++] = K_SPECIAL;
12244 temp[i++] = KS_MODIFIER;
12245 temp[i++] = mod_mask;
12246 }
12247 if (IS_SPECIAL(n))
12248 {
12249 temp[i++] = K_SPECIAL;
12250 temp[i++] = K_SECOND(n);
12251 temp[i++] = K_THIRD(n);
12252 }
12253#ifdef FEAT_MBYTE
12254 else if (has_mbyte)
12255 i += (*mb_char2bytes)(n, temp + i);
12256#endif
12257 else
12258 temp[i++] = n;
12259 temp[i++] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012260 rettv->v_type = VAR_STRING;
12261 rettv->vval.v_string = vim_strsave(temp);
Bram Moolenaar219b8702006-11-01 14:32:36 +000012262
12263#ifdef FEAT_MOUSE
Bram Moolenaar2526ef22013-03-16 14:20:51 +010012264 if (is_mouse_key(n))
Bram Moolenaar219b8702006-11-01 14:32:36 +000012265 {
12266 int row = mouse_row;
12267 int col = mouse_col;
12268 win_T *win;
12269 linenr_T lnum;
12270# ifdef FEAT_WINDOWS
12271 win_T *wp;
12272# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012273 int winnr = 1;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012274
12275 if (row >= 0 && col >= 0)
12276 {
12277 /* Find the window at the mouse coordinates and compute the
12278 * text position. */
12279 win = mouse_find_win(&row, &col);
12280 (void)mouse_comp_pos(win, &row, &col, &lnum);
12281# ifdef FEAT_WINDOWS
12282 for (wp = firstwin; wp != win; wp = wp->w_next)
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012283 ++winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012284# endif
Bram Moolenaarfe86f2d2008-11-28 20:29:07 +000012285 vimvars[VV_MOUSE_WIN].vv_nr = winnr;
Bram Moolenaar219b8702006-11-01 14:32:36 +000012286 vimvars[VV_MOUSE_LNUM].vv_nr = lnum;
12287 vimvars[VV_MOUSE_COL].vv_nr = col + 1;
12288 }
12289 }
12290#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012291 }
12292}
12293
12294/*
12295 * "getcharmod()" function
12296 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012297 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012298f_getcharmod(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012299{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012300 rettv->vval.v_number = mod_mask;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012301}
12302
12303/*
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012304 * "getcharsearch()" function
12305 */
12306 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012307f_getcharsearch(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020012308{
12309 if (rettv_dict_alloc(rettv) != FAIL)
12310 {
12311 dict_T *dict = rettv->vval.v_dict;
12312
12313 dict_add_nr_str(dict, "char", 0L, last_csearch());
12314 dict_add_nr_str(dict, "forward", last_csearch_forward(), NULL);
12315 dict_add_nr_str(dict, "until", last_csearch_until(), NULL);
12316 }
12317}
12318
12319/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012320 * "getcmdline()" function
12321 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012322 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012323f_getcmdline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012324{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012325 rettv->v_type = VAR_STRING;
12326 rettv->vval.v_string = get_cmdline_str();
Bram Moolenaar071d4272004-06-13 20:20:40 +000012327}
12328
12329/*
12330 * "getcmdpos()" function
12331 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012332 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012333f_getcmdpos(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012334{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012335 rettv->vval.v_number = get_cmdline_pos() + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012336}
12337
12338/*
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012339 * "getcmdtype()" function
12340 */
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012341 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012342f_getcmdtype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000012343{
12344 rettv->v_type = VAR_STRING;
12345 rettv->vval.v_string = alloc(2);
12346 if (rettv->vval.v_string != NULL)
12347 {
12348 rettv->vval.v_string[0] = get_cmdline_type();
12349 rettv->vval.v_string[1] = NUL;
12350 }
12351}
12352
12353/*
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012354 * "getcmdwintype()" function
12355 */
12356 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012357f_getcmdwintype(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar8c1329c2014-08-06 13:36:59 +020012358{
12359 rettv->v_type = VAR_STRING;
12360 rettv->vval.v_string = NULL;
12361#ifdef FEAT_CMDWIN
12362 rettv->vval.v_string = alloc(2);
12363 if (rettv->vval.v_string != NULL)
12364 {
12365 rettv->vval.v_string[0] = cmdwin_type;
12366 rettv->vval.v_string[1] = NUL;
12367 }
12368#endif
12369}
12370
12371/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012372 * "getcwd()" function
12373 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012374 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012375f_getcwd(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012376{
Bram Moolenaarc9703302016-01-17 21:49:33 +010012377 win_T *wp = NULL;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012378 char_u *cwd;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012379
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012380 rettv->v_type = VAR_STRING;
Bram Moolenaard9462e32011-04-11 21:35:11 +020012381 rettv->vval.v_string = NULL;
Bram Moolenaarc9703302016-01-17 21:49:33 +010012382
12383 wp = find_tabwin(&argvars[0], &argvars[1]);
12384 if (wp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012385 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012386 if (wp->w_localdir != NULL)
12387 rettv->vval.v_string = vim_strsave(wp->w_localdir);
12388 else if(globaldir != NULL)
12389 rettv->vval.v_string = vim_strsave(globaldir);
12390 else
Bram Moolenaard9462e32011-04-11 21:35:11 +020012391 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010012392 cwd = alloc(MAXPATHL);
12393 if (cwd != NULL)
12394 {
12395 if (mch_dirname(cwd, MAXPATHL) != FAIL)
12396 rettv->vval.v_string = vim_strsave(cwd);
12397 vim_free(cwd);
12398 }
Bram Moolenaard9462e32011-04-11 21:35:11 +020012399 }
Bram Moolenaarc9703302016-01-17 21:49:33 +010012400#ifdef BACKSLASH_IN_FILENAME
12401 if (rettv->vval.v_string != NULL)
12402 slash_adjust(rettv->vval.v_string);
12403#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000012404 }
12405}
12406
12407/*
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012408 * "getfontname()" function
12409 */
12410 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012411f_getfontname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012412{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012413 rettv->v_type = VAR_STRING;
12414 rettv->vval.v_string = NULL;
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012415#ifdef FEAT_GUI
12416 if (gui.in_use)
12417 {
12418 GuiFont font;
12419 char_u *name = NULL;
12420
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012421 if (argvars[0].v_type == VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012422 {
12423 /* Get the "Normal" font. Either the name saved by
12424 * hl_set_font_name() or from the font ID. */
12425 font = gui.norm_font;
12426 name = hl_get_font_name();
12427 }
12428 else
12429 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012430 name = get_tv_string(&argvars[0]);
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012431 if (STRCMP(name, "*") == 0) /* don't use font dialog */
12432 return;
12433 font = gui_mch_get_font(name, FALSE);
12434 if (font == NOFONT)
12435 return; /* Invalid font name, return empty string. */
12436 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012437 rettv->vval.v_string = gui_mch_get_fontname(font, name);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012438 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar46c9c732004-12-12 11:37:09 +000012439 gui_mch_free_font(font);
12440 }
12441#endif
12442}
12443
12444/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012445 * "getfperm({fname})" function
12446 */
12447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012448f_getfperm(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012449{
12450 char_u *fname;
12451 struct stat st;
12452 char_u *perm = NULL;
12453 char_u flags[] = "rwx";
12454 int i;
12455
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012456 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012457
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012458 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012459 if (mch_stat((char *)fname, &st) >= 0)
12460 {
12461 perm = vim_strsave((char_u *)"---------");
12462 if (perm != NULL)
12463 {
12464 for (i = 0; i < 9; i++)
12465 {
12466 if (st.st_mode & (1 << (8 - i)))
12467 perm[i] = flags[i % 3];
12468 }
12469 }
12470 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012471 rettv->vval.v_string = perm;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012472}
12473
12474/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012475 * "getfsize({fname})" function
12476 */
12477 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012478f_getfsize(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012479{
12480 char_u *fname;
12481 struct stat st;
12482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012483 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012484
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012485 rettv->v_type = VAR_NUMBER;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012486
12487 if (mch_stat((char *)fname, &st) >= 0)
12488 {
12489 if (mch_isdir(fname))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012490 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012491 else
Bram Moolenaard827ada2007-06-19 15:19:55 +000012492 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012493 rettv->vval.v_number = (varnumber_T)st.st_size;
Bram Moolenaard827ada2007-06-19 15:19:55 +000012494
12495 /* non-perfect check for overflow */
12496 if ((off_t)rettv->vval.v_number != (off_t)st.st_size)
12497 rettv->vval.v_number = -2;
12498 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012499 }
12500 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012501 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012502}
12503
12504/*
12505 * "getftime({fname})" function
12506 */
12507 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012508f_getftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012509{
12510 char_u *fname;
12511 struct stat st;
12512
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012513 fname = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012514
12515 if (mch_stat((char *)fname, &st) >= 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012516 rettv->vval.v_number = (varnumber_T)st.st_mtime;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012517 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012518 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012519}
12520
12521/*
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012522 * "getftype({fname})" function
12523 */
12524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012525f_getftype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012526{
12527 char_u *fname;
12528 struct stat st;
12529 char_u *type = NULL;
12530 char *t;
12531
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012532 fname = get_tv_string(&argvars[0]);
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012533
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012534 rettv->v_type = VAR_STRING;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012535 if (mch_lstat((char *)fname, &st) >= 0)
12536 {
12537#ifdef S_ISREG
12538 if (S_ISREG(st.st_mode))
12539 t = "file";
12540 else if (S_ISDIR(st.st_mode))
12541 t = "dir";
12542# ifdef S_ISLNK
12543 else if (S_ISLNK(st.st_mode))
12544 t = "link";
12545# endif
12546# ifdef S_ISBLK
12547 else if (S_ISBLK(st.st_mode))
12548 t = "bdev";
12549# endif
12550# ifdef S_ISCHR
12551 else if (S_ISCHR(st.st_mode))
12552 t = "cdev";
12553# endif
12554# ifdef S_ISFIFO
12555 else if (S_ISFIFO(st.st_mode))
12556 t = "fifo";
12557# endif
12558# ifdef S_ISSOCK
12559 else if (S_ISSOCK(st.st_mode))
12560 t = "fifo";
12561# endif
12562 else
12563 t = "other";
12564#else
12565# ifdef S_IFMT
12566 switch (st.st_mode & S_IFMT)
12567 {
12568 case S_IFREG: t = "file"; break;
12569 case S_IFDIR: t = "dir"; break;
12570# ifdef S_IFLNK
12571 case S_IFLNK: t = "link"; break;
12572# endif
12573# ifdef S_IFBLK
12574 case S_IFBLK: t = "bdev"; break;
12575# endif
12576# ifdef S_IFCHR
12577 case S_IFCHR: t = "cdev"; break;
12578# endif
12579# ifdef S_IFIFO
12580 case S_IFIFO: t = "fifo"; break;
12581# endif
12582# ifdef S_IFSOCK
12583 case S_IFSOCK: t = "socket"; break;
12584# endif
12585 default: t = "other";
12586 }
12587# else
12588 if (mch_isdir(fname))
12589 t = "dir";
12590 else
12591 t = "file";
12592# endif
12593#endif
12594 type = vim_strsave((char_u *)t);
12595 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012596 rettv->vval.v_string = type;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000012597}
12598
12599/*
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012600 * "getline(lnum, [end])" function
Bram Moolenaar0d660222005-01-07 21:51:51 +000012601 */
12602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012603f_getline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000012604{
12605 linenr_T lnum;
12606 linenr_T end;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012607 int retlist;
Bram Moolenaar0d660222005-01-07 21:51:51 +000012608
12609 lnum = get_tv_lnum(argvars);
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012610 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar342337a2005-07-21 21:11:17 +000012611 {
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012612 end = 0;
Bram Moolenaar342337a2005-07-21 21:11:17 +000012613 retlist = FALSE;
12614 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000012615 else
Bram Moolenaar342337a2005-07-21 21:11:17 +000012616 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000012617 end = get_tv_lnum(&argvars[1]);
Bram Moolenaar342337a2005-07-21 21:11:17 +000012618 retlist = TRUE;
12619 }
Bram Moolenaar80fc0432005-07-20 22:06:07 +000012620
Bram Moolenaar342337a2005-07-21 21:11:17 +000012621 get_buffer_lines(curbuf, lnum, end, retlist, rettv);
Bram Moolenaar0d660222005-01-07 21:51:51 +000012622}
12623
12624/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012625 * "getmatches()" function
12626 */
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012627 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012628f_getmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012629{
12630#ifdef FEAT_SEARCH_EXTRA
12631 dict_T *dict;
12632 matchitem_T *cur = curwin->w_match_head;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012633 int i;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012634
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012635 if (rettv_list_alloc(rettv) == OK)
12636 {
12637 while (cur != NULL)
12638 {
12639 dict = dict_alloc();
12640 if (dict == NULL)
12641 return;
Bram Moolenaarb3414592014-06-17 17:48:32 +020012642 if (cur->match.regprog == NULL)
12643 {
12644 /* match added with matchaddpos() */
12645 for (i = 0; i < MAXPOSMATCH; ++i)
12646 {
12647 llpos_T *llpos;
12648 char buf[6];
12649 list_T *l;
12650
12651 llpos = &cur->pos.pos[i];
12652 if (llpos->lnum == 0)
12653 break;
12654 l = list_alloc();
12655 if (l == NULL)
12656 break;
12657 list_append_number(l, (varnumber_T)llpos->lnum);
12658 if (llpos->col > 0)
12659 {
12660 list_append_number(l, (varnumber_T)llpos->col);
12661 list_append_number(l, (varnumber_T)llpos->len);
12662 }
12663 sprintf(buf, "pos%d", i + 1);
12664 dict_add_list(dict, buf, l);
12665 }
12666 }
12667 else
12668 {
12669 dict_add_nr_str(dict, "pattern", 0L, cur->pattern);
12670 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012671 dict_add_nr_str(dict, "group", 0L, syn_id2name(cur->hlg_id));
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012672 dict_add_nr_str(dict, "priority", (long)cur->priority, NULL);
12673 dict_add_nr_str(dict, "id", (long)cur->id, NULL);
Bram Moolenaar6561d522015-07-21 15:48:27 +020012674# ifdef FEAT_CONCEAL
12675 if (cur->conceal_char)
12676 {
12677 char_u buf[MB_MAXBYTES + 1];
12678
12679 buf[(*mb_char2bytes)((int)cur->conceal_char, buf)] = NUL;
12680 dict_add_nr_str(dict, "conceal", 0L, (char_u *)&buf);
12681 }
12682# endif
Bram Moolenaar6ee10162007-07-26 20:58:42 +000012683 list_append_dict(rettv->vval.v_list, dict);
12684 cur = cur->next;
12685 }
12686 }
12687#endif
12688}
12689
12690/*
Bram Moolenaar18081e32008-02-20 19:11:07 +000012691 * "getpid()" function
12692 */
Bram Moolenaar18081e32008-02-20 19:11:07 +000012693 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012694f_getpid(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar18081e32008-02-20 19:11:07 +000012695{
12696 rettv->vval.v_number = mch_get_pid();
12697}
12698
Bram Moolenaar48e697e2016-01-23 22:17:30 +010012699static void getpos_both(typval_T *argvars, typval_T *rettv, int getcurpos);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012700
12701/*
12702 * "getcurpos()" function
12703 */
12704 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012705f_getcurpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012706{
12707 getpos_both(argvars, rettv, TRUE);
12708}
12709
Bram Moolenaar18081e32008-02-20 19:11:07 +000012710/*
Bram Moolenaara5525202006-03-02 22:52:09 +000012711 * "getpos(string)" function
12712 */
12713 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012714f_getpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaara5525202006-03-02 22:52:09 +000012715{
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012716 getpos_both(argvars, rettv, FALSE);
12717}
12718
12719 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012720getpos_both(
12721 typval_T *argvars,
12722 typval_T *rettv,
12723 int getcurpos)
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012724{
Bram Moolenaara5525202006-03-02 22:52:09 +000012725 pos_T *fp;
12726 list_T *l;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012727 int fnum = -1;
Bram Moolenaara5525202006-03-02 22:52:09 +000012728
12729 if (rettv_list_alloc(rettv) == OK)
12730 {
12731 l = rettv->vval.v_list;
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012732 if (getcurpos)
12733 fp = &curwin->w_cursor;
12734 else
12735 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000012736 if (fnum != -1)
12737 list_append_number(l, (varnumber_T)fnum);
12738 else
12739 list_append_number(l, (varnumber_T)0);
Bram Moolenaara5525202006-03-02 22:52:09 +000012740 list_append_number(l, (fp != NULL) ? (varnumber_T)fp->lnum
12741 : (varnumber_T)0);
Bram Moolenaare65f7322007-10-02 20:08:54 +000012742 list_append_number(l, (fp != NULL)
12743 ? (varnumber_T)(fp->col == MAXCOL ? MAXCOL : fp->col + 1)
Bram Moolenaara5525202006-03-02 22:52:09 +000012744 : (varnumber_T)0);
12745 list_append_number(l,
12746#ifdef FEAT_VIRTUALEDIT
12747 (fp != NULL) ? (varnumber_T)fp->coladd :
12748#endif
12749 (varnumber_T)0);
Bram Moolenaar6f6c0f82014-05-28 20:31:42 +020012750 if (getcurpos)
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012751 {
12752 update_curswant();
Bram Moolenaar084abae2015-01-14 19:00:38 +010012753 list_append_number(l, curwin->w_curswant == MAXCOL ?
12754 (varnumber_T)MAXCOL : (varnumber_T)curwin->w_curswant + 1);
Bram Moolenaar2ab375e2016-02-10 22:23:06 +010012755 }
Bram Moolenaara5525202006-03-02 22:52:09 +000012756 }
12757 else
12758 rettv->vval.v_number = FALSE;
12759}
12760
12761/*
Bram Moolenaar280f1262006-01-30 00:14:18 +000012762 * "getqflist()" and "getloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000012763 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000012764 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012765f_getqflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012766{
12767#ifdef FEAT_QUICKFIX
Bram Moolenaar280f1262006-01-30 00:14:18 +000012768 win_T *wp;
Bram Moolenaar2641f772005-03-25 21:58:17 +000012769#endif
12770
Bram Moolenaar2641f772005-03-25 21:58:17 +000012771#ifdef FEAT_QUICKFIX
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012772 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000012773 {
Bram Moolenaar280f1262006-01-30 00:14:18 +000012774 wp = NULL;
12775 if (argvars[0].v_type != VAR_UNKNOWN) /* getloclist() */
12776 {
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012777 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar280f1262006-01-30 00:14:18 +000012778 if (wp == NULL)
12779 return;
12780 }
12781
Bram Moolenaareddf53b2006-02-27 00:11:10 +000012782 (void)get_errorlist(wp, rettv->vval.v_list);
Bram Moolenaar2641f772005-03-25 21:58:17 +000012783 }
12784#endif
12785}
12786
12787/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012788 * "getreg()" function
12789 */
12790 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012791f_getreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012792{
12793 char_u *strregname;
12794 int regname;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012795 int arg2 = FALSE;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012796 int return_list = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012797 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012798
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012799 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012800 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012801 strregname = get_tv_string_chk(&argvars[0]);
12802 error = strregname == NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012803 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012804 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012805 arg2 = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012806 if (!error && argvars[2].v_type != VAR_UNKNOWN)
12807 return_list = get_tv_number_chk(&argvars[2], &error);
12808 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000012809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012810 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000012811 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012812
12813 if (error)
12814 return;
12815
Bram Moolenaar071d4272004-06-13 20:20:40 +000012816 regname = (strregname == NULL ? '"' : *strregname);
12817 if (regname == 0)
12818 regname = '"';
12819
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012820 if (return_list)
12821 {
12822 rettv->v_type = VAR_LIST;
12823 rettv->vval.v_list = (list_T *)get_reg_contents(regname,
12824 (arg2 ? GREG_EXPR_SRC : 0) | GREG_LIST);
Bram Moolenaar42d84f82014-11-12 18:49:16 +010012825 if (rettv->vval.v_list != NULL)
12826 ++rettv->vval.v_list->lv_refcount;
Bram Moolenaarb7cb42b2014-04-02 19:55:10 +020012827 }
12828 else
12829 {
12830 rettv->v_type = VAR_STRING;
12831 rettv->vval.v_string = get_reg_contents(regname,
12832 arg2 ? GREG_EXPR_SRC : 0);
12833 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012834}
12835
12836/*
12837 * "getregtype()" function
12838 */
12839 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012840f_getregtype(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012841{
12842 char_u *strregname;
12843 int regname;
12844 char_u buf[NUMBUFLEN + 2];
12845 long reglen = 0;
12846
Bram Moolenaar49cd9572005-01-03 21:06:01 +000012847 if (argvars[0].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000012848 {
12849 strregname = get_tv_string_chk(&argvars[0]);
12850 if (strregname == NULL) /* type error; errmsg already given */
12851 {
12852 rettv->v_type = VAR_STRING;
12853 rettv->vval.v_string = NULL;
12854 return;
12855 }
12856 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000012857 else
12858 /* Default to v:register */
Bram Moolenaare9a41262005-01-15 22:18:47 +000012859 strregname = vimvars[VV_REG].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012860
12861 regname = (strregname == NULL ? '"' : *strregname);
12862 if (regname == 0)
12863 regname = '"';
12864
12865 buf[0] = NUL;
12866 buf[1] = NUL;
12867 switch (get_reg_type(regname, &reglen))
12868 {
12869 case MLINE: buf[0] = 'V'; break;
12870 case MCHAR: buf[0] = 'v'; break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012871 case MBLOCK:
12872 buf[0] = Ctrl_V;
12873 sprintf((char *)buf + 1, "%ld", reglen + 1);
12874 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012875 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012876 rettv->v_type = VAR_STRING;
12877 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000012878}
12879
12880/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012881 * "gettabvar()" function
12882 */
12883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012884f_gettabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012885{
Bram Moolenaar3089a102014-09-09 23:11:49 +020012886 win_T *oldcurwin;
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012887 tabpage_T *tp, *oldtabpage;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012888 dictitem_T *v;
12889 char_u *varname;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012890 int done = FALSE;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012891
12892 rettv->v_type = VAR_STRING;
12893 rettv->vval.v_string = NULL;
12894
12895 varname = get_tv_string_chk(&argvars[1]);
12896 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
12897 if (tp != NULL && varname != NULL)
12898 {
Bram Moolenaar3089a102014-09-09 23:11:49 +020012899 /* Set tp to be our tabpage, temporarily. Also set the window to the
12900 * first window in the tabpage, otherwise the window is not valid. */
Bram Moolenaar7e47d1a2015-08-25 16:19:05 +020012901 if (switch_win(&oldcurwin, &oldtabpage,
12902 tp->tp_firstwin == NULL ? firstwin : tp->tp_firstwin, tp, TRUE)
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012903 == OK)
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012904 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020012905 /* look up the variable */
12906 /* Let gettabvar({nr}, "") return the "t:" dictionary. */
12907 v = find_var_in_ht(&tp->tp_vars->dv_hashtab, 't', varname, FALSE);
12908 if (v != NULL)
12909 {
12910 copy_tv(&v->di_tv, rettv);
12911 done = TRUE;
12912 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012913 }
Bram Moolenaar0e2ea1b2014-09-09 16:13:08 +020012914
12915 /* restore previous notion of curwin */
12916 restore_win(oldcurwin, oldtabpage, TRUE);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012917 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020012918
12919 if (!done && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar63dbda12013-02-20 21:12:10 +010012920 copy_tv(&argvars[2], rettv);
Bram Moolenaar06b5d512010-05-22 15:37:44 +020012921}
12922
12923/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012924 * "gettabwinvar()" function
12925 */
12926 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012927f_gettabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000012928{
12929 getwinvar(argvars, rettv, 1);
12930}
12931
12932/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012933 * "getwinposx()" function
12934 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000012935 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010012936f_getwinposx(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000012937{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012938 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012939#ifdef FEAT_GUI
12940 if (gui.in_use)
12941 {
12942 int x, y;
12943
12944 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000012945 rettv->vval.v_number = x;
Bram Moolenaar071d4272004-06-13 20:20:40 +000012946 }
12947#endif
12948}
12949
12950/*
Bram Moolenaar9cdf86b2016-03-13 19:04:51 +010012951 * "win_findbuf()" function
12952 */
12953 static void
12954f_win_findbuf(typval_T *argvars, typval_T *rettv)
12955{
12956 if (rettv_list_alloc(rettv) != FAIL)
12957 win_findbuf(argvars, rettv->vval.v_list);
12958}
12959
12960/*
Bram Moolenaar86edef62016-03-13 18:07:30 +010012961 * "win_getid()" function
12962 */
12963 static void
12964f_win_getid(typval_T *argvars, typval_T *rettv)
12965{
12966 rettv->vval.v_number = win_getid(argvars);
12967}
12968
12969/*
12970 * "win_gotoid()" function
12971 */
12972 static void
12973f_win_gotoid(typval_T *argvars, typval_T *rettv)
12974{
12975 rettv->vval.v_number = win_gotoid(argvars);
12976}
12977
12978/*
12979 * "win_id2tabwin()" function
12980 */
12981 static void
12982f_win_id2tabwin(typval_T *argvars, typval_T *rettv)
12983{
12984 if (rettv_list_alloc(rettv) != FAIL)
12985 win_id2tabwin(argvars, rettv->vval.v_list);
12986}
12987
12988/*
12989 * "win_id2win()" function
12990 */
12991 static void
12992f_win_id2win(typval_T *argvars, typval_T *rettv)
12993{
12994 rettv->vval.v_number = win_id2win(argvars);
12995}
12996
12997/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000012998 * "getwinposy()" function
12999 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013000 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013001f_getwinposy(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013002{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013003 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013004#ifdef FEAT_GUI
13005 if (gui.in_use)
13006 {
13007 int x, y;
13008
13009 if (gui_mch_get_winpos(&x, &y) == OK)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013010 rettv->vval.v_number = y;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013011 }
13012#endif
13013}
13014
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013015/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013016 * Find window specified by "vp" in tabpage "tp".
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013017 */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013018 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013019find_win_by_nr(
13020 typval_T *vp,
13021 tabpage_T *tp UNUSED) /* NULL for current tab page */
Bram Moolenaara40058a2005-07-11 22:42:07 +000013022{
13023#ifdef FEAT_WINDOWS
13024 win_T *wp;
13025#endif
13026 int nr;
13027
13028 nr = get_tv_number_chk(vp, NULL);
13029
13030#ifdef FEAT_WINDOWS
13031 if (nr < 0)
13032 return NULL;
13033 if (nr == 0)
13034 return curwin;
13035
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013036 for (wp = (tp == NULL || tp == curtab) ? firstwin : tp->tp_firstwin;
13037 wp != NULL; wp = wp->w_next)
Bram Moolenaara40058a2005-07-11 22:42:07 +000013038 if (--nr <= 0)
13039 break;
13040 return wp;
13041#else
13042 if (nr == 0 || nr == 1)
13043 return curwin;
13044 return NULL;
13045#endif
13046}
13047
Bram Moolenaar071d4272004-06-13 20:20:40 +000013048/*
Bram Moolenaarc9703302016-01-17 21:49:33 +010013049 * Find window specified by "wvp" in tabpage "tvp".
13050 */
13051 static win_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010013052find_tabwin(
13053 typval_T *wvp, /* VAR_UNKNOWN for current window */
13054 typval_T *tvp) /* VAR_UNKNOWN for current tab page */
Bram Moolenaarc9703302016-01-17 21:49:33 +010013055{
13056 win_T *wp = NULL;
13057 tabpage_T *tp = NULL;
13058 long n;
13059
13060 if (wvp->v_type != VAR_UNKNOWN)
13061 {
13062 if (tvp->v_type != VAR_UNKNOWN)
13063 {
13064 n = get_tv_number(tvp);
13065 if (n >= 0)
13066 tp = find_tabpage(n);
13067 }
13068 else
13069 tp = curtab;
13070
13071 if (tp != NULL)
13072 wp = find_win_by_nr(wvp, tp);
13073 }
13074 else
13075 wp = curwin;
13076
13077 return wp;
13078}
13079
13080/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013081 * "getwinvar()" function
13082 */
13083 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013084f_getwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013085{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013086 getwinvar(argvars, rettv, 0);
13087}
13088
13089/*
13090 * getwinvar() and gettabwinvar()
13091 */
13092 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013093getwinvar(
13094 typval_T *argvars,
13095 typval_T *rettv,
13096 int off) /* 1 for gettabwinvar() */
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013097{
Bram Moolenaarba117c22015-09-29 16:53:22 +020013098 win_T *win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013099 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000013100 dictitem_T *v;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020013101 tabpage_T *tp = NULL;
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013102 int done = FALSE;
Bram Moolenaarba117c22015-09-29 16:53:22 +020013103#ifdef FEAT_WINDOWS
13104 win_T *oldcurwin;
13105 tabpage_T *oldtabpage;
13106 int need_switch_win;
13107#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013108
Bram Moolenaar99ebf042006-04-15 20:28:54 +000013109#ifdef FEAT_WINDOWS
13110 if (off == 1)
13111 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
13112 else
13113 tp = curtab;
13114#endif
13115 win = find_win_by_nr(&argvars[off], tp);
13116 varname = get_tv_string_chk(&argvars[off + 1]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013117 ++emsg_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013118
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013119 rettv->v_type = VAR_STRING;
13120 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013121
13122 if (win != NULL && varname != NULL)
13123 {
Bram Moolenaarba117c22015-09-29 16:53:22 +020013124#ifdef FEAT_WINDOWS
Bram Moolenaar105bc352013-05-17 16:03:57 +020013125 /* Set curwin to be our win, temporarily. Also set the tabpage,
Bram Moolenaarba117c22015-09-29 16:53:22 +020013126 * otherwise the window is not valid. Only do this when needed,
13127 * autocommands get blocked. */
13128 need_switch_win = !(tp == curtab && win == curwin);
13129 if (!need_switch_win
13130 || switch_win(&oldcurwin, &oldtabpage, win, tp, TRUE) == OK)
13131#endif
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013132 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013133 if (*varname == '&') /* window-local-option */
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013134 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020013135 if (get_option_tv(&varname, rettv, 1) == OK)
13136 done = TRUE;
13137 }
13138 else
13139 {
13140 /* Look up the variable. */
13141 /* Let getwinvar({nr}, "") return the "w:" dictionary. */
13142 v = find_var_in_ht(&win->w_vars->dv_hashtab, 'w',
13143 varname, FALSE);
13144 if (v != NULL)
13145 {
13146 copy_tv(&v->di_tv, rettv);
13147 done = TRUE;
13148 }
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013149 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013150 }
Bram Moolenaar69a7e432006-10-10 10:55:47 +000013151
Bram Moolenaarba117c22015-09-29 16:53:22 +020013152#ifdef FEAT_WINDOWS
13153 if (need_switch_win)
13154 /* restore previous notion of curwin */
13155 restore_win(oldcurwin, oldtabpage, TRUE);
13156#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013157 }
13158
Bram Moolenaar54c34fa2013-04-15 15:15:35 +020013159 if (!done && argvars[off + 2].v_type != VAR_UNKNOWN)
13160 /* use the default return value */
13161 copy_tv(&argvars[off + 2], rettv);
13162
Bram Moolenaar071d4272004-06-13 20:20:40 +000013163 --emsg_off;
13164}
13165
13166/*
13167 * "glob()" function
13168 */
13169 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013170f_glob(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013171{
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013172 int options = WILD_SILENT|WILD_USE_NL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013173 expand_T xpc;
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013174 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013175
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013176 /* When the optional second argument is non-zero, don't remove matches
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013177 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013178 rettv->v_type = VAR_STRING;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013179 if (argvars[1].v_type != VAR_UNKNOWN)
13180 {
13181 if (get_tv_number_chk(&argvars[1], &error))
13182 options |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013183 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013184 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013185 if (get_tv_number_chk(&argvars[2], &error))
13186 {
13187 rettv->v_type = VAR_LIST;
13188 rettv->vval.v_list = NULL;
13189 }
13190 if (argvars[3].v_type != VAR_UNKNOWN
13191 && get_tv_number_chk(&argvars[3], &error))
13192 options |= WILD_ALLLINKS;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013193 }
13194 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013195 if (!error)
13196 {
13197 ExpandInit(&xpc);
13198 xpc.xp_context = EXPAND_FILES;
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013199 if (p_wic)
13200 options += WILD_ICASE;
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013201 if (rettv->v_type == VAR_STRING)
13202 rettv->vval.v_string = ExpandOne(&xpc, get_tv_string(&argvars[0]),
Bram Moolenaar005c3c22010-12-02 21:44:40 +010013203 NULL, options, WILD_ALL);
Bram Moolenaar146e9c32012-03-07 19:18:23 +010013204 else if (rettv_list_alloc(rettv) != FAIL)
13205 {
13206 int i;
13207
13208 ExpandOne(&xpc, get_tv_string(&argvars[0]),
13209 NULL, options, WILD_ALL_KEEP);
13210 for (i = 0; i < xpc.xp_numfiles; i++)
13211 list_append_string(rettv->vval.v_list, xpc.xp_files[i], -1);
13212
13213 ExpandCleanup(&xpc);
13214 }
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013215 }
13216 else
13217 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013218}
13219
13220/*
13221 * "globpath()" function
13222 */
13223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013224f_globpath(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013225{
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013226 int flags = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013227 char_u buf1[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013228 char_u *file = get_tv_string_buf_chk(&argvars[1], buf1);
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013229 int error = FALSE;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013230 garray_T ga;
13231 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013232
Bram Moolenaarbb5ddda2008-11-28 10:01:10 +000013233 /* When the optional second argument is non-zero, don't remove matches
13234 * for 'wildignore' and don't put matches for 'suffixes' at the end. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013235 rettv->v_type = VAR_STRING;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013236 if (argvars[2].v_type != VAR_UNKNOWN)
13237 {
13238 if (get_tv_number_chk(&argvars[2], &error))
13239 flags |= WILD_KEEP_ALL;
Bram Moolenaara245bc72015-03-05 19:35:25 +010013240 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013241 {
Bram Moolenaara245bc72015-03-05 19:35:25 +010013242 if (get_tv_number_chk(&argvars[3], &error))
13243 {
13244 rettv->v_type = VAR_LIST;
13245 rettv->vval.v_list = NULL;
13246 }
13247 if (argvars[4].v_type != VAR_UNKNOWN
13248 && get_tv_number_chk(&argvars[4], &error))
13249 flags |= WILD_ALLLINKS;
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013250 }
13251 }
13252 if (file != NULL && !error)
13253 {
13254 ga_init2(&ga, (int)sizeof(char_u *), 10);
13255 globpath(get_tv_string(&argvars[0]), file, &ga, flags);
13256 if (rettv->v_type == VAR_STRING)
13257 rettv->vval.v_string = ga_concat_strings(&ga, "\n");
13258 else if (rettv_list_alloc(rettv) != FAIL)
13259 for (i = 0; i < ga.ga_len; ++i)
13260 list_append_string(rettv->vval.v_list,
13261 ((char_u **)(ga.ga_data))[i], -1);
13262 ga_clear_strings(&ga);
13263 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013264 else
Bram Moolenaar1b1063a2014-05-07 18:35:30 +020013265 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013266}
13267
13268/*
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013269 * "glob2regpat()" function
13270 */
13271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013272f_glob2regpat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013273{
13274 char_u *pat = get_tv_string_chk(&argvars[0]);
13275
13276 rettv->v_type = VAR_STRING;
Bram Moolenaar7465c632016-01-25 22:20:27 +010013277 rettv->vval.v_string = (pat == NULL)
13278 ? NULL : file_pat_to_reg_pat(pat, NULL, NULL, FALSE);
Bram Moolenaar825e7ab2015-03-20 17:36:42 +010013279}
13280
13281/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013282 * "has()" function
13283 */
13284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013285f_has(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013286{
13287 int i;
13288 char_u *name;
13289 int n = FALSE;
13290 static char *(has_list[]) =
13291 {
13292#ifdef AMIGA
13293 "amiga",
13294# ifdef FEAT_ARP
13295 "arp",
13296# endif
13297#endif
13298#ifdef __BEOS__
13299 "beos",
13300#endif
Bram Moolenaar241a8aa2005-12-06 20:04:44 +000013301#ifdef MACOS
Bram Moolenaar071d4272004-06-13 20:20:40 +000013302 "mac",
13303#endif
13304#if defined(MACOS_X_UNIX)
Bram Moolenaarf8df7ad2016-02-16 14:07:40 +010013305 "macunix", /* built with 'darwin' enabled */
13306#endif
13307#if defined(__APPLE__) && __APPLE__ == 1
13308 "osx", /* built with or without 'darwin' enabled */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013309#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013310#ifdef __QNX__
13311 "qnx",
13312#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013313#ifdef UNIX
13314 "unix",
13315#endif
13316#ifdef VMS
13317 "vms",
13318#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013319#ifdef WIN32
13320 "win32",
13321#endif
13322#if defined(UNIX) && (defined(__CYGWIN32__) || defined(__CYGWIN__))
13323 "win32unix",
13324#endif
Bram Moolenaare37d7992010-01-12 13:18:33 +010013325#if defined(WIN64) || defined(_WIN64)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013326 "win64",
13327#endif
13328#ifdef EBCDIC
13329 "ebcdic",
13330#endif
13331#ifndef CASE_INSENSITIVE_FILENAME
13332 "fname_case",
13333#endif
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013334#ifdef HAVE_ACL
13335 "acl",
13336#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013337#ifdef FEAT_ARABIC
13338 "arabic",
13339#endif
13340#ifdef FEAT_AUTOCMD
13341 "autocmd",
13342#endif
13343#ifdef FEAT_BEVAL
13344 "balloon_eval",
Bram Moolenaar342337a2005-07-21 21:11:17 +000013345# ifndef FEAT_GUI_W32 /* other GUIs always have multiline balloons */
13346 "balloon_multiline",
13347# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013348#endif
13349#if defined(SOME_BUILTIN_TCAPS) || defined(ALL_BUILTIN_TCAPS)
13350 "builtin_terms",
13351# ifdef ALL_BUILTIN_TCAPS
13352 "all_builtin_terms",
13353# endif
13354#endif
Bram Moolenaar77c604d2012-07-10 13:41:14 +020013355#if defined(FEAT_BROWSE) && (defined(USE_FILE_CHOOSER) \
13356 || defined(FEAT_GUI_W32) \
13357 || defined(FEAT_GUI_MOTIF))
13358 "browsefilter",
13359#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013360#ifdef FEAT_BYTEOFF
13361 "byte_offset",
13362#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013363#ifdef FEAT_JOB_CHANNEL
Bram Moolenaare0874f82016-01-24 20:36:41 +010013364 "channel",
13365#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013366#ifdef FEAT_CINDENT
13367 "cindent",
13368#endif
13369#ifdef FEAT_CLIENTSERVER
13370 "clientserver",
13371#endif
13372#ifdef FEAT_CLIPBOARD
13373 "clipboard",
13374#endif
13375#ifdef FEAT_CMDL_COMPL
13376 "cmdline_compl",
13377#endif
13378#ifdef FEAT_CMDHIST
13379 "cmdline_hist",
13380#endif
13381#ifdef FEAT_COMMENTS
13382 "comments",
13383#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013384#ifdef FEAT_CONCEAL
13385 "conceal",
13386#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013387#ifdef FEAT_CRYPT
13388 "cryptv",
Bram Moolenaar36d7cd82016-01-15 22:08:23 +010013389 "crypt-blowfish",
13390 "crypt-blowfish2",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013391#endif
13392#ifdef FEAT_CSCOPE
13393 "cscope",
13394#endif
Bram Moolenaar860cae12010-06-05 23:22:07 +020013395#ifdef FEAT_CURSORBIND
13396 "cursorbind",
13397#endif
Bram Moolenaarac6e65f2005-08-29 22:25:38 +000013398#ifdef CURSOR_SHAPE
13399 "cursorshape",
13400#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013401#ifdef DEBUG
13402 "debug",
13403#endif
13404#ifdef FEAT_CON_DIALOG
13405 "dialog_con",
13406#endif
13407#ifdef FEAT_GUI_DIALOG
13408 "dialog_gui",
13409#endif
13410#ifdef FEAT_DIFF
13411 "diff",
13412#endif
13413#ifdef FEAT_DIGRAPHS
13414 "digraphs",
13415#endif
Bram Moolenaarb5a7a8b2014-08-06 14:52:30 +020013416#ifdef FEAT_DIRECTX
13417 "directx",
13418#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013419#ifdef FEAT_DND
13420 "dnd",
13421#endif
13422#ifdef FEAT_EMACS_TAGS
13423 "emacs_tags",
13424#endif
13425 "eval", /* always present, of course! */
Bram Moolenaare2c38102016-01-31 14:55:40 +010013426 "ex_extra", /* graduated feature */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013427#ifdef FEAT_SEARCH_EXTRA
13428 "extra_search",
13429#endif
13430#ifdef FEAT_FKMAP
13431 "farsi",
13432#endif
13433#ifdef FEAT_SEARCHPATH
13434 "file_in_path",
13435#endif
Bram Moolenaar68a33fc2012-04-25 16:50:48 +020013436#ifdef FEAT_FILTERPIPE
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000013437 "filterpipe",
13438#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013439#ifdef FEAT_FIND_ID
13440 "find_in_path",
13441#endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013442#ifdef FEAT_FLOAT
13443 "float",
13444#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013445#ifdef FEAT_FOLDING
13446 "folding",
13447#endif
13448#ifdef FEAT_FOOTER
13449 "footer",
13450#endif
13451#if !defined(USE_SYSTEM) && defined(UNIX)
13452 "fork",
13453#endif
13454#ifdef FEAT_GETTEXT
13455 "gettext",
13456#endif
13457#ifdef FEAT_GUI
13458 "gui",
13459#endif
13460#ifdef FEAT_GUI_ATHENA
13461# ifdef FEAT_GUI_NEXTAW
13462 "gui_neXtaw",
13463# else
13464 "gui_athena",
13465# endif
13466#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013467#ifdef FEAT_GUI_GTK
13468 "gui_gtk",
Bram Moolenaar98921892016-02-23 17:14:37 +010013469# ifdef USE_GTK3
13470 "gui_gtk3",
13471# else
Bram Moolenaar071d4272004-06-13 20:20:40 +000013472 "gui_gtk2",
Bram Moolenaar98921892016-02-23 17:14:37 +010013473# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013474#endif
Bram Moolenaar7b188622007-09-25 10:51:12 +000013475#ifdef FEAT_GUI_GNOME
13476 "gui_gnome",
13477#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013478#ifdef FEAT_GUI_MAC
13479 "gui_mac",
13480#endif
13481#ifdef FEAT_GUI_MOTIF
13482 "gui_motif",
13483#endif
13484#ifdef FEAT_GUI_PHOTON
13485 "gui_photon",
13486#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013487#ifdef FEAT_GUI_W32
13488 "gui_win32",
13489#endif
13490#ifdef FEAT_HANGULIN
13491 "hangul_input",
13492#endif
13493#if defined(HAVE_ICONV_H) && defined(USE_ICONV)
13494 "iconv",
13495#endif
13496#ifdef FEAT_INS_EXPAND
13497 "insert_expand",
13498#endif
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010013499#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010013500 "job",
13501#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013502#ifdef FEAT_JUMPLIST
13503 "jumplist",
13504#endif
13505#ifdef FEAT_KEYMAP
13506 "keymap",
13507#endif
13508#ifdef FEAT_LANGMAP
13509 "langmap",
13510#endif
13511#ifdef FEAT_LIBCALL
13512 "libcall",
13513#endif
13514#ifdef FEAT_LINEBREAK
13515 "linebreak",
13516#endif
13517#ifdef FEAT_LISP
13518 "lispindent",
13519#endif
13520#ifdef FEAT_LISTCMDS
13521 "listcmds",
13522#endif
13523#ifdef FEAT_LOCALMAP
13524 "localmap",
13525#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013526#ifdef FEAT_LUA
13527# ifndef DYNAMIC_LUA
13528 "lua",
13529# endif
13530#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013531#ifdef FEAT_MENU
13532 "menu",
13533#endif
13534#ifdef FEAT_SESSION
13535 "mksession",
13536#endif
13537#ifdef FEAT_MODIFY_FNAME
13538 "modify_fname",
13539#endif
13540#ifdef FEAT_MOUSE
13541 "mouse",
13542#endif
13543#ifdef FEAT_MOUSESHAPE
13544 "mouseshape",
13545#endif
13546#if defined(UNIX) || defined(VMS)
13547# ifdef FEAT_MOUSE_DEC
13548 "mouse_dec",
13549# endif
13550# ifdef FEAT_MOUSE_GPM
13551 "mouse_gpm",
13552# endif
13553# ifdef FEAT_MOUSE_JSB
13554 "mouse_jsbterm",
13555# endif
13556# ifdef FEAT_MOUSE_NET
13557 "mouse_netterm",
13558# endif
13559# ifdef FEAT_MOUSE_PTERM
13560 "mouse_pterm",
13561# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013562# ifdef FEAT_MOUSE_SGR
13563 "mouse_sgr",
13564# endif
Bram Moolenaar8c8de832008-06-24 22:58:06 +000013565# ifdef FEAT_SYSMOUSE
13566 "mouse_sysmouse",
13567# endif
Bram Moolenaarcfb80702012-10-21 02:17:45 +020013568# ifdef FEAT_MOUSE_URXVT
13569 "mouse_urxvt",
13570# endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013571# ifdef FEAT_MOUSE_XTERM
13572 "mouse_xterm",
13573# endif
13574#endif
13575#ifdef FEAT_MBYTE
13576 "multi_byte",
13577#endif
13578#ifdef FEAT_MBYTE_IME
13579 "multi_byte_ime",
13580#endif
13581#ifdef FEAT_MULTI_LANG
13582 "multi_lang",
13583#endif
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013584#ifdef FEAT_MZSCHEME
Bram Moolenaar33570922005-01-25 22:26:29 +000013585#ifndef DYNAMIC_MZSCHEME
Bram Moolenaar325b7a22004-07-05 15:58:32 +000013586 "mzscheme",
13587#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013588#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013589#ifdef FEAT_OLE
13590 "ole",
13591#endif
Bram Moolenaarf6fee0e2016-02-21 23:02:49 +010013592 "packages",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013593#ifdef FEAT_PATH_EXTRA
13594 "path_extra",
13595#endif
13596#ifdef FEAT_PERL
13597#ifndef DYNAMIC_PERL
13598 "perl",
13599#endif
13600#endif
Bram Moolenaar55debbe2010-05-23 23:34:36 +020013601#ifdef FEAT_PERSISTENT_UNDO
13602 "persistent_undo",
13603#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013604#ifdef FEAT_PYTHON
13605#ifndef DYNAMIC_PYTHON
13606 "python",
13607#endif
13608#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013609#ifdef FEAT_PYTHON3
13610#ifndef DYNAMIC_PYTHON3
13611 "python3",
13612#endif
13613#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013614#ifdef FEAT_POSTSCRIPT
13615 "postscript",
13616#endif
13617#ifdef FEAT_PRINTER
13618 "printer",
13619#endif
Bram Moolenaar05159a02005-02-26 23:04:13 +000013620#ifdef FEAT_PROFILE
13621 "profile",
13622#endif
Bram Moolenaare580b0c2006-03-21 21:33:03 +000013623#ifdef FEAT_RELTIME
13624 "reltime",
13625#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013626#ifdef FEAT_QUICKFIX
13627 "quickfix",
13628#endif
13629#ifdef FEAT_RIGHTLEFT
13630 "rightleft",
13631#endif
13632#if defined(FEAT_RUBY) && !defined(DYNAMIC_RUBY)
13633 "ruby",
13634#endif
13635#ifdef FEAT_SCROLLBIND
13636 "scrollbind",
13637#endif
13638#ifdef FEAT_CMDL_INFO
13639 "showcmd",
13640 "cmdline_info",
13641#endif
13642#ifdef FEAT_SIGNS
13643 "signs",
13644#endif
13645#ifdef FEAT_SMARTINDENT
13646 "smartindent",
13647#endif
Bram Moolenaaref94eec2009-11-11 13:22:11 +000013648#ifdef STARTUPTIME
13649 "startuptime",
13650#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013651#ifdef FEAT_STL_OPT
13652 "statusline",
13653#endif
13654#ifdef FEAT_SUN_WORKSHOP
13655 "sun_workshop",
13656#endif
13657#ifdef FEAT_NETBEANS_INTG
13658 "netbeans_intg",
13659#endif
Bram Moolenaar3c56a962006-03-12 22:19:04 +000013660#ifdef FEAT_SPELL
Bram Moolenaar0e4d8772005-06-07 21:12:49 +000013661 "spell",
13662#endif
13663#ifdef FEAT_SYN_HL
Bram Moolenaar071d4272004-06-13 20:20:40 +000013664 "syntax",
13665#endif
13666#if defined(USE_SYSTEM) || !defined(UNIX)
13667 "system",
13668#endif
13669#ifdef FEAT_TAG_BINS
13670 "tag_binary",
13671#endif
13672#ifdef FEAT_TAG_OLDSTATIC
13673 "tag_old_static",
13674#endif
13675#ifdef FEAT_TAG_ANYWHITE
13676 "tag_any_white",
13677#endif
13678#ifdef FEAT_TCL
13679# ifndef DYNAMIC_TCL
13680 "tcl",
13681# endif
13682#endif
13683#ifdef TERMINFO
13684 "terminfo",
13685#endif
13686#ifdef FEAT_TERMRESPONSE
13687 "termresponse",
13688#endif
13689#ifdef FEAT_TEXTOBJ
13690 "textobjects",
13691#endif
13692#ifdef HAVE_TGETENT
13693 "tgetent",
13694#endif
Bram Moolenaar975b5272016-03-15 23:10:59 +010013695#ifdef FEAT_TIMERS
13696 "timers",
13697#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013698#ifdef FEAT_TITLE
13699 "title",
13700#endif
13701#ifdef FEAT_TOOLBAR
13702 "toolbar",
13703#endif
Bram Moolenaarbf9680e2010-12-02 21:43:16 +010013704#if defined(FEAT_CLIPBOARD) && defined(FEAT_X11)
13705 "unnamedplus",
13706#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013707#ifdef FEAT_USR_CMDS
13708 "user-commands", /* was accidentally included in 5.4 */
13709 "user_commands",
13710#endif
13711#ifdef FEAT_VIMINFO
13712 "viminfo",
13713#endif
13714#ifdef FEAT_VERTSPLIT
13715 "vertsplit",
13716#endif
13717#ifdef FEAT_VIRTUALEDIT
13718 "virtualedit",
13719#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013720 "visual",
Bram Moolenaar071d4272004-06-13 20:20:40 +000013721#ifdef FEAT_VISUALEXTRA
13722 "visualextra",
13723#endif
13724#ifdef FEAT_VREPLACE
13725 "vreplace",
13726#endif
13727#ifdef FEAT_WILDIGN
13728 "wildignore",
13729#endif
13730#ifdef FEAT_WILDMENU
13731 "wildmenu",
13732#endif
13733#ifdef FEAT_WINDOWS
13734 "windows",
13735#endif
13736#ifdef FEAT_WAK
13737 "winaltkeys",
13738#endif
13739#ifdef FEAT_WRITEBACKUP
13740 "writebackup",
13741#endif
13742#ifdef FEAT_XIM
13743 "xim",
13744#endif
13745#ifdef FEAT_XFONTSET
13746 "xfontset",
13747#endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013748#ifdef FEAT_XPM_W32
Bram Moolenaarb5ef5e12013-08-30 16:35:44 +020013749 "xpm",
13750 "xpm_w32", /* for backward compatibility */
13751#else
13752# if defined(HAVE_XPM)
13753 "xpm",
13754# endif
Bram Moolenaar79a2a492012-01-04 14:35:37 +010013755#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013756#ifdef USE_XSMP
13757 "xsmp",
13758#endif
13759#ifdef USE_XSMP_INTERACT
13760 "xsmp_interact",
13761#endif
13762#ifdef FEAT_XCLIPBOARD
13763 "xterm_clipboard",
13764#endif
13765#ifdef FEAT_XTERM_SAVE
13766 "xterm_save",
13767#endif
13768#if defined(UNIX) && defined(FEAT_X11)
13769 "X11",
13770#endif
13771 NULL
13772 };
13773
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013774 name = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013775 for (i = 0; has_list[i] != NULL; ++i)
13776 if (STRICMP(name, has_list[i]) == 0)
13777 {
13778 n = TRUE;
13779 break;
13780 }
13781
13782 if (n == FALSE)
13783 {
13784 if (STRNICMP(name, "patch", 5) == 0)
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013785 {
13786 if (name[5] == '-'
13787 && STRLEN(name) > 11
13788 && vim_isdigit(name[6])
13789 && vim_isdigit(name[8])
13790 && vim_isdigit(name[10]))
13791 {
13792 int major = atoi((char *)name + 6);
13793 int minor = atoi((char *)name + 8);
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013794
13795 /* Expect "patch-9.9.01234". */
13796 n = (major < VIM_VERSION_MAJOR
13797 || (major == VIM_VERSION_MAJOR
13798 && (minor < VIM_VERSION_MINOR
13799 || (minor == VIM_VERSION_MINOR
Bram Moolenaar6716d9a2014-04-02 12:12:08 +020013800 && has_patch(atoi((char *)name + 10))))));
Bram Moolenaar7f3be402014-04-01 22:08:54 +020013801 }
13802 else
13803 n = has_patch(atoi((char *)name + 5));
13804 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013805 else if (STRICMP(name, "vim_starting") == 0)
13806 n = (starting != 0);
Bram Moolenaar42022d52008-12-09 09:57:49 +000013807#ifdef FEAT_MBYTE
13808 else if (STRICMP(name, "multi_byte_encoding") == 0)
13809 n = has_mbyte;
13810#endif
Bram Moolenaar342337a2005-07-21 21:11:17 +000013811#if defined(FEAT_BEVAL) && defined(FEAT_GUI_W32)
13812 else if (STRICMP(name, "balloon_multiline") == 0)
13813 n = multiline_balloon_available();
13814#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013815#ifdef DYNAMIC_TCL
13816 else if (STRICMP(name, "tcl") == 0)
13817 n = tcl_enabled(FALSE);
13818#endif
13819#if defined(USE_ICONV) && defined(DYNAMIC_ICONV)
13820 else if (STRICMP(name, "iconv") == 0)
13821 n = iconv_enabled(FALSE);
13822#endif
Bram Moolenaar0ba04292010-07-14 23:23:17 +020013823#ifdef DYNAMIC_LUA
13824 else if (STRICMP(name, "lua") == 0)
13825 n = lua_enabled(FALSE);
13826#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000013827#ifdef DYNAMIC_MZSCHEME
13828 else if (STRICMP(name, "mzscheme") == 0)
13829 n = mzscheme_enabled(FALSE);
13830#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013831#ifdef DYNAMIC_RUBY
13832 else if (STRICMP(name, "ruby") == 0)
13833 n = ruby_enabled(FALSE);
13834#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013835#ifdef FEAT_PYTHON
Bram Moolenaar071d4272004-06-13 20:20:40 +000013836#ifdef DYNAMIC_PYTHON
13837 else if (STRICMP(name, "python") == 0)
13838 n = python_enabled(FALSE);
13839#endif
Bram Moolenaarbd5e15f2010-07-17 21:19:38 +020013840#endif
13841#ifdef FEAT_PYTHON3
13842#ifdef DYNAMIC_PYTHON3
13843 else if (STRICMP(name, "python3") == 0)
13844 n = python3_enabled(FALSE);
13845#endif
13846#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013847#ifdef DYNAMIC_PERL
13848 else if (STRICMP(name, "perl") == 0)
13849 n = perl_enabled(FALSE);
13850#endif
13851#ifdef FEAT_GUI
13852 else if (STRICMP(name, "gui_running") == 0)
13853 n = (gui.in_use || gui.starting);
13854# ifdef FEAT_GUI_W32
13855 else if (STRICMP(name, "gui_win32s") == 0)
13856 n = gui_is_win32s();
13857# endif
13858# ifdef FEAT_BROWSE
13859 else if (STRICMP(name, "browse") == 0)
13860 n = gui.in_use; /* gui_mch_browse() works when GUI is running */
13861# endif
13862#endif
13863#ifdef FEAT_SYN_HL
13864 else if (STRICMP(name, "syntax_items") == 0)
Bram Moolenaar860cae12010-06-05 23:22:07 +020013865 n = syntax_present(curwin);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013866#endif
13867#if defined(WIN3264)
13868 else if (STRICMP(name, "win95") == 0)
13869 n = mch_windows95();
13870#endif
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013871#ifdef FEAT_NETBEANS_INTG
13872 else if (STRICMP(name, "netbeans_enabled") == 0)
Bram Moolenaarb26e6322010-05-22 21:34:09 +020013873 n = netbeans_active();
Bram Moolenaar35a9aaa2004-10-24 19:23:07 +000013874#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000013875 }
13876
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013877 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013878}
13879
13880/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000013881 * "has_key()" function
13882 */
13883 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013884f_has_key(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9a41262005-01-15 22:18:47 +000013885{
Bram Moolenaare9a41262005-01-15 22:18:47 +000013886 if (argvars[0].v_type != VAR_DICT)
13887 {
13888 EMSG(_(e_dictreq));
13889 return;
13890 }
13891 if (argvars[0].vval.v_dict == NULL)
13892 return;
13893
13894 rettv->vval.v_number = dict_find(argvars[0].vval.v_dict,
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000013895 get_tv_string(&argvars[1]), -1) != NULL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000013896}
13897
13898/*
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013899 * "haslocaldir()" function
13900 */
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013901 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013902f_haslocaldir(typval_T *argvars, typval_T *rettv)
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013903{
Bram Moolenaarc9703302016-01-17 21:49:33 +010013904 win_T *wp = NULL;
13905
13906 wp = find_tabwin(&argvars[0], &argvars[1]);
13907 rettv->vval.v_number = (wp != NULL && wp->w_localdir != NULL);
Bram Moolenaard267b9c2007-04-26 15:06:45 +000013908}
13909
13910/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000013911 * "hasmapto()" function
13912 */
13913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013914f_hasmapto(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013915{
13916 char_u *name;
13917 char_u *mode;
13918 char_u buf[NUMBUFLEN];
Bram Moolenaar2c932302006-03-18 21:42:09 +000013919 int abbr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013920
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013921 name = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013922 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013923 mode = (char_u *)"nvo";
13924 else
Bram Moolenaar2c932302006-03-18 21:42:09 +000013925 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013926 mode = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000013927 if (argvars[2].v_type != VAR_UNKNOWN)
13928 abbr = get_tv_number(&argvars[2]);
13929 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000013930
Bram Moolenaar2c932302006-03-18 21:42:09 +000013931 if (map_to_exists(name, mode, abbr))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013932 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013933 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013934 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013935}
13936
13937/*
13938 * "histadd()" function
13939 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013940 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013941f_histadd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013942{
13943#ifdef FEAT_CMDHIST
13944 int histype;
13945 char_u *str;
13946 char_u buf[NUMBUFLEN];
13947#endif
13948
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013949 rettv->vval.v_number = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013950 if (check_restricted() || check_secure())
13951 return;
13952#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013953 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13954 histype = str != NULL ? get_histtype(str) : -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013955 if (histype >= 0)
13956 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013957 str = get_tv_string_buf(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000013958 if (*str != NUL)
13959 {
Bram Moolenaarc7be3f32009-12-24 14:01:12 +000013960 init_history();
Bram Moolenaar071d4272004-06-13 20:20:40 +000013961 add_to_history(histype, str, FALSE, NUL);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013962 rettv->vval.v_number = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013963 return;
13964 }
13965 }
13966#endif
13967}
13968
13969/*
13970 * "histdel()" function
13971 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000013972 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010013973f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013974{
13975#ifdef FEAT_CMDHIST
13976 int n;
13977 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013978 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013979
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013980 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
13981 if (str == NULL)
13982 n = 0;
13983 else if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013984 /* only one argument: clear entire history */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013985 n = clr_history(get_histtype(str));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000013986 else if (argvars[1].v_type == VAR_NUMBER)
Bram Moolenaar071d4272004-06-13 20:20:40 +000013987 /* index given: remove that entry */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013988 n = del_history_idx(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013989 (int)get_tv_number(&argvars[1]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000013990 else
13991 /* string given: remove all matching entries */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000013992 n = del_history_entry(get_histtype(str),
Bram Moolenaarc70646c2005-01-04 21:52:38 +000013993 get_tv_string_buf(&argvars[1], buf));
13994 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000013995#endif
13996}
13997
13998/*
13999 * "histget()" function
14000 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014001 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014002f_histget(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014003{
14004#ifdef FEAT_CMDHIST
14005 int type;
14006 int idx;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014007 char_u *str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014008
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014009 str = get_tv_string_chk(&argvars[0]); /* NULL on type error */
14010 if (str == NULL)
14011 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014012 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014013 {
14014 type = get_histtype(str);
14015 if (argvars[1].v_type == VAR_UNKNOWN)
14016 idx = get_history_idx(type);
14017 else
14018 idx = (int)get_tv_number_chk(&argvars[1], NULL);
14019 /* -1 on type error */
14020 rettv->vval.v_string = vim_strsave(get_history_entry(type, idx));
14021 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014022#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014023 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014024#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014025 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014026}
14027
14028/*
14029 * "histnr()" function
14030 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014031 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014032f_histnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014033{
14034 int i;
14035
14036#ifdef FEAT_CMDHIST
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014037 char_u *history = get_tv_string_chk(&argvars[0]);
14038
14039 i = history == NULL ? HIST_CMD - 1 : get_histtype(history);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014040 if (i >= HIST_CMD && i < HIST_COUNT)
14041 i = get_history_idx(i);
14042 else
14043#endif
14044 i = -1;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014045 rettv->vval.v_number = i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014046}
14047
14048/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014049 * "highlightID(name)" function
14050 */
14051 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014052f_hlID(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014053{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014054 rettv->vval.v_number = syn_name2id(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014055}
14056
14057/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014058 * "highlight_exists()" function
14059 */
14060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014061f_hlexists(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014062{
14063 rettv->vval.v_number = highlight_exists(get_tv_string(&argvars[0]));
14064}
14065
14066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014067 * "hostname()" function
14068 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014070f_hostname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014071{
14072 char_u hostname[256];
14073
14074 mch_get_host_name(hostname, 256);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014075 rettv->v_type = VAR_STRING;
14076 rettv->vval.v_string = vim_strsave(hostname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014077}
14078
14079/*
14080 * iconv() function
14081 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014083f_iconv(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014084{
14085#ifdef FEAT_MBYTE
14086 char_u buf1[NUMBUFLEN];
14087 char_u buf2[NUMBUFLEN];
14088 char_u *from, *to, *str;
14089 vimconv_T vimconv;
14090#endif
14091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014092 rettv->v_type = VAR_STRING;
14093 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014094
14095#ifdef FEAT_MBYTE
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014096 str = get_tv_string(&argvars[0]);
14097 from = enc_canonize(enc_skip(get_tv_string_buf(&argvars[1], buf1)));
14098 to = enc_canonize(enc_skip(get_tv_string_buf(&argvars[2], buf2)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014099 vimconv.vc_type = CONV_NONE;
14100 convert_setup(&vimconv, from, to);
14101
14102 /* If the encodings are equal, no conversion needed. */
14103 if (vimconv.vc_type == CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014104 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014105 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014106 rettv->vval.v_string = string_convert(&vimconv, str, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014107
14108 convert_setup(&vimconv, NULL, NULL);
14109 vim_free(from);
14110 vim_free(to);
14111#endif
14112}
14113
14114/*
14115 * "indent()" function
14116 */
14117 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014118f_indent(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014119{
14120 linenr_T lnum;
14121
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014122 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014123 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014124 rettv->vval.v_number = get_indent_lnum(lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014125 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014126 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014127}
14128
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014129/*
14130 * "index()" function
14131 */
14132 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014133f_index(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014134{
Bram Moolenaar33570922005-01-25 22:26:29 +000014135 list_T *l;
14136 listitem_T *item;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014137 long idx = 0;
14138 int ic = FALSE;
14139
14140 rettv->vval.v_number = -1;
14141 if (argvars[0].v_type != VAR_LIST)
14142 {
14143 EMSG(_(e_listreq));
14144 return;
14145 }
14146 l = argvars[0].vval.v_list;
14147 if (l != NULL)
14148 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014149 item = l->lv_first;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014150 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014151 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014152 int error = FALSE;
14153
Bram Moolenaar758711c2005-02-02 23:11:38 +000014154 /* Start at specified item. Use the cached index that list_find()
14155 * sets, so that a negative number also works. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014156 item = list_find(l, get_tv_number_chk(&argvars[2], &error));
Bram Moolenaar758711c2005-02-02 23:11:38 +000014157 idx = l->lv_idx;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014158 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014159 ic = get_tv_number_chk(&argvars[3], &error);
14160 if (error)
14161 item = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014162 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014163
Bram Moolenaar758711c2005-02-02 23:11:38 +000014164 for ( ; item != NULL; item = item->li_next, ++idx)
Bram Moolenaar67b3f992010-11-10 20:41:57 +010014165 if (tv_equal(&item->li_tv, &argvars[1], ic, FALSE))
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014166 {
14167 rettv->vval.v_number = idx;
14168 break;
14169 }
14170 }
14171}
14172
Bram Moolenaar071d4272004-06-13 20:20:40 +000014173static int inputsecret_flag = 0;
14174
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014175static void get_user_input(typval_T *argvars, typval_T *rettv, int inputdialog);
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014176
Bram Moolenaar071d4272004-06-13 20:20:40 +000014177/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014178 * This function is used by f_input() and f_inputdialog() functions. The third
14179 * argument to f_input() specifies the type of completion to use at the
14180 * prompt. The third argument to f_inputdialog() specifies the value to return
14181 * when the user cancels the prompt.
Bram Moolenaar071d4272004-06-13 20:20:40 +000014182 */
14183 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014184get_user_input(
14185 typval_T *argvars,
14186 typval_T *rettv,
14187 int inputdialog)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014188{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014189 char_u *prompt = get_tv_string_chk(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014190 char_u *p = NULL;
14191 int c;
14192 char_u buf[NUMBUFLEN];
14193 int cmd_silent_save = cmd_silent;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014194 char_u *defstr = (char_u *)"";
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014195 int xp_type = EXPAND_NOTHING;
14196 char_u *xp_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014197
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014198 rettv->v_type = VAR_STRING;
Bram Moolenaarce85c562007-09-16 12:21:16 +000014199 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014200
14201#ifdef NO_CONSOLE_INPUT
14202 /* While starting up, there is no place to enter text. */
14203 if (no_console_input())
Bram Moolenaar071d4272004-06-13 20:20:40 +000014204 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014205#endif
14206
14207 cmd_silent = FALSE; /* Want to see the prompt. */
14208 if (prompt != NULL)
14209 {
14210 /* Only the part of the message after the last NL is considered as
14211 * prompt for the command line */
14212 p = vim_strrchr(prompt, '\n');
14213 if (p == NULL)
14214 p = prompt;
14215 else
14216 {
14217 ++p;
14218 c = *p;
14219 *p = NUL;
14220 msg_start();
14221 msg_clr_eos();
14222 msg_puts_attr(prompt, echo_attr);
14223 msg_didout = FALSE;
14224 msg_starthere();
14225 *p = c;
14226 }
14227 cmdline_row = msg_row;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014228
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014229 if (argvars[1].v_type != VAR_UNKNOWN)
14230 {
14231 defstr = get_tv_string_buf_chk(&argvars[1], buf);
14232 if (defstr != NULL)
14233 stuffReadbuffSpec(defstr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014234
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014235 if (!inputdialog && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar4463f292005-09-25 22:20:24 +000014236 {
14237 char_u *xp_name;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000014238 int xp_namelen;
Bram Moolenaar4463f292005-09-25 22:20:24 +000014239 long argt;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014240
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014241 /* input() with a third argument: completion */
Bram Moolenaar4463f292005-09-25 22:20:24 +000014242 rettv->vval.v_string = NULL;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014243
Bram Moolenaar4463f292005-09-25 22:20:24 +000014244 xp_name = get_tv_string_buf_chk(&argvars[2], buf);
14245 if (xp_name == NULL)
14246 return;
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014247
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014248 xp_namelen = (int)STRLEN(xp_name);
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014249
Bram Moolenaar4463f292005-09-25 22:20:24 +000014250 if (parse_compl_arg(xp_name, xp_namelen, &xp_type, &argt,
14251 &xp_arg) == FAIL)
14252 return;
14253 }
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014254 }
14255
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014256 if (defstr != NULL)
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014257 {
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014258 int save_ex_normal_busy = ex_normal_busy;
14259 ex_normal_busy = 0;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014260 rettv->vval.v_string =
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014261 getcmdline_prompt(inputsecret_flag ? NUL : '@', p, echo_attr,
14262 xp_type, xp_arg);
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014263 ex_normal_busy = save_ex_normal_busy;
Bram Moolenaar35a7c682013-10-02 16:46:28 +020014264 }
Bram Moolenaar04b27512012-08-08 14:33:21 +020014265 if (inputdialog && rettv->vval.v_string == NULL
Bram Moolenaarb5c9cb52012-07-16 19:27:29 +020014266 && argvars[1].v_type != VAR_UNKNOWN
14267 && argvars[2].v_type != VAR_UNKNOWN)
14268 rettv->vval.v_string = vim_strsave(get_tv_string_buf(
14269 &argvars[2], buf));
Bram Moolenaarbfd8fc02005-09-20 23:22:24 +000014270
14271 vim_free(xp_arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014272
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014273 /* since the user typed this, no need to wait for return */
14274 need_wait_return = FALSE;
14275 msg_didout = FALSE;
14276 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000014277 cmd_silent = cmd_silent_save;
14278}
14279
14280/*
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014281 * "input()" function
14282 * Also handles inputsecret() when inputsecret is set.
14283 */
14284 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014285f_input(typval_T *argvars, typval_T *rettv)
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014286{
14287 get_user_input(argvars, rettv, FALSE);
14288}
14289
14290/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014291 * "inputdialog()" function
14292 */
14293 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014294f_inputdialog(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014295{
14296#if defined(FEAT_GUI_TEXTDIALOG)
14297 /* Use a GUI dialog if the GUI is running and 'c' is not in 'guioptions' */
14298 if (gui.in_use && vim_strchr(p_go, GO_CONDIALOG) == NULL)
14299 {
14300 char_u *message;
14301 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014302 char_u *defstr = (char_u *)"";
Bram Moolenaar071d4272004-06-13 20:20:40 +000014303
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014304 message = get_tv_string_chk(&argvars[0]);
14305 if (argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaarc05f93f2006-05-02 22:09:31 +000014306 && (defstr = get_tv_string_buf_chk(&argvars[1], buf)) != NULL)
Bram Moolenaarce0842a2005-07-18 21:58:11 +000014307 vim_strncpy(IObuff, defstr, IOSIZE - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014308 else
14309 IObuff[0] = NUL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014310 if (message != NULL && defstr != NULL
14311 && do_dialog(VIM_QUESTION, NULL, message,
Bram Moolenaard2c340a2011-01-17 20:08:11 +010014312 (char_u *)_("&OK\n&Cancel"), 1, IObuff, FALSE) == 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014313 rettv->vval.v_string = vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014314 else
14315 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014316 if (message != NULL && defstr != NULL
14317 && argvars[1].v_type != VAR_UNKNOWN
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014318 && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014319 rettv->vval.v_string = vim_strsave(
14320 get_tv_string_buf(&argvars[2], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014321 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014322 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014323 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014324 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014325 }
14326 else
14327#endif
Bram Moolenaarecbaf552006-07-13 06:31:00 +000014328 get_user_input(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014329}
14330
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014331/*
14332 * "inputlist()" function
14333 */
14334 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014335f_inputlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014336{
14337 listitem_T *li;
14338 int selected;
14339 int mouse_used;
14340
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014341#ifdef NO_CONSOLE_INPUT
14342 /* While starting up, there is no place to enter text. */
14343 if (no_console_input())
14344 return;
14345#endif
14346 if (argvars[0].v_type != VAR_LIST || argvars[0].vval.v_list == NULL)
14347 {
14348 EMSG2(_(e_listarg), "inputlist()");
14349 return;
14350 }
14351
14352 msg_start();
Bram Moolenaar412f7442006-07-23 19:51:57 +000014353 msg_row = Rows - 1; /* for when 'cmdheight' > 1 */
Bram Moolenaar6efa2b32005-09-10 19:26:26 +000014354 lines_left = Rows; /* avoid more prompt */
14355 msg_scroll = TRUE;
14356 msg_clr_eos();
14357
14358 for (li = argvars[0].vval.v_list->lv_first; li != NULL; li = li->li_next)
14359 {
14360 msg_puts(get_tv_string(&li->li_tv));
14361 msg_putchar('\n');
14362 }
14363
14364 /* Ask for choice. */
14365 selected = prompt_for_number(&mouse_used);
14366 if (mouse_used)
14367 selected -= lines_left;
14368
14369 rettv->vval.v_number = selected;
14370}
14371
14372
Bram Moolenaar071d4272004-06-13 20:20:40 +000014373static garray_T ga_userinput = {0, 0, sizeof(tasave_T), 4, NULL};
14374
14375/*
14376 * "inputrestore()" function
14377 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014378 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014379f_inputrestore(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014380{
14381 if (ga_userinput.ga_len > 0)
14382 {
14383 --ga_userinput.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014384 restore_typeahead((tasave_T *)(ga_userinput.ga_data)
14385 + ga_userinput.ga_len);
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014386 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014387 }
14388 else if (p_verbose > 1)
14389 {
Bram Moolenaar54ee7752005-05-31 22:22:17 +000014390 verb_msg((char_u *)_("called inputrestore() more often than inputsave()"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014391 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014392 }
14393}
14394
14395/*
14396 * "inputsave()" function
14397 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014398 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014399f_inputsave(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014400{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000014401 /* Add an entry to the stack of typeahead storage. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014402 if (ga_grow(&ga_userinput, 1) == OK)
14403 {
14404 save_typeahead((tasave_T *)(ga_userinput.ga_data)
14405 + ga_userinput.ga_len);
14406 ++ga_userinput.ga_len;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014407 /* default return is zero == OK */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014408 }
14409 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014410 rettv->vval.v_number = 1; /* Failed */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014411}
14412
14413/*
14414 * "inputsecret()" function
14415 */
14416 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014417f_inputsecret(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014418{
14419 ++cmdline_star;
14420 ++inputsecret_flag;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014421 f_input(argvars, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014422 --cmdline_star;
14423 --inputsecret_flag;
14424}
14425
14426/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014427 * "insert()" function
14428 */
14429 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014430f_insert(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014431{
14432 long before = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000014433 listitem_T *item;
14434 list_T *l;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014435 int error = FALSE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014436
14437 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014438 EMSG2(_(e_listarg), "insert()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014439 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020014440 && !tv_check_lock(l->lv_lock, (char_u *)N_("insert() argument"), TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014441 {
14442 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014443 before = get_tv_number_chk(&argvars[2], &error);
14444 if (error)
14445 return; /* type error; errmsg already given */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014446
Bram Moolenaar758711c2005-02-02 23:11:38 +000014447 if (before == l->lv_len)
14448 item = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014449 else
14450 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014451 item = list_find(l, before);
14452 if (item == NULL)
14453 {
14454 EMSGN(_(e_listidx), before);
14455 l = NULL;
14456 }
14457 }
14458 if (l != NULL)
14459 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014460 list_insert_tv(l, &argvars[1], item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000014461 copy_tv(&argvars[0], rettv);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014462 }
14463 }
14464}
14465
14466/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014467 * "invert(expr)" function
14468 */
14469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014470f_invert(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010014471{
14472 rettv->vval.v_number = ~get_tv_number_chk(&argvars[0], NULL);
14473}
14474
14475/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014476 * "isdirectory()" function
14477 */
14478 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014479f_isdirectory(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014480{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014481 rettv->vval.v_number = mch_isdir(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000014482}
14483
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014484/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014485 * "islocked()" function
14486 */
14487 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014488f_islocked(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014489{
14490 lval_T lv;
14491 char_u *end;
14492 dictitem_T *di;
14493
14494 rettv->vval.v_number = -1;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014495 end = get_lval(get_tv_string(&argvars[0]), NULL, &lv, FALSE, FALSE,
14496 GLV_NO_AUTOLOAD, FNE_CHECK_START);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014497 if (end != NULL && lv.ll_name != NULL)
14498 {
14499 if (*end != NUL)
14500 EMSG(_(e_trailing));
14501 else
14502 {
14503 if (lv.ll_tv == NULL)
14504 {
14505 if (check_changedtick(lv.ll_name))
14506 rettv->vval.v_number = 1; /* always locked */
14507 else
14508 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010014509 di = find_var(lv.ll_name, NULL, TRUE);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014510 if (di != NULL)
14511 {
14512 /* Consider a variable locked when:
14513 * 1. the variable itself is locked
14514 * 2. the value of the variable is locked.
14515 * 3. the List or Dict value is locked.
14516 */
14517 rettv->vval.v_number = ((di->di_flags & DI_FLAGS_LOCK)
14518 || tv_islocked(&di->di_tv));
14519 }
14520 }
14521 }
14522 else if (lv.ll_range)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000014523 EMSG(_("E786: Range not allowed"));
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014524 else if (lv.ll_newkey != NULL)
14525 EMSG2(_(e_dictkey), lv.ll_newkey);
14526 else if (lv.ll_list != NULL)
14527 /* List item. */
14528 rettv->vval.v_number = tv_islocked(&lv.ll_li->li_tv);
14529 else
14530 /* Dictionary item. */
14531 rettv->vval.v_number = tv_islocked(&lv.ll_di->di_tv);
14532 }
14533 }
14534
14535 clear_lval(&lv);
14536}
14537
Bram Moolenaarf1b6ac72016-02-23 21:26:43 +010014538#if defined(FEAT_FLOAT) && defined(HAVE_MATH_H)
14539/*
14540 * "isnan()" function
14541 */
14542 static void
14543f_isnan(typval_T *argvars, typval_T *rettv)
14544{
14545 rettv->vval.v_number = argvars[0].v_type == VAR_FLOAT
14546 && isnan(argvars[0].vval.v_float);
14547}
14548#endif
14549
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014550static void dict_list(typval_T *argvars, typval_T *rettv, int what);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014551
14552/*
14553 * Turn a dict into a list:
14554 * "what" == 0: list of keys
14555 * "what" == 1: list of values
14556 * "what" == 2: list of items
14557 */
14558 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014559dict_list(typval_T *argvars, typval_T *rettv, int what)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014560{
Bram Moolenaar33570922005-01-25 22:26:29 +000014561 list_T *l2;
14562 dictitem_T *di;
14563 hashitem_T *hi;
14564 listitem_T *li;
14565 listitem_T *li2;
14566 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014567 int todo;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014568
Bram Moolenaar8c711452005-01-14 21:53:12 +000014569 if (argvars[0].v_type != VAR_DICT)
14570 {
14571 EMSG(_(e_dictreq));
14572 return;
14573 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014574 if ((d = argvars[0].vval.v_dict) == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014575 return;
14576
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014577 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014578 return;
Bram Moolenaar8c711452005-01-14 21:53:12 +000014579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000014580 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000014581 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014582 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar8c711452005-01-14 21:53:12 +000014584 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014585 --todo;
14586 di = HI2DI(hi);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014587
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014588 li = listitem_alloc();
14589 if (li == NULL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014590 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000014591 list_append(rettv->vval.v_list, li);
Bram Moolenaar8c711452005-01-14 21:53:12 +000014592
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014593 if (what == 0)
14594 {
14595 /* keys() */
14596 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014597 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014598 li->li_tv.vval.v_string = vim_strsave(di->di_key);
14599 }
14600 else if (what == 1)
14601 {
14602 /* values() */
14603 copy_tv(&di->di_tv, &li->li_tv);
14604 }
14605 else
14606 {
14607 /* items() */
14608 l2 = list_alloc();
14609 li->li_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014610 li->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014611 li->li_tv.vval.v_list = l2;
14612 if (l2 == NULL)
14613 break;
14614 ++l2->lv_refcount;
14615
14616 li2 = listitem_alloc();
14617 if (li2 == NULL)
14618 break;
14619 list_append(l2, li2);
14620 li2->li_tv.v_type = VAR_STRING;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000014621 li2->li_tv.v_lock = 0;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000014622 li2->li_tv.vval.v_string = vim_strsave(di->di_key);
14623
14624 li2 = listitem_alloc();
14625 if (li2 == NULL)
14626 break;
14627 list_append(l2, li2);
14628 copy_tv(&di->di_tv, &li2->li_tv);
14629 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000014630 }
14631 }
14632}
14633
14634/*
14635 * "items(dict)" function
14636 */
14637 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014638f_items(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014639{
14640 dict_list(argvars, rettv, 2);
14641}
14642
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010014643#if defined(FEAT_JOB_CHANNEL) || defined(PROTO)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014644/*
14645 * Get the job from the argument.
14646 * Returns NULL if the job is invalid.
14647 */
14648 static job_T *
14649get_job_arg(typval_T *tv)
14650{
14651 job_T *job;
14652
14653 if (tv->v_type != VAR_JOB)
14654 {
14655 EMSG2(_(e_invarg2), get_tv_string(tv));
14656 return NULL;
14657 }
14658 job = tv->vval.v_job;
14659
14660 if (job == NULL)
14661 EMSG(_("E916: not a valid job"));
14662 return job;
14663}
Bram Moolenaarfa4bce72016-02-13 23:50:08 +010014664
Bram Moolenaar835dc632016-02-07 14:27:38 +010014665/*
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014666 * "job_getchannel()" function
14667 */
14668 static void
14669f_job_getchannel(typval_T *argvars, typval_T *rettv)
14670{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014671 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014672
Bram Moolenaar65edff82016-02-21 16:40:11 +010014673 if (job != NULL)
14674 {
Bram Moolenaar77073442016-02-13 23:23:53 +010014675 rettv->v_type = VAR_CHANNEL;
14676 rettv->vval.v_channel = job->jv_channel;
14677 if (job->jv_channel != NULL)
14678 ++job->jv_channel->ch_refcount;
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014679 }
14680}
14681
14682/*
Bram Moolenaar8950a562016-03-12 15:22:55 +010014683 * "job_info()" function
14684 */
14685 static void
14686f_job_info(typval_T *argvars, typval_T *rettv)
14687{
14688 job_T *job = get_job_arg(&argvars[0]);
14689
14690 if (job != NULL && rettv_dict_alloc(rettv) != FAIL)
14691 job_info(job, rettv->vval.v_dict);
14692}
14693
14694/*
Bram Moolenaar65edff82016-02-21 16:40:11 +010014695 * "job_setoptions()" function
14696 */
14697 static void
14698f_job_setoptions(typval_T *argvars, typval_T *rettv UNUSED)
14699{
14700 job_T *job = get_job_arg(&argvars[0]);
14701 jobopt_T opt;
14702
14703 if (job == NULL)
14704 return;
14705 clear_job_options(&opt);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014706 if (get_job_options(&argvars[1], &opt, JO_STOPONEXIT + JO_EXIT_CB) == FAIL)
Bram Moolenaar65edff82016-02-21 16:40:11 +010014707 return;
14708 job_set_options(job, &opt);
14709}
14710
14711/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014712 * "job_start()" function
14713 */
14714 static void
Bram Moolenaar151f6562016-03-07 21:19:38 +010014715f_job_start(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014716{
Bram Moolenaar835dc632016-02-07 14:27:38 +010014717 rettv->v_type = VAR_JOB;
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014718 rettv->vval.v_job = job_start(argvars);
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010014719}
14720
14721/*
Bram Moolenaar835dc632016-02-07 14:27:38 +010014722 * "job_status()" function
14723 */
14724 static void
Bram Moolenaar6463ca22016-02-13 17:04:46 +010014725f_job_status(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014726{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014727 job_T *job = get_job_arg(&argvars[0]);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014728
Bram Moolenaar65edff82016-02-21 16:40:11 +010014729 if (job != NULL)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014730 {
Bram Moolenaar835dc632016-02-07 14:27:38 +010014731 rettv->v_type = VAR_STRING;
Bram Moolenaar8950a562016-03-12 15:22:55 +010014732 rettv->vval.v_string = vim_strsave((char_u *)job_status(job));
Bram Moolenaar835dc632016-02-07 14:27:38 +010014733 }
14734}
14735
14736/*
14737 * "job_stop()" function
14738 */
14739 static void
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014740f_job_stop(typval_T *argvars, typval_T *rettv)
Bram Moolenaar835dc632016-02-07 14:27:38 +010014741{
Bram Moolenaar65edff82016-02-21 16:40:11 +010014742 job_T *job = get_job_arg(&argvars[0]);
14743
14744 if (job != NULL)
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010014745 rettv->vval.v_number = job_stop(job, argvars);
Bram Moolenaar835dc632016-02-07 14:27:38 +010014746}
14747#endif
14748
Bram Moolenaar071d4272004-06-13 20:20:40 +000014749/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014750 * "join()" function
14751 */
14752 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014753f_join(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014754{
14755 garray_T ga;
14756 char_u *sep;
14757
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014758 if (argvars[0].v_type != VAR_LIST)
14759 {
14760 EMSG(_(e_listreq));
14761 return;
14762 }
14763 if (argvars[0].vval.v_list == NULL)
14764 return;
14765 if (argvars[1].v_type == VAR_UNKNOWN)
14766 sep = (char_u *)" ";
14767 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014768 sep = get_tv_string_chk(&argvars[1]);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014769
14770 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014771
14772 if (sep != NULL)
14773 {
14774 ga_init2(&ga, (int)sizeof(char), 80);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000014775 list_join(&ga, argvars[0].vval.v_list, sep, TRUE, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000014776 ga_append(&ga, NUL);
14777 rettv->vval.v_string = (char_u *)ga.ga_data;
14778 }
14779 else
14780 rettv->vval.v_string = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000014781}
14782
14783/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014784 * "js_decode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014785 */
14786 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014787f_js_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014788{
14789 js_read_T reader;
14790
14791 reader.js_buf = get_tv_string(&argvars[0]);
14792 reader.js_fill = NULL;
14793 reader.js_used = 0;
14794 if (json_decode_all(&reader, rettv, JSON_JS) != OK)
14795 EMSG(_(e_invarg));
14796}
14797
14798/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014799 * "js_encode()" function
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014800 */
14801 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014802f_js_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014803{
14804 rettv->v_type = VAR_STRING;
14805 rettv->vval.v_string = json_encode(&argvars[0], JSON_JS);
14806}
14807
14808/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014809 * "json_decode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014810 */
14811 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014812f_json_decode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014813{
14814 js_read_T reader;
14815
14816 reader.js_buf = get_tv_string(&argvars[0]);
Bram Moolenaar56ead342016-02-02 18:20:08 +010014817 reader.js_fill = NULL;
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014818 reader.js_used = 0;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014819 if (json_decode_all(&reader, rettv, 0) != OK)
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010014820 EMSG(_(e_invarg));
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014821}
14822
14823/*
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014824 * "json_encode()" function
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014825 */
14826 static void
Bram Moolenaar7823a3b2016-02-11 21:08:32 +010014827f_json_encode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014828{
14829 rettv->v_type = VAR_STRING;
Bram Moolenaar595e64e2016-02-07 19:19:53 +010014830 rettv->vval.v_string = json_encode(&argvars[0], 0);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010014831}
14832
14833/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000014834 * "keys()" function
14835 */
14836 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014837f_keys(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000014838{
14839 dict_list(argvars, rettv, 0);
14840}
14841
14842/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014843 * "last_buffer_nr()" function.
14844 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014846f_last_buffer_nr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014847{
14848 int n = 0;
14849 buf_T *buf;
14850
14851 for (buf = firstbuf; buf != NULL; buf = buf->b_next)
14852 if (n < buf->b_fnum)
14853 n = buf->b_fnum;
14854
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014855 rettv->vval.v_number = n;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014856}
14857
14858/*
14859 * "len()" function
14860 */
14861 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014862f_len(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014863{
14864 switch (argvars[0].v_type)
14865 {
14866 case VAR_STRING:
14867 case VAR_NUMBER:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014868 rettv->vval.v_number = (varnumber_T)STRLEN(
14869 get_tv_string(&argvars[0]));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014870 break;
14871 case VAR_LIST:
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014872 rettv->vval.v_number = list_len(argvars[0].vval.v_list);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014873 break;
Bram Moolenaare9a41262005-01-15 22:18:47 +000014874 case VAR_DICT:
14875 rettv->vval.v_number = dict_len(argvars[0].vval.v_dict);
14876 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010014877 case VAR_UNKNOWN:
14878 case VAR_SPECIAL:
14879 case VAR_FLOAT:
14880 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010014881 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010014882 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010014883 case VAR_CHANNEL:
Bram Moolenaare49b69a2005-01-08 16:11:57 +000014884 EMSG(_("E701: Invalid type for len()"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014885 break;
14886 }
14887}
14888
Bram Moolenaar48e697e2016-01-23 22:17:30 +010014889static void libcall_common(typval_T *argvars, typval_T *rettv, int type);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014890
14891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014892libcall_common(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014893{
14894#ifdef FEAT_LIBCALL
14895 char_u *string_in;
14896 char_u **string_result;
14897 int nr_result;
14898#endif
14899
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014900 rettv->v_type = type;
Bram Moolenaar798b30b2009-04-22 10:56:16 +000014901 if (type != VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014902 rettv->vval.v_string = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014903
14904 if (check_restricted() || check_secure())
14905 return;
14906
14907#ifdef FEAT_LIBCALL
14908 /* The first two args must be strings, otherwise its meaningless */
14909 if (argvars[0].v_type == VAR_STRING && argvars[1].v_type == VAR_STRING)
14910 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000014911 string_in = NULL;
14912 if (argvars[2].v_type == VAR_STRING)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014913 string_in = argvars[2].vval.v_string;
14914 if (type == VAR_NUMBER)
14915 string_result = NULL;
14916 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014917 string_result = &rettv->vval.v_string;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014918 if (mch_libcall(argvars[0].vval.v_string,
14919 argvars[1].vval.v_string,
14920 string_in,
14921 argvars[2].vval.v_number,
14922 string_result,
14923 &nr_result) == OK
14924 && type == VAR_NUMBER)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014925 rettv->vval.v_number = nr_result;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000014926 }
14927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000014928}
14929
14930/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000014931 * "libcall()" function
14932 */
14933 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014934f_libcall(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014935{
14936 libcall_common(argvars, rettv, VAR_STRING);
14937}
14938
14939/*
14940 * "libcallnr()" function
14941 */
14942 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014943f_libcallnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000014944{
14945 libcall_common(argvars, rettv, VAR_NUMBER);
14946}
14947
14948/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000014949 * "line(string)" function
14950 */
14951 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014952f_line(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014953{
14954 linenr_T lnum = 0;
14955 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014956 int fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014957
Bram Moolenaar0e34f622006-03-03 23:00:03 +000014958 fp = var2fpos(&argvars[0], TRUE, &fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014959 if (fp != NULL)
14960 lnum = fp->lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014961 rettv->vval.v_number = lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014962}
14963
14964/*
14965 * "line2byte(lnum)" function
14966 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000014967 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014968f_line2byte(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014969{
14970#ifndef FEAT_BYTEOFF
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014971 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014972#else
14973 linenr_T lnum;
14974
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014975 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014976 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014977 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014978 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014979 rettv->vval.v_number = ml_find_line_or_offset(curbuf, lnum, NULL);
14980 if (rettv->vval.v_number >= 0)
14981 ++rettv->vval.v_number;
Bram Moolenaar071d4272004-06-13 20:20:40 +000014982#endif
14983}
14984
14985/*
14986 * "lispindent(lnum)" function
14987 */
14988 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010014989f_lispindent(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000014990{
14991#ifdef FEAT_LISP
14992 pos_T pos;
14993 linenr_T lnum;
14994
14995 pos = curwin->w_cursor;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000014996 lnum = get_tv_lnum(argvars);
Bram Moolenaar071d4272004-06-13 20:20:40 +000014997 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count)
14998 {
14999 curwin->w_cursor.lnum = lnum;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015000 rettv->vval.v_number = get_lisp_indent();
Bram Moolenaar071d4272004-06-13 20:20:40 +000015001 curwin->w_cursor = pos;
15002 }
15003 else
15004#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015005 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015006}
15007
15008/*
15009 * "localtime()" function
15010 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015011 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015012f_localtime(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015013{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015014 rettv->vval.v_number = (varnumber_T)time(NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015015}
15016
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015017static void get_maparg(typval_T *argvars, typval_T *rettv, int exact);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015018
15019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015020get_maparg(typval_T *argvars, typval_T *rettv, int exact)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015021{
15022 char_u *keys;
15023 char_u *which;
15024 char_u buf[NUMBUFLEN];
15025 char_u *keys_buf = NULL;
15026 char_u *rhs;
15027 int mode;
Bram Moolenaar2c932302006-03-18 21:42:09 +000015028 int abbr = FALSE;
Bram Moolenaar3fe37d62012-02-06 00:13:22 +010015029 int get_dict = FALSE;
Bram Moolenaarbd743252010-10-20 21:23:33 +020015030 mapblock_T *mp;
15031 int buffer_local;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015032
15033 /* return empty string for failure */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015034 rettv->v_type = VAR_STRING;
15035 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015036
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015037 keys = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015038 if (*keys == NUL)
15039 return;
15040
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015041 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaar2c932302006-03-18 21:42:09 +000015042 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015043 which = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar2c932302006-03-18 21:42:09 +000015044 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarbd743252010-10-20 21:23:33 +020015045 {
Bram Moolenaar2c932302006-03-18 21:42:09 +000015046 abbr = get_tv_number(&argvars[2]);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015047 if (argvars[3].v_type != VAR_UNKNOWN)
15048 get_dict = get_tv_number(&argvars[3]);
15049 }
Bram Moolenaar2c932302006-03-18 21:42:09 +000015050 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015051 else
15052 which = (char_u *)"";
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015053 if (which == NULL)
15054 return;
15055
Bram Moolenaar071d4272004-06-13 20:20:40 +000015056 mode = get_map_mode(&which, 0);
15057
Bram Moolenaar3fb9eda2006-05-03 21:29:58 +000015058 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, FALSE);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015059 rhs = check_map(keys, mode, exact, FALSE, abbr, &mp, &buffer_local);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015060 vim_free(keys_buf);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015061
15062 if (!get_dict)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015063 {
Bram Moolenaarbd743252010-10-20 21:23:33 +020015064 /* Return a string. */
15065 if (rhs != NULL)
15066 rettv->vval.v_string = str2special_save(rhs, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015067
Bram Moolenaarbd743252010-10-20 21:23:33 +020015068 }
15069 else if (rettv_dict_alloc(rettv) != FAIL && rhs != NULL)
15070 {
15071 /* Return a dictionary. */
15072 char_u *lhs = str2special_save(mp->m_keys, TRUE);
15073 char_u *mapmode = map_mode_to_chars(mp->m_mode);
15074 dict_T *dict = rettv->vval.v_dict;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015075
Bram Moolenaarbd743252010-10-20 21:23:33 +020015076 dict_add_nr_str(dict, "lhs", 0L, lhs);
15077 dict_add_nr_str(dict, "rhs", 0L, mp->m_orig_str);
15078 dict_add_nr_str(dict, "noremap", mp->m_noremap ? 1L : 0L , NULL);
15079 dict_add_nr_str(dict, "expr", mp->m_expr ? 1L : 0L, NULL);
15080 dict_add_nr_str(dict, "silent", mp->m_silent ? 1L : 0L, NULL);
15081 dict_add_nr_str(dict, "sid", (long)mp->m_script_ID, NULL);
15082 dict_add_nr_str(dict, "buffer", (long)buffer_local, NULL);
Bram Moolenaar72179e12013-06-29 13:58:31 +020015083 dict_add_nr_str(dict, "nowait", mp->m_nowait ? 1L : 0L, NULL);
Bram Moolenaarbd743252010-10-20 21:23:33 +020015084 dict_add_nr_str(dict, "mode", 0L, mapmode);
15085
15086 vim_free(lhs);
15087 vim_free(mapmode);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015088 }
15089}
15090
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015091#ifdef FEAT_FLOAT
15092/*
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015093 * "log()" function
15094 */
15095 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015096f_log(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015097{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015098 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020015099
15100 rettv->v_type = VAR_FLOAT;
15101 if (get_float_arg(argvars, &f) == OK)
15102 rettv->vval.v_float = log(f);
15103 else
15104 rettv->vval.v_float = 0.0;
15105}
15106
15107/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015108 * "log10()" function
15109 */
15110 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015111f_log10(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015112{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015113 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015114
15115 rettv->v_type = VAR_FLOAT;
15116 if (get_float_arg(argvars, &f) == OK)
15117 rettv->vval.v_float = log10(f);
15118 else
15119 rettv->vval.v_float = 0.0;
15120}
15121#endif
15122
Bram Moolenaar1dced572012-04-05 16:54:08 +020015123#ifdef FEAT_LUA
15124/*
15125 * "luaeval()" function
15126 */
15127 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015128f_luaeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1dced572012-04-05 16:54:08 +020015129{
15130 char_u *str;
15131 char_u buf[NUMBUFLEN];
15132
15133 str = get_tv_string_buf(&argvars[0], buf);
15134 do_luaeval(str, argvars + 1, rettv);
15135}
15136#endif
15137
Bram Moolenaar071d4272004-06-13 20:20:40 +000015138/*
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015139 * "map()" function
15140 */
15141 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015142f_map(typval_T *argvars, typval_T *rettv)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015143{
15144 filter_map(argvars, rettv, TRUE);
15145}
15146
15147/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015148 * "maparg()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015149 */
15150 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015151f_maparg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015152{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015153 get_maparg(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015154}
15155
15156/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015157 * "mapcheck()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000015158 */
15159 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015160f_mapcheck(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015161{
Bram Moolenaar0d660222005-01-07 21:51:51 +000015162 get_maparg(argvars, rettv, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015163}
15164
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015165static void find_some_match(typval_T *argvars, typval_T *rettv, int start);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015166
15167 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015168find_some_match(typval_T *argvars, typval_T *rettv, int type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015169{
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015170 char_u *str = NULL;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015171 long len = 0;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015172 char_u *expr = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015173 char_u *pat;
15174 regmatch_T regmatch;
15175 char_u patbuf[NUMBUFLEN];
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015176 char_u strbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000015177 char_u *save_cpo;
15178 long start = 0;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015179 long nth = 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015180 colnr_T startcol = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015181 int match = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000015182 list_T *l = NULL;
15183 listitem_T *li = NULL;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015184 long idx = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015185 char_u *tofree = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015186
15187 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
15188 save_cpo = p_cpo;
15189 p_cpo = (char_u *)"";
15190
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015191 rettv->vval.v_number = -1;
15192 if (type == 3)
15193 {
15194 /* return empty list when there are no matches */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000015195 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015196 goto theend;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015197 }
15198 else if (type == 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015199 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015200 rettv->v_type = VAR_STRING;
15201 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015202 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015203
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015204 if (argvars[0].v_type == VAR_LIST)
15205 {
15206 if ((l = argvars[0].vval.v_list) == NULL)
15207 goto theend;
15208 li = l->lv_first;
15209 }
15210 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015211 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015212 expr = str = get_tv_string(&argvars[0]);
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015213 len = (long)STRLEN(str);
15214 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015215
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015216 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
15217 if (pat == NULL)
15218 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015219
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015220 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015221 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015222 int error = FALSE;
15223
15224 start = get_tv_number_chk(&argvars[2], &error);
15225 if (error)
15226 goto theend;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015227 if (l != NULL)
15228 {
15229 li = list_find(l, start);
15230 if (li == NULL)
15231 goto theend;
Bram Moolenaar758711c2005-02-02 23:11:38 +000015232 idx = l->lv_idx; /* use the cached index */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015233 }
15234 else
15235 {
15236 if (start < 0)
15237 start = 0;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015238 if (start > len)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015239 goto theend;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015240 /* When "count" argument is there ignore matches before "start",
15241 * otherwise skip part of the string. Differs when pattern is "^"
15242 * or "\<". */
15243 if (argvars[3].v_type != VAR_UNKNOWN)
15244 startcol = start;
15245 else
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015246 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000015247 str += start;
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015248 len -= start;
15249 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015250 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015251
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015252 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015253 nth = get_tv_number_chk(&argvars[3], &error);
15254 if (error)
15255 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015256 }
15257
15258 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
15259 if (regmatch.regprog != NULL)
15260 {
15261 regmatch.rm_ic = p_ic;
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015262
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015263 for (;;)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015264 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015265 if (l != NULL)
15266 {
15267 if (li == NULL)
15268 {
15269 match = FALSE;
15270 break;
15271 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015272 vim_free(tofree);
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015273 str = echo_string(&li->li_tv, &tofree, strbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000015274 if (str == NULL)
15275 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015276 }
15277
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000015278 match = vim_regexec_nl(&regmatch, str, (colnr_T)startcol);
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015279
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015280 if (match && --nth <= 0)
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015281 break;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015282 if (l == NULL && !match)
15283 break;
15284
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015285 /* Advance to just after the match. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015286 if (l != NULL)
15287 {
15288 li = li->li_next;
15289 ++idx;
15290 }
15291 else
15292 {
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015293#ifdef FEAT_MBYTE
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015294 startcol = (colnr_T)(regmatch.startp[0]
15295 + (*mb_ptr2len)(regmatch.startp[0]) - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015296#else
Bram Moolenaar8765a4a2010-07-27 22:41:43 +020015297 startcol = (colnr_T)(regmatch.startp[0] + 1 - str);
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015298#endif
Bram Moolenaar9feaf622014-02-22 22:18:47 +010015299 if (startcol > (colnr_T)len
15300 || str + startcol <= regmatch.startp[0])
15301 {
15302 match = FALSE;
15303 break;
15304 }
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015305 }
Bram Moolenaar89cb5e02004-07-19 20:55:54 +000015306 }
15307
15308 if (match)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015309 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015310 if (type == 3)
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015311 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015312 int i;
15313
15314 /* return list with matched string and submatches */
15315 for (i = 0; i < NSUBEXP; ++i)
15316 {
15317 if (regmatch.endp[i] == NULL)
Bram Moolenaarf9393ef2006-04-24 19:47:27 +000015318 {
15319 if (list_append_string(rettv->vval.v_list,
15320 (char_u *)"", 0) == FAIL)
15321 break;
15322 }
15323 else if (list_append_string(rettv->vval.v_list,
Bram Moolenaar4463f292005-09-25 22:20:24 +000015324 regmatch.startp[i],
15325 (int)(regmatch.endp[i] - regmatch.startp[i]))
15326 == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015327 break;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015328 }
15329 }
15330 else if (type == 2)
15331 {
15332 /* return matched string */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015333 if (l != NULL)
15334 copy_tv(&li->li_tv, rettv);
15335 else
15336 rettv->vval.v_string = vim_strnsave(regmatch.startp[0],
Bram Moolenaar071d4272004-06-13 20:20:40 +000015337 (int)(regmatch.endp[0] - regmatch.startp[0]));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000015338 }
15339 else if (l != NULL)
15340 rettv->vval.v_number = idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015341 else
15342 {
15343 if (type != 0)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015344 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015345 (varnumber_T)(regmatch.startp[0] - str);
15346 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015347 rettv->vval.v_number =
Bram Moolenaar071d4272004-06-13 20:20:40 +000015348 (varnumber_T)(regmatch.endp[0] - str);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015349 rettv->vval.v_number += (varnumber_T)(str - expr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015350 }
15351 }
Bram Moolenaar473de612013-06-08 18:19:48 +020015352 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015353 }
15354
15355theend:
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015356 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015357 p_cpo = save_cpo;
15358}
15359
15360/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015361 * "match()" function
15362 */
15363 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015364f_match(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015365{
15366 find_some_match(argvars, rettv, 1);
15367}
15368
15369/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015370 * "matchadd()" function
15371 */
15372 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015373f_matchadd(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015374{
15375#ifdef FEAT_SEARCH_EXTRA
15376 char_u buf[NUMBUFLEN];
15377 char_u *grp = get_tv_string_buf_chk(&argvars[0], buf); /* group */
15378 char_u *pat = get_tv_string_buf_chk(&argvars[1], buf); /* pattern */
15379 int prio = 10; /* default priority */
15380 int id = -1;
15381 int error = FALSE;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015382 char_u *conceal_char = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015383
15384 rettv->vval.v_number = -1;
15385
15386 if (grp == NULL || pat == NULL)
15387 return;
15388 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015389 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015390 prio = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015391 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015392 {
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015393 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015394 if (argvars[4].v_type != VAR_UNKNOWN)
15395 {
15396 if (argvars[4].v_type != VAR_DICT)
15397 {
15398 EMSG(_(e_dictreq));
15399 return;
15400 }
15401 if (dict_find(argvars[4].vval.v_dict,
15402 (char_u *)"conceal", -1) != NULL)
15403 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15404 (char_u *)"conceal", FALSE);
15405 }
15406 }
Bram Moolenaar2240aeb2007-07-27 19:33:14 +000015407 }
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015408 if (error == TRUE)
15409 return;
15410 if (id >= 1 && id <= 3)
15411 {
15412 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15413 return;
15414 }
15415
Bram Moolenaar6561d522015-07-21 15:48:27 +020015416 rettv->vval.v_number = match_add(curwin, grp, pat, prio, id, NULL,
15417 conceal_char);
Bram Moolenaarb3414592014-06-17 17:48:32 +020015418#endif
15419}
15420
15421/*
15422 * "matchaddpos()" function
15423 */
15424 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015425f_matchaddpos(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarb3414592014-06-17 17:48:32 +020015426{
15427#ifdef FEAT_SEARCH_EXTRA
15428 char_u buf[NUMBUFLEN];
15429 char_u *group;
15430 int prio = 10;
15431 int id = -1;
15432 int error = FALSE;
15433 list_T *l;
Bram Moolenaar6561d522015-07-21 15:48:27 +020015434 char_u *conceal_char = NULL;
Bram Moolenaarb3414592014-06-17 17:48:32 +020015435
15436 rettv->vval.v_number = -1;
15437
15438 group = get_tv_string_buf_chk(&argvars[0], buf);
15439 if (group == NULL)
15440 return;
15441
15442 if (argvars[1].v_type != VAR_LIST)
15443 {
15444 EMSG2(_(e_listarg), "matchaddpos()");
15445 return;
15446 }
15447 l = argvars[1].vval.v_list;
15448 if (l == NULL)
15449 return;
15450
15451 if (argvars[2].v_type != VAR_UNKNOWN)
15452 {
15453 prio = get_tv_number_chk(&argvars[2], &error);
15454 if (argvars[3].v_type != VAR_UNKNOWN)
Bram Moolenaar6561d522015-07-21 15:48:27 +020015455 {
Bram Moolenaarb3414592014-06-17 17:48:32 +020015456 id = get_tv_number_chk(&argvars[3], &error);
Bram Moolenaar6561d522015-07-21 15:48:27 +020015457 if (argvars[4].v_type != VAR_UNKNOWN)
15458 {
15459 if (argvars[4].v_type != VAR_DICT)
15460 {
15461 EMSG(_(e_dictreq));
15462 return;
15463 }
15464 if (dict_find(argvars[4].vval.v_dict,
15465 (char_u *)"conceal", -1) != NULL)
15466 conceal_char = get_dict_string(argvars[4].vval.v_dict,
15467 (char_u *)"conceal", FALSE);
15468 }
15469 }
Bram Moolenaarb3414592014-06-17 17:48:32 +020015470 }
15471 if (error == TRUE)
15472 return;
15473
15474 /* id == 3 is ok because matchaddpos() is supposed to substitute :3match */
15475 if (id == 1 || id == 2)
15476 {
15477 EMSGN("E798: ID is reserved for \":match\": %ld", id);
15478 return;
15479 }
15480
Bram Moolenaar6561d522015-07-21 15:48:27 +020015481 rettv->vval.v_number = match_add(curwin, group, NULL, prio, id, l,
15482 conceal_char);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015483#endif
15484}
15485
15486/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015487 * "matcharg()" function
15488 */
15489 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015490f_matcharg(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015491{
15492 if (rettv_list_alloc(rettv) == OK)
15493 {
15494#ifdef FEAT_SEARCH_EXTRA
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015495 int id = get_tv_number(&argvars[0]);
15496 matchitem_T *m;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015497
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015498 if (id >= 1 && id <= 3)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015499 {
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015500 if ((m = (matchitem_T *)get_match(curwin, id)) != NULL)
15501 {
15502 list_append_string(rettv->vval.v_list,
15503 syn_id2name(m->hlg_id), -1);
15504 list_append_string(rettv->vval.v_list, m->pattern, -1);
15505 }
15506 else
15507 {
Bram Moolenaar5f4c8402014-01-06 06:19:11 +010015508 list_append_string(rettv->vval.v_list, NULL, -1);
15509 list_append_string(rettv->vval.v_list, NULL, -1);
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015510 }
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015511 }
15512#endif
15513 }
15514}
15515
15516/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015517 * "matchdelete()" function
15518 */
15519 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015520f_matchdelete(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000015521{
15522#ifdef FEAT_SEARCH_EXTRA
15523 rettv->vval.v_number = match_delete(curwin,
15524 (int)get_tv_number(&argvars[0]), TRUE);
15525#endif
15526}
15527
15528/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015529 * "matchend()" function
15530 */
15531 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015532f_matchend(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015533{
15534 find_some_match(argvars, rettv, 0);
15535}
15536
15537/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015538 * "matchlist()" function
15539 */
15540 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015541f_matchlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000015542{
15543 find_some_match(argvars, rettv, 3);
15544}
15545
15546/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015547 * "matchstr()" function
15548 */
15549 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015550f_matchstr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015551{
15552 find_some_match(argvars, rettv, 2);
15553}
15554
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015555static void max_min(typval_T *argvars, typval_T *rettv, int domax);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015556
15557 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015558max_min(typval_T *argvars, typval_T *rettv, int domax)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015559{
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015560 long n = 0;
15561 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015562 int error = FALSE;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015563
15564 if (argvars[0].v_type == VAR_LIST)
15565 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015566 list_T *l;
15567 listitem_T *li;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015568
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015569 l = argvars[0].vval.v_list;
15570 if (l != NULL)
15571 {
15572 li = l->lv_first;
15573 if (li != NULL)
15574 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015575 n = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaard8e9bb22005-07-09 21:14:46 +000015576 for (;;)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015577 {
15578 li = li->li_next;
15579 if (li == NULL)
15580 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015581 i = get_tv_number_chk(&li->li_tv, &error);
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015582 if (domax ? i > n : i < n)
15583 n = i;
15584 }
15585 }
15586 }
15587 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000015588 else if (argvars[0].v_type == VAR_DICT)
15589 {
Bram Moolenaar33570922005-01-25 22:26:29 +000015590 dict_T *d;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015591 int first = TRUE;
Bram Moolenaar33570922005-01-25 22:26:29 +000015592 hashitem_T *hi;
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015593 int todo;
Bram Moolenaare9a41262005-01-15 22:18:47 +000015594
15595 d = argvars[0].vval.v_dict;
15596 if (d != NULL)
15597 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000015598 todo = (int)d->dv_hashtab.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000015599 for (hi = d->dv_hashtab.ht_array; todo > 0; ++hi)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015600 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015601 if (!HASHITEM_EMPTY(hi))
Bram Moolenaare9a41262005-01-15 22:18:47 +000015602 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015603 --todo;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015604 i = get_tv_number_chk(&HI2DI(hi)->di_tv, &error);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000015605 if (first)
15606 {
15607 n = i;
15608 first = FALSE;
15609 }
15610 else if (domax ? i > n : i < n)
Bram Moolenaare9a41262005-01-15 22:18:47 +000015611 n = i;
15612 }
15613 }
15614 }
15615 }
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015616 else
Bram Moolenaar758711c2005-02-02 23:11:38 +000015617 EMSG(_(e_listdictarg));
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015618 rettv->vval.v_number = error ? 0 : n;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015619}
15620
15621/*
15622 * "max()" function
15623 */
15624 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015625f_max(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015626{
15627 max_min(argvars, rettv, TRUE);
15628}
15629
15630/*
15631 * "min()" function
15632 */
15633 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015634f_min(typval_T *argvars, typval_T *rettv)
Bram Moolenaar6cc16192005-01-08 21:49:45 +000015635{
15636 max_min(argvars, rettv, FALSE);
15637}
15638
Bram Moolenaar48e697e2016-01-23 22:17:30 +010015639static int mkdir_recurse(char_u *dir, int prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015640
15641/*
15642 * Create the directory in which "dir" is located, and higher levels when
15643 * needed.
15644 */
15645 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010015646mkdir_recurse(char_u *dir, int prot)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015647{
15648 char_u *p;
15649 char_u *updir;
15650 int r = FAIL;
15651
15652 /* Get end of directory name in "dir".
15653 * We're done when it's "/" or "c:/". */
15654 p = gettail_sep(dir);
15655 if (p <= get_past_head(dir))
15656 return OK;
15657
15658 /* If the directory exists we're done. Otherwise: create it.*/
15659 updir = vim_strnsave(dir, (int)(p - dir));
15660 if (updir == NULL)
15661 return FAIL;
15662 if (mch_isdir(updir))
15663 r = OK;
15664 else if (mkdir_recurse(updir, prot) == OK)
15665 r = vim_mkdir_emsg(updir, prot);
15666 vim_free(updir);
15667 return r;
15668}
15669
15670#ifdef vim_mkdir
15671/*
15672 * "mkdir()" function
15673 */
15674 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015675f_mkdir(typval_T *argvars, typval_T *rettv)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015676{
15677 char_u *dir;
15678 char_u buf[NUMBUFLEN];
15679 int prot = 0755;
15680
15681 rettv->vval.v_number = FAIL;
15682 if (check_restricted() || check_secure())
15683 return;
15684
15685 dir = get_tv_string_buf(&argvars[0], buf);
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015686 if (*dir == NUL)
15687 rettv->vval.v_number = FAIL;
15688 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015689 {
Bram Moolenaar195ef0c2013-08-30 16:00:08 +020015690 if (*gettail(dir) == NUL)
15691 /* remove trailing slashes */
15692 *gettail_sep(dir) = NUL;
15693
15694 if (argvars[1].v_type != VAR_UNKNOWN)
15695 {
15696 if (argvars[2].v_type != VAR_UNKNOWN)
15697 prot = get_tv_number_chk(&argvars[2], NULL);
15698 if (prot != -1 && STRCMP(get_tv_string(&argvars[1]), "p") == 0)
15699 mkdir_recurse(dir, prot);
15700 }
15701 rettv->vval.v_number = prot == -1 ? FAIL : vim_mkdir_emsg(dir, prot);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015702 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000015703}
15704#endif
15705
Bram Moolenaar0d660222005-01-07 21:51:51 +000015706/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015707 * "mode()" function
15708 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000015709 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015710f_mode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015711{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015712 char_u buf[3];
15713
15714 buf[1] = NUL;
15715 buf[2] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015716
Bram Moolenaar071d4272004-06-13 20:20:40 +000015717 if (VIsual_active)
15718 {
15719 if (VIsual_select)
15720 buf[0] = VIsual_mode + 's' - 'v';
15721 else
15722 buf[0] = VIsual_mode;
15723 }
Bram Moolenaarf7ff6e82014-03-23 15:13:05 +010015724 else if (State == HITRETURN || State == ASKMORE || State == SETWSIZE
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015725 || State == CONFIRM)
15726 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015727 buf[0] = 'r';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015728 if (State == ASKMORE)
15729 buf[1] = 'm';
15730 else if (State == CONFIRM)
15731 buf[1] = '?';
15732 }
15733 else if (State == EXTERNCMD)
15734 buf[0] = '!';
Bram Moolenaar071d4272004-06-13 20:20:40 +000015735 else if (State & INSERT)
15736 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015737#ifdef FEAT_VREPLACE
15738 if (State & VREPLACE_FLAG)
15739 {
15740 buf[0] = 'R';
15741 buf[1] = 'v';
15742 }
15743 else
15744#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000015745 if (State & REPLACE_FLAG)
15746 buf[0] = 'R';
15747 else
15748 buf[0] = 'i';
15749 }
15750 else if (State & CMDLINE)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015751 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015752 buf[0] = 'c';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015753 if (exmode_active)
15754 buf[1] = 'v';
15755 }
15756 else if (exmode_active)
15757 {
15758 buf[0] = 'c';
15759 buf[1] = 'e';
15760 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015761 else
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015762 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000015763 buf[0] = 'n';
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015764 if (finish_op)
15765 buf[1] = 'o';
15766 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015767
Bram Moolenaar05bb9532008-07-04 09:44:11 +000015768 /* Clear out the minor mode when the argument is not a non-zero number or
15769 * non-empty string. */
15770 if (!non_zero_arg(&argvars[0]))
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015771 buf[1] = NUL;
15772
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015773 rettv->vval.v_string = vim_strsave(buf);
15774 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000015775}
15776
Bram Moolenaar429fa852013-04-15 12:27:36 +020015777#if defined(FEAT_MZSCHEME) || defined(PROTO)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015778/*
15779 * "mzeval()" function
15780 */
15781 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015782f_mzeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015783{
15784 char_u *str;
15785 char_u buf[NUMBUFLEN];
15786
15787 str = get_tv_string_buf(&argvars[0], buf);
15788 do_mzeval(str, rettv);
15789}
Bram Moolenaar75676462013-01-30 14:55:42 +010015790
15791 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015792mzscheme_call_vim(char_u *name, typval_T *args, typval_T *rettv)
Bram Moolenaar75676462013-01-30 14:55:42 +010015793{
15794 typval_T argvars[3];
15795
15796 argvars[0].v_type = VAR_STRING;
15797 argvars[0].vval.v_string = name;
15798 copy_tv(args, &argvars[1]);
15799 argvars[2].v_type = VAR_UNKNOWN;
15800 f_call(argvars, rettv);
15801 clear_tv(&argvars[1]);
15802}
Bram Moolenaar7e506b62010-01-19 15:55:06 +010015803#endif
15804
Bram Moolenaar071d4272004-06-13 20:20:40 +000015805/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015806 * "nextnonblank()" function
15807 */
15808 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015809f_nextnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015810{
15811 linenr_T lnum;
15812
15813 for (lnum = get_tv_lnum(argvars); ; ++lnum)
15814 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000015815 if (lnum < 0 || lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015816 {
15817 lnum = 0;
15818 break;
15819 }
15820 if (*skipwhite(ml_get(lnum)) != NUL)
15821 break;
15822 }
15823 rettv->vval.v_number = lnum;
15824}
15825
15826/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000015827 * "nr2char()" function
15828 */
15829 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015830f_nr2char(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000015831{
15832 char_u buf[NUMBUFLEN];
15833
15834#ifdef FEAT_MBYTE
15835 if (has_mbyte)
Bram Moolenaard35d7842013-01-23 17:17:10 +010015836 {
15837 int utf8 = 0;
15838
15839 if (argvars[1].v_type != VAR_UNKNOWN)
15840 utf8 = get_tv_number_chk(&argvars[1], NULL);
15841 if (utf8)
15842 buf[(*utf_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15843 else
15844 buf[(*mb_char2bytes)((int)get_tv_number(&argvars[0]), buf)] = NUL;
15845 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000015846 else
15847#endif
15848 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015849 buf[0] = (char_u)get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000015850 buf[1] = NUL;
15851 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000015852 rettv->v_type = VAR_STRING;
15853 rettv->vval.v_string = vim_strsave(buf);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000015854}
15855
15856/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015857 * "or(expr, expr)" function
15858 */
15859 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015860f_or(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010015861{
15862 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
15863 | get_tv_number_chk(&argvars[1], NULL);
15864}
15865
15866/*
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015867 * "pathshorten()" function
15868 */
15869 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015870f_pathshorten(typval_T *argvars, typval_T *rettv)
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015871{
15872 char_u *p;
15873
15874 rettv->v_type = VAR_STRING;
15875 p = get_tv_string_chk(&argvars[0]);
15876 if (p == NULL)
15877 rettv->vval.v_string = NULL;
15878 else
15879 {
15880 p = vim_strsave(p);
15881 rettv->vval.v_string = p;
15882 if (p != NULL)
15883 shorten_dir(p);
15884 }
15885}
15886
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015887#ifdef FEAT_PERL
15888/*
15889 * "perleval()" function
15890 */
15891 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015892f_perleval(typval_T *argvars, typval_T *rettv)
Bram Moolenaare9b892e2016-01-17 21:15:58 +010015893{
15894 char_u *str;
15895 char_u buf[NUMBUFLEN];
15896
15897 str = get_tv_string_buf(&argvars[0], buf);
15898 do_perleval(str, rettv);
15899}
15900#endif
15901
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015902#ifdef FEAT_FLOAT
15903/*
15904 * "pow()" function
15905 */
15906 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015907f_pow(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015908{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010015909 float_T fx = 0.0, fy = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000015910
15911 rettv->v_type = VAR_FLOAT;
15912 if (get_float_arg(argvars, &fx) == OK
15913 && get_float_arg(&argvars[1], &fy) == OK)
15914 rettv->vval.v_float = pow(fx, fy);
15915 else
15916 rettv->vval.v_float = 0.0;
15917}
15918#endif
15919
Bram Moolenaar910f66f2006-04-05 20:41:53 +000015920/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000015921 * "prevnonblank()" function
15922 */
15923 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015924f_prevnonblank(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000015925{
15926 linenr_T lnum;
15927
15928 lnum = get_tv_lnum(argvars);
15929 if (lnum < 1 || lnum > curbuf->b_ml.ml_line_count)
15930 lnum = 0;
15931 else
15932 while (lnum >= 1 && *skipwhite(ml_get(lnum)) == NUL)
15933 --lnum;
15934 rettv->vval.v_number = lnum;
15935}
15936
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015937/* This dummy va_list is here because:
15938 * - passing a NULL pointer doesn't work when va_list isn't a pointer
15939 * - locally in the function results in a "used before set" warning
15940 * - using va_start() to initialize it gives "function with fixed args" error */
15941static va_list ap;
Bram Moolenaara6c840d2005-08-22 22:59:46 +000015942
Bram Moolenaar8c711452005-01-14 21:53:12 +000015943/*
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015944 * "printf()" function
15945 */
15946 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015947f_printf(typval_T *argvars, typval_T *rettv)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015948{
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015949 char_u buf[NUMBUFLEN];
15950 int len;
15951 char_u *s;
15952 int saved_did_emsg = did_emsg;
15953 char *fmt;
15954
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015955 rettv->v_type = VAR_STRING;
15956 rettv->vval.v_string = NULL;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015957
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015958 /* Get the required length, allocate the buffer and do it for real. */
15959 did_emsg = FALSE;
15960 fmt = (char *)get_tv_string_buf(&argvars[0], buf);
15961 len = vim_vsnprintf(NULL, 0, fmt, ap, argvars + 1);
15962 if (!did_emsg)
15963 {
15964 s = alloc(len + 1);
15965 if (s != NULL)
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015966 {
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015967 rettv->vval.v_string = s;
15968 (void)vim_vsnprintf((char *)s, len + 1, fmt, ap, argvars + 1);
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015969 }
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015970 }
Bram Moolenaarba4ef272016-01-30 21:48:49 +010015971 did_emsg |= saved_did_emsg;
Bram Moolenaar4be06f92005-07-29 22:36:03 +000015972}
15973
15974/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015975 * "pumvisible()" function
15976 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015977 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015978f_pumvisible(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015979{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000015980#ifdef FEAT_INS_EXPAND
15981 if (pum_visible())
15982 rettv->vval.v_number = 1;
15983#endif
15984}
15985
Bram Moolenaardb913952012-06-29 12:54:53 +020015986#ifdef FEAT_PYTHON3
15987/*
15988 * "py3eval()" function
15989 */
15990 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010015991f_py3eval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020015992{
15993 char_u *str;
15994 char_u buf[NUMBUFLEN];
15995
15996 str = get_tv_string_buf(&argvars[0], buf);
15997 do_py3eval(str, rettv);
15998}
15999#endif
16000
16001#ifdef FEAT_PYTHON
16002/*
16003 * "pyeval()" function
16004 */
16005 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016006f_pyeval(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb913952012-06-29 12:54:53 +020016007{
16008 char_u *str;
16009 char_u buf[NUMBUFLEN];
16010
16011 str = get_tv_string_buf(&argvars[0], buf);
16012 do_pyeval(str, rettv);
16013}
16014#endif
16015
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000016016/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016017 * "range()" function
16018 */
16019 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016020f_range(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016021{
16022 long start;
16023 long end;
16024 long stride = 1;
16025 long i;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016026 int error = FALSE;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016027
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016028 start = get_tv_number_chk(&argvars[0], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016029 if (argvars[1].v_type == VAR_UNKNOWN)
16030 {
16031 end = start - 1;
16032 start = 0;
16033 }
16034 else
16035 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016036 end = get_tv_number_chk(&argvars[1], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016037 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016038 stride = get_tv_number_chk(&argvars[2], &error);
Bram Moolenaar8c711452005-01-14 21:53:12 +000016039 }
16040
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016041 if (error)
16042 return; /* type error; errmsg already given */
Bram Moolenaar8c711452005-01-14 21:53:12 +000016043 if (stride == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016044 EMSG(_("E726: Stride is zero"));
Bram Moolenaar92124a32005-06-17 22:03:40 +000016045 else if (stride > 0 ? end + 1 < start : end - 1 > start)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016046 EMSG(_("E727: Start past end"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000016047 else
16048 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016049 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016050 for (i = start; stride > 0 ? i <= end : i >= end; i += stride)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016051 if (list_append_number(rettv->vval.v_list,
16052 (varnumber_T)i) == FAIL)
Bram Moolenaar8c711452005-01-14 21:53:12 +000016053 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016054 }
16055}
16056
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016057/*
16058 * "readfile()" function
16059 */
16060 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016061f_readfile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016062{
16063 int binary = FALSE;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016064 int failed = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016065 char_u *fname;
16066 FILE *fd;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016067 char_u buf[(IOSIZE/256)*256]; /* rounded to avoid odd + 1 */
16068 int io_size = sizeof(buf);
16069 int readlen; /* size of last fread() */
16070 char_u *prev = NULL; /* previously read bytes, if any */
16071 long prevlen = 0; /* length of data in prev */
16072 long prevsize = 0; /* size of prev buffer */
16073 long maxline = MAXLNUM;
16074 long cnt = 0;
16075 char_u *p; /* position in buf */
16076 char_u *start; /* start of current line */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016077
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016078 if (argvars[1].v_type != VAR_UNKNOWN)
16079 {
16080 if (STRCMP(get_tv_string(&argvars[1]), "b") == 0)
16081 binary = TRUE;
16082 if (argvars[2].v_type != VAR_UNKNOWN)
16083 maxline = get_tv_number(&argvars[2]);
16084 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016085
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016086 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016087 return;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016088
16089 /* Always open the file in binary mode, library functions have a mind of
16090 * their own about CR-LF conversion. */
16091 fname = get_tv_string(&argvars[0]);
16092 if (*fname == NUL || (fd = mch_fopen((char *)fname, READBIN)) == NULL)
16093 {
16094 EMSG2(_(e_notopen), *fname == NUL ? (char_u *)_("<empty>") : fname);
16095 return;
16096 }
16097
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016098 while (cnt < maxline || maxline < 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016099 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016100 readlen = (int)fread(buf, 1, io_size, fd);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016101
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016102 /* This for loop processes what was read, but is also entered at end
16103 * of file so that either:
16104 * - an incomplete line gets written
16105 * - a "binary" file gets an empty line at the end if it ends in a
16106 * newline. */
16107 for (p = buf, start = buf;
16108 p < buf + readlen || (readlen <= 0 && (prevlen > 0 || binary));
16109 ++p)
16110 {
16111 if (*p == '\n' || readlen <= 0)
16112 {
16113 listitem_T *li;
16114 char_u *s = NULL;
16115 long_u len = p - start;
16116
16117 /* Finished a line. Remove CRs before NL. */
16118 if (readlen > 0 && !binary)
16119 {
16120 while (len > 0 && start[len - 1] == '\r')
16121 --len;
16122 /* removal may cross back to the "prev" string */
16123 if (len == 0)
16124 while (prevlen > 0 && prev[prevlen - 1] == '\r')
16125 --prevlen;
16126 }
16127 if (prevlen == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016128 s = vim_strnsave(start, (int)len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016129 else
16130 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016131 /* Change "prev" buffer to be the right size. This way
16132 * the bytes are only copied once, and very long lines are
16133 * allocated only once. */
16134 if ((s = vim_realloc(prev, prevlen + len + 1)) != NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016135 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016136 mch_memmove(s + prevlen, start, len);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016137 s[prevlen + len] = NUL;
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016138 prev = NULL; /* the list will own the string */
16139 prevlen = prevsize = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016140 }
16141 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016142 if (s == NULL)
16143 {
16144 do_outofmem_msg((long_u) prevlen + len + 1);
16145 failed = TRUE;
16146 break;
16147 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016148
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016149 if ((li = listitem_alloc()) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016150 {
16151 vim_free(s);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016152 failed = TRUE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016153 break;
16154 }
16155 li->li_tv.v_type = VAR_STRING;
16156 li->li_tv.v_lock = 0;
16157 li->li_tv.vval.v_string = s;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016158 list_append(rettv->vval.v_list, li);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016159
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016160 start = p + 1; /* step over newline */
16161 if ((++cnt >= maxline && maxline >= 0) || readlen <= 0)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016162 break;
16163 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016164 else if (*p == NUL)
16165 *p = '\n';
Bram Moolenaar06583f12010-08-07 20:30:49 +020016166#ifdef FEAT_MBYTE
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016167 /* Check for utf8 "bom"; U+FEFF is encoded as EF BB BF. Do this
16168 * when finding the BF and check the previous two bytes. */
16169 else if (*p == 0xbf && enc_utf8 && !binary)
Bram Moolenaar06583f12010-08-07 20:30:49 +020016170 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016171 /* Find the two bytes before the 0xbf. If p is at buf, or buf
16172 * + 1, these may be in the "prev" string. */
16173 char_u back1 = p >= buf + 1 ? p[-1]
16174 : prevlen >= 1 ? prev[prevlen - 1] : NUL;
16175 char_u back2 = p >= buf + 2 ? p[-2]
16176 : p == buf + 1 && prevlen >= 1 ? prev[prevlen - 1]
16177 : prevlen >= 2 ? prev[prevlen - 2] : NUL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016178
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016179 if (back2 == 0xef && back1 == 0xbb)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016180 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016181 char_u *dest = p - 2;
16182
16183 /* Usually a BOM is at the beginning of a file, and so at
16184 * the beginning of a line; then we can just step over it.
16185 */
16186 if (start == dest)
16187 start = p + 1;
16188 else
Bram Moolenaar27b60562011-04-01 16:07:46 +020016189 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016190 /* have to shuffle buf to close gap */
16191 int adjust_prevlen = 0;
16192
16193 if (dest < buf)
16194 {
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016195 adjust_prevlen = (int)(buf - dest); /* must be 1 or 2 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016196 dest = buf;
16197 }
16198 if (readlen > p - buf + 1)
16199 mch_memmove(dest, p + 1, readlen - (p - buf) - 1);
16200 readlen -= 3 - adjust_prevlen;
16201 prevlen -= adjust_prevlen;
16202 p = dest - 1;
Bram Moolenaar27b60562011-04-01 16:07:46 +020016203 }
16204 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016205 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016206#endif
16207 } /* for */
16208
16209 if (failed || (cnt >= maxline && maxline >= 0) || readlen <= 0)
16210 break;
16211 if (start < p)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016212 {
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016213 /* There's part of a line in buf, store it in "prev". */
16214 if (p - start + prevlen >= prevsize)
16215 {
16216 /* need bigger "prev" buffer */
16217 char_u *newprev;
16218
16219 /* A common use case is ordinary text files and "prev" gets a
16220 * fragment of a line, so the first allocation is made
16221 * small, to avoid repeatedly 'allocing' large and
16222 * 'reallocing' small. */
16223 if (prevsize == 0)
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016224 prevsize = (long)(p - start);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016225 else
16226 {
16227 long grow50pc = (prevsize * 3) / 2;
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016228 long growmin = (long)((p - start) * 2 + prevlen);
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016229 prevsize = grow50pc > growmin ? grow50pc : growmin;
16230 }
Bram Moolenaar455981e2012-05-18 18:34:19 +020016231 newprev = prev == NULL ? alloc(prevsize)
16232 : vim_realloc(prev, prevsize);
16233 if (newprev == NULL)
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016234 {
16235 do_outofmem_msg((long_u)prevsize);
16236 failed = TRUE;
16237 break;
16238 }
16239 prev = newprev;
16240 }
16241 /* Add the line part to end of "prev". */
16242 mch_memmove(prev + prevlen, start, p - start);
Bram Moolenaar68ba0dd2012-02-11 20:44:10 +010016243 prevlen += (long)(p - start);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016244 }
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016245 } /* while */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016246
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016247 /*
16248 * For a negative line count use only the lines at the end of the file,
16249 * free the rest.
16250 */
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016251 if (!failed && maxline < 0)
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016252 while (cnt > -maxline)
16253 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016254 listitem_remove(rettv->vval.v_list, rettv->vval.v_list->lv_first);
Bram Moolenaarb982ca52005-03-28 21:02:15 +000016255 --cnt;
16256 }
16257
Bram Moolenaara489e1d2012-02-05 00:39:18 +010016258 if (failed)
16259 {
16260 list_free(rettv->vval.v_list, TRUE);
16261 /* readfile doc says an empty list is returned on error */
16262 rettv->vval.v_list = list_alloc();
16263 }
16264
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000016265 vim_free(prev);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016266 fclose(fd);
16267}
16268
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016269#if defined(FEAT_RELTIME)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016270static int list2proftime(typval_T *arg, proftime_T *tm);
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016271
16272/*
16273 * Convert a List to proftime_T.
16274 * Return FAIL when there is something wrong.
16275 */
16276 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016277list2proftime(typval_T *arg, proftime_T *tm)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016278{
16279 long n1, n2;
16280 int error = FALSE;
16281
16282 if (arg->v_type != VAR_LIST || arg->vval.v_list == NULL
16283 || arg->vval.v_list->lv_len != 2)
16284 return FAIL;
16285 n1 = list_find_nr(arg->vval.v_list, 0L, &error);
16286 n2 = list_find_nr(arg->vval.v_list, 1L, &error);
16287# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016288 tm->HighPart = n1;
16289 tm->LowPart = n2;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016290# else
16291 tm->tv_sec = n1;
16292 tm->tv_usec = n2;
16293# endif
16294 return error ? FAIL : OK;
16295}
16296#endif /* FEAT_RELTIME */
16297
16298/*
16299 * "reltime()" function
16300 */
16301 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016302f_reltime(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016303{
16304#ifdef FEAT_RELTIME
16305 proftime_T res;
16306 proftime_T start;
16307
16308 if (argvars[0].v_type == VAR_UNKNOWN)
16309 {
16310 /* No arguments: get current time. */
16311 profile_start(&res);
16312 }
16313 else if (argvars[1].v_type == VAR_UNKNOWN)
16314 {
16315 if (list2proftime(&argvars[0], &res) == FAIL)
16316 return;
16317 profile_end(&res);
16318 }
16319 else
16320 {
16321 /* Two arguments: compute the difference. */
16322 if (list2proftime(&argvars[0], &start) == FAIL
16323 || list2proftime(&argvars[1], &res) == FAIL)
16324 return;
16325 profile_sub(&res, &start);
16326 }
16327
16328 if (rettv_list_alloc(rettv) == OK)
16329 {
16330 long n1, n2;
16331
16332# ifdef WIN3264
Bram Moolenaardb552d602006-03-23 22:59:57 +000016333 n1 = res.HighPart;
16334 n2 = res.LowPart;
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016335# else
16336 n1 = res.tv_sec;
16337 n2 = res.tv_usec;
16338# endif
16339 list_append_number(rettv->vval.v_list, (varnumber_T)n1);
16340 list_append_number(rettv->vval.v_list, (varnumber_T)n2);
16341 }
16342#endif
16343}
16344
Bram Moolenaar79c2c882016-02-07 21:19:28 +010016345#ifdef FEAT_FLOAT
16346/*
16347 * "reltimefloat()" function
16348 */
16349 static void
16350f_reltimefloat(typval_T *argvars UNUSED, typval_T *rettv)
16351{
16352# ifdef FEAT_RELTIME
16353 proftime_T tm;
16354# endif
16355
16356 rettv->v_type = VAR_FLOAT;
16357 rettv->vval.v_float = 0;
16358# ifdef FEAT_RELTIME
16359 if (list2proftime(&argvars[0], &tm) == OK)
16360 rettv->vval.v_float = profile_float(&tm);
16361# endif
16362}
16363#endif
16364
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016365/*
16366 * "reltimestr()" function
16367 */
16368 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016369f_reltimestr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaare580b0c2006-03-21 21:33:03 +000016370{
16371#ifdef FEAT_RELTIME
16372 proftime_T tm;
16373#endif
16374
16375 rettv->v_type = VAR_STRING;
16376 rettv->vval.v_string = NULL;
16377#ifdef FEAT_RELTIME
16378 if (list2proftime(&argvars[0], &tm) == OK)
16379 rettv->vval.v_string = vim_strsave((char_u *)profile_msg(&tm));
16380#endif
16381}
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000016382
Bram Moolenaar0d660222005-01-07 21:51:51 +000016383#if defined(FEAT_CLIENTSERVER) && defined(FEAT_X11)
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016384static void make_connection(void);
16385static int check_connection(void);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016386
16387 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016388make_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016389{
16390 if (X_DISPLAY == NULL
16391# ifdef FEAT_GUI
16392 && !gui.in_use
16393# endif
16394 )
16395 {
16396 x_force_connect = TRUE;
16397 setup_term_clip();
16398 x_force_connect = FALSE;
16399 }
16400}
16401
16402 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010016403check_connection(void)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016404{
16405 make_connection();
16406 if (X_DISPLAY == NULL)
16407 {
16408 EMSG(_("E240: No connection to Vim server"));
16409 return FAIL;
16410 }
16411 return OK;
16412}
16413#endif
16414
16415#ifdef FEAT_CLIENTSERVER
Bram Moolenaar48e697e2016-01-23 22:17:30 +010016416static void remote_common(typval_T *argvars, typval_T *rettv, int expr);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016417
16418 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016419remote_common(typval_T *argvars, typval_T *rettv, int expr)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016420{
16421 char_u *server_name;
16422 char_u *keys;
16423 char_u *r = NULL;
16424 char_u buf[NUMBUFLEN];
16425# ifdef WIN32
16426 HWND w;
16427# else
16428 Window w;
16429# endif
16430
16431 if (check_restricted() || check_secure())
16432 return;
16433
16434# ifdef FEAT_X11
16435 if (check_connection() == FAIL)
16436 return;
16437# endif
16438
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016439 server_name = get_tv_string_chk(&argvars[0]);
16440 if (server_name == NULL)
16441 return; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016442 keys = get_tv_string_buf(&argvars[1], buf);
16443# ifdef WIN32
16444 if (serverSendToVim(server_name, keys, &r, &w, expr, TRUE) < 0)
16445# else
16446 if (serverSendToVim(X_DISPLAY, server_name, keys, &r, &w, expr, 0, TRUE)
16447 < 0)
16448# endif
16449 {
16450 if (r != NULL)
16451 EMSG(r); /* sending worked but evaluation failed */
16452 else
16453 EMSG2(_("E241: Unable to send to %s"), server_name);
16454 return;
16455 }
16456
16457 rettv->vval.v_string = r;
16458
16459 if (argvars[2].v_type != VAR_UNKNOWN)
16460 {
Bram Moolenaar33570922005-01-25 22:26:29 +000016461 dictitem_T v;
Bram Moolenaar555b2802005-05-19 21:08:39 +000016462 char_u str[30];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016463 char_u *idvar;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016464
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016465 sprintf((char *)str, PRINTF_HEX_LONG_U, (long_u)w);
Bram Moolenaar33570922005-01-25 22:26:29 +000016466 v.di_tv.v_type = VAR_STRING;
16467 v.di_tv.vval.v_string = vim_strsave(str);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016468 idvar = get_tv_string_chk(&argvars[2]);
16469 if (idvar != NULL)
16470 set_var(idvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016471 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016472 }
16473}
16474#endif
16475
16476/*
16477 * "remote_expr()" function
16478 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016479 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016480f_remote_expr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016481{
16482 rettv->v_type = VAR_STRING;
16483 rettv->vval.v_string = NULL;
16484#ifdef FEAT_CLIENTSERVER
16485 remote_common(argvars, rettv, TRUE);
16486#endif
16487}
16488
16489/*
16490 * "remote_foreground()" function
16491 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016492 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016493f_remote_foreground(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016494{
Bram Moolenaar0d660222005-01-07 21:51:51 +000016495#ifdef FEAT_CLIENTSERVER
16496# ifdef WIN32
16497 /* On Win32 it's done in this application. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016498 {
16499 char_u *server_name = get_tv_string_chk(&argvars[0]);
16500
16501 if (server_name != NULL)
16502 serverForeground(server_name);
16503 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016504# else
16505 /* Send a foreground() expression to the server. */
16506 argvars[1].v_type = VAR_STRING;
16507 argvars[1].vval.v_string = vim_strsave((char_u *)"foreground()");
16508 argvars[2].v_type = VAR_UNKNOWN;
16509 remote_common(argvars, rettv, TRUE);
16510 vim_free(argvars[1].vval.v_string);
16511# endif
16512#endif
16513}
16514
Bram Moolenaar0d660222005-01-07 21:51:51 +000016515 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016516f_remote_peek(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016517{
16518#ifdef FEAT_CLIENTSERVER
Bram Moolenaar33570922005-01-25 22:26:29 +000016519 dictitem_T v;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016520 char_u *s = NULL;
16521# ifdef WIN32
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016522 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016523# endif
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016524 char_u *serverid;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016525
16526 if (check_restricted() || check_secure())
16527 {
16528 rettv->vval.v_number = -1;
16529 return;
16530 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016531 serverid = get_tv_string_chk(&argvars[0]);
16532 if (serverid == NULL)
16533 {
16534 rettv->vval.v_number = -1;
16535 return; /* type error; errmsg already given */
16536 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000016537# ifdef WIN32
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016538 sscanf((const char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016539 if (n == 0)
16540 rettv->vval.v_number = -1;
16541 else
16542 {
16543 s = serverGetReply((HWND)n, FALSE, FALSE, FALSE);
16544 rettv->vval.v_number = (s != NULL);
16545 }
16546# else
Bram Moolenaar0d660222005-01-07 21:51:51 +000016547 if (check_connection() == FAIL)
16548 return;
16549
16550 rettv->vval.v_number = serverPeekReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016551 serverStrToWin(serverid), &s);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016552# endif
16553
16554 if (argvars[1].v_type != VAR_UNKNOWN && rettv->vval.v_number > 0)
16555 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016556 char_u *retvar;
16557
Bram Moolenaar33570922005-01-25 22:26:29 +000016558 v.di_tv.v_type = VAR_STRING;
16559 v.di_tv.vval.v_string = vim_strsave(s);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016560 retvar = get_tv_string_chk(&argvars[1]);
16561 if (retvar != NULL)
16562 set_var(retvar, &v.di_tv, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000016563 vim_free(v.di_tv.vval.v_string);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016564 }
16565#else
16566 rettv->vval.v_number = -1;
16567#endif
16568}
16569
Bram Moolenaar0d660222005-01-07 21:51:51 +000016570 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016571f_remote_read(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016572{
16573 char_u *r = NULL;
16574
16575#ifdef FEAT_CLIENTSERVER
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016576 char_u *serverid = get_tv_string_chk(&argvars[0]);
16577
16578 if (serverid != NULL && !check_restricted() && !check_secure())
Bram Moolenaar0d660222005-01-07 21:51:51 +000016579 {
16580# ifdef WIN32
16581 /* The server's HWND is encoded in the 'id' parameter */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000016582 long_u n = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016583
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010016584 sscanf((char *)serverid, SCANF_HEX_LONG_U, &n);
Bram Moolenaar0d660222005-01-07 21:51:51 +000016585 if (n != 0)
16586 r = serverGetReply((HWND)n, FALSE, TRUE, TRUE);
16587 if (r == NULL)
16588# else
16589 if (check_connection() == FAIL || serverReadReply(X_DISPLAY,
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016590 serverStrToWin(serverid), &r, FALSE) < 0)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016591# endif
16592 EMSG(_("E277: Unable to read a server reply"));
16593 }
16594#endif
16595 rettv->v_type = VAR_STRING;
16596 rettv->vval.v_string = r;
16597}
16598
16599/*
16600 * "remote_send()" function
16601 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000016602 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016603f_remote_send(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000016604{
16605 rettv->v_type = VAR_STRING;
16606 rettv->vval.v_string = NULL;
16607#ifdef FEAT_CLIENTSERVER
16608 remote_common(argvars, rettv, FALSE);
16609#endif
16610}
16611
16612/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000016613 * "remove()" function
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016614 */
16615 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016616f_remove(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016617{
Bram Moolenaar33570922005-01-25 22:26:29 +000016618 list_T *l;
16619 listitem_T *item, *item2;
16620 listitem_T *li;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016621 long idx;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016622 long end;
Bram Moolenaar8c711452005-01-14 21:53:12 +000016623 char_u *key;
Bram Moolenaar33570922005-01-25 22:26:29 +000016624 dict_T *d;
16625 dictitem_T *di;
Bram Moolenaar77354e72015-04-21 16:49:05 +020016626 char_u *arg_errmsg = (char_u *)N_("remove() argument");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016627
Bram Moolenaar8c711452005-01-14 21:53:12 +000016628 if (argvars[0].v_type == VAR_DICT)
16629 {
16630 if (argvars[2].v_type != VAR_UNKNOWN)
16631 EMSG2(_(e_toomanyarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016632 else if ((d = argvars[0].vval.v_dict) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016633 && !tv_check_lock(d->dv_lock, arg_errmsg, TRUE))
Bram Moolenaar8c711452005-01-14 21:53:12 +000016634 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016635 key = get_tv_string_chk(&argvars[1]);
16636 if (key != NULL)
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016637 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016638 di = dict_find(d, key, -1);
16639 if (di == NULL)
16640 EMSG2(_(e_dictkey), key);
Bram Moolenaar77354e72015-04-21 16:49:05 +020016641 else if (!var_check_fixed(di->di_flags, arg_errmsg, TRUE)
16642 && !var_check_ro(di->di_flags, arg_errmsg, TRUE))
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016643 {
16644 *rettv = di->di_tv;
16645 init_tv(&di->di_tv);
16646 dictitem_remove(d, di);
16647 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000016648 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000016649 }
16650 }
16651 else if (argvars[0].v_type != VAR_LIST)
16652 EMSG2(_(e_listdictarg), "remove()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016653 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016654 && !tv_check_lock(l->lv_lock, arg_errmsg, TRUE))
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016655 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016656 int error = FALSE;
16657
16658 idx = get_tv_number_chk(&argvars[1], &error);
16659 if (error)
16660 ; /* type error: do nothing, errmsg already given */
16661 else if ((item = list_find(l, idx)) == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016662 EMSGN(_(e_listidx), idx);
16663 else
16664 {
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016665 if (argvars[2].v_type == VAR_UNKNOWN)
16666 {
16667 /* Remove one item, return its value. */
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016668 vimlist_remove(l, item, item);
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016669 *rettv = item->li_tv;
16670 vim_free(item);
16671 }
16672 else
16673 {
16674 /* Remove range of items, return list with values. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000016675 end = get_tv_number_chk(&argvars[2], &error);
16676 if (error)
16677 ; /* type error: do nothing */
16678 else if ((item2 = list_find(l, end)) == NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016679 EMSGN(_(e_listidx), end);
16680 else
16681 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000016682 int cnt = 0;
16683
16684 for (li = item; li != NULL; li = li->li_next)
16685 {
16686 ++cnt;
16687 if (li == item2)
16688 break;
16689 }
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016690 if (li == NULL) /* didn't find "item2" after "item" */
16691 EMSG(_(e_invrange));
16692 else
16693 {
Bram Moolenaar3ec7f4e2014-05-07 17:31:37 +020016694 vimlist_remove(l, item, item2);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016695 if (rettv_list_alloc(rettv) == OK)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016696 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016697 l = rettv->vval.v_list;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016698 l->lv_first = item;
16699 l->lv_last = item2;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016700 item->li_prev = NULL;
16701 item2->li_next = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016702 l->lv_len = cnt;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016703 }
16704 }
16705 }
16706 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000016707 }
16708 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016709}
16710
16711/*
16712 * "rename({from}, {to})" function
16713 */
16714 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016715f_rename(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016716{
16717 char_u buf[NUMBUFLEN];
16718
16719 if (check_restricted() || check_secure())
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016720 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016721 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016722 rettv->vval.v_number = vim_rename(get_tv_string(&argvars[0]),
16723 get_tv_string_buf(&argvars[1], buf));
Bram Moolenaar071d4272004-06-13 20:20:40 +000016724}
16725
16726/*
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016727 * "repeat()" function
16728 */
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016729 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016730f_repeat(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016731{
16732 char_u *p;
16733 int n;
16734 int slen;
16735 int len;
16736 char_u *r;
16737 int i;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016738
16739 n = get_tv_number(&argvars[1]);
16740 if (argvars[0].v_type == VAR_LIST)
16741 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016742 if (rettv_list_alloc(rettv) == OK && argvars[0].vval.v_list != NULL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016743 while (n-- > 0)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000016744 if (list_extend(rettv->vval.v_list,
16745 argvars[0].vval.v_list, NULL) == FAIL)
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016746 break;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000016747 }
16748 else
16749 {
16750 p = get_tv_string(&argvars[0]);
16751 rettv->v_type = VAR_STRING;
16752 rettv->vval.v_string = NULL;
16753
16754 slen = (int)STRLEN(p);
16755 len = slen * n;
16756 if (len <= 0)
16757 return;
16758
16759 r = alloc(len + 1);
16760 if (r != NULL)
16761 {
16762 for (i = 0; i < n; i++)
16763 mch_memmove(r + i * slen, p, (size_t)slen);
16764 r[len] = NUL;
16765 }
16766
16767 rettv->vval.v_string = r;
16768 }
16769}
16770
16771/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000016772 * "resolve()" function
16773 */
16774 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016775f_resolve(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016776{
16777 char_u *p;
Bram Moolenaard9462e32011-04-11 21:35:11 +020016778#ifdef HAVE_READLINK
16779 char_u *buf = NULL;
16780#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000016781
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016782 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016783#ifdef FEAT_SHORTCUT
16784 {
16785 char_u *v = NULL;
16786
16787 v = mch_resolve_shortcut(p);
16788 if (v != NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016789 rettv->vval.v_string = v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016790 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016791 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016792 }
16793#else
16794# ifdef HAVE_READLINK
16795 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016796 char_u *cpy;
16797 int len;
16798 char_u *remain = NULL;
16799 char_u *q;
16800 int is_relative_to_current = FALSE;
16801 int has_trailing_pathsep = FALSE;
16802 int limit = 100;
16803
16804 p = vim_strsave(p);
16805
16806 if (p[0] == '.' && (vim_ispathsep(p[1])
16807 || (p[1] == '.' && (vim_ispathsep(p[2])))))
16808 is_relative_to_current = TRUE;
16809
16810 len = STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016811 if (len > 0 && after_pathsep(p, p + len))
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016812 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016813 has_trailing_pathsep = TRUE;
Bram Moolenaar1385c3e2011-05-19 14:59:10 +020016814 p[len - 1] = NUL; /* the trailing slash breaks readlink() */
16815 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000016816
16817 q = getnextcomp(p);
16818 if (*q != NUL)
16819 {
16820 /* Separate the first path component in "p", and keep the
16821 * remainder (beginning with the path separator). */
16822 remain = vim_strsave(q - 1);
16823 q[-1] = NUL;
16824 }
16825
Bram Moolenaard9462e32011-04-11 21:35:11 +020016826 buf = alloc(MAXPATHL + 1);
16827 if (buf == NULL)
16828 goto fail;
16829
Bram Moolenaar071d4272004-06-13 20:20:40 +000016830 for (;;)
16831 {
16832 for (;;)
16833 {
16834 len = readlink((char *)p, (char *)buf, MAXPATHL);
16835 if (len <= 0)
16836 break;
16837 buf[len] = NUL;
16838
16839 if (limit-- == 0)
16840 {
16841 vim_free(p);
16842 vim_free(remain);
16843 EMSG(_("E655: Too many symbolic links (cycle?)"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016844 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016845 goto fail;
16846 }
16847
16848 /* Ensure that the result will have a trailing path separator
16849 * if the argument has one. */
16850 if (remain == NULL && has_trailing_pathsep)
16851 add_pathsep(buf);
16852
16853 /* Separate the first path component in the link value and
16854 * concatenate the remainders. */
16855 q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf);
16856 if (*q != NUL)
16857 {
16858 if (remain == NULL)
16859 remain = vim_strsave(q - 1);
16860 else
16861 {
Bram Moolenaar900b4d72005-12-12 22:05:50 +000016862 cpy = concat_str(q - 1, remain);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016863 if (cpy != NULL)
16864 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016865 vim_free(remain);
16866 remain = cpy;
16867 }
16868 }
16869 q[-1] = NUL;
16870 }
16871
16872 q = gettail(p);
16873 if (q > p && *q == NUL)
16874 {
16875 /* Ignore trailing path separator. */
16876 q[-1] = NUL;
16877 q = gettail(p);
16878 }
16879 if (q > p && !mch_isFullName(buf))
16880 {
16881 /* symlink is relative to directory of argument */
16882 cpy = alloc((unsigned)(STRLEN(p) + STRLEN(buf) + 1));
16883 if (cpy != NULL)
16884 {
16885 STRCPY(cpy, p);
16886 STRCPY(gettail(cpy), buf);
16887 vim_free(p);
16888 p = cpy;
16889 }
16890 }
16891 else
16892 {
16893 vim_free(p);
16894 p = vim_strsave(buf);
16895 }
16896 }
16897
16898 if (remain == NULL)
16899 break;
16900
16901 /* Append the first path component of "remain" to "p". */
16902 q = getnextcomp(remain + 1);
16903 len = q - remain - (*q != NUL);
16904 cpy = vim_strnsave(p, STRLEN(p) + len);
16905 if (cpy != NULL)
16906 {
16907 STRNCAT(cpy, remain, len);
16908 vim_free(p);
16909 p = cpy;
16910 }
16911 /* Shorten "remain". */
16912 if (*q != NUL)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016913 STRMOVE(remain, q - 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016914 else
16915 {
16916 vim_free(remain);
16917 remain = NULL;
16918 }
16919 }
16920
16921 /* If the result is a relative path name, make it explicitly relative to
16922 * the current directory if and only if the argument had this form. */
16923 if (!vim_ispathsep(*p))
16924 {
16925 if (is_relative_to_current
16926 && *p != NUL
16927 && !(p[0] == '.'
16928 && (p[1] == NUL
16929 || vim_ispathsep(p[1])
16930 || (p[1] == '.'
16931 && (p[2] == NUL
16932 || vim_ispathsep(p[2]))))))
16933 {
16934 /* Prepend "./". */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000016935 cpy = concat_str((char_u *)"./", p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016936 if (cpy != NULL)
16937 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000016938 vim_free(p);
16939 p = cpy;
16940 }
16941 }
16942 else if (!is_relative_to_current)
16943 {
16944 /* Strip leading "./". */
16945 q = p;
16946 while (q[0] == '.' && vim_ispathsep(q[1]))
16947 q += 2;
16948 if (q > p)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000016949 STRMOVE(p, p + 2);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016950 }
16951 }
16952
16953 /* Ensure that the result will have no trailing path separator
16954 * if the argument had none. But keep "/" or "//". */
16955 if (!has_trailing_pathsep)
16956 {
16957 q = p + STRLEN(p);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000016958 if (after_pathsep(p, q))
16959 *gettail_sep(p) = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016960 }
16961
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016962 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016963 }
16964# else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016965 rettv->vval.v_string = vim_strsave(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016966# endif
16967#endif
16968
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016969 simplify_filename(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016970
16971#ifdef HAVE_READLINK
16972fail:
Bram Moolenaard9462e32011-04-11 21:35:11 +020016973 vim_free(buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000016974#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000016975 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016976}
16977
16978/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000016979 * "reverse({list})" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000016980 */
16981 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010016982f_reverse(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000016983{
Bram Moolenaar33570922005-01-25 22:26:29 +000016984 list_T *l;
16985 listitem_T *li, *ni;
Bram Moolenaar071d4272004-06-13 20:20:40 +000016986
Bram Moolenaar0d660222005-01-07 21:51:51 +000016987 if (argvars[0].v_type != VAR_LIST)
16988 EMSG2(_(e_listarg), "reverse()");
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000016989 else if ((l = argvars[0].vval.v_list) != NULL
Bram Moolenaar77354e72015-04-21 16:49:05 +020016990 && !tv_check_lock(l->lv_lock,
16991 (char_u *)N_("reverse() argument"), TRUE))
Bram Moolenaar0d660222005-01-07 21:51:51 +000016992 {
16993 li = l->lv_last;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000016994 l->lv_first = l->lv_last = NULL;
Bram Moolenaar758711c2005-02-02 23:11:38 +000016995 l->lv_len = 0;
Bram Moolenaar0d660222005-01-07 21:51:51 +000016996 while (li != NULL)
16997 {
16998 ni = li->li_prev;
16999 list_append(l, li);
17000 li = ni;
17001 }
17002 rettv->vval.v_list = l;
17003 rettv->v_type = VAR_LIST;
17004 ++l->lv_refcount;
Bram Moolenaar52514562008-04-01 11:12:09 +000017005 l->lv_idx = l->lv_len - l->lv_idx - 1;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017006 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017007}
17008
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017009#define SP_NOMOVE 0x01 /* don't move cursor */
17010#define SP_REPEAT 0x02 /* repeat to find outer pair */
17011#define SP_RETCOUNT 0x04 /* return matchcount */
17012#define SP_SETPCMARK 0x08 /* set previous context mark */
17013#define SP_START 0x10 /* accept match at start position */
17014#define SP_SUBPAT 0x20 /* return nr of matching sub-pattern */
17015#define SP_END 0x40 /* leave cursor at end of match */
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017016#define SP_COLUMN 0x80 /* start at cursor column */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017017
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017018static int get_search_arg(typval_T *varp, int *flagsp);
Bram Moolenaar0d660222005-01-07 21:51:51 +000017019
17020/*
17021 * Get flags for a search function.
17022 * Possibly sets "p_ws".
17023 * Returns BACKWARD, FORWARD or zero (for an error).
17024 */
17025 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017026get_search_arg(typval_T *varp, int *flagsp)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017027{
17028 int dir = FORWARD;
17029 char_u *flags;
17030 char_u nbuf[NUMBUFLEN];
17031 int mask;
17032
17033 if (varp->v_type != VAR_UNKNOWN)
17034 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017035 flags = get_tv_string_buf_chk(varp, nbuf);
17036 if (flags == NULL)
17037 return 0; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000017038 while (*flags != NUL)
17039 {
17040 switch (*flags)
17041 {
17042 case 'b': dir = BACKWARD; break;
17043 case 'w': p_ws = TRUE; break;
17044 case 'W': p_ws = FALSE; break;
17045 default: mask = 0;
17046 if (flagsp != NULL)
17047 switch (*flags)
17048 {
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017049 case 'c': mask = SP_START; break;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017050 case 'e': mask = SP_END; break;
17051 case 'm': mask = SP_RETCOUNT; break;
17052 case 'n': mask = SP_NOMOVE; break;
17053 case 'p': mask = SP_SUBPAT; break;
17054 case 'r': mask = SP_REPEAT; break;
17055 case 's': mask = SP_SETPCMARK; break;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017056 case 'z': mask = SP_COLUMN; break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017057 }
17058 if (mask == 0)
17059 {
17060 EMSG2(_(e_invarg2), flags);
17061 dir = 0;
17062 }
17063 else
17064 *flagsp |= mask;
17065 }
17066 if (dir == 0)
17067 break;
17068 ++flags;
17069 }
17070 }
17071 return dir;
17072}
17073
Bram Moolenaar071d4272004-06-13 20:20:40 +000017074/*
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017075 * Shared by search() and searchpos() functions.
Bram Moolenaar071d4272004-06-13 20:20:40 +000017076 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017077 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017078search_cmn(typval_T *argvars, pos_T *match_pos, int *flagsp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017079{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017080 int flags;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017081 char_u *pat;
17082 pos_T pos;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017083 pos_T save_cursor;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017084 int save_p_ws = p_ws;
17085 int dir;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017086 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017087 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017088 proftime_T tm;
17089#ifdef FEAT_RELTIME
17090 long time_limit = 0;
17091#endif
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017092 int options = SEARCH_KEEP;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017093 int subpatnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017094
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017095 pat = get_tv_string(&argvars[0]);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017096 dir = get_search_arg(&argvars[1], flagsp); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017097 if (dir == 0)
17098 goto theend;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017099 flags = *flagsp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017100 if (flags & SP_START)
17101 options |= SEARCH_START;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017102 if (flags & SP_END)
17103 options |= SEARCH_END;
Bram Moolenaarad4d8a12015-12-28 19:20:36 +010017104 if (flags & SP_COLUMN)
17105 options |= SEARCH_COL;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017106
Bram Moolenaar76929292008-01-06 19:07:36 +000017107 /* Optional arguments: line number to stop searching and timeout. */
17108 if (argvars[1].v_type != VAR_UNKNOWN && argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017109 {
17110 lnum_stop = get_tv_number_chk(&argvars[2], NULL);
17111 if (lnum_stop < 0)
17112 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017113#ifdef FEAT_RELTIME
17114 if (argvars[3].v_type != VAR_UNKNOWN)
17115 {
17116 time_limit = get_tv_number_chk(&argvars[3], NULL);
17117 if (time_limit < 0)
17118 goto theend;
17119 }
17120#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017121 }
17122
Bram Moolenaar76929292008-01-06 19:07:36 +000017123#ifdef FEAT_RELTIME
17124 /* Set the time limit, if there is one. */
17125 profile_setlimit(time_limit, &tm);
17126#endif
17127
Bram Moolenaar231334e2005-07-25 20:46:57 +000017128 /*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017129 * This function does not accept SP_REPEAT and SP_RETCOUNT flags.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017130 * Check to make sure only those flags are set.
17131 * Also, Only the SP_NOMOVE or the SP_SETPCMARK flag can be set. Both
17132 * flags cannot be set. Check for that condition also.
17133 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017134 if (((flags & (SP_REPEAT | SP_RETCOUNT)) != 0)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017135 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017136 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000017137 EMSG2(_(e_invarg2), get_tv_string(&argvars[1]));
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017138 goto theend;
17139 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017140
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017141 pos = save_cursor = curwin->w_cursor;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017142 subpatnum = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017143 options, RE_SEARCH, (linenr_T)lnum_stop, &tm);
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017144 if (subpatnum != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017145 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017146 if (flags & SP_SUBPAT)
17147 retval = subpatnum;
17148 else
17149 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017150 if (flags & SP_SETPCMARK)
17151 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017152 curwin->w_cursor = pos;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017153 if (match_pos != NULL)
17154 {
17155 /* Store the match cursor position */
17156 match_pos->lnum = pos.lnum;
17157 match_pos->col = pos.col + 1;
17158 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017159 /* "/$" will put the cursor after the end of the line, may need to
17160 * correct that here */
17161 check_cursor();
17162 }
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017163
17164 /* If 'n' flag is used: restore cursor position. */
17165 if (flags & SP_NOMOVE)
17166 curwin->w_cursor = save_cursor;
Bram Moolenaar7a42fa32007-07-10 11:28:55 +000017167 else
17168 curwin->w_set_curswant = TRUE;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017169theend:
Bram Moolenaar071d4272004-06-13 20:20:40 +000017170 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017171
17172 return retval;
17173}
17174
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017175#ifdef FEAT_FLOAT
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017176
17177/*
17178 * round() is not in C90, use ceil() or floor() instead.
17179 */
17180 float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010017181vim_round(float_T f)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017182{
17183 return f > 0 ? floor(f + 0.5) : ceil(f - 0.5);
17184}
17185
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017186/*
17187 * "round({float})" function
17188 */
17189 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017190f_round(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017191{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010017192 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017193
17194 rettv->v_type = VAR_FLOAT;
17195 if (get_float_arg(argvars, &f) == OK)
Bram Moolenaara2e14fc2013-06-10 20:10:44 +020017196 rettv->vval.v_float = vim_round(f);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000017197 else
17198 rettv->vval.v_float = 0.0;
17199}
17200#endif
17201
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017202/*
Bram Moolenaar9a773482013-06-11 18:40:13 +020017203 * "screenattr()" function
17204 */
17205 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017206f_screenattr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017207{
17208 int row;
17209 int col;
17210 int c;
17211
17212 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17213 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17214 if (row < 0 || row >= screen_Rows
17215 || col < 0 || col >= screen_Columns)
17216 c = -1;
17217 else
17218 c = ScreenAttrs[LineOffset[row] + col];
17219 rettv->vval.v_number = c;
17220}
17221
17222/*
17223 * "screenchar()" function
17224 */
17225 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010017226f_screenchar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar9a773482013-06-11 18:40:13 +020017227{
17228 int row;
17229 int col;
17230 int off;
17231 int c;
17232
17233 row = get_tv_number_chk(&argvars[0], NULL) - 1;
17234 col = get_tv_number_chk(&argvars[1], NULL) - 1;
17235 if (row < 0 || row >= screen_Rows
17236 || col < 0 || col >= screen_Columns)
17237 c = -1;
17238 else
17239 {
17240 off = LineOffset[row] + col;
17241#ifdef FEAT_MBYTE
17242 if (enc_utf8 && ScreenLinesUC[off] != 0)
17243 c = ScreenLinesUC[off];
17244 else
17245#endif
17246 c = ScreenLines[off];
17247 }
17248 rettv->vval.v_number = c;
17249}
17250
17251/*
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017252 * "screencol()" function
17253 *
17254 * First column is 1 to be consistent with virtcol().
17255 */
17256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017257f_screencol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017258{
17259 rettv->vval.v_number = screen_screencol() + 1;
17260}
17261
17262/*
17263 * "screenrow()" function
17264 */
17265 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017266f_screenrow(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9750bb12012-12-05 16:10:42 +010017267{
17268 rettv->vval.v_number = screen_screenrow() + 1;
17269}
17270
17271/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017272 * "search()" function
17273 */
17274 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017275f_search(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017276{
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017277 int flags = 0;
17278
17279 rettv->vval.v_number = search_cmn(argvars, NULL, &flags);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017280}
17281
Bram Moolenaar071d4272004-06-13 20:20:40 +000017282/*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017283 * "searchdecl()" function
17284 */
17285 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017286f_searchdecl(typval_T *argvars, typval_T *rettv)
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017287{
17288 int locally = 1;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017289 int thisblock = 0;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017290 int error = FALSE;
17291 char_u *name;
17292
17293 rettv->vval.v_number = 1; /* default: FAIL */
17294
17295 name = get_tv_string_chk(&argvars[0]);
17296 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaare6facf92005-09-13 21:22:27 +000017297 {
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017298 locally = get_tv_number_chk(&argvars[1], &error) == 0;
Bram Moolenaare6facf92005-09-13 21:22:27 +000017299 if (!error && argvars[2].v_type != VAR_UNKNOWN)
17300 thisblock = get_tv_number_chk(&argvars[2], &error) != 0;
17301 }
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017302 if (!error && name != NULL)
17303 rettv->vval.v_number = find_decl(name, (int)STRLEN(name),
Bram Moolenaare6facf92005-09-13 21:22:27 +000017304 locally, thisblock, SEARCH_KEEP) == FAIL;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000017305}
17306
17307/*
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017308 * Used by searchpair() and searchpairpos()
Bram Moolenaar071d4272004-06-13 20:20:40 +000017309 */
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017310 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010017311searchpair_cmn(typval_T *argvars, pos_T *match_pos)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017312{
17313 char_u *spat, *mpat, *epat;
17314 char_u *skip;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017315 int save_p_ws = p_ws;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017316 int dir;
17317 int flags = 0;
17318 char_u nbuf1[NUMBUFLEN];
17319 char_u nbuf2[NUMBUFLEN];
17320 char_u nbuf3[NUMBUFLEN];
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017321 int retval = 0; /* default: FAIL */
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017322 long lnum_stop = 0;
Bram Moolenaar76929292008-01-06 19:07:36 +000017323 long time_limit = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017324
Bram Moolenaar071d4272004-06-13 20:20:40 +000017325 /* Get the three pattern arguments: start, middle, end. */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017326 spat = get_tv_string_chk(&argvars[0]);
17327 mpat = get_tv_string_buf_chk(&argvars[1], nbuf1);
17328 epat = get_tv_string_buf_chk(&argvars[2], nbuf2);
17329 if (spat == NULL || mpat == NULL || epat == NULL)
17330 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017331
Bram Moolenaar071d4272004-06-13 20:20:40 +000017332 /* Handle the optional fourth argument: flags */
17333 dir = get_search_arg(&argvars[3], &flags); /* may set p_ws */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000017334 if (dir == 0)
17335 goto theend;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017336
17337 /* Don't accept SP_END or SP_SUBPAT.
Bram Moolenaar231334e2005-07-25 20:46:57 +000017338 * Only one of the SP_NOMOVE or SP_SETPCMARK flags can be set.
17339 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017340 if ((flags & (SP_END | SP_SUBPAT)) != 0
17341 || ((flags & SP_NOMOVE) && (flags & SP_SETPCMARK)))
Bram Moolenaar231334e2005-07-25 20:46:57 +000017342 {
Bram Moolenaar768b8c42006-03-04 21:58:33 +000017343 EMSG2(_(e_invarg2), get_tv_string(&argvars[3]));
Bram Moolenaar231334e2005-07-25 20:46:57 +000017344 goto theend;
17345 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017346
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017347 /* Using 'r' implies 'W', otherwise it doesn't work. */
17348 if (flags & SP_REPEAT)
17349 p_ws = FALSE;
17350
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017351 /* Optional fifth argument: skip expression */
Bram Moolenaar49cd9572005-01-03 21:06:01 +000017352 if (argvars[3].v_type == VAR_UNKNOWN
17353 || argvars[4].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017354 skip = (char_u *)"";
17355 else
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017356 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017357 skip = get_tv_string_buf_chk(&argvars[4], nbuf3);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017358 if (argvars[5].v_type != VAR_UNKNOWN)
17359 {
17360 lnum_stop = get_tv_number_chk(&argvars[5], NULL);
17361 if (lnum_stop < 0)
17362 goto theend;
Bram Moolenaar76929292008-01-06 19:07:36 +000017363#ifdef FEAT_RELTIME
17364 if (argvars[6].v_type != VAR_UNKNOWN)
17365 {
17366 time_limit = get_tv_number_chk(&argvars[6], NULL);
17367 if (time_limit < 0)
17368 goto theend;
17369 }
17370#endif
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017371 }
17372 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017373 if (skip == NULL)
17374 goto theend; /* type error */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017375
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017376 retval = do_searchpair(spat, mpat, epat, dir, skip, flags,
Bram Moolenaar76929292008-01-06 19:07:36 +000017377 match_pos, lnum_stop, time_limit);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017378
17379theend:
17380 p_ws = save_p_ws;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017381
17382 return retval;
17383}
17384
17385/*
17386 * "searchpair()" function
17387 */
17388 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017389f_searchpair(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017390{
17391 rettv->vval.v_number = searchpair_cmn(argvars, NULL);
17392}
17393
17394/*
17395 * "searchpairpos()" function
17396 */
17397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017398f_searchpairpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017399{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017400 pos_T match_pos;
17401 int lnum = 0;
17402 int col = 0;
17403
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017404 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017405 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017406
17407 if (searchpair_cmn(argvars, &match_pos) > 0)
17408 {
17409 lnum = match_pos.lnum;
17410 col = match_pos.col;
17411 }
17412
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017413 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17414 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017415}
17416
17417/*
17418 * Search for a start/middle/end thing.
17419 * Used by searchpair(), see its documentation for the details.
17420 * Returns 0 or -1 for no match,
17421 */
17422 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010017423do_searchpair(
17424 char_u *spat, /* start pattern */
17425 char_u *mpat, /* middle pattern */
17426 char_u *epat, /* end pattern */
17427 int dir, /* BACKWARD or FORWARD */
17428 char_u *skip, /* skip expression */
17429 int flags, /* SP_SETPCMARK and other SP_ values */
17430 pos_T *match_pos,
17431 linenr_T lnum_stop, /* stop at this line if not zero */
17432 long time_limit UNUSED) /* stop after this many msec */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017433{
17434 char_u *save_cpo;
17435 char_u *pat, *pat2 = NULL, *pat3 = NULL;
17436 long retval = 0;
17437 pos_T pos;
17438 pos_T firstpos;
17439 pos_T foundpos;
17440 pos_T save_cursor;
17441 pos_T save_pos;
17442 int n;
17443 int r;
17444 int nest = 1;
17445 int err;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017446 int options = SEARCH_KEEP;
Bram Moolenaar76929292008-01-06 19:07:36 +000017447 proftime_T tm;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017448
17449 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
17450 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017451 p_cpo = empty_option;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017452
Bram Moolenaar76929292008-01-06 19:07:36 +000017453#ifdef FEAT_RELTIME
17454 /* Set the time limit, if there is one. */
17455 profile_setlimit(time_limit, &tm);
17456#endif
17457
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017458 /* Make two search patterns: start/end (pat2, for in nested pairs) and
17459 * start/middle/end (pat3, for the top pair). */
17460 pat2 = alloc((unsigned)(STRLEN(spat) + STRLEN(epat) + 15));
17461 pat3 = alloc((unsigned)(STRLEN(spat) + STRLEN(mpat) + STRLEN(epat) + 23));
17462 if (pat2 == NULL || pat3 == NULL)
17463 goto theend;
17464 sprintf((char *)pat2, "\\(%s\\m\\)\\|\\(%s\\m\\)", spat, epat);
17465 if (*mpat == NUL)
17466 STRCPY(pat3, pat2);
17467 else
17468 sprintf((char *)pat3, "\\(%s\\m\\)\\|\\(%s\\m\\)\\|\\(%s\\m\\)",
17469 spat, epat, mpat);
Bram Moolenaar0e34f622006-03-03 23:00:03 +000017470 if (flags & SP_START)
17471 options |= SEARCH_START;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017472
Bram Moolenaar071d4272004-06-13 20:20:40 +000017473 save_cursor = curwin->w_cursor;
17474 pos = curwin->w_cursor;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000017475 clearpos(&firstpos);
17476 clearpos(&foundpos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017477 pat = pat3;
17478 for (;;)
17479 {
17480 n = searchit(curwin, curbuf, &pos, dir, pat, 1L,
Bram Moolenaar76929292008-01-06 19:07:36 +000017481 options, RE_SEARCH, lnum_stop, &tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017482 if (n == FAIL || (firstpos.lnum != 0 && equalpos(pos, firstpos)))
17483 /* didn't find it or found the first match again: FAIL */
17484 break;
17485
17486 if (firstpos.lnum == 0)
17487 firstpos = pos;
Bram Moolenaarc9a2d2e2005-04-24 22:09:56 +000017488 if (equalpos(pos, foundpos))
17489 {
17490 /* Found the same position again. Can happen with a pattern that
17491 * has "\zs" at the end and searching backwards. Advance one
17492 * character and try again. */
17493 if (dir == BACKWARD)
17494 decl(&pos);
17495 else
17496 incl(&pos);
17497 }
17498 foundpos = pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017499
Bram Moolenaar92de73d2008-01-22 10:59:38 +000017500 /* clear the start flag to avoid getting stuck here */
17501 options &= ~SEARCH_START;
17502
Bram Moolenaar071d4272004-06-13 20:20:40 +000017503 /* If the skip pattern matches, ignore this match. */
17504 if (*skip != NUL)
17505 {
17506 save_pos = curwin->w_cursor;
17507 curwin->w_cursor = pos;
17508 r = eval_to_bool(skip, &err, NULL, FALSE);
17509 curwin->w_cursor = save_pos;
17510 if (err)
17511 {
17512 /* Evaluating {skip} caused an error, break here. */
17513 curwin->w_cursor = save_cursor;
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017514 retval = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017515 break;
17516 }
17517 if (r)
17518 continue;
17519 }
17520
17521 if ((dir == BACKWARD && n == 3) || (dir == FORWARD && n == 2))
17522 {
17523 /* Found end when searching backwards or start when searching
17524 * forward: nested pair. */
17525 ++nest;
17526 pat = pat2; /* nested, don't search for middle */
17527 }
17528 else
17529 {
17530 /* Found end when searching forward or start when searching
17531 * backward: end of (nested) pair; or found middle in outer pair. */
17532 if (--nest == 1)
17533 pat = pat3; /* outer level, search for middle */
17534 }
17535
17536 if (nest == 0)
17537 {
17538 /* Found the match: return matchcount or line number. */
17539 if (flags & SP_RETCOUNT)
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017540 ++retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017541 else
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017542 retval = pos.lnum;
Bram Moolenaar231334e2005-07-25 20:46:57 +000017543 if (flags & SP_SETPCMARK)
17544 setpcmark();
Bram Moolenaar071d4272004-06-13 20:20:40 +000017545 curwin->w_cursor = pos;
17546 if (!(flags & SP_REPEAT))
17547 break;
17548 nest = 1; /* search for next unmatched */
17549 }
17550 }
17551
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017552 if (match_pos != NULL)
17553 {
17554 /* Store the match cursor position */
17555 match_pos->lnum = curwin->w_cursor.lnum;
17556 match_pos->col = curwin->w_cursor.col + 1;
17557 }
17558
Bram Moolenaar071d4272004-06-13 20:20:40 +000017559 /* If 'n' flag is used or search failed: restore cursor position. */
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017560 if ((flags & SP_NOMOVE) || retval == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017561 curwin->w_cursor = save_cursor;
17562
17563theend:
17564 vim_free(pat2);
17565 vim_free(pat3);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000017566 if (p_cpo == empty_option)
17567 p_cpo = save_cpo;
17568 else
17569 /* Darn, evaluating the {skip} expression changed the value. */
17570 free_string_option(save_cpo);
Bram Moolenaar9fad3082005-07-19 22:22:13 +000017571
17572 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017573}
17574
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017575/*
17576 * "searchpos()" function
17577 */
17578 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017579f_searchpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017580{
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017581 pos_T match_pos;
17582 int lnum = 0;
17583 int col = 0;
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017584 int n;
17585 int flags = 0;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017586
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017587 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017588 return;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017589
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017590 n = search_cmn(argvars, &match_pos, &flags);
17591 if (n > 0)
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017592 {
17593 lnum = match_pos.lnum;
17594 col = match_pos.col;
17595 }
17596
Bram Moolenaareddf53b2006-02-27 00:11:10 +000017597 list_append_number(rettv->vval.v_list, (varnumber_T)lnum);
17598 list_append_number(rettv->vval.v_list, (varnumber_T)col);
Bram Moolenaar362e1a32006-03-06 23:29:24 +000017599 if (flags & SP_SUBPAT)
17600 list_append_number(rettv->vval.v_list, (varnumber_T)n);
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000017601}
17602
Bram Moolenaar0d660222005-01-07 21:51:51 +000017603 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017604f_server2client(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017605{
Bram Moolenaar0d660222005-01-07 21:51:51 +000017606#ifdef FEAT_CLIENTSERVER
17607 char_u buf[NUMBUFLEN];
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017608 char_u *server = get_tv_string_chk(&argvars[0]);
17609 char_u *reply = get_tv_string_buf_chk(&argvars[1], buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017610
Bram Moolenaar0d660222005-01-07 21:51:51 +000017611 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017612 if (server == NULL || reply == NULL)
17613 return;
Bram Moolenaar0d660222005-01-07 21:51:51 +000017614 if (check_restricted() || check_secure())
17615 return;
17616# ifdef FEAT_X11
17617 if (check_connection() == FAIL)
17618 return;
17619# endif
17620
17621 if (serverSendReply(server, reply) < 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017622 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000017623 EMSG(_("E258: Unable to send to client"));
17624 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017625 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000017626 rettv->vval.v_number = 0;
17627#else
17628 rettv->vval.v_number = -1;
17629#endif
17630}
17631
Bram Moolenaar0d660222005-01-07 21:51:51 +000017632 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017633f_serverlist(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000017634{
17635 char_u *r = NULL;
17636
17637#ifdef FEAT_CLIENTSERVER
17638# ifdef WIN32
17639 r = serverGetVimNames();
17640# else
17641 make_connection();
17642 if (X_DISPLAY != NULL)
17643 r = serverGetVimNames(X_DISPLAY);
17644# endif
17645#endif
17646 rettv->v_type = VAR_STRING;
17647 rettv->vval.v_string = r;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017648}
17649
17650/*
17651 * "setbufvar()" function
17652 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017653 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017654f_setbufvar(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017655{
17656 buf_T *buf;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017657 aco_save_T aco;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017658 char_u *varname, *bufvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000017659 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017660 char_u nbuf[NUMBUFLEN];
17661
17662 if (check_restricted() || check_secure())
17663 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017664 (void)get_tv_number(&argvars[0]); /* issue errmsg if type error */
17665 varname = get_tv_string_chk(&argvars[1]);
Bram Moolenaar0c279bb2013-03-19 14:25:54 +010017666 buf = get_buf_tv(&argvars[0], FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017667 varp = &argvars[2];
17668
17669 if (buf != NULL && varname != NULL && varp != NULL)
17670 {
17671 /* set curbuf to be our buf, temporarily */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017672 aucmd_prepbuf(&aco, buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017673
17674 if (*varname == '&')
17675 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017676 long numval;
17677 char_u *strval;
17678 int error = FALSE;
17679
Bram Moolenaar071d4272004-06-13 20:20:40 +000017680 ++varname;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017681 numval = get_tv_number_chk(varp, &error);
17682 strval = get_tv_string_buf_chk(varp, nbuf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017683 if (!error && strval != NULL)
17684 set_option_value(varname, numval, strval, OPT_LOCAL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017685 }
17686 else
17687 {
17688 bufvarname = alloc((unsigned)STRLEN(varname) + 3);
17689 if (bufvarname != NULL)
17690 {
17691 STRCPY(bufvarname, "b:");
17692 STRCPY(bufvarname + 2, varname);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000017693 set_var(bufvarname, varp, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017694 vim_free(bufvarname);
17695 }
17696 }
17697
17698 /* reset notion of buffer */
Bram Moolenaar071d4272004-06-13 20:20:40 +000017699 aucmd_restbuf(&aco);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017700 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000017701}
17702
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017703 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017704f_setcharsearch(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017705{
17706 dict_T *d;
17707 dictitem_T *di;
17708 char_u *csearch;
17709
17710 if (argvars[0].v_type != VAR_DICT)
17711 {
17712 EMSG(_(e_dictreq));
17713 return;
17714 }
17715
17716 if ((d = argvars[0].vval.v_dict) != NULL)
17717 {
17718 csearch = get_dict_string(d, (char_u *)"char", FALSE);
17719 if (csearch != NULL)
17720 {
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017721#ifdef FEAT_MBYTE
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017722 if (enc_utf8)
17723 {
17724 int pcc[MAX_MCO];
17725 int c = utfc_ptr2char(csearch, pcc);
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017726
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017727 set_last_csearch(c, csearch, utfc_ptr2len(csearch));
17728 }
17729 else
Bram Moolenaar8e8b4862015-08-12 22:56:58 +020017730#endif
Bram Moolenaar3cfd5282015-08-13 23:28:43 +020017731 set_last_csearch(PTR2CHAR(csearch),
17732 csearch, MB_PTR2LEN(csearch));
Bram Moolenaardbd24b52015-08-11 14:26:19 +020017733 }
17734
17735 di = dict_find(d, (char_u *)"forward", -1);
17736 if (di != NULL)
17737 set_csearch_direction(get_tv_number(&di->di_tv)
17738 ? FORWARD : BACKWARD);
17739
17740 di = dict_find(d, (char_u *)"until", -1);
17741 if (di != NULL)
17742 set_csearch_until(!!get_tv_number(&di->di_tv));
17743 }
17744}
17745
Bram Moolenaar071d4272004-06-13 20:20:40 +000017746/*
17747 * "setcmdpos()" function
17748 */
17749 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017750f_setcmdpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017751{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017752 int pos = (int)get_tv_number(&argvars[0]) - 1;
17753
17754 if (pos >= 0)
17755 rettv->vval.v_number = set_cmdline_pos(pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017756}
17757
17758/*
Bram Moolenaar80492532016-03-08 17:08:53 +010017759 * "setfperm({fname}, {mode})" function
17760 */
17761 static void
17762f_setfperm(typval_T *argvars, typval_T *rettv)
17763{
17764 char_u *fname;
17765 char_u modebuf[NUMBUFLEN];
17766 char_u *mode_str;
17767 int i;
17768 int mask;
17769 int mode = 0;
17770
17771 rettv->vval.v_number = 0;
17772 fname = get_tv_string_chk(&argvars[0]);
17773 if (fname == NULL)
17774 return;
17775 mode_str = get_tv_string_buf_chk(&argvars[1], modebuf);
17776 if (mode_str == NULL)
17777 return;
17778 if (STRLEN(mode_str) != 9)
17779 {
17780 EMSG2(_(e_invarg2), mode_str);
17781 return;
17782 }
17783
17784 mask = 1;
17785 for (i = 8; i >= 0; --i)
17786 {
17787 if (mode_str[i] != '-')
17788 mode |= mask;
17789 mask = mask << 1;
17790 }
17791 rettv->vval.v_number = mch_setperm(fname, mode) == OK;
17792}
17793
17794/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000017795 * "setline()" function
17796 */
17797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017798f_setline(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017799{
17800 linenr_T lnum;
Bram Moolenaar0e6830e2005-05-27 20:23:44 +000017801 char_u *line = NULL;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017802 list_T *l = NULL;
17803 listitem_T *li = NULL;
17804 long added = 0;
17805 linenr_T lcount = curbuf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017806
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017807 lnum = get_tv_lnum(&argvars[0]);
17808 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaar071d4272004-06-13 20:20:40 +000017809 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017810 l = argvars[1].vval.v_list;
17811 li = l->lv_first;
Bram Moolenaar071d4272004-06-13 20:20:40 +000017812 }
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017813 else
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017814 line = get_tv_string_chk(&argvars[1]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017815
Bram Moolenaar798b30b2009-04-22 10:56:16 +000017816 /* default result is zero == OK */
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017817 for (;;)
17818 {
17819 if (l != NULL)
17820 {
17821 /* list argument, get next string */
17822 if (li == NULL)
17823 break;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017824 line = get_tv_string_chk(&li->li_tv);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017825 li = li->li_next;
17826 }
17827
17828 rettv->vval.v_number = 1; /* FAIL */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017829 if (line == NULL || lnum < 1 || lnum > curbuf->b_ml.ml_line_count + 1)
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017830 break;
Bram Moolenaar3c1e9c22013-07-04 20:25:41 +020017831
17832 /* When coming here from Insert mode, sync undo, so that this can be
17833 * undone separately from what was previously inserted. */
17834 if (u_sync_once == 2)
17835 {
17836 u_sync_once = 1; /* notify that u_sync() was called */
17837 u_sync(TRUE);
17838 }
17839
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017840 if (lnum <= curbuf->b_ml.ml_line_count)
17841 {
17842 /* existing line, replace it */
17843 if (u_savesub(lnum) == OK && ml_replace(lnum, line, TRUE) == OK)
17844 {
17845 changed_bytes(lnum, 0);
Bram Moolenaar87c19962007-04-26 08:54:21 +000017846 if (lnum == curwin->w_cursor.lnum)
17847 check_cursor_col();
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000017848 rettv->vval.v_number = 0; /* OK */
17849 }
17850 }
17851 else if (added > 0 || u_save(lnum - 1, lnum) == OK)
17852 {
17853 /* lnum is one past the last line, append the line */
17854 ++added;
17855 if (ml_append(lnum - 1, line, (colnr_T)0, FALSE) == OK)
17856 rettv->vval.v_number = 0; /* OK */
17857 }
17858
17859 if (l == NULL) /* only one string argument */
17860 break;
17861 ++lnum;
17862 }
17863
17864 if (added > 0)
17865 appended_lines_mark(lcount, added);
Bram Moolenaar071d4272004-06-13 20:20:40 +000017866}
17867
Bram Moolenaar48e697e2016-01-23 22:17:30 +010017868static 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 +000017869
Bram Moolenaar071d4272004-06-13 20:20:40 +000017870/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017871 * Used by "setqflist()" and "setloclist()" functions
Bram Moolenaar2641f772005-03-25 21:58:17 +000017872 */
Bram Moolenaar2641f772005-03-25 21:58:17 +000017873 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017874set_qf_ll_list(
17875 win_T *wp UNUSED,
17876 typval_T *list_arg UNUSED,
17877 typval_T *action_arg UNUSED,
17878 typval_T *rettv)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017879{
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017880#ifdef FEAT_QUICKFIX
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017881 char_u *act;
17882 int action = ' ';
Bram Moolenaar0ac93792006-01-21 22:16:51 +000017883#endif
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017884
Bram Moolenaar2641f772005-03-25 21:58:17 +000017885 rettv->vval.v_number = -1;
17886
17887#ifdef FEAT_QUICKFIX
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017888 if (list_arg->v_type != VAR_LIST)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017889 EMSG(_(e_listreq));
17890 else
17891 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017892 list_T *l = list_arg->vval.v_list;
Bram Moolenaar2641f772005-03-25 21:58:17 +000017893
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017894 if (action_arg->v_type == VAR_STRING)
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017895 {
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017896 act = get_tv_string_chk(action_arg);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000017897 if (act == NULL)
17898 return; /* type error; errmsg already given */
Bram Moolenaarf4630b62005-05-20 21:31:17 +000017899 if (*act == 'a' || *act == 'r')
17900 action = *act;
17901 }
17902
Bram Moolenaar81484f42012-12-05 15:16:47 +010017903 if (l != NULL && set_errorlist(wp, l, action,
17904 (char_u *)(wp == NULL ? "setqflist()" : "setloclist()")) == OK)
Bram Moolenaar2641f772005-03-25 21:58:17 +000017905 rettv->vval.v_number = 0;
17906 }
17907#endif
17908}
17909
17910/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017911 * "setloclist()" function
17912 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017913 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017914f_setloclist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017915{
17916 win_T *win;
17917
17918 rettv->vval.v_number = -1;
17919
Bram Moolenaar99ebf042006-04-15 20:28:54 +000017920 win = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar17c7c012006-01-26 22:25:15 +000017921 if (win != NULL)
17922 set_qf_ll_list(win, &argvars[1], &argvars[2], rettv);
17923}
17924
17925/*
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017926 * "setmatches()" function
17927 */
17928 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010017929f_setmatches(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017930{
17931#ifdef FEAT_SEARCH_EXTRA
17932 list_T *l;
17933 listitem_T *li;
17934 dict_T *d;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017935 list_T *s = NULL;
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017936
17937 rettv->vval.v_number = -1;
17938 if (argvars[0].v_type != VAR_LIST)
17939 {
17940 EMSG(_(e_listreq));
17941 return;
17942 }
17943 if ((l = argvars[0].vval.v_list) != NULL)
17944 {
17945
17946 /* To some extent make sure that we are dealing with a list from
17947 * "getmatches()". */
17948 li = l->lv_first;
17949 while (li != NULL)
17950 {
17951 if (li->li_tv.v_type != VAR_DICT
17952 || (d = li->li_tv.vval.v_dict) == NULL)
17953 {
17954 EMSG(_(e_invarg));
17955 return;
17956 }
17957 if (!(dict_find(d, (char_u *)"group", -1) != NULL
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017958 && (dict_find(d, (char_u *)"pattern", -1) != NULL
17959 || dict_find(d, (char_u *)"pos1", -1) != NULL)
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017960 && dict_find(d, (char_u *)"priority", -1) != NULL
17961 && dict_find(d, (char_u *)"id", -1) != NULL))
17962 {
17963 EMSG(_(e_invarg));
17964 return;
17965 }
17966 li = li->li_next;
17967 }
17968
17969 clear_matches(curwin);
17970 li = l->lv_first;
17971 while (li != NULL)
17972 {
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017973 int i = 0;
Bram Moolenaar6a7e2a62015-06-19 21:06:11 +020017974 char_u buf[5];
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017975 dictitem_T *di;
Bram Moolenaar6561d522015-07-21 15:48:27 +020017976 char_u *group;
17977 int priority;
17978 int id;
17979 char_u *conceal;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017980
Bram Moolenaar6ee10162007-07-26 20:58:42 +000017981 d = li->li_tv.vval.v_dict;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020017982 if (dict_find(d, (char_u *)"pattern", -1) == NULL)
17983 {
17984 if (s == NULL)
17985 {
17986 s = list_alloc();
17987 if (s == NULL)
17988 return;
17989 }
17990
17991 /* match from matchaddpos() */
17992 for (i = 1; i < 9; i++)
17993 {
17994 sprintf((char *)buf, (char *)"pos%d", i);
17995 if ((di = dict_find(d, (char_u *)buf, -1)) != NULL)
17996 {
17997 if (di->di_tv.v_type != VAR_LIST)
17998 return;
17999
18000 list_append_tv(s, &di->di_tv);
18001 s->lv_refcount++;
18002 }
18003 else
18004 break;
18005 }
18006 }
Bram Moolenaar6561d522015-07-21 15:48:27 +020018007
18008 group = get_dict_string(d, (char_u *)"group", FALSE);
18009 priority = (int)get_dict_number(d, (char_u *)"priority");
18010 id = (int)get_dict_number(d, (char_u *)"id");
18011 conceal = dict_find(d, (char_u *)"conceal", -1) != NULL
18012 ? get_dict_string(d, (char_u *)"conceal", FALSE)
18013 : NULL;
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018014 if (i == 0)
18015 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018016 match_add(curwin, group,
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018017 get_dict_string(d, (char_u *)"pattern", FALSE),
Bram Moolenaar6561d522015-07-21 15:48:27 +020018018 priority, id, NULL, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018019 }
18020 else
18021 {
Bram Moolenaar6561d522015-07-21 15:48:27 +020018022 match_add(curwin, group, NULL, priority, id, s, conceal);
Bram Moolenaar0fce4252015-06-19 16:32:57 +020018023 list_unref(s);
18024 s = NULL;
18025 }
18026
Bram Moolenaar6ee10162007-07-26 20:58:42 +000018027 li = li->li_next;
18028 }
18029 rettv->vval.v_number = 0;
18030 }
18031#endif
18032}
18033
18034/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018035 * "setpos()" function
18036 */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018038f_setpos(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018039{
18040 pos_T pos;
18041 int fnum;
18042 char_u *name;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018043 colnr_T curswant = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018044
Bram Moolenaar08250432008-02-13 11:42:46 +000018045 rettv->vval.v_number = -1;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018046 name = get_tv_string_chk(argvars);
18047 if (name != NULL)
18048 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018049 if (list2fpos(&argvars[1], &pos, &fnum, &curswant) == OK)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018050 {
Bram Moolenaar742d1ec2009-12-31 12:18:30 +000018051 if (--pos.col < 0)
18052 pos.col = 0;
Bram Moolenaar08250432008-02-13 11:42:46 +000018053 if (name[0] == '.' && name[1] == NUL)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018054 {
Bram Moolenaar08250432008-02-13 11:42:46 +000018055 /* set cursor */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018056 if (fnum == curbuf->b_fnum)
18057 {
18058 curwin->w_cursor = pos;
Bram Moolenaar493c1782014-05-28 14:34:46 +020018059 if (curswant >= 0)
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018060 {
Bram Moolenaar493c1782014-05-28 14:34:46 +020018061 curwin->w_curswant = curswant - 1;
Bram Moolenaarc21d67e2015-12-31 22:27:55 +010018062 curwin->w_set_curswant = FALSE;
18063 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018064 check_cursor();
Bram Moolenaar08250432008-02-13 11:42:46 +000018065 rettv->vval.v_number = 0;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018066 }
18067 else
18068 EMSG(_(e_invarg));
18069 }
Bram Moolenaar08250432008-02-13 11:42:46 +000018070 else if (name[0] == '\'' && name[1] != NUL && name[2] == NUL)
18071 {
18072 /* set mark */
18073 if (setmark_pos(name[1], &pos, fnum) == OK)
18074 rettv->vval.v_number = 0;
18075 }
Bram Moolenaar0e34f622006-03-03 23:00:03 +000018076 else
18077 EMSG(_(e_invarg));
18078 }
18079 }
18080}
18081
18082/*
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018083 * "setqflist()" function
18084 */
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018085 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018086f_setqflist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar17c7c012006-01-26 22:25:15 +000018087{
18088 set_qf_ll_list(NULL, &argvars[0], &argvars[1], rettv);
18089}
18090
18091/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000018092 * "setreg()" function
18093 */
18094 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018095f_setreg(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018096{
18097 int regname;
18098 char_u *strregname;
18099 char_u *stropt;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018100 char_u *strval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018101 int append;
18102 char_u yank_type;
18103 long block_len;
18104
18105 block_len = -1;
18106 yank_type = MAUTO;
18107 append = FALSE;
18108
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018109 strregname = get_tv_string_chk(argvars);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018110 rettv->vval.v_number = 1; /* FAIL is default */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018111
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018112 if (strregname == NULL)
18113 return; /* type error; errmsg already given */
18114 regname = *strregname;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018115 if (regname == 0 || regname == '@')
18116 regname = '"';
Bram Moolenaar071d4272004-06-13 20:20:40 +000018117
Bram Moolenaar49cd9572005-01-03 21:06:01 +000018118 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018119 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018120 stropt = get_tv_string_chk(&argvars[2]);
18121 if (stropt == NULL)
18122 return; /* type error */
18123 for (; *stropt != NUL; ++stropt)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018124 switch (*stropt)
18125 {
18126 case 'a': case 'A': /* append */
18127 append = TRUE;
18128 break;
18129 case 'v': case 'c': /* character-wise selection */
18130 yank_type = MCHAR;
18131 break;
18132 case 'V': case 'l': /* line-wise selection */
18133 yank_type = MLINE;
18134 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018135 case 'b': case Ctrl_V: /* block-wise selection */
18136 yank_type = MBLOCK;
18137 if (VIM_ISDIGIT(stropt[1]))
18138 {
18139 ++stropt;
18140 block_len = getdigits(&stropt) - 1;
18141 --stropt;
18142 }
18143 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018144 }
18145 }
18146
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018147 if (argvars[1].v_type == VAR_LIST)
18148 {
18149 char_u **lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018150 char_u **allocval;
18151 char_u buf[NUMBUFLEN];
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018152 char_u **curval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018153 char_u **curallocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018154 int len = argvars[1].vval.v_list->lv_len;
18155 listitem_T *li;
18156
Bram Moolenaar7d647822014-04-05 21:28:56 +020018157 /* First half: use for pointers to result lines; second half: use for
18158 * pointers to allocated copies. */
18159 lstval = (char_u **)alloc(sizeof(char_u *) * ((len + 1) * 2));
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018160 if (lstval == NULL)
18161 return;
18162 curval = lstval;
Bram Moolenaar7d647822014-04-05 21:28:56 +020018163 allocval = lstval + len + 2;
18164 curallocval = allocval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018165
18166 for (li = argvars[1].vval.v_list->lv_first; li != NULL;
18167 li = li->li_next)
18168 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018169 strval = get_tv_string_buf_chk(&li->li_tv, buf);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018170 if (strval == NULL)
Bram Moolenaar7d647822014-04-05 21:28:56 +020018171 goto free_lstval;
18172 if (strval == buf)
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018173 {
Bram Moolenaar7d647822014-04-05 21:28:56 +020018174 /* Need to make a copy, next get_tv_string_buf_chk() will
18175 * overwrite the string. */
18176 strval = vim_strsave(buf);
18177 if (strval == NULL)
18178 goto free_lstval;
18179 *curallocval++ = strval;
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018180 }
18181 *curval++ = strval;
18182 }
18183 *curval++ = NULL;
18184
18185 write_reg_contents_lst(regname, lstval, -1,
18186 append, yank_type, block_len);
Bram Moolenaar7d647822014-04-05 21:28:56 +020018187free_lstval:
18188 while (curallocval > allocval)
18189 vim_free(*--curallocval);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018190 vim_free(lstval);
18191 }
18192 else
18193 {
18194 strval = get_tv_string_chk(&argvars[1]);
18195 if (strval == NULL)
18196 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018197 write_reg_contents_ex(regname, strval, -1,
Bram Moolenaar071d4272004-06-13 20:20:40 +000018198 append, yank_type, block_len);
Bram Moolenaar5a50c222014-04-02 22:17:10 +020018199 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000018200 rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018201}
18202
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018203/*
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018204 * "settabvar()" function
18205 */
18206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018207f_settabvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018208{
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018209#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018210 tabpage_T *save_curtab;
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018211 tabpage_T *tp;
18212#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018213 char_u *varname, *tabvarname;
18214 typval_T *varp;
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018215
18216 rettv->vval.v_number = 0;
18217
18218 if (check_restricted() || check_secure())
18219 return;
18220
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018221#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018222 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018223#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018224 varname = get_tv_string_chk(&argvars[1]);
18225 varp = &argvars[2];
18226
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018227 if (varname != NULL && varp != NULL
18228#ifdef FEAT_WINDOWS
18229 && tp != NULL
18230#endif
18231 )
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018232 {
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018233#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018234 save_curtab = curtab;
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018235 goto_tabpage_tp(tp, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018236#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018237
18238 tabvarname = alloc((unsigned)STRLEN(varname) + 3);
18239 if (tabvarname != NULL)
18240 {
18241 STRCPY(tabvarname, "t:");
18242 STRCPY(tabvarname + 2, varname);
18243 set_var(tabvarname, varp, TRUE);
18244 vim_free(tabvarname);
18245 }
18246
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018247#ifdef FEAT_WINDOWS
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018248 /* Restore current tabpage */
18249 if (valid_tabpage(save_curtab))
Bram Moolenaar49e649f2013-05-06 04:50:35 +020018250 goto_tabpage_tp(save_curtab, FALSE, FALSE);
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018251#endif
Bram Moolenaar06b5d512010-05-22 15:37:44 +020018252 }
18253}
18254
18255/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018256 * "settabwinvar()" function
18257 */
18258 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018259f_settabwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018260{
18261 setwinvar(argvars, rettv, 1);
18262}
Bram Moolenaar071d4272004-06-13 20:20:40 +000018263
18264/*
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018265 * "setwinvar()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018266 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018267 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018268f_setwinvar(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018269{
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018270 setwinvar(argvars, rettv, 0);
18271}
18272
18273/*
18274 * "setwinvar()" and "settabwinvar()" functions
18275 */
Bram Moolenaar84e0f6c2013-05-06 03:52:55 +020018276
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018277 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018278setwinvar(typval_T *argvars, typval_T *rettv UNUSED, int off)
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018279{
Bram Moolenaar071d4272004-06-13 20:20:40 +000018280 win_T *win;
18281#ifdef FEAT_WINDOWS
18282 win_T *save_curwin;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018283 tabpage_T *save_curtab;
Bram Moolenaarba117c22015-09-29 16:53:22 +020018284 int need_switch_win;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018285#endif
18286 char_u *varname, *winvarname;
Bram Moolenaar33570922005-01-25 22:26:29 +000018287 typval_T *varp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018288 char_u nbuf[NUMBUFLEN];
Bram Moolenaar8c0e3222013-06-16 17:32:40 +020018289 tabpage_T *tp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018290
18291 if (check_restricted() || check_secure())
18292 return;
Bram Moolenaar99ebf042006-04-15 20:28:54 +000018293
18294#ifdef FEAT_WINDOWS
18295 if (off == 1)
18296 tp = find_tabpage((int)get_tv_number_chk(&argvars[0], NULL));
18297 else
18298 tp = curtab;
18299#endif
18300 win = find_win_by_nr(&argvars[off], tp);
18301 varname = get_tv_string_chk(&argvars[off + 1]);
18302 varp = &argvars[off + 2];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018303
18304 if (win != NULL && varname != NULL && varp != NULL)
18305 {
18306#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018307 need_switch_win = !(tp == curtab && win == curwin);
18308 if (!need_switch_win
18309 || switch_win(&save_curwin, &save_curtab, win, tp, TRUE) == OK)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018310#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000018311 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018312 if (*varname == '&')
Bram Moolenaar071d4272004-06-13 20:20:40 +000018313 {
Bram Moolenaar5d2bae82014-09-19 14:26:36 +020018314 long numval;
18315 char_u *strval;
18316 int error = FALSE;
18317
18318 ++varname;
18319 numval = get_tv_number_chk(varp, &error);
18320 strval = get_tv_string_buf_chk(varp, nbuf);
18321 if (!error && strval != NULL)
18322 set_option_value(varname, numval, strval, OPT_LOCAL);
18323 }
18324 else
18325 {
18326 winvarname = alloc((unsigned)STRLEN(varname) + 3);
18327 if (winvarname != NULL)
18328 {
18329 STRCPY(winvarname, "w:");
18330 STRCPY(winvarname + 2, varname);
18331 set_var(winvarname, varp, TRUE);
18332 vim_free(winvarname);
18333 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018334 }
18335 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018336#ifdef FEAT_WINDOWS
Bram Moolenaarba117c22015-09-29 16:53:22 +020018337 if (need_switch_win)
18338 restore_win(save_curwin, save_curtab, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000018339#endif
18340 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018341}
18342
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018343#ifdef FEAT_CRYPT
18344/*
18345 * "sha256({string})" function
18346 */
18347 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018348f_sha256(typval_T *argvars, typval_T *rettv)
Bram Moolenaaraf9aeb92013-02-13 17:35:04 +010018349{
18350 char_u *p;
18351
18352 p = get_tv_string(&argvars[0]);
18353 rettv->vval.v_string = vim_strsave(
18354 sha256_bytes(p, (int)STRLEN(p), NULL, 0));
18355 rettv->v_type = VAR_STRING;
18356}
18357#endif /* FEAT_CRYPT */
18358
Bram Moolenaar071d4272004-06-13 20:20:40 +000018359/*
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018360 * "shellescape({string})" function
18361 */
18362 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018363f_shellescape(typval_T *argvars, typval_T *rettv)
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018364{
Bram Moolenaar05bb9532008-07-04 09:44:11 +000018365 rettv->vval.v_string = vim_strsave_shellescape(
Bram Moolenaar26df0922014-02-23 23:39:13 +010018366 get_tv_string(&argvars[0]), non_zero_arg(&argvars[1]), TRUE);
Bram Moolenaar60a495f2006-10-03 12:44:42 +000018367 rettv->v_type = VAR_STRING;
18368}
18369
18370/*
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018371 * shiftwidth() function
18372 */
18373 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018374f_shiftwidth(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018375{
Bram Moolenaar6bcbcc52013-11-05 07:13:41 +010018376 rettv->vval.v_number = get_sw_value(curbuf);
Bram Moolenaar2d17fa32012-10-21 00:45:18 +020018377}
18378
18379/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000018380 * "simplify()" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000018381 */
18382 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018383f_simplify(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018384{
Bram Moolenaar0d660222005-01-07 21:51:51 +000018385 char_u *p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018386
Bram Moolenaar0d660222005-01-07 21:51:51 +000018387 p = get_tv_string(&argvars[0]);
18388 rettv->vval.v_string = vim_strsave(p);
18389 simplify_filename(rettv->vval.v_string); /* simplify in place */
18390 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018391}
18392
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018393#ifdef FEAT_FLOAT
18394/*
18395 * "sin()" function
18396 */
18397 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018398f_sin(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018399{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018400 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018401
18402 rettv->v_type = VAR_FLOAT;
18403 if (get_float_arg(argvars, &f) == OK)
18404 rettv->vval.v_float = sin(f);
18405 else
18406 rettv->vval.v_float = 0.0;
18407}
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018408
18409/*
18410 * "sinh()" function
18411 */
18412 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018413f_sinh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018414{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010018415 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020018416
18417 rettv->v_type = VAR_FLOAT;
18418 if (get_float_arg(argvars, &f) == OK)
18419 rettv->vval.v_float = sinh(f);
18420 else
18421 rettv->vval.v_float = 0.0;
18422}
Bram Moolenaar8c8de832008-06-24 22:58:06 +000018423#endif
18424
Bram Moolenaar0d660222005-01-07 21:51:51 +000018425static int
18426#ifdef __BORLANDC__
18427 _RTLENTRYF
18428#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018429 item_compare(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018430static int
18431#ifdef __BORLANDC__
18432 _RTLENTRYF
18433#endif
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018434 item_compare2(const void *s1, const void *s2);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018435
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018436/* struct used in the array that's given to qsort() */
18437typedef struct
18438{
18439 listitem_T *item;
18440 int idx;
18441} sortItem_T;
18442
Bram Moolenaar0b962472016-02-22 22:51:33 +010018443/* struct storing information about current sort */
18444typedef struct
18445{
18446 int item_compare_ic;
18447 int item_compare_numeric;
18448 int item_compare_numbers;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018449#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018450 int item_compare_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018451#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018452 char_u *item_compare_func;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018453 partial_T *item_compare_partial;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018454 dict_T *item_compare_selfdict;
18455 int item_compare_func_err;
18456 int item_compare_keep_zero;
18457} sortinfo_T;
18458static sortinfo_T *sortinfo = NULL;
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018459static void do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018460#define ITEM_COMPARE_FAIL 999
18461
Bram Moolenaar071d4272004-06-13 20:20:40 +000018462/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018463 * Compare functions for f_sort() and f_uniq() below.
Bram Moolenaar071d4272004-06-13 20:20:40 +000018464 */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018465 static int
18466#ifdef __BORLANDC__
18467_RTLENTRYF
18468#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018469item_compare(const void *s1, const void *s2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018470{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018471 sortItem_T *si1, *si2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018472 typval_T *tv1, *tv2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018473 char_u *p1, *p2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018474 char_u *tofree1 = NULL, *tofree2 = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018475 int res;
18476 char_u numbuf1[NUMBUFLEN];
18477 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000018478
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018479 si1 = (sortItem_T *)s1;
18480 si2 = (sortItem_T *)s2;
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018481 tv1 = &si1->item->li_tv;
18482 tv2 = &si2->item->li_tv;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018483
Bram Moolenaar0b962472016-02-22 22:51:33 +010018484 if (sortinfo->item_compare_numbers)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018485 {
18486 long v1 = get_tv_number(tv1);
18487 long v2 = get_tv_number(tv2);
18488
18489 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18490 }
18491
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018492#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018493 if (sortinfo->item_compare_float)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018494 {
18495 float_T v1 = get_tv_float(tv1);
18496 float_T v2 = get_tv_float(tv2);
18497
18498 return v1 == v2 ? 0 : v1 > v2 ? 1 : -1;
18499 }
18500#endif
18501
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018502 /* tv2string() puts quotes around a string and allocates memory. Don't do
18503 * that for string variables. Use a single quote when comparing with a
18504 * non-string to do what the docs promise. */
18505 if (tv1->v_type == VAR_STRING)
18506 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018507 if (tv2->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018508 p1 = (char_u *)"'";
18509 else
18510 p1 = tv1->vval.v_string;
18511 }
18512 else
18513 p1 = tv2string(tv1, &tofree1, numbuf1, 0);
18514 if (tv2->v_type == VAR_STRING)
18515 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018516 if (tv1->v_type != VAR_STRING || sortinfo->item_compare_numeric)
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018517 p2 = (char_u *)"'";
18518 else
18519 p2 = tv2->vval.v_string;
18520 }
18521 else
18522 p2 = tv2string(tv2, &tofree2, numbuf2, 0);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000018523 if (p1 == NULL)
18524 p1 = (char_u *)"";
18525 if (p2 == NULL)
18526 p2 = (char_u *)"";
Bram Moolenaar0b962472016-02-22 22:51:33 +010018527 if (!sortinfo->item_compare_numeric)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018528 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018529 if (sortinfo->item_compare_ic)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018530 res = STRICMP(p1, p2);
18531 else
18532 res = STRCMP(p1, p2);
18533 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018534 else
Bram Moolenaare8a34922014-06-25 17:31:09 +020018535 {
18536 double n1, n2;
18537 n1 = strtod((char *)p1, (char **)&p1);
18538 n2 = strtod((char *)p2, (char **)&p2);
18539 res = n1 == n2 ? 0 : n1 > n2 ? 1 : -1;
18540 }
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018541
Bram Moolenaar1b338d22014-08-22 13:13:27 +020018542 /* When the result would be zero, compare the item indexes. Makes the
18543 * sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018544 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018545 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018546
Bram Moolenaar0d660222005-01-07 21:51:51 +000018547 vim_free(tofree1);
18548 vim_free(tofree2);
18549 return res;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018550}
18551
18552 static int
Bram Moolenaar0d660222005-01-07 21:51:51 +000018553#ifdef __BORLANDC__
18554_RTLENTRYF
Bram Moolenaar071d4272004-06-13 20:20:40 +000018555#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010018556item_compare2(const void *s1, const void *s2)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018557{
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018558 sortItem_T *si1, *si2;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018559 int res;
Bram Moolenaar33570922005-01-25 22:26:29 +000018560 typval_T rettv;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000018561 typval_T argv[3];
Bram Moolenaar0d660222005-01-07 21:51:51 +000018562 int dummy;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018563 char_u *func_name;
18564 partial_T *partial = sortinfo->item_compare_partial;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018565
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018566 /* shortcut after failure in previous call; compare all items equal */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018567 if (sortinfo->item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018568 return 0;
18569
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018570 si1 = (sortItem_T *)s1;
18571 si2 = (sortItem_T *)s2;
18572
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018573 if (partial == NULL)
18574 func_name = sortinfo->item_compare_func;
18575 else
18576 func_name = partial->pt_name;
18577
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018578 /* Copy the values. This is needed to be able to set v_lock to VAR_FIXED
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000018579 * in the copy without changing the original list items. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018580 copy_tv(&si1->item->li_tv, &argv[0]);
18581 copy_tv(&si2->item->li_tv, &argv[1]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018582
18583 rettv.v_type = VAR_UNKNOWN; /* clear_tv() uses this */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018584 res = call_func(func_name, (int)STRLEN(func_name),
Bram Moolenaar5f894962011-06-19 02:55:37 +020018585 &rettv, 2, argv, 0L, 0L, &dummy, TRUE,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018586 partial, sortinfo->item_compare_selfdict);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018587 clear_tv(&argv[0]);
18588 clear_tv(&argv[1]);
18589
18590 if (res == FAIL)
18591 res = ITEM_COMPARE_FAIL;
18592 else
Bram Moolenaar0b962472016-02-22 22:51:33 +010018593 res = get_tv_number_chk(&rettv, &sortinfo->item_compare_func_err);
18594 if (sortinfo->item_compare_func_err)
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000018595 res = ITEM_COMPARE_FAIL; /* return value has wrong type */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018596 clear_tv(&rettv);
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018597
18598 /* When the result would be zero, compare the pointers themselves. Makes
18599 * the sort stable. */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018600 if (res == 0 && !sortinfo->item_compare_keep_zero)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018601 res = si1->idx > si2->idx ? 1 : -1;
Bram Moolenaarc35e3de2014-07-02 19:06:18 +020018602
Bram Moolenaar0d660222005-01-07 21:51:51 +000018603 return res;
18604}
18605
18606/*
18607 * "sort({list})" function
18608 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018609 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018610do_sort_uniq(typval_T *argvars, typval_T *rettv, int sort)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018611{
Bram Moolenaar33570922005-01-25 22:26:29 +000018612 list_T *l;
18613 listitem_T *li;
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018614 sortItem_T *ptrs;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018615 sortinfo_T *old_sortinfo;
18616 sortinfo_T info;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018617 long len;
18618 long i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018619
Bram Moolenaar0b962472016-02-22 22:51:33 +010018620 /* Pointer to current info struct used in compare function. Save and
18621 * restore the current one for nested calls. */
18622 old_sortinfo = sortinfo;
18623 sortinfo = &info;
18624
Bram Moolenaar0d660222005-01-07 21:51:51 +000018625 if (argvars[0].v_type != VAR_LIST)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018626 EMSG2(_(e_listarg), sort ? "sort()" : "uniq()");
Bram Moolenaar071d4272004-06-13 20:20:40 +000018627 else
18628 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018629 l = argvars[0].vval.v_list;
Bram Moolenaar32f649e2011-04-11 13:46:13 +020018630 if (l == NULL || tv_check_lock(l->lv_lock,
Bram Moolenaar77354e72015-04-21 16:49:05 +020018631 (char_u *)(sort ? N_("sort() argument") : N_("uniq() argument")),
18632 TRUE))
Bram Moolenaar0b962472016-02-22 22:51:33 +010018633 goto theend;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018634 rettv->vval.v_list = l;
18635 rettv->v_type = VAR_LIST;
18636 ++l->lv_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018637
Bram Moolenaar0d660222005-01-07 21:51:51 +000018638 len = list_len(l);
18639 if (len <= 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018640 goto theend; /* short list sorts pretty quickly */
Bram Moolenaar071d4272004-06-13 20:20:40 +000018641
Bram Moolenaar0b962472016-02-22 22:51:33 +010018642 info.item_compare_ic = FALSE;
18643 info.item_compare_numeric = FALSE;
18644 info.item_compare_numbers = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018645#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018646 info.item_compare_float = FALSE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018647#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018648 info.item_compare_func = NULL;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018649 info.item_compare_partial = NULL;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018650 info.item_compare_selfdict = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018651 if (argvars[1].v_type != VAR_UNKNOWN)
18652 {
Bram Moolenaar5f894962011-06-19 02:55:37 +020018653 /* optional second argument: {func} */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018654 if (argvars[1].v_type == VAR_FUNC)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018655 info.item_compare_func = argvars[1].vval.v_string;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018656 else if (argvars[1].v_type == VAR_PARTIAL)
18657 info.item_compare_partial = argvars[1].vval.v_partial;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018658 else
18659 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018660 int error = FALSE;
18661
18662 i = get_tv_number_chk(&argvars[1], &error);
18663 if (error)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018664 goto theend; /* type error; errmsg already given */
Bram Moolenaar0d660222005-01-07 21:51:51 +000018665 if (i == 1)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018666 info.item_compare_ic = TRUE;
Bram Moolenaar5131c142016-02-29 22:05:26 +010018667 else if (argvars[1].v_type != VAR_NUMBER)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018668 info.item_compare_func = get_tv_string(&argvars[1]);
Bram Moolenaar5131c142016-02-29 22:05:26 +010018669 else if (i != 0)
18670 {
18671 EMSG(_(e_invarg));
18672 goto theend;
18673 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018674 if (info.item_compare_func != NULL)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018675 {
Bram Moolenaar5131c142016-02-29 22:05:26 +010018676 if (*info.item_compare_func == NUL)
18677 {
18678 /* empty string means default sort */
18679 info.item_compare_func = NULL;
18680 }
18681 else if (STRCMP(info.item_compare_func, "n") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018682 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018683 info.item_compare_func = NULL;
18684 info.item_compare_numeric = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018685 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018686 else if (STRCMP(info.item_compare_func, "N") == 0)
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018687 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018688 info.item_compare_func = NULL;
18689 info.item_compare_numbers = TRUE;
Bram Moolenaarb00da1d2015-12-03 16:33:12 +010018690 }
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018691#ifdef FEAT_FLOAT
Bram Moolenaar0b962472016-02-22 22:51:33 +010018692 else if (STRCMP(info.item_compare_func, "f") == 0)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018693 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018694 info.item_compare_func = NULL;
18695 info.item_compare_float = TRUE;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010018696 }
18697#endif
Bram Moolenaar0b962472016-02-22 22:51:33 +010018698 else if (STRCMP(info.item_compare_func, "i") == 0)
Bram Moolenaare8a34922014-06-25 17:31:09 +020018699 {
Bram Moolenaar0b962472016-02-22 22:51:33 +010018700 info.item_compare_func = NULL;
18701 info.item_compare_ic = TRUE;
Bram Moolenaare8a34922014-06-25 17:31:09 +020018702 }
18703 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018704 }
Bram Moolenaar5f894962011-06-19 02:55:37 +020018705
18706 if (argvars[2].v_type != VAR_UNKNOWN)
18707 {
18708 /* optional third argument: {dict} */
18709 if (argvars[2].v_type != VAR_DICT)
18710 {
18711 EMSG(_(e_dictreq));
Bram Moolenaar0b962472016-02-22 22:51:33 +010018712 goto theend;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018713 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018714 info.item_compare_selfdict = argvars[2].vval.v_dict;
Bram Moolenaar5f894962011-06-19 02:55:37 +020018715 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018716 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018717
Bram Moolenaar0d660222005-01-07 21:51:51 +000018718 /* Make an array with each entry pointing to an item in the List. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018719 ptrs = (sortItem_T *)alloc((int)(len * sizeof(sortItem_T)));
Bram Moolenaar0d660222005-01-07 21:51:51 +000018720 if (ptrs == NULL)
Bram Moolenaar0b962472016-02-22 22:51:33 +010018721 goto theend;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018722
Bram Moolenaar327aa022014-03-25 18:24:23 +010018723 i = 0;
18724 if (sort)
18725 {
18726 /* sort(): ptrs will be the list to sort */
18727 for (li = l->lv_first; li != NULL; li = li->li_next)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018728 {
18729 ptrs[i].item = li;
18730 ptrs[i].idx = i;
18731 ++i;
18732 }
Bram Moolenaar327aa022014-03-25 18:24:23 +010018733
Bram Moolenaar0b962472016-02-22 22:51:33 +010018734 info.item_compare_func_err = FALSE;
18735 info.item_compare_keep_zero = FALSE;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018736 /* test the compare function */
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018737 if ((info.item_compare_func != NULL
18738 || info.item_compare_partial != NULL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018739 && item_compare2((void *)&ptrs[0], (void *)&ptrs[1])
Bram Moolenaar0d660222005-01-07 21:51:51 +000018740 == ITEM_COMPARE_FAIL)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018741 EMSG(_("E702: Sort compare function failed"));
18742 else
18743 {
18744 /* Sort the array with item pointers. */
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018745 qsort((void *)ptrs, (size_t)len, sizeof(sortItem_T),
Bram Moolenaar0b962472016-02-22 22:51:33 +010018746 info.item_compare_func == NULL
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018747 && info.item_compare_partial == NULL
Bram Moolenaar0b962472016-02-22 22:51:33 +010018748 ? item_compare : item_compare2);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018749
Bram Moolenaar0b962472016-02-22 22:51:33 +010018750 if (!info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018751 {
18752 /* Clear the List and append the items in sorted order. */
18753 l->lv_first = l->lv_last = l->lv_idx_item = NULL;
18754 l->lv_len = 0;
18755 for (i = 0; i < len; ++i)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018756 list_append(l, ptrs[i].item);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018757 }
18758 }
18759 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000018760 else
Bram Moolenaar0d660222005-01-07 21:51:51 +000018761 {
Bram Moolenaar48e697e2016-01-23 22:17:30 +010018762 int (*item_compare_func_ptr)(const void *, const void *);
Bram Moolenaar327aa022014-03-25 18:24:23 +010018763
18764 /* f_uniq(): ptrs will be a stack of items to remove */
Bram Moolenaar0b962472016-02-22 22:51:33 +010018765 info.item_compare_func_err = FALSE;
18766 info.item_compare_keep_zero = TRUE;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010018767 item_compare_func_ptr = info.item_compare_func != NULL
18768 || info.item_compare_partial != NULL
Bram Moolenaar327aa022014-03-25 18:24:23 +010018769 ? item_compare2 : item_compare;
18770
18771 for (li = l->lv_first; li != NULL && li->li_next != NULL;
18772 li = li->li_next)
18773 {
18774 if (item_compare_func_ptr((void *)&li, (void *)&li->li_next)
18775 == 0)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018776 ptrs[i++].item = li;
Bram Moolenaar0b962472016-02-22 22:51:33 +010018777 if (info.item_compare_func_err)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018778 {
18779 EMSG(_("E882: Uniq compare function failed"));
18780 break;
18781 }
18782 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018783
Bram Moolenaar0b962472016-02-22 22:51:33 +010018784 if (!info.item_compare_func_err)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018785 {
Bram Moolenaar327aa022014-03-25 18:24:23 +010018786 while (--i >= 0)
18787 {
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018788 li = ptrs[i].item->li_next;
18789 ptrs[i].item->li_next = li->li_next;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018790 if (li->li_next != NULL)
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018791 li->li_next->li_prev = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018792 else
Bram Moolenaar6bf55482014-07-09 17:51:51 +020018793 l->lv_last = ptrs[i].item;
Bram Moolenaar327aa022014-03-25 18:24:23 +010018794 list_fix_watch(l, li);
18795 listitem_free(li);
18796 l->lv_len--;
18797 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018798 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000018799 }
18800
18801 vim_free(ptrs);
18802 }
Bram Moolenaar0b962472016-02-22 22:51:33 +010018803theend:
18804 sortinfo = old_sortinfo;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018805}
18806
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018807/*
Bram Moolenaar327aa022014-03-25 18:24:23 +010018808 * "sort({list})" function
18809 */
18810 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018811f_sort(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018812{
18813 do_sort_uniq(argvars, rettv, TRUE);
18814}
18815
18816/*
18817 * "uniq({list})" function
18818 */
18819 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018820f_uniq(typval_T *argvars, typval_T *rettv)
Bram Moolenaar327aa022014-03-25 18:24:23 +010018821{
18822 do_sort_uniq(argvars, rettv, FALSE);
18823}
18824
18825/*
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018826 * "soundfold({word})" function
18827 */
18828 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018829f_soundfold(typval_T *argvars, typval_T *rettv)
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018830{
18831 char_u *s;
18832
18833 rettv->v_type = VAR_STRING;
18834 s = get_tv_string(&argvars[0]);
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018835#ifdef FEAT_SPELL
Bram Moolenaar24bbcfe2005-06-28 23:32:02 +000018836 rettv->vval.v_string = eval_soundfold(s);
18837#else
18838 rettv->vval.v_string = vim_strsave(s);
18839#endif
18840}
18841
18842/*
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018843 * "spellbadword()" function
18844 */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018845 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018846f_spellbadword(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018847{
Bram Moolenaar4463f292005-09-25 22:20:24 +000018848 char_u *word = (char_u *)"";
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018849 hlf_T attr = HLF_COUNT;
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +000018850 int len = 0;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018851
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018852 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018853 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018854
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018855#ifdef FEAT_SPELL
Bram Moolenaar4463f292005-09-25 22:20:24 +000018856 if (argvars[0].v_type == VAR_UNKNOWN)
18857 {
18858 /* Find the start and length of the badly spelled word. */
18859 len = spell_move_to(curwin, FORWARD, TRUE, TRUE, &attr);
18860 if (len != 0)
18861 word = ml_get_cursor();
18862 }
Bram Moolenaar860cae12010-06-05 23:22:07 +020018863 else if (curwin->w_p_spell && *curbuf->b_s.b_p_spl != NUL)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018864 {
18865 char_u *str = get_tv_string_chk(&argvars[0]);
18866 int capcol = -1;
18867
18868 if (str != NULL)
18869 {
18870 /* Check the argument for spelling. */
18871 while (*str != NUL)
18872 {
Bram Moolenaar4770d092006-01-12 23:22:24 +000018873 len = spell_check(curwin, str, &attr, &capcol, FALSE);
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018874 if (attr != HLF_COUNT)
Bram Moolenaar4463f292005-09-25 22:20:24 +000018875 {
18876 word = str;
18877 break;
18878 }
18879 str += len;
18880 }
18881 }
18882 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018883#endif
Bram Moolenaar4463f292005-09-25 22:20:24 +000018884
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018885 list_append_string(rettv->vval.v_list, word, len);
18886 list_append_string(rettv->vval.v_list, (char_u *)(
Bram Moolenaar482aaeb2005-09-29 18:26:07 +000018887 attr == HLF_SPB ? "bad" :
18888 attr == HLF_SPR ? "rare" :
18889 attr == HLF_SPL ? "local" :
18890 attr == HLF_SPC ? "caps" :
18891 ""), -1);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018892}
18893
18894/*
18895 * "spellsuggest()" function
18896 */
18897 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018898f_spellsuggest(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018899{
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018900#ifdef FEAT_SPELL
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018901 char_u *str;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018902 int typeerr = FALSE;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018903 int maxcount;
18904 garray_T ga;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018905 int i;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018906 listitem_T *li;
18907 int need_capital = FALSE;
18908#endif
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018909
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018910 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018911 return;
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018912
Bram Moolenaar3c56a962006-03-12 22:19:04 +000018913#ifdef FEAT_SPELL
Bram Moolenaar860cae12010-06-05 23:22:07 +020018914 if (curwin->w_p_spell && *curwin->w_s->b_p_spl != NUL)
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018915 {
18916 str = get_tv_string(&argvars[0]);
18917 if (argvars[1].v_type != VAR_UNKNOWN)
18918 {
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018919 maxcount = get_tv_number_chk(&argvars[1], &typeerr);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018920 if (maxcount <= 0)
18921 return;
Bram Moolenaar69e0ff92005-09-30 21:23:56 +000018922 if (argvars[2].v_type != VAR_UNKNOWN)
18923 {
18924 need_capital = get_tv_number_chk(&argvars[2], &typeerr);
18925 if (typeerr)
18926 return;
18927 }
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018928 }
18929 else
18930 maxcount = 25;
18931
Bram Moolenaar4770d092006-01-12 23:22:24 +000018932 spell_suggest_list(&ga, str, maxcount, need_capital, FALSE);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018933
18934 for (i = 0; i < ga.ga_len; ++i)
18935 {
18936 str = ((char_u **)ga.ga_data)[i];
18937
18938 li = listitem_alloc();
18939 if (li == NULL)
18940 vim_free(str);
18941 else
18942 {
18943 li->li_tv.v_type = VAR_STRING;
18944 li->li_tv.v_lock = 0;
18945 li->li_tv.vval.v_string = str;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018946 list_append(rettv->vval.v_list, li);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000018947 }
18948 }
18949 ga_clear(&ga);
18950 }
18951#endif
18952}
18953
Bram Moolenaar0d660222005-01-07 21:51:51 +000018954 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010018955f_split(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018956{
18957 char_u *str;
18958 char_u *end;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018959 char_u *pat = NULL;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018960 regmatch_T regmatch;
18961 char_u patbuf[NUMBUFLEN];
18962 char_u *save_cpo;
18963 int match;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018964 colnr_T col = 0;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018965 int keepempty = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018966 int typeerr = FALSE;
Bram Moolenaar0d660222005-01-07 21:51:51 +000018967
18968 /* Make 'cpoptions' empty, the 'l' flag should not be used here. */
18969 save_cpo = p_cpo;
18970 p_cpo = (char_u *)"";
18971
18972 str = get_tv_string(&argvars[0]);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018973 if (argvars[1].v_type != VAR_UNKNOWN)
18974 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018975 pat = get_tv_string_buf_chk(&argvars[1], patbuf);
18976 if (pat == NULL)
18977 typeerr = TRUE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018978 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018979 keepempty = get_tv_number_chk(&argvars[2], &typeerr);
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018980 }
18981 if (pat == NULL || *pat == NUL)
18982 pat = (char_u *)"[\\x01- ]\\+";
Bram Moolenaar0d660222005-01-07 21:51:51 +000018983
Bram Moolenaareddf53b2006-02-27 00:11:10 +000018984 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018985 return;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000018986 if (typeerr)
18987 return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000018988
Bram Moolenaar0d660222005-01-07 21:51:51 +000018989 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
18990 if (regmatch.regprog != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000018991 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000018992 regmatch.rm_ic = FALSE;
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018993 while (*str != NUL || keepempty)
Bram Moolenaar0d660222005-01-07 21:51:51 +000018994 {
Bram Moolenaar67fe1a12005-05-22 22:12:58 +000018995 if (*str == NUL)
18996 match = FALSE; /* empty item at the end */
18997 else
18998 match = vim_regexec_nl(&regmatch, str, col);
Bram Moolenaar0d660222005-01-07 21:51:51 +000018999 if (match)
19000 end = regmatch.startp[0];
19001 else
19002 end = str + STRLEN(str);
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019003 if (keepempty || end > str || (rettv->vval.v_list->lv_len > 0
19004 && *str != NUL && match && end < regmatch.endp[0]))
Bram Moolenaar0d660222005-01-07 21:51:51 +000019005 {
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019006 if (list_append_string(rettv->vval.v_list, str,
19007 (int)(end - str)) == FAIL)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019008 break;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019009 }
19010 if (!match)
19011 break;
19012 /* Advance to just after the match. */
19013 if (regmatch.endp[0] > str)
19014 col = 0;
19015 else
19016 {
19017 /* Don't get stuck at the same match. */
19018#ifdef FEAT_MBYTE
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000019019 col = (*mb_ptr2len)(regmatch.endp[0]);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019020#else
19021 col = 1;
19022#endif
19023 }
19024 str = regmatch.endp[0];
19025 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019026
Bram Moolenaar473de612013-06-08 18:19:48 +020019027 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019028 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019029
Bram Moolenaar0d660222005-01-07 21:51:51 +000019030 p_cpo = save_cpo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019031}
19032
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019033#ifdef FEAT_FLOAT
19034/*
19035 * "sqrt()" function
19036 */
19037 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019038f_sqrt(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019039{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010019040 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019041
19042 rettv->v_type = VAR_FLOAT;
19043 if (get_float_arg(argvars, &f) == OK)
19044 rettv->vval.v_float = sqrt(f);
19045 else
19046 rettv->vval.v_float = 0.0;
19047}
19048
19049/*
19050 * "str2float()" function
19051 */
19052 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019053f_str2float(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019054{
19055 char_u *p = skipwhite(get_tv_string(&argvars[0]));
19056
19057 if (*p == '+')
19058 p = skipwhite(p + 1);
19059 (void)string2float(p, &rettv->vval.v_float);
19060 rettv->v_type = VAR_FLOAT;
19061}
19062#endif
19063
Bram Moolenaar2c932302006-03-18 21:42:09 +000019064/*
19065 * "str2nr()" function
19066 */
19067 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019068f_str2nr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019069{
19070 int base = 10;
19071 char_u *p;
19072 long n;
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019073 int what;
Bram Moolenaar2c932302006-03-18 21:42:09 +000019074
19075 if (argvars[1].v_type != VAR_UNKNOWN)
19076 {
19077 base = get_tv_number(&argvars[1]);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019078 if (base != 2 && base != 8 && base != 10 && base != 16)
Bram Moolenaar2c932302006-03-18 21:42:09 +000019079 {
19080 EMSG(_(e_invarg));
19081 return;
19082 }
19083 }
19084
19085 p = skipwhite(get_tv_string(&argvars[0]));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019086 if (*p == '+')
19087 p = skipwhite(p + 1);
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010019088 switch (base)
19089 {
19090 case 2: what = STR2NR_BIN + STR2NR_FORCE; break;
19091 case 8: what = STR2NR_OCT + STR2NR_FORCE; break;
19092 case 16: what = STR2NR_HEX + STR2NR_FORCE; break;
19093 default: what = 0;
19094 }
19095 vim_str2nr(p, NULL, NULL, what, &n, NULL, 0);
Bram Moolenaar2c932302006-03-18 21:42:09 +000019096 rettv->vval.v_number = n;
19097}
19098
Bram Moolenaar071d4272004-06-13 20:20:40 +000019099#ifdef HAVE_STRFTIME
19100/*
19101 * "strftime({format}[, {time}])" function
19102 */
19103 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019104f_strftime(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019105{
19106 char_u result_buf[256];
19107 struct tm *curtime;
19108 time_t seconds;
19109 char_u *p;
19110
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019111 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019112
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019113 p = get_tv_string(&argvars[0]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019114 if (argvars[1].v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019115 seconds = time(NULL);
19116 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019117 seconds = (time_t)get_tv_number(&argvars[1]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019118 curtime = localtime(&seconds);
19119 /* MSVC returns NULL for an invalid value of seconds. */
19120 if (curtime == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019121 rettv->vval.v_string = vim_strsave((char_u *)_("(Invalid)"));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019122 else
19123 {
19124# ifdef FEAT_MBYTE
19125 vimconv_T conv;
19126 char_u *enc;
19127
19128 conv.vc_type = CONV_NONE;
19129 enc = enc_locale();
19130 convert_setup(&conv, p_enc, enc);
19131 if (conv.vc_type != CONV_NONE)
19132 p = string_convert(&conv, p, NULL);
19133# endif
19134 if (p != NULL)
19135 (void)strftime((char *)result_buf, sizeof(result_buf),
19136 (char *)p, curtime);
19137 else
19138 result_buf[0] = NUL;
19139
19140# ifdef FEAT_MBYTE
19141 if (conv.vc_type != CONV_NONE)
19142 vim_free(p);
19143 convert_setup(&conv, enc, p_enc);
19144 if (conv.vc_type != CONV_NONE)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019145 rettv->vval.v_string = string_convert(&conv, result_buf, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019146 else
19147# endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019148 rettv->vval.v_string = vim_strsave(result_buf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019149
19150# ifdef FEAT_MBYTE
19151 /* Release conversion descriptors */
19152 convert_setup(&conv, NULL, NULL);
19153 vim_free(enc);
19154# endif
19155 }
19156}
19157#endif
19158
19159/*
19160 * "stridx()" function
19161 */
19162 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019163f_stridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019164{
19165 char_u buf[NUMBUFLEN];
19166 char_u *needle;
19167 char_u *haystack;
Bram Moolenaar33570922005-01-25 22:26:29 +000019168 char_u *save_haystack;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019169 char_u *pos;
Bram Moolenaar33570922005-01-25 22:26:29 +000019170 int start_idx;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019171
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019172 needle = get_tv_string_chk(&argvars[1]);
19173 save_haystack = haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar33570922005-01-25 22:26:29 +000019174 rettv->vval.v_number = -1;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019175 if (needle == NULL || haystack == NULL)
19176 return; /* type error; errmsg already given */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019177
Bram Moolenaar33570922005-01-25 22:26:29 +000019178 if (argvars[2].v_type != VAR_UNKNOWN)
19179 {
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019180 int error = FALSE;
19181
19182 start_idx = get_tv_number_chk(&argvars[2], &error);
19183 if (error || start_idx >= (int)STRLEN(haystack))
Bram Moolenaar33570922005-01-25 22:26:29 +000019184 return;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019185 if (start_idx >= 0)
19186 haystack += start_idx;
Bram Moolenaar33570922005-01-25 22:26:29 +000019187 }
19188
19189 pos = (char_u *)strstr((char *)haystack, (char *)needle);
19190 if (pos != NULL)
19191 rettv->vval.v_number = (varnumber_T)(pos - save_haystack);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019192}
19193
19194/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019195 * "string()" function
19196 */
19197 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019198f_string(typval_T *argvars, typval_T *rettv)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019199{
19200 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000019201 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019202
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019203 rettv->v_type = VAR_STRING;
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000019204 rettv->vval.v_string = tv2string(&argvars[0], &tofree, numbuf, 0);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000019205 /* Make a copy if we have a value but it's not in allocated memory. */
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000019206 if (rettv->vval.v_string != NULL && tofree == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019207 rettv->vval.v_string = vim_strsave(rettv->vval.v_string);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019208}
19209
19210/*
19211 * "strlen()" function
19212 */
19213 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019214f_strlen(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019215{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019216 rettv->vval.v_number = (varnumber_T)(STRLEN(
19217 get_tv_string(&argvars[0])));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019218}
19219
19220/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019221 * "strchars()" function
19222 */
19223 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019224f_strchars(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019225{
19226 char_u *s = get_tv_string(&argvars[0]);
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019227 int skipcc = 0;
Bram Moolenaar72597a52010-07-18 15:31:08 +020019228#ifdef FEAT_MBYTE
19229 varnumber_T len = 0;
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019230 int (*func_mb_ptr2char_adv)(char_u **pp);
Bram Moolenaar72597a52010-07-18 15:31:08 +020019231#endif
Bram Moolenaar641e48c2015-06-25 16:09:26 +020019232
19233 if (argvars[1].v_type != VAR_UNKNOWN)
19234 skipcc = get_tv_number_chk(&argvars[1], NULL);
19235 if (skipcc < 0 || skipcc > 1)
19236 EMSG(_(e_invarg));
19237 else
19238 {
19239#ifdef FEAT_MBYTE
19240 func_mb_ptr2char_adv = skipcc ? mb_ptr2char_adv : mb_cptr2char_adv;
19241 while (*s != NUL)
19242 {
19243 func_mb_ptr2char_adv(&s);
19244 ++len;
19245 }
19246 rettv->vval.v_number = len;
19247#else
19248 rettv->vval.v_number = (varnumber_T)(STRLEN(s));
19249#endif
19250 }
Bram Moolenaar72597a52010-07-18 15:31:08 +020019251}
19252
19253/*
Bram Moolenaardc536092010-07-18 15:45:49 +020019254 * "strdisplaywidth()" function
19255 */
19256 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019257f_strdisplaywidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaardc536092010-07-18 15:45:49 +020019258{
19259 char_u *s = get_tv_string(&argvars[0]);
19260 int col = 0;
19261
19262 if (argvars[1].v_type != VAR_UNKNOWN)
19263 col = get_tv_number(&argvars[1]);
19264
Bram Moolenaar8a09b982010-07-22 22:20:57 +020019265 rettv->vval.v_number = (varnumber_T)(linetabsize_col(col, s) - col);
Bram Moolenaardc536092010-07-18 15:45:49 +020019266}
19267
19268/*
Bram Moolenaar72597a52010-07-18 15:31:08 +020019269 * "strwidth()" function
19270 */
19271 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019272f_strwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar72597a52010-07-18 15:31:08 +020019273{
19274 char_u *s = get_tv_string(&argvars[0]);
19275
19276 rettv->vval.v_number = (varnumber_T)(
19277#ifdef FEAT_MBYTE
19278 mb_string2cells(s, -1)
19279#else
19280 STRLEN(s)
19281#endif
19282 );
19283}
19284
19285/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019286 * "strpart()" function
19287 */
19288 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019289f_strpart(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019290{
19291 char_u *p;
19292 int n;
19293 int len;
19294 int slen;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019295 int error = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019296
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019297 p = get_tv_string(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019298 slen = (int)STRLEN(p);
19299
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019300 n = get_tv_number_chk(&argvars[1], &error);
19301 if (error)
19302 len = 0;
19303 else if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019304 len = get_tv_number(&argvars[2]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019305 else
19306 len = slen - n; /* default len: all bytes that are available. */
19307
19308 /*
19309 * Only return the overlap between the specified part and the actual
19310 * string.
19311 */
19312 if (n < 0)
19313 {
19314 len += n;
19315 n = 0;
19316 }
19317 else if (n > slen)
19318 n = slen;
19319 if (len < 0)
19320 len = 0;
19321 else if (n + len > slen)
19322 len = slen - n;
19323
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019324 rettv->v_type = VAR_STRING;
19325 rettv->vval.v_string = vim_strnsave(p + n, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019326}
19327
19328/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019329 * "strridx()" function
19330 */
19331 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019332f_strridx(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019333{
19334 char_u buf[NUMBUFLEN];
19335 char_u *needle;
19336 char_u *haystack;
19337 char_u *rest;
19338 char_u *lastmatch = NULL;
Bram Moolenaar532c7802005-01-27 14:44:31 +000019339 int haystack_len, end_idx;
Bram Moolenaar0d660222005-01-07 21:51:51 +000019340
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019341 needle = get_tv_string_chk(&argvars[1]);
19342 haystack = get_tv_string_buf_chk(&argvars[0], buf);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019343
19344 rettv->vval.v_number = -1;
19345 if (needle == NULL || haystack == NULL)
19346 return; /* type error; errmsg already given */
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019347
19348 haystack_len = (int)STRLEN(haystack);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019349 if (argvars[2].v_type != VAR_UNKNOWN)
19350 {
19351 /* Third argument: upper limit for index */
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019352 end_idx = get_tv_number_chk(&argvars[2], NULL);
Bram Moolenaar05159a02005-02-26 23:04:13 +000019353 if (end_idx < 0)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019354 return; /* can never find a match */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019355 }
19356 else
19357 end_idx = haystack_len;
19358
Bram Moolenaar0d660222005-01-07 21:51:51 +000019359 if (*needle == NUL)
Bram Moolenaar05159a02005-02-26 23:04:13 +000019360 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019361 /* Empty string matches past the end. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000019362 lastmatch = haystack + end_idx;
19363 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019364 else
Bram Moolenaar532c7802005-01-27 14:44:31 +000019365 {
Bram Moolenaar0d660222005-01-07 21:51:51 +000019366 for (rest = haystack; *rest != '\0'; ++rest)
19367 {
19368 rest = (char_u *)strstr((char *)rest, (char *)needle);
Bram Moolenaar532c7802005-01-27 14:44:31 +000019369 if (rest == NULL || rest > haystack + end_idx)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019370 break;
19371 lastmatch = rest;
19372 }
Bram Moolenaar532c7802005-01-27 14:44:31 +000019373 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019374
19375 if (lastmatch == NULL)
19376 rettv->vval.v_number = -1;
19377 else
19378 rettv->vval.v_number = (varnumber_T)(lastmatch - haystack);
19379}
19380
19381/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000019382 * "strtrans()" function
19383 */
19384 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019385f_strtrans(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019386{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019387 rettv->v_type = VAR_STRING;
19388 rettv->vval.v_string = transstr(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000019389}
19390
19391/*
Bram Moolenaar0d660222005-01-07 21:51:51 +000019392 * "submatch()" function
19393 */
19394 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019395f_submatch(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019396{
Bram Moolenaar41571762014-04-02 19:00:58 +020019397 int error = FALSE;
Bram Moolenaar41571762014-04-02 19:00:58 +020019398 int no;
19399 int retList = 0;
19400
19401 no = (int)get_tv_number_chk(&argvars[0], &error);
19402 if (error)
19403 return;
19404 error = FALSE;
19405 if (argvars[1].v_type != VAR_UNKNOWN)
19406 retList = get_tv_number_chk(&argvars[1], &error);
19407 if (error)
19408 return;
19409
19410 if (retList == 0)
19411 {
19412 rettv->v_type = VAR_STRING;
19413 rettv->vval.v_string = reg_submatch(no);
19414 }
19415 else
19416 {
19417 rettv->v_type = VAR_LIST;
19418 rettv->vval.v_list = reg_submatch_list(no);
19419 }
Bram Moolenaar0d660222005-01-07 21:51:51 +000019420}
19421
19422/*
19423 * "substitute()" function
19424 */
19425 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019426f_substitute(typval_T *argvars, typval_T *rettv)
Bram Moolenaar0d660222005-01-07 21:51:51 +000019427{
19428 char_u patbuf[NUMBUFLEN];
19429 char_u subbuf[NUMBUFLEN];
19430 char_u flagsbuf[NUMBUFLEN];
19431
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019432 char_u *str = get_tv_string_chk(&argvars[0]);
19433 char_u *pat = get_tv_string_buf_chk(&argvars[1], patbuf);
19434 char_u *sub = get_tv_string_buf_chk(&argvars[2], subbuf);
19435 char_u *flg = get_tv_string_buf_chk(&argvars[3], flagsbuf);
19436
Bram Moolenaar0d660222005-01-07 21:51:51 +000019437 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019438 if (str == NULL || pat == NULL || sub == NULL || flg == NULL)
19439 rettv->vval.v_string = NULL;
19440 else
19441 rettv->vval.v_string = do_string_sub(str, pat, sub, flg);
Bram Moolenaar0d660222005-01-07 21:51:51 +000019442}
19443
19444/*
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019445 * "synID(lnum, col, trans)" function
Bram Moolenaar071d4272004-06-13 20:20:40 +000019446 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019447 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019448f_synID(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019449{
19450 int id = 0;
19451#ifdef FEAT_SYN_HL
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019452 long lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019453 long col;
19454 int trans;
Bram Moolenaar92124a32005-06-17 22:03:40 +000019455 int transerr = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019456
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019457 lnum = get_tv_lnum(argvars); /* -1 on type error */
19458 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19459 trans = get_tv_number_chk(&argvars[2], &transerr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019460
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000019461 if (!transerr && lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaar54ff3412005-04-20 19:48:33 +000019462 && col >= 0 && col < (long)STRLEN(ml_get(lnum)))
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019463 id = syn_get_id(curwin, lnum, (colnr_T)col, trans, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019464#endif
19465
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019466 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019467}
19468
19469/*
19470 * "synIDattr(id, what [, mode])" function
19471 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019472 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019473f_synIDattr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019474{
19475 char_u *p = NULL;
19476#ifdef FEAT_SYN_HL
19477 int id;
19478 char_u *what;
19479 char_u *mode;
19480 char_u modebuf[NUMBUFLEN];
19481 int modec;
19482
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019483 id = get_tv_number(&argvars[0]);
19484 what = get_tv_string(&argvars[1]);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019485 if (argvars[2].v_type != VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019486 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019487 mode = get_tv_string_buf(&argvars[2], modebuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019488 modec = TOLOWER_ASC(mode[0]);
Bram Moolenaar61623362010-07-14 22:04:22 +020019489 if (modec != 't' && modec != 'c' && modec != 'g')
Bram Moolenaar071d4272004-06-13 20:20:40 +000019490 modec = 0; /* replace invalid with current */
19491 }
19492 else
19493 {
19494#ifdef FEAT_GUI
19495 if (gui.in_use)
19496 modec = 'g';
19497 else
19498#endif
19499 if (t_colors > 1)
19500 modec = 'c';
19501 else
19502 modec = 't';
19503 }
19504
19505
19506 switch (TOLOWER_ASC(what[0]))
19507 {
19508 case 'b':
19509 if (TOLOWER_ASC(what[1]) == 'g') /* bg[#] */
19510 p = highlight_color(id, what, modec);
19511 else /* bold */
19512 p = highlight_has_attr(id, HL_BOLD, modec);
19513 break;
19514
Bram Moolenaar12682fd2010-03-10 13:43:49 +010019515 case 'f': /* fg[#] or font */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019516 p = highlight_color(id, what, modec);
19517 break;
19518
19519 case 'i':
19520 if (TOLOWER_ASC(what[1]) == 'n') /* inverse */
19521 p = highlight_has_attr(id, HL_INVERSE, modec);
19522 else /* italic */
19523 p = highlight_has_attr(id, HL_ITALIC, modec);
19524 break;
19525
19526 case 'n': /* name */
19527 p = get_highlight_name(NULL, id - 1);
19528 break;
19529
19530 case 'r': /* reverse */
19531 p = highlight_has_attr(id, HL_INVERSE, modec);
19532 break;
19533
Bram Moolenaar6f507d62008-11-28 10:16:05 +000019534 case 's':
19535 if (TOLOWER_ASC(what[1]) == 'p') /* sp[#] */
19536 p = highlight_color(id, what, modec);
19537 else /* standout */
19538 p = highlight_has_attr(id, HL_STANDOUT, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019539 break;
19540
Bram Moolenaar5b743bf2005-03-15 22:50:43 +000019541 case 'u':
19542 if (STRLEN(what) <= 5 || TOLOWER_ASC(what[5]) != 'c')
19543 /* underline */
19544 p = highlight_has_attr(id, HL_UNDERLINE, modec);
19545 else
19546 /* undercurl */
19547 p = highlight_has_attr(id, HL_UNDERCURL, modec);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019548 break;
19549 }
19550
19551 if (p != NULL)
19552 p = vim_strsave(p);
19553#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019554 rettv->v_type = VAR_STRING;
19555 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019556}
19557
19558/*
19559 * "synIDtrans(id)" function
19560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000019561 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019562f_synIDtrans(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019563{
19564 int id;
19565
19566#ifdef FEAT_SYN_HL
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019567 id = get_tv_number(&argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019568
19569 if (id > 0)
19570 id = syn_get_final_id(id);
19571 else
19572#endif
19573 id = 0;
19574
Bram Moolenaarc70646c2005-01-04 21:52:38 +000019575 rettv->vval.v_number = id;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019576}
19577
19578/*
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019579 * "synconcealed(lnum, col)" function
19580 */
19581 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019582f_synconcealed(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7510fe72010-07-25 12:46:44 +020019583{
19584#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19585 long lnum;
19586 long col;
19587 int syntax_flags = 0;
19588 int cchar;
19589 int matchid = 0;
19590 char_u str[NUMBUFLEN];
19591#endif
19592
19593 rettv->v_type = VAR_LIST;
19594 rettv->vval.v_list = NULL;
19595
19596#if defined(FEAT_SYN_HL) && defined(FEAT_CONCEAL)
19597 lnum = get_tv_lnum(argvars); /* -1 on type error */
19598 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19599
19600 vim_memset(str, NUL, sizeof(str));
19601
19602 if (rettv_list_alloc(rettv) != FAIL)
19603 {
19604 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
19605 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
19606 && curwin->w_p_cole > 0)
19607 {
19608 (void)syn_get_id(curwin, lnum, col, FALSE, NULL, FALSE);
19609 syntax_flags = get_syntax_info(&matchid);
19610
19611 /* get the conceal character */
19612 if ((syntax_flags & HL_CONCEAL) && curwin->w_p_cole < 3)
19613 {
19614 cchar = syn_get_sub_char();
19615 if (cchar == NUL && curwin->w_p_cole == 1 && lcs_conceal != NUL)
19616 cchar = lcs_conceal;
19617 if (cchar != NUL)
19618 {
19619# ifdef FEAT_MBYTE
19620 if (has_mbyte)
19621 (*mb_char2bytes)(cchar, str);
19622 else
19623# endif
19624 str[0] = cchar;
19625 }
19626 }
19627 }
19628
19629 list_append_number(rettv->vval.v_list,
19630 (syntax_flags & HL_CONCEAL) != 0);
19631 /* -1 to auto-determine strlen */
19632 list_append_string(rettv->vval.v_list, str, -1);
19633 list_append_number(rettv->vval.v_list, matchid);
19634 }
19635#endif
19636}
19637
19638/*
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019639 * "synstack(lnum, col)" function
19640 */
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019641 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019642f_synstack(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019643{
19644#ifdef FEAT_SYN_HL
19645 long lnum;
19646 long col;
19647 int i;
19648 int id;
19649#endif
19650
19651 rettv->v_type = VAR_LIST;
19652 rettv->vval.v_list = NULL;
19653
19654#ifdef FEAT_SYN_HL
19655 lnum = get_tv_lnum(argvars); /* -1 on type error */
19656 col = get_tv_number(&argvars[1]) - 1; /* -1 on type error */
19657
19658 if (lnum >= 1 && lnum <= curbuf->b_ml.ml_line_count
Bram Moolenaard04b7502010-07-08 22:27:55 +020019659 && col >= 0 && col <= (long)STRLEN(ml_get(lnum))
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019660 && rettv_list_alloc(rettv) != FAIL)
19661 {
Bram Moolenaar56cefaf2008-01-12 15:47:10 +000019662 (void)syn_get_id(curwin, lnum, (colnr_T)col, FALSE, NULL, TRUE);
Bram Moolenaar9d188ab2008-01-10 21:24:39 +000019663 for (i = 0; ; ++i)
19664 {
19665 id = syn_get_stack_item(i);
19666 if (id < 0)
19667 break;
19668 if (list_append_number(rettv->vval.v_list, id) == FAIL)
19669 break;
19670 }
19671 }
19672#endif
19673}
19674
Bram Moolenaar071d4272004-06-13 20:20:40 +000019675 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019676get_cmd_output_as_rettv(
19677 typval_T *argvars,
19678 typval_T *rettv,
19679 int retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019680{
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019681 char_u *res = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019682 char_u *p;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019683 char_u *infile = NULL;
19684 char_u buf[NUMBUFLEN];
19685 int err = FALSE;
19686 FILE *fd;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019687 list_T *list = NULL;
Bram Moolenaar52a72462014-08-29 15:53:52 +020019688 int flags = SHELL_SILENT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019689
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019690 rettv->v_type = VAR_STRING;
19691 rettv->vval.v_string = NULL;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019692 if (check_restricted() || check_secure())
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019693 goto errret;
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000019694
Bram Moolenaar49cd9572005-01-03 21:06:01 +000019695 if (argvars[1].v_type != VAR_UNKNOWN)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019696 {
19697 /*
19698 * Write the string to a temp file, to be used for input of the shell
19699 * command.
19700 */
Bram Moolenaare5c421c2015-03-31 13:33:08 +020019701 if ((infile = vim_tempname('i', TRUE)) == NULL)
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019702 {
19703 EMSG(_(e_notmp));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019704 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019705 }
19706
19707 fd = mch_fopen((char *)infile, WRITEBIN);
19708 if (fd == NULL)
19709 {
19710 EMSG2(_(e_notopen), infile);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019711 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019712 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019713 if (argvars[1].v_type == VAR_LIST)
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019714 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019715 if (write_list(fd, argvars[1].vval.v_list, TRUE) == FAIL)
19716 err = TRUE;
Bram Moolenaareb3593b2006-04-22 22:33:57 +000019717 }
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019718 else
19719 {
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019720 size_t len;
19721
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019722 p = get_tv_string_buf_chk(&argvars[1], buf);
19723 if (p == NULL)
19724 {
19725 fclose(fd);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019726 goto errret; /* type error; errmsg already given */
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019727 }
Bram Moolenaar1ecfd9c2014-09-19 20:45:23 +020019728 len = STRLEN(p);
19729 if (len > 0 && fwrite(p, len, 1, fd) != 1)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020019730 err = TRUE;
19731 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019732 if (fclose(fd) != 0)
19733 err = TRUE;
19734 if (err)
19735 {
19736 EMSG(_("E677: Error writing temp file"));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019737 goto errret;
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019738 }
19739 }
19740
Bram Moolenaar52a72462014-08-29 15:53:52 +020019741 /* Omit SHELL_COOKED when invoked with ":silent". Avoids that the shell
19742 * echoes typeahead, that messes up the display. */
19743 if (!msg_silent)
19744 flags += SHELL_COOKED;
19745
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019746 if (retlist)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019747 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019748 int len;
19749 listitem_T *li;
19750 char_u *s = NULL;
19751 char_u *start;
19752 char_u *end;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019753 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019754
Bram Moolenaar52a72462014-08-29 15:53:52 +020019755 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, &len);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019756 if (res == NULL)
19757 goto errret;
19758
19759 list = list_alloc();
19760 if (list == NULL)
19761 goto errret;
19762
19763 for (i = 0; i < len; ++i)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019764 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019765 start = res + i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019766 while (i < len && res[i] != NL)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019767 ++i;
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019768 end = res + i;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019769
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019770 s = alloc((unsigned)(end - start + 1));
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019771 if (s == NULL)
19772 goto errret;
19773
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019774 for (p = s; start < end; ++p, ++start)
19775 *p = *start == NUL ? NL : *start;
19776 *p = NUL;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019777
19778 li = listitem_alloc();
19779 if (li == NULL)
19780 {
19781 vim_free(s);
19782 goto errret;
19783 }
19784 li->li_tv.v_type = VAR_STRING;
Bram Moolenaar9a492d42015-01-27 13:49:31 +010019785 li->li_tv.v_lock = 0;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019786 li->li_tv.vval.v_string = s;
19787 list_append(list, li);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019788 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019789
Bram Moolenaarb21a29b2014-04-11 10:22:53 +020019790 ++list->lv_refcount;
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019791 rettv->v_type = VAR_LIST;
19792 rettv->vval.v_list = list;
19793 list = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019794 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019795 else
19796 {
Bram Moolenaar52a72462014-08-29 15:53:52 +020019797 res = get_cmd_output(get_tv_string(&argvars[0]), infile, flags, NULL);
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019798#ifdef USE_CR
19799 /* translate <CR> into <NL> */
19800 if (res != NULL)
19801 {
19802 char_u *s;
19803
19804 for (s = res; *s; ++s)
19805 {
19806 if (*s == CAR)
19807 *s = NL;
19808 }
19809 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019810#else
19811# ifdef USE_CRNL
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019812 /* translate <CR><NL> into <NL> */
19813 if (res != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000019814 {
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019815 char_u *s, *d;
19816
19817 d = res;
19818 for (s = res; *s; ++s)
19819 {
19820 if (s[0] == CAR && s[1] == NL)
19821 ++s;
19822 *d++ = *s;
19823 }
19824 *d = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000019825 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000019826# endif
19827#endif
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019828 rettv->vval.v_string = res;
19829 res = NULL;
19830 }
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019831
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019832errret:
Bram Moolenaarc0197e22004-09-13 20:26:32 +000019833 if (infile != NULL)
19834 {
19835 mch_remove(infile);
19836 vim_free(infile);
19837 }
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019838 if (res != NULL)
19839 vim_free(res);
19840 if (list != NULL)
19841 list_free(list, TRUE);
19842}
19843
19844/*
19845 * "system()" function
19846 */
19847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019848f_system(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019849{
19850 get_cmd_output_as_rettv(argvars, rettv, FALSE);
19851}
19852
19853/*
19854 * "systemlist()" function
19855 */
19856 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019857f_systemlist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar39c29ed2014-04-05 19:44:40 +020019858{
19859 get_cmd_output_as_rettv(argvars, rettv, TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000019860}
19861
19862/*
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019863 * "tabpagebuflist()" function
19864 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019865 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019866f_tabpagebuflist(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019867{
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019868#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019869 tabpage_T *tp;
19870 win_T *wp = NULL;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019871
19872 if (argvars[0].v_type == VAR_UNKNOWN)
19873 wp = firstwin;
19874 else
19875 {
19876 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19877 if (tp != NULL)
Bram Moolenaar238a5642006-02-21 22:12:05 +000019878 wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019879 }
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019880 if (wp != NULL && rettv_list_alloc(rettv) != FAIL)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019881 {
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019882 for (; wp != NULL; wp = wp->w_next)
19883 if (list_append_number(rettv->vval.v_list,
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019884 wp->w_buffer->b_fnum) == FAIL)
Bram Moolenaar798b30b2009-04-22 10:56:16 +000019885 break;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019886 }
19887#endif
19888}
19889
19890
19891/*
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019892 * "tabpagenr()" function
19893 */
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019894 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019895f_tabpagenr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019896{
19897 int nr = 1;
19898#ifdef FEAT_WINDOWS
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019899 char_u *arg;
19900
19901 if (argvars[0].v_type != VAR_UNKNOWN)
19902 {
19903 arg = get_tv_string_chk(&argvars[0]);
19904 nr = 0;
19905 if (arg != NULL)
19906 {
19907 if (STRCMP(arg, "$") == 0)
Bram Moolenaara5621492006-02-25 21:55:24 +000019908 nr = tabpage_index(NULL) - 1;
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019909 else
19910 EMSG2(_(e_invexpr2), arg);
19911 }
19912 }
19913 else
Bram Moolenaar32466aa2006-02-24 23:53:04 +000019914 nr = tabpage_index(curtab);
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019915#endif
19916 rettv->vval.v_number = nr;
19917}
19918
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019919
19920#ifdef FEAT_WINDOWS
Bram Moolenaar48e697e2016-01-23 22:17:30 +010019921static int get_winnr(tabpage_T *tp, typval_T *argvar);
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019922
19923/*
19924 * Common code for tabpagewinnr() and winnr().
19925 */
19926 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010019927get_winnr(tabpage_T *tp, typval_T *argvar)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019928{
19929 win_T *twin;
19930 int nr = 1;
19931 win_T *wp;
19932 char_u *arg;
19933
19934 twin = (tp == curtab) ? curwin : tp->tp_curwin;
19935 if (argvar->v_type != VAR_UNKNOWN)
19936 {
19937 arg = get_tv_string_chk(argvar);
19938 if (arg == NULL)
19939 nr = 0; /* type error; errmsg already given */
19940 else if (STRCMP(arg, "$") == 0)
19941 twin = (tp == curtab) ? lastwin : tp->tp_lastwin;
19942 else if (STRCMP(arg, "#") == 0)
19943 {
19944 twin = (tp == curtab) ? prevwin : tp->tp_prevwin;
19945 if (twin == NULL)
19946 nr = 0;
19947 }
19948 else
19949 {
19950 EMSG2(_(e_invexpr2), arg);
19951 nr = 0;
19952 }
19953 }
19954
19955 if (nr > 0)
19956 for (wp = (tp == curtab) ? firstwin : tp->tp_firstwin;
19957 wp != twin; wp = wp->w_next)
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019958 {
19959 if (wp == NULL)
19960 {
19961 /* didn't find it in this tabpage */
19962 nr = 0;
19963 break;
19964 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019965 ++nr;
Bram Moolenaar4940e3f2007-03-25 15:49:08 +000019966 }
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019967 return nr;
19968}
19969#endif
19970
19971/*
19972 * "tabpagewinnr()" function
19973 */
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019974 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019975f_tabpagewinnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000019976{
19977 int nr = 1;
19978#ifdef FEAT_WINDOWS
19979 tabpage_T *tp;
19980
19981 tp = find_tabpage((int)get_tv_number(&argvars[0]));
19982 if (tp == NULL)
19983 nr = 0;
19984 else
19985 nr = get_winnr(tp, &argvars[1]);
19986#endif
19987 rettv->vval.v_number = nr;
19988}
19989
19990
Bram Moolenaar7e8fd632006-02-18 22:14:51 +000019991/*
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019992 * "tagfiles()" function
19993 */
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019994 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010019995f_tagfiles(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000019996{
Bram Moolenaard9462e32011-04-11 21:35:11 +020019997 char_u *fname;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000019998 tagname_T tn;
19999 int first;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020000
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020001 if (rettv_list_alloc(rettv) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020002 return;
Bram Moolenaard9462e32011-04-11 21:35:11 +020020003 fname = alloc(MAXPATHL);
20004 if (fname == NULL)
20005 return;
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020006
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020007 for (first = TRUE; ; first = FALSE)
20008 if (get_tagfname(&tn, first, fname) == FAIL
20009 || list_append_string(rettv->vval.v_list, fname, -1) == FAIL)
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020010 break;
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020011 tagname_free(&tn);
Bram Moolenaard9462e32011-04-11 21:35:11 +020020012 vim_free(fname);
Bram Moolenaard43b6cf2005-09-09 19:53:42 +000020013}
20014
20015/*
Bram Moolenaare2ac10d2005-03-07 23:26:06 +000020016 * "taglist()" function
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020017 */
20018 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020019f_taglist(typval_T *argvars, typval_T *rettv)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020020{
20021 char_u *tag_pattern;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020022
20023 tag_pattern = get_tv_string(&argvars[0]);
20024
20025 rettv->vval.v_number = FALSE;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020026 if (*tag_pattern == NUL)
20027 return;
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020028
Bram Moolenaareddf53b2006-02-27 00:11:10 +000020029 if (rettv_list_alloc(rettv) == OK)
20030 (void)get_tags(rettv->vval.v_list, tag_pattern);
Bram Moolenaar19a09a12005-03-04 23:39:37 +000020031}
20032
20033/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020034 * "tempname()" function
20035 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020036 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020037f_tempname(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020038{
20039 static int x = 'A';
20040
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020041 rettv->v_type = VAR_STRING;
Bram Moolenaare5c421c2015-03-31 13:33:08 +020020042 rettv->vval.v_string = vim_tempname(x, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020043
20044 /* Advance 'x' to use A-Z and 0-9, so that there are at least 34 different
20045 * names. Skip 'I' and 'O', they are used for shell redirection. */
20046 do
20047 {
20048 if (x == 'Z')
20049 x = '0';
20050 else if (x == '9')
20051 x = 'A';
20052 else
20053 {
20054#ifdef EBCDIC
20055 if (x == 'I')
20056 x = 'J';
20057 else if (x == 'R')
20058 x = 'S';
20059 else
20060#endif
20061 ++x;
20062 }
20063 } while (x == 'I' || x == 'O');
20064}
20065
20066/*
Bram Moolenaard52d9742005-08-21 22:20:28 +000020067 * "test(list)" function: Just checking the walls...
20068 */
Bram Moolenaard52d9742005-08-21 22:20:28 +000020069 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020070f_test(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaard52d9742005-08-21 22:20:28 +000020071{
20072 /* Used for unit testing. Change the code below to your liking. */
20073#if 0
20074 listitem_T *li;
20075 list_T *l;
20076 char_u *bad, *good;
20077
20078 if (argvars[0].v_type != VAR_LIST)
20079 return;
20080 l = argvars[0].vval.v_list;
20081 if (l == NULL)
20082 return;
20083 li = l->lv_first;
20084 if (li == NULL)
20085 return;
20086 bad = get_tv_string(&li->li_tv);
20087 li = li->li_next;
20088 if (li == NULL)
20089 return;
20090 good = get_tv_string(&li->li_tv);
20091 rettv->vval.v_number = test_edit_score(bad, good);
20092#endif
20093}
20094
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020095#ifdef FEAT_FLOAT
20096/*
20097 * "tan()" function
20098 */
20099 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020100f_tan(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020101{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020102 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020103
20104 rettv->v_type = VAR_FLOAT;
20105 if (get_float_arg(argvars, &f) == OK)
20106 rettv->vval.v_float = tan(f);
20107 else
20108 rettv->vval.v_float = 0.0;
20109}
20110
20111/*
20112 * "tanh()" function
20113 */
20114 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020115f_tanh(typval_T *argvars, typval_T *rettv)
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020116{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020117 float_T f = 0.0;
Bram Moolenaardb7c6862010-05-21 16:33:48 +020020118
20119 rettv->v_type = VAR_FLOAT;
20120 if (get_float_arg(argvars, &f) == OK)
20121 rettv->vval.v_float = tanh(f);
20122 else
20123 rettv->vval.v_float = 0.0;
20124}
20125#endif
20126
Bram Moolenaar975b5272016-03-15 23:10:59 +010020127#if defined(FEAT_JOB_CHANNEL) || defined(FEAT_TIMERS) || defined(PROTO)
20128/*
20129 * Get a callback from "arg". It can be a Funcref or a function name.
20130 * When "arg" is zero return an empty string.
20131 * Return NULL for an invalid argument.
20132 */
20133 char_u *
20134get_callback(typval_T *arg, partial_T **pp)
20135{
20136 if (arg->v_type == VAR_PARTIAL && arg->vval.v_partial != NULL)
20137 {
20138 *pp = arg->vval.v_partial;
20139 return (*pp)->pt_name;
20140 }
20141 *pp = NULL;
20142 if (arg->v_type == VAR_FUNC || arg->v_type == VAR_STRING)
20143 return arg->vval.v_string;
20144 if (arg->v_type == VAR_NUMBER && arg->vval.v_number == 0)
20145 return (char_u *)"";
20146 EMSG(_("E921: Invalid callback argument"));
20147 return NULL;
20148}
20149#endif
20150
20151#ifdef FEAT_TIMERS
20152/*
20153 * "timer_start(time, callback [, options])" function
20154 */
20155 static void
20156f_timer_start(typval_T *argvars, typval_T *rettv)
20157{
20158 long msec = get_tv_number(&argvars[0]);
20159 timer_T *timer;
20160 int repeat = 0;
20161 char_u *callback;
20162 dict_T *dict;
20163
20164 if (argvars[2].v_type != VAR_UNKNOWN)
20165 {
20166 if (argvars[2].v_type != VAR_DICT
20167 || (dict = argvars[2].vval.v_dict) == NULL)
20168 {
20169 EMSG2(_(e_invarg2), get_tv_string(&argvars[2]));
20170 return;
20171 }
20172 if (dict_find(dict, (char_u *)"repeat", -1) != NULL)
20173 repeat = get_dict_number(dict, (char_u *)"repeat");
20174 }
20175
20176 timer = create_timer(msec, repeat);
20177 callback = get_callback(&argvars[1], &timer->tr_partial);
20178 if (callback == NULL)
20179 {
20180 stop_timer(timer);
20181 rettv->vval.v_number = -1;
20182 }
20183 else
20184 {
20185 timer->tr_callback = vim_strsave(callback);
20186 rettv->vval.v_number = timer->tr_id;
20187 }
20188}
20189
20190/*
20191 * "timer_stop(timer)" function
20192 */
20193 static void
20194f_timer_stop(typval_T *argvars, typval_T *rettv UNUSED)
20195{
20196 timer_T *timer = find_timer(get_tv_number(&argvars[0]));
20197
20198 if (timer != NULL)
20199 stop_timer(timer);
20200}
20201#endif
20202
Bram Moolenaard52d9742005-08-21 22:20:28 +000020203/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020204 * "tolower(string)" function
20205 */
20206 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020207f_tolower(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020208{
20209 char_u *p;
20210
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020211 p = vim_strsave(get_tv_string(&argvars[0]));
20212 rettv->v_type = VAR_STRING;
20213 rettv->vval.v_string = p;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020214
20215 if (p != NULL)
20216 while (*p != NUL)
20217 {
20218#ifdef FEAT_MBYTE
20219 int l;
20220
20221 if (enc_utf8)
20222 {
20223 int c, lc;
20224
20225 c = utf_ptr2char(p);
20226 lc = utf_tolower(c);
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020227 l = utf_ptr2len(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020228 /* TODO: reallocate string when byte count changes. */
20229 if (utf_char2len(lc) == l)
20230 utf_char2bytes(lc, p);
20231 p += l;
20232 }
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020233 else if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020234 p += l; /* skip multi-byte character */
20235 else
20236#endif
20237 {
20238 *p = TOLOWER_LOC(*p); /* note that tolower() can be a macro */
20239 ++p;
20240 }
20241 }
20242}
20243
20244/*
20245 * "toupper(string)" function
20246 */
20247 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020248f_toupper(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020249{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020250 rettv->v_type = VAR_STRING;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020251 rettv->vval.v_string = strup_save(get_tv_string(&argvars[0]));
Bram Moolenaar071d4272004-06-13 20:20:40 +000020252}
20253
20254/*
Bram Moolenaar8299df92004-07-10 09:47:34 +000020255 * "tr(string, fromstr, tostr)" function
20256 */
20257 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020258f_tr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020259{
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020260 char_u *in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020261 char_u *fromstr;
20262 char_u *tostr;
20263 char_u *p;
20264#ifdef FEAT_MBYTE
Bram Moolenaar342337a2005-07-21 21:11:17 +000020265 int inlen;
20266 int fromlen;
20267 int tolen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020268 int idx;
20269 char_u *cpstr;
20270 int cplen;
20271 int first = TRUE;
20272#endif
20273 char_u buf[NUMBUFLEN];
20274 char_u buf2[NUMBUFLEN];
20275 garray_T ga;
20276
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020277 in_str = get_tv_string(&argvars[0]);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020278 fromstr = get_tv_string_buf_chk(&argvars[1], buf);
20279 tostr = get_tv_string_buf_chk(&argvars[2], buf2);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020280
20281 /* Default return value: empty string. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020282 rettv->v_type = VAR_STRING;
20283 rettv->vval.v_string = NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020284 if (fromstr == NULL || tostr == NULL)
20285 return; /* type error; errmsg already given */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020286 ga_init2(&ga, (int)sizeof(char), 80);
20287
20288#ifdef FEAT_MBYTE
20289 if (!has_mbyte)
20290#endif
20291 /* not multi-byte: fromstr and tostr must be the same length */
20292 if (STRLEN(fromstr) != STRLEN(tostr))
20293 {
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020294#ifdef FEAT_MBYTE
Bram Moolenaar8299df92004-07-10 09:47:34 +000020295error:
Bram Moolenaar5eb86f92004-07-26 12:53:41 +000020296#endif
Bram Moolenaar8299df92004-07-10 09:47:34 +000020297 EMSG2(_(e_invarg2), fromstr);
20298 ga_clear(&ga);
20299 return;
20300 }
20301
20302 /* fromstr and tostr have to contain the same number of chars */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020303 while (*in_str != NUL)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020304 {
20305#ifdef FEAT_MBYTE
20306 if (has_mbyte)
20307 {
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020308 inlen = (*mb_ptr2len)(in_str);
20309 cpstr = in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020310 cplen = inlen;
20311 idx = 0;
20312 for (p = fromstr; *p != NUL; p += fromlen)
20313 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020314 fromlen = (*mb_ptr2len)(p);
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020315 if (fromlen == inlen && STRNCMP(in_str, p, inlen) == 0)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020316 {
20317 for (p = tostr; *p != NUL; p += tolen)
20318 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020319 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020320 if (idx-- == 0)
20321 {
20322 cplen = tolen;
20323 cpstr = p;
20324 break;
20325 }
20326 }
20327 if (*p == NUL) /* tostr is shorter than fromstr */
20328 goto error;
20329 break;
20330 }
20331 ++idx;
20332 }
20333
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020334 if (first && cpstr == in_str)
Bram Moolenaar8299df92004-07-10 09:47:34 +000020335 {
20336 /* Check that fromstr and tostr have the same number of
20337 * (multi-byte) characters. Done only once when a character
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020338 * of in_str doesn't appear in fromstr. */
Bram Moolenaar8299df92004-07-10 09:47:34 +000020339 first = FALSE;
20340 for (p = tostr; *p != NUL; p += tolen)
20341 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000020342 tolen = (*mb_ptr2len)(p);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020343 --idx;
20344 }
20345 if (idx != 0)
20346 goto error;
20347 }
20348
Bram Moolenaarcde88542015-08-11 19:14:00 +020020349 (void)ga_grow(&ga, cplen);
Bram Moolenaar2df6dcc2004-07-12 15:53:54 +000020350 mch_memmove((char *)ga.ga_data + ga.ga_len, cpstr, (size_t)cplen);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020351 ga.ga_len += cplen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020352
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020353 in_str += inlen;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020354 }
20355 else
20356#endif
20357 {
20358 /* When not using multi-byte chars we can do it faster. */
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020359 p = vim_strchr(fromstr, *in_str);
Bram Moolenaar8299df92004-07-10 09:47:34 +000020360 if (p != NULL)
20361 ga_append(&ga, tostr[p - fromstr]);
20362 else
Bram Moolenaar70b2a562012-01-10 22:26:17 +010020363 ga_append(&ga, *in_str);
20364 ++in_str;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020365 }
20366 }
20367
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020368 /* add a terminating NUL */
Bram Moolenaarcde88542015-08-11 19:14:00 +020020369 (void)ga_grow(&ga, 1);
Bram Moolenaar61b974b2006-12-05 09:32:29 +000020370 ga_append(&ga, NUL);
20371
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020372 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar8299df92004-07-10 09:47:34 +000020373}
20374
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020375#ifdef FEAT_FLOAT
20376/*
20377 * "trunc({float})" function
20378 */
20379 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020380f_trunc(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020381{
Bram Moolenaara1e24b92016-02-18 20:18:09 +010020382 float_T f = 0.0;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020383
20384 rettv->v_type = VAR_FLOAT;
20385 if (get_float_arg(argvars, &f) == OK)
20386 /* trunc() is not in C90, use floor() or ceil() instead. */
20387 rettv->vval.v_float = f > 0 ? floor(f) : ceil(f);
20388 else
20389 rettv->vval.v_float = 0.0;
20390}
20391#endif
20392
Bram Moolenaar8299df92004-07-10 09:47:34 +000020393/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020394 * "type(expr)" function
20395 */
20396 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020397f_type(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020398{
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010020399 int n = -1;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020400
20401 switch (argvars[0].v_type)
20402 {
20403 case VAR_NUMBER: n = 0; break;
20404 case VAR_STRING: n = 1; break;
20405 case VAR_FUNC: n = 2; break;
20406 case VAR_LIST: n = 3; break;
Bram Moolenaar758711c2005-02-02 23:11:38 +000020407 case VAR_DICT: n = 4; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000020408 case VAR_FLOAT: n = 5; break;
Bram Moolenaarf95534c2016-01-23 21:59:52 +010020409 case VAR_SPECIAL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010020410 if (argvars[0].vval.v_number == VVAL_FALSE
20411 || argvars[0].vval.v_number == VVAL_TRUE)
20412 n = 6;
20413 else
20414 n = 7;
20415 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010020416 case VAR_JOB: n = 8; break;
20417 case VAR_CHANNEL: n = 9; break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010020418 case VAR_PARTIAL: n = 10; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010020419 case VAR_UNKNOWN:
20420 EMSG2(_(e_intern2), "f_type(UNKNOWN)");
20421 n = -1;
20422 break;
Bram Moolenaar6cc16192005-01-08 21:49:45 +000020423 }
20424 rettv->vval.v_number = n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020425}
20426
20427/*
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020428 * "undofile(name)" function
20429 */
20430 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020431f_undofile(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020432{
20433 rettv->v_type = VAR_STRING;
20434#ifdef FEAT_PERSISTENT_UNDO
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020435 {
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020436 char_u *fname = get_tv_string(&argvars[0]);
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020437
Bram Moolenaarb41d9682012-04-30 17:35:48 +020020438 if (*fname == NUL)
20439 {
20440 /* If there is no file name there will be no undo file. */
20441 rettv->vval.v_string = NULL;
20442 }
20443 else
20444 {
20445 char_u *ffname = FullName_save(fname, FALSE);
20446
20447 if (ffname != NULL)
20448 rettv->vval.v_string = u_get_undo_file_name(ffname, FALSE);
20449 vim_free(ffname);
20450 }
Bram Moolenaar945e2db2010-06-05 17:43:32 +020020451 }
Bram Moolenaara17d4c12010-05-30 18:30:36 +020020452#else
20453 rettv->vval.v_string = NULL;
20454#endif
20455}
20456
20457/*
Bram Moolenaara800b422010-06-27 01:15:55 +020020458 * "undotree()" function
20459 */
20460 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020461f_undotree(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaara800b422010-06-27 01:15:55 +020020462{
20463 if (rettv_dict_alloc(rettv) == OK)
20464 {
20465 dict_T *dict = rettv->vval.v_dict;
20466 list_T *list;
20467
Bram Moolenaar730cde92010-06-27 05:18:54 +020020468 dict_add_nr_str(dict, "synced", (long)curbuf->b_u_synced, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020469 dict_add_nr_str(dict, "seq_last", curbuf->b_u_seq_last, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020470 dict_add_nr_str(dict, "save_last",
20471 (long)curbuf->b_u_save_nr_last, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020472 dict_add_nr_str(dict, "seq_cur", curbuf->b_u_seq_cur, NULL);
20473 dict_add_nr_str(dict, "time_cur", (long)curbuf->b_u_time_cur, NULL);
Bram Moolenaar730cde92010-06-27 05:18:54 +020020474 dict_add_nr_str(dict, "save_cur", (long)curbuf->b_u_save_nr_cur, NULL);
Bram Moolenaara800b422010-06-27 01:15:55 +020020475
20476 list = list_alloc();
20477 if (list != NULL)
20478 {
20479 u_eval_tree(curbuf->b_u_oldhead, list);
20480 dict_add_list(dict, "entries", list);
20481 }
20482 }
20483}
20484
20485/*
Bram Moolenaar8c711452005-01-14 21:53:12 +000020486 * "values(dict)" function
20487 */
20488 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020489f_values(typval_T *argvars, typval_T *rettv)
Bram Moolenaar8c711452005-01-14 21:53:12 +000020490{
20491 dict_list(argvars, rettv, 1);
20492}
20493
20494/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020495 * "virtcol(string)" function
20496 */
20497 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020498f_virtcol(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020499{
20500 colnr_T vcol = 0;
20501 pos_T *fp;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020502 int fnum = curbuf->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020503
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020504 fp = var2fpos(&argvars[0], FALSE, &fnum);
20505 if (fp != NULL && fp->lnum <= curbuf->b_ml.ml_line_count
20506 && fnum == curbuf->b_fnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020507 {
20508 getvvcol(curwin, fp, NULL, NULL, &vcol);
20509 ++vcol;
20510 }
20511
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020512 rettv->vval.v_number = vcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020513}
20514
20515/*
20516 * "visualmode()" function
20517 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020518 static void
Bram Moolenaarf1d25012016-03-03 12:22:53 +010020519f_visualmode(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020520{
Bram Moolenaar071d4272004-06-13 20:20:40 +000020521 char_u str[2];
20522
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020523 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020524 str[0] = curbuf->b_visual_mode_eval;
20525 str[1] = NUL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020526 rettv->vval.v_string = vim_strsave(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020527
20528 /* A non-zero number or non-empty string argument: reset mode. */
Bram Moolenaar05bb9532008-07-04 09:44:11 +000020529 if (non_zero_arg(&argvars[0]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000020530 curbuf->b_visual_mode_eval = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020531}
20532
20533/*
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020534 * "wildmenumode()" function
20535 */
20536 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020537f_wildmenumode(typval_T *argvars UNUSED, typval_T *rettv UNUSED)
Bram Moolenaar8738fc12013-02-20 17:59:11 +010020538{
20539#ifdef FEAT_WILDMENU
20540 if (wild_menu_showing)
20541 rettv->vval.v_number = 1;
20542#endif
20543}
20544
20545/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020546 * "winbufnr(nr)" function
20547 */
20548 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020549f_winbufnr(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020550{
20551 win_T *wp;
20552
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020553 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020554 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020555 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020556 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020557 rettv->vval.v_number = wp->w_buffer->b_fnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020558}
20559
20560/*
20561 * "wincol()" function
20562 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020563 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020564f_wincol(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020565{
20566 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020567 rettv->vval.v_number = curwin->w_wcol + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020568}
20569
20570/*
20571 * "winheight(nr)" function
20572 */
20573 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020574f_winheight(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020575{
20576 win_T *wp;
20577
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020578 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020579 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020580 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020581 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020582 rettv->vval.v_number = wp->w_height;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020583}
20584
20585/*
20586 * "winline()" function
20587 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020588 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020589f_winline(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020590{
20591 validate_cursor();
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020592 rettv->vval.v_number = curwin->w_wrow + 1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020593}
20594
20595/*
20596 * "winnr()" function
20597 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020598 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020599f_winnr(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020600{
20601 int nr = 1;
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020602
Bram Moolenaar071d4272004-06-13 20:20:40 +000020603#ifdef FEAT_WINDOWS
Bram Moolenaarfaa959a2006-02-20 21:37:40 +000020604 nr = get_winnr(curtab, &argvars[0]);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020605#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020606 rettv->vval.v_number = nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020607}
20608
20609/*
20610 * "winrestcmd()" function
20611 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020612 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020613f_winrestcmd(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020614{
20615#ifdef FEAT_WINDOWS
20616 win_T *wp;
20617 int winnr = 1;
20618 garray_T ga;
20619 char_u buf[50];
20620
20621 ga_init2(&ga, (int)sizeof(char), 70);
20622 for (wp = firstwin; wp != NULL; wp = wp->w_next)
20623 {
20624 sprintf((char *)buf, "%dresize %d|", winnr, wp->w_height);
20625 ga_concat(&ga, buf);
20626# ifdef FEAT_VERTSPLIT
20627 sprintf((char *)buf, "vert %dresize %d|", winnr, wp->w_width);
20628 ga_concat(&ga, buf);
20629# endif
20630 ++winnr;
20631 }
Bram Moolenaar269ec652004-07-29 08:43:53 +000020632 ga_append(&ga, NUL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020633
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020634 rettv->vval.v_string = ga.ga_data;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020635#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020636 rettv->vval.v_string = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020637#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020638 rettv->v_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020639}
20640
20641/*
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020642 * "winrestview()" function
20643 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020644 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020645f_winrestview(typval_T *argvars, typval_T *rettv UNUSED)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020646{
20647 dict_T *dict;
20648
20649 if (argvars[0].v_type != VAR_DICT
20650 || (dict = argvars[0].vval.v_dict) == NULL)
20651 EMSG(_(e_invarg));
20652 else
20653 {
Bram Moolenaar82c25852014-05-28 16:47:16 +020020654 if (dict_find(dict, (char_u *)"lnum", -1) != NULL)
20655 curwin->w_cursor.lnum = get_dict_number(dict, (char_u *)"lnum");
20656 if (dict_find(dict, (char_u *)"col", -1) != NULL)
20657 curwin->w_cursor.col = get_dict_number(dict, (char_u *)"col");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020658#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar82c25852014-05-28 16:47:16 +020020659 if (dict_find(dict, (char_u *)"coladd", -1) != NULL)
20660 curwin->w_cursor.coladd = get_dict_number(dict, (char_u *)"coladd");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020661#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020662 if (dict_find(dict, (char_u *)"curswant", -1) != NULL)
20663 {
20664 curwin->w_curswant = get_dict_number(dict, (char_u *)"curswant");
20665 curwin->w_set_curswant = FALSE;
20666 }
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020667
Bram Moolenaar82c25852014-05-28 16:47:16 +020020668 if (dict_find(dict, (char_u *)"topline", -1) != NULL)
20669 set_topline(curwin, get_dict_number(dict, (char_u *)"topline"));
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020670#ifdef FEAT_DIFF
Bram Moolenaar82c25852014-05-28 16:47:16 +020020671 if (dict_find(dict, (char_u *)"topfill", -1) != NULL)
20672 curwin->w_topfill = get_dict_number(dict, (char_u *)"topfill");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020673#endif
Bram Moolenaar82c25852014-05-28 16:47:16 +020020674 if (dict_find(dict, (char_u *)"leftcol", -1) != NULL)
20675 curwin->w_leftcol = get_dict_number(dict, (char_u *)"leftcol");
20676 if (dict_find(dict, (char_u *)"skipcol", -1) != NULL)
20677 curwin->w_skipcol = get_dict_number(dict, (char_u *)"skipcol");
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020678
20679 check_cursor();
Bram Moolenaar6763c142012-07-19 18:05:44 +020020680 win_new_height(curwin, curwin->w_height);
20681# ifdef FEAT_VERTSPLIT
20682 win_new_width(curwin, W_WIDTH(curwin));
20683# endif
Bram Moolenaarab984db2012-06-06 16:29:10 +020020684 changed_window_setting();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020685
Bram Moolenaarb851a962014-10-31 15:45:52 +010020686 if (curwin->w_topline <= 0)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020687 curwin->w_topline = 1;
20688 if (curwin->w_topline > curbuf->b_ml.ml_line_count)
20689 curwin->w_topline = curbuf->b_ml.ml_line_count;
20690#ifdef FEAT_DIFF
20691 check_topfill(curwin, TRUE);
20692#endif
20693 }
20694}
20695
20696/*
20697 * "winsaveview()" function
20698 */
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020699 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020700f_winsaveview(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020701{
20702 dict_T *dict;
20703
Bram Moolenaara800b422010-06-27 01:15:55 +020020704 if (rettv_dict_alloc(rettv) == FAIL)
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020705 return;
Bram Moolenaara800b422010-06-27 01:15:55 +020020706 dict = rettv->vval.v_dict;
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020707
20708 dict_add_nr_str(dict, "lnum", (long)curwin->w_cursor.lnum, NULL);
20709 dict_add_nr_str(dict, "col", (long)curwin->w_cursor.col, NULL);
20710#ifdef FEAT_VIRTUALEDIT
20711 dict_add_nr_str(dict, "coladd", (long)curwin->w_cursor.coladd, NULL);
20712#endif
Bram Moolenaar9af1ba92006-08-29 19:55:53 +000020713 update_curswant();
Bram Moolenaar768b8c42006-03-04 21:58:33 +000020714 dict_add_nr_str(dict, "curswant", (long)curwin->w_curswant, NULL);
20715
20716 dict_add_nr_str(dict, "topline", (long)curwin->w_topline, NULL);
20717#ifdef FEAT_DIFF
20718 dict_add_nr_str(dict, "topfill", (long)curwin->w_topfill, NULL);
20719#endif
20720 dict_add_nr_str(dict, "leftcol", (long)curwin->w_leftcol, NULL);
20721 dict_add_nr_str(dict, "skipcol", (long)curwin->w_skipcol, NULL);
20722}
20723
20724/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020725 * "winwidth(nr)" function
20726 */
20727 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020728f_winwidth(typval_T *argvars, typval_T *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020729{
20730 win_T *wp;
20731
Bram Moolenaar99ebf042006-04-15 20:28:54 +000020732 wp = find_win_by_nr(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020733 if (wp == NULL)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020734 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020735 else
20736#ifdef FEAT_VERTSPLIT
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020737 rettv->vval.v_number = wp->w_width;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020738#else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000020739 rettv->vval.v_number = Columns;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020740#endif
20741}
20742
Bram Moolenaar071d4272004-06-13 20:20:40 +000020743/*
Bram Moolenaared767a22016-01-03 22:49:16 +010020744 * "wordcount()" function
20745 */
20746 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020747f_wordcount(typval_T *argvars UNUSED, typval_T *rettv)
Bram Moolenaared767a22016-01-03 22:49:16 +010020748{
20749 if (rettv_dict_alloc(rettv) == FAIL)
20750 return;
20751 cursor_pos_info(rettv->vval.v_dict);
20752}
20753
20754/*
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020755 * Write list of strings to file
20756 */
20757 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020758write_list(FILE *fd, list_T *list, int binary)
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020759{
20760 listitem_T *li;
20761 int c;
20762 int ret = OK;
20763 char_u *s;
20764
20765 for (li = list->lv_first; li != NULL; li = li->li_next)
20766 {
20767 for (s = get_tv_string(&li->li_tv); *s != NUL; ++s)
20768 {
20769 if (*s == '\n')
20770 c = putc(NUL, fd);
20771 else
20772 c = putc(*s, fd);
20773 if (c == EOF)
20774 {
20775 ret = FAIL;
20776 break;
20777 }
20778 }
20779 if (!binary || li->li_next != NULL)
20780 if (putc('\n', fd) == EOF)
20781 {
20782 ret = FAIL;
20783 break;
20784 }
20785 if (ret == FAIL)
20786 {
20787 EMSG(_(e_write));
20788 break;
20789 }
20790 }
20791 return ret;
20792}
20793
20794/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020795 * "writefile()" function
20796 */
20797 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020798f_writefile(typval_T *argvars, typval_T *rettv)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020799{
20800 int binary = FALSE;
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020801 int append = FALSE;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020802 char_u *fname;
20803 FILE *fd;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020804 int ret = 0;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020805
Bram Moolenaard9fe7c42007-04-29 11:53:56 +000020806 if (check_restricted() || check_secure())
20807 return;
20808
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020809 if (argvars[0].v_type != VAR_LIST)
20810 {
20811 EMSG2(_(e_listarg), "writefile()");
20812 return;
20813 }
20814 if (argvars[0].vval.v_list == NULL)
20815 return;
20816
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020817 if (argvars[2].v_type != VAR_UNKNOWN)
20818 {
20819 if (vim_strchr(get_tv_string(&argvars[2]), 'b') != NULL)
20820 binary = TRUE;
20821 if (vim_strchr(get_tv_string(&argvars[2]), 'a') != NULL)
20822 append = TRUE;
20823 }
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020824
20825 /* Always open the file in binary mode, library functions have a mind of
20826 * their own about CR-LF conversion. */
20827 fname = get_tv_string(&argvars[1]);
Bram Moolenaar6b2e9382014-11-05 18:06:01 +010020828 if (*fname == NUL || (fd = mch_fopen((char *)fname,
20829 append ? APPENDBIN : WRITEBIN)) == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020830 {
20831 EMSG2(_(e_notcreate), *fname == NUL ? (char_u *)_("<empty>") : fname);
20832 ret = -1;
20833 }
20834 else
20835 {
Bram Moolenaar57ebe6e2014-04-05 18:55:46 +020020836 if (write_list(fd, argvars[0].vval.v_list, binary) == FAIL)
20837 ret = -1;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000020838 fclose(fd);
20839 }
20840
20841 rettv->vval.v_number = ret;
20842}
20843
20844/*
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020845 * "xor(expr, expr)" function
20846 */
20847 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010020848f_xor(typval_T *argvars, typval_T *rettv)
Bram Moolenaard6e256c2011-12-14 15:32:50 +010020849{
20850 rettv->vval.v_number = get_tv_number_chk(&argvars[0], NULL)
20851 ^ get_tv_number_chk(&argvars[1], NULL);
20852}
20853
20854
20855/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000020856 * Translate a String variable into a position.
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020857 * Returns NULL when there is an error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000020858 */
20859 static pos_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010020860var2fpos(
20861 typval_T *varp,
20862 int dollar_lnum, /* TRUE when $ is last line */
20863 int *fnum) /* set to fnum for '0, 'A, etc. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020864{
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020865 char_u *name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020866 static pos_T pos;
Bram Moolenaar261bfea2006-03-01 22:12:31 +000020867 pos_T *pp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000020868
Bram Moolenaara5525202006-03-02 22:52:09 +000020869 /* Argument can be [lnum, col, coladd]. */
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020870 if (varp->v_type == VAR_LIST)
20871 {
20872 list_T *l;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020873 int len;
Bram Moolenaara5525202006-03-02 22:52:09 +000020874 int error = FALSE;
Bram Moolenaar477933c2007-07-17 14:32:23 +000020875 listitem_T *li;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020876
20877 l = varp->vval.v_list;
20878 if (l == NULL)
20879 return NULL;
20880
20881 /* Get the line number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020882 pos.lnum = list_find_nr(l, 0L, &error);
20883 if (error || pos.lnum <= 0 || pos.lnum > curbuf->b_ml.ml_line_count)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020884 return NULL; /* invalid line number */
20885
20886 /* Get the column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020887 pos.col = list_find_nr(l, 1L, &error);
20888 if (error)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020889 return NULL;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020890 len = (long)STRLEN(ml_get(pos.lnum));
Bram Moolenaar477933c2007-07-17 14:32:23 +000020891
20892 /* We accept "$" for the column number: last column. */
20893 li = list_find(l, 1L);
20894 if (li != NULL && li->li_tv.v_type == VAR_STRING
20895 && li->li_tv.vval.v_string != NULL
20896 && STRCMP(li->li_tv.vval.v_string, "$") == 0)
20897 pos.col = len + 1;
20898
Bram Moolenaara5525202006-03-02 22:52:09 +000020899 /* Accept a position up to the NUL after the line. */
Bram Moolenaar4c3f5362006-04-11 21:38:50 +000020900 if (pos.col == 0 || (int)pos.col > len + 1)
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020901 return NULL; /* invalid column number */
Bram Moolenaara5525202006-03-02 22:52:09 +000020902 --pos.col;
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020903
Bram Moolenaara5525202006-03-02 22:52:09 +000020904#ifdef FEAT_VIRTUALEDIT
20905 /* Get the virtual offset. Defaults to zero. */
20906 pos.coladd = list_find_nr(l, 2L, &error);
20907 if (error)
20908 pos.coladd = 0;
20909#endif
20910
Bram Moolenaar32466aa2006-02-24 23:53:04 +000020911 return &pos;
20912 }
20913
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000020914 name = get_tv_string_chk(varp);
20915 if (name == NULL)
20916 return NULL;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020917 if (name[0] == '.') /* cursor */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020918 return &curwin->w_cursor;
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020919 if (name[0] == 'v' && name[1] == NUL) /* Visual start */
20920 {
20921 if (VIsual_active)
20922 return &VIsual;
20923 return &curwin->w_cursor;
20924 }
Bram Moolenaar9ecd0232008-06-20 15:31:51 +000020925 if (name[0] == '\'') /* mark */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020926 {
Bram Moolenaar9d182dd2013-01-23 15:53:15 +010020927 pp = getmark_buf_fnum(curbuf, name[1], FALSE, fnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000020928 if (pp == NULL || pp == (pos_T *)-1 || pp->lnum <= 0)
20929 return NULL;
20930 return pp;
20931 }
Bram Moolenaara5525202006-03-02 22:52:09 +000020932
20933#ifdef FEAT_VIRTUALEDIT
20934 pos.coladd = 0;
20935#endif
20936
Bram Moolenaar477933c2007-07-17 14:32:23 +000020937 if (name[0] == 'w' && dollar_lnum)
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020938 {
20939 pos.col = 0;
20940 if (name[1] == '0') /* "w0": first visible line */
20941 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020942 update_topline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020943 pos.lnum = curwin->w_topline;
20944 return &pos;
20945 }
20946 else if (name[1] == '$') /* "w$": last visible line */
20947 {
Bram Moolenaarf740b292006-02-16 22:11:02 +000020948 validate_botline();
Bram Moolenaarf52c7252006-02-10 23:23:57 +000020949 pos.lnum = curwin->w_botline - 1;
20950 return &pos;
20951 }
20952 }
20953 else if (name[0] == '$') /* last column or line */
Bram Moolenaar071d4272004-06-13 20:20:40 +000020954 {
Bram Moolenaar477933c2007-07-17 14:32:23 +000020955 if (dollar_lnum)
Bram Moolenaar071d4272004-06-13 20:20:40 +000020956 {
20957 pos.lnum = curbuf->b_ml.ml_line_count;
20958 pos.col = 0;
20959 }
20960 else
20961 {
20962 pos.lnum = curwin->w_cursor.lnum;
20963 pos.col = (colnr_T)STRLEN(ml_get_curline());
20964 }
20965 return &pos;
20966 }
20967 return NULL;
20968}
20969
20970/*
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020971 * Convert list in "arg" into a position and optional file number.
20972 * When "fnump" is NULL there is no file number, only 3 items.
20973 * Note that the column is passed on as-is, the caller may want to decrement
20974 * it to use 1 for the first column.
20975 * Return FAIL when conversion is not possible, doesn't check the position for
20976 * validity.
20977 */
20978 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010020979list2fpos(
20980 typval_T *arg,
20981 pos_T *posp,
20982 int *fnump,
20983 colnr_T *curswantp)
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020984{
20985 list_T *l = arg->vval.v_list;
20986 long i = 0;
20987 long n;
20988
Bram Moolenaar493c1782014-05-28 14:34:46 +020020989 /* List must be: [fnum, lnum, col, coladd, curswant], where "fnum" is only
20990 * there when "fnump" isn't NULL; "coladd" and "curswant" are optional. */
Bram Moolenaarbde35262006-07-23 20:12:24 +000020991 if (arg->v_type != VAR_LIST
20992 || l == NULL
20993 || l->lv_len < (fnump == NULL ? 2 : 3)
Bram Moolenaar493c1782014-05-28 14:34:46 +020020994 || l->lv_len > (fnump == NULL ? 4 : 5))
Bram Moolenaar0e34f622006-03-03 23:00:03 +000020995 return FAIL;
20996
20997 if (fnump != NULL)
20998 {
20999 n = list_find_nr(l, i++, NULL); /* fnum */
21000 if (n < 0)
21001 return FAIL;
21002 if (n == 0)
21003 n = curbuf->b_fnum; /* current buffer */
21004 *fnump = n;
21005 }
21006
21007 n = list_find_nr(l, i++, NULL); /* lnum */
21008 if (n < 0)
21009 return FAIL;
21010 posp->lnum = n;
21011
21012 n = list_find_nr(l, i++, NULL); /* col */
21013 if (n < 0)
21014 return FAIL;
21015 posp->col = n;
21016
21017#ifdef FEAT_VIRTUALEDIT
Bram Moolenaar493c1782014-05-28 14:34:46 +020021018 n = list_find_nr(l, i, NULL); /* off */
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021019 if (n < 0)
Bram Moolenaarbde35262006-07-23 20:12:24 +000021020 posp->coladd = 0;
21021 else
21022 posp->coladd = n;
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021023#endif
21024
Bram Moolenaar493c1782014-05-28 14:34:46 +020021025 if (curswantp != NULL)
21026 *curswantp = list_find_nr(l, i + 1, NULL); /* curswant */
21027
Bram Moolenaar0e34f622006-03-03 23:00:03 +000021028 return OK;
21029}
21030
21031/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021032 * Get the length of an environment variable name.
21033 * Advance "arg" to the first character after the name.
21034 * Return 0 for error.
21035 */
21036 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021037get_env_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021038{
21039 char_u *p;
21040 int len;
21041
21042 for (p = *arg; vim_isIDc(*p); ++p)
21043 ;
21044 if (p == *arg) /* no name found */
21045 return 0;
21046
21047 len = (int)(p - *arg);
21048 *arg = p;
21049 return len;
21050}
21051
21052/*
21053 * Get the length of the name of a function or internal variable.
21054 * "arg" is advanced to the first non-white character after the name.
21055 * Return 0 if something is wrong.
21056 */
21057 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021058get_id_len(char_u **arg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021059{
21060 char_u *p;
21061 int len;
21062
21063 /* Find the end of the name. */
21064 for (p = *arg; eval_isnamec(*p); ++p)
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021065 {
21066 if (*p == ':')
21067 {
21068 /* "s:" is start of "s:var", but "n:" is not and can be used in
21069 * slice "[n:]". Also "xx:" is not a namespace. */
21070 len = (int)(p - *arg);
21071 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, **arg) == NULL)
21072 || len > 1)
21073 break;
21074 }
21075 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021076 if (p == *arg) /* no name found */
21077 return 0;
21078
21079 len = (int)(p - *arg);
21080 *arg = skipwhite(p);
21081
21082 return len;
21083}
21084
21085/*
Bram Moolenaara7043832005-01-21 11:56:39 +000021086 * Get the length of the name of a variable or function.
21087 * Only the name is recognized, does not handle ".key" or "[idx]".
Bram Moolenaar071d4272004-06-13 20:20:40 +000021088 * "arg" is advanced to the first non-white character after the name.
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021089 * Return -1 if curly braces expansion failed.
21090 * Return 0 if something else is wrong.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021091 * If the name contains 'magic' {}'s, expand them and return the
21092 * expanded name in an allocated string via 'alias' - caller must free.
21093 */
21094 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021095get_name_len(
21096 char_u **arg,
21097 char_u **alias,
21098 int evaluate,
21099 int verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021100{
21101 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021102 char_u *p;
21103 char_u *expr_start;
21104 char_u *expr_end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021105
21106 *alias = NULL; /* default to no alias */
21107
21108 if ((*arg)[0] == K_SPECIAL && (*arg)[1] == KS_EXTRA
21109 && (*arg)[2] == (int)KE_SNR)
21110 {
21111 /* hard coded <SNR>, already translated */
21112 *arg += 3;
21113 return get_id_len(arg) + 3;
21114 }
21115 len = eval_fname_script(*arg);
21116 if (len > 0)
21117 {
21118 /* literal "<SID>", "s:" or "<SNR>" */
21119 *arg += len;
21120 }
21121
Bram Moolenaar071d4272004-06-13 20:20:40 +000021122 /*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021123 * Find the end of the name; check for {} construction.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021124 */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021125 p = find_name_end(*arg, &expr_start, &expr_end,
21126 len > 0 ? 0 : FNE_CHECK_START);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021127 if (expr_start != NULL)
21128 {
21129 char_u *temp_string;
21130
21131 if (!evaluate)
21132 {
21133 len += (int)(p - *arg);
21134 *arg = skipwhite(p);
21135 return len;
21136 }
21137
21138 /*
21139 * Include any <SID> etc in the expanded string:
21140 * Thus the -len here.
21141 */
21142 temp_string = make_expanded_name(*arg - len, expr_start, expr_end, p);
21143 if (temp_string == NULL)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021144 return -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021145 *alias = temp_string;
21146 *arg = skipwhite(p);
21147 return (int)STRLEN(temp_string);
21148 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021149
21150 len += get_id_len(arg);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021151 if (len == 0 && verbose)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021152 EMSG2(_(e_invexpr2), *arg);
21153
21154 return len;
21155}
21156
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021157/*
21158 * Find the end of a variable or function name, taking care of magic braces.
21159 * If "expr_start" is not NULL then "expr_start" and "expr_end" are set to the
21160 * start and end of the first magic braces item.
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021161 * "flags" can have FNE_INCL_BR and FNE_CHECK_START.
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021162 * Return a pointer to just after the name. Equal to "arg" if there is no
21163 * valid name.
21164 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021165 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021166find_name_end(
21167 char_u *arg,
21168 char_u **expr_start,
21169 char_u **expr_end,
21170 int flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021171{
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021172 int mb_nest = 0;
21173 int br_nest = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021174 char_u *p;
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021175 int len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021176
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021177 if (expr_start != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021178 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021179 *expr_start = NULL;
21180 *expr_end = NULL;
21181 }
21182
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021183 /* Quick check for valid starting character. */
21184 if ((flags & FNE_CHECK_START) && !eval_isnamec1(*arg) && *arg != '{')
21185 return arg;
21186
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021187 for (p = arg; *p != NUL
21188 && (eval_isnamec(*p)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021189 || *p == '{'
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021190 || ((flags & FNE_INCL_BR) && (*p == '[' || *p == '.'))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021191 || mb_nest != 0
Bram Moolenaar8af24422005-08-08 22:06:28 +000021192 || br_nest != 0); mb_ptr_adv(p))
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021193 {
Bram Moolenaar8af24422005-08-08 22:06:28 +000021194 if (*p == '\'')
21195 {
21196 /* skip over 'string' to avoid counting [ and ] inside it. */
21197 for (p = p + 1; *p != NUL && *p != '\''; mb_ptr_adv(p))
21198 ;
21199 if (*p == NUL)
21200 break;
21201 }
21202 else if (*p == '"')
21203 {
21204 /* skip over "str\"ing" to avoid counting [ and ] inside it. */
21205 for (p = p + 1; *p != NUL && *p != '"'; mb_ptr_adv(p))
21206 if (*p == '\\' && p[1] != NUL)
21207 ++p;
21208 if (*p == NUL)
21209 break;
21210 }
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021211 else if (br_nest == 0 && mb_nest == 0 && *p == ':')
21212 {
21213 /* "s:" is start of "s:var", but "n:" is not and can be used in
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021214 * slice "[n:]". Also "xx:" is not a namespace. But {ns}: is. */
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021215 len = (int)(p - arg);
21216 if ((len == 1 && vim_strchr(NAMESPACE_CHAR, *arg) == NULL)
Bram Moolenaar4119cf82016-01-17 14:59:01 +010021217 || (len > 1 && p[-1] != '}'))
Bram Moolenaar9bbf63d2016-01-16 16:49:28 +010021218 break;
21219 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021220
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021221 if (mb_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021222 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021223 if (*p == '[')
21224 ++br_nest;
21225 else if (*p == ']')
21226 --br_nest;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021227 }
Bram Moolenaar8af24422005-08-08 22:06:28 +000021228
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021229 if (br_nest == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021230 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021231 if (*p == '{')
21232 {
21233 mb_nest++;
21234 if (expr_start != NULL && *expr_start == NULL)
21235 *expr_start = p;
21236 }
21237 else if (*p == '}')
21238 {
21239 mb_nest--;
21240 if (expr_start != NULL && mb_nest == 0 && *expr_end == NULL)
21241 *expr_end = p;
21242 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021243 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021244 }
21245
21246 return p;
21247}
21248
21249/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021250 * Expands out the 'magic' {}'s in a variable/function name.
21251 * Note that this can call itself recursively, to deal with
21252 * constructs like foo{bar}{baz}{bam}
21253 * The four pointer arguments point to "foo{expre}ss{ion}bar"
21254 * "in_start" ^
21255 * "expr_start" ^
21256 * "expr_end" ^
21257 * "in_end" ^
21258 *
21259 * Returns a new allocated string, which the caller must free.
21260 * Returns NULL for failure.
21261 */
21262 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021263make_expanded_name(
21264 char_u *in_start,
21265 char_u *expr_start,
21266 char_u *expr_end,
21267 char_u *in_end)
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021268{
21269 char_u c1;
21270 char_u *retval = NULL;
21271 char_u *temp_result;
21272 char_u *nextcmd = NULL;
21273
21274 if (expr_end == NULL || in_end == NULL)
21275 return NULL;
21276 *expr_start = NUL;
21277 *expr_end = NUL;
21278 c1 = *in_end;
21279 *in_end = NUL;
21280
Bram Moolenaar362e1a32006-03-06 23:29:24 +000021281 temp_result = eval_to_string(expr_start + 1, &nextcmd, FALSE);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021282 if (temp_result != NULL && nextcmd == NULL)
21283 {
21284 retval = alloc((unsigned)(STRLEN(temp_result) + (expr_start - in_start)
21285 + (in_end - expr_end) + 1));
21286 if (retval != NULL)
21287 {
21288 STRCPY(retval, in_start);
21289 STRCAT(retval, temp_result);
21290 STRCAT(retval, expr_end + 1);
21291 }
21292 }
21293 vim_free(temp_result);
21294
21295 *in_end = c1; /* put char back for error messages */
21296 *expr_start = '{';
21297 *expr_end = '}';
21298
21299 if (retval != NULL)
21300 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021301 temp_result = find_name_end(retval, &expr_start, &expr_end, 0);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021302 if (expr_start != NULL)
21303 {
21304 /* Further expansion! */
21305 temp_result = make_expanded_name(retval, expr_start,
21306 expr_end, temp_result);
21307 vim_free(retval);
21308 retval = temp_result;
21309 }
21310 }
21311
21312 return retval;
21313}
21314
21315/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021316 * Return TRUE if character "c" can be used in a variable or function name.
Bram Moolenaare9a41262005-01-15 22:18:47 +000021317 * Does not include '{' or '}' for magic braces.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021318 */
21319 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021320eval_isnamec(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021321{
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021322 return (ASCII_ISALNUM(c) || c == '_' || c == ':' || c == AUTOLOAD_CHAR);
21323}
21324
21325/*
21326 * Return TRUE if character "c" can be used as the first character in a
21327 * variable or function name (excluding '{' and '}').
21328 */
21329 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021330eval_isnamec1(int c)
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000021331{
21332 return (ASCII_ISALPHA(c) || c == '_');
Bram Moolenaar071d4272004-06-13 20:20:40 +000021333}
21334
21335/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021336 * Set number v: variable to "val".
21337 */
21338 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021339set_vim_var_nr(int idx, long val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021340{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021341 vimvars[idx].vv_nr = val;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021342}
21343
21344/*
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021345 * Get number v: variable value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021346 */
21347 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021348get_vim_var_nr(int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021349{
Bram Moolenaare9a41262005-01-15 22:18:47 +000021350 return vimvars[idx].vv_nr;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021351}
21352
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021353/*
21354 * Get string v: variable value. Uses a static buffer, can only be used once.
21355 */
21356 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021357get_vim_var_str(int idx)
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021358{
21359 return get_tv_string(&vimvars[idx].vv_tv);
21360}
Bram Moolenaar19a09a12005-03-04 23:39:37 +000021361
Bram Moolenaar071d4272004-06-13 20:20:40 +000021362/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021363 * Get List v: variable value. Caller must take care of reference count when
21364 * needed.
21365 */
21366 list_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021367get_vim_var_list(int idx)
Bram Moolenaard812df62008-11-09 12:46:09 +000021368{
21369 return vimvars[idx].vv_list;
21370}
21371
21372/*
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021373 * Set v:char to character "c".
21374 */
21375 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021376set_vim_var_char(int c)
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021377{
Bram Moolenaar9a920d82012-06-01 15:21:02 +020021378 char_u buf[MB_MAXBYTES + 1];
Bram Moolenaarda9591e2009-09-30 13:17:02 +000021379
21380#ifdef FEAT_MBYTE
21381 if (has_mbyte)
21382 buf[(*mb_char2bytes)(c, buf)] = NUL;
21383 else
21384#endif
21385 {
21386 buf[0] = c;
21387 buf[1] = NUL;
21388 }
21389 set_vim_var_string(VV_CHAR, buf, -1);
21390}
21391
21392/*
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021393 * Set v:count to "count" and v:count1 to "count1".
21394 * When "set_prevcount" is TRUE first set v:prevcount from v:count.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021395 */
21396 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021397set_vcount(
21398 long count,
21399 long count1,
21400 int set_prevcount)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021401{
Bram Moolenaar8df74be2008-11-20 15:12:02 +000021402 if (set_prevcount)
21403 vimvars[VV_PREVCOUNT].vv_nr = vimvars[VV_COUNT].vv_nr;
Bram Moolenaare9a41262005-01-15 22:18:47 +000021404 vimvars[VV_COUNT].vv_nr = count;
21405 vimvars[VV_COUNT1].vv_nr = count1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021406}
21407
21408/*
21409 * Set string v: variable to a copy of "val".
21410 */
21411 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021412set_vim_var_string(
21413 int idx,
21414 char_u *val,
21415 int len) /* length of "val" to use or -1 (whole string) */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021416{
Bram Moolenaara542c682016-01-31 16:28:04 +010021417 clear_tv(&vimvars[idx].vv_di.di_tv);
21418 vimvars[idx].vv_type = VAR_STRING;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021419 if (val == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021420 vimvars[idx].vv_str = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021421 else if (len == -1)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021422 vimvars[idx].vv_str = vim_strsave(val);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021423 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000021424 vimvars[idx].vv_str = vim_strnsave(val, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021425}
21426
21427/*
Bram Moolenaard812df62008-11-09 12:46:09 +000021428 * Set List v: variable to "val".
21429 */
21430 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021431set_vim_var_list(int idx, list_T *val)
Bram Moolenaard812df62008-11-09 12:46:09 +000021432{
Bram Moolenaara542c682016-01-31 16:28:04 +010021433 clear_tv(&vimvars[idx].vv_di.di_tv);
21434 vimvars[idx].vv_type = VAR_LIST;
Bram Moolenaard812df62008-11-09 12:46:09 +000021435 vimvars[idx].vv_list = val;
21436 if (val != NULL)
21437 ++val->lv_refcount;
21438}
21439
21440/*
Bram Moolenaar42a45122015-07-10 17:56:23 +020021441 * Set Dictionary v: variable to "val".
21442 */
21443 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021444set_vim_var_dict(int idx, dict_T *val)
Bram Moolenaar42a45122015-07-10 17:56:23 +020021445{
21446 int todo;
21447 hashitem_T *hi;
21448
Bram Moolenaara542c682016-01-31 16:28:04 +010021449 clear_tv(&vimvars[idx].vv_di.di_tv);
21450 vimvars[idx].vv_type = VAR_DICT;
Bram Moolenaar42a45122015-07-10 17:56:23 +020021451 vimvars[idx].vv_dict = val;
21452 if (val != NULL)
21453 {
21454 ++val->dv_refcount;
21455
21456 /* Set readonly */
21457 todo = (int)val->dv_hashtab.ht_used;
21458 for (hi = val->dv_hashtab.ht_array; todo > 0 ; ++hi)
21459 {
21460 if (HASHITEM_EMPTY(hi))
21461 continue;
21462 --todo;
21463 HI2DI(hi)->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
21464 }
21465 }
21466}
21467
21468/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021469 * Set v:register if needed.
21470 */
21471 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021472set_reg_var(int c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021473{
21474 char_u regname;
21475
21476 if (c == 0 || c == ' ')
21477 regname = '"';
21478 else
21479 regname = c;
21480 /* Avoid free/alloc when the value is already right. */
Bram Moolenaare9a41262005-01-15 22:18:47 +000021481 if (vimvars[VV_REG].vv_str == NULL || vimvars[VV_REG].vv_str[0] != c)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021482 set_vim_var_string(VV_REG, &regname, 1);
21483}
21484
21485/*
21486 * Get or set v:exception. If "oldval" == NULL, return the current value.
21487 * Otherwise, restore the value to "oldval" and return NULL.
21488 * Must always be called in pairs to save and restore v:exception! Does not
21489 * take care of memory allocations.
21490 */
21491 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021492v_exception(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021493{
21494 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021495 return vimvars[VV_EXCEPTION].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021496
Bram Moolenaare9a41262005-01-15 22:18:47 +000021497 vimvars[VV_EXCEPTION].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021498 return NULL;
21499}
21500
21501/*
21502 * Get or set v:throwpoint. If "oldval" == NULL, return the current value.
21503 * Otherwise, restore the value to "oldval" and return NULL.
21504 * Must always be called in pairs to save and restore v:throwpoint! Does not
21505 * take care of memory allocations.
21506 */
21507 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021508v_throwpoint(char_u *oldval)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021509{
21510 if (oldval == NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021511 return vimvars[VV_THROWPOINT].vv_str;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021512
Bram Moolenaare9a41262005-01-15 22:18:47 +000021513 vimvars[VV_THROWPOINT].vv_str = oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021514 return NULL;
21515}
21516
21517#if defined(FEAT_AUTOCMD) || defined(PROTO)
21518/*
21519 * Set v:cmdarg.
21520 * If "eap" != NULL, use "eap" to generate the value and return the old value.
21521 * If "oldarg" != NULL, restore the value to "oldarg" and return NULL.
21522 * Must always be called in pairs!
21523 */
21524 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021525set_cmdarg(exarg_T *eap, char_u *oldarg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021526{
21527 char_u *oldval;
21528 char_u *newval;
21529 unsigned len;
21530
Bram Moolenaare9a41262005-01-15 22:18:47 +000021531 oldval = vimvars[VV_CMDARG].vv_str;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021532 if (eap == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021533 {
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021534 vim_free(oldval);
Bram Moolenaare9a41262005-01-15 22:18:47 +000021535 vimvars[VV_CMDARG].vv_str = oldarg;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021536 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021537 }
21538
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021539 if (eap->force_bin == FORCE_BIN)
21540 len = 6;
21541 else if (eap->force_bin == FORCE_NOBIN)
21542 len = 8;
21543 else
21544 len = 0;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021545
21546 if (eap->read_edit)
21547 len += 7;
21548
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021549 if (eap->force_ff != 0)
21550 len += (unsigned)STRLEN(eap->cmd + eap->force_ff) + 6;
21551# ifdef FEAT_MBYTE
21552 if (eap->force_enc != 0)
21553 len += (unsigned)STRLEN(eap->cmd + eap->force_enc) + 7;
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021554 if (eap->bad_char != 0)
21555 len += 7 + 4; /* " ++bad=" + "keep" or "drop" */
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021556# endif
21557
21558 newval = alloc(len + 1);
21559 if (newval == NULL)
21560 return NULL;
21561
21562 if (eap->force_bin == FORCE_BIN)
21563 sprintf((char *)newval, " ++bin");
21564 else if (eap->force_bin == FORCE_NOBIN)
21565 sprintf((char *)newval, " ++nobin");
21566 else
21567 *newval = NUL;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000021568
21569 if (eap->read_edit)
21570 STRCAT(newval, " ++edit");
21571
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021572 if (eap->force_ff != 0)
21573 sprintf((char *)newval + STRLEN(newval), " ++ff=%s",
21574 eap->cmd + eap->force_ff);
21575# ifdef FEAT_MBYTE
21576 if (eap->force_enc != 0)
21577 sprintf((char *)newval + STRLEN(newval), " ++enc=%s",
21578 eap->cmd + eap->force_enc);
Bram Moolenaar34b4daf2010-05-16 13:26:25 +020021579 if (eap->bad_char == BAD_KEEP)
21580 STRCPY(newval + STRLEN(newval), " ++bad=keep");
21581 else if (eap->bad_char == BAD_DROP)
21582 STRCPY(newval + STRLEN(newval), " ++bad=drop");
21583 else if (eap->bad_char != 0)
21584 sprintf((char *)newval + STRLEN(newval), " ++bad=%c", eap->bad_char);
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021585# endif
Bram Moolenaare9a41262005-01-15 22:18:47 +000021586 vimvars[VV_CMDARG].vv_str = newval;
Bram Moolenaar3fdfa4a2004-10-07 21:02:47 +000021587 return oldval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021588}
21589#endif
21590
21591/*
21592 * Get the value of internal variable "name".
21593 * Return OK or FAIL.
21594 */
21595 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021596get_var_tv(
21597 char_u *name,
21598 int len, /* length of "name" */
21599 typval_T *rettv, /* NULL when only checking existence */
21600 dictitem_T **dip, /* non-NULL when typval's dict item is needed */
21601 int verbose, /* may give error message */
21602 int no_autoload) /* do not use script autoloading */
Bram Moolenaar071d4272004-06-13 20:20:40 +000021603{
21604 int ret = OK;
Bram Moolenaar33570922005-01-25 22:26:29 +000021605 typval_T *tv = NULL;
21606 typval_T atv;
21607 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021608 int cc;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021609
21610 /* truncate the name, so that we can use strcmp() */
21611 cc = name[len];
21612 name[len] = NUL;
21613
21614 /*
21615 * Check for "b:changedtick".
21616 */
21617 if (STRCMP(name, "b:changedtick") == 0)
21618 {
Bram Moolenaare9a41262005-01-15 22:18:47 +000021619 atv.v_type = VAR_NUMBER;
21620 atv.vval.v_number = curbuf->b_changedtick;
21621 tv = &atv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021622 }
21623
21624 /*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021625 * Check for user-defined variables.
21626 */
21627 else
21628 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010021629 v = find_var(name, NULL, no_autoload);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021630 if (v != NULL)
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021631 {
Bram Moolenaar33570922005-01-25 22:26:29 +000021632 tv = &v->di_tv;
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020021633 if (dip != NULL)
21634 *dip = v;
21635 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021636 }
21637
Bram Moolenaare9a41262005-01-15 22:18:47 +000021638 if (tv == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021639 {
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021640 if (rettv != NULL && verbose)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021641 EMSG2(_(e_undefvar), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021642 ret = FAIL;
21643 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021644 else if (rettv != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000021645 copy_tv(tv, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000021646
21647 name[len] = cc;
21648
21649 return ret;
21650}
21651
21652/*
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021653 * Handle expr[expr], expr[expr:expr] subscript and .name lookup.
21654 * Also handle function call with Funcref variable: func(expr)
21655 * Can all be combined: dict.func(expr)[idx]['func'](expr)
21656 */
21657 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010021658handle_subscript(
21659 char_u **arg,
21660 typval_T *rettv,
21661 int evaluate, /* do more than finding the end */
21662 int verbose) /* give error messages */
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021663{
21664 int ret = OK;
21665 dict_T *selfdict = NULL;
21666 char_u *s;
21667 int len;
Bram Moolenaard9fba312005-06-26 22:34:35 +000021668 typval_T functv;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021669 partial_T *pt = NULL;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021670
21671 while (ret == OK
21672 && (**arg == '['
21673 || (**arg == '.' && rettv->v_type == VAR_DICT)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021674 || (**arg == '(' && (!evaluate || rettv->v_type == VAR_FUNC
21675 || rettv->v_type == VAR_PARTIAL)))
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021676 && !vim_iswhite(*(*arg - 1)))
21677 {
21678 if (**arg == '(')
21679 {
Bram Moolenaard9fba312005-06-26 22:34:35 +000021680 /* need to copy the funcref so that we can clear rettv */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021681 if (evaluate)
21682 {
21683 functv = *rettv;
21684 rettv->v_type = VAR_UNKNOWN;
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021685
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021686 /* Invoke the function. Recursive! */
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021687 if (functv.v_type == VAR_PARTIAL)
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021688 {
21689 pt = functv.vval.v_partial;
21690 s = pt->pt_name;
21691 }
21692 else
21693 s = functv.vval.v_string;
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021694 }
21695 else
21696 s = (char_u *)"";
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000021697 ret = get_func_tv(s, (int)STRLEN(s), rettv, arg,
Bram Moolenaard9fba312005-06-26 22:34:35 +000021698 curwin->w_cursor.lnum, curwin->w_cursor.lnum,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021699 &len, evaluate, pt, selfdict);
Bram Moolenaard9fba312005-06-26 22:34:35 +000021700
21701 /* Clear the funcref afterwards, so that deleting it while
21702 * evaluating the arguments is possible (see test55). */
Bram Moolenaar0f8de8d2013-11-11 04:25:53 +010021703 if (evaluate)
21704 clear_tv(&functv);
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021705
21706 /* Stop the expression evaluation when immediately aborting on
21707 * error, or when an interrupt occurred or an exception was thrown
21708 * but not caught. */
21709 if (aborting())
21710 {
21711 if (ret == OK)
21712 clear_tv(rettv);
21713 ret = FAIL;
21714 }
21715 dict_unref(selfdict);
21716 selfdict = NULL;
21717 }
21718 else /* **arg == '[' || **arg == '.' */
21719 {
21720 dict_unref(selfdict);
21721 if (rettv->v_type == VAR_DICT)
21722 {
21723 selfdict = rettv->vval.v_dict;
21724 if (selfdict != NULL)
21725 ++selfdict->dv_refcount;
21726 }
21727 else
21728 selfdict = NULL;
21729 if (eval_index(arg, rettv, evaluate, verbose) == FAIL)
21730 {
21731 clear_tv(rettv);
21732 ret = FAIL;
21733 }
21734 }
21735 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021736
21737 if (rettv->v_type == VAR_FUNC && selfdict != NULL)
21738 {
Bram Moolenaar6f2e4b32016-03-16 22:52:12 +010021739 char_u *fname;
21740 char_u *tofree = NULL;
21741 ufunc_T *fp;
21742 char_u fname_buf[FLEN_FIXED + 1];
21743 int error;
21744
21745 /* Translate "s:func" to the stored function name. */
21746 fname = fname_trans_sid(rettv->vval.v_string, fname_buf,
21747 &tofree, &error);
21748 fp = find_func(fname);
21749 vim_free(tofree);
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021750
21751 /* Turn "dict.Func" into a partial for "Func" with "dict". */
Bram Moolenaar65639032016-03-16 21:40:30 +010021752 if (fp != NULL && (fp->uf_flags & FC_DICT))
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021753 {
Bram Moolenaar65639032016-03-16 21:40:30 +010021754 partial_T *pt = (partial_T *)alloc_clear(sizeof(partial_T));
21755
21756 if (pt != NULL)
21757 {
21758 pt->pt_refcount = 1;
21759 pt->pt_dict = selfdict;
21760 selfdict = NULL;
21761 pt->pt_name = rettv->vval.v_string;
21762 func_ref(pt->pt_name);
21763 rettv->v_type = VAR_PARTIAL;
21764 rettv->vval.v_partial = pt;
21765 }
Bram Moolenaarab1fa392016-03-15 19:33:34 +010021766 }
21767 }
21768
Bram Moolenaar2a8d1f82005-02-05 21:43:56 +000021769 dict_unref(selfdict);
21770 return ret;
21771}
21772
21773/*
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021774 * Allocate memory for a variable type-value, and make it empty (0 or NULL
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021775 * value).
21776 */
Bram Moolenaar11e0afa2016-02-01 22:41:00 +010021777 typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021778alloc_tv(void)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021779{
Bram Moolenaar33570922005-01-25 22:26:29 +000021780 return (typval_T *)alloc_clear((unsigned)sizeof(typval_T));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021781}
21782
21783/*
21784 * Allocate memory for a variable type-value, and assign a string to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021785 * The string "s" must have been allocated, it is consumed.
21786 * Return NULL for out of memory, the variable otherwise.
21787 */
Bram Moolenaar33570922005-01-25 22:26:29 +000021788 static typval_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010021789alloc_string_tv(char_u *s)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021790{
Bram Moolenaar33570922005-01-25 22:26:29 +000021791 typval_T *rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021792
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021793 rettv = alloc_tv();
21794 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021795 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021796 rettv->v_type = VAR_STRING;
21797 rettv->vval.v_string = s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021798 }
21799 else
21800 vim_free(s);
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021801 return rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021802}
21803
21804/*
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021805 * Free the memory for a variable type-value.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021806 */
Bram Moolenaar4770d092006-01-12 23:22:24 +000021807 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021808free_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021809{
21810 if (varp != NULL)
21811 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021812 switch (varp->v_type)
21813 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021814 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021815 func_unref(varp->vval.v_string);
21816 /*FALLTHROUGH*/
21817 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021818 vim_free(varp->vval.v_string);
21819 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021820 case VAR_PARTIAL:
21821 partial_unref(varp->vval.v_partial);
21822 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021823 case VAR_LIST:
21824 list_unref(varp->vval.v_list);
21825 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021826 case VAR_DICT:
21827 dict_unref(varp->vval.v_dict);
21828 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021829 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021830#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021831 job_unref(varp->vval.v_job);
21832 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021833#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021834 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021835#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021836 channel_unref(varp->vval.v_channel);
21837 break;
21838#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021839 case VAR_NUMBER:
21840 case VAR_FLOAT:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021841 case VAR_UNKNOWN:
Bram Moolenaar6650a692016-01-26 19:59:10 +010021842 case VAR_SPECIAL:
Bram Moolenaar758711c2005-02-02 23:11:38 +000021843 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021844 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000021845 vim_free(varp);
21846 }
21847}
21848
21849/*
21850 * Free the memory for a variable value and set the value to NULL or 0.
21851 */
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000021852 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021853clear_tv(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021854{
21855 if (varp != NULL)
21856 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021857 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021858 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021859 case VAR_FUNC:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021860 func_unref(varp->vval.v_string);
21861 /*FALLTHROUGH*/
21862 case VAR_STRING:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021863 vim_free(varp->vval.v_string);
21864 varp->vval.v_string = NULL;
21865 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021866 case VAR_PARTIAL:
21867 partial_unref(varp->vval.v_partial);
21868 varp->vval.v_partial = NULL;
21869 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021870 case VAR_LIST:
21871 list_unref(varp->vval.v_list);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021872 varp->vval.v_list = NULL;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021873 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021874 case VAR_DICT:
21875 dict_unref(varp->vval.v_dict);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000021876 varp->vval.v_dict = NULL;
Bram Moolenaar8c711452005-01-14 21:53:12 +000021877 break;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021878 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010021879 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021880 varp->vval.v_number = 0;
21881 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021882 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021883#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021884 varp->vval.v_float = 0.0;
21885 break;
21886#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010021887 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021888#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021889 job_unref(varp->vval.v_job);
21890 varp->vval.v_job = NULL;
21891#endif
21892 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010021893 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021894#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021895 channel_unref(varp->vval.v_channel);
21896 varp->vval.v_channel = NULL;
21897#endif
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021898 case VAR_UNKNOWN:
21899 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021900 }
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000021901 varp->v_lock = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021902 }
21903}
21904
21905/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021906 * Set the value of a variable to NULL without freeing items.
21907 */
21908 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010021909init_tv(typval_T *varp)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021910{
21911 if (varp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000021912 vim_memset(varp, 0, sizeof(typval_T));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000021913}
21914
21915/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000021916 * Get the number value of a variable.
21917 * If it is a String variable, uses vim_str2nr().
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021918 * For incompatible types, return 0.
21919 * get_tv_number_chk() is similar to get_tv_number(), but informs the
21920 * caller of incompatible types: it sets *denote to TRUE if "denote"
21921 * is not NULL or returns -1 otherwise.
Bram Moolenaar071d4272004-06-13 20:20:40 +000021922 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010021923 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021924get_tv_number(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021925{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021926 int error = FALSE;
21927
21928 return get_tv_number_chk(varp, &error); /* return 0L on error */
21929}
21930
Bram Moolenaar4be06f92005-07-29 22:36:03 +000021931 long
Bram Moolenaar7454a062016-01-30 15:14:10 +010021932get_tv_number_chk(typval_T *varp, int *denote)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021933{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021934 long n = 0L;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021935
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021936 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000021937 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021938 case VAR_NUMBER:
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021939 return (long)(varp->vval.v_number);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021940 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010021941#ifdef FEAT_FLOAT
Bram Moolenaared0e7452008-06-27 19:17:34 +000021942 EMSG(_("E805: Using a Float as a Number"));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000021943 break;
21944#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021945 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021946 case VAR_PARTIAL:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021947 EMSG(_("E703: Using a Funcref as a Number"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021948 break;
21949 case VAR_STRING:
21950 if (varp->vval.v_string != NULL)
21951 vim_str2nr(varp->vval.v_string, NULL, NULL,
Bram Moolenaar887c1fe2016-01-02 17:56:35 +010021952 STR2NR_ALL, &n, NULL, 0);
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021953 return n;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021954 case VAR_LIST:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021955 EMSG(_("E745: Using a List as a Number"));
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000021956 break;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021957 case VAR_DICT:
Bram Moolenaared0e7452008-06-27 19:17:34 +000021958 EMSG(_("E728: Using a Dictionary as a Number"));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000021959 break;
Bram Moolenaar17a13432016-01-24 14:22:10 +010021960 case VAR_SPECIAL:
21961 return varp->vval.v_number == VVAL_TRUE ? 1 : 0;
21962 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010021963 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021964#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010021965 EMSG(_("E910: Using a Job as a Number"));
21966 break;
21967#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010021968 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010021969#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010021970 EMSG(_("E913: Using a Channel as a Number"));
21971 break;
21972#endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010021973 case VAR_UNKNOWN:
21974 EMSG2(_(e_intern2), "get_tv_number(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021975 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021976 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000021977 if (denote == NULL) /* useful for values that must be unsigned */
21978 n = -1;
21979 else
21980 *denote = TRUE;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000021981 return n;
Bram Moolenaar071d4272004-06-13 20:20:40 +000021982}
21983
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021984#ifdef FEAT_FLOAT
21985 static float_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010021986get_tv_float(typval_T *varp)
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021987{
21988 switch (varp->v_type)
21989 {
21990 case VAR_NUMBER:
21991 return (float_T)(varp->vval.v_number);
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021992 case VAR_FLOAT:
21993 return varp->vval.v_float;
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021994 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010021995 case VAR_PARTIAL:
Bram Moolenaarf7edf402016-01-19 23:36:15 +010021996 EMSG(_("E891: Using a Funcref as a Float"));
21997 break;
21998 case VAR_STRING:
21999 EMSG(_("E892: Using a String as a Float"));
22000 break;
22001 case VAR_LIST:
22002 EMSG(_("E893: Using a List as a Float"));
22003 break;
22004 case VAR_DICT:
22005 EMSG(_("E894: Using a Dictionary as a Float"));
22006 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022007 case VAR_SPECIAL:
22008 EMSG(_("E907: Using a special value as a Float"));
22009 break;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022010 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022011# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022012 EMSG(_("E911: Using a Job as a Float"));
22013 break;
22014# endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022015 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022016# ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022017 EMSG(_("E914: Using a Channel as a Float"));
22018 break;
22019# endif
Bram Moolenaara03f2332016-02-06 18:09:59 +010022020 case VAR_UNKNOWN:
22021 EMSG2(_(e_intern2), "get_tv_float(UNKNOWN)");
Bram Moolenaarf7edf402016-01-19 23:36:15 +010022022 break;
22023 }
22024 return 0;
22025}
22026#endif
22027
Bram Moolenaar071d4272004-06-13 20:20:40 +000022028/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022029 * Get the lnum from the first argument.
22030 * Also accepts ".", "$", etc., but that only works for the current buffer.
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022031 * Returns -1 on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022032 */
22033 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022034get_tv_lnum(typval_T *argvars)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022035{
Bram Moolenaar33570922005-01-25 22:26:29 +000022036 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022037 linenr_T lnum;
22038
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022039 lnum = get_tv_number_chk(&argvars[0], NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022040 if (lnum == 0) /* no valid number, try using line() */
22041 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022042 rettv.v_type = VAR_NUMBER;
22043 f_line(argvars, &rettv);
22044 lnum = rettv.vval.v_number;
22045 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022046 }
22047 return lnum;
22048}
22049
22050/*
Bram Moolenaar661b1822005-07-28 22:36:45 +000022051 * Get the lnum from the first argument.
22052 * Also accepts "$", then "buf" is used.
22053 * Returns 0 on error.
22054 */
22055 static linenr_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010022056get_tv_lnum_buf(typval_T *argvars, buf_T *buf)
Bram Moolenaar661b1822005-07-28 22:36:45 +000022057{
22058 if (argvars[0].v_type == VAR_STRING
22059 && argvars[0].vval.v_string != NULL
22060 && argvars[0].vval.v_string[0] == '$'
22061 && buf != NULL)
22062 return buf->b_ml.ml_line_count;
22063 return get_tv_number_chk(&argvars[0], NULL);
22064}
22065
22066/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022067 * Get the string value of a variable.
22068 * If it is a Number variable, the number is converted into a string.
Bram Moolenaara7043832005-01-21 11:56:39 +000022069 * get_tv_string() uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22070 * get_tv_string_buf() uses a given buffer.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022071 * If the String variable has never been set, return an empty string.
22072 * Never returns NULL;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022073 * get_tv_string_chk() and get_tv_string_buf_chk() are similar, but return
22074 * NULL on error.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022075 */
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022076 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022077get_tv_string(typval_T *varp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022078{
22079 static char_u mybuf[NUMBUFLEN];
22080
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022081 return get_tv_string_buf(varp, mybuf);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022082}
22083
Bram Moolenaar8e2c9422016-03-12 13:43:33 +010022084 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022085get_tv_string_buf(typval_T *varp, char_u *buf)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022086{
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022087 char_u *res = get_tv_string_buf_chk(varp, buf);
22088
22089 return res != NULL ? res : (char_u *)"";
22090}
22091
Bram Moolenaar7d647822014-04-05 21:28:56 +020022092/*
22093 * Careful: This uses a single, static buffer. YOU CAN ONLY USE IT ONCE!
22094 */
Bram Moolenaar4be06f92005-07-29 22:36:03 +000022095 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022096get_tv_string_chk(typval_T *varp)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022097{
22098 static char_u mybuf[NUMBUFLEN];
22099
22100 return get_tv_string_buf_chk(varp, mybuf);
22101}
22102
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022103 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022104get_tv_string_buf_chk(typval_T *varp, char_u *buf)
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022105{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022106 switch (varp->v_type)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022107 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022108 case VAR_NUMBER:
22109 sprintf((char *)buf, "%ld", (long)varp->vval.v_number);
22110 return buf;
22111 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022112 case VAR_PARTIAL:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022113 EMSG(_("E729: using Funcref as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022114 break;
22115 case VAR_LIST:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022116 EMSG(_("E730: using List as a String"));
Bram Moolenaar8c711452005-01-14 21:53:12 +000022117 break;
22118 case VAR_DICT:
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022119 EMSG(_("E731: using Dictionary as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022120 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022121 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022122#ifdef FEAT_FLOAT
Bram Moolenaar2a876e42013-06-12 22:08:58 +020022123 EMSG(_(e_float_as_string));
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022124 break;
22125#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022126 case VAR_STRING:
22127 if (varp->vval.v_string != NULL)
22128 return varp->vval.v_string;
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022129 return (char_u *)"";
Bram Moolenaar17a13432016-01-24 14:22:10 +010022130 case VAR_SPECIAL:
22131 STRCPY(buf, get_var_special_name(varp->vval.v_number));
22132 return buf;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022133 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022134#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022135 {
22136 job_T *job = varp->vval.v_job;
Bram Moolenaar839fd112016-03-06 21:34:03 +010022137 char *status;
22138
22139 if (job == NULL)
22140 return (char_u *)"no process";
22141 status = job->jv_status == JOB_FAILED ? "fail"
Bram Moolenaar835dc632016-02-07 14:27:38 +010022142 : job->jv_status == JOB_ENDED ? "dead"
22143 : "run";
22144# ifdef UNIX
22145 vim_snprintf((char *)buf, NUMBUFLEN,
22146 "process %ld %s", (long)job->jv_pid, status);
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022147# elif defined(WIN32)
22148 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar76467df2016-02-12 19:30:26 +010022149 "process %ld %s",
22150 (long)job->jv_proc_info.dwProcessId,
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022151 status);
Bram Moolenaar835dc632016-02-07 14:27:38 +010022152# else
Bram Moolenaar4d8747c2016-02-09 20:39:26 +010022153 /* fall-back */
Bram Moolenaar835dc632016-02-07 14:27:38 +010022154 vim_snprintf((char *)buf, NUMBUFLEN, "process ? %s", status);
22155# endif
22156 return buf;
22157 }
22158#endif
22159 break;
Bram Moolenaar77073442016-02-13 23:23:53 +010022160 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022161#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022162 {
22163 channel_T *channel = varp->vval.v_channel;
22164 char *status = channel_status(channel);
22165
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022166 if (channel == NULL)
22167 vim_snprintf((char *)buf, NUMBUFLEN, "channel %s", status);
22168 else
22169 vim_snprintf((char *)buf, NUMBUFLEN,
Bram Moolenaar77073442016-02-13 23:23:53 +010022170 "channel %d %s", channel->ch_id, status);
22171 return buf;
22172 }
22173#endif
22174 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022175 case VAR_UNKNOWN:
22176 EMSG(_("E908: using an invalid value as a String"));
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022177 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022178 }
Bram Moolenaar9ba0eb82005-06-13 22:28:56 +000022179 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022180}
22181
22182/*
22183 * Find variable "name" in the list of variables.
22184 * Return a pointer to it if found, NULL if not found.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022185 * Careful: "a:0" variables don't have a name.
Bram Moolenaara7043832005-01-21 11:56:39 +000022186 * When "htp" is not NULL we are writing to the variable, set "htp" to the
Bram Moolenaar33570922005-01-25 22:26:29 +000022187 * hashtab_T used.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022188 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022189 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022190find_var(char_u *name, hashtab_T **htp, int no_autoload)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022191{
Bram Moolenaar071d4272004-06-13 20:20:40 +000022192 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022193 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022194
Bram Moolenaara7043832005-01-21 11:56:39 +000022195 ht = find_var_ht(name, &varname);
22196 if (htp != NULL)
22197 *htp = ht;
22198 if (ht == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022199 return NULL;
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022200 return find_var_in_ht(ht, *name, varname, no_autoload || htp != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022201}
22202
22203/*
Bram Moolenaar332ac062013-04-15 13:06:21 +020022204 * Find variable "varname" in hashtab "ht" with name "htname".
Bram Moolenaara7043832005-01-21 11:56:39 +000022205 * Returns NULL if not found.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022206 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022207 static dictitem_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022208find_var_in_ht(
22209 hashtab_T *ht,
22210 int htname,
22211 char_u *varname,
22212 int no_autoload)
Bram Moolenaara7043832005-01-21 11:56:39 +000022213{
Bram Moolenaar33570922005-01-25 22:26:29 +000022214 hashitem_T *hi;
22215
22216 if (*varname == NUL)
22217 {
22218 /* Must be something like "s:", otherwise "ht" would be NULL. */
Bram Moolenaar332ac062013-04-15 13:06:21 +020022219 switch (htname)
Bram Moolenaar33570922005-01-25 22:26:29 +000022220 {
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022221 case 's': return &SCRIPT_SV(current_SID)->sv_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022222 case 'g': return &globvars_var;
22223 case 'v': return &vimvars_var;
22224 case 'b': return &curbuf->b_bufvar;
22225 case 'w': return &curwin->w_winvar;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022226#ifdef FEAT_WINDOWS
22227 case 't': return &curtab->tp_winvar;
22228#endif
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022229 case 'l': return current_funccal == NULL
22230 ? NULL : &current_funccal->l_vars_var;
22231 case 'a': return current_funccal == NULL
22232 ? NULL : &current_funccal->l_avars_var;
Bram Moolenaar33570922005-01-25 22:26:29 +000022233 }
22234 return NULL;
22235 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022236
22237 hi = hash_find(ht, varname);
22238 if (HASHITEM_EMPTY(hi))
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022239 {
22240 /* For global variables we may try auto-loading the script. If it
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000022241 * worked find the variable again. Don't auto-load a script if it was
22242 * loaded already, otherwise it would be loaded every time when
22243 * checking if a function name is a Funcref variable. */
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022244 if (ht == &globvarht && !no_autoload)
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022245 {
22246 /* Note: script_autoload() may make "hi" invalid. It must either
22247 * be obtained again or not used. */
22248 if (!script_autoload(varname, FALSE) || aborting())
22249 return NULL;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022250 hi = hash_find(ht, varname);
Bram Moolenaar8000baf2011-11-30 15:19:28 +010022251 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022252 if (HASHITEM_EMPTY(hi))
22253 return NULL;
22254 }
Bram Moolenaar33570922005-01-25 22:26:29 +000022255 return HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022256}
22257
22258/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022259 * Find the hashtab used for a variable name.
Bram Moolenaar73627d02015-08-11 15:46:09 +020022260 * Return NULL if the name is not valid.
Bram Moolenaara7043832005-01-21 11:56:39 +000022261 * Set "varname" to the start of name without ':'.
22262 */
Bram Moolenaar33570922005-01-25 22:26:29 +000022263 static hashtab_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022264find_var_ht(char_u *name, char_u **varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022265{
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022266 hashitem_T *hi;
22267
Bram Moolenaar73627d02015-08-11 15:46:09 +020022268 if (name[0] == NUL)
22269 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022270 if (name[1] != ':')
22271 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022272 /* The name must not start with a colon or #. */
22273 if (name[0] == ':' || name[0] == AUTOLOAD_CHAR)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022274 return NULL;
22275 *varname = name;
Bram Moolenaar532c7802005-01-27 14:44:31 +000022276
22277 /* "version" is "v:version" in all scopes */
Bram Moolenaar75c50c42005-06-04 22:06:24 +000022278 hi = hash_find(&compat_hashtab, name);
22279 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar532c7802005-01-27 14:44:31 +000022280 return &compat_hashtab;
22281
Bram Moolenaar071d4272004-06-13 20:20:40 +000022282 if (current_funccal == NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000022283 return &globvarht; /* global variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022284 return &get_funccal()->l_vars.dv_hashtab; /* l: variable */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022285 }
22286 *varname = name + 2;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022287 if (*name == 'g') /* global variable */
22288 return &globvarht;
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000022289 /* There must be no ':' or '#' in the rest of the name, unless g: is used
22290 */
22291 if (vim_strchr(name + 2, ':') != NULL
22292 || vim_strchr(name + 2, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000022293 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022294 if (*name == 'b') /* buffer variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022295 return &curbuf->b_vars->dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022296 if (*name == 'w') /* window variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022297 return &curwin->w_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022298#ifdef FEAT_WINDOWS
22299 if (*name == 't') /* tab page variable */
Bram Moolenaar429fa852013-04-15 12:27:36 +020022300 return &curtab->tp_vars->dv_hashtab;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000022301#endif
Bram Moolenaar33570922005-01-25 22:26:29 +000022302 if (*name == 'v') /* v: variable */
22303 return &vimvarht;
22304 if (*name == 'a' && current_funccal != NULL) /* function argument */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022305 return &get_funccal()->l_avars.dv_hashtab;
Bram Moolenaar33570922005-01-25 22:26:29 +000022306 if (*name == 'l' && current_funccal != NULL) /* local function variable */
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022307 return &get_funccal()->l_vars.dv_hashtab;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022308 if (*name == 's' /* script variable */
22309 && current_SID > 0 && current_SID <= ga_scripts.ga_len)
22310 return &SCRIPT_VARS(current_SID);
22311 return NULL;
22312}
22313
22314/*
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022315 * Get function call environment based on bactrace debug level
22316 */
22317 static funccall_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022318get_funccal(void)
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022319{
22320 int i;
22321 funccall_T *funccal;
22322 funccall_T *temp_funccal;
22323
22324 funccal = current_funccal;
22325 if (debug_backtrace_level > 0)
22326 {
Bram Moolenaarc9703302016-01-17 21:49:33 +010022327 for (i = 0; i < debug_backtrace_level; i++)
22328 {
22329 temp_funccal = funccal->caller;
22330 if (temp_funccal)
22331 funccal = temp_funccal;
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022332 else
Bram Moolenaarc9703302016-01-17 21:49:33 +010022333 /* backtrace level overflow. reset to max */
22334 debug_backtrace_level = i;
22335 }
Bram Moolenaarf1f60f82016-01-16 15:40:53 +010022336 }
22337 return funccal;
22338}
22339
22340/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022341 * Get the string value of a (global/local) variable.
Bram Moolenaar1950c352010-06-06 15:21:10 +020022342 * Note: see get_tv_string() for how long the pointer remains valid.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022343 * Returns NULL when it doesn't exist.
22344 */
22345 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010022346get_var_value(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022347{
Bram Moolenaar33570922005-01-25 22:26:29 +000022348 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022349
Bram Moolenaar6d977d62014-01-14 15:24:39 +010022350 v = find_var(name, NULL, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022351 if (v == NULL)
22352 return NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000022353 return get_tv_string(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022354}
22355
22356/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022357 * Allocate a new hashtab for a sourced script. It will be used while
Bram Moolenaar071d4272004-06-13 20:20:40 +000022358 * sourcing this script and when executing functions defined in the script.
22359 */
22360 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022361new_script_vars(scid_T id)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022362{
Bram Moolenaara7043832005-01-21 11:56:39 +000022363 int i;
Bram Moolenaar33570922005-01-25 22:26:29 +000022364 hashtab_T *ht;
22365 scriptvar_T *sv;
Bram Moolenaara7043832005-01-21 11:56:39 +000022366
Bram Moolenaar071d4272004-06-13 20:20:40 +000022367 if (ga_grow(&ga_scripts, (int)(id - ga_scripts.ga_len)) == OK)
22368 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022369 /* Re-allocating ga_data means that an ht_array pointing to
22370 * ht_smallarray becomes invalid. We can recognize this: ht_mask is
Bram Moolenaar33570922005-01-25 22:26:29 +000022371 * at its init value. Also reset "v_dict", it's always the same. */
Bram Moolenaara7043832005-01-21 11:56:39 +000022372 for (i = 1; i <= ga_scripts.ga_len; ++i)
22373 {
22374 ht = &SCRIPT_VARS(i);
22375 if (ht->ht_mask == HT_INIT_SIZE - 1)
22376 ht->ht_array = ht->ht_smallarray;
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022377 sv = SCRIPT_SV(i);
Bram Moolenaar33570922005-01-25 22:26:29 +000022378 sv->sv_var.di_tv.vval.v_dict = &sv->sv_dict;
Bram Moolenaara7043832005-01-21 11:56:39 +000022379 }
22380
Bram Moolenaar071d4272004-06-13 20:20:40 +000022381 while (ga_scripts.ga_len < id)
22382 {
Bram Moolenaar2c704a72010-06-03 21:17:25 +020022383 sv = SCRIPT_SV(ga_scripts.ga_len + 1) =
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020022384 (scriptvar_T *)alloc_clear(sizeof(scriptvar_T));
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022385 init_var_dict(&sv->sv_dict, &sv->sv_var, VAR_SCOPE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022386 ++ga_scripts.ga_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022387 }
22388 }
22389}
22390
22391/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022392 * Initialize dictionary "dict" as a scope and set variable "dict_var" to
22393 * point to it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022394 */
22395 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022396init_var_dict(dict_T *dict, dictitem_T *dict_var, int scope)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022397{
Bram Moolenaar33570922005-01-25 22:26:29 +000022398 hash_init(&dict->dv_hashtab);
Bram Moolenaared465602012-06-20 14:13:06 +020022399 dict->dv_lock = 0;
Bram Moolenaarbdb62052012-07-16 17:31:53 +020022400 dict->dv_scope = scope;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022401 dict->dv_refcount = DO_NOT_FREE_CNT;
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000022402 dict->dv_copyID = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000022403 dict_var->di_tv.vval.v_dict = dict;
22404 dict_var->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022405 dict_var->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000022406 dict_var->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
22407 dict_var->di_key[0] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022408}
22409
22410/*
Bram Moolenaar429fa852013-04-15 12:27:36 +020022411 * Unreference a dictionary initialized by init_var_dict().
22412 */
22413 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022414unref_var_dict(dict_T *dict)
Bram Moolenaar429fa852013-04-15 12:27:36 +020022415{
22416 /* Now the dict needs to be freed if no one else is using it, go back to
22417 * normal reference counting. */
22418 dict->dv_refcount -= DO_NOT_FREE_CNT - 1;
22419 dict_unref(dict);
22420}
22421
22422/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022423 * Clean up a list of internal variables.
Bram Moolenaar33570922005-01-25 22:26:29 +000022424 * Frees all allocated variables and the value they contain.
22425 * Clears hashtab "ht", does not free it.
Bram Moolenaar071d4272004-06-13 20:20:40 +000022426 */
22427 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022428vars_clear(hashtab_T *ht)
Bram Moolenaar33570922005-01-25 22:26:29 +000022429{
22430 vars_clear_ext(ht, TRUE);
22431}
22432
22433/*
22434 * Like vars_clear(), but only free the value if "free_val" is TRUE.
22435 */
22436 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022437vars_clear_ext(hashtab_T *ht, int free_val)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022438{
Bram Moolenaara7043832005-01-21 11:56:39 +000022439 int todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000022440 hashitem_T *hi;
22441 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022442
Bram Moolenaar33570922005-01-25 22:26:29 +000022443 hash_lock(ht);
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000022444 todo = (int)ht->ht_used;
Bram Moolenaara7043832005-01-21 11:56:39 +000022445 for (hi = ht->ht_array; todo > 0; ++hi)
22446 {
22447 if (!HASHITEM_EMPTY(hi))
22448 {
22449 --todo;
22450
Bram Moolenaar33570922005-01-25 22:26:29 +000022451 /* Free the variable. Don't remove it from the hashtab,
Bram Moolenaara7043832005-01-21 11:56:39 +000022452 * ht_array might change then. hash_clear() takes care of it
22453 * later. */
Bram Moolenaar33570922005-01-25 22:26:29 +000022454 v = HI2DI(hi);
22455 if (free_val)
22456 clear_tv(&v->di_tv);
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022457 if (v->di_flags & DI_FLAGS_ALLOC)
Bram Moolenaar33570922005-01-25 22:26:29 +000022458 vim_free(v);
Bram Moolenaara7043832005-01-21 11:56:39 +000022459 }
22460 }
22461 hash_clear(ht);
Bram Moolenaar9a50b1b2005-06-27 22:48:21 +000022462 ht->ht_used = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022463}
22464
Bram Moolenaara7043832005-01-21 11:56:39 +000022465/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022466 * Delete a variable from hashtab "ht" at item "hi".
22467 * Clear the variable value and free the dictitem.
Bram Moolenaara7043832005-01-21 11:56:39 +000022468 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022469 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022470delete_var(hashtab_T *ht, hashitem_T *hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022471{
Bram Moolenaar33570922005-01-25 22:26:29 +000022472 dictitem_T *di = HI2DI(hi);
Bram Moolenaara7043832005-01-21 11:56:39 +000022473
22474 hash_remove(ht, hi);
Bram Moolenaar33570922005-01-25 22:26:29 +000022475 clear_tv(&di->di_tv);
22476 vim_free(di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022477}
22478
22479/*
22480 * List the value of one internal variable.
22481 */
22482 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022483list_one_var(dictitem_T *v, char_u *prefix, int *first)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022484{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022485 char_u *tofree;
22486 char_u *s;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022487 char_u numbuf[NUMBUFLEN];
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022488
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022489 s = echo_string(&v->di_tv, &tofree, numbuf, get_copyID());
Bram Moolenaar33570922005-01-25 22:26:29 +000022490 list_one_var_a(prefix, v->di_key, v->di_tv.v_type,
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022491 s == NULL ? (char_u *)"" : s, first);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022492 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022493}
22494
Bram Moolenaar071d4272004-06-13 20:20:40 +000022495 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022496list_one_var_a(
22497 char_u *prefix,
22498 char_u *name,
22499 int type,
22500 char_u *string,
22501 int *first) /* when TRUE clear rest of screen and set to FALSE */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022502{
Bram Moolenaar31859182007-08-14 20:41:13 +000022503 /* don't use msg() or msg_attr() to avoid overwriting "v:statusmsg" */
22504 msg_start();
22505 msg_puts(prefix);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022506 if (name != NULL) /* "a:" vars don't have a name stored */
22507 msg_puts(name);
22508 msg_putchar(' ');
22509 msg_advance(22);
22510 if (type == VAR_NUMBER)
22511 msg_putchar('#');
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022512 else if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022513 msg_putchar('*');
22514 else if (type == VAR_LIST)
22515 {
22516 msg_putchar('[');
22517 if (*string == '[')
22518 ++string;
22519 }
Bram Moolenaar8c711452005-01-14 21:53:12 +000022520 else if (type == VAR_DICT)
22521 {
22522 msg_putchar('{');
22523 if (*string == '{')
22524 ++string;
22525 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022526 else
22527 msg_putchar(' ');
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022528
Bram Moolenaar071d4272004-06-13 20:20:40 +000022529 msg_outtrans(string);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022530
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022531 if (type == VAR_FUNC || type == VAR_PARTIAL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022532 msg_puts((char_u *)"()");
Bram Moolenaar7d61a922007-08-30 09:12:23 +000022533 if (*first)
22534 {
22535 msg_clr_eos();
22536 *first = FALSE;
22537 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022538}
22539
22540/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022541 * Set variable "name" to value in "tv".
Bram Moolenaar071d4272004-06-13 20:20:40 +000022542 * If the variable already exists, the value is updated.
22543 * Otherwise the variable is created.
22544 */
22545 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022546set_var(
22547 char_u *name,
22548 typval_T *tv,
22549 int copy) /* make copy of value in "tv" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000022550{
Bram Moolenaar33570922005-01-25 22:26:29 +000022551 dictitem_T *v;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022552 char_u *varname;
Bram Moolenaar33570922005-01-25 22:26:29 +000022553 hashtab_T *ht;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022554
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022555 ht = find_var_ht(name, &varname);
22556 if (ht == NULL || *varname == NUL)
22557 {
22558 EMSG2(_(e_illvar), name);
22559 return;
22560 }
Bram Moolenaar332ac062013-04-15 13:06:21 +020022561 v = find_var_in_ht(ht, 0, varname, TRUE);
Bram Moolenaarbaff0fe2010-03-17 19:53:49 +010022562
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022563 if ((tv->v_type == VAR_FUNC || tv->v_type == VAR_PARTIAL)
22564 && var_check_func_name(name, v == NULL))
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022565 return;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022566
Bram Moolenaar33570922005-01-25 22:26:29 +000022567 if (v != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022568 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022569 /* existing variable, need to clear the value */
Bram Moolenaar77354e72015-04-21 16:49:05 +020022570 if (var_check_ro(v->di_flags, name, FALSE)
22571 || tv_check_lock(v->di_tv.v_lock, name, FALSE))
Bram Moolenaar33570922005-01-25 22:26:29 +000022572 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022573
22574 /*
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022575 * Handle setting internal v: variables separately where needed to
22576 * prevent changing the type.
Bram Moolenaar33570922005-01-25 22:26:29 +000022577 */
22578 if (ht == &vimvarht)
22579 {
22580 if (v->di_tv.v_type == VAR_STRING)
22581 {
22582 vim_free(v->di_tv.vval.v_string);
22583 if (copy || tv->v_type != VAR_STRING)
22584 v->di_tv.vval.v_string = vim_strsave(get_tv_string(tv));
22585 else
22586 {
22587 /* Take over the string to avoid an extra alloc/free. */
22588 v->di_tv.vval.v_string = tv->vval.v_string;
22589 tv->vval.v_string = NULL;
22590 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022591 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022592 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022593 else if (v->di_tv.v_type == VAR_NUMBER)
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022594 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022595 v->di_tv.vval.v_number = get_tv_number(tv);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022596 if (STRCMP(varname, "searchforward") == 0)
22597 set_search_direction(v->di_tv.vval.v_number ? '/' : '?');
Bram Moolenaar8050efa2013-11-08 04:30:20 +010022598#ifdef FEAT_SEARCH_EXTRA
22599 else if (STRCMP(varname, "hlsearch") == 0)
22600 {
22601 no_hlsearch = !v->di_tv.vval.v_number;
22602 redraw_all_later(SOME_VALID);
22603 }
22604#endif
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022605 return;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022606 }
Bram Moolenaar1cd5e612015-05-04 11:10:27 +020022607 else if (v->di_tv.v_type != tv->v_type)
22608 EMSG2(_(e_intern2), "set_var()");
Bram Moolenaar33570922005-01-25 22:26:29 +000022609 }
22610
22611 clear_tv(&v->di_tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022612 }
22613 else /* add a new variable */
22614 {
Bram Moolenaar5fcc3fe2006-06-22 15:35:14 +000022615 /* Can't add "v:" variable. */
22616 if (ht == &vimvarht)
22617 {
22618 EMSG2(_(e_illvar), name);
22619 return;
22620 }
22621
Bram Moolenaar92124a32005-06-17 22:03:40 +000022622 /* Make sure the variable name is valid. */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022623 if (!valid_varname(varname))
22624 return;
Bram Moolenaar92124a32005-06-17 22:03:40 +000022625
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022626 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
22627 + STRLEN(varname)));
Bram Moolenaara7043832005-01-21 11:56:39 +000022628 if (v == NULL)
22629 return;
Bram Moolenaar33570922005-01-25 22:26:29 +000022630 STRCPY(v->di_key, varname);
Bram Moolenaar33570922005-01-25 22:26:29 +000022631 if (hash_add(ht, DI2HIKEY(v)) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022632 {
Bram Moolenaara7043832005-01-21 11:56:39 +000022633 vim_free(v);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022634 return;
22635 }
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020022636 v->di_flags = DI_FLAGS_ALLOC;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022637 }
Bram Moolenaara7043832005-01-21 11:56:39 +000022638
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022639 if (copy || tv->v_type == VAR_NUMBER || tv->v_type == VAR_FLOAT)
Bram Moolenaar33570922005-01-25 22:26:29 +000022640 copy_tv(tv, &v->di_tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022641 else
22642 {
Bram Moolenaar33570922005-01-25 22:26:29 +000022643 v->di_tv = *tv;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022644 v->di_tv.v_lock = 0;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022645 init_tv(tv);
Bram Moolenaar1c2fda22005-01-02 11:43:19 +000022646 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022647}
22648
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022649/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022650 * Return TRUE if di_flags "flags" indicates variable "name" is read-only.
Bram Moolenaar33570922005-01-25 22:26:29 +000022651 * Also give an error message.
22652 */
22653 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022654var_check_ro(int flags, char_u *name, int use_gettext)
Bram Moolenaar33570922005-01-25 22:26:29 +000022655{
22656 if (flags & DI_FLAGS_RO)
22657 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022658 EMSG2(_(e_readonlyvar), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022659 return TRUE;
22660 }
22661 if ((flags & DI_FLAGS_RO_SBX) && sandbox)
22662 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022663 EMSG2(_(e_readonlysbx), use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar33570922005-01-25 22:26:29 +000022664 return TRUE;
22665 }
22666 return FALSE;
22667}
22668
22669/*
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022670 * Return TRUE if di_flags "flags" indicates variable "name" is fixed.
22671 * Also give an error message.
22672 */
22673 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022674var_check_fixed(int flags, char_u *name, int use_gettext)
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022675{
22676 if (flags & DI_FLAGS_FIX)
22677 {
Bram Moolenaar77354e72015-04-21 16:49:05 +020022678 EMSG2(_("E795: Cannot delete variable %s"),
22679 use_gettext ? (char_u *)_(name) : name);
Bram Moolenaar4e957af2006-09-02 11:41:07 +000022680 return TRUE;
22681 }
22682 return FALSE;
22683}
22684
22685/*
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022686 * Check if a funcref is assigned to a valid variable name.
22687 * Return TRUE and give an error if not.
22688 */
22689 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022690var_check_func_name(
22691 char_u *name, /* points to start of variable name */
22692 int new_var) /* TRUE when creating the variable */
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022693{
Bram Moolenaarcbc67722014-05-22 14:19:56 +020022694 /* Allow for w: b: s: and t:. */
22695 if (!(vim_strchr((char_u *)"wbst", name[0]) != NULL && name[1] == ':')
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022696 && !ASCII_ISUPPER((name[0] != NUL && name[1] == ':')
22697 ? name[2] : name[0]))
22698 {
22699 EMSG2(_("E704: Funcref variable name must start with a capital: %s"),
22700 name);
22701 return TRUE;
22702 }
22703 /* Don't allow hiding a function. When "v" is not NULL we might be
22704 * assigning another function to the same var, the type is checked
22705 * below. */
22706 if (new_var && function_exists(name))
22707 {
22708 EMSG2(_("E705: Variable name conflicts with existing function: %s"),
22709 name);
22710 return TRUE;
22711 }
22712 return FALSE;
22713}
22714
22715/*
22716 * Check if a variable name is valid.
22717 * Return FALSE and give an error if not.
22718 */
22719 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022720valid_varname(char_u *varname)
Bram Moolenaar4228bec2011-03-27 16:03:15 +020022721{
22722 char_u *p;
22723
22724 for (p = varname; *p != NUL; ++p)
22725 if (!eval_isnamec1(*p) && (p == varname || !VIM_ISDIGIT(*p))
22726 && *p != AUTOLOAD_CHAR)
22727 {
22728 EMSG2(_(e_illvar), varname);
22729 return FALSE;
22730 }
22731 return TRUE;
22732}
22733
22734/*
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022735 * Return TRUE if typeval "tv" is set to be locked (immutable).
Bram Moolenaar77354e72015-04-21 16:49:05 +020022736 * Also give an error message, using "name" or _("name") when use_gettext is
22737 * TRUE.
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022738 */
22739 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022740tv_check_lock(int lock, char_u *name, int use_gettext)
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022741{
22742 if (lock & VAR_LOCKED)
22743 {
22744 EMSG2(_("E741: Value is locked: %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022745 name == NULL ? (char_u *)_("Unknown")
22746 : use_gettext ? (char_u *)_(name)
22747 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022748 return TRUE;
22749 }
22750 if (lock & VAR_FIXED)
22751 {
22752 EMSG2(_("E742: Cannot change value of %s"),
Bram Moolenaar77354e72015-04-21 16:49:05 +020022753 name == NULL ? (char_u *)_("Unknown")
22754 : use_gettext ? (char_u *)_(name)
22755 : name);
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022756 return TRUE;
22757 }
22758 return FALSE;
22759}
22760
22761/*
Bram Moolenaar33570922005-01-25 22:26:29 +000022762 * Copy the values from typval_T "from" to typval_T "to".
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022763 * When needed allocates string or increases reference count.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022764 * Does not make a copy of a list or dict but copies the reference!
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000022765 * It is OK for "from" and "to" to point to the same item. This is used to
22766 * make a copy later.
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022767 */
Bram Moolenaar7e506b62010-01-19 15:55:06 +010022768 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022769copy_tv(typval_T *from, typval_T *to)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022770{
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022771 to->v_type = from->v_type;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022772 to->v_lock = 0;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022773 switch (from->v_type)
22774 {
22775 case VAR_NUMBER:
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022776 case VAR_SPECIAL:
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022777 to->vval.v_number = from->vval.v_number;
22778 break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022779 case VAR_FLOAT:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022780#ifdef FEAT_FLOAT
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022781 to->vval.v_float = from->vval.v_float;
22782 break;
22783#endif
Bram Moolenaar835dc632016-02-07 14:27:38 +010022784 case VAR_JOB:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022785#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar835dc632016-02-07 14:27:38 +010022786 to->vval.v_job = from->vval.v_job;
Bram Moolenaaree1cffc2016-02-21 19:14:41 +010022787 if (to->vval.v_job != NULL)
22788 ++to->vval.v_job->jv_refcount;
Bram Moolenaar835dc632016-02-07 14:27:38 +010022789 break;
22790#endif
Bram Moolenaar77073442016-02-13 23:23:53 +010022791 case VAR_CHANNEL:
Bram Moolenaar509ce2a2016-03-11 22:52:15 +010022792#ifdef FEAT_JOB_CHANNEL
Bram Moolenaar77073442016-02-13 23:23:53 +010022793 to->vval.v_channel = from->vval.v_channel;
Bram Moolenaar5cefd402016-02-16 12:44:26 +010022794 if (to->vval.v_channel != NULL)
22795 ++to->vval.v_channel->ch_refcount;
Bram Moolenaar77073442016-02-13 23:23:53 +010022796 break;
22797#endif
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022798 case VAR_STRING:
22799 case VAR_FUNC:
22800 if (from->vval.v_string == NULL)
22801 to->vval.v_string = NULL;
22802 else
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022803 {
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022804 to->vval.v_string = vim_strsave(from->vval.v_string);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000022805 if (from->v_type == VAR_FUNC)
22806 func_ref(to->vval.v_string);
22807 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022808 break;
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022809 case VAR_PARTIAL:
22810 if (from->vval.v_partial == NULL)
22811 to->vval.v_partial = NULL;
22812 else
22813 {
22814 to->vval.v_partial = from->vval.v_partial;
22815 ++to->vval.v_partial->pt_refcount;
22816 }
22817 break;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022818 case VAR_LIST:
22819 if (from->vval.v_list == NULL)
22820 to->vval.v_list = NULL;
22821 else
22822 {
22823 to->vval.v_list = from->vval.v_list;
22824 ++to->vval.v_list->lv_refcount;
22825 }
22826 break;
Bram Moolenaar8c711452005-01-14 21:53:12 +000022827 case VAR_DICT:
22828 if (from->vval.v_dict == NULL)
22829 to->vval.v_dict = NULL;
22830 else
22831 {
22832 to->vval.v_dict = from->vval.v_dict;
22833 ++to->vval.v_dict->dv_refcount;
22834 }
22835 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022836 case VAR_UNKNOWN:
22837 EMSG2(_(e_intern2), "copy_tv(UNKNOWN)");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022838 break;
22839 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022840}
22841
22842/*
Bram Moolenaare9a41262005-01-15 22:18:47 +000022843 * Make a copy of an item.
22844 * Lists and Dictionaries are also copied. A deep copy if "deep" is set.
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022845 * For deepcopy() "copyID" is zero for a full copy or the ID for when a
22846 * reference to an already copied list/dict can be used.
22847 * Returns FAIL or OK.
Bram Moolenaare9a41262005-01-15 22:18:47 +000022848 */
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022849 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010022850item_copy(
22851 typval_T *from,
22852 typval_T *to,
22853 int deep,
22854 int copyID)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022855{
22856 static int recurse = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022857 int ret = OK;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022858
Bram Moolenaar33570922005-01-25 22:26:29 +000022859 if (recurse >= DICT_MAXNEST)
Bram Moolenaare9a41262005-01-15 22:18:47 +000022860 {
22861 EMSG(_("E698: variable nested too deep for making a copy"));
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022862 return FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022863 }
22864 ++recurse;
22865
22866 switch (from->v_type)
22867 {
22868 case VAR_NUMBER:
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022869 case VAR_FLOAT:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022870 case VAR_STRING:
22871 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010022872 case VAR_PARTIAL:
Bram Moolenaar15550002016-01-31 18:45:24 +010022873 case VAR_SPECIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010022874 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010022875 case VAR_CHANNEL:
Bram Moolenaare9a41262005-01-15 22:18:47 +000022876 copy_tv(from, to);
22877 break;
22878 case VAR_LIST:
22879 to->v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022880 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022881 if (from->vval.v_list == NULL)
22882 to->vval.v_list = NULL;
22883 else if (copyID != 0 && from->vval.v_list->lv_copyID == copyID)
22884 {
22885 /* use the copy made earlier */
22886 to->vval.v_list = from->vval.v_list->lv_copylist;
22887 ++to->vval.v_list->lv_refcount;
22888 }
22889 else
22890 to->vval.v_list = list_copy(from->vval.v_list, deep, copyID);
22891 if (to->vval.v_list == NULL)
22892 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022893 break;
22894 case VAR_DICT:
22895 to->v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000022896 to->v_lock = 0;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022897 if (from->vval.v_dict == NULL)
22898 to->vval.v_dict = NULL;
22899 else if (copyID != 0 && from->vval.v_dict->dv_copyID == copyID)
22900 {
22901 /* use the copy made earlier */
22902 to->vval.v_dict = from->vval.v_dict->dv_copydict;
22903 ++to->vval.v_dict->dv_refcount;
22904 }
22905 else
22906 to->vval.v_dict = dict_copy(from->vval.v_dict, deep, copyID);
22907 if (to->vval.v_dict == NULL)
22908 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022909 break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010022910 case VAR_UNKNOWN:
22911 EMSG2(_(e_intern2), "item_copy(UNKNOWN)");
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022912 ret = FAIL;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022913 }
22914 --recurse;
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022915 return ret;
Bram Moolenaare9a41262005-01-15 22:18:47 +000022916}
22917
22918/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000022919 * ":echo expr1 ..." print each argument separated with a space, add a
22920 * newline at the end.
22921 * ":echon expr1 ..." print each argument plain.
22922 */
22923 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010022924ex_echo(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022925{
22926 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000022927 typval_T rettv;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000022928 char_u *tofree;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022929 char_u *p;
22930 int needclr = TRUE;
22931 int atstart = TRUE;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000022932 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000022933
22934 if (eap->skip)
22935 ++emsg_skip;
22936 while (*arg != NUL && *arg != '|' && *arg != '\n' && !got_int)
22937 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022938 /* If eval1() causes an error message the text from the command may
22939 * still need to be cleared. E.g., "echo 22,44". */
22940 need_clr_eos = needclr;
22941
Bram Moolenaar071d4272004-06-13 20:20:40 +000022942 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000022943 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022944 {
22945 /*
22946 * Report the invalid expression unless the expression evaluation
22947 * has been cancelled due to an aborting error, an interrupt, or an
22948 * exception.
22949 */
22950 if (!aborting())
22951 EMSG2(_(e_invexpr2), p);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022952 need_clr_eos = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000022953 break;
22954 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000022955 need_clr_eos = FALSE;
22956
Bram Moolenaar071d4272004-06-13 20:20:40 +000022957 if (!eap->skip)
22958 {
22959 if (atstart)
22960 {
22961 atstart = FALSE;
22962 /* Call msg_start() after eval1(), evaluating the expression
22963 * may cause a message to appear. */
22964 if (eap->cmdidx == CMD_echo)
Bram Moolenaar12b02902012-03-23 15:18:24 +010022965 {
Bram Moolenaar6df5e5a2012-03-28 16:49:29 +020022966 /* Mark the saved text as finishing the line, so that what
22967 * follows is displayed on a new line when scrolling back
22968 * at the more prompt. */
22969 msg_sb_eol();
Bram Moolenaar071d4272004-06-13 20:20:40 +000022970 msg_start();
Bram Moolenaar12b02902012-03-23 15:18:24 +010022971 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000022972 }
22973 else if (eap->cmdidx == CMD_echo)
22974 msg_puts_attr((char_u *)" ", echo_attr);
Bram Moolenaar520e1e42016-01-23 19:46:28 +010022975 p = echo_string(&rettv, &tofree, numbuf, get_copyID());
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022976 if (p != NULL)
22977 for ( ; *p != NUL && !got_int; ++p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022978 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022979 if (*p == '\n' || *p == '\r' || *p == TAB)
Bram Moolenaar071d4272004-06-13 20:20:40 +000022980 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022981 if (*p != TAB && needclr)
22982 {
22983 /* remove any text still there from the command */
22984 msg_clr_eos();
22985 needclr = FALSE;
22986 }
22987 msg_putchar_attr(*p, echo_attr);
Bram Moolenaar071d4272004-06-13 20:20:40 +000022988 }
22989 else
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022990 {
22991#ifdef FEAT_MBYTE
22992 if (has_mbyte)
22993 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +000022994 int i = (*mb_ptr2len)(p);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000022995
22996 (void)msg_outtrans_len_attr(p, i, echo_attr);
22997 p += i - 1;
22998 }
22999 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000023000#endif
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023001 (void)msg_outtrans_len_attr(p, 1, echo_attr);
23002 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023003 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023004 vim_free(tofree);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023005 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023006 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023007 arg = skipwhite(arg);
23008 }
23009 eap->nextcmd = check_nextcmd(arg);
23010
23011 if (eap->skip)
23012 --emsg_skip;
23013 else
23014 {
23015 /* remove text that may still be there from the command */
23016 if (needclr)
23017 msg_clr_eos();
23018 if (eap->cmdidx == CMD_echo)
23019 msg_end();
23020 }
23021}
23022
23023/*
23024 * ":echohl {name}".
23025 */
23026 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023027ex_echohl(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023028{
23029 int id;
23030
23031 id = syn_name2id(eap->arg);
23032 if (id == 0)
23033 echo_attr = 0;
23034 else
23035 echo_attr = syn_id2attr(id);
23036}
23037
23038/*
23039 * ":execute expr1 ..." execute the result of an expression.
23040 * ":echomsg expr1 ..." Print a message
23041 * ":echoerr expr1 ..." Print an error
23042 * Each gets spaces around each argument and a newline at the end for
23043 * echo commands
23044 */
23045 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023046ex_execute(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023047{
23048 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000023049 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023050 int ret = OK;
23051 char_u *p;
23052 garray_T ga;
23053 int len;
23054 int save_did_emsg;
23055
23056 ga_init2(&ga, 1, 80);
23057
23058 if (eap->skip)
23059 ++emsg_skip;
23060 while (*arg != NUL && *arg != '|' && *arg != '\n')
23061 {
23062 p = arg;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023063 if (eval1(&arg, &rettv, !eap->skip) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023064 {
23065 /*
23066 * Report the invalid expression unless the expression evaluation
23067 * has been cancelled due to an aborting error, an interrupt, or an
23068 * exception.
23069 */
23070 if (!aborting())
23071 EMSG2(_(e_invexpr2), p);
23072 ret = FAIL;
23073 break;
23074 }
23075
23076 if (!eap->skip)
23077 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023078 p = get_tv_string(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023079 len = (int)STRLEN(p);
23080 if (ga_grow(&ga, len + 2) == FAIL)
23081 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023082 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023083 ret = FAIL;
23084 break;
23085 }
23086 if (ga.ga_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023087 ((char_u *)(ga.ga_data))[ga.ga_len++] = ' ';
Bram Moolenaar071d4272004-06-13 20:20:40 +000023088 STRCPY((char_u *)(ga.ga_data) + ga.ga_len, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023089 ga.ga_len += len;
23090 }
23091
Bram Moolenaarc70646c2005-01-04 21:52:38 +000023092 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023093 arg = skipwhite(arg);
23094 }
23095
23096 if (ret != FAIL && ga.ga_data != NULL)
23097 {
23098 if (eap->cmdidx == CMD_echomsg)
Bram Moolenaar4770d092006-01-12 23:22:24 +000023099 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000023100 MSG_ATTR(ga.ga_data, echo_attr);
Bram Moolenaar4770d092006-01-12 23:22:24 +000023101 out_flush();
23102 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023103 else if (eap->cmdidx == CMD_echoerr)
23104 {
23105 /* We don't want to abort following commands, restore did_emsg. */
23106 save_did_emsg = did_emsg;
23107 EMSG((char_u *)ga.ga_data);
23108 if (!force_abort)
23109 did_emsg = save_did_emsg;
23110 }
23111 else if (eap->cmdidx == CMD_execute)
23112 do_cmdline((char_u *)ga.ga_data,
23113 eap->getline, eap->cookie, DOCMD_NOWAIT|DOCMD_VERBOSE);
23114 }
23115
23116 ga_clear(&ga);
23117
23118 if (eap->skip)
23119 --emsg_skip;
23120
23121 eap->nextcmd = check_nextcmd(arg);
23122}
23123
23124/*
23125 * Skip over the name of an option: "&option", "&g:option" or "&l:option".
23126 * "arg" points to the "&" or '+' when called, to "option" when returning.
23127 * Returns NULL when no option name found. Otherwise pointer to the char
23128 * after the option name.
23129 */
23130 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023131find_option_end(char_u **arg, int *opt_flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023132{
23133 char_u *p = *arg;
23134
23135 ++p;
23136 if (*p == 'g' && p[1] == ':')
23137 {
23138 *opt_flags = OPT_GLOBAL;
23139 p += 2;
23140 }
23141 else if (*p == 'l' && p[1] == ':')
23142 {
23143 *opt_flags = OPT_LOCAL;
23144 p += 2;
23145 }
23146 else
23147 *opt_flags = 0;
23148
23149 if (!ASCII_ISALPHA(*p))
23150 return NULL;
23151 *arg = p;
23152
23153 if (p[0] == 't' && p[1] == '_' && p[2] != NUL && p[3] != NUL)
23154 p += 4; /* termcap option */
23155 else
23156 while (ASCII_ISALPHA(*p))
23157 ++p;
23158 return p;
23159}
23160
23161/*
23162 * ":function"
23163 */
23164 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010023165ex_function(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023166{
23167 char_u *theline;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023168 int i;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023169 int j;
23170 int c;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023171 int saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023172 int saved_wait_return = need_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023173 char_u *name = NULL;
23174 char_u *p;
23175 char_u *arg;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023176 char_u *line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023177 garray_T newargs;
23178 garray_T newlines;
23179 int varargs = FALSE;
23180 int mustend = FALSE;
23181 int flags = 0;
23182 ufunc_T *fp;
23183 int indent;
23184 int nesting;
23185 char_u *skip_until = NULL;
Bram Moolenaar33570922005-01-25 22:26:29 +000023186 dictitem_T *v;
23187 funcdict_T fudi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023188 static int func_nr = 0; /* number for nameless function */
23189 int paren;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023190 hashtab_T *ht;
23191 int todo;
23192 hashitem_T *hi;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023193 int sourcing_lnum_off;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023194
23195 /*
23196 * ":function" without argument: list functions.
23197 */
23198 if (ends_excmd(*eap->arg))
23199 {
23200 if (!eap->skip)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023201 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023202 todo = (int)func_hashtab.ht_used;
Bram Moolenaar038eb0e2005-02-27 22:43:26 +000023203 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023204 {
23205 if (!HASHITEM_EMPTY(hi))
23206 {
23207 --todo;
23208 fp = HI2UF(hi);
23209 if (!isdigit(*fp->uf_name))
23210 list_func_head(fp, FALSE);
23211 }
23212 }
23213 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023214 eap->nextcmd = check_nextcmd(eap->arg);
23215 return;
23216 }
23217
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023218 /*
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023219 * ":function /pat": list functions matching pattern.
23220 */
23221 if (*eap->arg == '/')
23222 {
23223 p = skip_regexp(eap->arg + 1, '/', TRUE, NULL);
23224 if (!eap->skip)
23225 {
23226 regmatch_T regmatch;
23227
23228 c = *p;
23229 *p = NUL;
23230 regmatch.regprog = vim_regcomp(eap->arg + 1, RE_MAGIC);
23231 *p = c;
23232 if (regmatch.regprog != NULL)
23233 {
23234 regmatch.rm_ic = p_ic;
23235
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023236 todo = (int)func_hashtab.ht_used;
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023237 for (hi = func_hashtab.ht_array; todo > 0 && !got_int; ++hi)
23238 {
23239 if (!HASHITEM_EMPTY(hi))
23240 {
23241 --todo;
23242 fp = HI2UF(hi);
23243 if (!isdigit(*fp->uf_name)
23244 && vim_regexec(&regmatch, fp->uf_name, 0))
23245 list_func_head(fp, FALSE);
23246 }
23247 }
Bram Moolenaar473de612013-06-08 18:19:48 +020023248 vim_regfree(regmatch.regprog);
Bram Moolenaardd2436f2005-09-05 22:14:46 +000023249 }
23250 }
23251 if (*p == '/')
23252 ++p;
23253 eap->nextcmd = check_nextcmd(p);
23254 return;
23255 }
23256
23257 /*
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023258 * Get the function name. There are these situations:
23259 * func normal function name
23260 * "name" == func, "fudi.fd_dict" == NULL
23261 * dict.func new dictionary entry
23262 * "name" == NULL, "fudi.fd_dict" set,
23263 * "fudi.fd_di" == NULL, "fudi.fd_newkey" == func
23264 * dict.func existing dict entry with a Funcref
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023265 * "name" == func, "fudi.fd_dict" set,
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023266 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
23267 * dict.func existing dict entry that's not a Funcref
23268 * "name" == NULL, "fudi.fd_dict" set,
23269 * "fudi.fd_di" set, "fudi.fd_newkey" == NULL
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023270 * s:func script-local function name
23271 * g:func global function name, same as "func"
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023272 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023273 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010023274 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023275 paren = (vim_strchr(p, '(') != NULL);
23276 if (name == NULL && (fudi.fd_dict == NULL || !paren) && !eap->skip)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023277 {
23278 /*
23279 * Return on an invalid expression in braces, unless the expression
23280 * evaluation has been cancelled due to an aborting error, an
23281 * interrupt, or an exception.
23282 */
23283 if (!aborting())
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023284 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023285 if (!eap->skip && fudi.fd_newkey != NULL)
23286 EMSG2(_(e_dictkey), fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023287 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023288 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023289 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023290 else
23291 eap->skip = TRUE;
23292 }
Bram Moolenaar1f35bf92006-03-07 22:38:47 +000023293
Bram Moolenaar071d4272004-06-13 20:20:40 +000023294 /* An error in a function call during evaluation of an expression in magic
23295 * braces should not cause the function not to be defined. */
23296 saved_did_emsg = did_emsg;
23297 did_emsg = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023298
23299 /*
23300 * ":function func" with only function name: list function.
23301 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023302 if (!paren)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023303 {
23304 if (!ends_excmd(*skipwhite(p)))
23305 {
23306 EMSG(_(e_trailing));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023307 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023308 }
23309 eap->nextcmd = check_nextcmd(p);
23310 if (eap->nextcmd != NULL)
23311 *p = NUL;
23312 if (!eap->skip && !got_int)
23313 {
23314 fp = find_func(name);
23315 if (fp != NULL)
23316 {
23317 list_func_head(fp, TRUE);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023318 for (j = 0; j < fp->uf_lines.ga_len && !got_int; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023319 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023320 if (FUNCLINE(fp, j) == NULL)
23321 continue;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023322 msg_putchar('\n');
23323 msg_outnum((long)(j + 1));
23324 if (j < 9)
23325 msg_putchar(' ');
23326 if (j < 99)
23327 msg_putchar(' ');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023328 msg_prt_line(FUNCLINE(fp, j), FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023329 out_flush(); /* show a line at a time */
23330 ui_breakcheck();
23331 }
23332 if (!got_int)
23333 {
23334 msg_putchar('\n');
23335 msg_puts((char_u *)" endfunction");
23336 }
23337 }
23338 else
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023339 emsg_funcname(N_("E123: Undefined function: %s"), name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023340 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023341 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023342 }
23343
23344 /*
23345 * ":function name(arg1, arg2)" Define function.
23346 */
23347 p = skipwhite(p);
23348 if (*p != '(')
23349 {
23350 if (!eap->skip)
23351 {
23352 EMSG2(_("E124: Missing '(': %s"), eap->arg);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023353 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023354 }
23355 /* attempt to continue by skipping some text */
23356 if (vim_strchr(p, '(') != NULL)
23357 p = vim_strchr(p, '(');
23358 }
23359 p = skipwhite(p + 1);
23360
23361 ga_init2(&newargs, (int)sizeof(char_u *), 3);
23362 ga_init2(&newlines, (int)sizeof(char_u *), 3);
23363
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023364 if (!eap->skip)
23365 {
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023366 /* Check the name of the function. Unless it's a dictionary function
23367 * (that we are overwriting). */
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023368 if (name != NULL)
23369 arg = name;
23370 else
23371 arg = fudi.fd_newkey;
Bram Moolenaarb42dc232006-11-21 18:36:05 +000023372 if (arg != NULL && (fudi.fd_di == NULL
23373 || fudi.fd_di->di_tv.v_type != VAR_FUNC))
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023374 {
23375 if (*arg == K_SPECIAL)
23376 j = 3;
23377 else
23378 j = 0;
23379 while (arg[j] != NUL && (j == 0 ? eval_isnamec1(arg[j])
23380 : eval_isnamec(arg[j])))
23381 ++j;
23382 if (arg[j] != NUL)
Bram Moolenaarb67cc162009-02-04 15:27:06 +000023383 emsg_funcname((char *)e_invarg2, arg);
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023384 }
Bram Moolenaar2142e5d2013-02-20 15:19:43 +010023385 /* Disallow using the g: dict. */
23386 if (fudi.fd_dict != NULL && fudi.fd_dict->dv_scope == VAR_DEF_SCOPE)
23387 EMSG(_("E862: Cannot use g: here"));
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023388 }
23389
Bram Moolenaar071d4272004-06-13 20:20:40 +000023390 /*
23391 * Isolate the arguments: "arg1, arg2, ...)"
23392 */
23393 while (*p != ')')
23394 {
23395 if (p[0] == '.' && p[1] == '.' && p[2] == '.')
23396 {
23397 varargs = TRUE;
23398 p += 3;
23399 mustend = TRUE;
23400 }
23401 else
23402 {
23403 arg = p;
23404 while (ASCII_ISALNUM(*p) || *p == '_')
23405 ++p;
23406 if (arg == p || isdigit(*arg)
23407 || (p - arg == 9 && STRNCMP(arg, "firstline", 9) == 0)
23408 || (p - arg == 8 && STRNCMP(arg, "lastline", 8) == 0))
23409 {
23410 if (!eap->skip)
23411 EMSG2(_("E125: Illegal argument: %s"), arg);
23412 break;
23413 }
23414 if (ga_grow(&newargs, 1) == FAIL)
23415 goto erret;
23416 c = *p;
23417 *p = NUL;
23418 arg = vim_strsave(arg);
23419 if (arg == NULL)
23420 goto erret;
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023421
23422 /* Check for duplicate argument name. */
23423 for (i = 0; i < newargs.ga_len; ++i)
23424 if (STRCMP(((char_u **)(newargs.ga_data))[i], arg) == 0)
23425 {
23426 EMSG2(_("E853: Duplicate argument name: %s"), arg);
Bram Moolenaar47b83422014-02-24 03:32:00 +010023427 vim_free(arg);
Bram Moolenaaracd6a042011-09-30 16:39:48 +020023428 goto erret;
23429 }
23430
Bram Moolenaar071d4272004-06-13 20:20:40 +000023431 ((char_u **)(newargs.ga_data))[newargs.ga_len] = arg;
23432 *p = c;
23433 newargs.ga_len++;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023434 if (*p == ',')
23435 ++p;
23436 else
23437 mustend = TRUE;
23438 }
23439 p = skipwhite(p);
23440 if (mustend && *p != ')')
23441 {
23442 if (!eap->skip)
23443 EMSG2(_(e_invarg2), eap->arg);
23444 break;
23445 }
23446 }
Bram Moolenaardd8a5282015-08-11 15:54:52 +020023447 if (*p != ')')
23448 goto erret;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023449 ++p; /* skip the ')' */
23450
Bram Moolenaare9a41262005-01-15 22:18:47 +000023451 /* find extra arguments "range", "dict" and "abort" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023452 for (;;)
23453 {
23454 p = skipwhite(p);
23455 if (STRNCMP(p, "range", 5) == 0)
23456 {
23457 flags |= FC_RANGE;
23458 p += 5;
23459 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000023460 else if (STRNCMP(p, "dict", 4) == 0)
23461 {
23462 flags |= FC_DICT;
23463 p += 4;
23464 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023465 else if (STRNCMP(p, "abort", 5) == 0)
23466 {
23467 flags |= FC_ABORT;
23468 p += 5;
23469 }
23470 else
23471 break;
23472 }
23473
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023474 /* When there is a line break use what follows for the function body.
23475 * Makes 'exe "func Test()\n...\nendfunc"' work. */
23476 if (*p == '\n')
23477 line_arg = p + 1;
23478 else if (*p != NUL && *p != '"' && !eap->skip && !did_emsg)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023479 EMSG(_(e_trailing));
23480
23481 /*
23482 * Read the body of the function, until ":endfunction" is found.
23483 */
23484 if (KeyTyped)
23485 {
23486 /* Check if the function already exists, don't let the user type the
23487 * whole function before telling him it doesn't work! For a script we
23488 * need to skip the body to be able to find what follows. */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023489 if (!eap->skip && !eap->forceit)
23490 {
23491 if (fudi.fd_dict != NULL && fudi.fd_newkey == NULL)
23492 EMSG(_(e_funcdict));
23493 else if (name != NULL && find_func(name) != NULL)
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023494 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023495 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023496
Bram Moolenaard857f0e2005-06-21 22:37:39 +000023497 if (!eap->skip && did_emsg)
23498 goto erret;
23499
Bram Moolenaar071d4272004-06-13 20:20:40 +000023500 msg_putchar('\n'); /* don't overwrite the function name */
23501 cmdline_row = msg_row;
23502 }
23503
23504 indent = 2;
23505 nesting = 0;
23506 for (;;)
23507 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023508 if (KeyTyped)
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023509 {
Bram Moolenaar52af9652011-09-14 14:33:51 +020023510 msg_scroll = TRUE;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023511 saved_wait_return = FALSE;
23512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023513 need_wait_return = FALSE;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023514 sourcing_lnum_off = sourcing_lnum;
23515
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023516 if (line_arg != NULL)
23517 {
23518 /* Use eap->arg, split up in parts by line breaks. */
23519 theline = line_arg;
23520 p = vim_strchr(theline, '\n');
23521 if (p == NULL)
23522 line_arg += STRLEN(line_arg);
23523 else
23524 {
23525 *p = NUL;
23526 line_arg = p + 1;
23527 }
23528 }
23529 else if (eap->getline == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023530 theline = getcmdline(':', 0L, indent);
23531 else
23532 theline = eap->getline(':', eap->cookie, indent);
23533 if (KeyTyped)
23534 lines_left = Rows - 1;
23535 if (theline == NULL)
23536 {
23537 EMSG(_("E126: Missing :endfunction"));
23538 goto erret;
23539 }
23540
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023541 /* Detect line continuation: sourcing_lnum increased more than one. */
23542 if (sourcing_lnum > sourcing_lnum_off + 1)
23543 sourcing_lnum_off = sourcing_lnum - sourcing_lnum_off - 1;
23544 else
23545 sourcing_lnum_off = 0;
23546
Bram Moolenaar071d4272004-06-13 20:20:40 +000023547 if (skip_until != NULL)
23548 {
23549 /* between ":append" and "." and between ":python <<EOF" and "EOF"
23550 * don't check for ":endfunc". */
23551 if (STRCMP(theline, skip_until) == 0)
23552 {
23553 vim_free(skip_until);
23554 skip_until = NULL;
23555 }
23556 }
23557 else
23558 {
23559 /* skip ':' and blanks*/
23560 for (p = theline; vim_iswhite(*p) || *p == ':'; ++p)
23561 ;
23562
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023563 /* Check for "endfunction". */
23564 if (checkforcmd(&p, "endfunction", 4) && nesting-- == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023565 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023566 if (line_arg == NULL)
23567 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023568 break;
23569 }
23570
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023571 /* Increase indent inside "if", "while", "for" and "try", decrease
Bram Moolenaar071d4272004-06-13 20:20:40 +000023572 * at "end". */
23573 if (indent > 2 && STRNCMP(p, "end", 3) == 0)
23574 indent -= 2;
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023575 else if (STRNCMP(p, "if", 2) == 0
23576 || STRNCMP(p, "wh", 2) == 0
23577 || STRNCMP(p, "for", 3) == 0
Bram Moolenaar071d4272004-06-13 20:20:40 +000023578 || STRNCMP(p, "try", 3) == 0)
23579 indent += 2;
23580
23581 /* Check for defining a function inside this function. */
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023582 if (checkforcmd(&p, "function", 2))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023583 {
Bram Moolenaar31c67ef2005-01-11 21:34:41 +000023584 if (*p == '!')
23585 p = skipwhite(p + 1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023586 p += eval_fname_script(p);
Bram Moolenaar65639032016-03-16 21:40:30 +010023587 vim_free(trans_function_name(&p, TRUE, 0, NULL, NULL));
Bram Moolenaaref923902014-12-13 21:00:55 +010023588 if (*skipwhite(p) == '(')
Bram Moolenaar071d4272004-06-13 20:20:40 +000023589 {
Bram Moolenaaref923902014-12-13 21:00:55 +010023590 ++nesting;
23591 indent += 2;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023592 }
23593 }
23594
23595 /* Check for ":append" or ":insert". */
23596 p = skip_range(p, NULL);
23597 if ((p[0] == 'a' && (!ASCII_ISALPHA(p[1]) || p[1] == 'p'))
23598 || (p[0] == 'i'
23599 && (!ASCII_ISALPHA(p[1]) || (p[1] == 'n'
23600 && (!ASCII_ISALPHA(p[2]) || (p[2] == 's'))))))
23601 skip_until = vim_strsave((char_u *)".");
23602
23603 /* Check for ":python <<EOF", ":tcl <<EOF", etc. */
23604 arg = skipwhite(skiptowhite(p));
23605 if (arg[0] == '<' && arg[1] =='<'
23606 && ((p[0] == 'p' && p[1] == 'y'
23607 && (!ASCII_ISALPHA(p[2]) || p[2] == 't'))
23608 || (p[0] == 'p' && p[1] == 'e'
23609 && (!ASCII_ISALPHA(p[2]) || p[2] == 'r'))
23610 || (p[0] == 't' && p[1] == 'c'
23611 && (!ASCII_ISALPHA(p[2]) || p[2] == 'l'))
Bram Moolenaar50bfb322011-10-26 13:19:27 +020023612 || (p[0] == 'l' && p[1] == 'u' && p[2] == 'a'
23613 && !ASCII_ISALPHA(p[3]))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023614 || (p[0] == 'r' && p[1] == 'u' && p[2] == 'b'
23615 && (!ASCII_ISALPHA(p[3]) || p[3] == 'y'))
Bram Moolenaar325b7a22004-07-05 15:58:32 +000023616 || (p[0] == 'm' && p[1] == 'z'
23617 && (!ASCII_ISALPHA(p[2]) || p[2] == 's'))
Bram Moolenaar071d4272004-06-13 20:20:40 +000023618 ))
23619 {
23620 /* ":python <<" continues until a dot, like ":append" */
23621 p = skipwhite(arg + 2);
23622 if (*p == NUL)
23623 skip_until = vim_strsave((char_u *)".");
23624 else
23625 skip_until = vim_strsave(p);
23626 }
23627 }
23628
23629 /* Add the line to the function. */
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023630 if (ga_grow(&newlines, 1 + sourcing_lnum_off) == FAIL)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023631 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023632 if (line_arg == NULL)
23633 vim_free(theline);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023634 goto erret;
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023635 }
23636
23637 /* Copy the line to newly allocated memory. get_one_sourceline()
23638 * allocates 250 bytes per line, this saves 80% on average. The cost
23639 * is an extra alloc/free. */
23640 p = vim_strsave(theline);
23641 if (p != NULL)
23642 {
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023643 if (line_arg == NULL)
23644 vim_free(theline);
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000023645 theline = p;
23646 }
23647
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000023648 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = theline;
23649
23650 /* Add NULL lines for continuation lines, so that the line count is
23651 * equal to the index in the growarray. */
23652 while (sourcing_lnum_off-- > 0)
23653 ((char_u **)(newlines.ga_data))[newlines.ga_len++] = NULL;
Bram Moolenaar997fb4b2006-02-17 21:53:23 +000023654
23655 /* Check for end of eap->arg. */
23656 if (line_arg != NULL && *line_arg == NUL)
23657 line_arg = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023658 }
23659
23660 /* Don't define the function when skipping commands or when an error was
23661 * detected. */
23662 if (eap->skip || did_emsg)
23663 goto erret;
23664
23665 /*
23666 * If there are no errors, add the function
23667 */
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023668 if (fudi.fd_dict == NULL)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023669 {
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023670 v = find_var(name, &ht, FALSE);
Bram Moolenaar33570922005-01-25 22:26:29 +000023671 if (v != NULL && v->di_tv.v_type == VAR_FUNC)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023672 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023673 emsg_funcname(N_("E707: Function name conflicts with variable: %s"),
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023674 name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023675 goto erret;
23676 }
Bram Moolenaar49cd9572005-01-03 21:06:01 +000023677
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023678 fp = find_func(name);
23679 if (fp != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023680 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023681 if (!eap->forceit)
23682 {
Bram Moolenaar81bf7082005-02-12 14:31:42 +000023683 emsg_funcname(e_funcexts, name);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023684 goto erret;
23685 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023686 if (fp->uf_calls > 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023687 {
Bram Moolenaar7b76b0a2009-01-28 18:09:38 +000023688 emsg_funcname(N_("E127: Cannot redefine function %s: It is in use"),
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023689 name);
23690 goto erret;
23691 }
23692 /* redefine existing function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023693 ga_clear_strings(&(fp->uf_args));
23694 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023695 vim_free(name);
23696 name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023697 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023698 }
23699 else
23700 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023701 char numbuf[20];
23702
23703 fp = NULL;
23704 if (fudi.fd_newkey == NULL && !eap->forceit)
23705 {
23706 EMSG(_(e_funcdict));
23707 goto erret;
23708 }
Bram Moolenaar758711c2005-02-02 23:11:38 +000023709 if (fudi.fd_di == NULL)
23710 {
23711 /* Can't add a function to a locked dictionary */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023712 if (tv_check_lock(fudi.fd_dict->dv_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023713 goto erret;
23714 }
23715 /* Can't change an existing function if it is locked */
Bram Moolenaar77354e72015-04-21 16:49:05 +020023716 else if (tv_check_lock(fudi.fd_di->di_tv.v_lock, eap->arg, FALSE))
Bram Moolenaar758711c2005-02-02 23:11:38 +000023717 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023718
23719 /* Give the function a sequential number. Can only be used with a
23720 * Funcref! */
23721 vim_free(name);
23722 sprintf(numbuf, "%d", ++func_nr);
23723 name = vim_strsave((char_u *)numbuf);
23724 if (name == NULL)
23725 goto erret;
23726 }
23727
23728 if (fp == NULL)
23729 {
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023730 if (fudi.fd_dict == NULL && vim_strchr(name, AUTOLOAD_CHAR) != NULL)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023731 {
23732 int slen, plen;
23733 char_u *scriptname;
23734
23735 /* Check that the autoload name matches the script name. */
23736 j = FAIL;
23737 if (sourcing_name != NULL)
23738 {
23739 scriptname = autoload_name(name);
23740 if (scriptname != NULL)
23741 {
23742 p = vim_strchr(scriptname, '/');
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023743 plen = (int)STRLEN(p);
23744 slen = (int)STRLEN(sourcing_name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023745 if (slen > plen && fnamecmp(p,
23746 sourcing_name + slen - plen) == 0)
23747 j = OK;
23748 vim_free(scriptname);
23749 }
23750 }
23751 if (j == FAIL)
23752 {
23753 EMSG2(_("E746: Function name does not match script file name: %s"), name);
23754 goto erret;
23755 }
23756 }
23757
23758 fp = (ufunc_T *)alloc((unsigned)(sizeof(ufunc_T) + STRLEN(name)));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023759 if (fp == NULL)
23760 goto erret;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023761
23762 if (fudi.fd_dict != NULL)
23763 {
23764 if (fudi.fd_di == NULL)
23765 {
23766 /* add new dict entry */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023767 fudi.fd_di = dictitem_alloc(fudi.fd_newkey);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023768 if (fudi.fd_di == NULL)
23769 {
23770 vim_free(fp);
23771 goto erret;
23772 }
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023773 if (dict_add(fudi.fd_dict, fudi.fd_di) == FAIL)
23774 {
23775 vim_free(fudi.fd_di);
Bram Moolenaar0a5fd8b2006-08-16 20:02:22 +000023776 vim_free(fp);
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023777 goto erret;
23778 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023779 }
23780 else
23781 /* overwrite existing dict entry */
23782 clear_tv(&fudi.fd_di->di_tv);
23783 fudi.fd_di->di_tv.v_type = VAR_FUNC;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000023784 fudi.fd_di->di_tv.v_lock = 0;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023785 fudi.fd_di->di_tv.vval.v_string = vim_strsave(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023786 fp->uf_refcount = 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +000023787
23788 /* behave like "dict" was used */
23789 flags |= FC_DICT;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023790 }
23791
Bram Moolenaar071d4272004-06-13 20:20:40 +000023792 /* insert the new function in the function list */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023793 STRCPY(fp->uf_name, name);
Bram Moolenaar0107f5b2015-12-28 22:51:20 +010023794 if (hash_add(&func_hashtab, UF2HIKEY(fp)) == FAIL)
23795 {
23796 vim_free(fp);
23797 goto erret;
23798 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000023799 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023800 fp->uf_args = newargs;
23801 fp->uf_lines = newlines;
Bram Moolenaar05159a02005-02-26 23:04:13 +000023802#ifdef FEAT_PROFILE
23803 fp->uf_tml_count = NULL;
23804 fp->uf_tml_total = NULL;
23805 fp->uf_tml_self = NULL;
23806 fp->uf_profiling = FALSE;
23807 if (prof_def_func())
23808 func_do_profile(fp);
23809#endif
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000023810 fp->uf_varargs = varargs;
23811 fp->uf_flags = flags;
23812 fp->uf_calls = 0;
23813 fp->uf_script_ID = current_SID;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023814 goto ret_free;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023815
23816erret:
Bram Moolenaar071d4272004-06-13 20:20:40 +000023817 ga_clear_strings(&newargs);
23818 ga_clear_strings(&newlines);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023819ret_free:
23820 vim_free(skip_until);
23821 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023822 vim_free(name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000023823 did_emsg |= saved_did_emsg;
Bram Moolenaarccf623f2013-07-05 18:29:48 +020023824 need_wait_return |= saved_wait_return;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023825}
23826
23827/*
23828 * Get a function name, translating "<SID>" and "<SNR>".
Bram Moolenaara7043832005-01-21 11:56:39 +000023829 * Also handles a Funcref in a List or Dictionary.
Bram Moolenaar071d4272004-06-13 20:20:40 +000023830 * Returns the function name in allocated memory, or NULL for failure.
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023831 * flags:
Bram Moolenaarc9703302016-01-17 21:49:33 +010023832 * TFN_INT: internal function name OK
23833 * TFN_QUIET: be quiet
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023834 * TFN_NO_AUTOLOAD: do not use script autoloading
Bram Moolenaar071d4272004-06-13 20:20:40 +000023835 * Advances "pp" to just after the function name (if no error).
23836 */
23837 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010023838trans_function_name(
23839 char_u **pp,
23840 int skip, /* only find the end, don't evaluate */
23841 int flags,
Bram Moolenaar65639032016-03-16 21:40:30 +010023842 funcdict_T *fdp, /* return: info about dictionary used */
23843 partial_T **partial) /* return: partial of a FuncRef */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023844{
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023845 char_u *name = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000023846 char_u *start;
23847 char_u *end;
23848 int lead;
23849 char_u sid_buf[20];
Bram Moolenaar071d4272004-06-13 20:20:40 +000023850 int len;
Bram Moolenaar33570922005-01-25 22:26:29 +000023851 lval_T lv;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023852
23853 if (fdp != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000023854 vim_memset(fdp, 0, sizeof(funcdict_T));
Bram Moolenaar071d4272004-06-13 20:20:40 +000023855 start = *pp;
Bram Moolenaara7043832005-01-21 11:56:39 +000023856
23857 /* Check for hard coded <SNR>: already translated function ID (from a user
23858 * command). */
23859 if ((*pp)[0] == K_SPECIAL && (*pp)[1] == KS_EXTRA
23860 && (*pp)[2] == (int)KE_SNR)
23861 {
23862 *pp += 3;
23863 len = get_id_len(pp) + 3;
23864 return vim_strnsave(start, len);
23865 }
23866
23867 /* A name starting with "<SID>" or "<SNR>" is local to a script. But
23868 * don't skip over "s:", get_lval() needs it for "s:dict.func". */
Bram Moolenaar071d4272004-06-13 20:20:40 +000023869 lead = eval_fname_script(start);
Bram Moolenaara7043832005-01-21 11:56:39 +000023870 if (lead > 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +000023871 start += lead;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023872
Bram Moolenaar6d977d62014-01-14 15:24:39 +010023873 /* Note that TFN_ flags use the same values as GLV_ flags. */
23874 end = get_lval(start, NULL, &lv, FALSE, skip, flags,
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023875 lead > 2 ? 0 : FNE_CHECK_START);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023876 if (end == start)
23877 {
23878 if (!skip)
23879 EMSG(_("E129: Function name required"));
23880 goto theend;
23881 }
Bram Moolenaara7043832005-01-21 11:56:39 +000023882 if (end == NULL || (lv.ll_tv != NULL && (lead > 2 || lv.ll_range)))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023883 {
23884 /*
23885 * Report an invalid expression in braces, unless the expression
23886 * evaluation has been cancelled due to an aborting error, an
23887 * interrupt, or an exception.
23888 */
23889 if (!aborting())
23890 {
23891 if (end != NULL)
23892 EMSG2(_(e_invarg2), start);
23893 }
23894 else
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000023895 *pp = find_name_end(start, NULL, NULL, FNE_INCL_BR);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023896 goto theend;
23897 }
23898
23899 if (lv.ll_tv != NULL)
23900 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023901 if (fdp != NULL)
23902 {
23903 fdp->fd_dict = lv.ll_dict;
23904 fdp->fd_newkey = lv.ll_newkey;
23905 lv.ll_newkey = NULL;
23906 fdp->fd_di = lv.ll_di;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023907 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023908 if (lv.ll_tv->v_type == VAR_FUNC && lv.ll_tv->vval.v_string != NULL)
23909 {
23910 name = vim_strsave(lv.ll_tv->vval.v_string);
23911 *pp = end;
23912 }
Bram Moolenaard22a1892016-03-17 20:50:47 +010023913 else if (lv.ll_tv->v_type == VAR_PARTIAL
23914 && lv.ll_tv->vval.v_partial != NULL)
23915 {
23916 name = vim_strsave(lv.ll_tv->vval.v_partial->pt_name);
23917 *pp = end;
23918 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023919 else
23920 {
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000023921 if (!skip && !(flags & TFN_QUIET) && (fdp == NULL
23922 || lv.ll_dict == NULL || fdp->fd_newkey == NULL))
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000023923 EMSG(_(e_funcref));
23924 else
23925 *pp = end;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023926 name = NULL;
23927 }
23928 goto theend;
23929 }
23930
23931 if (lv.ll_name == NULL)
23932 {
23933 /* Error found, but continue after the function name. */
23934 *pp = end;
23935 goto theend;
23936 }
23937
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023938 /* Check if the name is a Funcref. If so, use the value. */
23939 if (lv.ll_exp_name != NULL)
23940 {
23941 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaar65639032016-03-16 21:40:30 +010023942 name = deref_func_name(lv.ll_exp_name, &len, partial,
Bram Moolenaar1735bc92016-03-14 23:05:14 +010023943 flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023944 if (name == lv.ll_exp_name)
23945 name = NULL;
23946 }
23947 else
23948 {
23949 len = (int)(end - *pp);
Bram Moolenaar65639032016-03-16 21:40:30 +010023950 name = deref_func_name(*pp, &len, partial, flags & TFN_NO_AUTOLOAD);
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023951 if (name == *pp)
23952 name = NULL;
23953 }
23954 if (name != NULL)
23955 {
23956 name = vim_strsave(name);
23957 *pp = end;
Bram Moolenaar355a95a2014-04-29 14:03:02 +020023958 if (STRNCMP(name, "<SNR>", 5) == 0)
23959 {
23960 /* Change "<SNR>" to the byte sequence. */
23961 name[0] = K_SPECIAL;
23962 name[1] = KS_EXTRA;
23963 name[2] = (int)KE_SNR;
23964 mch_memmove(name + 3, name + 5, STRLEN(name + 5) + 1);
23965 }
Bram Moolenaar33e1a802007-09-06 12:26:44 +000023966 goto theend;
23967 }
23968
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023969 if (lv.ll_exp_name != NULL)
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023970 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000023971 len = (int)STRLEN(lv.ll_exp_name);
Bram Moolenaarc32840f2006-01-14 21:23:38 +000023972 if (lead <= 2 && lv.ll_name == lv.ll_exp_name
23973 && STRNCMP(lv.ll_name, "s:", 2) == 0)
23974 {
23975 /* When there was "s:" already or the name expanded to get a
23976 * leading "s:" then remove it. */
23977 lv.ll_name += 2;
23978 len -= 2;
23979 lead = 2;
23980 }
23981 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023982 else
Bram Moolenaara7043832005-01-21 11:56:39 +000023983 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020023984 /* skip over "s:" and "g:" */
23985 if (lead == 2 || (lv.ll_name[0] == 'g' && lv.ll_name[1] == ':'))
Bram Moolenaara7043832005-01-21 11:56:39 +000023986 lv.ll_name += 2;
23987 len = (int)(end - lv.ll_name);
23988 }
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000023989
23990 /*
23991 * Copy the function name to allocated memory.
23992 * Accept <SID>name() inside a script, translate into <SNR>123_name().
23993 * Accept <SNR>123_name() outside a script.
23994 */
23995 if (skip)
23996 lead = 0; /* do nothing */
23997 else if (lead > 0)
23998 {
23999 lead = 3;
Bram Moolenaar86c9ee22006-05-13 11:33:27 +000024000 if ((lv.ll_exp_name != NULL && eval_fname_sid(lv.ll_exp_name))
24001 || eval_fname_sid(*pp))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024002 {
Bram Moolenaar899dddf2006-03-26 21:06:50 +000024003 /* It's "s:" or "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024004 if (current_SID <= 0)
24005 {
24006 EMSG(_(e_usingsid));
24007 goto theend;
24008 }
24009 sprintf((char *)sid_buf, "%ld_", (long)current_SID);
24010 lead += (int)STRLEN(sid_buf);
24011 }
24012 }
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024013 else if (!(flags & TFN_INT) && builtin_function(lv.ll_name, len))
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024014 {
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024015 EMSG2(_("E128: Function name must start with a capital or \"s:\": %s"),
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024016 start);
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024017 goto theend;
24018 }
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024019 if (!skip && !(flags & TFN_QUIET))
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024020 {
24021 char_u *cp = vim_strchr(lv.ll_name, ':');
24022
24023 if (cp != NULL && cp < end)
24024 {
Bram Moolenaareccb7fc2014-04-23 20:43:41 +020024025 EMSG2(_("E884: Function name cannot contain a colon: %s"), start);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024026 goto theend;
24027 }
24028 }
24029
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024030 name = alloc((unsigned)(len + lead + 1));
24031 if (name != NULL)
24032 {
24033 if (lead > 0)
24034 {
24035 name[0] = K_SPECIAL;
24036 name[1] = KS_EXTRA;
24037 name[2] = (int)KE_SNR;
Bram Moolenaara7043832005-01-21 11:56:39 +000024038 if (lead > 3) /* If it's "<SID>" */
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024039 STRCPY(name + 3, sid_buf);
24040 }
24041 mch_memmove(name + lead, lv.ll_name, (size_t)len);
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024042 name[lead + len] = NUL;
Bram Moolenaar7480b5c2005-01-16 22:07:38 +000024043 }
24044 *pp = end;
24045
24046theend:
24047 clear_lval(&lv);
24048 return name;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024049}
24050
24051/*
24052 * Return 5 if "p" starts with "<SID>" or "<SNR>" (ignoring case).
24053 * Return 2 if "p" starts with "s:".
24054 * Return 0 otherwise.
24055 */
24056 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024057eval_fname_script(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024058{
Bram Moolenaare266d6d2016-01-19 20:51:32 +010024059 /* Use MB_STRICMP() because in Turkish comparing the "I" may not work with
24060 * the standard library function. */
24061 if (p[0] == '<' && (MB_STRNICMP(p + 1, "SID>", 4) == 0
24062 || MB_STRNICMP(p + 1, "SNR>", 4) == 0))
Bram Moolenaar071d4272004-06-13 20:20:40 +000024063 return 5;
24064 if (p[0] == 's' && p[1] == ':')
24065 return 2;
24066 return 0;
24067}
24068
24069/*
24070 * Return TRUE if "p" starts with "<SID>" or "s:".
24071 * Only works if eval_fname_script() returned non-zero for "p"!
24072 */
24073 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024074eval_fname_sid(char_u *p)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024075{
24076 return (*p == 's' || TOUPPER_ASC(p[2]) == 'I');
24077}
24078
24079/*
24080 * List the head of the function: "name(arg1, arg2)".
24081 */
24082 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024083list_func_head(ufunc_T *fp, int indent)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024084{
24085 int j;
24086
24087 msg_start();
24088 if (indent)
24089 MSG_PUTS(" ");
24090 MSG_PUTS("function ");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024091 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024092 {
24093 MSG_PUTS_ATTR("<SNR>", hl_attr(HLF_8));
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024094 msg_puts(fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024095 }
24096 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024097 msg_puts(fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024098 msg_putchar('(');
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024099 for (j = 0; j < fp->uf_args.ga_len; ++j)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024100 {
24101 if (j)
24102 MSG_PUTS(", ");
24103 msg_puts(FUNCARG(fp, j));
24104 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024105 if (fp->uf_varargs)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024106 {
24107 if (j)
24108 MSG_PUTS(", ");
24109 MSG_PUTS("...");
24110 }
24111 msg_putchar(')');
Bram Moolenaar4cd92d52013-06-06 21:31:06 +020024112 if (fp->uf_flags & FC_ABORT)
24113 MSG_PUTS(" abort");
24114 if (fp->uf_flags & FC_RANGE)
24115 MSG_PUTS(" range");
24116 if (fp->uf_flags & FC_DICT)
24117 MSG_PUTS(" dict");
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000024118 msg_clr_eos();
Bram Moolenaar5b8d8fd2005-08-16 23:01:50 +000024119 if (p_verbose > 0)
24120 last_set_msg(fp->uf_script_ID);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024121}
24122
24123/*
24124 * Find a function by name, return pointer to it in ufuncs.
24125 * Return NULL for unknown function.
24126 */
24127 static ufunc_T *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024128find_func(char_u *name)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024129{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024130 hashitem_T *hi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024131
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024132 hi = hash_find(&func_hashtab, name);
24133 if (!HASHITEM_EMPTY(hi))
24134 return HI2UF(hi);
24135 return NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024136}
24137
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024138#if defined(EXITFREE) || defined(PROTO)
24139 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024140free_all_functions(void)
Bram Moolenaar29a1c1d2005-06-24 23:11:15 +000024141{
24142 hashitem_T *hi;
24143
24144 /* Need to start all over every time, because func_free() may change the
24145 * hash table. */
24146 while (func_hashtab.ht_used > 0)
24147 for (hi = func_hashtab.ht_array; ; ++hi)
24148 if (!HASHITEM_EMPTY(hi))
24149 {
24150 func_free(HI2UF(hi));
24151 break;
24152 }
24153}
24154#endif
24155
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024156 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024157translated_function_exists(char_u *name)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024158{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024159 if (builtin_function(name, -1))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024160 return find_internal_func(name) >= 0;
24161 return find_func(name) != NULL;
24162}
24163
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024164/*
24165 * Return TRUE if a function "name" exists.
24166 */
24167 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024168function_exists(char_u *name)
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024169{
Bram Moolenaaraa35dd12006-04-29 22:03:41 +000024170 char_u *nm = name;
24171 char_u *p;
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024172 int n = FALSE;
24173
Bram Moolenaar6d977d62014-01-14 15:24:39 +010024174 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET|TFN_NO_AUTOLOAD,
Bram Moolenaar65639032016-03-16 21:40:30 +010024175 NULL, NULL);
Bram Moolenaar79783442006-05-05 21:18:03 +000024176 nm = skipwhite(nm);
24177
24178 /* Only accept "funcname", "funcname ", "funcname (..." and
24179 * "funcname(...", not "funcname!...". */
24180 if (p != NULL && (*nm == NUL || *nm == '('))
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024181 n = translated_function_exists(p);
Bram Moolenaar79783442006-05-05 21:18:03 +000024182 vim_free(p);
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024183 return n;
24184}
24185
Bram Moolenaara1544c02013-05-30 12:35:52 +020024186 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024187get_expanded_name(char_u *name, int check)
Bram Moolenaara1544c02013-05-30 12:35:52 +020024188{
24189 char_u *nm = name;
24190 char_u *p;
24191
Bram Moolenaar65639032016-03-16 21:40:30 +010024192 p = trans_function_name(&nm, FALSE, TFN_INT|TFN_QUIET, NULL, NULL);
Bram Moolenaara1544c02013-05-30 12:35:52 +020024193
24194 if (p != NULL && *nm == NUL)
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024195 if (!check || translated_function_exists(p))
Bram Moolenaara1544c02013-05-30 12:35:52 +020024196 return p;
Bram Moolenaar355fd9b2013-05-30 13:14:13 +020024197
Bram Moolenaara1544c02013-05-30 12:35:52 +020024198 vim_free(p);
24199 return NULL;
24200}
24201
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024202/*
24203 * Return TRUE if "name" looks like a builtin function name: starts with a
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024204 * lower case letter and doesn't contain AUTOLOAD_CHAR.
24205 * "len" is the length of "name", or -1 for NUL terminated.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024206 */
24207 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024208builtin_function(char_u *name, int len)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024209{
Bram Moolenaar9bdfb002014-04-23 17:43:42 +020024210 char_u *p;
24211
24212 if (!ASCII_ISLOWER(name[0]))
24213 return FALSE;
24214 p = vim_strchr(name, AUTOLOAD_CHAR);
24215 return p == NULL || (len > 0 && p > name + len);
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024216}
24217
Bram Moolenaar05159a02005-02-26 23:04:13 +000024218#if defined(FEAT_PROFILE) || defined(PROTO)
24219/*
24220 * Start profiling function "fp".
24221 */
24222 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024223func_do_profile(ufunc_T *fp)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024224{
Bram Moolenaar904c6222010-07-24 16:57:39 +020024225 int len = fp->uf_lines.ga_len;
24226
24227 if (len == 0)
24228 len = 1; /* avoid getting error for allocating zero bytes */
Bram Moolenaar05159a02005-02-26 23:04:13 +000024229 fp->uf_tm_count = 0;
24230 profile_zero(&fp->uf_tm_self);
24231 profile_zero(&fp->uf_tm_total);
24232 if (fp->uf_tml_count == NULL)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024233 fp->uf_tml_count = (int *)alloc_clear((unsigned) (sizeof(int) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024234 if (fp->uf_tml_total == NULL)
24235 fp->uf_tml_total = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024236 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024237 if (fp->uf_tml_self == NULL)
24238 fp->uf_tml_self = (proftime_T *)alloc_clear((unsigned)
Bram Moolenaar904c6222010-07-24 16:57:39 +020024239 (sizeof(proftime_T) * len));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024240 fp->uf_tml_idx = -1;
24241 if (fp->uf_tml_count == NULL || fp->uf_tml_total == NULL
24242 || fp->uf_tml_self == NULL)
24243 return; /* out of memory */
24244
24245 fp->uf_profiling = TRUE;
24246}
24247
24248/*
24249 * Dump the profiling results for all functions in file "fd".
24250 */
24251 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024252func_dump_profile(FILE *fd)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024253{
24254 hashitem_T *hi;
24255 int todo;
24256 ufunc_T *fp;
24257 int i;
Bram Moolenaar73830342005-02-28 22:48:19 +000024258 ufunc_T **sorttab;
24259 int st_len = 0;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024260
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000024261 todo = (int)func_hashtab.ht_used;
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024262 if (todo == 0)
24263 return; /* nothing to dump */
24264
Bram Moolenaare2e4b982015-06-09 20:30:51 +020024265 sorttab = (ufunc_T **)alloc((unsigned)(sizeof(ufunc_T *) * todo));
Bram Moolenaar73830342005-02-28 22:48:19 +000024266
Bram Moolenaar05159a02005-02-26 23:04:13 +000024267 for (hi = func_hashtab.ht_array; todo > 0; ++hi)
24268 {
24269 if (!HASHITEM_EMPTY(hi))
24270 {
24271 --todo;
24272 fp = HI2UF(hi);
24273 if (fp->uf_profiling)
24274 {
Bram Moolenaar73830342005-02-28 22:48:19 +000024275 if (sorttab != NULL)
24276 sorttab[st_len++] = fp;
24277
Bram Moolenaar05159a02005-02-26 23:04:13 +000024278 if (fp->uf_name[0] == K_SPECIAL)
24279 fprintf(fd, "FUNCTION <SNR>%s()\n", fp->uf_name + 3);
24280 else
24281 fprintf(fd, "FUNCTION %s()\n", fp->uf_name);
24282 if (fp->uf_tm_count == 1)
24283 fprintf(fd, "Called 1 time\n");
24284 else
24285 fprintf(fd, "Called %d times\n", fp->uf_tm_count);
24286 fprintf(fd, "Total time: %s\n", profile_msg(&fp->uf_tm_total));
24287 fprintf(fd, " Self time: %s\n", profile_msg(&fp->uf_tm_self));
24288 fprintf(fd, "\n");
24289 fprintf(fd, "count total (s) self (s)\n");
24290
24291 for (i = 0; i < fp->uf_lines.ga_len; ++i)
24292 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000024293 if (FUNCLINE(fp, i) == NULL)
24294 continue;
Bram Moolenaar73830342005-02-28 22:48:19 +000024295 prof_func_line(fd, fp->uf_tml_count[i],
24296 &fp->uf_tml_total[i], &fp->uf_tml_self[i], TRUE);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024297 fprintf(fd, "%s\n", FUNCLINE(fp, i));
24298 }
24299 fprintf(fd, "\n");
24300 }
24301 }
24302 }
Bram Moolenaar73830342005-02-28 22:48:19 +000024303
24304 if (sorttab != NULL && st_len > 0)
24305 {
24306 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24307 prof_total_cmp);
24308 prof_sort_list(fd, sorttab, st_len, "TOTAL", FALSE);
24309 qsort((void *)sorttab, (size_t)st_len, sizeof(ufunc_T *),
24310 prof_self_cmp);
24311 prof_sort_list(fd, sorttab, st_len, "SELF", TRUE);
24312 }
Bram Moolenaar61c4e2c2008-08-25 02:49:18 +000024313
24314 vim_free(sorttab);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024315}
Bram Moolenaar73830342005-02-28 22:48:19 +000024316
24317 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024318prof_sort_list(
24319 FILE *fd,
24320 ufunc_T **sorttab,
24321 int st_len,
24322 char *title,
24323 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024324{
24325 int i;
24326 ufunc_T *fp;
24327
24328 fprintf(fd, "FUNCTIONS SORTED ON %s TIME\n", title);
24329 fprintf(fd, "count total (s) self (s) function\n");
24330 for (i = 0; i < 20 && i < st_len; ++i)
24331 {
24332 fp = sorttab[i];
24333 prof_func_line(fd, fp->uf_tm_count, &fp->uf_tm_total, &fp->uf_tm_self,
24334 prefer_self);
24335 if (fp->uf_name[0] == K_SPECIAL)
24336 fprintf(fd, " <SNR>%s()\n", fp->uf_name + 3);
24337 else
24338 fprintf(fd, " %s()\n", fp->uf_name);
24339 }
24340 fprintf(fd, "\n");
24341}
24342
24343/*
24344 * Print the count and times for one function or function line.
24345 */
24346 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024347prof_func_line(
24348 FILE *fd,
24349 int count,
24350 proftime_T *total,
24351 proftime_T *self,
24352 int prefer_self) /* when equal print only self time */
Bram Moolenaar73830342005-02-28 22:48:19 +000024353{
24354 if (count > 0)
24355 {
24356 fprintf(fd, "%5d ", count);
24357 if (prefer_self && profile_equal(total, self))
24358 fprintf(fd, " ");
24359 else
24360 fprintf(fd, "%s ", profile_msg(total));
24361 if (!prefer_self && profile_equal(total, self))
24362 fprintf(fd, " ");
24363 else
24364 fprintf(fd, "%s ", profile_msg(self));
24365 }
24366 else
24367 fprintf(fd, " ");
24368}
24369
24370/*
24371 * Compare function for total time sorting.
24372 */
24373 static int
24374#ifdef __BORLANDC__
24375_RTLENTRYF
24376#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024377prof_total_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024378{
24379 ufunc_T *p1, *p2;
24380
24381 p1 = *(ufunc_T **)s1;
24382 p2 = *(ufunc_T **)s2;
24383 return profile_cmp(&p1->uf_tm_total, &p2->uf_tm_total);
24384}
24385
24386/*
24387 * Compare function for self time sorting.
24388 */
24389 static int
24390#ifdef __BORLANDC__
24391_RTLENTRYF
24392#endif
Bram Moolenaar7454a062016-01-30 15:14:10 +010024393prof_self_cmp(const void *s1, const void *s2)
Bram Moolenaar73830342005-02-28 22:48:19 +000024394{
24395 ufunc_T *p1, *p2;
24396
24397 p1 = *(ufunc_T **)s1;
24398 p2 = *(ufunc_T **)s2;
24399 return profile_cmp(&p1->uf_tm_self, &p2->uf_tm_self);
24400}
24401
Bram Moolenaar05159a02005-02-26 23:04:13 +000024402#endif
24403
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024404/*
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024405 * If "name" has a package name try autoloading the script for it.
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024406 * Return TRUE if a package was loaded.
24407 */
Bram Moolenaar018acca2013-05-30 13:37:28 +020024408 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010024409script_autoload(
24410 char_u *name,
24411 int reload) /* load script again when already loaded */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024412{
24413 char_u *p;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024414 char_u *scriptname, *tofree;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024415 int ret = FALSE;
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024416 int i;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024417
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024418 /* If there is no '#' after name[0] there is no package name. */
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024419 p = vim_strchr(name, AUTOLOAD_CHAR);
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024420 if (p == NULL || p == name)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024421 return FALSE;
24422
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024423 tofree = scriptname = autoload_name(name);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024424
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024425 /* Find the name in the list of previously loaded package names. Skip
24426 * "autoload/", it's always the same. */
24427 for (i = 0; i < ga_loaded.ga_len; ++i)
24428 if (STRCMP(((char_u **)ga_loaded.ga_data)[i] + 9, scriptname + 9) == 0)
24429 break;
24430 if (!reload && i < ga_loaded.ga_len)
24431 ret = FALSE; /* was loaded already */
24432 else
24433 {
24434 /* Remember the name if it wasn't loaded already. */
24435 if (i == ga_loaded.ga_len && ga_grow(&ga_loaded, 1) == OK)
24436 {
24437 ((char_u **)ga_loaded.ga_data)[ga_loaded.ga_len++] = scriptname;
24438 tofree = NULL;
24439 }
24440
24441 /* Try loading the package from $VIMRUNTIME/autoload/<name>.vim */
Bram Moolenaar7f8989d2016-03-12 22:11:39 +010024442 if (source_runtime(scriptname, 0) == OK)
Bram Moolenaar87e25fd2005-07-27 21:13:01 +000024443 ret = TRUE;
24444 }
24445
24446 vim_free(tofree);
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024447 return ret;
24448}
24449
24450/*
24451 * Return the autoload script name for a function or variable name.
24452 * Returns NULL when out of memory.
24453 */
24454 static char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024455autoload_name(char_u *name)
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024456{
24457 char_u *p;
24458 char_u *scriptname;
24459
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024460 /* Get the script file name: replace '#' with '/', append ".vim". */
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024461 scriptname = alloc((unsigned)(STRLEN(name) + 14));
24462 if (scriptname == NULL)
24463 return FALSE;
24464 STRCPY(scriptname, "autoload/");
24465 STRCAT(scriptname, name);
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024466 *vim_strrchr(scriptname, AUTOLOAD_CHAR) = NUL;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024467 STRCAT(scriptname, ".vim");
Bram Moolenaar34cdc3e2005-05-18 22:24:46 +000024468 while ((p = vim_strchr(scriptname, AUTOLOAD_CHAR)) != NULL)
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024469 *p = '/';
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024470 return scriptname;
Bram Moolenaarb11bd7e2005-02-07 22:05:52 +000024471}
24472
Bram Moolenaar071d4272004-06-13 20:20:40 +000024473#if defined(FEAT_CMDL_COMPL) || defined(PROTO)
24474
24475/*
24476 * Function given to ExpandGeneric() to obtain the list of user defined
24477 * function names.
24478 */
24479 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010024480get_user_func_name(expand_T *xp, int idx)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024481{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024482 static long_u done;
24483 static hashitem_T *hi;
24484 ufunc_T *fp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024485
24486 if (idx == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024487 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024488 done = 0;
24489 hi = func_hashtab.ht_array;
24490 }
24491 if (done < func_hashtab.ht_used)
24492 {
24493 if (done++ > 0)
24494 ++hi;
24495 while (HASHITEM_EMPTY(hi))
24496 ++hi;
24497 fp = HI2UF(hi);
24498
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024499 if (fp->uf_flags & FC_DICT)
Bram Moolenaar975261e2012-01-26 18:52:06 +010024500 return (char_u *)""; /* don't show dict functions */
Bram Moolenaar195ea0f2011-11-30 14:57:31 +010024501
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024502 if (STRLEN(fp->uf_name) + 4 >= IOSIZE)
24503 return fp->uf_name; /* prevents overflow */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024504
24505 cat_func_name(IObuff, fp);
24506 if (xp->xp_context != EXPAND_USER_FUNC)
24507 {
24508 STRCAT(IObuff, "(");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024509 if (!fp->uf_varargs && fp->uf_args.ga_len == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024510 STRCAT(IObuff, ")");
24511 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024512 return IObuff;
24513 }
24514 return NULL;
24515}
24516
24517#endif /* FEAT_CMDL_COMPL */
24518
24519/*
24520 * Copy the function name of "fp" to buffer "buf".
24521 * "buf" must be able to hold the function name plus three bytes.
24522 * Takes care of script-local function names.
24523 */
24524 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024525cat_func_name(char_u *buf, ufunc_T *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024526{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024527 if (fp->uf_name[0] == K_SPECIAL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024528 {
24529 STRCPY(buf, "<SNR>");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024530 STRCAT(buf, fp->uf_name + 3);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024531 }
24532 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024533 STRCPY(buf, fp->uf_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024534}
24535
24536/*
24537 * ":delfunction {name}"
24538 */
24539 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024540ex_delfunction(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024541{
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024542 ufunc_T *fp = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024543 char_u *p;
24544 char_u *name;
Bram Moolenaar33570922005-01-25 22:26:29 +000024545 funcdict_T fudi;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024546
24547 p = eap->arg;
Bram Moolenaar65639032016-03-16 21:40:30 +010024548 name = trans_function_name(&p, eap->skip, 0, &fudi, NULL);
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024549 vim_free(fudi.fd_newkey);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024550 if (name == NULL)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024551 {
24552 if (fudi.fd_dict != NULL && !eap->skip)
24553 EMSG(_(e_funcref));
Bram Moolenaar071d4272004-06-13 20:20:40 +000024554 return;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024555 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024556 if (!ends_excmd(*skipwhite(p)))
24557 {
24558 vim_free(name);
24559 EMSG(_(e_trailing));
24560 return;
24561 }
24562 eap->nextcmd = check_nextcmd(p);
24563 if (eap->nextcmd != NULL)
24564 *p = NUL;
24565
24566 if (!eap->skip)
24567 fp = find_func(name);
24568 vim_free(name);
24569
24570 if (!eap->skip)
24571 {
24572 if (fp == NULL)
24573 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000024574 EMSG2(_(e_nofunc), eap->arg);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024575 return;
24576 }
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024577 if (fp->uf_calls > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024578 {
24579 EMSG2(_("E131: Cannot delete function %s: It is in use"), eap->arg);
24580 return;
24581 }
24582
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024583 if (fudi.fd_dict != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024584 {
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024585 /* Delete the dict item that refers to the function, it will
24586 * invoke func_unref() and possibly delete the function. */
Bram Moolenaarce5e58e2005-01-19 22:24:34 +000024587 dictitem_remove(fudi.fd_dict, fudi.fd_di);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024588 }
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024589 else
24590 func_free(fp);
24591 }
24592}
24593
24594/*
24595 * Free a function and remove it from the list of functions.
24596 */
24597 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024598func_free(ufunc_T *fp)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024599{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024600 hashitem_T *hi;
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024601
24602 /* clear this function */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024603 ga_clear_strings(&(fp->uf_args));
24604 ga_clear_strings(&(fp->uf_lines));
Bram Moolenaar05159a02005-02-26 23:04:13 +000024605#ifdef FEAT_PROFILE
24606 vim_free(fp->uf_tml_count);
24607 vim_free(fp->uf_tml_total);
24608 vim_free(fp->uf_tml_self);
24609#endif
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024610
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024611 /* remove the function from the function hashtable */
24612 hi = hash_find(&func_hashtab, UF2HIKEY(fp));
24613 if (HASHITEM_EMPTY(hi))
24614 EMSG2(_(e_intern2), "func_free()");
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024615 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024616 hash_remove(&func_hashtab, hi);
24617
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024618 vim_free(fp);
24619}
24620
24621/*
24622 * Unreference a Function: decrement the reference count and free it when it
24623 * becomes zero. Only for numbered functions.
24624 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024625 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024626func_unref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024627{
24628 ufunc_T *fp;
24629
24630 if (name != NULL && isdigit(*name))
24631 {
24632 fp = find_func(name);
24633 if (fp == NULL)
24634 EMSG2(_(e_intern2), "func_unref()");
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024635 else if (--fp->uf_refcount <= 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024636 {
24637 /* Only delete it when it's not being used. Otherwise it's done
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024638 * when "uf_calls" becomes zero. */
24639 if (fp->uf_calls == 0)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024640 func_free(fp);
24641 }
24642 }
24643}
24644
24645/*
24646 * Count a reference to a Function.
24647 */
Bram Moolenaardb913952012-06-29 12:54:53 +020024648 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024649func_ref(char_u *name)
Bram Moolenaar9ef486d2005-01-17 22:23:00 +000024650{
24651 ufunc_T *fp;
24652
24653 if (name != NULL && isdigit(*name))
24654 {
24655 fp = find_func(name);
24656 if (fp == NULL)
24657 EMSG2(_(e_intern2), "func_ref()");
24658 else
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024659 ++fp->uf_refcount;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024660 }
24661}
24662
24663/*
24664 * Call a user function.
24665 */
24666 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010024667call_user_func(
24668 ufunc_T *fp, /* pointer to function */
24669 int argcount, /* nr of args */
24670 typval_T *argvars, /* arguments */
24671 typval_T *rettv, /* return value */
24672 linenr_T firstline, /* first line of range */
24673 linenr_T lastline, /* last line of range */
24674 dict_T *selfdict) /* Dictionary for "self" */
Bram Moolenaar071d4272004-06-13 20:20:40 +000024675{
Bram Moolenaar33570922005-01-25 22:26:29 +000024676 char_u *save_sourcing_name;
24677 linenr_T save_sourcing_lnum;
24678 scid_T save_current_SID;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024679 funccall_T *fc;
Bram Moolenaar33570922005-01-25 22:26:29 +000024680 int save_did_emsg;
24681 static int depth = 0;
24682 dictitem_T *v;
24683 int fixvar_idx = 0; /* index in fixvar[] */
24684 int i;
24685 int ai;
24686 char_u numbuf[NUMBUFLEN];
24687 char_u *name;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024688 size_t len;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024689#ifdef FEAT_PROFILE
24690 proftime_T wait_start;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024691 proftime_T call_start;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024692#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024693
24694 /* If depth of calling is getting too high, don't execute the function */
24695 if (depth >= p_mfd)
24696 {
24697 EMSG(_("E132: Function call depth is higher than 'maxfuncdepth'"));
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024698 rettv->v_type = VAR_NUMBER;
24699 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024700 return;
24701 }
24702 ++depth;
24703
24704 line_breakcheck(); /* check for CTRL-C hit */
24705
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024706 fc = (funccall_T *)alloc(sizeof(funccall_T));
24707 fc->caller = current_funccal;
24708 current_funccal = fc;
24709 fc->func = fp;
24710 fc->rettv = rettv;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024711 rettv->vval.v_number = 0;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024712 fc->linenr = 0;
24713 fc->returned = FALSE;
24714 fc->level = ex_nesting_level;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024715 /* Check if this function has a breakpoint. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024716 fc->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name, (linenr_T)0);
24717 fc->dbg_tick = debug_tick;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024718
Bram Moolenaar33570922005-01-25 22:26:29 +000024719 /*
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024720 * Note about using fc->fixvar[]: This is an array of FIXVAR_CNT variables
Bram Moolenaar33570922005-01-25 22:26:29 +000024721 * with names up to VAR_SHORT_LEN long. This avoids having to alloc/free
24722 * each argument variable and saves a lot of time.
24723 */
24724 /*
24725 * Init l: variables.
24726 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024727 init_var_dict(&fc->l_vars, &fc->l_vars_var, VAR_DEF_SCOPE);
Bram Moolenaara7043832005-01-21 11:56:39 +000024728 if (selfdict != NULL)
Bram Moolenaare9a41262005-01-15 22:18:47 +000024729 {
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024730 /* Set l:self to "selfdict". Use "name" to avoid a warning from
24731 * some compiler that checks the destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024732 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar76b92b22006-03-24 22:46:53 +000024733 name = v->di_key;
24734 STRCPY(name, "self");
Bram Moolenaar33570922005-01-25 22:26:29 +000024735 v->di_flags = DI_FLAGS_RO + DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024736 hash_add(&fc->l_vars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024737 v->di_tv.v_type = VAR_DICT;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024738 v->di_tv.v_lock = 0;
Bram Moolenaar33570922005-01-25 22:26:29 +000024739 v->di_tv.vval.v_dict = selfdict;
24740 ++selfdict->dv_refcount;
24741 }
Bram Moolenaare9a41262005-01-15 22:18:47 +000024742
Bram Moolenaar33570922005-01-25 22:26:29 +000024743 /*
24744 * Init a: variables.
24745 * Set a:0 to "argcount".
24746 * Set a:000 to a list with room for the "..." arguments.
24747 */
Bram Moolenaarbdb62052012-07-16 17:31:53 +020024748 init_var_dict(&fc->l_avars, &fc->l_avars_var, VAR_SCOPE);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024749 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "0",
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024750 (varnumber_T)(argcount - fp->uf_args.ga_len));
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024751 /* Use "name" to avoid a warning from some compiler that checks the
24752 * destination size. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024753 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar0cd49302008-11-20 09:37:01 +000024754 name = v->di_key;
24755 STRCPY(name, "000");
Bram Moolenaar33570922005-01-25 22:26:29 +000024756 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024757 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024758 v->di_tv.v_type = VAR_LIST;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024759 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024760 v->di_tv.vval.v_list = &fc->l_varlist;
24761 vim_memset(&fc->l_varlist, 0, sizeof(list_T));
24762 fc->l_varlist.lv_refcount = DO_NOT_FREE_CNT;
24763 fc->l_varlist.lv_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024764
24765 /*
24766 * Set a:firstline to "firstline" and a:lastline to "lastline".
24767 * Set a:name to named arguments.
24768 * Set a:N to the "..." arguments.
24769 */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024770 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "firstline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024771 (varnumber_T)firstline);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024772 add_nr_var(&fc->l_avars, &fc->fixvar[fixvar_idx++].var, "lastline",
Bram Moolenaar33570922005-01-25 22:26:29 +000024773 (varnumber_T)lastline);
24774 for (i = 0; i < argcount; ++i)
24775 {
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024776 ai = i - fp->uf_args.ga_len;
Bram Moolenaar33570922005-01-25 22:26:29 +000024777 if (ai < 0)
24778 /* named argument a:name */
24779 name = FUNCARG(fp, i);
24780 else
Bram Moolenaare9a41262005-01-15 22:18:47 +000024781 {
Bram Moolenaar33570922005-01-25 22:26:29 +000024782 /* "..." argument a:1, a:2, etc. */
24783 sprintf((char *)numbuf, "%d", ai + 1);
24784 name = numbuf;
24785 }
24786 if (fixvar_idx < FIXVAR_CNT && STRLEN(name) <= VAR_SHORT_LEN)
24787 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024788 v = &fc->fixvar[fixvar_idx++].var;
Bram Moolenaar33570922005-01-25 22:26:29 +000024789 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
24790 }
24791 else
24792 {
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024793 v = (dictitem_T *)alloc((unsigned)(sizeof(dictitem_T)
24794 + STRLEN(name)));
Bram Moolenaar33570922005-01-25 22:26:29 +000024795 if (v == NULL)
24796 break;
Bram Moolenaar9bc174b2015-04-13 16:16:38 +020024797 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX | DI_FLAGS_ALLOC;
Bram Moolenaar33570922005-01-25 22:26:29 +000024798 }
24799 STRCPY(v->di_key, name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024800 hash_add(&fc->l_avars.dv_hashtab, DI2HIKEY(v));
Bram Moolenaar33570922005-01-25 22:26:29 +000024801
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024802 /* Note: the values are copied directly to avoid alloc/free.
24803 * "argvars" must have VAR_FIXED for v_lock. */
Bram Moolenaar33570922005-01-25 22:26:29 +000024804 v->di_tv = argvars[i];
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000024805 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000024806
24807 if (ai >= 0 && ai < MAX_FUNC_ARGS)
24808 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024809 list_append(&fc->l_varlist, &fc->l_listitems[ai]);
24810 fc->l_listitems[ai].li_tv = argvars[i];
24811 fc->l_listitems[ai].li_tv.v_lock = VAR_FIXED;
Bram Moolenaare9a41262005-01-15 22:18:47 +000024812 }
24813 }
24814
Bram Moolenaar071d4272004-06-13 20:20:40 +000024815 /* Don't redraw while executing the function. */
24816 ++RedrawingDisabled;
24817 save_sourcing_name = sourcing_name;
24818 save_sourcing_lnum = sourcing_lnum;
24819 sourcing_lnum = 1;
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024820 /* need space for function name + ("function " + 3) or "[number]" */
24821 len = (save_sourcing_name == NULL ? 0 : STRLEN(save_sourcing_name))
24822 + STRLEN(fp->uf_name) + 20;
24823 sourcing_name = alloc((unsigned)len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024824 if (sourcing_name != NULL)
24825 {
24826 if (save_sourcing_name != NULL
24827 && STRNCMP(save_sourcing_name, "function ", 9) == 0)
Bram Moolenaar1d6328c2015-09-25 17:37:16 +020024828 sprintf((char *)sourcing_name, "%s[%d]..",
24829 save_sourcing_name, (int)save_sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024830 else
24831 STRCPY(sourcing_name, "function ");
24832 cat_func_name(sourcing_name + STRLEN(sourcing_name), fp);
24833
24834 if (p_verbose >= 12)
24835 {
24836 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024837 verbose_enter_scroll();
24838
Bram Moolenaar555b2802005-05-19 21:08:39 +000024839 smsg((char_u *)_("calling %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024840 if (p_verbose >= 14)
24841 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024842 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024843 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024844 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024845 char_u *s;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024846
24847 msg_puts((char_u *)"(");
24848 for (i = 0; i < argcount; ++i)
24849 {
24850 if (i > 0)
24851 msg_puts((char_u *)", ");
Bram Moolenaar49cd9572005-01-03 21:06:01 +000024852 if (argvars[i].v_type == VAR_NUMBER)
24853 msg_outnum((long)argvars[i].vval.v_number);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024854 else
24855 {
Bram Moolenaar8502c702014-06-17 12:51:16 +020024856 /* Do not want errors such as E724 here. */
24857 ++emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024858 s = tv2string(&argvars[i], &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024859 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024860 if (s != NULL)
24861 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024862 if (vim_strsize(s) > MSG_BUF_CLEN)
24863 {
24864 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24865 s = buf;
24866 }
24867 msg_puts(s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024868 vim_free(tofree);
24869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024870 }
24871 }
24872 msg_puts((char_u *)")");
24873 }
24874 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024875
24876 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024877 --no_wait_return;
24878 }
24879 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000024880#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024881 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024882 {
24883 if (!fp->uf_profiling && has_profiling(FALSE, fp->uf_name, NULL))
24884 func_do_profile(fp);
24885 if (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024886 || (fc->caller != NULL && fc->caller->func->uf_profiling))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024887 {
24888 ++fp->uf_tm_count;
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024889 profile_start(&call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024890 profile_zero(&fp->uf_tm_children);
24891 }
24892 script_prof_save(&wait_start);
24893 }
24894#endif
24895
Bram Moolenaar071d4272004-06-13 20:20:40 +000024896 save_current_SID = current_SID;
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024897 current_SID = fp->uf_script_ID;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024898 save_did_emsg = did_emsg;
24899 did_emsg = FALSE;
24900
24901 /* call do_cmdline() to execute the lines */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024902 do_cmdline(NULL, get_func_line, (void *)fc,
Bram Moolenaar071d4272004-06-13 20:20:40 +000024903 DOCMD_NOWAIT|DOCMD_VERBOSE|DOCMD_REPEAT);
24904
24905 --RedrawingDisabled;
24906
24907 /* when the function was aborted because of an error, return -1 */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000024908 if ((did_emsg && (fp->uf_flags & FC_ABORT)) || rettv->v_type == VAR_UNKNOWN)
Bram Moolenaar071d4272004-06-13 20:20:40 +000024909 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000024910 clear_tv(rettv);
24911 rettv->v_type = VAR_NUMBER;
24912 rettv->vval.v_number = -1;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024913 }
24914
Bram Moolenaar05159a02005-02-26 23:04:13 +000024915#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024916 if (do_profiling == PROF_YES && (fp->uf_profiling
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024917 || (fc->caller != NULL && fc->caller->func->uf_profiling)))
Bram Moolenaar05159a02005-02-26 23:04:13 +000024918 {
Bram Moolenaare9da72e2006-11-01 17:34:40 +000024919 profile_end(&call_start);
24920 profile_sub_wait(&wait_start, &call_start);
24921 profile_add(&fp->uf_tm_total, &call_start);
24922 profile_self(&fp->uf_tm_self, &call_start, &fp->uf_tm_children);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024923 if (fc->caller != NULL && fc->caller->func->uf_profiling)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024924 {
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024925 profile_add(&fc->caller->func->uf_tm_children, &call_start);
24926 profile_add(&fc->caller->func->uf_tml_children, &call_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000024927 }
24928 }
24929#endif
24930
Bram Moolenaar071d4272004-06-13 20:20:40 +000024931 /* when being verbose, mention the return value */
24932 if (p_verbose >= 12)
24933 {
Bram Moolenaar071d4272004-06-13 20:20:40 +000024934 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024935 verbose_enter_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024936
Bram Moolenaar071d4272004-06-13 20:20:40 +000024937 if (aborting())
Bram Moolenaar555b2802005-05-19 21:08:39 +000024938 smsg((char_u *)_("%s aborted"), sourcing_name);
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024939 else if (fc->rettv->v_type == VAR_NUMBER)
Bram Moolenaar555b2802005-05-19 21:08:39 +000024940 smsg((char_u *)_("%s returning #%ld"), sourcing_name,
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024941 (long)fc->rettv->vval.v_number);
Bram Moolenaar758711c2005-02-02 23:11:38 +000024942 else
Bram Moolenaar071d4272004-06-13 20:20:40 +000024943 {
Bram Moolenaar758711c2005-02-02 23:11:38 +000024944 char_u buf[MSG_BUF_LEN];
Bram Moolenaar89d40322006-08-29 15:30:07 +000024945 char_u numbuf2[NUMBUFLEN];
Bram Moolenaar758711c2005-02-02 23:11:38 +000024946 char_u *tofree;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024947 char_u *s;
Bram Moolenaar758711c2005-02-02 23:11:38 +000024948
Bram Moolenaar555b2802005-05-19 21:08:39 +000024949 /* The value may be very long. Skip the middle part, so that we
24950 * have some idea how it starts and ends. smsg() would always
Bram Moolenaar8502c702014-06-17 12:51:16 +020024951 * truncate it at the end. Don't want errors such as E724 here. */
24952 ++emsg_off;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024953 s = tv2string(fc->rettv, &tofree, numbuf2, 0);
Bram Moolenaar8502c702014-06-17 12:51:16 +020024954 --emsg_off;
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024955 if (s != NULL)
24956 {
Bram Moolenaarf31b7642012-01-20 20:44:43 +010024957 if (vim_strsize(s) > MSG_BUF_CLEN)
24958 {
24959 trunc_string(s, buf, MSG_BUF_CLEN, MSG_BUF_LEN);
24960 s = buf;
24961 }
24962 smsg((char_u *)_("%s returning %s"), sourcing_name, s);
Bram Moolenaar92c5aba2007-08-14 20:29:31 +000024963 vim_free(tofree);
24964 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000024965 }
24966 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024967
24968 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024969 --no_wait_return;
24970 }
24971
24972 vim_free(sourcing_name);
24973 sourcing_name = save_sourcing_name;
24974 sourcing_lnum = save_sourcing_lnum;
24975 current_SID = save_current_SID;
Bram Moolenaar05159a02005-02-26 23:04:13 +000024976#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000024977 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000024978 script_prof_restore(&wait_start);
24979#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000024980
24981 if (p_verbose >= 12 && sourcing_name != NULL)
24982 {
24983 ++no_wait_return;
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024984 verbose_enter_scroll();
24985
Bram Moolenaar555b2802005-05-19 21:08:39 +000024986 smsg((char_u *)_("continuing in %s"), sourcing_name);
Bram Moolenaar071d4272004-06-13 20:20:40 +000024987 msg_puts((char_u *)"\n"); /* don't overwrite this either */
Bram Moolenaar54ee7752005-05-31 22:22:17 +000024988
24989 verbose_leave_scroll();
Bram Moolenaar071d4272004-06-13 20:20:40 +000024990 --no_wait_return;
24991 }
24992
24993 did_emsg |= save_did_emsg;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024994 current_funccal = fc->caller;
Bram Moolenaar071d4272004-06-13 20:20:40 +000024995 --depth;
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024996
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000024997 /* If the a:000 list and the l: and a: dicts are not referenced we can
24998 * free the funccall_T and what's in it. */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000024999 if (fc->l_varlist.lv_refcount == DO_NOT_FREE_CNT
25000 && fc->l_vars.dv_refcount == DO_NOT_FREE_CNT
25001 && fc->l_avars.dv_refcount == DO_NOT_FREE_CNT)
25002 {
25003 free_funccal(fc, FALSE);
25004 }
25005 else
25006 {
25007 hashitem_T *hi;
25008 listitem_T *li;
25009 int todo;
25010
25011 /* "fc" is still in use. This can happen when returning "a:000" or
25012 * assigning "l:" to a global variable.
25013 * Link "fc" in the list for garbage collection later. */
25014 fc->caller = previous_funccal;
25015 previous_funccal = fc;
25016
25017 /* Make a copy of the a: variables, since we didn't do that above. */
25018 todo = (int)fc->l_avars.dv_hashtab.ht_used;
25019 for (hi = fc->l_avars.dv_hashtab.ht_array; todo > 0; ++hi)
25020 {
25021 if (!HASHITEM_EMPTY(hi))
25022 {
25023 --todo;
25024 v = HI2DI(hi);
25025 copy_tv(&v->di_tv, &v->di_tv);
25026 }
25027 }
25028
25029 /* Make a copy of the a:000 items, since we didn't do that above. */
25030 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25031 copy_tv(&li->li_tv, &li->li_tv);
25032 }
25033}
25034
25035/*
25036 * Return TRUE if items in "fc" do not have "copyID". That means they are not
Bram Moolenaarc0a6fac2009-05-24 11:40:58 +000025037 * referenced from anywhere that is in use.
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025038 */
25039 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025040can_free_funccal(funccall_T *fc, int copyID)
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025041{
25042 return (fc->l_varlist.lv_copyID != copyID
25043 && fc->l_vars.dv_copyID != copyID
25044 && fc->l_avars.dv_copyID != copyID);
25045}
25046
25047/*
25048 * Free "fc" and what it contains.
25049 */
25050 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025051free_funccal(
25052 funccall_T *fc,
25053 int free_val) /* a: vars were allocated */
Bram Moolenaar8ba1bd22008-12-23 22:52:58 +000025054{
25055 listitem_T *li;
25056
25057 /* The a: variables typevals may not have been allocated, only free the
25058 * allocated variables. */
25059 vars_clear_ext(&fc->l_avars.dv_hashtab, free_val);
25060
25061 /* free all l: variables */
25062 vars_clear(&fc->l_vars.dv_hashtab);
25063
25064 /* Free the a:000 variables if they were allocated. */
25065 if (free_val)
25066 for (li = fc->l_varlist.lv_first; li != NULL; li = li->li_next)
25067 clear_tv(&li->li_tv);
25068
25069 vim_free(fc);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025070}
25071
25072/*
Bram Moolenaar33570922005-01-25 22:26:29 +000025073 * Add a number variable "name" to dict "dp" with value "nr".
25074 */
25075 static void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025076add_nr_var(
25077 dict_T *dp,
25078 dictitem_T *v,
25079 char *name,
25080 varnumber_T nr)
Bram Moolenaar33570922005-01-25 22:26:29 +000025081{
25082 STRCPY(v->di_key, name);
25083 v->di_flags = DI_FLAGS_RO | DI_FLAGS_FIX;
25084 hash_add(&dp->dv_hashtab, DI2HIKEY(v));
25085 v->di_tv.v_type = VAR_NUMBER;
Bram Moolenaar2e6aff32005-01-31 19:25:36 +000025086 v->di_tv.v_lock = VAR_FIXED;
Bram Moolenaar33570922005-01-25 22:26:29 +000025087 v->di_tv.vval.v_number = nr;
25088}
25089
25090/*
Bram Moolenaar071d4272004-06-13 20:20:40 +000025091 * ":return [expr]"
25092 */
25093 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025094ex_return(exarg_T *eap)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025095{
25096 char_u *arg = eap->arg;
Bram Moolenaar33570922005-01-25 22:26:29 +000025097 typval_T rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025098 int returning = FALSE;
25099
25100 if (current_funccal == NULL)
25101 {
25102 EMSG(_("E133: :return not inside a function"));
25103 return;
25104 }
25105
25106 if (eap->skip)
25107 ++emsg_skip;
25108
25109 eap->nextcmd = NULL;
25110 if ((*arg != NUL && *arg != '|' && *arg != '\n')
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025111 && eval0(arg, &rettv, &eap->nextcmd, !eap->skip) != FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025112 {
25113 if (!eap->skip)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025114 returning = do_return(eap, FALSE, TRUE, &rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025115 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025116 clear_tv(&rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025117 }
25118 /* It's safer to return also on error. */
25119 else if (!eap->skip)
25120 {
25121 /*
25122 * Return unless the expression evaluation has been cancelled due to an
25123 * aborting error, an interrupt, or an exception.
25124 */
25125 if (!aborting())
25126 returning = do_return(eap, FALSE, TRUE, NULL);
25127 }
25128
25129 /* When skipping or the return gets pending, advance to the next command
25130 * in this line (!returning). Otherwise, ignore the rest of the line.
25131 * Following lines will be ignored by get_func_line(). */
25132 if (returning)
25133 eap->nextcmd = NULL;
25134 else if (eap->nextcmd == NULL) /* no argument */
25135 eap->nextcmd = check_nextcmd(arg);
25136
25137 if (eap->skip)
25138 --emsg_skip;
25139}
25140
25141/*
25142 * Return from a function. Possibly makes the return pending. Also called
25143 * for a pending return at the ":endtry" or after returning from an extra
25144 * do_cmdline(). "reanimate" is used in the latter case. "is_cmd" is set
Bram Moolenaar33570922005-01-25 22:26:29 +000025145 * when called due to a ":return" command. "rettv" may point to a typval_T
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025146 * with the return rettv. Returns TRUE when the return can be carried out,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025147 * FALSE when the return gets pending.
25148 */
25149 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025150do_return(
25151 exarg_T *eap,
25152 int reanimate,
25153 int is_cmd,
25154 void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025155{
25156 int idx;
25157 struct condstack *cstack = eap->cstack;
25158
25159 if (reanimate)
25160 /* Undo the return. */
25161 current_funccal->returned = FALSE;
25162
25163 /*
25164 * Cleanup (and inactivate) conditionals, but stop when a try conditional
25165 * not in its finally clause (which then is to be executed next) is found.
25166 * In this case, make the ":return" pending for execution at the ":endtry".
25167 * Otherwise, return normally.
25168 */
25169 idx = cleanup_conditionals(eap->cstack, 0, TRUE);
25170 if (idx >= 0)
25171 {
25172 cstack->cs_pending[idx] = CSTP_RETURN;
25173
25174 if (!is_cmd && !reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025175 /* A pending return again gets pending. "rettv" points to an
25176 * allocated variable with the rettv of the original ":return"'s
Bram Moolenaar071d4272004-06-13 20:20:40 +000025177 * argument if present or is NULL else. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025178 cstack->cs_rettv[idx] = rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025179 else
25180 {
25181 /* When undoing a return in order to make it pending, get the stored
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025182 * return rettv. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025183 if (reanimate)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025184 rettv = current_funccal->rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025185
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025186 if (rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025187 {
25188 /* Store the value of the pending return. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025189 if ((cstack->cs_rettv[idx] = alloc_tv()) != NULL)
Bram Moolenaar33570922005-01-25 22:26:29 +000025190 *(typval_T *)cstack->cs_rettv[idx] = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025191 else
25192 EMSG(_(e_outofmem));
25193 }
25194 else
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025195 cstack->cs_rettv[idx] = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025196
25197 if (reanimate)
25198 {
25199 /* The pending return value could be overwritten by a ":return"
25200 * without argument in a finally clause; reset the default
25201 * return value. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025202 current_funccal->rettv->v_type = VAR_NUMBER;
25203 current_funccal->rettv->vval.v_number = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025204 }
25205 }
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025206 report_make_pending(CSTP_RETURN, rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025207 }
25208 else
25209 {
25210 current_funccal->returned = TRUE;
25211
25212 /* If the return is carried out now, store the return value. For
25213 * a return immediately after reanimation, the value is already
25214 * there. */
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025215 if (!reanimate && rettv != NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025216 {
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025217 clear_tv(current_funccal->rettv);
Bram Moolenaar33570922005-01-25 22:26:29 +000025218 *current_funccal->rettv = *(typval_T *)rettv;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025219 if (!is_cmd)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025220 vim_free(rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025221 }
25222 }
25223
25224 return idx < 0;
25225}
25226
25227/*
25228 * Free the variable with a pending return value.
25229 */
25230 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025231discard_pending_return(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025232{
Bram Moolenaar33570922005-01-25 22:26:29 +000025233 free_tv((typval_T *)rettv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025234}
25235
25236/*
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025237 * Generate a return command for producing the value of "rettv". The result
Bram Moolenaar071d4272004-06-13 20:20:40 +000025238 * is an allocated string. Used by report_pending() for verbose messages.
25239 */
25240 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025241get_return_cmd(void *rettv)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025242{
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025243 char_u *s = NULL;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025244 char_u *tofree = NULL;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025245 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025246
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025247 if (rettv != NULL)
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025248 s = echo_string((typval_T *)rettv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025249 if (s == NULL)
25250 s = (char_u *)"";
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025251
25252 STRCPY(IObuff, ":return ");
25253 STRNCPY(IObuff + 8, s, IOSIZE - 8);
25254 if (STRLEN(s) + 8 >= IOSIZE)
25255 STRCPY(IObuff + IOSIZE - 4, "...");
25256 vim_free(tofree);
25257 return vim_strsave(IObuff);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025258}
25259
25260/*
25261 * Get next function line.
25262 * Called by do_cmdline() to get the next line.
25263 * Returns allocated string, or NULL for end of function.
25264 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025265 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010025266get_func_line(
25267 int c UNUSED,
25268 void *cookie,
25269 int indent UNUSED)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025270{
Bram Moolenaar33570922005-01-25 22:26:29 +000025271 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025272 ufunc_T *fp = fcp->func;
25273 char_u *retval;
25274 garray_T *gap; /* growarray with function lines */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025275
25276 /* If breakpoints have been added/deleted need to check for it. */
25277 if (fcp->dbg_tick != debug_tick)
25278 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025279 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025280 sourcing_lnum);
25281 fcp->dbg_tick = debug_tick;
25282 }
Bram Moolenaar05159a02005-02-26 23:04:13 +000025283#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025284 if (do_profiling == PROF_YES)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025285 func_line_end(cookie);
25286#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025287
Bram Moolenaar05159a02005-02-26 23:04:13 +000025288 gap = &fp->uf_lines;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025289 if (((fp->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
25290 || fcp->returned)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025291 retval = NULL;
25292 else
25293 {
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025294 /* Skip NULL lines (continuation lines). */
25295 while (fcp->linenr < gap->ga_len
25296 && ((char_u **)(gap->ga_data))[fcp->linenr] == NULL)
25297 ++fcp->linenr;
25298 if (fcp->linenr >= gap->ga_len)
25299 retval = NULL;
25300 else
25301 {
25302 retval = vim_strsave(((char_u **)(gap->ga_data))[fcp->linenr++]);
25303 sourcing_lnum = fcp->linenr;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025304#ifdef FEAT_PROFILE
Bram Moolenaarb3656ed2006-03-20 21:59:49 +000025305 if (do_profiling == PROF_YES)
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025306 func_line_start(cookie);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025307#endif
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025308 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025309 }
25310
25311 /* Did we encounter a breakpoint? */
25312 if (fcp->breakpoint != 0 && fcp->breakpoint <= sourcing_lnum)
25313 {
Bram Moolenaar05159a02005-02-26 23:04:13 +000025314 dbg_breakpoint(fp->uf_name, sourcing_lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025315 /* Find next breakpoint. */
Bram Moolenaar05159a02005-02-26 23:04:13 +000025316 fcp->breakpoint = dbg_find_breakpoint(FALSE, fp->uf_name,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025317 sourcing_lnum);
25318 fcp->dbg_tick = debug_tick;
25319 }
25320
25321 return retval;
25322}
25323
Bram Moolenaar05159a02005-02-26 23:04:13 +000025324#if defined(FEAT_PROFILE) || defined(PROTO)
25325/*
25326 * Called when starting to read a function line.
25327 * "sourcing_lnum" must be correct!
25328 * When skipping lines it may not actually be executed, but we won't find out
25329 * until later and we need to store the time now.
25330 */
25331 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025332func_line_start(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025333{
25334 funccall_T *fcp = (funccall_T *)cookie;
25335 ufunc_T *fp = fcp->func;
25336
25337 if (fp->uf_profiling && sourcing_lnum >= 1
25338 && sourcing_lnum <= fp->uf_lines.ga_len)
25339 {
25340 fp->uf_tml_idx = sourcing_lnum - 1;
Bram Moolenaar5e3cb7e2006-02-27 23:58:35 +000025341 /* Skip continuation lines. */
25342 while (fp->uf_tml_idx > 0 && FUNCLINE(fp, fp->uf_tml_idx) == NULL)
25343 --fp->uf_tml_idx;
Bram Moolenaar05159a02005-02-26 23:04:13 +000025344 fp->uf_tml_execed = FALSE;
25345 profile_start(&fp->uf_tml_start);
25346 profile_zero(&fp->uf_tml_children);
25347 profile_get_wait(&fp->uf_tml_wait);
25348 }
25349}
25350
25351/*
25352 * Called when actually executing a function line.
25353 */
25354 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025355func_line_exec(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025356{
25357 funccall_T *fcp = (funccall_T *)cookie;
25358 ufunc_T *fp = fcp->func;
25359
25360 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25361 fp->uf_tml_execed = TRUE;
25362}
25363
25364/*
25365 * Called when done with a function line.
25366 */
25367 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025368func_line_end(void *cookie)
Bram Moolenaar05159a02005-02-26 23:04:13 +000025369{
25370 funccall_T *fcp = (funccall_T *)cookie;
25371 ufunc_T *fp = fcp->func;
25372
25373 if (fp->uf_profiling && fp->uf_tml_idx >= 0)
25374 {
25375 if (fp->uf_tml_execed)
25376 {
25377 ++fp->uf_tml_count[fp->uf_tml_idx];
25378 profile_end(&fp->uf_tml_start);
25379 profile_sub_wait(&fp->uf_tml_wait, &fp->uf_tml_start);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025380 profile_add(&fp->uf_tml_total[fp->uf_tml_idx], &fp->uf_tml_start);
Bram Moolenaar1056d982006-03-09 22:37:52 +000025381 profile_self(&fp->uf_tml_self[fp->uf_tml_idx], &fp->uf_tml_start,
25382 &fp->uf_tml_children);
Bram Moolenaar05159a02005-02-26 23:04:13 +000025383 }
25384 fp->uf_tml_idx = -1;
25385 }
25386}
25387#endif
25388
Bram Moolenaar071d4272004-06-13 20:20:40 +000025389/*
25390 * Return TRUE if the currently active function should be ended, because a
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025391 * return was encountered or an error occurred. Used inside a ":while".
Bram Moolenaar071d4272004-06-13 20:20:40 +000025392 */
25393 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025394func_has_ended(void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025395{
Bram Moolenaar33570922005-01-25 22:26:29 +000025396 funccall_T *fcp = (funccall_T *)cookie;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025397
25398 /* Ignore the "abort" flag if the abortion behavior has been changed due to
25399 * an error inside a try conditional. */
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025400 return (((fcp->func->uf_flags & FC_ABORT) && did_emsg && !aborted_in_try())
Bram Moolenaar071d4272004-06-13 20:20:40 +000025401 || fcp->returned);
25402}
25403
25404/*
25405 * return TRUE if cookie indicates a function which "abort"s on errors.
25406 */
25407 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025408func_has_abort(
25409 void *cookie)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025410{
Bram Moolenaar5313dcb2005-02-22 08:56:13 +000025411 return ((funccall_T *)cookie)->func->uf_flags & FC_ABORT;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025412}
25413
25414#if defined(FEAT_VIMINFO) || defined(FEAT_SESSION)
25415typedef enum
25416{
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025417 VAR_FLAVOUR_DEFAULT, /* doesn't start with uppercase */
25418 VAR_FLAVOUR_SESSION, /* starts with uppercase, some lower */
25419 VAR_FLAVOUR_VIMINFO /* all uppercase */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025420} var_flavour_T;
25421
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025422static var_flavour_T var_flavour(char_u *varname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025423
25424 static var_flavour_T
Bram Moolenaar7454a062016-01-30 15:14:10 +010025425var_flavour(char_u *varname)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025426{
25427 char_u *p = varname;
25428
25429 if (ASCII_ISUPPER(*p))
25430 {
25431 while (*(++p))
25432 if (ASCII_ISLOWER(*p))
25433 return VAR_FLAVOUR_SESSION;
25434 return VAR_FLAVOUR_VIMINFO;
25435 }
25436 else
25437 return VAR_FLAVOUR_DEFAULT;
25438}
25439#endif
25440
25441#if defined(FEAT_VIMINFO) || defined(PROTO)
25442/*
25443 * Restore global vars that start with a capital from the viminfo file
25444 */
25445 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025446read_viminfo_varlist(vir_T *virp, int writing)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025447{
25448 char_u *tab;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025449 int type = VAR_NUMBER;
Bram Moolenaar33570922005-01-25 22:26:29 +000025450 typval_T tv;
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025451 funccall_T *save_funccal;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025452
25453 if (!writing && (find_viminfo_parameter('!') != NULL))
25454 {
25455 tab = vim_strchr(virp->vir_line + 1, '\t');
25456 if (tab != NULL)
25457 {
25458 *tab++ = '\0'; /* isolate the variable name */
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025459 switch (*tab)
25460 {
25461 case 'S': type = VAR_STRING; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025462#ifdef FEAT_FLOAT
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025463 case 'F': type = VAR_FLOAT; break;
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025464#endif
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025465 case 'D': type = VAR_DICT; break;
25466 case 'L': type = VAR_LIST; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025467 case 'X': type = VAR_SPECIAL; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025468 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025469
25470 tab = vim_strchr(tab, '\t');
25471 if (tab != NULL)
25472 {
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025473 tv.v_type = type;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025474 if (type == VAR_STRING || type == VAR_DICT || type == VAR_LIST)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025475 tv.vval.v_string = viminfo_readstring(virp,
Bram Moolenaar071d4272004-06-13 20:20:40 +000025476 (int)(tab - virp->vir_line + 1), TRUE);
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025477#ifdef FEAT_FLOAT
25478 else if (type == VAR_FLOAT)
25479 (void)string2float(tab + 1, &tv.vval.v_float);
25480#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025481 else
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025482 tv.vval.v_number = atol((char *)tab + 1);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025483 if (type == VAR_DICT || type == VAR_LIST)
25484 {
25485 typval_T *etv = eval_expr(tv.vval.v_string, NULL);
25486
25487 if (etv == NULL)
25488 /* Failed to parse back the dict or list, use it as a
25489 * string. */
25490 tv.v_type = VAR_STRING;
25491 else
25492 {
25493 vim_free(tv.vval.v_string);
25494 tv = *etv;
Bram Moolenaar507cc8a2012-03-23 15:37:02 +010025495 vim_free(etv);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025496 }
25497 }
25498
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025499 /* when in a function use global variables */
25500 save_funccal = current_funccal;
25501 current_funccal = NULL;
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025502 set_var(virp->vir_line + 1, &tv, FALSE);
Bram Moolenaarb20e3342016-01-18 23:29:01 +010025503 current_funccal = save_funccal;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025504
25505 if (tv.v_type == VAR_STRING)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025506 vim_free(tv.vval.v_string);
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025507 else if (tv.v_type == VAR_DICT || tv.v_type == VAR_LIST)
25508 clear_tv(&tv);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025509 }
25510 }
25511 }
25512
25513 return viminfo_readline(virp);
25514}
25515
25516/*
25517 * Write global vars that start with a capital to the viminfo file
25518 */
25519 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025520write_viminfo_varlist(FILE *fp)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025521{
Bram Moolenaar33570922005-01-25 22:26:29 +000025522 hashitem_T *hi;
25523 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025524 int todo;
Bram Moolenaar2fc83fc2016-02-08 22:57:24 +010025525 char *s = "";
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025526 char_u *p;
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025527 char_u *tofree;
Bram Moolenaar8a283e52005-01-06 23:28:25 +000025528 char_u numbuf[NUMBUFLEN];
Bram Moolenaar071d4272004-06-13 20:20:40 +000025529
25530 if (find_viminfo_parameter('!') == NULL)
25531 return;
25532
Bram Moolenaar9577c3e2010-05-14 12:16:25 +020025533 fputs(_("\n# global variables:\n"), fp);
Bram Moolenaara7043832005-01-21 11:56:39 +000025534
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025535 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025536 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025537 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025538 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025539 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025540 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025541 this_var = HI2DI(hi);
25542 if (var_flavour(this_var->di_key) == VAR_FLAVOUR_VIMINFO)
Bram Moolenaarc70646c2005-01-04 21:52:38 +000025543 {
Bram Moolenaar33570922005-01-25 22:26:29 +000025544 switch (this_var->di_tv.v_type)
Bram Moolenaara7043832005-01-21 11:56:39 +000025545 {
25546 case VAR_STRING: s = "STR"; break;
25547 case VAR_NUMBER: s = "NUM"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025548 case VAR_FLOAT: s = "FLO"; break;
Bram Moolenaar680eeca2010-10-20 17:44:42 +020025549 case VAR_DICT: s = "DIC"; break;
25550 case VAR_LIST: s = "LIS"; break;
Bram Moolenaara03f2332016-02-06 18:09:59 +010025551 case VAR_SPECIAL: s = "XPL"; break;
25552
25553 case VAR_UNKNOWN:
25554 case VAR_FUNC:
Bram Moolenaar1735bc92016-03-14 23:05:14 +010025555 case VAR_PARTIAL:
Bram Moolenaar835dc632016-02-07 14:27:38 +010025556 case VAR_JOB:
Bram Moolenaar77073442016-02-13 23:23:53 +010025557 case VAR_CHANNEL:
Bram Moolenaara03f2332016-02-06 18:09:59 +010025558 continue;
Bram Moolenaara7043832005-01-21 11:56:39 +000025559 }
Bram Moolenaar33570922005-01-25 22:26:29 +000025560 fprintf(fp, "!%s\t%s\t", this_var->di_key, s);
Bram Moolenaarb71eaae2006-01-20 23:10:18 +000025561 p = echo_string(&this_var->di_tv, &tofree, numbuf, 0);
Bram Moolenaar81bf7082005-02-12 14:31:42 +000025562 if (p != NULL)
25563 viminfo_writestring(fp, p);
Bram Moolenaara7043832005-01-21 11:56:39 +000025564 vim_free(tofree);
25565 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025566 }
25567 }
25568}
25569#endif
25570
25571#if defined(FEAT_SESSION) || defined(PROTO)
25572 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025573store_session_globals(FILE *fd)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025574{
Bram Moolenaar33570922005-01-25 22:26:29 +000025575 hashitem_T *hi;
25576 dictitem_T *this_var;
Bram Moolenaara7043832005-01-21 11:56:39 +000025577 int todo;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025578 char_u *p, *t;
25579
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025580 todo = (int)globvarht.ht_used;
Bram Moolenaar33570922005-01-25 22:26:29 +000025581 for (hi = globvarht.ht_array; todo > 0; ++hi)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025582 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025583 if (!HASHITEM_EMPTY(hi))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025584 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025585 --todo;
Bram Moolenaar33570922005-01-25 22:26:29 +000025586 this_var = HI2DI(hi);
25587 if ((this_var->di_tv.v_type == VAR_NUMBER
25588 || this_var->di_tv.v_type == VAR_STRING)
25589 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
Bram Moolenaar3d60ec22005-01-05 22:19:46 +000025590 {
Bram Moolenaara7043832005-01-21 11:56:39 +000025591 /* Escape special characters with a backslash. Turn a LF and
25592 * CR into \n and \r. */
Bram Moolenaar33570922005-01-25 22:26:29 +000025593 p = vim_strsave_escaped(get_tv_string(&this_var->di_tv),
Bram Moolenaara7043832005-01-21 11:56:39 +000025594 (char_u *)"\\\"\n\r");
25595 if (p == NULL) /* out of memory */
25596 break;
25597 for (t = p; *t != NUL; ++t)
25598 if (*t == '\n')
25599 *t = 'n';
25600 else if (*t == '\r')
25601 *t = 'r';
25602 if ((fprintf(fd, "let %s = %c%s%c",
Bram Moolenaar33570922005-01-25 22:26:29 +000025603 this_var->di_key,
25604 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25605 : ' ',
25606 p,
25607 (this_var->di_tv.v_type == VAR_STRING) ? '"'
25608 : ' ') < 0)
Bram Moolenaara7043832005-01-21 11:56:39 +000025609 || put_eol(fd) == FAIL)
25610 {
25611 vim_free(p);
25612 return FAIL;
25613 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025614 vim_free(p);
25615 }
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025616#ifdef FEAT_FLOAT
25617 else if (this_var->di_tv.v_type == VAR_FLOAT
25618 && var_flavour(this_var->di_key) == VAR_FLAVOUR_SESSION)
25619 {
25620 float_T f = this_var->di_tv.vval.v_float;
25621 int sign = ' ';
25622
25623 if (f < 0)
25624 {
25625 f = -f;
25626 sign = '-';
25627 }
Bram Moolenaar2b04b192012-01-26 11:45:30 +010025628 if ((fprintf(fd, "let %s = %c%f",
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025629 this_var->di_key, sign, f) < 0)
25630 || put_eol(fd) == FAIL)
25631 return FAIL;
25632 }
25633#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025634 }
25635 }
25636 return OK;
25637}
25638#endif
25639
Bram Moolenaar661b1822005-07-28 22:36:45 +000025640/*
25641 * Display script name where an item was last set.
25642 * Should only be invoked when 'verbose' is non-zero.
25643 */
25644 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025645last_set_msg(scid_T scriptID)
Bram Moolenaar661b1822005-07-28 22:36:45 +000025646{
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025647 char_u *p;
25648
Bram Moolenaar661b1822005-07-28 22:36:45 +000025649 if (scriptID != 0)
25650 {
Bram Moolenaarcafda4f2005-09-06 19:25:11 +000025651 p = home_replace_save(NULL, get_scriptname(scriptID));
25652 if (p != NULL)
25653 {
25654 verbose_enter();
25655 MSG_PUTS(_("\n\tLast set from "));
25656 MSG_PUTS(p);
25657 vim_free(p);
25658 verbose_leave();
25659 }
Bram Moolenaar661b1822005-07-28 22:36:45 +000025660 }
25661}
25662
Bram Moolenaard812df62008-11-09 12:46:09 +000025663/*
25664 * List v:oldfiles in a nice way.
25665 */
Bram Moolenaard812df62008-11-09 12:46:09 +000025666 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025667ex_oldfiles(exarg_T *eap UNUSED)
Bram Moolenaard812df62008-11-09 12:46:09 +000025668{
25669 list_T *l = vimvars[VV_OLDFILES].vv_list;
25670 listitem_T *li;
25671 int nr = 0;
25672
25673 if (l == NULL)
25674 msg((char_u *)_("No old files"));
25675 else
25676 {
25677 msg_start();
25678 msg_scroll = TRUE;
25679 for (li = l->lv_first; li != NULL && !got_int; li = li->li_next)
25680 {
25681 msg_outnum((long)++nr);
25682 MSG_PUTS(": ");
25683 msg_outtrans(get_tv_string(&li->li_tv));
25684 msg_putchar('\n');
25685 out_flush(); /* output one line at a time */
25686 ui_breakcheck();
25687 }
25688 /* Assume "got_int" was set to truncate the listing. */
25689 got_int = FALSE;
25690
25691#ifdef FEAT_BROWSE_CMD
25692 if (cmdmod.browse)
25693 {
25694 quit_more = FALSE;
25695 nr = prompt_for_number(FALSE);
25696 msg_starthere();
25697 if (nr > 0)
25698 {
25699 char_u *p = list_find_str(get_vim_var_list(VV_OLDFILES),
25700 (long)nr);
25701
25702 if (p != NULL)
25703 {
25704 p = expand_env_save(p);
25705 eap->arg = p;
25706 eap->cmdidx = CMD_edit;
25707 cmdmod.browse = FALSE;
25708 do_exedit(eap, NULL);
25709 vim_free(p);
25710 }
25711 }
25712 }
25713#endif
25714 }
25715}
25716
Bram Moolenaar53744302015-07-17 17:38:22 +020025717/* reset v:option_new, v:option_old and v:option_type */
25718 void
Bram Moolenaar7454a062016-01-30 15:14:10 +010025719reset_v_option_vars(void)
Bram Moolenaar53744302015-07-17 17:38:22 +020025720{
25721 set_vim_var_string(VV_OPTION_NEW, NULL, -1);
25722 set_vim_var_string(VV_OPTION_OLD, NULL, -1);
25723 set_vim_var_string(VV_OPTION_TYPE, NULL, -1);
25724}
25725
25726
Bram Moolenaar071d4272004-06-13 20:20:40 +000025727#endif /* FEAT_EVAL */
25728
Bram Moolenaar071d4272004-06-13 20:20:40 +000025729
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025730#if defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025731
25732#ifdef WIN3264
25733/*
25734 * Functions for ":8" filename modifier: get 8.3 version of a filename.
25735 */
Bram Moolenaar48e697e2016-01-23 22:17:30 +010025736static int get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen);
25737static int shortpath_for_invalid_fname(char_u **fname, char_u **bufp, int *fnamelen);
25738static int shortpath_for_partial(char_u **fnamep, char_u **bufp, int *fnamelen);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025739
25740/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025741 * Get the short path (8.3) for the filename in "fnamep".
25742 * Only works for a valid file name.
25743 * When the path gets longer "fnamep" is changed and the allocated buffer
25744 * is put in "bufp".
25745 * *fnamelen is the length of "fnamep" and set to 0 for a nonexistent path.
25746 * Returns OK on success, FAIL on failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025747 */
25748 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025749get_short_pathname(char_u **fnamep, char_u **bufp, int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025750{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025751 int l, len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025752 char_u *newbuf;
25753
25754 len = *fnamelen;
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025755 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025756 if (l > len - 1)
25757 {
25758 /* If that doesn't work (not enough space), then save the string
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025759 * and try again with a new buffer big enough. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025760 newbuf = vim_strnsave(*fnamep, l);
25761 if (newbuf == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025762 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025763
25764 vim_free(*bufp);
25765 *fnamep = *bufp = newbuf;
25766
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025767 /* Really should always succeed, as the buffer is big enough. */
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010025768 l = GetShortPathName((LPSTR)*fnamep, (LPSTR)*fnamep, l+1);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025769 }
25770
25771 *fnamelen = l;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025772 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025773}
25774
25775/*
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025776 * Get the short path (8.3) for the filename in "fname". The converted
25777 * path is returned in "bufp".
25778 *
25779 * Some of the directories specified in "fname" may not exist. This function
25780 * will shorten the existing directories at the beginning of the path and then
25781 * append the remaining non-existing path.
25782 *
25783 * fname - Pointer to the filename to shorten. On return, contains the
Bram Moolenaar2c704a72010-06-03 21:17:25 +020025784 * pointer to the shortened pathname
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025785 * bufp - Pointer to an allocated buffer for the filename.
25786 * fnamelen - Length of the filename pointed to by fname
25787 *
25788 * Returns OK on success (or nothing done) and FAIL on failure (out of memory).
Bram Moolenaar071d4272004-06-13 20:20:40 +000025789 */
25790 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025791shortpath_for_invalid_fname(
25792 char_u **fname,
25793 char_u **bufp,
25794 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025795{
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025796 char_u *short_fname, *save_fname, *pbuf_unused;
25797 char_u *endp, *save_endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025798 char_u ch;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025799 int old_len, len;
25800 int new_len, sfx_len;
25801 int retval = OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025802
25803 /* Make a copy */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025804 old_len = *fnamelen;
25805 save_fname = vim_strnsave(*fname, old_len);
25806 pbuf_unused = NULL;
25807 short_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025808
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025809 endp = save_fname + old_len - 1; /* Find the end of the copy */
25810 save_endp = endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025811
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025812 /*
25813 * Try shortening the supplied path till it succeeds by removing one
25814 * directory at a time from the tail of the path.
25815 */
25816 len = 0;
25817 for (;;)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025818 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025819 /* go back one path-separator */
25820 while (endp > save_fname && !after_pathsep(save_fname, endp + 1))
25821 --endp;
25822 if (endp <= save_fname)
25823 break; /* processed the complete path */
25824
25825 /*
25826 * Replace the path separator with a NUL and try to shorten the
25827 * resulting path.
25828 */
25829 ch = *endp;
25830 *endp = 0;
25831 short_fname = save_fname;
Bram Moolenaarc236c162008-07-13 17:41:49 +000025832 len = (int)STRLEN(short_fname) + 1;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025833 if (get_short_pathname(&short_fname, &pbuf_unused, &len) == FAIL)
25834 {
25835 retval = FAIL;
25836 goto theend;
25837 }
25838 *endp = ch; /* preserve the string */
25839
25840 if (len > 0)
25841 break; /* successfully shortened the path */
25842
25843 /* failed to shorten the path. Skip the path separator */
25844 --endp;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025845 }
25846
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025847 if (len > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025848 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025849 /*
25850 * Succeeded in shortening the path. Now concatenate the shortened
25851 * path with the remaining path at the tail.
25852 */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025853
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025854 /* Compute the length of the new path. */
25855 sfx_len = (int)(save_endp - endp) + 1;
25856 new_len = len + sfx_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025857
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025858 *fnamelen = new_len;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025859 vim_free(*bufp);
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025860 if (new_len > old_len)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025861 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025862 /* There is not enough space in the currently allocated string,
25863 * copy it to a buffer big enough. */
25864 *fname = *bufp = vim_strnsave(short_fname, new_len);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025865 if (*fname == NULL)
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025866 {
25867 retval = FAIL;
25868 goto theend;
25869 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025870 }
25871 else
25872 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025873 /* Transfer short_fname to the main buffer (it's big enough),
25874 * unless get_short_pathname() did its work in-place. */
25875 *fname = *bufp = save_fname;
25876 if (short_fname != save_fname)
25877 vim_strncpy(save_fname, short_fname, len);
25878 save_fname = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025879 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025880
25881 /* concat the not-shortened part of the path */
25882 vim_strncpy(*fname + len, endp, sfx_len);
25883 (*fname)[new_len] = NUL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025884 }
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025885
25886theend:
25887 vim_free(pbuf_unused);
25888 vim_free(save_fname);
25889
25890 return retval;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025891}
25892
25893/*
25894 * Get a pathname for a partial path.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025895 * Returns OK for success, FAIL for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025896 */
25897 static int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025898shortpath_for_partial(
25899 char_u **fnamep,
25900 char_u **bufp,
25901 int *fnamelen)
Bram Moolenaar071d4272004-06-13 20:20:40 +000025902{
25903 int sepcount, len, tflen;
25904 char_u *p;
25905 char_u *pbuf, *tfname;
25906 int hasTilde;
25907
Bram Moolenaar8c8de832008-06-24 22:58:06 +000025908 /* Count up the path separators from the RHS.. so we know which part
25909 * of the path to return. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025910 sepcount = 0;
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025911 for (p = *fnamep; p < *fnamep + *fnamelen; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000025912 if (vim_ispathsep(*p))
25913 ++sepcount;
25914
25915 /* Need full path first (use expand_env() to remove a "~/") */
25916 hasTilde = (**fnamep == '~');
25917 if (hasTilde)
25918 pbuf = tfname = expand_env_save(*fnamep);
25919 else
25920 pbuf = tfname = FullName_save(*fnamep, FALSE);
25921
Bram Moolenaara93fa7e2006-04-17 22:14:47 +000025922 len = tflen = (int)STRLEN(tfname);
Bram Moolenaar071d4272004-06-13 20:20:40 +000025923
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025924 if (get_short_pathname(&tfname, &pbuf, &len) == FAIL)
25925 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025926
25927 if (len == 0)
25928 {
25929 /* Don't have a valid filename, so shorten the rest of the
25930 * path if we can. This CAN give us invalid 8.3 filenames, but
25931 * there's not a lot of point in guessing what it might be.
25932 */
25933 len = tflen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025934 if (shortpath_for_invalid_fname(&tfname, &pbuf, &len) == FAIL)
25935 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025936 }
25937
25938 /* Count the paths backward to find the beginning of the desired string. */
25939 for (p = tfname + len - 1; p >= tfname; --p)
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025940 {
25941#ifdef FEAT_MBYTE
25942 if (has_mbyte)
25943 p -= mb_head_off(tfname, p);
25944#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000025945 if (vim_ispathsep(*p))
25946 {
25947 if (sepcount == 0 || (hasTilde && sepcount == 1))
25948 break;
25949 else
25950 sepcount --;
25951 }
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000025952 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000025953 if (hasTilde)
25954 {
25955 --p;
25956 if (p >= tfname)
25957 *p = '~';
25958 else
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025959 return FAIL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025960 }
25961 else
25962 ++p;
25963
25964 /* Copy in the string - p indexes into tfname - allocated at pbuf */
25965 vim_free(*bufp);
25966 *fnamelen = (int)STRLEN(p);
25967 *bufp = pbuf;
25968 *fnamep = p;
25969
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025970 return OK;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025971}
25972#endif /* WIN3264 */
25973
25974/*
25975 * Adjust a filename, according to a string of modifiers.
25976 * *fnamep must be NUL terminated when called. When returning, the length is
25977 * determined by *fnamelen.
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000025978 * Returns VALID_ flags or -1 for failure.
Bram Moolenaar071d4272004-06-13 20:20:40 +000025979 * When there is an error, *fnamep is set to NULL.
25980 */
25981 int
Bram Moolenaar7454a062016-01-30 15:14:10 +010025982modify_fname(
25983 char_u *src, /* string with modifiers */
25984 int *usedlen, /* characters after src that are used */
25985 char_u **fnamep, /* file name so far */
25986 char_u **bufp, /* buffer for allocated file name or NULL */
25987 int *fnamelen) /* length of fnamep */
Bram Moolenaar071d4272004-06-13 20:20:40 +000025988{
25989 int valid = 0;
25990 char_u *tail;
25991 char_u *s, *p, *pbuf;
25992 char_u dirname[MAXPATHL];
25993 int c;
25994 int has_fullname = 0;
25995#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020025996 char_u *fname_start = *fnamep;
Bram Moolenaar071d4272004-06-13 20:20:40 +000025997 int has_shortname = 0;
25998#endif
25999
26000repeat:
26001 /* ":p" - full path/file_name */
26002 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'p')
26003 {
26004 has_fullname = 1;
26005
26006 valid |= VALID_PATH;
26007 *usedlen += 2;
26008
26009 /* Expand "~/path" for all systems and "~user/path" for Unix and VMS */
26010 if ((*fnamep)[0] == '~'
26011#if !defined(UNIX) && !(defined(VMS) && defined(USER_HOME))
26012 && ((*fnamep)[1] == '/'
26013# ifdef BACKSLASH_IN_FILENAME
26014 || (*fnamep)[1] == '\\'
26015# endif
26016 || (*fnamep)[1] == NUL)
26017
26018#endif
26019 )
26020 {
26021 *fnamep = expand_env_save(*fnamep);
26022 vim_free(*bufp); /* free any allocated file name */
26023 *bufp = *fnamep;
26024 if (*fnamep == NULL)
26025 return -1;
26026 }
26027
26028 /* When "/." or "/.." is used: force expansion to get rid of it. */
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026029 for (p = *fnamep; *p != NUL; mb_ptr_adv(p))
Bram Moolenaar071d4272004-06-13 20:20:40 +000026030 {
26031 if (vim_ispathsep(*p)
26032 && p[1] == '.'
26033 && (p[2] == NUL
26034 || vim_ispathsep(p[2])
26035 || (p[2] == '.'
26036 && (p[3] == NUL || vim_ispathsep(p[3])))))
26037 break;
26038 }
26039
26040 /* FullName_save() is slow, don't use it when not needed. */
26041 if (*p != NUL || !vim_isAbsName(*fnamep))
26042 {
26043 *fnamep = FullName_save(*fnamep, *p != NUL);
26044 vim_free(*bufp); /* free any allocated file name */
26045 *bufp = *fnamep;
26046 if (*fnamep == NULL)
26047 return -1;
26048 }
26049
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026050#ifdef WIN3264
26051# if _WIN32_WINNT >= 0x0500
26052 if (vim_strchr(*fnamep, '~') != NULL)
26053 {
26054 /* Expand 8.3 filename to full path. Needed to make sure the same
26055 * file does not have two different names.
26056 * Note: problem does not occur if _WIN32_WINNT < 0x0500. */
26057 p = alloc(_MAX_PATH + 1);
26058 if (p != NULL)
26059 {
Bram Moolenaar6aa2cd42016-02-16 15:06:59 +010026060 if (GetLongPathName((LPSTR)*fnamep, (LPSTR)p, _MAX_PATH))
Bram Moolenaar9158f9e2012-06-20 14:02:27 +020026061 {
26062 vim_free(*bufp);
26063 *bufp = *fnamep = p;
26064 }
26065 else
26066 vim_free(p);
26067 }
26068 }
26069# endif
26070#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +000026071 /* Append a path separator to a directory. */
26072 if (mch_isdir(*fnamep))
26073 {
26074 /* Make room for one or two extra characters. */
26075 *fnamep = vim_strnsave(*fnamep, (int)STRLEN(*fnamep) + 2);
26076 vim_free(*bufp); /* free any allocated file name */
26077 *bufp = *fnamep;
26078 if (*fnamep == NULL)
26079 return -1;
26080 add_pathsep(*fnamep);
26081 }
26082 }
26083
26084 /* ":." - path relative to the current directory */
26085 /* ":~" - path relative to the home directory */
26086 /* ":8" - shortname path - postponed till after */
26087 while (src[*usedlen] == ':'
26088 && ((c = src[*usedlen + 1]) == '.' || c == '~' || c == '8'))
26089 {
26090 *usedlen += 2;
26091 if (c == '8')
26092 {
26093#ifdef WIN3264
26094 has_shortname = 1; /* Postpone this. */
26095#endif
26096 continue;
26097 }
26098 pbuf = NULL;
26099 /* Need full path first (use expand_env() to remove a "~/") */
26100 if (!has_fullname)
26101 {
26102 if (c == '.' && **fnamep == '~')
26103 p = pbuf = expand_env_save(*fnamep);
26104 else
26105 p = pbuf = FullName_save(*fnamep, FALSE);
26106 }
26107 else
26108 p = *fnamep;
26109
26110 has_fullname = 0;
26111
26112 if (p != NULL)
26113 {
26114 if (c == '.')
26115 {
26116 mch_dirname(dirname, MAXPATHL);
26117 s = shorten_fname(p, dirname);
26118 if (s != NULL)
26119 {
26120 *fnamep = s;
26121 if (pbuf != NULL)
26122 {
26123 vim_free(*bufp); /* free any allocated file name */
26124 *bufp = pbuf;
26125 pbuf = NULL;
26126 }
26127 }
26128 }
26129 else
26130 {
26131 home_replace(NULL, p, dirname, MAXPATHL, TRUE);
26132 /* Only replace it when it starts with '~' */
26133 if (*dirname == '~')
26134 {
26135 s = vim_strsave(dirname);
26136 if (s != NULL)
26137 {
26138 *fnamep = s;
26139 vim_free(*bufp);
26140 *bufp = s;
26141 }
26142 }
26143 }
26144 vim_free(pbuf);
26145 }
26146 }
26147
26148 tail = gettail(*fnamep);
26149 *fnamelen = (int)STRLEN(*fnamep);
26150
26151 /* ":h" - head, remove "/file_name", can be repeated */
26152 /* Don't remove the first "/" or "c:\" */
26153 while (src[*usedlen] == ':' && src[*usedlen + 1] == 'h')
26154 {
26155 valid |= VALID_HEAD;
26156 *usedlen += 2;
26157 s = get_past_head(*fnamep);
Bram Moolenaar1cd871b2004-12-19 22:46:22 +000026158 while (tail > s && after_pathsep(s, tail))
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026159 mb_ptr_back(*fnamep, tail);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026160 *fnamelen = (int)(tail - *fnamep);
26161#ifdef VMS
26162 if (*fnamelen > 0)
26163 *fnamelen += 1; /* the path separator is part of the path */
26164#endif
Bram Moolenaar5461cfe2007-09-25 18:40:14 +000026165 if (*fnamelen == 0)
26166 {
26167 /* Result is empty. Turn it into "." to make ":cd %:h" work. */
26168 p = vim_strsave((char_u *)".");
26169 if (p == NULL)
26170 return -1;
26171 vim_free(*bufp);
26172 *bufp = *fnamep = tail = p;
26173 *fnamelen = 1;
26174 }
26175 else
26176 {
26177 while (tail > s && !after_pathsep(s, tail))
26178 mb_ptr_back(*fnamep, tail);
26179 }
Bram Moolenaar071d4272004-06-13 20:20:40 +000026180 }
26181
26182 /* ":8" - shortname */
26183 if (src[*usedlen] == ':' && src[*usedlen + 1] == '8')
26184 {
26185 *usedlen += 2;
26186#ifdef WIN3264
26187 has_shortname = 1;
26188#endif
26189 }
26190
26191#ifdef WIN3264
Bram Moolenaardc935552011-08-17 15:23:23 +020026192 /*
26193 * Handle ":8" after we have done 'heads' and before we do 'tails'.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026194 */
26195 if (has_shortname)
26196 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026197 /* Copy the string if it is shortened by :h and when it wasn't copied
26198 * yet, because we are going to change it in place. Avoids changing
26199 * the buffer name for "%:8". */
26200 if (*fnamelen < (int)STRLEN(*fnamep) || *fnamep == fname_start)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026201 {
26202 p = vim_strnsave(*fnamep, *fnamelen);
Bram Moolenaardc935552011-08-17 15:23:23 +020026203 if (p == NULL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026204 return -1;
26205 vim_free(*bufp);
26206 *bufp = *fnamep = p;
26207 }
26208
26209 /* Split into two implementations - makes it easier. First is where
Bram Moolenaardc935552011-08-17 15:23:23 +020026210 * there isn't a full name already, second is where there is. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026211 if (!has_fullname && !vim_isAbsName(*fnamep))
26212 {
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026213 if (shortpath_for_partial(fnamep, bufp, fnamelen) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026214 return -1;
26215 }
26216 else
26217 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026218 int l = *fnamelen;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026219
Bram Moolenaardc935552011-08-17 15:23:23 +020026220 /* Simple case, already have the full-name.
Bram Moolenaar071d4272004-06-13 20:20:40 +000026221 * Nearly always shorter, so try first time. */
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026222 if (get_short_pathname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026223 return -1;
26224
26225 if (l == 0)
26226 {
Bram Moolenaardc935552011-08-17 15:23:23 +020026227 /* Couldn't find the filename, search the paths. */
Bram Moolenaar071d4272004-06-13 20:20:40 +000026228 l = *fnamelen;
Bram Moolenaarbcebfb62008-05-29 19:47:13 +000026229 if (shortpath_for_invalid_fname(fnamep, bufp, &l) == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026230 return -1;
26231 }
26232 *fnamelen = l;
26233 }
26234 }
26235#endif /* WIN3264 */
26236
26237 /* ":t" - tail, just the basename */
26238 if (src[*usedlen] == ':' && src[*usedlen + 1] == 't')
26239 {
26240 *usedlen += 2;
26241 *fnamelen -= (int)(tail - *fnamep);
26242 *fnamep = tail;
26243 }
26244
26245 /* ":e" - extension, can be repeated */
26246 /* ":r" - root, without extension, can be repeated */
26247 while (src[*usedlen] == ':'
26248 && (src[*usedlen + 1] == 'e' || src[*usedlen + 1] == 'r'))
26249 {
26250 /* find a '.' in the tail:
26251 * - for second :e: before the current fname
26252 * - otherwise: The last '.'
26253 */
26254 if (src[*usedlen + 1] == 'e' && *fnamep > tail)
26255 s = *fnamep - 2;
26256 else
26257 s = *fnamep + *fnamelen - 1;
26258 for ( ; s > tail; --s)
26259 if (s[0] == '.')
26260 break;
26261 if (src[*usedlen + 1] == 'e') /* :e */
26262 {
26263 if (s > tail)
26264 {
26265 *fnamelen += (int)(*fnamep - (s + 1));
26266 *fnamep = s + 1;
26267#ifdef VMS
26268 /* cut version from the extension */
26269 s = *fnamep + *fnamelen - 1;
26270 for ( ; s > *fnamep; --s)
26271 if (s[0] == ';')
26272 break;
26273 if (s > *fnamep)
26274 *fnamelen = s - *fnamep;
26275#endif
26276 }
26277 else if (*fnamep <= tail)
26278 *fnamelen = 0;
26279 }
26280 else /* :r */
26281 {
26282 if (s > tail) /* remove one extension */
26283 *fnamelen = (int)(s - *fnamep);
26284 }
26285 *usedlen += 2;
26286 }
26287
26288 /* ":s?pat?foo?" - substitute */
26289 /* ":gs?pat?foo?" - global substitute */
26290 if (src[*usedlen] == ':'
26291 && (src[*usedlen + 1] == 's'
26292 || (src[*usedlen + 1] == 'g' && src[*usedlen + 2] == 's')))
26293 {
26294 char_u *str;
26295 char_u *pat;
26296 char_u *sub;
26297 int sep;
26298 char_u *flags;
26299 int didit = FALSE;
26300
26301 flags = (char_u *)"";
26302 s = src + *usedlen + 2;
26303 if (src[*usedlen + 1] == 'g')
26304 {
26305 flags = (char_u *)"g";
26306 ++s;
26307 }
26308
26309 sep = *s++;
26310 if (sep)
26311 {
26312 /* find end of pattern */
26313 p = vim_strchr(s, sep);
26314 if (p != NULL)
26315 {
26316 pat = vim_strnsave(s, (int)(p - s));
26317 if (pat != NULL)
26318 {
26319 s = p + 1;
26320 /* find end of substitution */
26321 p = vim_strchr(s, sep);
26322 if (p != NULL)
26323 {
26324 sub = vim_strnsave(s, (int)(p - s));
26325 str = vim_strnsave(*fnamep, *fnamelen);
26326 if (sub != NULL && str != NULL)
26327 {
26328 *usedlen = (int)(p + 1 - src);
26329 s = do_string_sub(str, pat, sub, flags);
26330 if (s != NULL)
26331 {
26332 *fnamep = s;
26333 *fnamelen = (int)STRLEN(s);
26334 vim_free(*bufp);
26335 *bufp = s;
26336 didit = TRUE;
26337 }
26338 }
26339 vim_free(sub);
26340 vim_free(str);
26341 }
26342 vim_free(pat);
26343 }
26344 }
26345 /* after using ":s", repeat all the modifiers */
26346 if (didit)
26347 goto repeat;
26348 }
26349 }
26350
Bram Moolenaar26df0922014-02-23 23:39:13 +010026351 if (src[*usedlen] == ':' && src[*usedlen + 1] == 'S')
26352 {
26353 p = vim_strsave_shellescape(*fnamep, FALSE, FALSE);
26354 if (p == NULL)
26355 return -1;
26356 vim_free(*bufp);
26357 *bufp = *fnamep = p;
26358 *fnamelen = (int)STRLEN(p);
26359 *usedlen += 2;
26360 }
26361
Bram Moolenaar071d4272004-06-13 20:20:40 +000026362 return valid;
26363}
26364
26365/*
26366 * Perform a substitution on "str" with pattern "pat" and substitute "sub".
26367 * "flags" can be "g" to do a global substitute.
26368 * Returns an allocated string, NULL for error.
26369 */
26370 char_u *
Bram Moolenaar7454a062016-01-30 15:14:10 +010026371do_string_sub(
26372 char_u *str,
26373 char_u *pat,
26374 char_u *sub,
26375 char_u *flags)
Bram Moolenaar071d4272004-06-13 20:20:40 +000026376{
26377 int sublen;
26378 regmatch_T regmatch;
26379 int i;
26380 int do_all;
26381 char_u *tail;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026382 char_u *end;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026383 garray_T ga;
26384 char_u *ret;
26385 char_u *save_cpo;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026386 char_u *zero_width = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026387
26388 /* Make 'cpoptions' empty, so that the 'l' flag doesn't work here */
26389 save_cpo = p_cpo;
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026390 p_cpo = empty_option;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026391
26392 ga_init2(&ga, 1, 200);
26393
26394 do_all = (flags[0] == 'g');
26395
26396 regmatch.rm_ic = p_ic;
26397 regmatch.regprog = vim_regcomp(pat, RE_MAGIC + RE_STRING);
26398 if (regmatch.regprog != NULL)
26399 {
26400 tail = str;
Bram Moolenaare90c8532014-11-05 16:03:44 +010026401 end = str + STRLEN(str);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026402 while (vim_regexec_nl(&regmatch, str, (colnr_T)(tail - str)))
26403 {
Bram Moolenaar8af26912014-01-23 20:09:34 +010026404 /* Skip empty match except for first match. */
26405 if (regmatch.startp[0] == regmatch.endp[0])
26406 {
26407 if (zero_width == regmatch.startp[0])
26408 {
26409 /* avoid getting stuck on a match with an empty string */
Bram Moolenaar8e7048c2014-06-12 18:39:22 +020026410 i = MB_PTR2LEN(tail);
26411 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail,
26412 (size_t)i);
26413 ga.ga_len += i;
26414 tail += i;
Bram Moolenaar8af26912014-01-23 20:09:34 +010026415 continue;
26416 }
26417 zero_width = regmatch.startp[0];
26418 }
26419
Bram Moolenaar071d4272004-06-13 20:20:40 +000026420 /*
26421 * Get some space for a temporary buffer to do the substitution
26422 * into. It will contain:
26423 * - The text up to where the match is.
26424 * - The substituted text.
26425 * - The text after the match.
26426 */
26427 sublen = vim_regsub(&regmatch, sub, tail, FALSE, TRUE, FALSE);
Bram Moolenaare90c8532014-11-05 16:03:44 +010026428 if (ga_grow(&ga, (int)((end - tail) + sublen -
Bram Moolenaar071d4272004-06-13 20:20:40 +000026429 (regmatch.endp[0] - regmatch.startp[0]))) == FAIL)
26430 {
26431 ga_clear(&ga);
26432 break;
26433 }
26434
26435 /* copy the text up to where the match is */
26436 i = (int)(regmatch.startp[0] - tail);
26437 mch_memmove((char_u *)ga.ga_data + ga.ga_len, tail, (size_t)i);
26438 /* add the substituted text */
26439 (void)vim_regsub(&regmatch, sub, (char_u *)ga.ga_data
26440 + ga.ga_len + i, TRUE, TRUE, FALSE);
26441 ga.ga_len += i + sublen - 1;
Bram Moolenaarceb84af2013-09-29 21:11:05 +020026442 tail = regmatch.endp[0];
26443 if (*tail == NUL)
26444 break;
Bram Moolenaar071d4272004-06-13 20:20:40 +000026445 if (!do_all)
26446 break;
26447 }
26448
26449 if (ga.ga_data != NULL)
26450 STRCPY((char *)ga.ga_data + ga.ga_len, tail);
26451
Bram Moolenaar473de612013-06-08 18:19:48 +020026452 vim_regfree(regmatch.regprog);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026453 }
26454
26455 ret = vim_strsave(ga.ga_data == NULL ? str : (char_u *)ga.ga_data);
26456 ga_clear(&ga);
Bram Moolenaar9c24ccc2008-07-14 21:05:15 +000026457 if (p_cpo == empty_option)
26458 p_cpo = save_cpo;
26459 else
26460 /* Darn, evaluating {sub} expression changed the value. */
26461 free_string_option(save_cpo);
Bram Moolenaar071d4272004-06-13 20:20:40 +000026462
26463 return ret;
26464}
26465
26466#endif /* defined(FEAT_MODIFY_FNAME) || defined(FEAT_EVAL) */